blob: b9126ef46a9e87a77a5c63aeb6112f8c1eaa7bbb [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 Rostedt53d0aa72008-05-12 21:21:01 +0200612 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200613 data->trace_head = data->trace_tail = head_page(data);
614 data->trace_head_idx = 0;
615 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200616}
617
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200618#define SAVED_CMDLINES 128
619static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
620static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
621static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
622static int cmdline_idx;
623static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200624
625/* trace in all context switches */
626atomic_t trace_record_cmdline_enabled __read_mostly;
627
628/* temporary disable recording */
629atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200630
631static void trace_init_cmdlines(void)
632{
633 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
634 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
635 cmdline_idx = 0;
636}
637
Ingo Molnare309b412008-05-12 21:20:51 +0200638void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200639
Ingo Molnare309b412008-05-12 21:20:51 +0200640static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200641{
642 unsigned map;
643 unsigned idx;
644
645 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
646 return;
647
648 /*
649 * It's not the end of the world if we don't get
650 * the lock, but we also don't want to spin
651 * nor do we want to disable interrupts,
652 * so if we miss here, then better luck next time.
653 */
654 if (!spin_trylock(&trace_cmdline_lock))
655 return;
656
657 idx = map_pid_to_cmdline[tsk->pid];
658 if (idx >= SAVED_CMDLINES) {
659 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
660
661 map = map_cmdline_to_pid[idx];
662 if (map <= PID_MAX_DEFAULT)
663 map_pid_to_cmdline[map] = (unsigned)-1;
664
665 map_pid_to_cmdline[tsk->pid] = idx;
666
667 cmdline_idx = idx;
668 }
669
670 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
671
672 spin_unlock(&trace_cmdline_lock);
673}
674
Ingo Molnare309b412008-05-12 21:20:51 +0200675static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200676{
677 char *cmdline = "<...>";
678 unsigned map;
679
680 if (!pid)
681 return "<idle>";
682
683 if (pid > PID_MAX_DEFAULT)
684 goto out;
685
686 map = map_pid_to_cmdline[pid];
687 if (map >= SAVED_CMDLINES)
688 goto out;
689
690 cmdline = saved_cmdlines[map];
691
692 out:
693 return cmdline;
694}
695
Ingo Molnare309b412008-05-12 21:20:51 +0200696void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200697{
698 if (atomic_read(&trace_record_cmdline_disabled))
699 return;
700
701 trace_save_cmdline(tsk);
702}
703
Ingo Molnare309b412008-05-12 21:20:51 +0200704static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200705trace_next_list(struct trace_array_cpu *data, struct list_head *next)
706{
707 /*
708 * Roundrobin - but skip the head (which is not a real page):
709 */
710 next = next->next;
711 if (unlikely(next == &data->trace_pages))
712 next = next->next;
713 BUG_ON(next == &data->trace_pages);
714
715 return next;
716}
717
Ingo Molnare309b412008-05-12 21:20:51 +0200718static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200719trace_next_page(struct trace_array_cpu *data, void *addr)
720{
721 struct list_head *next;
722 struct page *page;
723
724 page = virt_to_page(addr);
725
726 next = trace_next_list(data, &page->lru);
727 page = list_entry(next, struct page, lru);
728
729 return page_address(page);
730}
731
Ingo Molnare309b412008-05-12 21:20:51 +0200732static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200733tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200734{
735 unsigned long idx, idx_next;
736 struct trace_entry *entry;
737
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200738 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200739 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200740 idx_next = idx + 1;
741
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200742 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
743
Steven Rostedt93a588f2008-05-12 21:20:45 +0200744 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200745
746 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200747 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200748 idx_next = 0;
749 }
750
Steven Rostedt93a588f2008-05-12 21:20:45 +0200751 if (data->trace_head == data->trace_tail &&
752 idx_next == data->trace_tail_idx) {
753 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200754 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200755 data->trace_tail_idx++;
756 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
757 data->trace_tail =
758 trace_next_page(data, data->trace_tail);
759 data->trace_tail_idx = 0;
760 }
761 }
762
763 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200764
765 return entry;
766}
767
Ingo Molnare309b412008-05-12 21:20:51 +0200768static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200769tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200770{
771 struct task_struct *tsk = current;
772 unsigned long pc;
773
774 pc = preempt_count();
775
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200776 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200777 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200778 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200779 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
780 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
781 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
782 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
783}
784
Ingo Molnare309b412008-05-12 21:20:51 +0200785void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200786trace_function(struct trace_array *tr, struct trace_array_cpu *data,
787 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200788{
789 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200790 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200791
Steven Rostedt92205c22008-05-12 21:20:55 +0200792 raw_local_irq_save(irq_flags);
793 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200794 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200795 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200796 entry->type = TRACE_FN;
797 entry->fn.ip = ip;
798 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200799 __raw_spin_unlock(&data->lock);
800 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200801}
802
Ingo Molnare309b412008-05-12 21:20:51 +0200803void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200804ftrace(struct trace_array *tr, struct trace_array_cpu *data,
805 unsigned long ip, unsigned long parent_ip, unsigned long flags)
806{
807 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200808 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200809}
810
Ingo Molnare309b412008-05-12 21:20:51 +0200811void
Ingo Molnar4e655512008-05-12 21:20:52 +0200812__trace_special(void *__tr, void *__data,
813 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200814{
Ingo Molnar4e655512008-05-12 21:20:52 +0200815 struct trace_array_cpu *data = __data;
816 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200817 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200818 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200819
Steven Rostedt92205c22008-05-12 21:20:55 +0200820 raw_local_irq_save(irq_flags);
821 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200822 entry = tracing_get_trace_entry(tr, data);
823 tracing_generic_entry_update(entry, 0);
824 entry->type = TRACE_SPECIAL;
825 entry->special.arg1 = arg1;
826 entry->special.arg2 = arg2;
827 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200828 __raw_spin_unlock(&data->lock);
829 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200830
831 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200832}
833
834void __trace_stack(struct trace_array *tr,
835 struct trace_array_cpu *data,
836 unsigned long flags,
837 int skip)
838{
839 struct trace_entry *entry;
840 struct stack_trace trace;
841
842 if (!(trace_flags & TRACE_ITER_STACKTRACE))
843 return;
844
845 entry = tracing_get_trace_entry(tr, data);
846 tracing_generic_entry_update(entry, flags);
847 entry->type = TRACE_STACK;
848
849 memset(&entry->stack, 0, sizeof(entry->stack));
850
851 trace.nr_entries = 0;
852 trace.max_entries = FTRACE_STACK_ENTRIES;
853 trace.skip = skip;
854 trace.entries = entry->stack.caller;
855
856 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200857}
858
Ingo Molnare309b412008-05-12 21:20:51 +0200859void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200860tracing_sched_switch_trace(struct trace_array *tr,
861 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200862 struct task_struct *prev,
863 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200864 unsigned long flags)
865{
866 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200867 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200868
Steven Rostedt92205c22008-05-12 21:20:55 +0200869 raw_local_irq_save(irq_flags);
870 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200871 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200872 tracing_generic_entry_update(entry, flags);
873 entry->type = TRACE_CTX;
874 entry->ctx.prev_pid = prev->pid;
875 entry->ctx.prev_prio = prev->prio;
876 entry->ctx.prev_state = prev->state;
877 entry->ctx.next_pid = next->pid;
878 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200879 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200880 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200881 __raw_spin_unlock(&data->lock);
882 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200883}
884
Ingo Molnar57422792008-05-12 21:20:51 +0200885void
886tracing_sched_wakeup_trace(struct trace_array *tr,
887 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200888 struct task_struct *wakee,
889 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200890 unsigned long flags)
891{
892 struct trace_entry *entry;
893 unsigned long irq_flags;
894
Steven Rostedt92205c22008-05-12 21:20:55 +0200895 raw_local_irq_save(irq_flags);
896 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200897 entry = tracing_get_trace_entry(tr, data);
898 tracing_generic_entry_update(entry, flags);
899 entry->type = TRACE_WAKE;
900 entry->ctx.prev_pid = curr->pid;
901 entry->ctx.prev_prio = curr->prio;
902 entry->ctx.prev_state = curr->state;
903 entry->ctx.next_pid = wakee->pid;
904 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200905 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200906 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200907 __raw_spin_unlock(&data->lock);
908 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200909
910 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200911}
912
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200913#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200914static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200915function_trace_call(unsigned long ip, unsigned long parent_ip)
916{
917 struct trace_array *tr = &global_trace;
918 struct trace_array_cpu *data;
919 unsigned long flags;
920 long disabled;
921 int cpu;
922
923 if (unlikely(!tracer_enabled))
924 return;
925
926 local_irq_save(flags);
927 cpu = raw_smp_processor_id();
928 data = tr->data[cpu];
929 disabled = atomic_inc_return(&data->disabled);
930
931 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200932 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200933
934 atomic_dec(&data->disabled);
935 local_irq_restore(flags);
936}
937
938static struct ftrace_ops trace_ops __read_mostly =
939{
940 .func = function_trace_call,
941};
942
Ingo Molnare309b412008-05-12 21:20:51 +0200943void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200944{
945 register_ftrace_function(&trace_ops);
946}
947
Ingo Molnare309b412008-05-12 21:20:51 +0200948void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200949{
950 unregister_ftrace_function(&trace_ops);
951}
952#endif
953
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200954enum trace_file_type {
955 TRACE_FILE_LAT_FMT = 1,
956};
957
958static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200959trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
960 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200961{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200962 struct page *page;
963 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200964
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200965 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200966 iter->next_idx[cpu] >= data->trace_idx ||
967 (data->trace_head == data->trace_tail &&
968 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200969 return NULL;
970
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200971 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200972 /* Initialize the iterator for this cpu trace buffer */
973 WARN_ON(!data->trace_tail);
974 page = virt_to_page(data->trace_tail);
975 iter->next_page[cpu] = &page->lru;
976 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200977 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200978
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200979 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200980 BUG_ON(&data->trace_pages == &page->lru);
981
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200982 array = page_address(page);
983
Steven Rostedt93a588f2008-05-12 21:20:45 +0200984 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200985 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200986}
987
Ingo Molnare309b412008-05-12 21:20:51 +0200988static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200989find_next_entry(struct trace_iterator *iter, int *ent_cpu)
990{
991 struct trace_array *tr = iter->tr;
992 struct trace_entry *ent, *next = NULL;
993 int next_cpu = -1;
994 int cpu;
995
Steven Rostedtab464282008-05-12 21:21:00 +0200996 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200997 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200998 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200999 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001000 /*
1001 * Pick the entry with the smallest timestamp:
1002 */
1003 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001004 next = ent;
1005 next_cpu = cpu;
1006 }
1007 }
1008
1009 if (ent_cpu)
1010 *ent_cpu = next_cpu;
1011
1012 return next;
1013}
1014
Ingo Molnare309b412008-05-12 21:20:51 +02001015static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001016{
1017 iter->idx++;
1018 iter->next_idx[iter->cpu]++;
1019 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001020
Steven Rostedtb3806b42008-05-12 21:20:46 +02001021 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1022 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1023
1024 iter->next_page_idx[iter->cpu] = 0;
1025 iter->next_page[iter->cpu] =
1026 trace_next_list(data, iter->next_page[iter->cpu]);
1027 }
1028}
1029
Ingo Molnare309b412008-05-12 21:20:51 +02001030static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001031{
1032 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1033
1034 data->trace_tail_idx++;
1035 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1036 data->trace_tail = trace_next_page(data, data->trace_tail);
1037 data->trace_tail_idx = 0;
1038 }
1039
1040 /* Check if we empty it, then reset the index */
1041 if (data->trace_head == data->trace_tail &&
1042 data->trace_head_idx == data->trace_tail_idx)
1043 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001044}
1045
Ingo Molnare309b412008-05-12 21:20:51 +02001046static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001047{
1048 struct trace_entry *next;
1049 int next_cpu = -1;
1050
1051 next = find_next_entry(iter, &next_cpu);
1052
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001053 iter->prev_ent = iter->ent;
1054 iter->prev_cpu = iter->cpu;
1055
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056 iter->ent = next;
1057 iter->cpu = next_cpu;
1058
Steven Rostedtb3806b42008-05-12 21:20:46 +02001059 if (next)
1060 trace_iterator_increment(iter);
1061
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001062 return next ? iter : NULL;
1063}
1064
Ingo Molnare309b412008-05-12 21:20:51 +02001065static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001066{
1067 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068 void *last_ent = iter->ent;
1069 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001070 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001071
1072 (*pos)++;
1073
1074 /* can't go backwards */
1075 if (iter->idx > i)
1076 return NULL;
1077
1078 if (iter->idx < 0)
1079 ent = find_next_entry_inc(iter);
1080 else
1081 ent = iter;
1082
1083 while (ent && iter->idx < i)
1084 ent = find_next_entry_inc(iter);
1085
1086 iter->pos = *pos;
1087
1088 if (last_ent && !ent)
1089 seq_puts(m, "\n\nvim:ft=help\n");
1090
1091 return ent;
1092}
1093
1094static void *s_start(struct seq_file *m, loff_t *pos)
1095{
1096 struct trace_iterator *iter = m->private;
1097 void *p = NULL;
1098 loff_t l = 0;
1099 int i;
1100
1101 mutex_lock(&trace_types_lock);
1102
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001103 if (!current_trace || current_trace != iter->trace) {
1104 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001106 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001107
1108 atomic_inc(&trace_record_cmdline_disabled);
1109
1110 /* let the tracer grab locks here if needed */
1111 if (current_trace->start)
1112 current_trace->start(iter);
1113
1114 if (*pos != iter->pos) {
1115 iter->ent = NULL;
1116 iter->cpu = 0;
1117 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001118 iter->prev_ent = NULL;
1119 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120
Steven Rostedtab464282008-05-12 21:21:00 +02001121 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001122 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001123 iter->next_page[i] = NULL;
1124 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001125
1126 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1127 ;
1128
1129 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001130 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001131 p = s_next(m, p, &l);
1132 }
1133
1134 return p;
1135}
1136
1137static void s_stop(struct seq_file *m, void *p)
1138{
1139 struct trace_iterator *iter = m->private;
1140
1141 atomic_dec(&trace_record_cmdline_disabled);
1142
1143 /* let the tracer release locks here if needed */
1144 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1145 iter->trace->stop(iter);
1146
1147 mutex_unlock(&trace_types_lock);
1148}
1149
Steven Rostedtb3806b42008-05-12 21:20:46 +02001150static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001151seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001152{
1153#ifdef CONFIG_KALLSYMS
1154 char str[KSYM_SYMBOL_LEN];
1155
1156 kallsyms_lookup(address, NULL, NULL, NULL, str);
1157
Steven Rostedtb3806b42008-05-12 21:20:46 +02001158 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001159#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001160 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001161}
1162
Steven Rostedtb3806b42008-05-12 21:20:46 +02001163static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001164seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1165 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001166{
1167#ifdef CONFIG_KALLSYMS
1168 char str[KSYM_SYMBOL_LEN];
1169
1170 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001171 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001173 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174}
1175
1176#ifndef CONFIG_64BIT
1177# define IP_FMT "%08lx"
1178#else
1179# define IP_FMT "%016lx"
1180#endif
1181
Ingo Molnare309b412008-05-12 21:20:51 +02001182static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001183seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001184{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001185 int ret;
1186
1187 if (!ip)
1188 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001189
1190 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001191 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001192 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001193 ret = seq_print_sym_short(s, "%s", ip);
1194
1195 if (!ret)
1196 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197
1198 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001199 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1200 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001201}
1202
Ingo Molnare309b412008-05-12 21:20:51 +02001203static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001204{
1205 seq_puts(m, "# _------=> CPU# \n");
1206 seq_puts(m, "# / _-----=> irqs-off \n");
1207 seq_puts(m, "# | / _----=> need-resched \n");
1208 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1209 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1210 seq_puts(m, "# |||| / \n");
1211 seq_puts(m, "# ||||| delay \n");
1212 seq_puts(m, "# cmd pid ||||| time | caller \n");
1213 seq_puts(m, "# \\ / ||||| \\ | / \n");
1214}
1215
Ingo Molnare309b412008-05-12 21:20:51 +02001216static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001217{
1218 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1219 seq_puts(m, "# | | | | |\n");
1220}
1221
1222
Ingo Molnare309b412008-05-12 21:20:51 +02001223static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001224print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1225{
1226 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1227 struct trace_array *tr = iter->tr;
1228 struct trace_array_cpu *data = tr->data[tr->cpu];
1229 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001230 unsigned long total = 0;
1231 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001232 int cpu;
1233 const char *name = "preemption";
1234
1235 if (type)
1236 name = type->name;
1237
Steven Rostedtab464282008-05-12 21:21:00 +02001238 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001239 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001240 total += tr->data[cpu]->trace_idx;
1241 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001242 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001243 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001244 entries += tr->data[cpu]->trace_idx;
1245 }
1246 }
1247
1248 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1249 name, UTS_RELEASE);
1250 seq_puts(m, "-----------------------------------"
1251 "---------------------------------\n");
1252 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1253 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001254 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001255 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001256 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001257 tr->cpu,
1258#if defined(CONFIG_PREEMPT_NONE)
1259 "server",
1260#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1261 "desktop",
1262#elif defined(CONFIG_PREEMPT_DESKTOP)
1263 "preempt",
1264#else
1265 "unknown",
1266#endif
1267 /* These are reserved for later use */
1268 0, 0, 0, 0);
1269#ifdef CONFIG_SMP
1270 seq_printf(m, " #P:%d)\n", num_online_cpus());
1271#else
1272 seq_puts(m, ")\n");
1273#endif
1274 seq_puts(m, " -----------------\n");
1275 seq_printf(m, " | task: %.16s-%d "
1276 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1277 data->comm, data->pid, data->uid, data->nice,
1278 data->policy, data->rt_priority);
1279 seq_puts(m, " -----------------\n");
1280
1281 if (data->critical_start) {
1282 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001283 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1284 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001285 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001286 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1287 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288 seq_puts(m, "\n");
1289 }
1290
1291 seq_puts(m, "\n");
1292}
1293
Ingo Molnare309b412008-05-12 21:20:51 +02001294static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001295lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001296{
1297 int hardirq, softirq;
1298 char *comm;
1299
1300 comm = trace_find_cmdline(entry->pid);
1301
Steven Rostedt214023c2008-05-12 21:20:46 +02001302 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1303 trace_seq_printf(s, "%d", cpu);
1304 trace_seq_printf(s, "%c%c",
1305 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1306 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001307
1308 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1309 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001310 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001311 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001312 } else {
1313 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001314 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001315 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001317 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001319 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001320 }
1321 }
1322
1323 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001324 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001325 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001326 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001327}
1328
1329unsigned long preempt_mark_thresh = 100;
1330
Ingo Molnare309b412008-05-12 21:20:51 +02001331static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001332lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001333 unsigned long rel_usecs)
1334{
Steven Rostedt214023c2008-05-12 21:20:46 +02001335 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001336 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001337 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001338 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001339 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001340 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001341 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001342}
1343
1344static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1345
Ingo Molnare309b412008-05-12 21:20:51 +02001346static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001347print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001348{
Steven Rostedt214023c2008-05-12 21:20:46 +02001349 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001350 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1351 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1352 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1353 struct trace_entry *entry = iter->ent;
1354 unsigned long abs_usecs;
1355 unsigned long rel_usecs;
1356 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001357 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001358 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001359 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001360
1361 if (!next_entry)
1362 next_entry = entry;
1363 rel_usecs = ns2usecs(next_entry->t - entry->t);
1364 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1365
1366 if (verbose) {
1367 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001368 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1369 " %ld.%03ldms (+%ld.%03ldms): ",
1370 comm,
1371 entry->pid, cpu, entry->flags,
1372 entry->preempt_count, trace_idx,
1373 ns2usecs(entry->t),
1374 abs_usecs/1000,
1375 abs_usecs % 1000, rel_usecs/1000,
1376 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001377 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001378 lat_print_generic(s, entry, cpu);
1379 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380 }
1381 switch (entry->type) {
1382 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001383 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1384 trace_seq_puts(s, " (");
1385 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1386 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387 break;
1388 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001389 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001390 T = entry->ctx.next_state < sizeof(state_to_char) ?
1391 state_to_char[entry->ctx.next_state] : 'X';
1392
Ankita Gargd17d9692008-05-12 21:20:58 +02001393 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1394 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001395 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001396 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001397 entry->ctx.prev_pid,
1398 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001399 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001400 entry->ctx.next_pid,
1401 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001402 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001403 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001404 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001405 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001406 entry->special.arg1,
1407 entry->special.arg2,
1408 entry->special.arg3);
1409 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001410 case TRACE_STACK:
1411 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1412 if (i)
1413 trace_seq_puts(s, " <= ");
1414 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1415 }
1416 trace_seq_puts(s, "\n");
1417 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001418 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001419 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001420 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001421 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001422}
1423
Ingo Molnare309b412008-05-12 21:20:51 +02001424static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001425{
Steven Rostedt214023c2008-05-12 21:20:46 +02001426 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001428 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001429 unsigned long usec_rem;
1430 unsigned long long t;
1431 unsigned long secs;
1432 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001433 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001434 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001435 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001436
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001437 entry = iter->ent;
1438
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001439 comm = trace_find_cmdline(iter->ent->pid);
1440
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001441 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001442 usec_rem = do_div(t, 1000000ULL);
1443 secs = (unsigned long)t;
1444
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001445 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1446 if (!ret)
1447 return 0;
1448 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1449 if (!ret)
1450 return 0;
1451 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1452 if (!ret)
1453 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001454
1455 switch (entry->type) {
1456 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001457 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1458 if (!ret)
1459 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001460 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1461 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001462 ret = trace_seq_printf(s, " <-");
1463 if (!ret)
1464 return 0;
1465 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1466 sym_flags);
1467 if (!ret)
1468 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001469 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001470 ret = trace_seq_printf(s, "\n");
1471 if (!ret)
1472 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001473 break;
1474 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001475 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001476 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1477 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001478 T = entry->ctx.next_state < sizeof(state_to_char) ?
1479 state_to_char[entry->ctx.next_state] : 'X';
1480 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001481 entry->ctx.prev_pid,
1482 entry->ctx.prev_prio,
1483 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001484 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001485 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001486 entry->ctx.next_prio,
1487 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001488 if (!ret)
1489 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001490 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001491 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001492 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001493 entry->special.arg1,
1494 entry->special.arg2,
1495 entry->special.arg3);
1496 if (!ret)
1497 return 0;
1498 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001499 case TRACE_STACK:
1500 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1501 if (i) {
1502 ret = trace_seq_puts(s, " <= ");
1503 if (!ret)
1504 return 0;
1505 }
1506 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1507 sym_flags);
1508 if (!ret)
1509 return 0;
1510 }
1511 ret = trace_seq_puts(s, "\n");
1512 if (!ret)
1513 return 0;
1514 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001515 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001516 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001517}
1518
Ingo Molnare309b412008-05-12 21:20:51 +02001519static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001520{
1521 struct trace_seq *s = &iter->seq;
1522 struct trace_entry *entry;
1523 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001524 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001525
1526 entry = iter->ent;
1527
1528 ret = trace_seq_printf(s, "%d %d %llu ",
1529 entry->pid, iter->cpu, entry->t);
1530 if (!ret)
1531 return 0;
1532
1533 switch (entry->type) {
1534 case TRACE_FN:
1535 ret = trace_seq_printf(s, "%x %x\n",
1536 entry->fn.ip, entry->fn.parent_ip);
1537 if (!ret)
1538 return 0;
1539 break;
1540 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001541 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001542 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1543 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001544 T = entry->ctx.next_state < sizeof(state_to_char) ?
1545 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001546 if (entry->type == TRACE_WAKE)
1547 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001548 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001549 entry->ctx.prev_pid,
1550 entry->ctx.prev_prio,
1551 S,
1552 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001553 entry->ctx.next_prio,
1554 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001555 if (!ret)
1556 return 0;
1557 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001558 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001559 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001560 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001561 entry->special.arg1,
1562 entry->special.arg2,
1563 entry->special.arg3);
1564 if (!ret)
1565 return 0;
1566 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001567 }
1568 return 1;
1569}
1570
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001571#define SEQ_PUT_FIELD_RET(s, x) \
1572do { \
1573 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1574 return 0; \
1575} while (0)
1576
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001577#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1578do { \
1579 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1580 return 0; \
1581} while (0)
1582
Ingo Molnare309b412008-05-12 21:20:51 +02001583static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001584{
1585 struct trace_seq *s = &iter->seq;
1586 unsigned char newline = '\n';
1587 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001588 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001589
1590 entry = iter->ent;
1591
1592 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1593 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1594 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1595
1596 switch (entry->type) {
1597 case TRACE_FN:
1598 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1599 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1600 break;
1601 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001602 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001603 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1604 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001605 T = entry->ctx.next_state < sizeof(state_to_char) ?
1606 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001607 if (entry->type == TRACE_WAKE)
1608 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001609 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1610 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1611 SEQ_PUT_HEX_FIELD_RET(s, S);
1612 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1613 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1614 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001615 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001616 break;
1617 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001618 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001619 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1620 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1621 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1622 break;
1623 }
1624 SEQ_PUT_FIELD_RET(s, newline);
1625
1626 return 1;
1627}
1628
Ingo Molnare309b412008-05-12 21:20:51 +02001629static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001630{
1631 struct trace_seq *s = &iter->seq;
1632 struct trace_entry *entry;
1633
1634 entry = iter->ent;
1635
1636 SEQ_PUT_FIELD_RET(s, entry->pid);
1637 SEQ_PUT_FIELD_RET(s, entry->cpu);
1638 SEQ_PUT_FIELD_RET(s, entry->t);
1639
1640 switch (entry->type) {
1641 case TRACE_FN:
1642 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1643 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1644 break;
1645 case TRACE_CTX:
1646 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1647 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1648 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1649 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1650 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001651 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001652 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001653 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001654 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001655 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1656 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1657 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1658 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001659 }
1660 return 1;
1661}
1662
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001663static int trace_empty(struct trace_iterator *iter)
1664{
1665 struct trace_array_cpu *data;
1666 int cpu;
1667
Steven Rostedtab464282008-05-12 21:21:00 +02001668 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001669 data = iter->tr->data[cpu];
1670
Steven Rostedtb3806b42008-05-12 21:20:46 +02001671 if (head_page(data) && data->trace_idx &&
1672 (data->trace_tail != data->trace_head ||
1673 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001674 return 0;
1675 }
1676 return 1;
1677}
1678
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001679static int print_trace_line(struct trace_iterator *iter)
1680{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001681 if (iter->trace && iter->trace->print_line)
1682 return iter->trace->print_line(iter);
1683
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001684 if (trace_flags & TRACE_ITER_BIN)
1685 return print_bin_fmt(iter);
1686
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001687 if (trace_flags & TRACE_ITER_HEX)
1688 return print_hex_fmt(iter);
1689
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001690 if (trace_flags & TRACE_ITER_RAW)
1691 return print_raw_fmt(iter);
1692
1693 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1694 return print_lat_fmt(iter, iter->idx, iter->cpu);
1695
1696 return print_trace_fmt(iter);
1697}
1698
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001699static int s_show(struct seq_file *m, void *v)
1700{
1701 struct trace_iterator *iter = v;
1702
1703 if (iter->ent == NULL) {
1704 if (iter->tr) {
1705 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1706 seq_puts(m, "#\n");
1707 }
1708 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1709 /* print nothing if the buffers are empty */
1710 if (trace_empty(iter))
1711 return 0;
1712 print_trace_header(m, iter);
1713 if (!(trace_flags & TRACE_ITER_VERBOSE))
1714 print_lat_help_header(m);
1715 } else {
1716 if (!(trace_flags & TRACE_ITER_VERBOSE))
1717 print_func_help_header(m);
1718 }
1719 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001720 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001721 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001722 }
1723
1724 return 0;
1725}
1726
1727static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001728 .start = s_start,
1729 .next = s_next,
1730 .stop = s_stop,
1731 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732};
1733
Ingo Molnare309b412008-05-12 21:20:51 +02001734static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001735__tracing_open(struct inode *inode, struct file *file, int *ret)
1736{
1737 struct trace_iterator *iter;
1738
Steven Rostedt60a11772008-05-12 21:20:44 +02001739 if (tracing_disabled) {
1740 *ret = -ENODEV;
1741 return NULL;
1742 }
1743
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001744 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1745 if (!iter) {
1746 *ret = -ENOMEM;
1747 goto out;
1748 }
1749
1750 mutex_lock(&trace_types_lock);
1751 if (current_trace && current_trace->print_max)
1752 iter->tr = &max_tr;
1753 else
1754 iter->tr = inode->i_private;
1755 iter->trace = current_trace;
1756 iter->pos = -1;
1757
1758 /* TODO stop tracer */
1759 *ret = seq_open(file, &tracer_seq_ops);
1760 if (!*ret) {
1761 struct seq_file *m = file->private_data;
1762 m->private = iter;
1763
1764 /* stop the trace while dumping */
1765 if (iter->tr->ctrl)
1766 tracer_enabled = 0;
1767
1768 if (iter->trace && iter->trace->open)
1769 iter->trace->open(iter);
1770 } else {
1771 kfree(iter);
1772 iter = NULL;
1773 }
1774 mutex_unlock(&trace_types_lock);
1775
1776 out:
1777 return iter;
1778}
1779
1780int tracing_open_generic(struct inode *inode, struct file *filp)
1781{
Steven Rostedt60a11772008-05-12 21:20:44 +02001782 if (tracing_disabled)
1783 return -ENODEV;
1784
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001785 filp->private_data = inode->i_private;
1786 return 0;
1787}
1788
1789int tracing_release(struct inode *inode, struct file *file)
1790{
1791 struct seq_file *m = (struct seq_file *)file->private_data;
1792 struct trace_iterator *iter = m->private;
1793
1794 mutex_lock(&trace_types_lock);
1795 if (iter->trace && iter->trace->close)
1796 iter->trace->close(iter);
1797
1798 /* reenable tracing if it was previously enabled */
1799 if (iter->tr->ctrl)
1800 tracer_enabled = 1;
1801 mutex_unlock(&trace_types_lock);
1802
1803 seq_release(inode, file);
1804 kfree(iter);
1805 return 0;
1806}
1807
1808static int tracing_open(struct inode *inode, struct file *file)
1809{
1810 int ret;
1811
1812 __tracing_open(inode, file, &ret);
1813
1814 return ret;
1815}
1816
1817static int tracing_lt_open(struct inode *inode, struct file *file)
1818{
1819 struct trace_iterator *iter;
1820 int ret;
1821
1822 iter = __tracing_open(inode, file, &ret);
1823
1824 if (!ret)
1825 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1826
1827 return ret;
1828}
1829
1830
Ingo Molnare309b412008-05-12 21:20:51 +02001831static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001832t_next(struct seq_file *m, void *v, loff_t *pos)
1833{
1834 struct tracer *t = m->private;
1835
1836 (*pos)++;
1837
1838 if (t)
1839 t = t->next;
1840
1841 m->private = t;
1842
1843 return t;
1844}
1845
1846static void *t_start(struct seq_file *m, loff_t *pos)
1847{
1848 struct tracer *t = m->private;
1849 loff_t l = 0;
1850
1851 mutex_lock(&trace_types_lock);
1852 for (; t && l < *pos; t = t_next(m, t, &l))
1853 ;
1854
1855 return t;
1856}
1857
1858static void t_stop(struct seq_file *m, void *p)
1859{
1860 mutex_unlock(&trace_types_lock);
1861}
1862
1863static int t_show(struct seq_file *m, void *v)
1864{
1865 struct tracer *t = v;
1866
1867 if (!t)
1868 return 0;
1869
1870 seq_printf(m, "%s", t->name);
1871 if (t->next)
1872 seq_putc(m, ' ');
1873 else
1874 seq_putc(m, '\n');
1875
1876 return 0;
1877}
1878
1879static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001880 .start = t_start,
1881 .next = t_next,
1882 .stop = t_stop,
1883 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001884};
1885
1886static int show_traces_open(struct inode *inode, struct file *file)
1887{
1888 int ret;
1889
Steven Rostedt60a11772008-05-12 21:20:44 +02001890 if (tracing_disabled)
1891 return -ENODEV;
1892
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001893 ret = seq_open(file, &show_traces_seq_ops);
1894 if (!ret) {
1895 struct seq_file *m = file->private_data;
1896 m->private = trace_types;
1897 }
1898
1899 return ret;
1900}
1901
1902static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001903 .open = tracing_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 tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001910 .open = tracing_lt_open,
1911 .read = seq_read,
1912 .llseek = seq_lseek,
1913 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001914};
1915
1916static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001917 .open = show_traces_open,
1918 .read = seq_read,
1919 .release = seq_release,
1920};
1921
Ingo Molnar36dfe922008-05-12 21:20:52 +02001922/*
1923 * Only trace on a CPU if the bitmask is set:
1924 */
1925static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1926
1927/*
1928 * When tracing/tracing_cpu_mask is modified then this holds
1929 * the new bitmask we are about to install:
1930 */
1931static cpumask_t tracing_cpumask_new;
1932
1933/*
1934 * The tracer itself will not take this lock, but still we want
1935 * to provide a consistent cpumask to user-space:
1936 */
1937static DEFINE_MUTEX(tracing_cpumask_update_lock);
1938
1939/*
1940 * Temporary storage for the character representation of the
1941 * CPU bitmask (and one more byte for the newline):
1942 */
1943static char mask_str[NR_CPUS + 1];
1944
Ingo Molnarc7078de2008-05-12 21:20:52 +02001945static ssize_t
1946tracing_cpumask_read(struct file *filp, char __user *ubuf,
1947 size_t count, loff_t *ppos)
1948{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001949 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001950
1951 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001952
1953 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1954 if (count - len < 2) {
1955 count = -EINVAL;
1956 goto out_err;
1957 }
1958 len += sprintf(mask_str + len, "\n");
1959 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1960
1961out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001962 mutex_unlock(&tracing_cpumask_update_lock);
1963
1964 return count;
1965}
1966
1967static ssize_t
1968tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1969 size_t count, loff_t *ppos)
1970{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001971 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001972
1973 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001974 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1975 if (err)
1976 goto err_unlock;
1977
Steven Rostedt92205c22008-05-12 21:20:55 +02001978 raw_local_irq_disable();
1979 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001980 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001981 /*
1982 * Increase/decrease the disabled counter if we are
1983 * about to flip a bit in the cpumask:
1984 */
1985 if (cpu_isset(cpu, tracing_cpumask) &&
1986 !cpu_isset(cpu, tracing_cpumask_new)) {
1987 atomic_inc(&global_trace.data[cpu]->disabled);
1988 }
1989 if (!cpu_isset(cpu, tracing_cpumask) &&
1990 cpu_isset(cpu, tracing_cpumask_new)) {
1991 atomic_dec(&global_trace.data[cpu]->disabled);
1992 }
1993 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001994 __raw_spin_unlock(&ftrace_max_lock);
1995 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001996
1997 tracing_cpumask = tracing_cpumask_new;
1998
Ingo Molnarc7078de2008-05-12 21:20:52 +02001999 mutex_unlock(&tracing_cpumask_update_lock);
2000
Ingo Molnarc7078de2008-05-12 21:20:52 +02002001 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002002
2003err_unlock:
2004 mutex_unlock(&tracing_cpumask_update_lock);
2005
2006 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002007}
2008
2009static struct file_operations tracing_cpumask_fops = {
2010 .open = tracing_open_generic,
2011 .read = tracing_cpumask_read,
2012 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002013};
2014
2015static ssize_t
2016tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2017 size_t cnt, loff_t *ppos)
2018{
2019 char *buf;
2020 int r = 0;
2021 int len = 0;
2022 int i;
2023
2024 /* calulate max size */
2025 for (i = 0; trace_options[i]; i++) {
2026 len += strlen(trace_options[i]);
2027 len += 3; /* "no" and space */
2028 }
2029
2030 /* +2 for \n and \0 */
2031 buf = kmalloc(len + 2, GFP_KERNEL);
2032 if (!buf)
2033 return -ENOMEM;
2034
2035 for (i = 0; trace_options[i]; i++) {
2036 if (trace_flags & (1 << i))
2037 r += sprintf(buf + r, "%s ", trace_options[i]);
2038 else
2039 r += sprintf(buf + r, "no%s ", trace_options[i]);
2040 }
2041
2042 r += sprintf(buf + r, "\n");
2043 WARN_ON(r >= len + 2);
2044
Ingo Molnar36dfe922008-05-12 21:20:52 +02002045 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002046
2047 kfree(buf);
2048
2049 return r;
2050}
2051
2052static ssize_t
2053tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2054 size_t cnt, loff_t *ppos)
2055{
2056 char buf[64];
2057 char *cmp = buf;
2058 int neg = 0;
2059 int i;
2060
Steven Rostedtcffae432008-05-12 21:21:00 +02002061 if (cnt >= sizeof(buf))
2062 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002063
2064 if (copy_from_user(&buf, ubuf, cnt))
2065 return -EFAULT;
2066
2067 buf[cnt] = 0;
2068
2069 if (strncmp(buf, "no", 2) == 0) {
2070 neg = 1;
2071 cmp += 2;
2072 }
2073
2074 for (i = 0; trace_options[i]; i++) {
2075 int len = strlen(trace_options[i]);
2076
2077 if (strncmp(cmp, trace_options[i], len) == 0) {
2078 if (neg)
2079 trace_flags &= ~(1 << i);
2080 else
2081 trace_flags |= (1 << i);
2082 break;
2083 }
2084 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002085 /*
2086 * If no option could be set, return an error:
2087 */
2088 if (!trace_options[i])
2089 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002090
2091 filp->f_pos += cnt;
2092
2093 return cnt;
2094}
2095
2096static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002097 .open = tracing_open_generic,
2098 .read = tracing_iter_ctrl_read,
2099 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002100};
2101
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002102static const char readme_msg[] =
2103 "tracing mini-HOWTO:\n\n"
2104 "# mkdir /debug\n"
2105 "# mount -t debugfs nodev /debug\n\n"
2106 "# cat /debug/tracing/available_tracers\n"
2107 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2108 "# cat /debug/tracing/current_tracer\n"
2109 "none\n"
2110 "# echo sched_switch > /debug/tracing/current_tracer\n"
2111 "# cat /debug/tracing/current_tracer\n"
2112 "sched_switch\n"
2113 "# cat /debug/tracing/iter_ctrl\n"
2114 "noprint-parent nosym-offset nosym-addr noverbose\n"
2115 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2116 "# echo 1 > /debug/tracing/tracing_enabled\n"
2117 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2118 "echo 0 > /debug/tracing/tracing_enabled\n"
2119;
2120
2121static ssize_t
2122tracing_readme_read(struct file *filp, char __user *ubuf,
2123 size_t cnt, loff_t *ppos)
2124{
2125 return simple_read_from_buffer(ubuf, cnt, ppos,
2126 readme_msg, strlen(readme_msg));
2127}
2128
2129static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002130 .open = tracing_open_generic,
2131 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002132};
2133
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002134static ssize_t
2135tracing_ctrl_read(struct file *filp, char __user *ubuf,
2136 size_t cnt, loff_t *ppos)
2137{
2138 struct trace_array *tr = filp->private_data;
2139 char buf[64];
2140 int r;
2141
2142 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002143 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002144}
2145
2146static ssize_t
2147tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2148 size_t cnt, loff_t *ppos)
2149{
2150 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002151 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002152 long val;
2153 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002154
Steven Rostedtcffae432008-05-12 21:21:00 +02002155 if (cnt >= sizeof(buf))
2156 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002157
2158 if (copy_from_user(&buf, ubuf, cnt))
2159 return -EFAULT;
2160
2161 buf[cnt] = 0;
2162
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002163 ret = strict_strtoul(buf, 10, &val);
2164 if (ret < 0)
2165 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002166
2167 val = !!val;
2168
2169 mutex_lock(&trace_types_lock);
2170 if (tr->ctrl ^ val) {
2171 if (val)
2172 tracer_enabled = 1;
2173 else
2174 tracer_enabled = 0;
2175
2176 tr->ctrl = val;
2177
2178 if (current_trace && current_trace->ctrl_update)
2179 current_trace->ctrl_update(tr);
2180 }
2181 mutex_unlock(&trace_types_lock);
2182
2183 filp->f_pos += cnt;
2184
2185 return cnt;
2186}
2187
2188static ssize_t
2189tracing_set_trace_read(struct file *filp, char __user *ubuf,
2190 size_t cnt, loff_t *ppos)
2191{
2192 char buf[max_tracer_type_len+2];
2193 int r;
2194
2195 mutex_lock(&trace_types_lock);
2196 if (current_trace)
2197 r = sprintf(buf, "%s\n", current_trace->name);
2198 else
2199 r = sprintf(buf, "\n");
2200 mutex_unlock(&trace_types_lock);
2201
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002202 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002203}
2204
2205static ssize_t
2206tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2207 size_t cnt, loff_t *ppos)
2208{
2209 struct trace_array *tr = &global_trace;
2210 struct tracer *t;
2211 char buf[max_tracer_type_len+1];
2212 int i;
2213
2214 if (cnt > max_tracer_type_len)
2215 cnt = max_tracer_type_len;
2216
2217 if (copy_from_user(&buf, ubuf, cnt))
2218 return -EFAULT;
2219
2220 buf[cnt] = 0;
2221
2222 /* strip ending whitespace. */
2223 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2224 buf[i] = 0;
2225
2226 mutex_lock(&trace_types_lock);
2227 for (t = trace_types; t; t = t->next) {
2228 if (strcmp(t->name, buf) == 0)
2229 break;
2230 }
2231 if (!t || t == current_trace)
2232 goto out;
2233
2234 if (current_trace && current_trace->reset)
2235 current_trace->reset(tr);
2236
2237 current_trace = t;
2238 if (t->init)
2239 t->init(tr);
2240
2241 out:
2242 mutex_unlock(&trace_types_lock);
2243
2244 filp->f_pos += cnt;
2245
2246 return cnt;
2247}
2248
2249static ssize_t
2250tracing_max_lat_read(struct file *filp, char __user *ubuf,
2251 size_t cnt, loff_t *ppos)
2252{
2253 unsigned long *ptr = filp->private_data;
2254 char buf[64];
2255 int r;
2256
Steven Rostedtcffae432008-05-12 21:21:00 +02002257 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002258 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002259 if (r > sizeof(buf))
2260 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002261 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002262}
2263
2264static ssize_t
2265tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2266 size_t cnt, loff_t *ppos)
2267{
2268 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002269 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002270 long val;
2271 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002272
Steven Rostedtcffae432008-05-12 21:21:00 +02002273 if (cnt >= sizeof(buf))
2274 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002275
2276 if (copy_from_user(&buf, ubuf, cnt))
2277 return -EFAULT;
2278
2279 buf[cnt] = 0;
2280
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002281 ret = strict_strtoul(buf, 10, &val);
2282 if (ret < 0)
2283 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002284
2285 *ptr = val * 1000;
2286
2287 return cnt;
2288}
2289
Steven Rostedtb3806b42008-05-12 21:20:46 +02002290static atomic_t tracing_reader;
2291
2292static int tracing_open_pipe(struct inode *inode, struct file *filp)
2293{
2294 struct trace_iterator *iter;
2295
2296 if (tracing_disabled)
2297 return -ENODEV;
2298
2299 /* We only allow for reader of the pipe */
2300 if (atomic_inc_return(&tracing_reader) != 1) {
2301 atomic_dec(&tracing_reader);
2302 return -EBUSY;
2303 }
2304
2305 /* create a buffer to store the information to pass to userspace */
2306 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2307 if (!iter)
2308 return -ENOMEM;
2309
2310 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002311 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002312
2313 filp->private_data = iter;
2314
2315 return 0;
2316}
2317
2318static int tracing_release_pipe(struct inode *inode, struct file *file)
2319{
2320 struct trace_iterator *iter = file->private_data;
2321
2322 kfree(iter);
2323 atomic_dec(&tracing_reader);
2324
2325 return 0;
2326}
2327
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002328static unsigned int
2329tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2330{
2331 struct trace_iterator *iter = filp->private_data;
2332
2333 if (trace_flags & TRACE_ITER_BLOCK) {
2334 /*
2335 * Always select as readable when in blocking mode
2336 */
2337 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002338 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002339 if (!trace_empty(iter))
2340 return POLLIN | POLLRDNORM;
2341 poll_wait(filp, &trace_wait, poll_table);
2342 if (!trace_empty(iter))
2343 return POLLIN | POLLRDNORM;
2344
2345 return 0;
2346 }
2347}
2348
Steven Rostedtb3806b42008-05-12 21:20:46 +02002349/*
2350 * Consumer reader.
2351 */
2352static ssize_t
2353tracing_read_pipe(struct file *filp, char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2355{
2356 struct trace_iterator *iter = filp->private_data;
2357 struct trace_array_cpu *data;
2358 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002359 static int start;
2360 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002361#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002362 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002363#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002364 int read = 0;
2365 int cpu;
2366 int len;
2367 int ret;
2368
2369 /* return any leftover data */
2370 if (iter->seq.len > start) {
2371 len = iter->seq.len - start;
2372 if (cnt > len)
2373 cnt = len;
2374 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2375 if (ret)
2376 cnt = -EFAULT;
2377
2378 start += len;
2379
2380 return cnt;
2381 }
2382
2383 trace_seq_reset(&iter->seq);
2384 start = 0;
2385
2386 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002387
2388 if ((filp->f_flags & O_NONBLOCK))
2389 return -EAGAIN;
2390
Steven Rostedtb3806b42008-05-12 21:20:46 +02002391 /*
2392 * This is a make-shift waitqueue. The reason we don't use
2393 * an actual wait queue is because:
2394 * 1) we only ever have one waiter
2395 * 2) the tracing, traces all functions, we don't want
2396 * the overhead of calling wake_up and friends
2397 * (and tracing them too)
2398 * Anyway, this is really very primitive wakeup.
2399 */
2400 set_current_state(TASK_INTERRUPTIBLE);
2401 iter->tr->waiter = current;
2402
2403 /* sleep for one second, and try again. */
2404 schedule_timeout(HZ);
2405
2406 iter->tr->waiter = NULL;
2407
2408 if (signal_pending(current))
2409 return -EINTR;
2410
Steven Rostedt84527992008-05-12 21:20:58 +02002411 if (iter->trace != current_trace)
2412 return 0;
2413
Steven Rostedtb3806b42008-05-12 21:20:46 +02002414 /*
2415 * We block until we read something and tracing is disabled.
2416 * We still block if tracing is disabled, but we have never
2417 * read anything. This allows a user to cat this file, and
2418 * then enable tracing. But after we have read something,
2419 * we give an EOF when tracing is again disabled.
2420 *
2421 * iter->pos will be 0 if we haven't read anything.
2422 */
2423 if (!tracer_enabled && iter->pos)
2424 break;
2425
2426 continue;
2427 }
2428
2429 /* stop when tracing is finished */
2430 if (trace_empty(iter))
2431 return 0;
2432
2433 if (cnt >= PAGE_SIZE)
2434 cnt = PAGE_SIZE - 1;
2435
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002436 /* reset all but tr, trace, and overruns */
Steven Rostedtb3806b42008-05-12 21:20:46 +02002437 iter->pos = -1;
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002438 memset(&iter->seq, 0,
2439 sizeof(struct trace_iterator) -
2440 offsetof(struct trace_iterator, seq));
Steven Rostedtb3806b42008-05-12 21:20:46 +02002441
2442 /*
2443 * We need to stop all tracing on all CPUS to read the
2444 * the next buffer. This is a bit expensive, but is
2445 * not done often. We fill all what we can read,
2446 * and then release the locks again.
2447 */
2448
2449 cpus_clear(mask);
2450 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002451#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002452 ftrace_save = ftrace_enabled;
2453 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002454#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002455 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002456 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002457 data = iter->tr->data[cpu];
2458
2459 if (!head_page(data) || !data->trace_idx)
2460 continue;
2461
2462 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002463 cpu_set(cpu, mask);
2464 }
2465
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002466 for_each_cpu_mask(cpu, mask) {
2467 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002468 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002469
2470 if (data->overrun > iter->last_overrun[cpu])
2471 iter->overrun[cpu] +=
2472 data->overrun - iter->last_overrun[cpu];
2473 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002474 }
2475
Steven Rostedt088b1e422008-05-12 21:20:48 +02002476 while (find_next_entry_inc(iter) != NULL) {
2477 int len = iter->seq.len;
2478
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002479 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002480 if (!ret) {
2481 /* don't print partial lines */
2482 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002483 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002484 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002485
2486 trace_consume(iter);
2487
2488 if (iter->seq.len >= cnt)
2489 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002490 }
2491
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002492 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002493 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002494 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002495 }
2496
2497 for_each_cpu_mask(cpu, mask) {
2498 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002499 atomic_dec(&data->disabled);
2500 }
Ingo Molnar25770462008-05-12 21:20:49 +02002501#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002502 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002503#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002504 local_irq_restore(flags);
2505
2506 /* Now copy what we have to the user */
2507 read = iter->seq.len;
2508 if (read > cnt)
2509 read = cnt;
2510
2511 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2512
2513 if (read < iter->seq.len)
2514 start = read;
2515 else
2516 trace_seq_reset(&iter->seq);
2517
2518 if (ret)
2519 read = -EFAULT;
2520
2521 return read;
2522}
2523
Steven Rostedta98a3c32008-05-12 21:20:59 +02002524static ssize_t
2525tracing_entries_read(struct file *filp, char __user *ubuf,
2526 size_t cnt, loff_t *ppos)
2527{
2528 struct trace_array *tr = filp->private_data;
2529 char buf[64];
2530 int r;
2531
2532 r = sprintf(buf, "%lu\n", tr->entries);
2533 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2534}
2535
2536static ssize_t
2537tracing_entries_write(struct file *filp, const char __user *ubuf,
2538 size_t cnt, loff_t *ppos)
2539{
2540 unsigned long val;
2541 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002542 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002543
Steven Rostedtcffae432008-05-12 21:21:00 +02002544 if (cnt >= sizeof(buf))
2545 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002546
2547 if (copy_from_user(&buf, ubuf, cnt))
2548 return -EFAULT;
2549
2550 buf[cnt] = 0;
2551
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002552 ret = strict_strtoul(buf, 10, &val);
2553 if (ret < 0)
2554 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002555
2556 /* must have at least 1 entry */
2557 if (!val)
2558 return -EINVAL;
2559
2560 mutex_lock(&trace_types_lock);
2561
2562 if (current_trace != &no_tracer) {
2563 cnt = -EBUSY;
2564 pr_info("ftrace: set current_tracer to none"
2565 " before modifying buffer size\n");
2566 goto out;
2567 }
2568
2569 if (val > global_trace.entries) {
2570 while (global_trace.entries < val) {
2571 if (trace_alloc_page()) {
2572 cnt = -ENOMEM;
2573 goto out;
2574 }
2575 }
2576 } else {
2577 /* include the number of entries in val (inc of page entries) */
2578 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2579 trace_free_page();
2580 }
2581
2582 filp->f_pos += cnt;
2583
2584 out:
2585 max_tr.entries = global_trace.entries;
2586 mutex_unlock(&trace_types_lock);
2587
2588 return cnt;
2589}
2590
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002591static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002592 .open = tracing_open_generic,
2593 .read = tracing_max_lat_read,
2594 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002595};
2596
2597static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002598 .open = tracing_open_generic,
2599 .read = tracing_ctrl_read,
2600 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002601};
2602
2603static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002604 .open = tracing_open_generic,
2605 .read = tracing_set_trace_read,
2606 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002607};
2608
Steven Rostedtb3806b42008-05-12 21:20:46 +02002609static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002610 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002611 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002612 .read = tracing_read_pipe,
2613 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002614};
2615
Steven Rostedta98a3c32008-05-12 21:20:59 +02002616static struct file_operations tracing_entries_fops = {
2617 .open = tracing_open_generic,
2618 .read = tracing_entries_read,
2619 .write = tracing_entries_write,
2620};
2621
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002622#ifdef CONFIG_DYNAMIC_FTRACE
2623
2624static ssize_t
2625tracing_read_long(struct file *filp, char __user *ubuf,
2626 size_t cnt, loff_t *ppos)
2627{
2628 unsigned long *p = filp->private_data;
2629 char buf[64];
2630 int r;
2631
2632 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002633
2634 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002635}
2636
2637static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002638 .open = tracing_open_generic,
2639 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002640};
2641#endif
2642
2643static struct dentry *d_tracer;
2644
2645struct dentry *tracing_init_dentry(void)
2646{
2647 static int once;
2648
2649 if (d_tracer)
2650 return d_tracer;
2651
2652 d_tracer = debugfs_create_dir("tracing", NULL);
2653
2654 if (!d_tracer && !once) {
2655 once = 1;
2656 pr_warning("Could not create debugfs directory 'tracing'\n");
2657 return NULL;
2658 }
2659
2660 return d_tracer;
2661}
2662
Steven Rostedt60a11772008-05-12 21:20:44 +02002663#ifdef CONFIG_FTRACE_SELFTEST
2664/* Let selftest have access to static functions in this file */
2665#include "trace_selftest.c"
2666#endif
2667
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002668static __init void tracer_init_debugfs(void)
2669{
2670 struct dentry *d_tracer;
2671 struct dentry *entry;
2672
2673 d_tracer = tracing_init_dentry();
2674
2675 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2676 &global_trace, &tracing_ctrl_fops);
2677 if (!entry)
2678 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2679
2680 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2681 NULL, &tracing_iter_fops);
2682 if (!entry)
2683 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2684
Ingo Molnarc7078de2008-05-12 21:20:52 +02002685 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2686 NULL, &tracing_cpumask_fops);
2687 if (!entry)
2688 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2689
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002690 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2691 &global_trace, &tracing_lt_fops);
2692 if (!entry)
2693 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2694
2695 entry = debugfs_create_file("trace", 0444, d_tracer,
2696 &global_trace, &tracing_fops);
2697 if (!entry)
2698 pr_warning("Could not create debugfs 'trace' entry\n");
2699
2700 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2701 &global_trace, &show_traces_fops);
2702 if (!entry)
2703 pr_warning("Could not create debugfs 'trace' entry\n");
2704
2705 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2706 &global_trace, &set_tracer_fops);
2707 if (!entry)
2708 pr_warning("Could not create debugfs 'trace' entry\n");
2709
2710 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2711 &tracing_max_latency,
2712 &tracing_max_lat_fops);
2713 if (!entry)
2714 pr_warning("Could not create debugfs "
2715 "'tracing_max_latency' entry\n");
2716
2717 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2718 &tracing_thresh, &tracing_max_lat_fops);
2719 if (!entry)
2720 pr_warning("Could not create debugfs "
2721 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002722 entry = debugfs_create_file("README", 0644, d_tracer,
2723 NULL, &tracing_readme_fops);
2724 if (!entry)
2725 pr_warning("Could not create debugfs 'README' entry\n");
2726
Steven Rostedtb3806b42008-05-12 21:20:46 +02002727 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2728 NULL, &tracing_pipe_fops);
2729 if (!entry)
2730 pr_warning("Could not create debugfs "
2731 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002732
Steven Rostedta98a3c32008-05-12 21:20:59 +02002733 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2734 &global_trace, &tracing_entries_fops);
2735 if (!entry)
2736 pr_warning("Could not create debugfs "
2737 "'tracing_threash' entry\n");
2738
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002739#ifdef CONFIG_DYNAMIC_FTRACE
2740 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2741 &ftrace_update_tot_cnt,
2742 &tracing_read_long_fops);
2743 if (!entry)
2744 pr_warning("Could not create debugfs "
2745 "'dyn_ftrace_total_info' entry\n");
2746#endif
2747}
2748
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002749static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002750{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002751 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002752 struct page *page, *tmp;
2753 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002754 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002755 int i;
2756
2757 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002758 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002759 array = (void *)__get_free_page(GFP_KERNEL);
2760 if (array == NULL) {
2761 printk(KERN_ERR "tracer: failed to allocate page"
2762 "for trace buffer!\n");
2763 goto free_pages;
2764 }
2765
2766 page = virt_to_page(array);
2767 list_add(&page->lru, &pages);
2768
2769/* Only allocate if we are actually using the max trace */
2770#ifdef CONFIG_TRACER_MAX_TRACE
2771 array = (void *)__get_free_page(GFP_KERNEL);
2772 if (array == NULL) {
2773 printk(KERN_ERR "tracer: failed to allocate page"
2774 "for trace buffer!\n");
2775 goto free_pages;
2776 }
2777 page = virt_to_page(array);
2778 list_add(&page->lru, &pages);
2779#endif
2780 }
2781
2782 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002783 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002784 data = global_trace.data[i];
2785 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002786 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002787 list_add_tail(&page->lru, &data->trace_pages);
2788 ClearPageLRU(page);
2789
2790#ifdef CONFIG_TRACER_MAX_TRACE
2791 data = max_tr.data[i];
2792 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002793 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002794 list_add_tail(&page->lru, &data->trace_pages);
2795 SetPageLRU(page);
2796#endif
2797 }
2798 global_trace.entries += ENTRIES_PER_PAGE;
2799
2800 return 0;
2801
2802 free_pages:
2803 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002804 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002805 __free_page(page);
2806 }
2807 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002808}
2809
Steven Rostedta98a3c32008-05-12 21:20:59 +02002810static int trace_free_page(void)
2811{
2812 struct trace_array_cpu *data;
2813 struct page *page;
2814 struct list_head *p;
2815 int i;
2816 int ret = 0;
2817
2818 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002819 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002820 data = global_trace.data[i];
2821 p = data->trace_pages.next;
2822 if (p == &data->trace_pages) {
2823 /* should never happen */
2824 WARN_ON(1);
2825 tracing_disabled = 1;
2826 ret = -1;
2827 break;
2828 }
2829 page = list_entry(p, struct page, lru);
2830 ClearPageLRU(page);
2831 list_del(&page->lru);
2832 __free_page(page);
2833
2834 tracing_reset(data);
2835
2836#ifdef CONFIG_TRACER_MAX_TRACE
2837 data = max_tr.data[i];
2838 p = data->trace_pages.next;
2839 if (p == &data->trace_pages) {
2840 /* should never happen */
2841 WARN_ON(1);
2842 tracing_disabled = 1;
2843 ret = -1;
2844 break;
2845 }
2846 page = list_entry(p, struct page, lru);
2847 ClearPageLRU(page);
2848 list_del(&page->lru);
2849 __free_page(page);
2850
2851 tracing_reset(data);
2852#endif
2853 }
2854 global_trace.entries -= ENTRIES_PER_PAGE;
2855
2856 return ret;
2857}
2858
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002859__init static int tracer_alloc_buffers(void)
2860{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002861 struct trace_array_cpu *data;
2862 void *array;
2863 struct page *page;
2864 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002865 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002866 int i;
2867
Steven Rostedt26994ea2008-05-12 21:20:48 +02002868 global_trace.ctrl = tracer_enabled;
2869
Steven Rostedtab464282008-05-12 21:21:00 +02002870 /* TODO: make the number of buffers hot pluggable with CPUS */
2871 tracing_nr_buffers = num_possible_cpus();
2872 tracing_buffer_mask = cpu_possible_map;
2873
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002874 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02002875 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002876 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002877 max_tr.data[i] = &per_cpu(max_data, i);
2878
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002879 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002880 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002881 printk(KERN_ERR "tracer: failed to allocate page"
2882 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002883 goto free_buffers;
2884 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002885
2886 /* set the array to the list */
2887 INIT_LIST_HEAD(&data->trace_pages);
2888 page = virt_to_page(array);
2889 list_add(&page->lru, &data->trace_pages);
2890 /* use the LRU flag to differentiate the two buffers */
2891 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002892
Steven Rostedta98a3c32008-05-12 21:20:59 +02002893 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2894 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2895
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002896/* Only allocate if we are actually using the max trace */
2897#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002898 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002899 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002900 printk(KERN_ERR "tracer: failed to allocate page"
2901 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002902 goto free_buffers;
2903 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002904
2905 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2906 page = virt_to_page(array);
2907 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2908 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002909#endif
2910 }
2911
2912 /*
2913 * Since we allocate by orders of pages, we may be able to
2914 * round up a bit.
2915 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002916 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002917 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002918
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002919 while (global_trace.entries < trace_nr_entries) {
2920 if (trace_alloc_page())
2921 break;
2922 pages++;
2923 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002924 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002925
2926 pr_info("tracer: %d pages allocated for %ld",
2927 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002928 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2929 pr_info(" actual entries %ld\n", global_trace.entries);
2930
2931 tracer_init_debugfs();
2932
2933 trace_init_cmdlines();
2934
2935 register_tracer(&no_tracer);
2936 current_trace = &no_tracer;
2937
Steven Rostedt60a11772008-05-12 21:20:44 +02002938 /* All seems OK, enable tracing */
2939 tracing_disabled = 0;
2940
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002941 return 0;
2942
2943 free_buffers:
2944 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002945 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002946 struct trace_array_cpu *data = global_trace.data[i];
2947
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002948 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002949 list_for_each_entry_safe(page, tmp,
2950 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002951 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002952 __free_page(page);
2953 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002954 }
2955
2956#ifdef CONFIG_TRACER_MAX_TRACE
2957 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002958 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002959 list_for_each_entry_safe(page, tmp,
2960 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002961 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002962 __free_page(page);
2963 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002964 }
2965#endif
2966 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002967 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002968}
Steven Rostedt60a11772008-05-12 21:20:44 +02002969fs_initcall(tracer_alloc_buffers);