blob: a917bea827155dbcfe74425d723de10073617986 [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>
Abhishek Sagar76094a22008-05-28 00:03:18 +053030#include <linux/kprobes.h>
Steven Rostedt3eefae92008-05-12 21:21:04 +020031#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020032
Ingo Molnar86387f72008-05-12 21:20:51 +020033#include <linux/stacktrace.h>
34
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020035#include "trace.h"
36
37unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
38unsigned long __read_mostly tracing_thresh;
39
Steven Rostedtab464282008-05-12 21:21:00 +020040static unsigned long __read_mostly tracing_nr_buffers;
41static cpumask_t __read_mostly tracing_buffer_mask;
42
43#define for_each_tracing_cpu(cpu) \
44 for_each_cpu_mask(cpu, tracing_buffer_mask)
45
Steven Rostedta98a3c32008-05-12 21:20:59 +020046static int trace_alloc_page(void);
47static int trace_free_page(void);
48
Steven Rostedt60a11772008-05-12 21:20:44 +020049static int tracing_disabled = 1;
50
Steven Rostedt3eefae92008-05-12 21:21:04 +020051static unsigned long tracing_pages_allocated;
52
Thomas Gleixner72829bc2008-05-23 21:37:28 +020053long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020054ns2usecs(cycle_t nsec)
55{
56 nsec += 500;
57 do_div(nsec, 1000);
58 return nsec;
59}
60
Ingo Molnare309b412008-05-12 21:20:51 +020061cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020062{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020063 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020064}
65
Steven Rostedt4fcdae82008-05-12 21:21:00 +020066/*
67 * The global_trace is the descriptor that holds the tracing
68 * buffers for the live tracing. For each CPU, it contains
69 * a link list of pages that will store trace entries. The
70 * page descriptor of the pages in the memory is used to hold
71 * the link list by linking the lru item in the page descriptor
72 * to each of the pages in the buffer per CPU.
73 *
74 * For each active CPU there is a data field that holds the
75 * pages for the buffer for that CPU. Each CPU has the same number
76 * of pages allocated for its buffer.
77 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020078static struct trace_array global_trace;
79
80static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
81
Steven Rostedt4fcdae82008-05-12 21:21:00 +020082/*
83 * The max_tr is used to snapshot the global_trace when a maximum
84 * latency is reached. Some tracers will use this to store a maximum
85 * trace while it continues examining live traces.
86 *
87 * The buffers for the max_tr are set up the same as the global_trace.
88 * When a snapshot is taken, the link list of the max_tr is swapped
89 * with the link list of the global_trace and the buffers are reset for
90 * the global_trace so the tracing can continue.
91 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020092static struct trace_array max_tr;
93
94static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
95
Steven Rostedt4fcdae82008-05-12 21:21:00 +020096/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +020097static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +020098
Steven Rostedt60bc0802008-07-10 20:58:16 -040099/* function tracing enabled */
100int ftrace_function_enabled;
101
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200102/*
103 * trace_nr_entries is the number of entries that is allocated
104 * for a buffer. Note, the number of entries is always rounded
105 * to ENTRIES_PER_PAGE.
106 */
Ingo Molnar57422792008-05-12 21:20:51 +0200107static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200108
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200109/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200110static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200111
112/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200113static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200114
115/*
116 * max_tracer_type_len is used to simplify the allocating of
117 * buffers to read userspace tracer names. We keep track of
118 * the longest tracer name registered.
119 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200120static int max_tracer_type_len;
121
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200122/*
123 * trace_types_lock is used to protect the trace_types list.
124 * This lock is also used to keep user access serialized.
125 * Accesses from userspace will grab this lock while userspace
126 * activities happen inside the kernel.
127 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200128static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200129
130/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200131static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
132
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200133/* trace_flags holds iter_ctrl options */
Ingo Molnar4e655512008-05-12 21:20:52 +0200134unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
135
Ankita Garg2b1bce12008-06-09 14:10:25 +0530136static notrace void no_trace_init(struct trace_array *tr)
137{
138 int cpu;
139
Steven Rostedt60bc0802008-07-10 20:58:16 -0400140 ftrace_function_enabled = 0;
Ankita Garg2b1bce12008-06-09 14:10:25 +0530141 if(tr->ctrl)
142 for_each_online_cpu(cpu)
143 tracing_reset(tr->data[cpu]);
144 tracer_enabled = 0;
145}
146
147/* dummy trace to disable tracing */
148static struct tracer no_tracer __read_mostly = {
149 .name = "none",
150 .init = no_trace_init
151};
152
153
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200154/**
155 * trace_wake_up - wake up tasks waiting for trace input
156 *
157 * Simply wakes up any task that is blocked on the trace_wait
158 * queue. These is used with trace_poll for tasks polling the trace.
159 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200160void trace_wake_up(void)
161{
Ingo Molnar017730c2008-05-12 21:20:52 +0200162 /*
163 * The runqueue_is_locked() can fail, but this is the best we
164 * have for now:
165 */
166 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200167 wake_up(&trace_wait);
168}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200169
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200170#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
171
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200172static int __init set_nr_entries(char *str)
173{
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200174 unsigned long nr_entries;
175 int ret;
176
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200177 if (!str)
178 return 0;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200179 ret = strict_strtoul(str, 0, &nr_entries);
180 /* nr_entries can not be zero */
181 if (ret < 0 || nr_entries == 0)
182 return 0;
183 trace_nr_entries = nr_entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200184 return 1;
185}
186__setup("trace_entries=", set_nr_entries);
187
Steven Rostedt57f50be2008-05-12 21:20:44 +0200188unsigned long nsecs_to_usecs(unsigned long nsecs)
189{
190 return nsecs / 1000;
191}
192
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200193/*
194 * trace_flag_type is an enumeration that holds different
195 * states when a trace occurs. These are:
196 * IRQS_OFF - interrupts were disabled
197 * NEED_RESCED - reschedule is requested
198 * HARDIRQ - inside an interrupt handler
199 * SOFTIRQ - inside a softirq handler
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400200 * CONT - multiple entries hold the trace item
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200201 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200202enum trace_flag_type {
203 TRACE_FLAG_IRQS_OFF = 0x01,
204 TRACE_FLAG_NEED_RESCHED = 0x02,
205 TRACE_FLAG_HARDIRQ = 0x04,
206 TRACE_FLAG_SOFTIRQ = 0x08,
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400207 TRACE_FLAG_CONT = 0x10,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200208};
209
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200210/*
211 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
212 * control the output of kernel symbols.
213 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200214#define TRACE_ITER_SYM_MASK \
215 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
216
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200217/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200218static const char *trace_options[] = {
219 "print-parent",
220 "sym-offset",
221 "sym-addr",
222 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200223 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200224 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200225 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200226 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200227 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200228 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200229 NULL
230};
231
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200232/*
233 * ftrace_max_lock is used to protect the swapping of buffers
234 * when taking a max snapshot. The buffers themselves are
235 * protected by per_cpu spinlocks. But the action of the swap
236 * needs its own lock.
237 *
238 * This is defined as a raw_spinlock_t in order to help
239 * with performance when lockdep debugging is enabled.
240 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200241static raw_spinlock_t ftrace_max_lock =
242 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200243
244/*
245 * Copy the new maximum trace into the separate maximum-trace
246 * structure. (this way the maximum trace is permanently saved,
247 * for later retrieval via /debugfs/tracing/latency_trace)
248 */
Ingo Molnare309b412008-05-12 21:20:51 +0200249static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200250__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
251{
252 struct trace_array_cpu *data = tr->data[cpu];
253
254 max_tr.cpu = cpu;
255 max_tr.time_start = data->preempt_timestamp;
256
257 data = max_tr.data[cpu];
258 data->saved_latency = tracing_max_latency;
259
260 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
261 data->pid = tsk->pid;
262 data->uid = tsk->uid;
263 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
264 data->policy = tsk->policy;
265 data->rt_priority = tsk->rt_priority;
266
267 /* record this tasks comm */
268 tracing_record_cmdline(current);
269}
270
Steven Rostedt19384c02008-05-22 00:22:16 -0400271#define CHECK_COND(cond) \
272 if (unlikely(cond)) { \
273 tracing_disabled = 1; \
274 WARN_ON(1); \
275 return -1; \
276 }
277
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200278/**
279 * check_pages - integrity check of trace buffers
280 *
281 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400282 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200283 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400284int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200285{
286 struct page *page, *tmp;
287
Steven Rostedt19384c02008-05-22 00:22:16 -0400288 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
289 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200290
291 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400292 CHECK_COND(page->lru.next->prev != &page->lru);
293 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200294 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400295
296 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200297}
298
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200299/**
300 * head_page - page address of the first page in per_cpu buffer.
301 *
302 * head_page returns the page address of the first page in
303 * a per_cpu buffer. This also preforms various consistency
304 * checks to make sure the buffer has not been corrupted.
305 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200306void *head_page(struct trace_array_cpu *data)
307{
308 struct page *page;
309
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200310 if (list_empty(&data->trace_pages))
311 return NULL;
312
313 page = list_entry(data->trace_pages.next, struct page, lru);
314 BUG_ON(&page->lru == &data->trace_pages);
315
316 return page_address(page);
317}
318
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200319/**
320 * trace_seq_printf - sequence printing of trace information
321 * @s: trace sequence descriptor
322 * @fmt: printf format string
323 *
324 * The tracer may use either sequence operations or its own
325 * copy to user routines. To simplify formating of a trace
326 * trace_seq_printf is used to store strings into a special
327 * buffer (@s). Then the output may be either used by
328 * the sequencer or pulled into another buffer.
329 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200330int
Steven Rostedt214023c2008-05-12 21:20:46 +0200331trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
332{
333 int len = (PAGE_SIZE - 1) - s->len;
334 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200335 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200336
337 if (!len)
338 return 0;
339
340 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200341 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200342 va_end(ap);
343
Steven Rostedtb3806b42008-05-12 21:20:46 +0200344 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200345 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200346 return 0;
347
348 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200349
350 return len;
351}
352
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200353/**
354 * trace_seq_puts - trace sequence printing of simple string
355 * @s: trace sequence descriptor
356 * @str: simple string to record
357 *
358 * The tracer may use either the sequence operations or its own
359 * copy to user routines. This function records a simple string
360 * into a special buffer (@s) for later retrieval by a sequencer
361 * or other mechanism.
362 */
Ingo Molnare309b412008-05-12 21:20:51 +0200363static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200364trace_seq_puts(struct trace_seq *s, const char *str)
365{
366 int len = strlen(str);
367
368 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200369 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200370
371 memcpy(s->buffer + s->len, str, len);
372 s->len += len;
373
374 return len;
375}
376
Ingo Molnare309b412008-05-12 21:20:51 +0200377static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200378trace_seq_putc(struct trace_seq *s, unsigned char c)
379{
380 if (s->len >= (PAGE_SIZE - 1))
381 return 0;
382
383 s->buffer[s->len++] = c;
384
385 return 1;
386}
387
Ingo Molnare309b412008-05-12 21:20:51 +0200388static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200389trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
390{
391 if (len > ((PAGE_SIZE - 1) - s->len))
392 return 0;
393
394 memcpy(s->buffer + s->len, mem, len);
395 s->len += len;
396
397 return len;
398}
399
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200400#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200401static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200402
Ingo Molnare309b412008-05-12 21:20:51 +0200403static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200404trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
405{
406 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200407 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200408 unsigned char byte;
409 int i, j;
410
411 BUG_ON(len >= HEX_CHARS);
412
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200413#ifdef __BIG_ENDIAN
414 for (i = 0, j = 0; i < len; i++) {
415#else
416 for (i = len-1, j = 0; i >= 0; i--) {
417#endif
418 byte = data[i];
419
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200420 hex[j++] = hex2asc[byte & 0x0f];
421 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200422 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200423 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200424
425 return trace_seq_putmem(s, hex, j);
426}
427
Ingo Molnare309b412008-05-12 21:20:51 +0200428static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200429trace_seq_reset(struct trace_seq *s)
430{
431 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200432 s->readpos = 0;
433}
434
435ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
436{
437 int len;
438 int ret;
439
440 if (s->len <= s->readpos)
441 return -EBUSY;
442
443 len = s->len - s->readpos;
444 if (cnt > len)
445 cnt = len;
446 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
447 if (ret)
448 return -EFAULT;
449
450 s->readpos += len;
451 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200452}
453
Ingo Molnare309b412008-05-12 21:20:51 +0200454static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200455trace_print_seq(struct seq_file *m, struct trace_seq *s)
456{
457 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
458
459 s->buffer[len] = 0;
460 seq_puts(m, s->buffer);
461
462 trace_seq_reset(s);
463}
464
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200465/*
466 * flip the trace buffers between two trace descriptors.
467 * This usually is the buffers between the global_trace and
468 * the max_tr to record a snapshot of a current trace.
469 *
470 * The ftrace_max_lock must be held.
471 */
Ingo Molnare309b412008-05-12 21:20:51 +0200472static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200473flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
474{
475 struct list_head flip_pages;
476
477 INIT_LIST_HEAD(&flip_pages);
478
Steven Rostedt93a588f2008-05-12 21:20:45 +0200479 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200480 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200481 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200482
483 check_pages(tr1);
484 check_pages(tr2);
485 list_splice_init(&tr1->trace_pages, &flip_pages);
486 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
487 list_splice_init(&flip_pages, &tr2->trace_pages);
488 BUG_ON(!list_empty(&flip_pages));
489 check_pages(tr1);
490 check_pages(tr2);
491}
492
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200493/**
494 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
495 * @tr: tracer
496 * @tsk: the task with the latency
497 * @cpu: The cpu that initiated the trace.
498 *
499 * Flip the buffers between the @tr and the max_tr and record information
500 * about which task was the cause of this latency.
501 */
Ingo Molnare309b412008-05-12 21:20:51 +0200502void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200503update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
504{
505 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200506 int i;
507
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200508 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200509 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200510 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200511 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200512 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200513 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200514 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200515 }
516
517 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200518 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200519}
520
521/**
522 * update_max_tr_single - only copy one trace over, and reset the rest
523 * @tr - tracer
524 * @tsk - task with the latency
525 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200526 *
527 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200528 */
Ingo Molnare309b412008-05-12 21:20:51 +0200529void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200530update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
531{
532 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200533 int i;
534
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200535 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200536 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200537 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200538 tracing_reset(max_tr.data[i]);
539
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200540 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200541 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200542
543 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200544 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200545}
546
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200547/**
548 * register_tracer - register a tracer with the ftrace system.
549 * @type - the plugin for the tracer
550 *
551 * Register a new plugin tracer.
552 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200553int register_tracer(struct tracer *type)
554{
555 struct tracer *t;
556 int len;
557 int ret = 0;
558
559 if (!type->name) {
560 pr_info("Tracer must have a name\n");
561 return -1;
562 }
563
564 mutex_lock(&trace_types_lock);
565 for (t = trace_types; t; t = t->next) {
566 if (strcmp(type->name, t->name) == 0) {
567 /* already found */
568 pr_info("Trace %s already registered\n",
569 type->name);
570 ret = -1;
571 goto out;
572 }
573 }
574
Steven Rostedt60a11772008-05-12 21:20:44 +0200575#ifdef CONFIG_FTRACE_STARTUP_TEST
576 if (type->selftest) {
577 struct tracer *saved_tracer = current_trace;
578 struct trace_array_cpu *data;
579 struct trace_array *tr = &global_trace;
580 int saved_ctrl = tr->ctrl;
581 int i;
582 /*
583 * Run a selftest on this tracer.
584 * Here we reset the trace buffer, and set the current
585 * tracer to be this tracer. The tracer can then run some
586 * internal tracing to verify that everything is in order.
587 * If we fail, we do not register this tracer.
588 */
Steven Rostedtab464282008-05-12 21:21:00 +0200589 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200590 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200591 if (!head_page(data))
592 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200593 tracing_reset(data);
594 }
595 current_trace = type;
596 tr->ctrl = 0;
597 /* the test is responsible for initializing and enabling */
598 pr_info("Testing tracer %s: ", type->name);
599 ret = type->selftest(type, tr);
600 /* the test is responsible for resetting too */
601 current_trace = saved_tracer;
602 tr->ctrl = saved_ctrl;
603 if (ret) {
604 printk(KERN_CONT "FAILED!\n");
605 goto out;
606 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200607 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200608 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200609 data = tr->data[i];
610 if (!head_page(data))
611 continue;
612 tracing_reset(data);
613 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200614 printk(KERN_CONT "PASSED\n");
615 }
616#endif
617
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200618 type->next = trace_types;
619 trace_types = type;
620 len = strlen(type->name);
621 if (len > max_tracer_type_len)
622 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200623
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200624 out:
625 mutex_unlock(&trace_types_lock);
626
627 return ret;
628}
629
630void unregister_tracer(struct tracer *type)
631{
632 struct tracer **t;
633 int len;
634
635 mutex_lock(&trace_types_lock);
636 for (t = &trace_types; *t; t = &(*t)->next) {
637 if (*t == type)
638 goto found;
639 }
640 pr_info("Trace %s not registered\n", type->name);
641 goto out;
642
643 found:
644 *t = (*t)->next;
645 if (strlen(type->name) != max_tracer_type_len)
646 goto out;
647
648 max_tracer_type_len = 0;
649 for (t = &trace_types; *t; t = &(*t)->next) {
650 len = strlen((*t)->name);
651 if (len > max_tracer_type_len)
652 max_tracer_type_len = len;
653 }
654 out:
655 mutex_unlock(&trace_types_lock);
656}
657
Ingo Molnare309b412008-05-12 21:20:51 +0200658void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200659{
660 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200661 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200662 data->trace_head = data->trace_tail = head_page(data);
663 data->trace_head_idx = 0;
664 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200665}
666
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200667#define SAVED_CMDLINES 128
668static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
669static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
670static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
671static int cmdline_idx;
672static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200673
Steven Rostedt25b0b442008-05-12 21:21:00 +0200674/* temporary disable recording */
675atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200676
677static void trace_init_cmdlines(void)
678{
679 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
680 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
681 cmdline_idx = 0;
682}
683
Ingo Molnare309b412008-05-12 21:20:51 +0200684void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200685
Ingo Molnare309b412008-05-12 21:20:51 +0200686static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200687{
688 unsigned map;
689 unsigned idx;
690
691 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
692 return;
693
694 /*
695 * It's not the end of the world if we don't get
696 * the lock, but we also don't want to spin
697 * nor do we want to disable interrupts,
698 * so if we miss here, then better luck next time.
699 */
700 if (!spin_trylock(&trace_cmdline_lock))
701 return;
702
703 idx = map_pid_to_cmdline[tsk->pid];
704 if (idx >= SAVED_CMDLINES) {
705 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
706
707 map = map_cmdline_to_pid[idx];
708 if (map <= PID_MAX_DEFAULT)
709 map_pid_to_cmdline[map] = (unsigned)-1;
710
711 map_pid_to_cmdline[tsk->pid] = idx;
712
713 cmdline_idx = idx;
714 }
715
716 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
717
718 spin_unlock(&trace_cmdline_lock);
719}
720
Ingo Molnare309b412008-05-12 21:20:51 +0200721static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200722{
723 char *cmdline = "<...>";
724 unsigned map;
725
726 if (!pid)
727 return "<idle>";
728
729 if (pid > PID_MAX_DEFAULT)
730 goto out;
731
732 map = map_pid_to_cmdline[pid];
733 if (map >= SAVED_CMDLINES)
734 goto out;
735
736 cmdline = saved_cmdlines[map];
737
738 out:
739 return cmdline;
740}
741
Ingo Molnare309b412008-05-12 21:20:51 +0200742void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200743{
744 if (atomic_read(&trace_record_cmdline_disabled))
745 return;
746
747 trace_save_cmdline(tsk);
748}
749
Ingo Molnare309b412008-05-12 21:20:51 +0200750static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200751trace_next_list(struct trace_array_cpu *data, struct list_head *next)
752{
753 /*
754 * Roundrobin - but skip the head (which is not a real page):
755 */
756 next = next->next;
757 if (unlikely(next == &data->trace_pages))
758 next = next->next;
759 BUG_ON(next == &data->trace_pages);
760
761 return next;
762}
763
Ingo Molnare309b412008-05-12 21:20:51 +0200764static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200765trace_next_page(struct trace_array_cpu *data, void *addr)
766{
767 struct list_head *next;
768 struct page *page;
769
770 page = virt_to_page(addr);
771
772 next = trace_next_list(data, &page->lru);
773 page = list_entry(next, struct page, lru);
774
775 return page_address(page);
776}
777
Ingo Molnare309b412008-05-12 21:20:51 +0200778static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200779tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200780{
781 unsigned long idx, idx_next;
782 struct trace_entry *entry;
783
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200784 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200785 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200786 idx_next = idx + 1;
787
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200788 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
789
Steven Rostedt93a588f2008-05-12 21:20:45 +0200790 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200791
792 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200793 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200794 idx_next = 0;
795 }
796
Steven Rostedt93a588f2008-05-12 21:20:45 +0200797 if (data->trace_head == data->trace_tail &&
798 idx_next == data->trace_tail_idx) {
799 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200800 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200801 data->trace_tail_idx++;
802 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
803 data->trace_tail =
804 trace_next_page(data, data->trace_tail);
805 data->trace_tail_idx = 0;
806 }
807 }
808
809 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200810
811 return entry;
812}
813
Ingo Molnare309b412008-05-12 21:20:51 +0200814static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200815tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200816{
817 struct task_struct *tsk = current;
818 unsigned long pc;
819
820 pc = preempt_count();
821
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400822 entry->field.preempt_count = pc & 0xff;
823 entry->field.pid = (tsk) ? tsk->pid : 0;
824 entry->field.t = ftrace_now(raw_smp_processor_id());
825 entry->field.flags =
826 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200827 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
828 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
829 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
830}
831
Ingo Molnare309b412008-05-12 21:20:51 +0200832void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200833trace_function(struct trace_array *tr, struct trace_array_cpu *data,
834 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200835{
836 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200837 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200838
Steven Rostedt92205c22008-05-12 21:20:55 +0200839 raw_local_irq_save(irq_flags);
840 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400841 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200842 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400843 entry->type = TRACE_FN;
844 entry->field.fn.ip = ip;
845 entry->field.fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200846 __raw_spin_unlock(&data->lock);
847 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200848}
849
Ingo Molnare309b412008-05-12 21:20:51 +0200850void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200851ftrace(struct trace_array *tr, struct trace_array_cpu *data,
852 unsigned long ip, unsigned long parent_ip, unsigned long flags)
853{
854 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200855 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200856}
857
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200858#ifdef CONFIG_MMIOTRACE
859void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
860 struct mmiotrace_rw *rw)
861{
862 struct trace_entry *entry;
863 unsigned long irq_flags;
864
Ingo Molnar801a1752008-05-12 21:20:58 +0200865 raw_local_irq_save(irq_flags);
866 __raw_spin_lock(&data->lock);
867
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400868 entry = tracing_get_trace_entry(tr, data);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200869 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400870 entry->type = TRACE_MMIO_RW;
871 entry->field.mmiorw = *rw;
Ingo Molnar801a1752008-05-12 21:20:58 +0200872
873 __raw_spin_unlock(&data->lock);
874 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200875
876 trace_wake_up();
877}
878
879void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
880 struct mmiotrace_map *map)
881{
882 struct trace_entry *entry;
883 unsigned long irq_flags;
884
Ingo Molnar801a1752008-05-12 21:20:58 +0200885 raw_local_irq_save(irq_flags);
886 __raw_spin_lock(&data->lock);
887
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400888 entry = tracing_get_trace_entry(tr, data);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200889 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400890 entry->type = TRACE_MMIO_MAP;
891 entry->field.mmiomap = *map;
Ingo Molnar801a1752008-05-12 21:20:58 +0200892
893 __raw_spin_unlock(&data->lock);
894 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200895
896 trace_wake_up();
897}
898#endif
899
Ingo Molnar86387f72008-05-12 21:20:51 +0200900void __trace_stack(struct trace_array *tr,
901 struct trace_array_cpu *data,
902 unsigned long flags,
903 int skip)
904{
905 struct trace_entry *entry;
906 struct stack_trace trace;
907
908 if (!(trace_flags & TRACE_ITER_STACKTRACE))
909 return;
910
911 entry = tracing_get_trace_entry(tr, data);
912 tracing_generic_entry_update(entry, flags);
913 entry->type = TRACE_STACK;
914
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400915 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
Ingo Molnar86387f72008-05-12 21:20:51 +0200916
917 trace.nr_entries = 0;
918 trace.max_entries = FTRACE_STACK_ENTRIES;
919 trace.skip = skip;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400920 trace.entries = entry->field.stack.caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200921
922 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200923}
924
Ingo Molnare309b412008-05-12 21:20:51 +0200925void
Ingo Molnara4feb832008-05-12 21:21:02 +0200926__trace_special(void *__tr, void *__data,
927 unsigned long arg1, unsigned long arg2, unsigned long arg3)
928{
929 struct trace_array_cpu *data = __data;
930 struct trace_array *tr = __tr;
931 struct trace_entry *entry;
932 unsigned long irq_flags;
933
934 raw_local_irq_save(irq_flags);
935 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400936 entry = tracing_get_trace_entry(tr, data);
Ingo Molnara4feb832008-05-12 21:21:02 +0200937 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400938 entry->type = TRACE_SPECIAL;
939 entry->field.special.arg1 = arg1;
940 entry->field.special.arg2 = arg2;
941 entry->field.special.arg3 = arg3;
Ingo Molnara4feb832008-05-12 21:21:02 +0200942 __trace_stack(tr, data, irq_flags, 4);
943 __raw_spin_unlock(&data->lock);
944 raw_local_irq_restore(irq_flags);
945
946 trace_wake_up();
947}
948
949void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200950tracing_sched_switch_trace(struct trace_array *tr,
951 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200952 struct task_struct *prev,
953 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200954 unsigned long flags)
955{
956 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200957 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200958
Steven Rostedt92205c22008-05-12 21:20:55 +0200959 raw_local_irq_save(irq_flags);
960 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400961 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200962 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400963 entry->type = TRACE_CTX;
964 entry->field.ctx.prev_pid = prev->pid;
965 entry->field.ctx.prev_prio = prev->prio;
966 entry->field.ctx.prev_state = prev->state;
967 entry->field.ctx.next_pid = next->pid;
968 entry->field.ctx.next_prio = next->prio;
969 entry->field.ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200970 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200971 __raw_spin_unlock(&data->lock);
972 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200973}
974
Ingo Molnar57422792008-05-12 21:20:51 +0200975void
976tracing_sched_wakeup_trace(struct trace_array *tr,
977 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200978 struct task_struct *wakee,
979 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200980 unsigned long flags)
981{
982 struct trace_entry *entry;
983 unsigned long irq_flags;
984
Steven Rostedt92205c22008-05-12 21:20:55 +0200985 raw_local_irq_save(irq_flags);
986 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200987 entry = tracing_get_trace_entry(tr, data);
988 tracing_generic_entry_update(entry, flags);
989 entry->type = TRACE_WAKE;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400990 entry->field.ctx.prev_pid = curr->pid;
991 entry->field.ctx.prev_prio = curr->prio;
992 entry->field.ctx.prev_state = curr->state;
993 entry->field.ctx.next_pid = wakee->pid;
994 entry->field.ctx.next_prio = wakee->prio;
995 entry->field.ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200996 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200997 __raw_spin_unlock(&data->lock);
998 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200999
1000 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +02001001}
1002
Steven Rostedt4902f882008-05-22 00:22:18 -04001003void
1004ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1005{
1006 struct trace_array *tr = &global_trace;
1007 struct trace_array_cpu *data;
1008 unsigned long flags;
1009 long disabled;
1010 int cpu;
1011
1012 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1013 return;
1014
1015 local_irq_save(flags);
1016 cpu = raw_smp_processor_id();
1017 data = tr->data[cpu];
1018 disabled = atomic_inc_return(&data->disabled);
1019
1020 if (likely(disabled == 1))
1021 __trace_special(tr, data, arg1, arg2, arg3);
1022
1023 atomic_dec(&data->disabled);
1024 local_irq_restore(flags);
1025}
1026
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001027#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +02001028static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001029function_trace_call(unsigned long ip, unsigned long parent_ip)
1030{
1031 struct trace_array *tr = &global_trace;
1032 struct trace_array_cpu *data;
1033 unsigned long flags;
1034 long disabled;
1035 int cpu;
1036
Steven Rostedt60bc0802008-07-10 20:58:16 -04001037 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001038 return;
1039
Abhishek Sagarecea6562008-06-21 23:47:53 +05301040 if (skip_trace(ip))
1041 return;
1042
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001043 local_irq_save(flags);
1044 cpu = raw_smp_processor_id();
1045 data = tr->data[cpu];
1046 disabled = atomic_inc_return(&data->disabled);
1047
1048 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +02001049 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001050
1051 atomic_dec(&data->disabled);
1052 local_irq_restore(flags);
1053}
1054
1055static struct ftrace_ops trace_ops __read_mostly =
1056{
1057 .func = function_trace_call,
1058};
1059
Ingo Molnare309b412008-05-12 21:20:51 +02001060void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001061{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001062 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001063 register_ftrace_function(&trace_ops);
Steven Rostedt60bc0802008-07-10 20:58:16 -04001064 if (tracer_enabled)
1065 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001066}
1067
Ingo Molnare309b412008-05-12 21:20:51 +02001068void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001069{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001070 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001071 unregister_ftrace_function(&trace_ops);
1072}
1073#endif
1074
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001075enum trace_file_type {
1076 TRACE_FILE_LAT_FMT = 1,
1077};
1078
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001079/* Return the current entry. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001080static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001081trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1082 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001083{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001084 struct page *page;
1085 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001086
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001087 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001088 iter->next_idx[cpu] >= data->trace_idx ||
1089 (data->trace_head == data->trace_tail &&
1090 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001091 return NULL;
1092
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001093 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001094 /* Initialize the iterator for this cpu trace buffer */
1095 WARN_ON(!data->trace_tail);
1096 page = virt_to_page(data->trace_tail);
1097 iter->next_page[cpu] = &page->lru;
1098 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001099 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001100
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001101 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001102 BUG_ON(&data->trace_pages == &page->lru);
1103
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001104 array = page_address(page);
1105
Steven Rostedt93a588f2008-05-12 21:20:45 +02001106 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001107 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001108}
1109
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001110/* Increment the index counter of an iterator by one */
1111static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1112{
1113 iter->idx++;
1114 iter->next_idx[cpu]++;
1115 iter->next_page_idx[cpu]++;
1116
1117 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1118 struct trace_array_cpu *data = iter->tr->data[cpu];
1119
1120 iter->next_page_idx[cpu] = 0;
1121 iter->next_page[cpu] =
1122 trace_next_list(data, iter->next_page[cpu]);
1123 }
1124}
1125
Ingo Molnare309b412008-05-12 21:20:51 +02001126static struct trace_entry *
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001127trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1128 struct trace_iterator *iter, int cpu)
1129{
1130 struct list_head *next_page;
1131 struct trace_entry *ent;
1132 int idx, next_idx, next_page_idx;
1133
1134 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1135
1136 if (likely(!ent || ent->type != TRACE_CONT))
1137 return ent;
1138
1139 /* save the iterator details */
1140 idx = iter->idx;
1141 next_idx = iter->next_idx[cpu];
1142 next_page_idx = iter->next_page_idx[cpu];
1143 next_page = iter->next_page[cpu];
1144
1145 /* find a real entry */
1146 do {
1147 trace_iterator_increment(iter, cpu);
1148 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1149 } while (ent && ent->type != TRACE_CONT);
1150
1151 /* reset the iterator */
1152 iter->idx = idx;
1153 iter->next_idx[cpu] = next_idx;
1154 iter->next_page_idx[cpu] = next_page_idx;
1155 iter->next_page[cpu] = next_page;
1156
1157 return ent;
1158}
1159
1160static struct trace_entry *
1161__find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001162{
1163 struct trace_array *tr = iter->tr;
1164 struct trace_entry *ent, *next = NULL;
1165 int next_cpu = -1;
1166 int cpu;
1167
Steven Rostedtab464282008-05-12 21:21:00 +02001168 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001169 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001170 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001171
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001172 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001173
1174 if (ent && ent->type == TRACE_CONT) {
1175 struct trace_array_cpu *data = tr->data[cpu];
1176
1177 if (!inc)
1178 ent = trace_entry_next(tr, data, iter, cpu);
1179 else {
1180 while (ent && ent->type == TRACE_CONT) {
1181 trace_iterator_increment(iter, cpu);
1182 ent = trace_entry_idx(tr, tr->data[cpu],
1183 iter, cpu);
1184 }
1185 }
1186 }
1187
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001188 /*
1189 * Pick the entry with the smallest timestamp:
1190 */
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001191 if (ent && (!next || ent->field.t < next->field.t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001192 next = ent;
1193 next_cpu = cpu;
1194 }
1195 }
1196
1197 if (ent_cpu)
1198 *ent_cpu = next_cpu;
1199
1200 return next;
1201}
1202
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001203/* Find the next real entry, without updating the iterator itself */
1204static struct trace_entry *
1205find_next_entry(struct trace_iterator *iter, int *ent_cpu)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001206{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001207 return __find_next_entry(iter, ent_cpu, 0);
1208}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001209
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001210/* Find the next real entry, and increment the iterator to the next entry */
1211static void *find_next_entry_inc(struct trace_iterator *iter)
1212{
1213 struct trace_entry *next;
1214 int next_cpu = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001215
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001216 next = __find_next_entry(iter, &next_cpu, 1);
1217
1218 iter->prev_ent = iter->ent;
1219 iter->prev_cpu = iter->cpu;
1220
1221 iter->ent = next;
1222 iter->cpu = next_cpu;
1223
1224 if (next)
1225 trace_iterator_increment(iter, iter->cpu);
1226
1227 return next ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001228}
1229
Ingo Molnare309b412008-05-12 21:20:51 +02001230static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001231{
1232 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001233 struct trace_entry *ent;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001234
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001235 again:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001236 data->trace_tail_idx++;
1237 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1238 data->trace_tail = trace_next_page(data, data->trace_tail);
1239 data->trace_tail_idx = 0;
1240 }
1241
1242 /* Check if we empty it, then reset the index */
1243 if (data->trace_head == data->trace_tail &&
1244 data->trace_head_idx == data->trace_tail_idx)
1245 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001246
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001247 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1248 iter, iter->cpu);
1249 if (ent && ent->type == TRACE_CONT)
1250 goto again;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001251}
1252
Ingo Molnare309b412008-05-12 21:20:51 +02001253static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001254{
1255 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001256 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001257 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001258
1259 (*pos)++;
1260
1261 /* can't go backwards */
1262 if (iter->idx > i)
1263 return NULL;
1264
1265 if (iter->idx < 0)
1266 ent = find_next_entry_inc(iter);
1267 else
1268 ent = iter;
1269
1270 while (ent && iter->idx < i)
1271 ent = find_next_entry_inc(iter);
1272
1273 iter->pos = *pos;
1274
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001275 return ent;
1276}
1277
1278static void *s_start(struct seq_file *m, loff_t *pos)
1279{
1280 struct trace_iterator *iter = m->private;
1281 void *p = NULL;
1282 loff_t l = 0;
1283 int i;
1284
1285 mutex_lock(&trace_types_lock);
1286
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001287 if (!current_trace || current_trace != iter->trace) {
1288 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001289 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001290 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001291
1292 atomic_inc(&trace_record_cmdline_disabled);
1293
1294 /* let the tracer grab locks here if needed */
1295 if (current_trace->start)
1296 current_trace->start(iter);
1297
1298 if (*pos != iter->pos) {
1299 iter->ent = NULL;
1300 iter->cpu = 0;
1301 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001302 iter->prev_ent = NULL;
1303 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001304
Steven Rostedtab464282008-05-12 21:21:00 +02001305 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001306 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001307 iter->next_page[i] = NULL;
1308 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001309
1310 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1311 ;
1312
1313 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001314 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001315 p = s_next(m, p, &l);
1316 }
1317
1318 return p;
1319}
1320
1321static void s_stop(struct seq_file *m, void *p)
1322{
1323 struct trace_iterator *iter = m->private;
1324
1325 atomic_dec(&trace_record_cmdline_disabled);
1326
1327 /* let the tracer release locks here if needed */
1328 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1329 iter->trace->stop(iter);
1330
1331 mutex_unlock(&trace_types_lock);
1332}
1333
Abhishek Sagar76094a22008-05-28 00:03:18 +05301334#define KRETPROBE_MSG "[unknown/kretprobe'd]"
1335
1336#ifdef CONFIG_KRETPROBES
1337static inline int kretprobed(unsigned long addr)
1338{
1339 return addr == (unsigned long)kretprobe_trampoline;
1340}
1341#else
1342static inline int kretprobed(unsigned long addr)
1343{
1344 return 0;
1345}
1346#endif /* CONFIG_KRETPROBES */
1347
Steven Rostedtb3806b42008-05-12 21:20:46 +02001348static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001349seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001350{
1351#ifdef CONFIG_KALLSYMS
1352 char str[KSYM_SYMBOL_LEN];
1353
1354 kallsyms_lookup(address, NULL, NULL, NULL, str);
1355
Steven Rostedtb3806b42008-05-12 21:20:46 +02001356 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001357#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001358 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001359}
1360
Steven Rostedtb3806b42008-05-12 21:20:46 +02001361static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001362seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1363 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001364{
1365#ifdef CONFIG_KALLSYMS
1366 char str[KSYM_SYMBOL_LEN];
1367
1368 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001369 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001371 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001372}
1373
1374#ifndef CONFIG_64BIT
1375# define IP_FMT "%08lx"
1376#else
1377# define IP_FMT "%016lx"
1378#endif
1379
Ingo Molnare309b412008-05-12 21:20:51 +02001380static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001381seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001382{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001383 int ret;
1384
1385 if (!ip)
1386 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387
1388 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001389 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001390 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001391 ret = seq_print_sym_short(s, "%s", ip);
1392
1393 if (!ret)
1394 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001395
1396 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001397 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1398 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001399}
1400
Ingo Molnare309b412008-05-12 21:20:51 +02001401static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001402{
1403 seq_puts(m, "# _------=> CPU# \n");
1404 seq_puts(m, "# / _-----=> irqs-off \n");
1405 seq_puts(m, "# | / _----=> need-resched \n");
1406 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1407 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1408 seq_puts(m, "# |||| / \n");
1409 seq_puts(m, "# ||||| delay \n");
1410 seq_puts(m, "# cmd pid ||||| time | caller \n");
1411 seq_puts(m, "# \\ / ||||| \\ | / \n");
1412}
1413
Ingo Molnare309b412008-05-12 21:20:51 +02001414static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001415{
1416 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1417 seq_puts(m, "# | | | | |\n");
1418}
1419
1420
Ingo Molnare309b412008-05-12 21:20:51 +02001421static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001422print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1423{
1424 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1425 struct trace_array *tr = iter->tr;
1426 struct trace_array_cpu *data = tr->data[tr->cpu];
1427 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001428 unsigned long total = 0;
1429 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001430 int cpu;
1431 const char *name = "preemption";
1432
1433 if (type)
1434 name = type->name;
1435
Steven Rostedtab464282008-05-12 21:21:00 +02001436 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001437 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001438 total += tr->data[cpu]->trace_idx;
1439 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001440 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001441 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001442 entries += tr->data[cpu]->trace_idx;
1443 }
1444 }
1445
1446 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1447 name, UTS_RELEASE);
1448 seq_puts(m, "-----------------------------------"
1449 "---------------------------------\n");
1450 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1451 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001452 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001453 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001454 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 tr->cpu,
1456#if defined(CONFIG_PREEMPT_NONE)
1457 "server",
1458#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1459 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001460#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001461 "preempt",
1462#else
1463 "unknown",
1464#endif
1465 /* These are reserved for later use */
1466 0, 0, 0, 0);
1467#ifdef CONFIG_SMP
1468 seq_printf(m, " #P:%d)\n", num_online_cpus());
1469#else
1470 seq_puts(m, ")\n");
1471#endif
1472 seq_puts(m, " -----------------\n");
1473 seq_printf(m, " | task: %.16s-%d "
1474 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1475 data->comm, data->pid, data->uid, data->nice,
1476 data->policy, data->rt_priority);
1477 seq_puts(m, " -----------------\n");
1478
1479 if (data->critical_start) {
1480 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001481 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1482 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001483 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001484 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1485 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001486 seq_puts(m, "\n");
1487 }
1488
1489 seq_puts(m, "\n");
1490}
1491
Ingo Molnare309b412008-05-12 21:20:51 +02001492static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001493lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001494{
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001495 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001496 int hardirq, softirq;
1497 char *comm;
1498
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001499 comm = trace_find_cmdline(field->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001500
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001501 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001502 trace_seq_printf(s, "%d", cpu);
1503 trace_seq_printf(s, "%c%c",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001504 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1505 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001506
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001507 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1508 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001509 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001510 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001511 } else {
1512 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001513 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001514 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001515 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001516 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001517 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001518 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001519 }
1520 }
1521
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001522 if (field->preempt_count)
1523 trace_seq_printf(s, "%x", field->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001524 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001525 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001526}
1527
1528unsigned long preempt_mark_thresh = 100;
1529
Ingo Molnare309b412008-05-12 21:20:51 +02001530static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001531lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001532 unsigned long rel_usecs)
1533{
Steven Rostedt214023c2008-05-12 21:20:46 +02001534 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001536 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001537 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001538 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001539 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001540 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001541}
1542
1543static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1544
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001545static void
1546trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1547{
1548 struct trace_array *tr = iter->tr;
1549 struct trace_array_cpu *data = tr->data[iter->cpu];
1550 struct trace_entry *ent;
1551
1552 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1553 if (!ent || ent->type != TRACE_CONT) {
1554 trace_seq_putc(s, '\n');
1555 return;
1556 }
1557
1558 do {
1559 trace_seq_printf(s, "%s", ent->cont.buf);
1560 trace_iterator_increment(iter, iter->cpu);
1561 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1562 } while (ent && ent->type == TRACE_CONT);
1563}
1564
Ingo Molnare309b412008-05-12 21:20:51 +02001565static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001566print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001567{
Steven Rostedt214023c2008-05-12 21:20:46 +02001568 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001569 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1570 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1571 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1572 struct trace_entry *entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001573 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001574 unsigned long abs_usecs;
1575 unsigned long rel_usecs;
1576 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001577 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001578 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001579 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580
1581 if (!next_entry)
1582 next_entry = entry;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001583
1584 if (entry->type == TRACE_CONT)
1585 return 1;
1586
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001587 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1588 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001589
1590 if (verbose) {
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001591 comm = trace_find_cmdline(field->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001592 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1593 " %ld.%03ldms (+%ld.%03ldms): ",
1594 comm,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001595 field->pid, cpu, field->flags,
1596 field->preempt_count, trace_idx,
1597 ns2usecs(field->t),
Steven Rostedt214023c2008-05-12 21:20:46 +02001598 abs_usecs/1000,
1599 abs_usecs % 1000, rel_usecs/1000,
1600 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001601 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001602 lat_print_generic(s, entry, cpu);
1603 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001604 }
1605 switch (entry->type) {
1606 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001607 seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001608 trace_seq_puts(s, " (");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001609 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301610 trace_seq_puts(s, KRETPROBE_MSG);
1611 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001612 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001613 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001614 break;
1615 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001616 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001617 T = field->ctx.next_state < sizeof(state_to_char) ?
1618 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001619
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001620 state = field->ctx.prev_state ?
1621 __ffs(field->ctx.prev_state) + 1 : 0;
Ankita Gargd17d9692008-05-12 21:20:58 +02001622 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001623 comm = trace_find_cmdline(field->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001624 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001625 field->ctx.prev_pid,
1626 field->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001627 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001628 field->ctx.next_pid,
1629 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001630 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001631 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001632 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001633 trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001634 field->special.arg1,
1635 field->special.arg2,
1636 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001637 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001638 case TRACE_STACK:
1639 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1640 if (i)
1641 trace_seq_puts(s, " <= ");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001642 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +02001643 }
1644 trace_seq_puts(s, "\n");
1645 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001646 case TRACE_PRINT:
1647 seq_print_ip_sym(s, field->print.ip, sym_flags);
1648 trace_seq_printf(s, ": %s", field->print.buf);
1649 if (field->flags && TRACE_FLAG_CONT)
1650 trace_seq_print_cont(s, iter);
1651 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001652 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001653 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001654 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001655 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001656}
1657
Ingo Molnare309b412008-05-12 21:20:51 +02001658static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001659{
Steven Rostedt214023c2008-05-12 21:20:46 +02001660 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001661 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001662 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001663 struct trace_field *field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001664 unsigned long usec_rem;
1665 unsigned long long t;
1666 unsigned long secs;
1667 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001668 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001669 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001670 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001671
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001672 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001673
1674 if (entry->type == TRACE_CONT)
1675 return 1;
1676
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001677 field = &entry->field;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001678
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001679 comm = trace_find_cmdline(iter->ent->field.pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001680
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001681 t = ns2usecs(field->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001682 usec_rem = do_div(t, 1000000ULL);
1683 secs = (unsigned long)t;
1684
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001685 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001686 if (!ret)
1687 return 0;
1688 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1689 if (!ret)
1690 return 0;
1691 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1692 if (!ret)
1693 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001694
1695 switch (entry->type) {
1696 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001697 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001698 if (!ret)
1699 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001700 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001701 field->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001702 ret = trace_seq_printf(s, " <-");
1703 if (!ret)
1704 return 0;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001705 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301706 ret = trace_seq_puts(s, KRETPROBE_MSG);
1707 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001708 ret = seq_print_ip_sym(s,
1709 field->fn.parent_ip,
Abhishek Sagar76094a22008-05-28 00:03:18 +05301710 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001711 if (!ret)
1712 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001713 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001714 ret = trace_seq_printf(s, "\n");
1715 if (!ret)
1716 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001717 break;
1718 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001719 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001720 S = field->ctx.prev_state < sizeof(state_to_char) ?
1721 state_to_char[field->ctx.prev_state] : 'X';
1722 T = field->ctx.next_state < sizeof(state_to_char) ?
1723 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001724 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001725 field->ctx.prev_pid,
1726 field->ctx.prev_prio,
Steven Rostedtb3806b42008-05-12 21:20:46 +02001727 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001728 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001729 field->ctx.next_pid,
1730 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001731 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001732 if (!ret)
1733 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001734 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001735 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001736 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001737 field->special.arg1,
1738 field->special.arg2,
1739 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001740 if (!ret)
1741 return 0;
1742 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001743 case TRACE_STACK:
1744 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1745 if (i) {
1746 ret = trace_seq_puts(s, " <= ");
1747 if (!ret)
1748 return 0;
1749 }
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001750 ret = seq_print_ip_sym(s, field->stack.caller[i],
Ingo Molnar86387f72008-05-12 21:20:51 +02001751 sym_flags);
1752 if (!ret)
1753 return 0;
1754 }
1755 ret = trace_seq_puts(s, "\n");
1756 if (!ret)
1757 return 0;
1758 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001759 case TRACE_PRINT:
1760 seq_print_ip_sym(s, field->print.ip, sym_flags);
1761 trace_seq_printf(s, ": %s", field->print.buf);
1762 if (field->flags && TRACE_FLAG_CONT)
1763 trace_seq_print_cont(s, iter);
1764 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001766 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001767}
1768
Ingo Molnare309b412008-05-12 21:20:51 +02001769static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001770{
1771 struct trace_seq *s = &iter->seq;
1772 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001773 struct trace_field *field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001774 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001775 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001776
1777 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001778
1779 if (entry->type == TRACE_CONT)
1780 return 1;
1781
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001782 field = &entry->field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001783
1784 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001785 field->pid, iter->cpu, field->t);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001786 if (!ret)
1787 return 0;
1788
1789 switch (entry->type) {
1790 case TRACE_FN:
1791 ret = trace_seq_printf(s, "%x %x\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001792 field->fn.ip,
1793 field->fn.parent_ip);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001794 if (!ret)
1795 return 0;
1796 break;
1797 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001798 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001799 S = field->ctx.prev_state < sizeof(state_to_char) ?
1800 state_to_char[field->ctx.prev_state] : 'X';
1801 T = field->ctx.next_state < sizeof(state_to_char) ?
1802 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001803 if (entry->type == TRACE_WAKE)
1804 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001805 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001806 field->ctx.prev_pid,
1807 field->ctx.prev_prio,
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001808 S,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001809 field->ctx.next_pid,
1810 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001811 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001812 if (!ret)
1813 return 0;
1814 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001815 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001816 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001817 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001818 field->special.arg1,
1819 field->special.arg2,
1820 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001821 if (!ret)
1822 return 0;
1823 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001824 case TRACE_PRINT:
1825 trace_seq_printf(s, "# %lx %s",
1826 field->print.ip, field->print.buf);
1827 if (field->flags && TRACE_FLAG_CONT)
1828 trace_seq_print_cont(s, iter);
1829 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001830 }
1831 return 1;
1832}
1833
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001834#define SEQ_PUT_FIELD_RET(s, x) \
1835do { \
1836 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1837 return 0; \
1838} while (0)
1839
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001840#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1841do { \
1842 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1843 return 0; \
1844} while (0)
1845
Ingo Molnare309b412008-05-12 21:20:51 +02001846static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001847{
1848 struct trace_seq *s = &iter->seq;
1849 unsigned char newline = '\n';
1850 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001851 struct trace_field *field;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001852 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001853
1854 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001855
1856 if (entry->type == TRACE_CONT)
1857 return 1;
1858
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001859 field = &entry->field;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001860
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001861 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001862 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001863 SEQ_PUT_HEX_FIELD_RET(s, field->t);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001864
1865 switch (entry->type) {
1866 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001867 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1868 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001869 break;
1870 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001871 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001872 S = field->ctx.prev_state < sizeof(state_to_char) ?
1873 state_to_char[field->ctx.prev_state] : 'X';
1874 T = field->ctx.next_state < sizeof(state_to_char) ?
1875 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001876 if (entry->type == TRACE_WAKE)
1877 S = '+';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001878 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1879 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001880 SEQ_PUT_HEX_FIELD_RET(s, S);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001881 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1882 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001883 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001884 break;
1885 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001886 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001887 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1888 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1889 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001890 break;
1891 }
1892 SEQ_PUT_FIELD_RET(s, newline);
1893
1894 return 1;
1895}
1896
Ingo Molnare309b412008-05-12 21:20:51 +02001897static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001898{
1899 struct trace_seq *s = &iter->seq;
1900 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001901 struct trace_field *field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001902
1903 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001904
1905 if (entry->type == TRACE_CONT)
1906 return 1;
1907
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001908 field = &entry->field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001909
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001910 SEQ_PUT_FIELD_RET(s, field->pid);
1911 SEQ_PUT_FIELD_RET(s, field->cpu);
1912 SEQ_PUT_FIELD_RET(s, field->t);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001913
1914 switch (entry->type) {
1915 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001916 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1917 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001918 break;
1919 case TRACE_CTX:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001920 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1921 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1922 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1923 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1924 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1925 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001926 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001927 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001928 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001929 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1930 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1931 SEQ_PUT_FIELD_RET(s, field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001932 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001933 }
1934 return 1;
1935}
1936
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001937static int trace_empty(struct trace_iterator *iter)
1938{
1939 struct trace_array_cpu *data;
1940 int cpu;
1941
Steven Rostedtab464282008-05-12 21:21:00 +02001942 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001943 data = iter->tr->data[cpu];
1944
Steven Rostedtb3806b42008-05-12 21:20:46 +02001945 if (head_page(data) && data->trace_idx &&
1946 (data->trace_tail != data->trace_head ||
1947 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001948 return 0;
1949 }
1950 return 1;
1951}
1952
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001953static int print_trace_line(struct trace_iterator *iter)
1954{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001955 if (iter->trace && iter->trace->print_line)
1956 return iter->trace->print_line(iter);
1957
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001958 if (trace_flags & TRACE_ITER_BIN)
1959 return print_bin_fmt(iter);
1960
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001961 if (trace_flags & TRACE_ITER_HEX)
1962 return print_hex_fmt(iter);
1963
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001964 if (trace_flags & TRACE_ITER_RAW)
1965 return print_raw_fmt(iter);
1966
1967 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1968 return print_lat_fmt(iter, iter->idx, iter->cpu);
1969
1970 return print_trace_fmt(iter);
1971}
1972
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001973static int s_show(struct seq_file *m, void *v)
1974{
1975 struct trace_iterator *iter = v;
1976
1977 if (iter->ent == NULL) {
1978 if (iter->tr) {
1979 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1980 seq_puts(m, "#\n");
1981 }
1982 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1983 /* print nothing if the buffers are empty */
1984 if (trace_empty(iter))
1985 return 0;
1986 print_trace_header(m, iter);
1987 if (!(trace_flags & TRACE_ITER_VERBOSE))
1988 print_lat_help_header(m);
1989 } else {
1990 if (!(trace_flags & TRACE_ITER_VERBOSE))
1991 print_func_help_header(m);
1992 }
1993 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001994 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001995 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001996 }
1997
1998 return 0;
1999}
2000
2001static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002002 .start = s_start,
2003 .next = s_next,
2004 .stop = s_stop,
2005 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002006};
2007
Ingo Molnare309b412008-05-12 21:20:51 +02002008static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002009__tracing_open(struct inode *inode, struct file *file, int *ret)
2010{
2011 struct trace_iterator *iter;
2012
Steven Rostedt60a11772008-05-12 21:20:44 +02002013 if (tracing_disabled) {
2014 *ret = -ENODEV;
2015 return NULL;
2016 }
2017
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002018 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2019 if (!iter) {
2020 *ret = -ENOMEM;
2021 goto out;
2022 }
2023
2024 mutex_lock(&trace_types_lock);
2025 if (current_trace && current_trace->print_max)
2026 iter->tr = &max_tr;
2027 else
2028 iter->tr = inode->i_private;
2029 iter->trace = current_trace;
2030 iter->pos = -1;
2031
2032 /* TODO stop tracer */
2033 *ret = seq_open(file, &tracer_seq_ops);
2034 if (!*ret) {
2035 struct seq_file *m = file->private_data;
2036 m->private = iter;
2037
2038 /* stop the trace while dumping */
Steven Rostedt60bc0802008-07-10 20:58:16 -04002039 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002040 tracer_enabled = 0;
Steven Rostedt60bc0802008-07-10 20:58:16 -04002041 ftrace_function_enabled = 0;
2042 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002043
2044 if (iter->trace && iter->trace->open)
2045 iter->trace->open(iter);
2046 } else {
2047 kfree(iter);
2048 iter = NULL;
2049 }
2050 mutex_unlock(&trace_types_lock);
2051
2052 out:
2053 return iter;
2054}
2055
2056int tracing_open_generic(struct inode *inode, struct file *filp)
2057{
Steven Rostedt60a11772008-05-12 21:20:44 +02002058 if (tracing_disabled)
2059 return -ENODEV;
2060
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002061 filp->private_data = inode->i_private;
2062 return 0;
2063}
2064
2065int tracing_release(struct inode *inode, struct file *file)
2066{
2067 struct seq_file *m = (struct seq_file *)file->private_data;
2068 struct trace_iterator *iter = m->private;
2069
2070 mutex_lock(&trace_types_lock);
2071 if (iter->trace && iter->trace->close)
2072 iter->trace->close(iter);
2073
2074 /* reenable tracing if it was previously enabled */
Steven Rostedt60bc0802008-07-10 20:58:16 -04002075 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002076 tracer_enabled = 1;
Steven Rostedt60bc0802008-07-10 20:58:16 -04002077 /*
2078 * It is safe to enable function tracing even if it
2079 * isn't used
2080 */
2081 ftrace_function_enabled = 1;
2082 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002083 mutex_unlock(&trace_types_lock);
2084
2085 seq_release(inode, file);
2086 kfree(iter);
2087 return 0;
2088}
2089
2090static int tracing_open(struct inode *inode, struct file *file)
2091{
2092 int ret;
2093
2094 __tracing_open(inode, file, &ret);
2095
2096 return ret;
2097}
2098
2099static int tracing_lt_open(struct inode *inode, struct file *file)
2100{
2101 struct trace_iterator *iter;
2102 int ret;
2103
2104 iter = __tracing_open(inode, file, &ret);
2105
2106 if (!ret)
2107 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2108
2109 return ret;
2110}
2111
2112
Ingo Molnare309b412008-05-12 21:20:51 +02002113static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002114t_next(struct seq_file *m, void *v, loff_t *pos)
2115{
2116 struct tracer *t = m->private;
2117
2118 (*pos)++;
2119
2120 if (t)
2121 t = t->next;
2122
2123 m->private = t;
2124
2125 return t;
2126}
2127
2128static void *t_start(struct seq_file *m, loff_t *pos)
2129{
2130 struct tracer *t = m->private;
2131 loff_t l = 0;
2132
2133 mutex_lock(&trace_types_lock);
2134 for (; t && l < *pos; t = t_next(m, t, &l))
2135 ;
2136
2137 return t;
2138}
2139
2140static void t_stop(struct seq_file *m, void *p)
2141{
2142 mutex_unlock(&trace_types_lock);
2143}
2144
2145static int t_show(struct seq_file *m, void *v)
2146{
2147 struct tracer *t = v;
2148
2149 if (!t)
2150 return 0;
2151
2152 seq_printf(m, "%s", t->name);
2153 if (t->next)
2154 seq_putc(m, ' ');
2155 else
2156 seq_putc(m, '\n');
2157
2158 return 0;
2159}
2160
2161static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002162 .start = t_start,
2163 .next = t_next,
2164 .stop = t_stop,
2165 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002166};
2167
2168static int show_traces_open(struct inode *inode, struct file *file)
2169{
2170 int ret;
2171
Steven Rostedt60a11772008-05-12 21:20:44 +02002172 if (tracing_disabled)
2173 return -ENODEV;
2174
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002175 ret = seq_open(file, &show_traces_seq_ops);
2176 if (!ret) {
2177 struct seq_file *m = file->private_data;
2178 m->private = trace_types;
2179 }
2180
2181 return ret;
2182}
2183
2184static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002185 .open = tracing_open,
2186 .read = seq_read,
2187 .llseek = seq_lseek,
2188 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002189};
2190
2191static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002192 .open = tracing_lt_open,
2193 .read = seq_read,
2194 .llseek = seq_lseek,
2195 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002196};
2197
2198static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002199 .open = show_traces_open,
2200 .read = seq_read,
2201 .release = seq_release,
2202};
2203
Ingo Molnar36dfe922008-05-12 21:20:52 +02002204/*
2205 * Only trace on a CPU if the bitmask is set:
2206 */
2207static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2208
2209/*
2210 * When tracing/tracing_cpu_mask is modified then this holds
2211 * the new bitmask we are about to install:
2212 */
2213static cpumask_t tracing_cpumask_new;
2214
2215/*
2216 * The tracer itself will not take this lock, but still we want
2217 * to provide a consistent cpumask to user-space:
2218 */
2219static DEFINE_MUTEX(tracing_cpumask_update_lock);
2220
2221/*
2222 * Temporary storage for the character representation of the
2223 * CPU bitmask (and one more byte for the newline):
2224 */
2225static char mask_str[NR_CPUS + 1];
2226
Ingo Molnarc7078de2008-05-12 21:20:52 +02002227static ssize_t
2228tracing_cpumask_read(struct file *filp, char __user *ubuf,
2229 size_t count, loff_t *ppos)
2230{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002231 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002232
2233 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002234
2235 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2236 if (count - len < 2) {
2237 count = -EINVAL;
2238 goto out_err;
2239 }
2240 len += sprintf(mask_str + len, "\n");
2241 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2242
2243out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002244 mutex_unlock(&tracing_cpumask_update_lock);
2245
2246 return count;
2247}
2248
2249static ssize_t
2250tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2251 size_t count, loff_t *ppos)
2252{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002253 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002254
2255 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002256 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2257 if (err)
2258 goto err_unlock;
2259
Steven Rostedt92205c22008-05-12 21:20:55 +02002260 raw_local_irq_disable();
2261 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002262 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002263 /*
2264 * Increase/decrease the disabled counter if we are
2265 * about to flip a bit in the cpumask:
2266 */
2267 if (cpu_isset(cpu, tracing_cpumask) &&
2268 !cpu_isset(cpu, tracing_cpumask_new)) {
2269 atomic_inc(&global_trace.data[cpu]->disabled);
2270 }
2271 if (!cpu_isset(cpu, tracing_cpumask) &&
2272 cpu_isset(cpu, tracing_cpumask_new)) {
2273 atomic_dec(&global_trace.data[cpu]->disabled);
2274 }
2275 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002276 __raw_spin_unlock(&ftrace_max_lock);
2277 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002278
2279 tracing_cpumask = tracing_cpumask_new;
2280
Ingo Molnarc7078de2008-05-12 21:20:52 +02002281 mutex_unlock(&tracing_cpumask_update_lock);
2282
Ingo Molnarc7078de2008-05-12 21:20:52 +02002283 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002284
2285err_unlock:
2286 mutex_unlock(&tracing_cpumask_update_lock);
2287
2288 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002289}
2290
2291static struct file_operations tracing_cpumask_fops = {
2292 .open = tracing_open_generic,
2293 .read = tracing_cpumask_read,
2294 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002295};
2296
2297static ssize_t
2298tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2299 size_t cnt, loff_t *ppos)
2300{
2301 char *buf;
2302 int r = 0;
2303 int len = 0;
2304 int i;
2305
2306 /* calulate max size */
2307 for (i = 0; trace_options[i]; i++) {
2308 len += strlen(trace_options[i]);
2309 len += 3; /* "no" and space */
2310 }
2311
2312 /* +2 for \n and \0 */
2313 buf = kmalloc(len + 2, GFP_KERNEL);
2314 if (!buf)
2315 return -ENOMEM;
2316
2317 for (i = 0; trace_options[i]; i++) {
2318 if (trace_flags & (1 << i))
2319 r += sprintf(buf + r, "%s ", trace_options[i]);
2320 else
2321 r += sprintf(buf + r, "no%s ", trace_options[i]);
2322 }
2323
2324 r += sprintf(buf + r, "\n");
2325 WARN_ON(r >= len + 2);
2326
Ingo Molnar36dfe922008-05-12 21:20:52 +02002327 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002328
2329 kfree(buf);
2330
2331 return r;
2332}
2333
2334static ssize_t
2335tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2337{
2338 char buf[64];
2339 char *cmp = buf;
2340 int neg = 0;
2341 int i;
2342
Steven Rostedtcffae432008-05-12 21:21:00 +02002343 if (cnt >= sizeof(buf))
2344 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002345
2346 if (copy_from_user(&buf, ubuf, cnt))
2347 return -EFAULT;
2348
2349 buf[cnt] = 0;
2350
2351 if (strncmp(buf, "no", 2) == 0) {
2352 neg = 1;
2353 cmp += 2;
2354 }
2355
2356 for (i = 0; trace_options[i]; i++) {
2357 int len = strlen(trace_options[i]);
2358
2359 if (strncmp(cmp, trace_options[i], len) == 0) {
2360 if (neg)
2361 trace_flags &= ~(1 << i);
2362 else
2363 trace_flags |= (1 << i);
2364 break;
2365 }
2366 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002367 /*
2368 * If no option could be set, return an error:
2369 */
2370 if (!trace_options[i])
2371 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002372
2373 filp->f_pos += cnt;
2374
2375 return cnt;
2376}
2377
2378static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002379 .open = tracing_open_generic,
2380 .read = tracing_iter_ctrl_read,
2381 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002382};
2383
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002384static const char readme_msg[] =
2385 "tracing mini-HOWTO:\n\n"
2386 "# mkdir /debug\n"
2387 "# mount -t debugfs nodev /debug\n\n"
2388 "# cat /debug/tracing/available_tracers\n"
2389 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2390 "# cat /debug/tracing/current_tracer\n"
2391 "none\n"
2392 "# echo sched_switch > /debug/tracing/current_tracer\n"
2393 "# cat /debug/tracing/current_tracer\n"
2394 "sched_switch\n"
2395 "# cat /debug/tracing/iter_ctrl\n"
2396 "noprint-parent nosym-offset nosym-addr noverbose\n"
2397 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2398 "# echo 1 > /debug/tracing/tracing_enabled\n"
2399 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2400 "echo 0 > /debug/tracing/tracing_enabled\n"
2401;
2402
2403static ssize_t
2404tracing_readme_read(struct file *filp, char __user *ubuf,
2405 size_t cnt, loff_t *ppos)
2406{
2407 return simple_read_from_buffer(ubuf, cnt, ppos,
2408 readme_msg, strlen(readme_msg));
2409}
2410
2411static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002412 .open = tracing_open_generic,
2413 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002414};
2415
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002416static ssize_t
2417tracing_ctrl_read(struct file *filp, char __user *ubuf,
2418 size_t cnt, loff_t *ppos)
2419{
2420 struct trace_array *tr = filp->private_data;
2421 char buf[64];
2422 int r;
2423
2424 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002425 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002426}
2427
2428static ssize_t
2429tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2430 size_t cnt, loff_t *ppos)
2431{
2432 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002433 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002434 long val;
2435 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002436
Steven Rostedtcffae432008-05-12 21:21:00 +02002437 if (cnt >= sizeof(buf))
2438 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002439
2440 if (copy_from_user(&buf, ubuf, cnt))
2441 return -EFAULT;
2442
2443 buf[cnt] = 0;
2444
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002445 ret = strict_strtoul(buf, 10, &val);
2446 if (ret < 0)
2447 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002448
2449 val = !!val;
2450
2451 mutex_lock(&trace_types_lock);
2452 if (tr->ctrl ^ val) {
2453 if (val)
2454 tracer_enabled = 1;
2455 else
2456 tracer_enabled = 0;
2457
2458 tr->ctrl = val;
2459
2460 if (current_trace && current_trace->ctrl_update)
2461 current_trace->ctrl_update(tr);
2462 }
2463 mutex_unlock(&trace_types_lock);
2464
2465 filp->f_pos += cnt;
2466
2467 return cnt;
2468}
2469
2470static ssize_t
2471tracing_set_trace_read(struct file *filp, char __user *ubuf,
2472 size_t cnt, loff_t *ppos)
2473{
2474 char buf[max_tracer_type_len+2];
2475 int r;
2476
2477 mutex_lock(&trace_types_lock);
2478 if (current_trace)
2479 r = sprintf(buf, "%s\n", current_trace->name);
2480 else
2481 r = sprintf(buf, "\n");
2482 mutex_unlock(&trace_types_lock);
2483
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002484 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002485}
2486
2487static ssize_t
2488tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2489 size_t cnt, loff_t *ppos)
2490{
2491 struct trace_array *tr = &global_trace;
2492 struct tracer *t;
2493 char buf[max_tracer_type_len+1];
2494 int i;
2495
2496 if (cnt > max_tracer_type_len)
2497 cnt = max_tracer_type_len;
2498
2499 if (copy_from_user(&buf, ubuf, cnt))
2500 return -EFAULT;
2501
2502 buf[cnt] = 0;
2503
2504 /* strip ending whitespace. */
2505 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2506 buf[i] = 0;
2507
2508 mutex_lock(&trace_types_lock);
2509 for (t = trace_types; t; t = t->next) {
2510 if (strcmp(t->name, buf) == 0)
2511 break;
2512 }
2513 if (!t || t == current_trace)
2514 goto out;
2515
2516 if (current_trace && current_trace->reset)
2517 current_trace->reset(tr);
2518
2519 current_trace = t;
2520 if (t->init)
2521 t->init(tr);
2522
2523 out:
2524 mutex_unlock(&trace_types_lock);
2525
2526 filp->f_pos += cnt;
2527
2528 return cnt;
2529}
2530
2531static ssize_t
2532tracing_max_lat_read(struct file *filp, char __user *ubuf,
2533 size_t cnt, loff_t *ppos)
2534{
2535 unsigned long *ptr = filp->private_data;
2536 char buf[64];
2537 int r;
2538
Steven Rostedtcffae432008-05-12 21:21:00 +02002539 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002540 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002541 if (r > sizeof(buf))
2542 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002543 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002544}
2545
2546static ssize_t
2547tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2548 size_t cnt, loff_t *ppos)
2549{
2550 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002551 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002552 long val;
2553 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002554
Steven Rostedtcffae432008-05-12 21:21:00 +02002555 if (cnt >= sizeof(buf))
2556 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002557
2558 if (copy_from_user(&buf, ubuf, cnt))
2559 return -EFAULT;
2560
2561 buf[cnt] = 0;
2562
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002563 ret = strict_strtoul(buf, 10, &val);
2564 if (ret < 0)
2565 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002566
2567 *ptr = val * 1000;
2568
2569 return cnt;
2570}
2571
Steven Rostedtb3806b42008-05-12 21:20:46 +02002572static atomic_t tracing_reader;
2573
2574static int tracing_open_pipe(struct inode *inode, struct file *filp)
2575{
2576 struct trace_iterator *iter;
2577
2578 if (tracing_disabled)
2579 return -ENODEV;
2580
2581 /* We only allow for reader of the pipe */
2582 if (atomic_inc_return(&tracing_reader) != 1) {
2583 atomic_dec(&tracing_reader);
2584 return -EBUSY;
2585 }
2586
2587 /* create a buffer to store the information to pass to userspace */
2588 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2589 if (!iter)
2590 return -ENOMEM;
2591
Steven Rostedt107bad82008-05-12 21:21:01 +02002592 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002593 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002594 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002595 filp->private_data = iter;
2596
Steven Rostedt107bad82008-05-12 21:21:01 +02002597 if (iter->trace->pipe_open)
2598 iter->trace->pipe_open(iter);
2599 mutex_unlock(&trace_types_lock);
2600
Steven Rostedtb3806b42008-05-12 21:20:46 +02002601 return 0;
2602}
2603
2604static int tracing_release_pipe(struct inode *inode, struct file *file)
2605{
2606 struct trace_iterator *iter = file->private_data;
2607
2608 kfree(iter);
2609 atomic_dec(&tracing_reader);
2610
2611 return 0;
2612}
2613
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002614static unsigned int
2615tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2616{
2617 struct trace_iterator *iter = filp->private_data;
2618
2619 if (trace_flags & TRACE_ITER_BLOCK) {
2620 /*
2621 * Always select as readable when in blocking mode
2622 */
2623 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002624 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002625 if (!trace_empty(iter))
2626 return POLLIN | POLLRDNORM;
2627 poll_wait(filp, &trace_wait, poll_table);
2628 if (!trace_empty(iter))
2629 return POLLIN | POLLRDNORM;
2630
2631 return 0;
2632 }
2633}
2634
Steven Rostedtb3806b42008-05-12 21:20:46 +02002635/*
2636 * Consumer reader.
2637 */
2638static ssize_t
2639tracing_read_pipe(struct file *filp, char __user *ubuf,
2640 size_t cnt, loff_t *ppos)
2641{
2642 struct trace_iterator *iter = filp->private_data;
2643 struct trace_array_cpu *data;
2644 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002645 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002646#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002647 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002648#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002649 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002650 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002651
2652 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002653 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2654 if (sret != -EBUSY)
2655 return sret;
2656 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002657
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002658 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002659
Steven Rostedt107bad82008-05-12 21:21:01 +02002660 mutex_lock(&trace_types_lock);
2661 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002662 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2663 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002664 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002665 }
2666
Steven Rostedtb3806b42008-05-12 21:20:46 +02002667 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002668
Steven Rostedt107bad82008-05-12 21:21:01 +02002669 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002670 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002671 goto out;
2672 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002673
Steven Rostedtb3806b42008-05-12 21:20:46 +02002674 /*
2675 * This is a make-shift waitqueue. The reason we don't use
2676 * an actual wait queue is because:
2677 * 1) we only ever have one waiter
2678 * 2) the tracing, traces all functions, we don't want
2679 * the overhead of calling wake_up and friends
2680 * (and tracing them too)
2681 * Anyway, this is really very primitive wakeup.
2682 */
2683 set_current_state(TASK_INTERRUPTIBLE);
2684 iter->tr->waiter = current;
2685
Steven Rostedt107bad82008-05-12 21:21:01 +02002686 mutex_unlock(&trace_types_lock);
2687
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002688 /* sleep for 100 msecs, and try again. */
2689 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002690
Steven Rostedt107bad82008-05-12 21:21:01 +02002691 mutex_lock(&trace_types_lock);
2692
Steven Rostedtb3806b42008-05-12 21:20:46 +02002693 iter->tr->waiter = NULL;
2694
Steven Rostedt107bad82008-05-12 21:21:01 +02002695 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002696 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002697 goto out;
2698 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002699
Steven Rostedt84527992008-05-12 21:20:58 +02002700 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002701 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002702
Steven Rostedtb3806b42008-05-12 21:20:46 +02002703 /*
2704 * We block until we read something and tracing is disabled.
2705 * We still block if tracing is disabled, but we have never
2706 * read anything. This allows a user to cat this file, and
2707 * then enable tracing. But after we have read something,
2708 * we give an EOF when tracing is again disabled.
2709 *
2710 * iter->pos will be 0 if we haven't read anything.
2711 */
2712 if (!tracer_enabled && iter->pos)
2713 break;
2714
2715 continue;
2716 }
2717
2718 /* stop when tracing is finished */
2719 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002720 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002721
2722 if (cnt >= PAGE_SIZE)
2723 cnt = PAGE_SIZE - 1;
2724
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002725 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002726 memset(&iter->seq, 0,
2727 sizeof(struct trace_iterator) -
2728 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002729 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002730
2731 /*
2732 * We need to stop all tracing on all CPUS to read the
2733 * the next buffer. This is a bit expensive, but is
2734 * not done often. We fill all what we can read,
2735 * and then release the locks again.
2736 */
2737
2738 cpus_clear(mask);
2739 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002740#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002741 ftrace_save = ftrace_enabled;
2742 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002743#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002744 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002745 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002746 data = iter->tr->data[cpu];
2747
2748 if (!head_page(data) || !data->trace_idx)
2749 continue;
2750
2751 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002752 cpu_set(cpu, mask);
2753 }
2754
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002755 for_each_cpu_mask(cpu, mask) {
2756 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002757 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002758
2759 if (data->overrun > iter->last_overrun[cpu])
2760 iter->overrun[cpu] +=
2761 data->overrun - iter->last_overrun[cpu];
2762 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002763 }
2764
Steven Rostedt088b1e422008-05-12 21:20:48 +02002765 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002766 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002767 int len = iter->seq.len;
2768
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002769 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002770 if (!ret) {
2771 /* don't print partial lines */
2772 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002773 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002774 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002775
2776 trace_consume(iter);
2777
2778 if (iter->seq.len >= cnt)
2779 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002780 }
2781
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002782 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002783 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002784 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002785 }
2786
2787 for_each_cpu_mask(cpu, mask) {
2788 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002789 atomic_dec(&data->disabled);
2790 }
Ingo Molnar25770462008-05-12 21:20:49 +02002791#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002792 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002793#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002794 local_irq_restore(flags);
2795
2796 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002797 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2798 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002799 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002800 if (sret == -EBUSY)
2801 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002802
Steven Rostedt107bad82008-05-12 21:21:01 +02002803out:
2804 mutex_unlock(&trace_types_lock);
2805
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002806 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002807}
2808
Steven Rostedta98a3c32008-05-12 21:20:59 +02002809static ssize_t
2810tracing_entries_read(struct file *filp, char __user *ubuf,
2811 size_t cnt, loff_t *ppos)
2812{
2813 struct trace_array *tr = filp->private_data;
2814 char buf[64];
2815 int r;
2816
2817 r = sprintf(buf, "%lu\n", tr->entries);
2818 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2819}
2820
2821static ssize_t
2822tracing_entries_write(struct file *filp, const char __user *ubuf,
2823 size_t cnt, loff_t *ppos)
2824{
2825 unsigned long val;
2826 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002827 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002828
Steven Rostedtcffae432008-05-12 21:21:00 +02002829 if (cnt >= sizeof(buf))
2830 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002831
2832 if (copy_from_user(&buf, ubuf, cnt))
2833 return -EFAULT;
2834
2835 buf[cnt] = 0;
2836
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002837 ret = strict_strtoul(buf, 10, &val);
2838 if (ret < 0)
2839 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002840
2841 /* must have at least 1 entry */
2842 if (!val)
2843 return -EINVAL;
2844
2845 mutex_lock(&trace_types_lock);
2846
2847 if (current_trace != &no_tracer) {
2848 cnt = -EBUSY;
2849 pr_info("ftrace: set current_tracer to none"
2850 " before modifying buffer size\n");
2851 goto out;
2852 }
2853
2854 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002855 long pages_requested;
2856 unsigned long freeable_pages;
2857
2858 /* make sure we have enough memory before mapping */
2859 pages_requested =
2860 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2861
2862 /* account for each buffer (and max_tr) */
2863 pages_requested *= tracing_nr_buffers * 2;
2864
2865 /* Check for overflow */
2866 if (pages_requested < 0) {
2867 cnt = -ENOMEM;
2868 goto out;
2869 }
2870
2871 freeable_pages = determine_dirtyable_memory();
2872
2873 /* we only allow to request 1/4 of useable memory */
2874 if (pages_requested >
2875 ((freeable_pages + tracing_pages_allocated) / 4)) {
2876 cnt = -ENOMEM;
2877 goto out;
2878 }
2879
Steven Rostedta98a3c32008-05-12 21:20:59 +02002880 while (global_trace.entries < val) {
2881 if (trace_alloc_page()) {
2882 cnt = -ENOMEM;
2883 goto out;
2884 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002885 /* double check that we don't go over the known pages */
2886 if (tracing_pages_allocated > pages_requested)
2887 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002888 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002889
Steven Rostedta98a3c32008-05-12 21:20:59 +02002890 } else {
2891 /* include the number of entries in val (inc of page entries) */
2892 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2893 trace_free_page();
2894 }
2895
Steven Rostedt19384c02008-05-22 00:22:16 -04002896 /* check integrity */
2897 for_each_tracing_cpu(i)
2898 check_pages(global_trace.data[i]);
2899
Steven Rostedta98a3c32008-05-12 21:20:59 +02002900 filp->f_pos += cnt;
2901
Steven Rostedt19384c02008-05-22 00:22:16 -04002902 /* If check pages failed, return ENOMEM */
2903 if (tracing_disabled)
2904 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002905 out:
2906 max_tr.entries = global_trace.entries;
2907 mutex_unlock(&trace_types_lock);
2908
2909 return cnt;
2910}
2911
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002912static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002913 .open = tracing_open_generic,
2914 .read = tracing_max_lat_read,
2915 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002916};
2917
2918static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002919 .open = tracing_open_generic,
2920 .read = tracing_ctrl_read,
2921 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002922};
2923
2924static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002925 .open = tracing_open_generic,
2926 .read = tracing_set_trace_read,
2927 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002928};
2929
Steven Rostedtb3806b42008-05-12 21:20:46 +02002930static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002931 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002932 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002933 .read = tracing_read_pipe,
2934 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002935};
2936
Steven Rostedta98a3c32008-05-12 21:20:59 +02002937static struct file_operations tracing_entries_fops = {
2938 .open = tracing_open_generic,
2939 .read = tracing_entries_read,
2940 .write = tracing_entries_write,
2941};
2942
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002943#ifdef CONFIG_DYNAMIC_FTRACE
2944
2945static ssize_t
2946tracing_read_long(struct file *filp, char __user *ubuf,
2947 size_t cnt, loff_t *ppos)
2948{
2949 unsigned long *p = filp->private_data;
2950 char buf[64];
2951 int r;
2952
2953 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002954
2955 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002956}
2957
2958static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002959 .open = tracing_open_generic,
2960 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002961};
2962#endif
2963
2964static struct dentry *d_tracer;
2965
2966struct dentry *tracing_init_dentry(void)
2967{
2968 static int once;
2969
2970 if (d_tracer)
2971 return d_tracer;
2972
2973 d_tracer = debugfs_create_dir("tracing", NULL);
2974
2975 if (!d_tracer && !once) {
2976 once = 1;
2977 pr_warning("Could not create debugfs directory 'tracing'\n");
2978 return NULL;
2979 }
2980
2981 return d_tracer;
2982}
2983
Steven Rostedt60a11772008-05-12 21:20:44 +02002984#ifdef CONFIG_FTRACE_SELFTEST
2985/* Let selftest have access to static functions in this file */
2986#include "trace_selftest.c"
2987#endif
2988
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002989static __init void tracer_init_debugfs(void)
2990{
2991 struct dentry *d_tracer;
2992 struct dentry *entry;
2993
2994 d_tracer = tracing_init_dentry();
2995
2996 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2997 &global_trace, &tracing_ctrl_fops);
2998 if (!entry)
2999 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3000
3001 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3002 NULL, &tracing_iter_fops);
3003 if (!entry)
3004 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3005
Ingo Molnarc7078de2008-05-12 21:20:52 +02003006 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3007 NULL, &tracing_cpumask_fops);
3008 if (!entry)
3009 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3010
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003011 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3012 &global_trace, &tracing_lt_fops);
3013 if (!entry)
3014 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3015
3016 entry = debugfs_create_file("trace", 0444, d_tracer,
3017 &global_trace, &tracing_fops);
3018 if (!entry)
3019 pr_warning("Could not create debugfs 'trace' entry\n");
3020
3021 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3022 &global_trace, &show_traces_fops);
3023 if (!entry)
3024 pr_warning("Could not create debugfs 'trace' entry\n");
3025
3026 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3027 &global_trace, &set_tracer_fops);
3028 if (!entry)
3029 pr_warning("Could not create debugfs 'trace' entry\n");
3030
3031 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3032 &tracing_max_latency,
3033 &tracing_max_lat_fops);
3034 if (!entry)
3035 pr_warning("Could not create debugfs "
3036 "'tracing_max_latency' entry\n");
3037
3038 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3039 &tracing_thresh, &tracing_max_lat_fops);
3040 if (!entry)
3041 pr_warning("Could not create debugfs "
3042 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003043 entry = debugfs_create_file("README", 0644, d_tracer,
3044 NULL, &tracing_readme_fops);
3045 if (!entry)
3046 pr_warning("Could not create debugfs 'README' entry\n");
3047
Steven Rostedtb3806b42008-05-12 21:20:46 +02003048 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3049 NULL, &tracing_pipe_fops);
3050 if (!entry)
3051 pr_warning("Could not create debugfs "
3052 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003053
Steven Rostedta98a3c32008-05-12 21:20:59 +02003054 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3055 &global_trace, &tracing_entries_fops);
3056 if (!entry)
3057 pr_warning("Could not create debugfs "
3058 "'tracing_threash' entry\n");
3059
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003060#ifdef CONFIG_DYNAMIC_FTRACE
3061 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3062 &ftrace_update_tot_cnt,
3063 &tracing_read_long_fops);
3064 if (!entry)
3065 pr_warning("Could not create debugfs "
3066 "'dyn_ftrace_total_info' entry\n");
3067#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003068#ifdef CONFIG_SYSPROF_TRACER
3069 init_tracer_sysprof_debugfs(d_tracer);
3070#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003071}
3072
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003073#define TRACE_BUF_SIZE 1024
3074#define TRACE_PRINT_BUF_SIZE \
3075 (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
3076#define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
3077
3078/**
3079 * ftrace_printk - printf formatting in the ftrace buffer
3080 * @fmt - the printf format for printing.
3081 *
3082 * Note: __ftrace_printk is an internal function for ftrace_printk and
3083 * the @ip is passed in via the ftrace_printk macro.
3084 *
3085 * This function allows a kernel developer to debug fast path sections
3086 * that printk is not appropriate for. By scattering in various
3087 * printk like tracing in the code, a developer can quickly see
3088 * where problems are occurring.
3089 *
3090 * This is intended as a debugging tool for the developer only.
3091 * Please reframe from leaving ftrace_printks scattered around in
3092 * your code.
3093 */
3094int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3095{
3096 struct trace_array *tr = &global_trace;
3097 static DEFINE_SPINLOCK(trace_buf_lock);
3098 static char trace_buf[TRACE_BUF_SIZE];
3099 struct trace_array_cpu *data;
3100 struct trace_entry *entry;
3101 unsigned long flags;
3102 long disabled;
3103 va_list ap;
3104 int cpu, len = 0, write, written = 0;
3105
3106 if (likely(!ftrace_function_enabled))
3107 return 0;
3108
3109 local_irq_save(flags);
3110 cpu = raw_smp_processor_id();
3111 data = tr->data[cpu];
3112 disabled = atomic_inc_return(&data->disabled);
3113
3114 if (unlikely(disabled != 1 || !ftrace_function_enabled))
3115 goto out;
3116
3117 spin_lock(&trace_buf_lock);
3118 va_start(ap, fmt);
3119 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, ap);
3120 va_end(ap);
3121
3122 len = min(len, TRACE_BUF_SIZE-1);
3123 trace_buf[len] = 0;
3124
3125 __raw_spin_lock(&data->lock);
3126 entry = tracing_get_trace_entry(tr, data);
3127 tracing_generic_entry_update(entry, flags);
3128 entry->type = TRACE_PRINT;
3129 entry->field.print.ip = ip;
3130
3131 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3132
3133 memcpy(&entry->field.print.buf, trace_buf, write);
3134 entry->field.print.buf[write] = 0;
3135 written = write;
3136
3137 if (written != len)
3138 entry->field.flags |= TRACE_FLAG_CONT;
3139
3140 while (written != len) {
3141 entry = tracing_get_trace_entry(tr, data);
3142
3143 entry->type = TRACE_CONT;
3144 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3145 memcpy(&entry->cont.buf, trace_buf+written, write);
3146 entry->cont.buf[write] = 0;
3147 written += write;
3148 }
3149 __raw_spin_unlock(&data->lock);
3150
3151 spin_unlock(&trace_buf_lock);
3152
3153 out:
3154 atomic_dec(&data->disabled);
3155 local_irq_restore(flags);
3156
3157 return len;
3158}
3159EXPORT_SYMBOL_GPL(__ftrace_printk);
3160
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003161static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003162{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003163 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003164 struct page *page, *tmp;
3165 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003166 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003167 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003168 int i;
3169
3170 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02003171 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003172 array = (void *)__get_free_page(GFP_KERNEL);
3173 if (array == NULL) {
3174 printk(KERN_ERR "tracer: failed to allocate page"
3175 "for trace buffer!\n");
3176 goto free_pages;
3177 }
3178
Steven Rostedt3eefae92008-05-12 21:21:04 +02003179 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003180 page = virt_to_page(array);
3181 list_add(&page->lru, &pages);
3182
3183/* Only allocate if we are actually using the max trace */
3184#ifdef CONFIG_TRACER_MAX_TRACE
3185 array = (void *)__get_free_page(GFP_KERNEL);
3186 if (array == NULL) {
3187 printk(KERN_ERR "tracer: failed to allocate page"
3188 "for trace buffer!\n");
3189 goto free_pages;
3190 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003191 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003192 page = virt_to_page(array);
3193 list_add(&page->lru, &pages);
3194#endif
3195 }
3196
3197 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02003198 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003199 data = global_trace.data[i];
3200 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003201 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003202 list_add_tail(&page->lru, &data->trace_pages);
3203 ClearPageLRU(page);
3204
3205#ifdef CONFIG_TRACER_MAX_TRACE
3206 data = max_tr.data[i];
3207 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003208 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003209 list_add_tail(&page->lru, &data->trace_pages);
3210 SetPageLRU(page);
3211#endif
3212 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003213 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003214 global_trace.entries += ENTRIES_PER_PAGE;
3215
3216 return 0;
3217
3218 free_pages:
3219 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003220 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003221 __free_page(page);
3222 }
3223 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003224}
3225
Steven Rostedta98a3c32008-05-12 21:20:59 +02003226static int trace_free_page(void)
3227{
3228 struct trace_array_cpu *data;
3229 struct page *page;
3230 struct list_head *p;
3231 int i;
3232 int ret = 0;
3233
3234 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02003235 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003236 data = global_trace.data[i];
3237 p = data->trace_pages.next;
3238 if (p == &data->trace_pages) {
3239 /* should never happen */
3240 WARN_ON(1);
3241 tracing_disabled = 1;
3242 ret = -1;
3243 break;
3244 }
3245 page = list_entry(p, struct page, lru);
3246 ClearPageLRU(page);
3247 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02003248 tracing_pages_allocated--;
3249 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003250 __free_page(page);
3251
3252 tracing_reset(data);
3253
3254#ifdef CONFIG_TRACER_MAX_TRACE
3255 data = max_tr.data[i];
3256 p = data->trace_pages.next;
3257 if (p == &data->trace_pages) {
3258 /* should never happen */
3259 WARN_ON(1);
3260 tracing_disabled = 1;
3261 ret = -1;
3262 break;
3263 }
3264 page = list_entry(p, struct page, lru);
3265 ClearPageLRU(page);
3266 list_del(&page->lru);
3267 __free_page(page);
3268
3269 tracing_reset(data);
3270#endif
3271 }
3272 global_trace.entries -= ENTRIES_PER_PAGE;
3273
3274 return ret;
3275}
3276
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003277__init static int tracer_alloc_buffers(void)
3278{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003279 struct trace_array_cpu *data;
3280 void *array;
3281 struct page *page;
3282 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02003283 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003284 int i;
3285
Steven Rostedtab464282008-05-12 21:21:00 +02003286 /* TODO: make the number of buffers hot pluggable with CPUS */
3287 tracing_nr_buffers = num_possible_cpus();
3288 tracing_buffer_mask = cpu_possible_map;
3289
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003290 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003291 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003292 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003293 max_tr.data[i] = &per_cpu(max_data, i);
3294
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003295 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003296 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003297 printk(KERN_ERR "tracer: failed to allocate page"
3298 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003299 goto free_buffers;
3300 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003301
3302 /* set the array to the list */
3303 INIT_LIST_HEAD(&data->trace_pages);
3304 page = virt_to_page(array);
3305 list_add(&page->lru, &data->trace_pages);
3306 /* use the LRU flag to differentiate the two buffers */
3307 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003308
Steven Rostedta98a3c32008-05-12 21:20:59 +02003309 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3310 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3311
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003312/* Only allocate if we are actually using the max trace */
3313#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003314 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003315 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003316 printk(KERN_ERR "tracer: failed to allocate page"
3317 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003318 goto free_buffers;
3319 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003320
3321 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3322 page = virt_to_page(array);
3323 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3324 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003325#endif
3326 }
3327
3328 /*
3329 * Since we allocate by orders of pages, we may be able to
3330 * round up a bit.
3331 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003332 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003333 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003334
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003335 while (global_trace.entries < trace_nr_entries) {
3336 if (trace_alloc_page())
3337 break;
3338 pages++;
3339 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003340 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003341
Jiri Slaby20764ff2008-06-12 11:27:03 +02003342 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3343 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003344 pr_info(" actual entries %ld\n", global_trace.entries);
3345
3346 tracer_init_debugfs();
3347
3348 trace_init_cmdlines();
3349
3350 register_tracer(&no_tracer);
3351 current_trace = &no_tracer;
3352
Steven Rostedt60a11772008-05-12 21:20:44 +02003353 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003354 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003355 tracing_disabled = 0;
3356
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003357 return 0;
3358
3359 free_buffers:
3360 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003361 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003362 struct trace_array_cpu *data = global_trace.data[i];
3363
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003364 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003365 list_for_each_entry_safe(page, tmp,
3366 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003367 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003368 __free_page(page);
3369 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003370 }
3371
3372#ifdef CONFIG_TRACER_MAX_TRACE
3373 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003374 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003375 list_for_each_entry_safe(page, tmp,
3376 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003377 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003378 __free_page(page);
3379 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003380 }
3381#endif
3382 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003383 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003384}
Steven Rostedt60a11772008-05-12 21:20:44 +02003385fs_initcall(tracer_alloc_buffers);