blob: a102b11eacf2276784ee11271919ad25e5b5fa0f [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);
623atomic_t trace_record_cmdline_disabled;
624
625static void trace_init_cmdlines(void)
626{
627 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
628 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
629 cmdline_idx = 0;
630}
631
Ingo Molnare309b412008-05-12 21:20:51 +0200632void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200633
Ingo Molnare309b412008-05-12 21:20:51 +0200634static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200635{
636 unsigned map;
637 unsigned idx;
638
639 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
640 return;
641
642 /*
643 * It's not the end of the world if we don't get
644 * the lock, but we also don't want to spin
645 * nor do we want to disable interrupts,
646 * so if we miss here, then better luck next time.
647 */
648 if (!spin_trylock(&trace_cmdline_lock))
649 return;
650
651 idx = map_pid_to_cmdline[tsk->pid];
652 if (idx >= SAVED_CMDLINES) {
653 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
654
655 map = map_cmdline_to_pid[idx];
656 if (map <= PID_MAX_DEFAULT)
657 map_pid_to_cmdline[map] = (unsigned)-1;
658
659 map_pid_to_cmdline[tsk->pid] = idx;
660
661 cmdline_idx = idx;
662 }
663
664 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
665
666 spin_unlock(&trace_cmdline_lock);
667}
668
Ingo Molnare309b412008-05-12 21:20:51 +0200669static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200670{
671 char *cmdline = "<...>";
672 unsigned map;
673
674 if (!pid)
675 return "<idle>";
676
677 if (pid > PID_MAX_DEFAULT)
678 goto out;
679
680 map = map_pid_to_cmdline[pid];
681 if (map >= SAVED_CMDLINES)
682 goto out;
683
684 cmdline = saved_cmdlines[map];
685
686 out:
687 return cmdline;
688}
689
Ingo Molnare309b412008-05-12 21:20:51 +0200690void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200691{
692 if (atomic_read(&trace_record_cmdline_disabled))
693 return;
694
695 trace_save_cmdline(tsk);
696}
697
Ingo Molnare309b412008-05-12 21:20:51 +0200698static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200699trace_next_list(struct trace_array_cpu *data, struct list_head *next)
700{
701 /*
702 * Roundrobin - but skip the head (which is not a real page):
703 */
704 next = next->next;
705 if (unlikely(next == &data->trace_pages))
706 next = next->next;
707 BUG_ON(next == &data->trace_pages);
708
709 return next;
710}
711
Ingo Molnare309b412008-05-12 21:20:51 +0200712static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200713trace_next_page(struct trace_array_cpu *data, void *addr)
714{
715 struct list_head *next;
716 struct page *page;
717
718 page = virt_to_page(addr);
719
720 next = trace_next_list(data, &page->lru);
721 page = list_entry(next, struct page, lru);
722
723 return page_address(page);
724}
725
Ingo Molnare309b412008-05-12 21:20:51 +0200726static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200727tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200728{
729 unsigned long idx, idx_next;
730 struct trace_entry *entry;
731
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200732 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200733 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200734 idx_next = idx + 1;
735
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200736 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
737
Steven Rostedt93a588f2008-05-12 21:20:45 +0200738 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200739
740 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200741 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200742 idx_next = 0;
743 }
744
Steven Rostedt93a588f2008-05-12 21:20:45 +0200745 if (data->trace_head == data->trace_tail &&
746 idx_next == data->trace_tail_idx) {
747 /* overrun */
748 data->trace_tail_idx++;
749 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
750 data->trace_tail =
751 trace_next_page(data, data->trace_tail);
752 data->trace_tail_idx = 0;
753 }
754 }
755
756 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200757
758 return entry;
759}
760
Ingo Molnare309b412008-05-12 21:20:51 +0200761static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200762tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200763{
764 struct task_struct *tsk = current;
765 unsigned long pc;
766
767 pc = preempt_count();
768
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200769 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200770 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200771 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200772 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
773 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
774 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
775 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
776}
777
Ingo Molnare309b412008-05-12 21:20:51 +0200778void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200779trace_function(struct trace_array *tr, struct trace_array_cpu *data,
780 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781{
782 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200783 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200784
Steven Rostedt92205c22008-05-12 21:20:55 +0200785 raw_local_irq_save(irq_flags);
786 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200787 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200788 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200789 entry->type = TRACE_FN;
790 entry->fn.ip = ip;
791 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200792 __raw_spin_unlock(&data->lock);
793 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200794}
795
Ingo Molnare309b412008-05-12 21:20:51 +0200796void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200797ftrace(struct trace_array *tr, struct trace_array_cpu *data,
798 unsigned long ip, unsigned long parent_ip, unsigned long flags)
799{
800 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200801 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200802}
803
Ingo Molnare309b412008-05-12 21:20:51 +0200804void
Ingo Molnar4e655512008-05-12 21:20:52 +0200805__trace_special(void *__tr, void *__data,
806 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200807{
Ingo Molnar4e655512008-05-12 21:20:52 +0200808 struct trace_array_cpu *data = __data;
809 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200810 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200811 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200812
Steven Rostedt92205c22008-05-12 21:20:55 +0200813 raw_local_irq_save(irq_flags);
814 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200815 entry = tracing_get_trace_entry(tr, data);
816 tracing_generic_entry_update(entry, 0);
817 entry->type = TRACE_SPECIAL;
818 entry->special.arg1 = arg1;
819 entry->special.arg2 = arg2;
820 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200821 __raw_spin_unlock(&data->lock);
822 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200823
824 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200825}
826
827void __trace_stack(struct trace_array *tr,
828 struct trace_array_cpu *data,
829 unsigned long flags,
830 int skip)
831{
832 struct trace_entry *entry;
833 struct stack_trace trace;
834
835 if (!(trace_flags & TRACE_ITER_STACKTRACE))
836 return;
837
838 entry = tracing_get_trace_entry(tr, data);
839 tracing_generic_entry_update(entry, flags);
840 entry->type = TRACE_STACK;
841
842 memset(&entry->stack, 0, sizeof(entry->stack));
843
844 trace.nr_entries = 0;
845 trace.max_entries = FTRACE_STACK_ENTRIES;
846 trace.skip = skip;
847 trace.entries = entry->stack.caller;
848
849 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200850}
851
Ingo Molnare309b412008-05-12 21:20:51 +0200852void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200853tracing_sched_switch_trace(struct trace_array *tr,
854 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200855 struct task_struct *prev,
856 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200857 unsigned long flags)
858{
859 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200860 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200861
Steven Rostedt92205c22008-05-12 21:20:55 +0200862 raw_local_irq_save(irq_flags);
863 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200864 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200865 tracing_generic_entry_update(entry, flags);
866 entry->type = TRACE_CTX;
867 entry->ctx.prev_pid = prev->pid;
868 entry->ctx.prev_prio = prev->prio;
869 entry->ctx.prev_state = prev->state;
870 entry->ctx.next_pid = next->pid;
871 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200872 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200873 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200874 __raw_spin_unlock(&data->lock);
875 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200876}
877
Ingo Molnar57422792008-05-12 21:20:51 +0200878void
879tracing_sched_wakeup_trace(struct trace_array *tr,
880 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200881 struct task_struct *wakee,
882 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200883 unsigned long flags)
884{
885 struct trace_entry *entry;
886 unsigned long irq_flags;
887
Steven Rostedt92205c22008-05-12 21:20:55 +0200888 raw_local_irq_save(irq_flags);
889 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200890 entry = tracing_get_trace_entry(tr, data);
891 tracing_generic_entry_update(entry, flags);
892 entry->type = TRACE_WAKE;
893 entry->ctx.prev_pid = curr->pid;
894 entry->ctx.prev_prio = curr->prio;
895 entry->ctx.prev_state = curr->state;
896 entry->ctx.next_pid = wakee->pid;
897 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200898 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200899 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200900 __raw_spin_unlock(&data->lock);
901 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200902
903 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200904}
905
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200906#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200907static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200908function_trace_call(unsigned long ip, unsigned long parent_ip)
909{
910 struct trace_array *tr = &global_trace;
911 struct trace_array_cpu *data;
912 unsigned long flags;
913 long disabled;
914 int cpu;
915
916 if (unlikely(!tracer_enabled))
917 return;
918
919 local_irq_save(flags);
920 cpu = raw_smp_processor_id();
921 data = tr->data[cpu];
922 disabled = atomic_inc_return(&data->disabled);
923
924 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200925 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200926
927 atomic_dec(&data->disabled);
928 local_irq_restore(flags);
929}
930
931static struct ftrace_ops trace_ops __read_mostly =
932{
933 .func = function_trace_call,
934};
935
Ingo Molnare309b412008-05-12 21:20:51 +0200936void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200937{
938 register_ftrace_function(&trace_ops);
939}
940
Ingo Molnare309b412008-05-12 21:20:51 +0200941void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200942{
943 unregister_ftrace_function(&trace_ops);
944}
945#endif
946
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200947enum trace_file_type {
948 TRACE_FILE_LAT_FMT = 1,
949};
950
951static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200952trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
953 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200954{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200955 struct page *page;
956 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200957
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200958 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200959 iter->next_idx[cpu] >= data->trace_idx ||
960 (data->trace_head == data->trace_tail &&
961 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200962 return NULL;
963
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200964 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200965 /* Initialize the iterator for this cpu trace buffer */
966 WARN_ON(!data->trace_tail);
967 page = virt_to_page(data->trace_tail);
968 iter->next_page[cpu] = &page->lru;
969 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200970 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200971
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200972 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200973 BUG_ON(&data->trace_pages == &page->lru);
974
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200975 array = page_address(page);
976
Steven Rostedt93a588f2008-05-12 21:20:45 +0200977 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200978 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200979}
980
Ingo Molnare309b412008-05-12 21:20:51 +0200981static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200982find_next_entry(struct trace_iterator *iter, int *ent_cpu)
983{
984 struct trace_array *tr = iter->tr;
985 struct trace_entry *ent, *next = NULL;
986 int next_cpu = -1;
987 int cpu;
988
Steven Rostedtab464282008-05-12 21:21:00 +0200989 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200990 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200991 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200992 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200993 /*
994 * Pick the entry with the smallest timestamp:
995 */
996 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200997 next = ent;
998 next_cpu = cpu;
999 }
1000 }
1001
1002 if (ent_cpu)
1003 *ent_cpu = next_cpu;
1004
1005 return next;
1006}
1007
Ingo Molnare309b412008-05-12 21:20:51 +02001008static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001009{
1010 iter->idx++;
1011 iter->next_idx[iter->cpu]++;
1012 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001013
Steven Rostedtb3806b42008-05-12 21:20:46 +02001014 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1015 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1016
1017 iter->next_page_idx[iter->cpu] = 0;
1018 iter->next_page[iter->cpu] =
1019 trace_next_list(data, iter->next_page[iter->cpu]);
1020 }
1021}
1022
Ingo Molnare309b412008-05-12 21:20:51 +02001023static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001024{
1025 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1026
1027 data->trace_tail_idx++;
1028 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1029 data->trace_tail = trace_next_page(data, data->trace_tail);
1030 data->trace_tail_idx = 0;
1031 }
1032
1033 /* Check if we empty it, then reset the index */
1034 if (data->trace_head == data->trace_tail &&
1035 data->trace_head_idx == data->trace_tail_idx)
1036 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001037}
1038
Ingo Molnare309b412008-05-12 21:20:51 +02001039static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001040{
1041 struct trace_entry *next;
1042 int next_cpu = -1;
1043
1044 next = find_next_entry(iter, &next_cpu);
1045
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001046 iter->prev_ent = iter->ent;
1047 iter->prev_cpu = iter->cpu;
1048
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001049 iter->ent = next;
1050 iter->cpu = next_cpu;
1051
Steven Rostedtb3806b42008-05-12 21:20:46 +02001052 if (next)
1053 trace_iterator_increment(iter);
1054
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001055 return next ? iter : NULL;
1056}
1057
Ingo Molnare309b412008-05-12 21:20:51 +02001058static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001059{
1060 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001061 void *last_ent = iter->ent;
1062 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001063 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001064
1065 (*pos)++;
1066
1067 /* can't go backwards */
1068 if (iter->idx > i)
1069 return NULL;
1070
1071 if (iter->idx < 0)
1072 ent = find_next_entry_inc(iter);
1073 else
1074 ent = iter;
1075
1076 while (ent && iter->idx < i)
1077 ent = find_next_entry_inc(iter);
1078
1079 iter->pos = *pos;
1080
1081 if (last_ent && !ent)
1082 seq_puts(m, "\n\nvim:ft=help\n");
1083
1084 return ent;
1085}
1086
1087static void *s_start(struct seq_file *m, loff_t *pos)
1088{
1089 struct trace_iterator *iter = m->private;
1090 void *p = NULL;
1091 loff_t l = 0;
1092 int i;
1093
1094 mutex_lock(&trace_types_lock);
1095
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001096 if (!current_trace || current_trace != iter->trace) {
1097 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001098 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001099 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001100
1101 atomic_inc(&trace_record_cmdline_disabled);
1102
1103 /* let the tracer grab locks here if needed */
1104 if (current_trace->start)
1105 current_trace->start(iter);
1106
1107 if (*pos != iter->pos) {
1108 iter->ent = NULL;
1109 iter->cpu = 0;
1110 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001111 iter->prev_ent = NULL;
1112 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001113
Steven Rostedtab464282008-05-12 21:21:00 +02001114 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001115 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001116 iter->next_page[i] = NULL;
1117 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118
1119 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1120 ;
1121
1122 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001123 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001124 p = s_next(m, p, &l);
1125 }
1126
1127 return p;
1128}
1129
1130static void s_stop(struct seq_file *m, void *p)
1131{
1132 struct trace_iterator *iter = m->private;
1133
1134 atomic_dec(&trace_record_cmdline_disabled);
1135
1136 /* let the tracer release locks here if needed */
1137 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1138 iter->trace->stop(iter);
1139
1140 mutex_unlock(&trace_types_lock);
1141}
1142
Steven Rostedtb3806b42008-05-12 21:20:46 +02001143static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001144seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001145{
1146#ifdef CONFIG_KALLSYMS
1147 char str[KSYM_SYMBOL_LEN];
1148
1149 kallsyms_lookup(address, NULL, NULL, NULL, str);
1150
Steven Rostedtb3806b42008-05-12 21:20:46 +02001151 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001152#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001153 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001154}
1155
Steven Rostedtb3806b42008-05-12 21:20:46 +02001156static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001157seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1158 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001159{
1160#ifdef CONFIG_KALLSYMS
1161 char str[KSYM_SYMBOL_LEN];
1162
1163 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001164 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001165#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001166 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167}
1168
1169#ifndef CONFIG_64BIT
1170# define IP_FMT "%08lx"
1171#else
1172# define IP_FMT "%016lx"
1173#endif
1174
Ingo Molnare309b412008-05-12 21:20:51 +02001175static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001176seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001178 int ret;
1179
1180 if (!ip)
1181 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001182
1183 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001184 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001186 ret = seq_print_sym_short(s, "%s", ip);
1187
1188 if (!ret)
1189 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001190
1191 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001192 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1193 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001194}
1195
Ingo Molnare309b412008-05-12 21:20:51 +02001196static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197{
1198 seq_puts(m, "# _------=> CPU# \n");
1199 seq_puts(m, "# / _-----=> irqs-off \n");
1200 seq_puts(m, "# | / _----=> need-resched \n");
1201 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1202 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1203 seq_puts(m, "# |||| / \n");
1204 seq_puts(m, "# ||||| delay \n");
1205 seq_puts(m, "# cmd pid ||||| time | caller \n");
1206 seq_puts(m, "# \\ / ||||| \\ | / \n");
1207}
1208
Ingo Molnare309b412008-05-12 21:20:51 +02001209static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001210{
1211 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1212 seq_puts(m, "# | | | | |\n");
1213}
1214
1215
Ingo Molnare309b412008-05-12 21:20:51 +02001216static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001217print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1218{
1219 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1220 struct trace_array *tr = iter->tr;
1221 struct trace_array_cpu *data = tr->data[tr->cpu];
1222 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001223 unsigned long total = 0;
1224 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001225 int cpu;
1226 const char *name = "preemption";
1227
1228 if (type)
1229 name = type->name;
1230
Steven Rostedtab464282008-05-12 21:21:00 +02001231 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001232 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001233 total += tr->data[cpu]->trace_idx;
1234 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001235 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001236 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001237 entries += tr->data[cpu]->trace_idx;
1238 }
1239 }
1240
1241 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1242 name, UTS_RELEASE);
1243 seq_puts(m, "-----------------------------------"
1244 "---------------------------------\n");
1245 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1246 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001247 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001248 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001249 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001250 tr->cpu,
1251#if defined(CONFIG_PREEMPT_NONE)
1252 "server",
1253#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1254 "desktop",
1255#elif defined(CONFIG_PREEMPT_DESKTOP)
1256 "preempt",
1257#else
1258 "unknown",
1259#endif
1260 /* These are reserved for later use */
1261 0, 0, 0, 0);
1262#ifdef CONFIG_SMP
1263 seq_printf(m, " #P:%d)\n", num_online_cpus());
1264#else
1265 seq_puts(m, ")\n");
1266#endif
1267 seq_puts(m, " -----------------\n");
1268 seq_printf(m, " | task: %.16s-%d "
1269 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1270 data->comm, data->pid, data->uid, data->nice,
1271 data->policy, data->rt_priority);
1272 seq_puts(m, " -----------------\n");
1273
1274 if (data->critical_start) {
1275 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001276 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1277 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001278 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001279 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1280 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001281 seq_puts(m, "\n");
1282 }
1283
1284 seq_puts(m, "\n");
1285}
1286
Ingo Molnare309b412008-05-12 21:20:51 +02001287static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001288lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001289{
1290 int hardirq, softirq;
1291 char *comm;
1292
1293 comm = trace_find_cmdline(entry->pid);
1294
Steven Rostedt214023c2008-05-12 21:20:46 +02001295 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1296 trace_seq_printf(s, "%d", cpu);
1297 trace_seq_printf(s, "%c%c",
1298 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1299 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001300
1301 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1302 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001303 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001304 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001305 } else {
1306 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001307 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001308 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001309 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001310 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001311 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001312 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001313 }
1314 }
1315
1316 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001317 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001319 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001320}
1321
1322unsigned long preempt_mark_thresh = 100;
1323
Ingo Molnare309b412008-05-12 21:20:51 +02001324static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001325lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001326 unsigned long rel_usecs)
1327{
Steven Rostedt214023c2008-05-12 21:20:46 +02001328 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001329 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001330 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001331 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001332 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001333 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001334 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001335}
1336
1337static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1338
Ingo Molnare309b412008-05-12 21:20:51 +02001339static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001340print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001341{
Steven Rostedt214023c2008-05-12 21:20:46 +02001342 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001343 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1344 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1345 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1346 struct trace_entry *entry = iter->ent;
1347 unsigned long abs_usecs;
1348 unsigned long rel_usecs;
1349 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001350 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001351 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001352 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353
1354 if (!next_entry)
1355 next_entry = entry;
1356 rel_usecs = ns2usecs(next_entry->t - entry->t);
1357 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1358
1359 if (verbose) {
1360 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001361 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1362 " %ld.%03ldms (+%ld.%03ldms): ",
1363 comm,
1364 entry->pid, cpu, entry->flags,
1365 entry->preempt_count, trace_idx,
1366 ns2usecs(entry->t),
1367 abs_usecs/1000,
1368 abs_usecs % 1000, rel_usecs/1000,
1369 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001371 lat_print_generic(s, entry, cpu);
1372 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373 }
1374 switch (entry->type) {
1375 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001376 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1377 trace_seq_puts(s, " (");
1378 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1379 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380 break;
1381 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001382 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001383 T = entry->ctx.next_state < sizeof(state_to_char) ?
1384 state_to_char[entry->ctx.next_state] : 'X';
1385
Ankita Gargd17d9692008-05-12 21:20:58 +02001386 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1387 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001388 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001389 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001390 entry->ctx.prev_pid,
1391 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001392 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001393 entry->ctx.next_pid,
1394 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001395 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001396 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001397 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001398 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001399 entry->special.arg1,
1400 entry->special.arg2,
1401 entry->special.arg3);
1402 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001403 case TRACE_STACK:
1404 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1405 if (i)
1406 trace_seq_puts(s, " <= ");
1407 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1408 }
1409 trace_seq_puts(s, "\n");
1410 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001411 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001412 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001413 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001414 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001415}
1416
Ingo Molnare309b412008-05-12 21:20:51 +02001417static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001418{
Steven Rostedt214023c2008-05-12 21:20:46 +02001419 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001420 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001421 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001422 unsigned long usec_rem;
1423 unsigned long long t;
1424 unsigned long secs;
1425 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001426 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001427 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001428 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001429
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001430 entry = iter->ent;
1431
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001432 comm = trace_find_cmdline(iter->ent->pid);
1433
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001434 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001435 usec_rem = do_div(t, 1000000ULL);
1436 secs = (unsigned long)t;
1437
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001438 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1439 if (!ret)
1440 return 0;
1441 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1442 if (!ret)
1443 return 0;
1444 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1445 if (!ret)
1446 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001447
1448 switch (entry->type) {
1449 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001450 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1451 if (!ret)
1452 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001453 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1454 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001455 ret = trace_seq_printf(s, " <-");
1456 if (!ret)
1457 return 0;
1458 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1459 sym_flags);
1460 if (!ret)
1461 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001462 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001463 ret = trace_seq_printf(s, "\n");
1464 if (!ret)
1465 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001466 break;
1467 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001468 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001469 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1470 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001471 T = entry->ctx.next_state < sizeof(state_to_char) ?
1472 state_to_char[entry->ctx.next_state] : 'X';
1473 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001474 entry->ctx.prev_pid,
1475 entry->ctx.prev_prio,
1476 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001477 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001478 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001479 entry->ctx.next_prio,
1480 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001481 if (!ret)
1482 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001483 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001484 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001485 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001486 entry->special.arg1,
1487 entry->special.arg2,
1488 entry->special.arg3);
1489 if (!ret)
1490 return 0;
1491 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001492 case TRACE_STACK:
1493 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1494 if (i) {
1495 ret = trace_seq_puts(s, " <= ");
1496 if (!ret)
1497 return 0;
1498 }
1499 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1500 sym_flags);
1501 if (!ret)
1502 return 0;
1503 }
1504 ret = trace_seq_puts(s, "\n");
1505 if (!ret)
1506 return 0;
1507 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001508 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001509 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001510}
1511
Ingo Molnare309b412008-05-12 21:20:51 +02001512static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001513{
1514 struct trace_seq *s = &iter->seq;
1515 struct trace_entry *entry;
1516 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001517 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001518
1519 entry = iter->ent;
1520
1521 ret = trace_seq_printf(s, "%d %d %llu ",
1522 entry->pid, iter->cpu, entry->t);
1523 if (!ret)
1524 return 0;
1525
1526 switch (entry->type) {
1527 case TRACE_FN:
1528 ret = trace_seq_printf(s, "%x %x\n",
1529 entry->fn.ip, entry->fn.parent_ip);
1530 if (!ret)
1531 return 0;
1532 break;
1533 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001534 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001535 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1536 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001537 T = entry->ctx.next_state < sizeof(state_to_char) ?
1538 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001539 if (entry->type == TRACE_WAKE)
1540 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001541 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001542 entry->ctx.prev_pid,
1543 entry->ctx.prev_prio,
1544 S,
1545 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001546 entry->ctx.next_prio,
1547 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001548 if (!ret)
1549 return 0;
1550 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001551 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001552 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001553 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001554 entry->special.arg1,
1555 entry->special.arg2,
1556 entry->special.arg3);
1557 if (!ret)
1558 return 0;
1559 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001560 }
1561 return 1;
1562}
1563
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001564#define SEQ_PUT_FIELD_RET(s, x) \
1565do { \
1566 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1567 return 0; \
1568} while (0)
1569
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001570#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1571do { \
1572 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1573 return 0; \
1574} while (0)
1575
Ingo Molnare309b412008-05-12 21:20:51 +02001576static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001577{
1578 struct trace_seq *s = &iter->seq;
1579 unsigned char newline = '\n';
1580 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001581 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001582
1583 entry = iter->ent;
1584
1585 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1586 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1587 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1588
1589 switch (entry->type) {
1590 case TRACE_FN:
1591 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1592 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1593 break;
1594 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001595 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001596 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1597 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001598 T = entry->ctx.next_state < sizeof(state_to_char) ?
1599 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001600 if (entry->type == TRACE_WAKE)
1601 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001602 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1603 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1604 SEQ_PUT_HEX_FIELD_RET(s, S);
1605 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1606 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1607 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001608 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001609 break;
1610 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001611 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001612 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1613 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1614 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1615 break;
1616 }
1617 SEQ_PUT_FIELD_RET(s, newline);
1618
1619 return 1;
1620}
1621
Ingo Molnare309b412008-05-12 21:20:51 +02001622static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001623{
1624 struct trace_seq *s = &iter->seq;
1625 struct trace_entry *entry;
1626
1627 entry = iter->ent;
1628
1629 SEQ_PUT_FIELD_RET(s, entry->pid);
1630 SEQ_PUT_FIELD_RET(s, entry->cpu);
1631 SEQ_PUT_FIELD_RET(s, entry->t);
1632
1633 switch (entry->type) {
1634 case TRACE_FN:
1635 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1636 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1637 break;
1638 case TRACE_CTX:
1639 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1640 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1641 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1642 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1643 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001644 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001645 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001646 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001647 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001648 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1649 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1650 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1651 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001652 }
1653 return 1;
1654}
1655
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001656static int trace_empty(struct trace_iterator *iter)
1657{
1658 struct trace_array_cpu *data;
1659 int cpu;
1660
Steven Rostedtab464282008-05-12 21:21:00 +02001661 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001662 data = iter->tr->data[cpu];
1663
Steven Rostedtb3806b42008-05-12 21:20:46 +02001664 if (head_page(data) && data->trace_idx &&
1665 (data->trace_tail != data->trace_head ||
1666 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001667 return 0;
1668 }
1669 return 1;
1670}
1671
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001672static int print_trace_line(struct trace_iterator *iter)
1673{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001674 if (iter->trace && iter->trace->print_line)
1675 return iter->trace->print_line(iter);
1676
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001677 if (trace_flags & TRACE_ITER_BIN)
1678 return print_bin_fmt(iter);
1679
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001680 if (trace_flags & TRACE_ITER_HEX)
1681 return print_hex_fmt(iter);
1682
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001683 if (trace_flags & TRACE_ITER_RAW)
1684 return print_raw_fmt(iter);
1685
1686 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1687 return print_lat_fmt(iter, iter->idx, iter->cpu);
1688
1689 return print_trace_fmt(iter);
1690}
1691
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001692static int s_show(struct seq_file *m, void *v)
1693{
1694 struct trace_iterator *iter = v;
1695
1696 if (iter->ent == NULL) {
1697 if (iter->tr) {
1698 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1699 seq_puts(m, "#\n");
1700 }
1701 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1702 /* print nothing if the buffers are empty */
1703 if (trace_empty(iter))
1704 return 0;
1705 print_trace_header(m, iter);
1706 if (!(trace_flags & TRACE_ITER_VERBOSE))
1707 print_lat_help_header(m);
1708 } else {
1709 if (!(trace_flags & TRACE_ITER_VERBOSE))
1710 print_func_help_header(m);
1711 }
1712 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001713 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001714 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001715 }
1716
1717 return 0;
1718}
1719
1720static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001721 .start = s_start,
1722 .next = s_next,
1723 .stop = s_stop,
1724 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001725};
1726
Ingo Molnare309b412008-05-12 21:20:51 +02001727static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001728__tracing_open(struct inode *inode, struct file *file, int *ret)
1729{
1730 struct trace_iterator *iter;
1731
Steven Rostedt60a11772008-05-12 21:20:44 +02001732 if (tracing_disabled) {
1733 *ret = -ENODEV;
1734 return NULL;
1735 }
1736
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001737 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1738 if (!iter) {
1739 *ret = -ENOMEM;
1740 goto out;
1741 }
1742
1743 mutex_lock(&trace_types_lock);
1744 if (current_trace && current_trace->print_max)
1745 iter->tr = &max_tr;
1746 else
1747 iter->tr = inode->i_private;
1748 iter->trace = current_trace;
1749 iter->pos = -1;
1750
1751 /* TODO stop tracer */
1752 *ret = seq_open(file, &tracer_seq_ops);
1753 if (!*ret) {
1754 struct seq_file *m = file->private_data;
1755 m->private = iter;
1756
1757 /* stop the trace while dumping */
1758 if (iter->tr->ctrl)
1759 tracer_enabled = 0;
1760
1761 if (iter->trace && iter->trace->open)
1762 iter->trace->open(iter);
1763 } else {
1764 kfree(iter);
1765 iter = NULL;
1766 }
1767 mutex_unlock(&trace_types_lock);
1768
1769 out:
1770 return iter;
1771}
1772
1773int tracing_open_generic(struct inode *inode, struct file *filp)
1774{
Steven Rostedt60a11772008-05-12 21:20:44 +02001775 if (tracing_disabled)
1776 return -ENODEV;
1777
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001778 filp->private_data = inode->i_private;
1779 return 0;
1780}
1781
1782int tracing_release(struct inode *inode, struct file *file)
1783{
1784 struct seq_file *m = (struct seq_file *)file->private_data;
1785 struct trace_iterator *iter = m->private;
1786
1787 mutex_lock(&trace_types_lock);
1788 if (iter->trace && iter->trace->close)
1789 iter->trace->close(iter);
1790
1791 /* reenable tracing if it was previously enabled */
1792 if (iter->tr->ctrl)
1793 tracer_enabled = 1;
1794 mutex_unlock(&trace_types_lock);
1795
1796 seq_release(inode, file);
1797 kfree(iter);
1798 return 0;
1799}
1800
1801static int tracing_open(struct inode *inode, struct file *file)
1802{
1803 int ret;
1804
1805 __tracing_open(inode, file, &ret);
1806
1807 return ret;
1808}
1809
1810static int tracing_lt_open(struct inode *inode, struct file *file)
1811{
1812 struct trace_iterator *iter;
1813 int ret;
1814
1815 iter = __tracing_open(inode, file, &ret);
1816
1817 if (!ret)
1818 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1819
1820 return ret;
1821}
1822
1823
Ingo Molnare309b412008-05-12 21:20:51 +02001824static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001825t_next(struct seq_file *m, void *v, loff_t *pos)
1826{
1827 struct tracer *t = m->private;
1828
1829 (*pos)++;
1830
1831 if (t)
1832 t = t->next;
1833
1834 m->private = t;
1835
1836 return t;
1837}
1838
1839static void *t_start(struct seq_file *m, loff_t *pos)
1840{
1841 struct tracer *t = m->private;
1842 loff_t l = 0;
1843
1844 mutex_lock(&trace_types_lock);
1845 for (; t && l < *pos; t = t_next(m, t, &l))
1846 ;
1847
1848 return t;
1849}
1850
1851static void t_stop(struct seq_file *m, void *p)
1852{
1853 mutex_unlock(&trace_types_lock);
1854}
1855
1856static int t_show(struct seq_file *m, void *v)
1857{
1858 struct tracer *t = v;
1859
1860 if (!t)
1861 return 0;
1862
1863 seq_printf(m, "%s", t->name);
1864 if (t->next)
1865 seq_putc(m, ' ');
1866 else
1867 seq_putc(m, '\n');
1868
1869 return 0;
1870}
1871
1872static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001873 .start = t_start,
1874 .next = t_next,
1875 .stop = t_stop,
1876 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001877};
1878
1879static int show_traces_open(struct inode *inode, struct file *file)
1880{
1881 int ret;
1882
Steven Rostedt60a11772008-05-12 21:20:44 +02001883 if (tracing_disabled)
1884 return -ENODEV;
1885
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001886 ret = seq_open(file, &show_traces_seq_ops);
1887 if (!ret) {
1888 struct seq_file *m = file->private_data;
1889 m->private = trace_types;
1890 }
1891
1892 return ret;
1893}
1894
1895static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001896 .open = tracing_open,
1897 .read = seq_read,
1898 .llseek = seq_lseek,
1899 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001900};
1901
1902static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001903 .open = tracing_lt_open,
1904 .read = seq_read,
1905 .llseek = seq_lseek,
1906 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001907};
1908
1909static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001910 .open = show_traces_open,
1911 .read = seq_read,
1912 .release = seq_release,
1913};
1914
Ingo Molnar36dfe922008-05-12 21:20:52 +02001915/*
1916 * Only trace on a CPU if the bitmask is set:
1917 */
1918static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1919
1920/*
1921 * When tracing/tracing_cpu_mask is modified then this holds
1922 * the new bitmask we are about to install:
1923 */
1924static cpumask_t tracing_cpumask_new;
1925
1926/*
1927 * The tracer itself will not take this lock, but still we want
1928 * to provide a consistent cpumask to user-space:
1929 */
1930static DEFINE_MUTEX(tracing_cpumask_update_lock);
1931
1932/*
1933 * Temporary storage for the character representation of the
1934 * CPU bitmask (and one more byte for the newline):
1935 */
1936static char mask_str[NR_CPUS + 1];
1937
Ingo Molnarc7078de2008-05-12 21:20:52 +02001938static ssize_t
1939tracing_cpumask_read(struct file *filp, char __user *ubuf,
1940 size_t count, loff_t *ppos)
1941{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001942 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001943
1944 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001945
1946 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1947 if (count - len < 2) {
1948 count = -EINVAL;
1949 goto out_err;
1950 }
1951 len += sprintf(mask_str + len, "\n");
1952 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1953
1954out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001955 mutex_unlock(&tracing_cpumask_update_lock);
1956
1957 return count;
1958}
1959
1960static ssize_t
1961tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1962 size_t count, loff_t *ppos)
1963{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001964 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001965
1966 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001967 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1968 if (err)
1969 goto err_unlock;
1970
Steven Rostedt92205c22008-05-12 21:20:55 +02001971 raw_local_irq_disable();
1972 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001973 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001974 /*
1975 * Increase/decrease the disabled counter if we are
1976 * about to flip a bit in the cpumask:
1977 */
1978 if (cpu_isset(cpu, tracing_cpumask) &&
1979 !cpu_isset(cpu, tracing_cpumask_new)) {
1980 atomic_inc(&global_trace.data[cpu]->disabled);
1981 }
1982 if (!cpu_isset(cpu, tracing_cpumask) &&
1983 cpu_isset(cpu, tracing_cpumask_new)) {
1984 atomic_dec(&global_trace.data[cpu]->disabled);
1985 }
1986 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001987 __raw_spin_unlock(&ftrace_max_lock);
1988 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001989
1990 tracing_cpumask = tracing_cpumask_new;
1991
Ingo Molnarc7078de2008-05-12 21:20:52 +02001992 mutex_unlock(&tracing_cpumask_update_lock);
1993
Ingo Molnarc7078de2008-05-12 21:20:52 +02001994 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001995
1996err_unlock:
1997 mutex_unlock(&tracing_cpumask_update_lock);
1998
1999 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002000}
2001
2002static struct file_operations tracing_cpumask_fops = {
2003 .open = tracing_open_generic,
2004 .read = tracing_cpumask_read,
2005 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002006};
2007
2008static ssize_t
2009tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2010 size_t cnt, loff_t *ppos)
2011{
2012 char *buf;
2013 int r = 0;
2014 int len = 0;
2015 int i;
2016
2017 /* calulate max size */
2018 for (i = 0; trace_options[i]; i++) {
2019 len += strlen(trace_options[i]);
2020 len += 3; /* "no" and space */
2021 }
2022
2023 /* +2 for \n and \0 */
2024 buf = kmalloc(len + 2, GFP_KERNEL);
2025 if (!buf)
2026 return -ENOMEM;
2027
2028 for (i = 0; trace_options[i]; i++) {
2029 if (trace_flags & (1 << i))
2030 r += sprintf(buf + r, "%s ", trace_options[i]);
2031 else
2032 r += sprintf(buf + r, "no%s ", trace_options[i]);
2033 }
2034
2035 r += sprintf(buf + r, "\n");
2036 WARN_ON(r >= len + 2);
2037
Ingo Molnar36dfe922008-05-12 21:20:52 +02002038 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002039
2040 kfree(buf);
2041
2042 return r;
2043}
2044
2045static ssize_t
2046tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2047 size_t cnt, loff_t *ppos)
2048{
2049 char buf[64];
2050 char *cmp = buf;
2051 int neg = 0;
2052 int i;
2053
Steven Rostedtcffae432008-05-12 21:21:00 +02002054 if (cnt >= sizeof(buf))
2055 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002056
2057 if (copy_from_user(&buf, ubuf, cnt))
2058 return -EFAULT;
2059
2060 buf[cnt] = 0;
2061
2062 if (strncmp(buf, "no", 2) == 0) {
2063 neg = 1;
2064 cmp += 2;
2065 }
2066
2067 for (i = 0; trace_options[i]; i++) {
2068 int len = strlen(trace_options[i]);
2069
2070 if (strncmp(cmp, trace_options[i], len) == 0) {
2071 if (neg)
2072 trace_flags &= ~(1 << i);
2073 else
2074 trace_flags |= (1 << i);
2075 break;
2076 }
2077 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002078 /*
2079 * If no option could be set, return an error:
2080 */
2081 if (!trace_options[i])
2082 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002083
2084 filp->f_pos += cnt;
2085
2086 return cnt;
2087}
2088
2089static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002090 .open = tracing_open_generic,
2091 .read = tracing_iter_ctrl_read,
2092 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002093};
2094
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002095static const char readme_msg[] =
2096 "tracing mini-HOWTO:\n\n"
2097 "# mkdir /debug\n"
2098 "# mount -t debugfs nodev /debug\n\n"
2099 "# cat /debug/tracing/available_tracers\n"
2100 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2101 "# cat /debug/tracing/current_tracer\n"
2102 "none\n"
2103 "# echo sched_switch > /debug/tracing/current_tracer\n"
2104 "# cat /debug/tracing/current_tracer\n"
2105 "sched_switch\n"
2106 "# cat /debug/tracing/iter_ctrl\n"
2107 "noprint-parent nosym-offset nosym-addr noverbose\n"
2108 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2109 "# echo 1 > /debug/tracing/tracing_enabled\n"
2110 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2111 "echo 0 > /debug/tracing/tracing_enabled\n"
2112;
2113
2114static ssize_t
2115tracing_readme_read(struct file *filp, char __user *ubuf,
2116 size_t cnt, loff_t *ppos)
2117{
2118 return simple_read_from_buffer(ubuf, cnt, ppos,
2119 readme_msg, strlen(readme_msg));
2120}
2121
2122static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002123 .open = tracing_open_generic,
2124 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002125};
2126
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002127static ssize_t
2128tracing_ctrl_read(struct file *filp, char __user *ubuf,
2129 size_t cnt, loff_t *ppos)
2130{
2131 struct trace_array *tr = filp->private_data;
2132 char buf[64];
2133 int r;
2134
2135 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002136 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002137}
2138
2139static ssize_t
2140tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2141 size_t cnt, loff_t *ppos)
2142{
2143 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002144 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002145 long val;
2146 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002147
Steven Rostedtcffae432008-05-12 21:21:00 +02002148 if (cnt >= sizeof(buf))
2149 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002150
2151 if (copy_from_user(&buf, ubuf, cnt))
2152 return -EFAULT;
2153
2154 buf[cnt] = 0;
2155
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002156 ret = strict_strtoul(buf, 10, &val);
2157 if (ret < 0)
2158 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002159
2160 val = !!val;
2161
2162 mutex_lock(&trace_types_lock);
2163 if (tr->ctrl ^ val) {
2164 if (val)
2165 tracer_enabled = 1;
2166 else
2167 tracer_enabled = 0;
2168
2169 tr->ctrl = val;
2170
2171 if (current_trace && current_trace->ctrl_update)
2172 current_trace->ctrl_update(tr);
2173 }
2174 mutex_unlock(&trace_types_lock);
2175
2176 filp->f_pos += cnt;
2177
2178 return cnt;
2179}
2180
2181static ssize_t
2182tracing_set_trace_read(struct file *filp, char __user *ubuf,
2183 size_t cnt, loff_t *ppos)
2184{
2185 char buf[max_tracer_type_len+2];
2186 int r;
2187
2188 mutex_lock(&trace_types_lock);
2189 if (current_trace)
2190 r = sprintf(buf, "%s\n", current_trace->name);
2191 else
2192 r = sprintf(buf, "\n");
2193 mutex_unlock(&trace_types_lock);
2194
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002195 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002196}
2197
2198static ssize_t
2199tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2200 size_t cnt, loff_t *ppos)
2201{
2202 struct trace_array *tr = &global_trace;
2203 struct tracer *t;
2204 char buf[max_tracer_type_len+1];
2205 int i;
2206
2207 if (cnt > max_tracer_type_len)
2208 cnt = max_tracer_type_len;
2209
2210 if (copy_from_user(&buf, ubuf, cnt))
2211 return -EFAULT;
2212
2213 buf[cnt] = 0;
2214
2215 /* strip ending whitespace. */
2216 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2217 buf[i] = 0;
2218
2219 mutex_lock(&trace_types_lock);
2220 for (t = trace_types; t; t = t->next) {
2221 if (strcmp(t->name, buf) == 0)
2222 break;
2223 }
2224 if (!t || t == current_trace)
2225 goto out;
2226
2227 if (current_trace && current_trace->reset)
2228 current_trace->reset(tr);
2229
2230 current_trace = t;
2231 if (t->init)
2232 t->init(tr);
2233
2234 out:
2235 mutex_unlock(&trace_types_lock);
2236
2237 filp->f_pos += cnt;
2238
2239 return cnt;
2240}
2241
2242static ssize_t
2243tracing_max_lat_read(struct file *filp, char __user *ubuf,
2244 size_t cnt, loff_t *ppos)
2245{
2246 unsigned long *ptr = filp->private_data;
2247 char buf[64];
2248 int r;
2249
Steven Rostedtcffae432008-05-12 21:21:00 +02002250 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002251 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002252 if (r > sizeof(buf))
2253 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002254 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255}
2256
2257static ssize_t
2258tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2259 size_t cnt, loff_t *ppos)
2260{
2261 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002262 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002263 long val;
2264 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002265
Steven Rostedtcffae432008-05-12 21:21:00 +02002266 if (cnt >= sizeof(buf))
2267 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002268
2269 if (copy_from_user(&buf, ubuf, cnt))
2270 return -EFAULT;
2271
2272 buf[cnt] = 0;
2273
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002274 ret = strict_strtoul(buf, 10, &val);
2275 if (ret < 0)
2276 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002277
2278 *ptr = val * 1000;
2279
2280 return cnt;
2281}
2282
Steven Rostedtb3806b42008-05-12 21:20:46 +02002283static atomic_t tracing_reader;
2284
2285static int tracing_open_pipe(struct inode *inode, struct file *filp)
2286{
2287 struct trace_iterator *iter;
2288
2289 if (tracing_disabled)
2290 return -ENODEV;
2291
2292 /* We only allow for reader of the pipe */
2293 if (atomic_inc_return(&tracing_reader) != 1) {
2294 atomic_dec(&tracing_reader);
2295 return -EBUSY;
2296 }
2297
2298 /* create a buffer to store the information to pass to userspace */
2299 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2300 if (!iter)
2301 return -ENOMEM;
2302
2303 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002304 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002305
2306 filp->private_data = iter;
2307
2308 return 0;
2309}
2310
2311static int tracing_release_pipe(struct inode *inode, struct file *file)
2312{
2313 struct trace_iterator *iter = file->private_data;
2314
2315 kfree(iter);
2316 atomic_dec(&tracing_reader);
2317
2318 return 0;
2319}
2320
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002321static unsigned int
2322tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2323{
2324 struct trace_iterator *iter = filp->private_data;
2325
2326 if (trace_flags & TRACE_ITER_BLOCK) {
2327 /*
2328 * Always select as readable when in blocking mode
2329 */
2330 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002331 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002332 if (!trace_empty(iter))
2333 return POLLIN | POLLRDNORM;
2334 poll_wait(filp, &trace_wait, poll_table);
2335 if (!trace_empty(iter))
2336 return POLLIN | POLLRDNORM;
2337
2338 return 0;
2339 }
2340}
2341
Steven Rostedtb3806b42008-05-12 21:20:46 +02002342/*
2343 * Consumer reader.
2344 */
2345static ssize_t
2346tracing_read_pipe(struct file *filp, char __user *ubuf,
2347 size_t cnt, loff_t *ppos)
2348{
2349 struct trace_iterator *iter = filp->private_data;
2350 struct trace_array_cpu *data;
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002351 struct trace_array *tr = iter->tr;
2352 struct tracer *tracer = iter->trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002353 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002354 static int start;
2355 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002356#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002357 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002358#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002359 int read = 0;
2360 int cpu;
2361 int len;
2362 int ret;
2363
2364 /* return any leftover data */
2365 if (iter->seq.len > start) {
2366 len = iter->seq.len - start;
2367 if (cnt > len)
2368 cnt = len;
2369 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2370 if (ret)
2371 cnt = -EFAULT;
2372
2373 start += len;
2374
2375 return cnt;
2376 }
2377
2378 trace_seq_reset(&iter->seq);
2379 start = 0;
2380
2381 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002382
2383 if ((filp->f_flags & O_NONBLOCK))
2384 return -EAGAIN;
2385
Steven Rostedtb3806b42008-05-12 21:20:46 +02002386 /*
2387 * This is a make-shift waitqueue. The reason we don't use
2388 * an actual wait queue is because:
2389 * 1) we only ever have one waiter
2390 * 2) the tracing, traces all functions, we don't want
2391 * the overhead of calling wake_up and friends
2392 * (and tracing them too)
2393 * Anyway, this is really very primitive wakeup.
2394 */
2395 set_current_state(TASK_INTERRUPTIBLE);
2396 iter->tr->waiter = current;
2397
2398 /* sleep for one second, and try again. */
2399 schedule_timeout(HZ);
2400
2401 iter->tr->waiter = NULL;
2402
2403 if (signal_pending(current))
2404 return -EINTR;
2405
Steven Rostedt84527992008-05-12 21:20:58 +02002406 if (iter->trace != current_trace)
2407 return 0;
2408
Steven Rostedtb3806b42008-05-12 21:20:46 +02002409 /*
2410 * We block until we read something and tracing is disabled.
2411 * We still block if tracing is disabled, but we have never
2412 * read anything. This allows a user to cat this file, and
2413 * then enable tracing. But after we have read something,
2414 * we give an EOF when tracing is again disabled.
2415 *
2416 * iter->pos will be 0 if we haven't read anything.
2417 */
2418 if (!tracer_enabled && iter->pos)
2419 break;
2420
2421 continue;
2422 }
2423
2424 /* stop when tracing is finished */
2425 if (trace_empty(iter))
2426 return 0;
2427
2428 if (cnt >= PAGE_SIZE)
2429 cnt = PAGE_SIZE - 1;
2430
2431 memset(iter, 0, sizeof(*iter));
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002432 iter->tr = tr;
2433 iter->trace = tracer;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002434 iter->pos = -1;
2435
2436 /*
2437 * We need to stop all tracing on all CPUS to read the
2438 * the next buffer. This is a bit expensive, but is
2439 * not done often. We fill all what we can read,
2440 * and then release the locks again.
2441 */
2442
2443 cpus_clear(mask);
2444 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002445#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002446 ftrace_save = ftrace_enabled;
2447 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002448#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002449 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002450 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002451 data = iter->tr->data[cpu];
2452
2453 if (!head_page(data) || !data->trace_idx)
2454 continue;
2455
2456 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002457 cpu_set(cpu, mask);
2458 }
2459
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002460 for_each_cpu_mask(cpu, mask) {
2461 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002462 __raw_spin_lock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002463 }
2464
Steven Rostedt088b1e422008-05-12 21:20:48 +02002465 while (find_next_entry_inc(iter) != NULL) {
2466 int len = iter->seq.len;
2467
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002468 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002469 if (!ret) {
2470 /* don't print partial lines */
2471 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002472 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002473 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002474
2475 trace_consume(iter);
2476
2477 if (iter->seq.len >= cnt)
2478 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002479 }
2480
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002481 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002482 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002483 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002484 }
2485
2486 for_each_cpu_mask(cpu, mask) {
2487 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002488 atomic_dec(&data->disabled);
2489 }
Ingo Molnar25770462008-05-12 21:20:49 +02002490#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002491 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002492#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002493 local_irq_restore(flags);
2494
2495 /* Now copy what we have to the user */
2496 read = iter->seq.len;
2497 if (read > cnt)
2498 read = cnt;
2499
2500 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2501
2502 if (read < iter->seq.len)
2503 start = read;
2504 else
2505 trace_seq_reset(&iter->seq);
2506
2507 if (ret)
2508 read = -EFAULT;
2509
2510 return read;
2511}
2512
Steven Rostedta98a3c32008-05-12 21:20:59 +02002513static ssize_t
2514tracing_entries_read(struct file *filp, char __user *ubuf,
2515 size_t cnt, loff_t *ppos)
2516{
2517 struct trace_array *tr = filp->private_data;
2518 char buf[64];
2519 int r;
2520
2521 r = sprintf(buf, "%lu\n", tr->entries);
2522 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2523}
2524
2525static ssize_t
2526tracing_entries_write(struct file *filp, const char __user *ubuf,
2527 size_t cnt, loff_t *ppos)
2528{
2529 unsigned long val;
2530 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002531 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002532
Steven Rostedtcffae432008-05-12 21:21:00 +02002533 if (cnt >= sizeof(buf))
2534 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002535
2536 if (copy_from_user(&buf, ubuf, cnt))
2537 return -EFAULT;
2538
2539 buf[cnt] = 0;
2540
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002541 ret = strict_strtoul(buf, 10, &val);
2542 if (ret < 0)
2543 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002544
2545 /* must have at least 1 entry */
2546 if (!val)
2547 return -EINVAL;
2548
2549 mutex_lock(&trace_types_lock);
2550
2551 if (current_trace != &no_tracer) {
2552 cnt = -EBUSY;
2553 pr_info("ftrace: set current_tracer to none"
2554 " before modifying buffer size\n");
2555 goto out;
2556 }
2557
2558 if (val > global_trace.entries) {
2559 while (global_trace.entries < val) {
2560 if (trace_alloc_page()) {
2561 cnt = -ENOMEM;
2562 goto out;
2563 }
2564 }
2565 } else {
2566 /* include the number of entries in val (inc of page entries) */
2567 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2568 trace_free_page();
2569 }
2570
2571 filp->f_pos += cnt;
2572
2573 out:
2574 max_tr.entries = global_trace.entries;
2575 mutex_unlock(&trace_types_lock);
2576
2577 return cnt;
2578}
2579
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002580static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002581 .open = tracing_open_generic,
2582 .read = tracing_max_lat_read,
2583 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002584};
2585
2586static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002587 .open = tracing_open_generic,
2588 .read = tracing_ctrl_read,
2589 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002590};
2591
2592static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002593 .open = tracing_open_generic,
2594 .read = tracing_set_trace_read,
2595 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002596};
2597
Steven Rostedtb3806b42008-05-12 21:20:46 +02002598static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002599 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002600 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002601 .read = tracing_read_pipe,
2602 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002603};
2604
Steven Rostedta98a3c32008-05-12 21:20:59 +02002605static struct file_operations tracing_entries_fops = {
2606 .open = tracing_open_generic,
2607 .read = tracing_entries_read,
2608 .write = tracing_entries_write,
2609};
2610
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002611#ifdef CONFIG_DYNAMIC_FTRACE
2612
2613static ssize_t
2614tracing_read_long(struct file *filp, char __user *ubuf,
2615 size_t cnt, loff_t *ppos)
2616{
2617 unsigned long *p = filp->private_data;
2618 char buf[64];
2619 int r;
2620
2621 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002622
2623 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002624}
2625
2626static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002627 .open = tracing_open_generic,
2628 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002629};
2630#endif
2631
2632static struct dentry *d_tracer;
2633
2634struct dentry *tracing_init_dentry(void)
2635{
2636 static int once;
2637
2638 if (d_tracer)
2639 return d_tracer;
2640
2641 d_tracer = debugfs_create_dir("tracing", NULL);
2642
2643 if (!d_tracer && !once) {
2644 once = 1;
2645 pr_warning("Could not create debugfs directory 'tracing'\n");
2646 return NULL;
2647 }
2648
2649 return d_tracer;
2650}
2651
Steven Rostedt60a11772008-05-12 21:20:44 +02002652#ifdef CONFIG_FTRACE_SELFTEST
2653/* Let selftest have access to static functions in this file */
2654#include "trace_selftest.c"
2655#endif
2656
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002657static __init void tracer_init_debugfs(void)
2658{
2659 struct dentry *d_tracer;
2660 struct dentry *entry;
2661
2662 d_tracer = tracing_init_dentry();
2663
2664 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2665 &global_trace, &tracing_ctrl_fops);
2666 if (!entry)
2667 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2668
2669 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2670 NULL, &tracing_iter_fops);
2671 if (!entry)
2672 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2673
Ingo Molnarc7078de2008-05-12 21:20:52 +02002674 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2675 NULL, &tracing_cpumask_fops);
2676 if (!entry)
2677 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2678
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002679 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2680 &global_trace, &tracing_lt_fops);
2681 if (!entry)
2682 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2683
2684 entry = debugfs_create_file("trace", 0444, d_tracer,
2685 &global_trace, &tracing_fops);
2686 if (!entry)
2687 pr_warning("Could not create debugfs 'trace' entry\n");
2688
2689 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2690 &global_trace, &show_traces_fops);
2691 if (!entry)
2692 pr_warning("Could not create debugfs 'trace' entry\n");
2693
2694 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2695 &global_trace, &set_tracer_fops);
2696 if (!entry)
2697 pr_warning("Could not create debugfs 'trace' entry\n");
2698
2699 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2700 &tracing_max_latency,
2701 &tracing_max_lat_fops);
2702 if (!entry)
2703 pr_warning("Could not create debugfs "
2704 "'tracing_max_latency' entry\n");
2705
2706 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2707 &tracing_thresh, &tracing_max_lat_fops);
2708 if (!entry)
2709 pr_warning("Could not create debugfs "
2710 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002711 entry = debugfs_create_file("README", 0644, d_tracer,
2712 NULL, &tracing_readme_fops);
2713 if (!entry)
2714 pr_warning("Could not create debugfs 'README' entry\n");
2715
Steven Rostedtb3806b42008-05-12 21:20:46 +02002716 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2717 NULL, &tracing_pipe_fops);
2718 if (!entry)
2719 pr_warning("Could not create debugfs "
2720 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002721
Steven Rostedta98a3c32008-05-12 21:20:59 +02002722 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2723 &global_trace, &tracing_entries_fops);
2724 if (!entry)
2725 pr_warning("Could not create debugfs "
2726 "'tracing_threash' entry\n");
2727
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002728#ifdef CONFIG_DYNAMIC_FTRACE
2729 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2730 &ftrace_update_tot_cnt,
2731 &tracing_read_long_fops);
2732 if (!entry)
2733 pr_warning("Could not create debugfs "
2734 "'dyn_ftrace_total_info' entry\n");
2735#endif
2736}
2737
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002738static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002739{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002740 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002741 struct page *page, *tmp;
2742 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002743 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002744 int i;
2745
2746 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002747 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002748 array = (void *)__get_free_page(GFP_KERNEL);
2749 if (array == NULL) {
2750 printk(KERN_ERR "tracer: failed to allocate page"
2751 "for trace buffer!\n");
2752 goto free_pages;
2753 }
2754
2755 page = virt_to_page(array);
2756 list_add(&page->lru, &pages);
2757
2758/* Only allocate if we are actually using the max trace */
2759#ifdef CONFIG_TRACER_MAX_TRACE
2760 array = (void *)__get_free_page(GFP_KERNEL);
2761 if (array == NULL) {
2762 printk(KERN_ERR "tracer: failed to allocate page"
2763 "for trace buffer!\n");
2764 goto free_pages;
2765 }
2766 page = virt_to_page(array);
2767 list_add(&page->lru, &pages);
2768#endif
2769 }
2770
2771 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002772 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002773 data = global_trace.data[i];
2774 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002775 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002776 list_add_tail(&page->lru, &data->trace_pages);
2777 ClearPageLRU(page);
2778
2779#ifdef CONFIG_TRACER_MAX_TRACE
2780 data = max_tr.data[i];
2781 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002782 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002783 list_add_tail(&page->lru, &data->trace_pages);
2784 SetPageLRU(page);
2785#endif
2786 }
2787 global_trace.entries += ENTRIES_PER_PAGE;
2788
2789 return 0;
2790
2791 free_pages:
2792 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002793 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002794 __free_page(page);
2795 }
2796 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002797}
2798
Steven Rostedta98a3c32008-05-12 21:20:59 +02002799static int trace_free_page(void)
2800{
2801 struct trace_array_cpu *data;
2802 struct page *page;
2803 struct list_head *p;
2804 int i;
2805 int ret = 0;
2806
2807 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002808 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002809 data = global_trace.data[i];
2810 p = data->trace_pages.next;
2811 if (p == &data->trace_pages) {
2812 /* should never happen */
2813 WARN_ON(1);
2814 tracing_disabled = 1;
2815 ret = -1;
2816 break;
2817 }
2818 page = list_entry(p, struct page, lru);
2819 ClearPageLRU(page);
2820 list_del(&page->lru);
2821 __free_page(page);
2822
2823 tracing_reset(data);
2824
2825#ifdef CONFIG_TRACER_MAX_TRACE
2826 data = max_tr.data[i];
2827 p = data->trace_pages.next;
2828 if (p == &data->trace_pages) {
2829 /* should never happen */
2830 WARN_ON(1);
2831 tracing_disabled = 1;
2832 ret = -1;
2833 break;
2834 }
2835 page = list_entry(p, struct page, lru);
2836 ClearPageLRU(page);
2837 list_del(&page->lru);
2838 __free_page(page);
2839
2840 tracing_reset(data);
2841#endif
2842 }
2843 global_trace.entries -= ENTRIES_PER_PAGE;
2844
2845 return ret;
2846}
2847
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002848__init static int tracer_alloc_buffers(void)
2849{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002850 struct trace_array_cpu *data;
2851 void *array;
2852 struct page *page;
2853 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002854 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002855 int i;
2856
Steven Rostedt26994ea2008-05-12 21:20:48 +02002857 global_trace.ctrl = tracer_enabled;
2858
Steven Rostedtab464282008-05-12 21:21:00 +02002859 /* TODO: make the number of buffers hot pluggable with CPUS */
2860 tracing_nr_buffers = num_possible_cpus();
2861 tracing_buffer_mask = cpu_possible_map;
2862
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002863 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02002864 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002865 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002866 max_tr.data[i] = &per_cpu(max_data, i);
2867
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002868 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002869 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002870 printk(KERN_ERR "tracer: failed to allocate page"
2871 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002872 goto free_buffers;
2873 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002874
2875 /* set the array to the list */
2876 INIT_LIST_HEAD(&data->trace_pages);
2877 page = virt_to_page(array);
2878 list_add(&page->lru, &data->trace_pages);
2879 /* use the LRU flag to differentiate the two buffers */
2880 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002881
Steven Rostedta98a3c32008-05-12 21:20:59 +02002882 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2883 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2884
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002885/* Only allocate if we are actually using the max trace */
2886#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002887 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002888 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002889 printk(KERN_ERR "tracer: failed to allocate page"
2890 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002891 goto free_buffers;
2892 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002893
2894 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2895 page = virt_to_page(array);
2896 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2897 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002898#endif
2899 }
2900
2901 /*
2902 * Since we allocate by orders of pages, we may be able to
2903 * round up a bit.
2904 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002905 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002906 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002907
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002908 while (global_trace.entries < trace_nr_entries) {
2909 if (trace_alloc_page())
2910 break;
2911 pages++;
2912 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002913 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002914
2915 pr_info("tracer: %d pages allocated for %ld",
2916 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002917 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2918 pr_info(" actual entries %ld\n", global_trace.entries);
2919
2920 tracer_init_debugfs();
2921
2922 trace_init_cmdlines();
2923
2924 register_tracer(&no_tracer);
2925 current_trace = &no_tracer;
2926
Steven Rostedt60a11772008-05-12 21:20:44 +02002927 /* All seems OK, enable tracing */
2928 tracing_disabled = 0;
2929
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002930 return 0;
2931
2932 free_buffers:
2933 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002934 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002935 struct trace_array_cpu *data = global_trace.data[i];
2936
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002937 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002938 list_for_each_entry_safe(page, tmp,
2939 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002940 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002941 __free_page(page);
2942 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002943 }
2944
2945#ifdef CONFIG_TRACER_MAX_TRACE
2946 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002947 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002948 list_for_each_entry_safe(page, tmp,
2949 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002950 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002951 __free_page(page);
2952 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002953 }
2954#endif
2955 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002956 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002957}
Steven Rostedt60a11772008-05-12 21:20:44 +02002958fs_initcall(tracer_alloc_buffers);