blob: 2e37857f7dfe0f2585e50302ae0ffef9d6fb3c4e [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
200 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200201enum trace_flag_type {
202 TRACE_FLAG_IRQS_OFF = 0x01,
203 TRACE_FLAG_NEED_RESCHED = 0x02,
204 TRACE_FLAG_HARDIRQ = 0x04,
205 TRACE_FLAG_SOFTIRQ = 0x08,
206};
207
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200208/*
209 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
210 * control the output of kernel symbols.
211 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200212#define TRACE_ITER_SYM_MASK \
213 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
214
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200215/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200216static const char *trace_options[] = {
217 "print-parent",
218 "sym-offset",
219 "sym-addr",
220 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200221 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200222 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200223 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200224 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200225 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200226 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200227 NULL
228};
229
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200230/*
231 * ftrace_max_lock is used to protect the swapping of buffers
232 * when taking a max snapshot. The buffers themselves are
233 * protected by per_cpu spinlocks. But the action of the swap
234 * needs its own lock.
235 *
236 * This is defined as a raw_spinlock_t in order to help
237 * with performance when lockdep debugging is enabled.
238 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200239static raw_spinlock_t ftrace_max_lock =
240 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200241
242/*
243 * Copy the new maximum trace into the separate maximum-trace
244 * structure. (this way the maximum trace is permanently saved,
245 * for later retrieval via /debugfs/tracing/latency_trace)
246 */
Ingo Molnare309b412008-05-12 21:20:51 +0200247static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200248__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
249{
250 struct trace_array_cpu *data = tr->data[cpu];
251
252 max_tr.cpu = cpu;
253 max_tr.time_start = data->preempt_timestamp;
254
255 data = max_tr.data[cpu];
256 data->saved_latency = tracing_max_latency;
257
258 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
259 data->pid = tsk->pid;
260 data->uid = tsk->uid;
261 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
262 data->policy = tsk->policy;
263 data->rt_priority = tsk->rt_priority;
264
265 /* record this tasks comm */
266 tracing_record_cmdline(current);
267}
268
Steven Rostedt19384c02008-05-22 00:22:16 -0400269#define CHECK_COND(cond) \
270 if (unlikely(cond)) { \
271 tracing_disabled = 1; \
272 WARN_ON(1); \
273 return -1; \
274 }
275
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200276/**
277 * check_pages - integrity check of trace buffers
278 *
279 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400280 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200281 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400282int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200283{
284 struct page *page, *tmp;
285
Steven Rostedt19384c02008-05-22 00:22:16 -0400286 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
287 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200288
289 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400290 CHECK_COND(page->lru.next->prev != &page->lru);
291 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200292 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400293
294 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200295}
296
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200297/**
298 * head_page - page address of the first page in per_cpu buffer.
299 *
300 * head_page returns the page address of the first page in
301 * a per_cpu buffer. This also preforms various consistency
302 * checks to make sure the buffer has not been corrupted.
303 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200304void *head_page(struct trace_array_cpu *data)
305{
306 struct page *page;
307
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200308 if (list_empty(&data->trace_pages))
309 return NULL;
310
311 page = list_entry(data->trace_pages.next, struct page, lru);
312 BUG_ON(&page->lru == &data->trace_pages);
313
314 return page_address(page);
315}
316
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200317/**
318 * trace_seq_printf - sequence printing of trace information
319 * @s: trace sequence descriptor
320 * @fmt: printf format string
321 *
322 * The tracer may use either sequence operations or its own
323 * copy to user routines. To simplify formating of a trace
324 * trace_seq_printf is used to store strings into a special
325 * buffer (@s). Then the output may be either used by
326 * the sequencer or pulled into another buffer.
327 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200328int
Steven Rostedt214023c2008-05-12 21:20:46 +0200329trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
330{
331 int len = (PAGE_SIZE - 1) - s->len;
332 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200333 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200334
335 if (!len)
336 return 0;
337
338 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200339 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200340 va_end(ap);
341
Steven Rostedtb3806b42008-05-12 21:20:46 +0200342 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200343 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200344 return 0;
345
346 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200347
348 return len;
349}
350
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200351/**
352 * trace_seq_puts - trace sequence printing of simple string
353 * @s: trace sequence descriptor
354 * @str: simple string to record
355 *
356 * The tracer may use either the sequence operations or its own
357 * copy to user routines. This function records a simple string
358 * into a special buffer (@s) for later retrieval by a sequencer
359 * or other mechanism.
360 */
Ingo Molnare309b412008-05-12 21:20:51 +0200361static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200362trace_seq_puts(struct trace_seq *s, const char *str)
363{
364 int len = strlen(str);
365
366 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200367 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200368
369 memcpy(s->buffer + s->len, str, len);
370 s->len += len;
371
372 return len;
373}
374
Ingo Molnare309b412008-05-12 21:20:51 +0200375static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200376trace_seq_putc(struct trace_seq *s, unsigned char c)
377{
378 if (s->len >= (PAGE_SIZE - 1))
379 return 0;
380
381 s->buffer[s->len++] = c;
382
383 return 1;
384}
385
Ingo Molnare309b412008-05-12 21:20:51 +0200386static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200387trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
388{
389 if (len > ((PAGE_SIZE - 1) - s->len))
390 return 0;
391
392 memcpy(s->buffer + s->len, mem, len);
393 s->len += len;
394
395 return len;
396}
397
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200398#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200399static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200400
Ingo Molnare309b412008-05-12 21:20:51 +0200401static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200402trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
403{
404 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200405 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200406 unsigned char byte;
407 int i, j;
408
409 BUG_ON(len >= HEX_CHARS);
410
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200411#ifdef __BIG_ENDIAN
412 for (i = 0, j = 0; i < len; i++) {
413#else
414 for (i = len-1, j = 0; i >= 0; i--) {
415#endif
416 byte = data[i];
417
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200418 hex[j++] = hex2asc[byte & 0x0f];
419 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200420 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200421 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200422
423 return trace_seq_putmem(s, hex, j);
424}
425
Ingo Molnare309b412008-05-12 21:20:51 +0200426static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200427trace_seq_reset(struct trace_seq *s)
428{
429 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200430 s->readpos = 0;
431}
432
433ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
434{
435 int len;
436 int ret;
437
438 if (s->len <= s->readpos)
439 return -EBUSY;
440
441 len = s->len - s->readpos;
442 if (cnt > len)
443 cnt = len;
444 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
445 if (ret)
446 return -EFAULT;
447
448 s->readpos += len;
449 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200450}
451
Ingo Molnare309b412008-05-12 21:20:51 +0200452static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200453trace_print_seq(struct seq_file *m, struct trace_seq *s)
454{
455 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
456
457 s->buffer[len] = 0;
458 seq_puts(m, s->buffer);
459
460 trace_seq_reset(s);
461}
462
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200463/*
464 * flip the trace buffers between two trace descriptors.
465 * This usually is the buffers between the global_trace and
466 * the max_tr to record a snapshot of a current trace.
467 *
468 * The ftrace_max_lock must be held.
469 */
Ingo Molnare309b412008-05-12 21:20:51 +0200470static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200471flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
472{
473 struct list_head flip_pages;
474
475 INIT_LIST_HEAD(&flip_pages);
476
Steven Rostedt93a588f2008-05-12 21:20:45 +0200477 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200478 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200479 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200480
481 check_pages(tr1);
482 check_pages(tr2);
483 list_splice_init(&tr1->trace_pages, &flip_pages);
484 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
485 list_splice_init(&flip_pages, &tr2->trace_pages);
486 BUG_ON(!list_empty(&flip_pages));
487 check_pages(tr1);
488 check_pages(tr2);
489}
490
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200491/**
492 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
493 * @tr: tracer
494 * @tsk: the task with the latency
495 * @cpu: The cpu that initiated the trace.
496 *
497 * Flip the buffers between the @tr and the max_tr and record information
498 * about which task was the cause of this latency.
499 */
Ingo Molnare309b412008-05-12 21:20:51 +0200500void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
502{
503 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200504 int i;
505
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200506 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200507 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200508 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200509 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200510 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200511 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200512 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200513 }
514
515 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200516 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200517}
518
519/**
520 * update_max_tr_single - only copy one trace over, and reset the rest
521 * @tr - tracer
522 * @tsk - task with the latency
523 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200524 *
525 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200526 */
Ingo Molnare309b412008-05-12 21:20:51 +0200527void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200528update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
529{
530 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200531 int i;
532
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200533 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200534 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200535 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200536 tracing_reset(max_tr.data[i]);
537
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200538 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200539 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200540
541 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200542 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200543}
544
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200545/**
546 * register_tracer - register a tracer with the ftrace system.
547 * @type - the plugin for the tracer
548 *
549 * Register a new plugin tracer.
550 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200551int register_tracer(struct tracer *type)
552{
553 struct tracer *t;
554 int len;
555 int ret = 0;
556
557 if (!type->name) {
558 pr_info("Tracer must have a name\n");
559 return -1;
560 }
561
562 mutex_lock(&trace_types_lock);
563 for (t = trace_types; t; t = t->next) {
564 if (strcmp(type->name, t->name) == 0) {
565 /* already found */
566 pr_info("Trace %s already registered\n",
567 type->name);
568 ret = -1;
569 goto out;
570 }
571 }
572
Steven Rostedt60a11772008-05-12 21:20:44 +0200573#ifdef CONFIG_FTRACE_STARTUP_TEST
574 if (type->selftest) {
575 struct tracer *saved_tracer = current_trace;
576 struct trace_array_cpu *data;
577 struct trace_array *tr = &global_trace;
578 int saved_ctrl = tr->ctrl;
579 int i;
580 /*
581 * Run a selftest on this tracer.
582 * Here we reset the trace buffer, and set the current
583 * tracer to be this tracer. The tracer can then run some
584 * internal tracing to verify that everything is in order.
585 * If we fail, we do not register this tracer.
586 */
Steven Rostedtab464282008-05-12 21:21:00 +0200587 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200588 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200589 if (!head_page(data))
590 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200591 tracing_reset(data);
592 }
593 current_trace = type;
594 tr->ctrl = 0;
595 /* the test is responsible for initializing and enabling */
596 pr_info("Testing tracer %s: ", type->name);
597 ret = type->selftest(type, tr);
598 /* the test is responsible for resetting too */
599 current_trace = saved_tracer;
600 tr->ctrl = saved_ctrl;
601 if (ret) {
602 printk(KERN_CONT "FAILED!\n");
603 goto out;
604 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200605 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200606 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200607 data = tr->data[i];
608 if (!head_page(data))
609 continue;
610 tracing_reset(data);
611 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200612 printk(KERN_CONT "PASSED\n");
613 }
614#endif
615
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200616 type->next = trace_types;
617 trace_types = type;
618 len = strlen(type->name);
619 if (len > max_tracer_type_len)
620 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200621
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200622 out:
623 mutex_unlock(&trace_types_lock);
624
625 return ret;
626}
627
628void unregister_tracer(struct tracer *type)
629{
630 struct tracer **t;
631 int len;
632
633 mutex_lock(&trace_types_lock);
634 for (t = &trace_types; *t; t = &(*t)->next) {
635 if (*t == type)
636 goto found;
637 }
638 pr_info("Trace %s not registered\n", type->name);
639 goto out;
640
641 found:
642 *t = (*t)->next;
643 if (strlen(type->name) != max_tracer_type_len)
644 goto out;
645
646 max_tracer_type_len = 0;
647 for (t = &trace_types; *t; t = &(*t)->next) {
648 len = strlen((*t)->name);
649 if (len > max_tracer_type_len)
650 max_tracer_type_len = len;
651 }
652 out:
653 mutex_unlock(&trace_types_lock);
654}
655
Ingo Molnare309b412008-05-12 21:20:51 +0200656void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200657{
658 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200659 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200660 data->trace_head = data->trace_tail = head_page(data);
661 data->trace_head_idx = 0;
662 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200663}
664
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200665#define SAVED_CMDLINES 128
666static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
667static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
668static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
669static int cmdline_idx;
670static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200671
Steven Rostedt25b0b442008-05-12 21:21:00 +0200672/* temporary disable recording */
673atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200674
675static void trace_init_cmdlines(void)
676{
677 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
678 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
679 cmdline_idx = 0;
680}
681
Ingo Molnare309b412008-05-12 21:20:51 +0200682void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200683
Ingo Molnare309b412008-05-12 21:20:51 +0200684static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200685{
686 unsigned map;
687 unsigned idx;
688
689 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
690 return;
691
692 /*
693 * It's not the end of the world if we don't get
694 * the lock, but we also don't want to spin
695 * nor do we want to disable interrupts,
696 * so if we miss here, then better luck next time.
697 */
698 if (!spin_trylock(&trace_cmdline_lock))
699 return;
700
701 idx = map_pid_to_cmdline[tsk->pid];
702 if (idx >= SAVED_CMDLINES) {
703 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
704
705 map = map_cmdline_to_pid[idx];
706 if (map <= PID_MAX_DEFAULT)
707 map_pid_to_cmdline[map] = (unsigned)-1;
708
709 map_pid_to_cmdline[tsk->pid] = idx;
710
711 cmdline_idx = idx;
712 }
713
714 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
715
716 spin_unlock(&trace_cmdline_lock);
717}
718
Ingo Molnare309b412008-05-12 21:20:51 +0200719static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200720{
721 char *cmdline = "<...>";
722 unsigned map;
723
724 if (!pid)
725 return "<idle>";
726
727 if (pid > PID_MAX_DEFAULT)
728 goto out;
729
730 map = map_pid_to_cmdline[pid];
731 if (map >= SAVED_CMDLINES)
732 goto out;
733
734 cmdline = saved_cmdlines[map];
735
736 out:
737 return cmdline;
738}
739
Ingo Molnare309b412008-05-12 21:20:51 +0200740void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200741{
742 if (atomic_read(&trace_record_cmdline_disabled))
743 return;
744
745 trace_save_cmdline(tsk);
746}
747
Ingo Molnare309b412008-05-12 21:20:51 +0200748static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200749trace_next_list(struct trace_array_cpu *data, struct list_head *next)
750{
751 /*
752 * Roundrobin - but skip the head (which is not a real page):
753 */
754 next = next->next;
755 if (unlikely(next == &data->trace_pages))
756 next = next->next;
757 BUG_ON(next == &data->trace_pages);
758
759 return next;
760}
761
Ingo Molnare309b412008-05-12 21:20:51 +0200762static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200763trace_next_page(struct trace_array_cpu *data, void *addr)
764{
765 struct list_head *next;
766 struct page *page;
767
768 page = virt_to_page(addr);
769
770 next = trace_next_list(data, &page->lru);
771 page = list_entry(next, struct page, lru);
772
773 return page_address(page);
774}
775
Ingo Molnare309b412008-05-12 21:20:51 +0200776static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200777tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200778{
779 unsigned long idx, idx_next;
780 struct trace_entry *entry;
781
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200782 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200783 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200784 idx_next = idx + 1;
785
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200786 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
787
Steven Rostedt93a588f2008-05-12 21:20:45 +0200788 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200789
790 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200791 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200792 idx_next = 0;
793 }
794
Steven Rostedt93a588f2008-05-12 21:20:45 +0200795 if (data->trace_head == data->trace_tail &&
796 idx_next == data->trace_tail_idx) {
797 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200798 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200799 data->trace_tail_idx++;
800 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
801 data->trace_tail =
802 trace_next_page(data, data->trace_tail);
803 data->trace_tail_idx = 0;
804 }
805 }
806
807 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808
809 return entry;
810}
811
Ingo Molnare309b412008-05-12 21:20:51 +0200812static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200813tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200814{
815 struct task_struct *tsk = current;
816 unsigned long pc;
817
818 pc = preempt_count();
819
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200820 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200821 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200822 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200823 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
824 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
825 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
826 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
827}
828
Ingo Molnare309b412008-05-12 21:20:51 +0200829void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200830trace_function(struct trace_array *tr, struct trace_array_cpu *data,
831 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200832{
833 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200834 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200835
Steven Rostedt92205c22008-05-12 21:20:55 +0200836 raw_local_irq_save(irq_flags);
837 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200838 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200839 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200840 entry->type = TRACE_FN;
841 entry->fn.ip = ip;
842 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200843 __raw_spin_unlock(&data->lock);
844 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200845}
846
Ingo Molnare309b412008-05-12 21:20:51 +0200847void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200848ftrace(struct trace_array *tr, struct trace_array_cpu *data,
849 unsigned long ip, unsigned long parent_ip, unsigned long flags)
850{
851 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200852 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200853}
854
Ingo Molnar86387f72008-05-12 21:20:51 +0200855void __trace_stack(struct trace_array *tr,
856 struct trace_array_cpu *data,
857 unsigned long flags,
858 int skip)
859{
860 struct trace_entry *entry;
861 struct stack_trace trace;
862
863 if (!(trace_flags & TRACE_ITER_STACKTRACE))
864 return;
865
866 entry = tracing_get_trace_entry(tr, data);
867 tracing_generic_entry_update(entry, flags);
868 entry->type = TRACE_STACK;
869
870 memset(&entry->stack, 0, sizeof(entry->stack));
871
872 trace.nr_entries = 0;
873 trace.max_entries = FTRACE_STACK_ENTRIES;
874 trace.skip = skip;
875 trace.entries = entry->stack.caller;
876
877 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200878}
879
Ingo Molnare309b412008-05-12 21:20:51 +0200880void
Ingo Molnara4feb832008-05-12 21:21:02 +0200881__trace_special(void *__tr, void *__data,
882 unsigned long arg1, unsigned long arg2, unsigned long arg3)
883{
884 struct trace_array_cpu *data = __data;
885 struct trace_array *tr = __tr;
886 struct trace_entry *entry;
887 unsigned long irq_flags;
888
889 raw_local_irq_save(irq_flags);
890 __raw_spin_lock(&data->lock);
891 entry = tracing_get_trace_entry(tr, data);
892 tracing_generic_entry_update(entry, 0);
893 entry->type = TRACE_SPECIAL;
894 entry->special.arg1 = arg1;
895 entry->special.arg2 = arg2;
896 entry->special.arg3 = arg3;
897 __trace_stack(tr, data, irq_flags, 4);
898 __raw_spin_unlock(&data->lock);
899 raw_local_irq_restore(irq_flags);
900
901 trace_wake_up();
902}
903
904void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200905tracing_sched_switch_trace(struct trace_array *tr,
906 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200907 struct task_struct *prev,
908 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200909 unsigned long flags)
910{
911 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200912 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200913
Steven Rostedt92205c22008-05-12 21:20:55 +0200914 raw_local_irq_save(irq_flags);
915 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200916 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200917 tracing_generic_entry_update(entry, flags);
918 entry->type = TRACE_CTX;
919 entry->ctx.prev_pid = prev->pid;
920 entry->ctx.prev_prio = prev->prio;
921 entry->ctx.prev_state = prev->state;
922 entry->ctx.next_pid = next->pid;
923 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200924 entry->ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200925 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200926 __raw_spin_unlock(&data->lock);
927 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200928}
929
Ingo Molnar57422792008-05-12 21:20:51 +0200930void
931tracing_sched_wakeup_trace(struct trace_array *tr,
932 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200933 struct task_struct *wakee,
934 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200935 unsigned long flags)
936{
937 struct trace_entry *entry;
938 unsigned long irq_flags;
939
Steven Rostedt92205c22008-05-12 21:20:55 +0200940 raw_local_irq_save(irq_flags);
941 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200942 entry = tracing_get_trace_entry(tr, data);
943 tracing_generic_entry_update(entry, flags);
944 entry->type = TRACE_WAKE;
945 entry->ctx.prev_pid = curr->pid;
946 entry->ctx.prev_prio = curr->prio;
947 entry->ctx.prev_state = curr->state;
948 entry->ctx.next_pid = wakee->pid;
949 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200950 entry->ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200951 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200952 __raw_spin_unlock(&data->lock);
953 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200954
955 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200956}
957
Steven Rostedt4902f882008-05-22 00:22:18 -0400958void
959ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
960{
961 struct trace_array *tr = &global_trace;
962 struct trace_array_cpu *data;
963 unsigned long flags;
964 long disabled;
965 int cpu;
966
967 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
968 return;
969
970 local_irq_save(flags);
971 cpu = raw_smp_processor_id();
972 data = tr->data[cpu];
973 disabled = atomic_inc_return(&data->disabled);
974
975 if (likely(disabled == 1))
976 __trace_special(tr, data, arg1, arg2, arg3);
977
978 atomic_dec(&data->disabled);
979 local_irq_restore(flags);
980}
981
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200982#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200983static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200984function_trace_call(unsigned long ip, unsigned long parent_ip)
985{
986 struct trace_array *tr = &global_trace;
987 struct trace_array_cpu *data;
988 unsigned long flags;
989 long disabled;
990 int cpu;
991
Steven Rostedt60bc0802008-07-10 20:58:16 -0400992 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200993 return;
994
Abhishek Sagarecea6562008-06-21 23:47:53 +0530995 if (skip_trace(ip))
996 return;
997
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200998 local_irq_save(flags);
999 cpu = raw_smp_processor_id();
1000 data = tr->data[cpu];
1001 disabled = atomic_inc_return(&data->disabled);
1002
1003 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +02001004 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001005
1006 atomic_dec(&data->disabled);
1007 local_irq_restore(flags);
1008}
1009
1010static struct ftrace_ops trace_ops __read_mostly =
1011{
1012 .func = function_trace_call,
1013};
1014
Ingo Molnare309b412008-05-12 21:20:51 +02001015void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001016{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001017 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001018 register_ftrace_function(&trace_ops);
Steven Rostedt60bc0802008-07-10 20:58:16 -04001019 if (tracer_enabled)
1020 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001021}
1022
Ingo Molnare309b412008-05-12 21:20:51 +02001023void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001024{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001025 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001026 unregister_ftrace_function(&trace_ops);
1027}
1028#endif
1029
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001030enum trace_file_type {
1031 TRACE_FILE_LAT_FMT = 1,
1032};
1033
1034static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001035trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1036 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001037{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001038 struct page *page;
1039 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001040
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001041 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001042 iter->next_idx[cpu] >= data->trace_idx ||
1043 (data->trace_head == data->trace_tail &&
1044 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001045 return NULL;
1046
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001047 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001048 /* Initialize the iterator for this cpu trace buffer */
1049 WARN_ON(!data->trace_tail);
1050 page = virt_to_page(data->trace_tail);
1051 iter->next_page[cpu] = &page->lru;
1052 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001053 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001054
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001055 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001056 BUG_ON(&data->trace_pages == &page->lru);
1057
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001058 array = page_address(page);
1059
Steven Rostedt93a588f2008-05-12 21:20:45 +02001060 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001061 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001062}
1063
Ingo Molnare309b412008-05-12 21:20:51 +02001064static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001065find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1066{
1067 struct trace_array *tr = iter->tr;
1068 struct trace_entry *ent, *next = NULL;
1069 int next_cpu = -1;
1070 int cpu;
1071
Steven Rostedtab464282008-05-12 21:21:00 +02001072 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001073 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001074 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001075 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001076 /*
1077 * Pick the entry with the smallest timestamp:
1078 */
1079 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001080 next = ent;
1081 next_cpu = cpu;
1082 }
1083 }
1084
1085 if (ent_cpu)
1086 *ent_cpu = next_cpu;
1087
1088 return next;
1089}
1090
Ingo Molnare309b412008-05-12 21:20:51 +02001091static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001092{
1093 iter->idx++;
1094 iter->next_idx[iter->cpu]++;
1095 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001096
Steven Rostedtb3806b42008-05-12 21:20:46 +02001097 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1098 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1099
1100 iter->next_page_idx[iter->cpu] = 0;
1101 iter->next_page[iter->cpu] =
1102 trace_next_list(data, iter->next_page[iter->cpu]);
1103 }
1104}
1105
Ingo Molnare309b412008-05-12 21:20:51 +02001106static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001107{
1108 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1109
1110 data->trace_tail_idx++;
1111 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1112 data->trace_tail = trace_next_page(data, data->trace_tail);
1113 data->trace_tail_idx = 0;
1114 }
1115
1116 /* Check if we empty it, then reset the index */
1117 if (data->trace_head == data->trace_tail &&
1118 data->trace_head_idx == data->trace_tail_idx)
1119 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001120}
1121
Ingo Molnare309b412008-05-12 21:20:51 +02001122static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001123{
1124 struct trace_entry *next;
1125 int next_cpu = -1;
1126
1127 next = find_next_entry(iter, &next_cpu);
1128
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001129 iter->prev_ent = iter->ent;
1130 iter->prev_cpu = iter->cpu;
1131
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001132 iter->ent = next;
1133 iter->cpu = next_cpu;
1134
Steven Rostedtb3806b42008-05-12 21:20:46 +02001135 if (next)
1136 trace_iterator_increment(iter);
1137
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001138 return next ? iter : NULL;
1139}
1140
Ingo Molnare309b412008-05-12 21:20:51 +02001141static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001142{
1143 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001144 void *last_ent = iter->ent;
1145 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001146 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001147
1148 (*pos)++;
1149
1150 /* can't go backwards */
1151 if (iter->idx > i)
1152 return NULL;
1153
1154 if (iter->idx < 0)
1155 ent = find_next_entry_inc(iter);
1156 else
1157 ent = iter;
1158
1159 while (ent && iter->idx < i)
1160 ent = find_next_entry_inc(iter);
1161
1162 iter->pos = *pos;
1163
1164 if (last_ent && !ent)
1165 seq_puts(m, "\n\nvim:ft=help\n");
1166
1167 return ent;
1168}
1169
1170static void *s_start(struct seq_file *m, loff_t *pos)
1171{
1172 struct trace_iterator *iter = m->private;
1173 void *p = NULL;
1174 loff_t l = 0;
1175 int i;
1176
1177 mutex_lock(&trace_types_lock);
1178
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001179 if (!current_trace || current_trace != iter->trace) {
1180 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001181 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001182 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001183
1184 atomic_inc(&trace_record_cmdline_disabled);
1185
1186 /* let the tracer grab locks here if needed */
1187 if (current_trace->start)
1188 current_trace->start(iter);
1189
1190 if (*pos != iter->pos) {
1191 iter->ent = NULL;
1192 iter->cpu = 0;
1193 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001194 iter->prev_ent = NULL;
1195 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001196
Steven Rostedtab464282008-05-12 21:21:00 +02001197 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001198 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001199 iter->next_page[i] = NULL;
1200 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001201
1202 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1203 ;
1204
1205 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001206 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001207 p = s_next(m, p, &l);
1208 }
1209
1210 return p;
1211}
1212
1213static void s_stop(struct seq_file *m, void *p)
1214{
1215 struct trace_iterator *iter = m->private;
1216
1217 atomic_dec(&trace_record_cmdline_disabled);
1218
1219 /* let the tracer release locks here if needed */
1220 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1221 iter->trace->stop(iter);
1222
1223 mutex_unlock(&trace_types_lock);
1224}
1225
Abhishek Sagar76094a22008-05-28 00:03:18 +05301226#define KRETPROBE_MSG "[unknown/kretprobe'd]"
1227
1228#ifdef CONFIG_KRETPROBES
1229static inline int kretprobed(unsigned long addr)
1230{
1231 return addr == (unsigned long)kretprobe_trampoline;
1232}
1233#else
1234static inline int kretprobed(unsigned long addr)
1235{
1236 return 0;
1237}
1238#endif /* CONFIG_KRETPROBES */
1239
Steven Rostedtb3806b42008-05-12 21:20:46 +02001240static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001241seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001242{
1243#ifdef CONFIG_KALLSYMS
1244 char str[KSYM_SYMBOL_LEN];
1245
1246 kallsyms_lookup(address, NULL, NULL, NULL, str);
1247
Steven Rostedtb3806b42008-05-12 21:20:46 +02001248 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001249#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001250 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001251}
1252
Steven Rostedtb3806b42008-05-12 21:20:46 +02001253static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001254seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1255 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001256{
1257#ifdef CONFIG_KALLSYMS
1258 char str[KSYM_SYMBOL_LEN];
1259
1260 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001261 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001262#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001263 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001264}
1265
1266#ifndef CONFIG_64BIT
1267# define IP_FMT "%08lx"
1268#else
1269# define IP_FMT "%016lx"
1270#endif
1271
Ingo Molnare309b412008-05-12 21:20:51 +02001272static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001273seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001274{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001275 int ret;
1276
1277 if (!ip)
1278 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001279
1280 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001281 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001282 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001283 ret = seq_print_sym_short(s, "%s", ip);
1284
1285 if (!ret)
1286 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001287
1288 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001289 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1290 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001291}
1292
Ingo Molnare309b412008-05-12 21:20:51 +02001293static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001294{
1295 seq_puts(m, "# _------=> CPU# \n");
1296 seq_puts(m, "# / _-----=> irqs-off \n");
1297 seq_puts(m, "# | / _----=> need-resched \n");
1298 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1299 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1300 seq_puts(m, "# |||| / \n");
1301 seq_puts(m, "# ||||| delay \n");
1302 seq_puts(m, "# cmd pid ||||| time | caller \n");
1303 seq_puts(m, "# \\ / ||||| \\ | / \n");
1304}
1305
Ingo Molnare309b412008-05-12 21:20:51 +02001306static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001307{
1308 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1309 seq_puts(m, "# | | | | |\n");
1310}
1311
1312
Ingo Molnare309b412008-05-12 21:20:51 +02001313static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001314print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1315{
1316 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1317 struct trace_array *tr = iter->tr;
1318 struct trace_array_cpu *data = tr->data[tr->cpu];
1319 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001320 unsigned long total = 0;
1321 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322 int cpu;
1323 const char *name = "preemption";
1324
1325 if (type)
1326 name = type->name;
1327
Steven Rostedtab464282008-05-12 21:21:00 +02001328 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001329 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001330 total += tr->data[cpu]->trace_idx;
1331 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001332 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001333 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001334 entries += tr->data[cpu]->trace_idx;
1335 }
1336 }
1337
1338 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1339 name, UTS_RELEASE);
1340 seq_puts(m, "-----------------------------------"
1341 "---------------------------------\n");
1342 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1343 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001344 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001345 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001346 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001347 tr->cpu,
1348#if defined(CONFIG_PREEMPT_NONE)
1349 "server",
1350#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1351 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001352#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353 "preempt",
1354#else
1355 "unknown",
1356#endif
1357 /* These are reserved for later use */
1358 0, 0, 0, 0);
1359#ifdef CONFIG_SMP
1360 seq_printf(m, " #P:%d)\n", num_online_cpus());
1361#else
1362 seq_puts(m, ")\n");
1363#endif
1364 seq_puts(m, " -----------------\n");
1365 seq_printf(m, " | task: %.16s-%d "
1366 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1367 data->comm, data->pid, data->uid, data->nice,
1368 data->policy, data->rt_priority);
1369 seq_puts(m, " -----------------\n");
1370
1371 if (data->critical_start) {
1372 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001373 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1374 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001375 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001376 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1377 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001378 seq_puts(m, "\n");
1379 }
1380
1381 seq_puts(m, "\n");
1382}
1383
Ingo Molnare309b412008-05-12 21:20:51 +02001384static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001385lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386{
1387 int hardirq, softirq;
1388 char *comm;
1389
1390 comm = trace_find_cmdline(entry->pid);
1391
Steven Rostedt214023c2008-05-12 21:20:46 +02001392 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1393 trace_seq_printf(s, "%d", cpu);
1394 trace_seq_printf(s, "%c%c",
1395 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1396 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001397
1398 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1399 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001400 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001401 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001402 } else {
1403 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001404 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001405 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001406 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001407 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001408 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001409 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001410 }
1411 }
1412
1413 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001414 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001415 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001416 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001417}
1418
1419unsigned long preempt_mark_thresh = 100;
1420
Ingo Molnare309b412008-05-12 21:20:51 +02001421static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001422lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001423 unsigned long rel_usecs)
1424{
Steven Rostedt214023c2008-05-12 21:20:46 +02001425 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001426 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001427 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001428 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001429 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001430 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001431 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001432}
1433
1434static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1435
Ingo Molnare309b412008-05-12 21:20:51 +02001436static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001437print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001438{
Steven Rostedt214023c2008-05-12 21:20:46 +02001439 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001440 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1441 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1442 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1443 struct trace_entry *entry = iter->ent;
1444 unsigned long abs_usecs;
1445 unsigned long rel_usecs;
1446 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001447 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001448 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001449 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001450
1451 if (!next_entry)
1452 next_entry = entry;
1453 rel_usecs = ns2usecs(next_entry->t - entry->t);
1454 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1455
1456 if (verbose) {
1457 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001458 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1459 " %ld.%03ldms (+%ld.%03ldms): ",
1460 comm,
1461 entry->pid, cpu, entry->flags,
1462 entry->preempt_count, trace_idx,
1463 ns2usecs(entry->t),
1464 abs_usecs/1000,
1465 abs_usecs % 1000, rel_usecs/1000,
1466 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001467 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001468 lat_print_generic(s, entry, cpu);
1469 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001470 }
1471 switch (entry->type) {
1472 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001473 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1474 trace_seq_puts(s, " (");
Abhishek Sagar76094a22008-05-28 00:03:18 +05301475 if (kretprobed(entry->fn.parent_ip))
1476 trace_seq_puts(s, KRETPROBE_MSG);
1477 else
1478 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001479 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480 break;
1481 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001482 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001483 T = entry->ctx.next_state < sizeof(state_to_char) ?
1484 state_to_char[entry->ctx.next_state] : 'X';
1485
Ankita Gargd17d9692008-05-12 21:20:58 +02001486 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1487 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001489 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001490 entry->ctx.prev_pid,
1491 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001492 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001493 entry->ctx.next_pid,
1494 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001495 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001496 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001497 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001498 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001499 entry->special.arg1,
1500 entry->special.arg2,
1501 entry->special.arg3);
1502 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001503 case TRACE_STACK:
1504 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1505 if (i)
1506 trace_seq_puts(s, " <= ");
1507 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1508 }
1509 trace_seq_puts(s, "\n");
1510 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001511 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001512 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001513 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001514 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001515}
1516
Ingo Molnare309b412008-05-12 21:20:51 +02001517static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001518{
Steven Rostedt214023c2008-05-12 21:20:46 +02001519 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001520 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001521 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001522 unsigned long usec_rem;
1523 unsigned long long t;
1524 unsigned long secs;
1525 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001526 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001527 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001528 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001529
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001530 entry = iter->ent;
1531
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001532 comm = trace_find_cmdline(iter->ent->pid);
1533
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001534 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535 usec_rem = do_div(t, 1000000ULL);
1536 secs = (unsigned long)t;
1537
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001538 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1539 if (!ret)
1540 return 0;
1541 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1542 if (!ret)
1543 return 0;
1544 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1545 if (!ret)
1546 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547
1548 switch (entry->type) {
1549 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001550 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1551 if (!ret)
1552 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001553 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1554 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001555 ret = trace_seq_printf(s, " <-");
1556 if (!ret)
1557 return 0;
Abhishek Sagar76094a22008-05-28 00:03:18 +05301558 if (kretprobed(entry->fn.parent_ip))
1559 ret = trace_seq_puts(s, KRETPROBE_MSG);
1560 else
1561 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1562 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001563 if (!ret)
1564 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001565 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001566 ret = trace_seq_printf(s, "\n");
1567 if (!ret)
1568 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001569 break;
1570 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001571 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001572 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1573 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001574 T = entry->ctx.next_state < sizeof(state_to_char) ?
1575 state_to_char[entry->ctx.next_state] : 'X';
1576 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001577 entry->ctx.prev_pid,
1578 entry->ctx.prev_prio,
1579 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001580 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001581 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001582 entry->ctx.next_prio,
1583 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001584 if (!ret)
1585 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001586 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001587 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001588 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001589 entry->special.arg1,
1590 entry->special.arg2,
1591 entry->special.arg3);
1592 if (!ret)
1593 return 0;
1594 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001595 case TRACE_STACK:
1596 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1597 if (i) {
1598 ret = trace_seq_puts(s, " <= ");
1599 if (!ret)
1600 return 0;
1601 }
1602 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1603 sym_flags);
1604 if (!ret)
1605 return 0;
1606 }
1607 ret = trace_seq_puts(s, "\n");
1608 if (!ret)
1609 return 0;
1610 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001611 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001612 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001613}
1614
Ingo Molnare309b412008-05-12 21:20:51 +02001615static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001616{
1617 struct trace_seq *s = &iter->seq;
1618 struct trace_entry *entry;
1619 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001620 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001621
1622 entry = iter->ent;
1623
1624 ret = trace_seq_printf(s, "%d %d %llu ",
1625 entry->pid, iter->cpu, entry->t);
1626 if (!ret)
1627 return 0;
1628
1629 switch (entry->type) {
1630 case TRACE_FN:
1631 ret = trace_seq_printf(s, "%x %x\n",
1632 entry->fn.ip, entry->fn.parent_ip);
1633 if (!ret)
1634 return 0;
1635 break;
1636 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001637 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001638 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1639 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001640 T = entry->ctx.next_state < sizeof(state_to_char) ?
1641 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001642 if (entry->type == TRACE_WAKE)
1643 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001644 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001645 entry->ctx.prev_pid,
1646 entry->ctx.prev_prio,
1647 S,
1648 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001649 entry->ctx.next_prio,
1650 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001651 if (!ret)
1652 return 0;
1653 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001654 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001655 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001656 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001657 entry->special.arg1,
1658 entry->special.arg2,
1659 entry->special.arg3);
1660 if (!ret)
1661 return 0;
1662 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001663 }
1664 return 1;
1665}
1666
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001667#define SEQ_PUT_FIELD_RET(s, x) \
1668do { \
1669 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1670 return 0; \
1671} while (0)
1672
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001673#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1674do { \
1675 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1676 return 0; \
1677} while (0)
1678
Ingo Molnare309b412008-05-12 21:20:51 +02001679static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001680{
1681 struct trace_seq *s = &iter->seq;
1682 unsigned char newline = '\n';
1683 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001684 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001685
1686 entry = iter->ent;
1687
1688 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1689 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1690 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1691
1692 switch (entry->type) {
1693 case TRACE_FN:
1694 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1695 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1696 break;
1697 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001698 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001699 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1700 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001701 T = entry->ctx.next_state < sizeof(state_to_char) ?
1702 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001703 if (entry->type == TRACE_WAKE)
1704 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001705 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1706 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1707 SEQ_PUT_HEX_FIELD_RET(s, S);
1708 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1709 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1710 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001711 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001712 break;
1713 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001714 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001715 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1716 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1717 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1718 break;
1719 }
1720 SEQ_PUT_FIELD_RET(s, newline);
1721
1722 return 1;
1723}
1724
Ingo Molnare309b412008-05-12 21:20:51 +02001725static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001726{
1727 struct trace_seq *s = &iter->seq;
1728 struct trace_entry *entry;
1729
1730 entry = iter->ent;
1731
1732 SEQ_PUT_FIELD_RET(s, entry->pid);
1733 SEQ_PUT_FIELD_RET(s, entry->cpu);
1734 SEQ_PUT_FIELD_RET(s, entry->t);
1735
1736 switch (entry->type) {
1737 case TRACE_FN:
1738 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1739 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1740 break;
1741 case TRACE_CTX:
1742 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1743 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1744 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1745 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1746 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001747 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001748 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001749 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001750 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001751 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1752 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1753 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1754 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001755 }
1756 return 1;
1757}
1758
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001759static int trace_empty(struct trace_iterator *iter)
1760{
1761 struct trace_array_cpu *data;
1762 int cpu;
1763
Steven Rostedtab464282008-05-12 21:21:00 +02001764 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765 data = iter->tr->data[cpu];
1766
Steven Rostedtb3806b42008-05-12 21:20:46 +02001767 if (head_page(data) && data->trace_idx &&
1768 (data->trace_tail != data->trace_head ||
1769 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001770 return 0;
1771 }
1772 return 1;
1773}
1774
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001775static int print_trace_line(struct trace_iterator *iter)
1776{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001777 if (iter->trace && iter->trace->print_line)
1778 return iter->trace->print_line(iter);
1779
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001780 if (trace_flags & TRACE_ITER_BIN)
1781 return print_bin_fmt(iter);
1782
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001783 if (trace_flags & TRACE_ITER_HEX)
1784 return print_hex_fmt(iter);
1785
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001786 if (trace_flags & TRACE_ITER_RAW)
1787 return print_raw_fmt(iter);
1788
1789 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1790 return print_lat_fmt(iter, iter->idx, iter->cpu);
1791
1792 return print_trace_fmt(iter);
1793}
1794
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001795static int s_show(struct seq_file *m, void *v)
1796{
1797 struct trace_iterator *iter = v;
1798
1799 if (iter->ent == NULL) {
1800 if (iter->tr) {
1801 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1802 seq_puts(m, "#\n");
1803 }
1804 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1805 /* print nothing if the buffers are empty */
1806 if (trace_empty(iter))
1807 return 0;
1808 print_trace_header(m, iter);
1809 if (!(trace_flags & TRACE_ITER_VERBOSE))
1810 print_lat_help_header(m);
1811 } else {
1812 if (!(trace_flags & TRACE_ITER_VERBOSE))
1813 print_func_help_header(m);
1814 }
1815 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001816 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001817 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001818 }
1819
1820 return 0;
1821}
1822
1823static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001824 .start = s_start,
1825 .next = s_next,
1826 .stop = s_stop,
1827 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001828};
1829
Ingo Molnare309b412008-05-12 21:20:51 +02001830static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001831__tracing_open(struct inode *inode, struct file *file, int *ret)
1832{
1833 struct trace_iterator *iter;
1834
Steven Rostedt60a11772008-05-12 21:20:44 +02001835 if (tracing_disabled) {
1836 *ret = -ENODEV;
1837 return NULL;
1838 }
1839
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001840 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1841 if (!iter) {
1842 *ret = -ENOMEM;
1843 goto out;
1844 }
1845
1846 mutex_lock(&trace_types_lock);
1847 if (current_trace && current_trace->print_max)
1848 iter->tr = &max_tr;
1849 else
1850 iter->tr = inode->i_private;
1851 iter->trace = current_trace;
1852 iter->pos = -1;
1853
1854 /* TODO stop tracer */
1855 *ret = seq_open(file, &tracer_seq_ops);
1856 if (!*ret) {
1857 struct seq_file *m = file->private_data;
1858 m->private = iter;
1859
1860 /* stop the trace while dumping */
Steven Rostedt60bc0802008-07-10 20:58:16 -04001861 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001862 tracer_enabled = 0;
Steven Rostedt60bc0802008-07-10 20:58:16 -04001863 ftrace_function_enabled = 0;
1864 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001865
1866 if (iter->trace && iter->trace->open)
1867 iter->trace->open(iter);
1868 } else {
1869 kfree(iter);
1870 iter = NULL;
1871 }
1872 mutex_unlock(&trace_types_lock);
1873
1874 out:
1875 return iter;
1876}
1877
1878int tracing_open_generic(struct inode *inode, struct file *filp)
1879{
Steven Rostedt60a11772008-05-12 21:20:44 +02001880 if (tracing_disabled)
1881 return -ENODEV;
1882
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001883 filp->private_data = inode->i_private;
1884 return 0;
1885}
1886
1887int tracing_release(struct inode *inode, struct file *file)
1888{
1889 struct seq_file *m = (struct seq_file *)file->private_data;
1890 struct trace_iterator *iter = m->private;
1891
1892 mutex_lock(&trace_types_lock);
1893 if (iter->trace && iter->trace->close)
1894 iter->trace->close(iter);
1895
1896 /* reenable tracing if it was previously enabled */
Steven Rostedt60bc0802008-07-10 20:58:16 -04001897 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001898 tracer_enabled = 1;
Steven Rostedt60bc0802008-07-10 20:58:16 -04001899 /*
1900 * It is safe to enable function tracing even if it
1901 * isn't used
1902 */
1903 ftrace_function_enabled = 1;
1904 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001905 mutex_unlock(&trace_types_lock);
1906
1907 seq_release(inode, file);
1908 kfree(iter);
1909 return 0;
1910}
1911
1912static int tracing_open(struct inode *inode, struct file *file)
1913{
1914 int ret;
1915
1916 __tracing_open(inode, file, &ret);
1917
1918 return ret;
1919}
1920
1921static int tracing_lt_open(struct inode *inode, struct file *file)
1922{
1923 struct trace_iterator *iter;
1924 int ret;
1925
1926 iter = __tracing_open(inode, file, &ret);
1927
1928 if (!ret)
1929 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1930
1931 return ret;
1932}
1933
1934
Ingo Molnare309b412008-05-12 21:20:51 +02001935static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001936t_next(struct seq_file *m, void *v, loff_t *pos)
1937{
1938 struct tracer *t = m->private;
1939
1940 (*pos)++;
1941
1942 if (t)
1943 t = t->next;
1944
1945 m->private = t;
1946
1947 return t;
1948}
1949
1950static void *t_start(struct seq_file *m, loff_t *pos)
1951{
1952 struct tracer *t = m->private;
1953 loff_t l = 0;
1954
1955 mutex_lock(&trace_types_lock);
1956 for (; t && l < *pos; t = t_next(m, t, &l))
1957 ;
1958
1959 return t;
1960}
1961
1962static void t_stop(struct seq_file *m, void *p)
1963{
1964 mutex_unlock(&trace_types_lock);
1965}
1966
1967static int t_show(struct seq_file *m, void *v)
1968{
1969 struct tracer *t = v;
1970
1971 if (!t)
1972 return 0;
1973
1974 seq_printf(m, "%s", t->name);
1975 if (t->next)
1976 seq_putc(m, ' ');
1977 else
1978 seq_putc(m, '\n');
1979
1980 return 0;
1981}
1982
1983static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001984 .start = t_start,
1985 .next = t_next,
1986 .stop = t_stop,
1987 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001988};
1989
1990static int show_traces_open(struct inode *inode, struct file *file)
1991{
1992 int ret;
1993
Steven Rostedt60a11772008-05-12 21:20:44 +02001994 if (tracing_disabled)
1995 return -ENODEV;
1996
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001997 ret = seq_open(file, &show_traces_seq_ops);
1998 if (!ret) {
1999 struct seq_file *m = file->private_data;
2000 m->private = trace_types;
2001 }
2002
2003 return ret;
2004}
2005
2006static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002007 .open = tracing_open,
2008 .read = seq_read,
2009 .llseek = seq_lseek,
2010 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002011};
2012
2013static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002014 .open = tracing_lt_open,
2015 .read = seq_read,
2016 .llseek = seq_lseek,
2017 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002018};
2019
2020static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002021 .open = show_traces_open,
2022 .read = seq_read,
2023 .release = seq_release,
2024};
2025
Ingo Molnar36dfe922008-05-12 21:20:52 +02002026/*
2027 * Only trace on a CPU if the bitmask is set:
2028 */
2029static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2030
2031/*
2032 * When tracing/tracing_cpu_mask is modified then this holds
2033 * the new bitmask we are about to install:
2034 */
2035static cpumask_t tracing_cpumask_new;
2036
2037/*
2038 * The tracer itself will not take this lock, but still we want
2039 * to provide a consistent cpumask to user-space:
2040 */
2041static DEFINE_MUTEX(tracing_cpumask_update_lock);
2042
2043/*
2044 * Temporary storage for the character representation of the
2045 * CPU bitmask (and one more byte for the newline):
2046 */
2047static char mask_str[NR_CPUS + 1];
2048
Ingo Molnarc7078de2008-05-12 21:20:52 +02002049static ssize_t
2050tracing_cpumask_read(struct file *filp, char __user *ubuf,
2051 size_t count, loff_t *ppos)
2052{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002053 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002054
2055 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002056
2057 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2058 if (count - len < 2) {
2059 count = -EINVAL;
2060 goto out_err;
2061 }
2062 len += sprintf(mask_str + len, "\n");
2063 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2064
2065out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002066 mutex_unlock(&tracing_cpumask_update_lock);
2067
2068 return count;
2069}
2070
2071static ssize_t
2072tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2073 size_t count, loff_t *ppos)
2074{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002075 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002076
2077 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002078 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2079 if (err)
2080 goto err_unlock;
2081
Steven Rostedt92205c22008-05-12 21:20:55 +02002082 raw_local_irq_disable();
2083 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002084 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002085 /*
2086 * Increase/decrease the disabled counter if we are
2087 * about to flip a bit in the cpumask:
2088 */
2089 if (cpu_isset(cpu, tracing_cpumask) &&
2090 !cpu_isset(cpu, tracing_cpumask_new)) {
2091 atomic_inc(&global_trace.data[cpu]->disabled);
2092 }
2093 if (!cpu_isset(cpu, tracing_cpumask) &&
2094 cpu_isset(cpu, tracing_cpumask_new)) {
2095 atomic_dec(&global_trace.data[cpu]->disabled);
2096 }
2097 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002098 __raw_spin_unlock(&ftrace_max_lock);
2099 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002100
2101 tracing_cpumask = tracing_cpumask_new;
2102
Ingo Molnarc7078de2008-05-12 21:20:52 +02002103 mutex_unlock(&tracing_cpumask_update_lock);
2104
Ingo Molnarc7078de2008-05-12 21:20:52 +02002105 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002106
2107err_unlock:
2108 mutex_unlock(&tracing_cpumask_update_lock);
2109
2110 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002111}
2112
2113static struct file_operations tracing_cpumask_fops = {
2114 .open = tracing_open_generic,
2115 .read = tracing_cpumask_read,
2116 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002117};
2118
2119static ssize_t
2120tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2121 size_t cnt, loff_t *ppos)
2122{
2123 char *buf;
2124 int r = 0;
2125 int len = 0;
2126 int i;
2127
2128 /* calulate max size */
2129 for (i = 0; trace_options[i]; i++) {
2130 len += strlen(trace_options[i]);
2131 len += 3; /* "no" and space */
2132 }
2133
2134 /* +2 for \n and \0 */
2135 buf = kmalloc(len + 2, GFP_KERNEL);
2136 if (!buf)
2137 return -ENOMEM;
2138
2139 for (i = 0; trace_options[i]; i++) {
2140 if (trace_flags & (1 << i))
2141 r += sprintf(buf + r, "%s ", trace_options[i]);
2142 else
2143 r += sprintf(buf + r, "no%s ", trace_options[i]);
2144 }
2145
2146 r += sprintf(buf + r, "\n");
2147 WARN_ON(r >= len + 2);
2148
Ingo Molnar36dfe922008-05-12 21:20:52 +02002149 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002150
2151 kfree(buf);
2152
2153 return r;
2154}
2155
2156static ssize_t
2157tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2158 size_t cnt, loff_t *ppos)
2159{
2160 char buf[64];
2161 char *cmp = buf;
2162 int neg = 0;
2163 int i;
2164
Steven Rostedtcffae432008-05-12 21:21:00 +02002165 if (cnt >= sizeof(buf))
2166 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002167
2168 if (copy_from_user(&buf, ubuf, cnt))
2169 return -EFAULT;
2170
2171 buf[cnt] = 0;
2172
2173 if (strncmp(buf, "no", 2) == 0) {
2174 neg = 1;
2175 cmp += 2;
2176 }
2177
2178 for (i = 0; trace_options[i]; i++) {
2179 int len = strlen(trace_options[i]);
2180
2181 if (strncmp(cmp, trace_options[i], len) == 0) {
2182 if (neg)
2183 trace_flags &= ~(1 << i);
2184 else
2185 trace_flags |= (1 << i);
2186 break;
2187 }
2188 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002189 /*
2190 * If no option could be set, return an error:
2191 */
2192 if (!trace_options[i])
2193 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002194
2195 filp->f_pos += cnt;
2196
2197 return cnt;
2198}
2199
2200static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002201 .open = tracing_open_generic,
2202 .read = tracing_iter_ctrl_read,
2203 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002204};
2205
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002206static const char readme_msg[] =
2207 "tracing mini-HOWTO:\n\n"
2208 "# mkdir /debug\n"
2209 "# mount -t debugfs nodev /debug\n\n"
2210 "# cat /debug/tracing/available_tracers\n"
2211 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2212 "# cat /debug/tracing/current_tracer\n"
2213 "none\n"
2214 "# echo sched_switch > /debug/tracing/current_tracer\n"
2215 "# cat /debug/tracing/current_tracer\n"
2216 "sched_switch\n"
2217 "# cat /debug/tracing/iter_ctrl\n"
2218 "noprint-parent nosym-offset nosym-addr noverbose\n"
2219 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2220 "# echo 1 > /debug/tracing/tracing_enabled\n"
2221 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2222 "echo 0 > /debug/tracing/tracing_enabled\n"
2223;
2224
2225static ssize_t
2226tracing_readme_read(struct file *filp, char __user *ubuf,
2227 size_t cnt, loff_t *ppos)
2228{
2229 return simple_read_from_buffer(ubuf, cnt, ppos,
2230 readme_msg, strlen(readme_msg));
2231}
2232
2233static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002234 .open = tracing_open_generic,
2235 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002236};
2237
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002238static ssize_t
2239tracing_ctrl_read(struct file *filp, char __user *ubuf,
2240 size_t cnt, loff_t *ppos)
2241{
2242 struct trace_array *tr = filp->private_data;
2243 char buf[64];
2244 int r;
2245
2246 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002247 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002248}
2249
2250static ssize_t
2251tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2252 size_t cnt, loff_t *ppos)
2253{
2254 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002256 long val;
2257 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002258
Steven Rostedtcffae432008-05-12 21:21:00 +02002259 if (cnt >= sizeof(buf))
2260 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002261
2262 if (copy_from_user(&buf, ubuf, cnt))
2263 return -EFAULT;
2264
2265 buf[cnt] = 0;
2266
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002267 ret = strict_strtoul(buf, 10, &val);
2268 if (ret < 0)
2269 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270
2271 val = !!val;
2272
2273 mutex_lock(&trace_types_lock);
2274 if (tr->ctrl ^ val) {
2275 if (val)
2276 tracer_enabled = 1;
2277 else
2278 tracer_enabled = 0;
2279
2280 tr->ctrl = val;
2281
2282 if (current_trace && current_trace->ctrl_update)
2283 current_trace->ctrl_update(tr);
2284 }
2285 mutex_unlock(&trace_types_lock);
2286
2287 filp->f_pos += cnt;
2288
2289 return cnt;
2290}
2291
2292static ssize_t
2293tracing_set_trace_read(struct file *filp, char __user *ubuf,
2294 size_t cnt, loff_t *ppos)
2295{
2296 char buf[max_tracer_type_len+2];
2297 int r;
2298
2299 mutex_lock(&trace_types_lock);
2300 if (current_trace)
2301 r = sprintf(buf, "%s\n", current_trace->name);
2302 else
2303 r = sprintf(buf, "\n");
2304 mutex_unlock(&trace_types_lock);
2305
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002306 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002307}
2308
2309static ssize_t
2310tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2311 size_t cnt, loff_t *ppos)
2312{
2313 struct trace_array *tr = &global_trace;
2314 struct tracer *t;
2315 char buf[max_tracer_type_len+1];
2316 int i;
2317
2318 if (cnt > max_tracer_type_len)
2319 cnt = max_tracer_type_len;
2320
2321 if (copy_from_user(&buf, ubuf, cnt))
2322 return -EFAULT;
2323
2324 buf[cnt] = 0;
2325
2326 /* strip ending whitespace. */
2327 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2328 buf[i] = 0;
2329
2330 mutex_lock(&trace_types_lock);
2331 for (t = trace_types; t; t = t->next) {
2332 if (strcmp(t->name, buf) == 0)
2333 break;
2334 }
2335 if (!t || t == current_trace)
2336 goto out;
2337
2338 if (current_trace && current_trace->reset)
2339 current_trace->reset(tr);
2340
2341 current_trace = t;
2342 if (t->init)
2343 t->init(tr);
2344
2345 out:
2346 mutex_unlock(&trace_types_lock);
2347
2348 filp->f_pos += cnt;
2349
2350 return cnt;
2351}
2352
2353static ssize_t
2354tracing_max_lat_read(struct file *filp, char __user *ubuf,
2355 size_t cnt, loff_t *ppos)
2356{
2357 unsigned long *ptr = filp->private_data;
2358 char buf[64];
2359 int r;
2360
Steven Rostedtcffae432008-05-12 21:21:00 +02002361 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002362 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002363 if (r > sizeof(buf))
2364 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002365 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002366}
2367
2368static ssize_t
2369tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2370 size_t cnt, loff_t *ppos)
2371{
2372 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002373 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002374 long val;
2375 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002376
Steven Rostedtcffae432008-05-12 21:21:00 +02002377 if (cnt >= sizeof(buf))
2378 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002379
2380 if (copy_from_user(&buf, ubuf, cnt))
2381 return -EFAULT;
2382
2383 buf[cnt] = 0;
2384
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002385 ret = strict_strtoul(buf, 10, &val);
2386 if (ret < 0)
2387 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002388
2389 *ptr = val * 1000;
2390
2391 return cnt;
2392}
2393
Steven Rostedtb3806b42008-05-12 21:20:46 +02002394static atomic_t tracing_reader;
2395
2396static int tracing_open_pipe(struct inode *inode, struct file *filp)
2397{
2398 struct trace_iterator *iter;
2399
2400 if (tracing_disabled)
2401 return -ENODEV;
2402
2403 /* We only allow for reader of the pipe */
2404 if (atomic_inc_return(&tracing_reader) != 1) {
2405 atomic_dec(&tracing_reader);
2406 return -EBUSY;
2407 }
2408
2409 /* create a buffer to store the information to pass to userspace */
2410 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2411 if (!iter)
2412 return -ENOMEM;
2413
Steven Rostedt107bad82008-05-12 21:21:01 +02002414 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002415 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002416 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002417 filp->private_data = iter;
2418
Steven Rostedt107bad82008-05-12 21:21:01 +02002419 if (iter->trace->pipe_open)
2420 iter->trace->pipe_open(iter);
2421 mutex_unlock(&trace_types_lock);
2422
Steven Rostedtb3806b42008-05-12 21:20:46 +02002423 return 0;
2424}
2425
2426static int tracing_release_pipe(struct inode *inode, struct file *file)
2427{
2428 struct trace_iterator *iter = file->private_data;
2429
2430 kfree(iter);
2431 atomic_dec(&tracing_reader);
2432
2433 return 0;
2434}
2435
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002436static unsigned int
2437tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2438{
2439 struct trace_iterator *iter = filp->private_data;
2440
2441 if (trace_flags & TRACE_ITER_BLOCK) {
2442 /*
2443 * Always select as readable when in blocking mode
2444 */
2445 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002446 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002447 if (!trace_empty(iter))
2448 return POLLIN | POLLRDNORM;
2449 poll_wait(filp, &trace_wait, poll_table);
2450 if (!trace_empty(iter))
2451 return POLLIN | POLLRDNORM;
2452
2453 return 0;
2454 }
2455}
2456
Steven Rostedtb3806b42008-05-12 21:20:46 +02002457/*
2458 * Consumer reader.
2459 */
2460static ssize_t
2461tracing_read_pipe(struct file *filp, char __user *ubuf,
2462 size_t cnt, loff_t *ppos)
2463{
2464 struct trace_iterator *iter = filp->private_data;
2465 struct trace_array_cpu *data;
2466 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002467 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002468#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002469 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002470#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002471 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002472 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002473
2474 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002475 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2476 if (sret != -EBUSY)
2477 return sret;
2478 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002479
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002480 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002481
Steven Rostedt107bad82008-05-12 21:21:01 +02002482 mutex_lock(&trace_types_lock);
2483 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002484 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2485 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002486 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002487 }
2488
Steven Rostedtb3806b42008-05-12 21:20:46 +02002489 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002490
Steven Rostedt107bad82008-05-12 21:21:01 +02002491 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002492 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002493 goto out;
2494 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002495
Steven Rostedtb3806b42008-05-12 21:20:46 +02002496 /*
2497 * This is a make-shift waitqueue. The reason we don't use
2498 * an actual wait queue is because:
2499 * 1) we only ever have one waiter
2500 * 2) the tracing, traces all functions, we don't want
2501 * the overhead of calling wake_up and friends
2502 * (and tracing them too)
2503 * Anyway, this is really very primitive wakeup.
2504 */
2505 set_current_state(TASK_INTERRUPTIBLE);
2506 iter->tr->waiter = current;
2507
Steven Rostedt107bad82008-05-12 21:21:01 +02002508 mutex_unlock(&trace_types_lock);
2509
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002510 /* sleep for 100 msecs, and try again. */
2511 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002512
Steven Rostedt107bad82008-05-12 21:21:01 +02002513 mutex_lock(&trace_types_lock);
2514
Steven Rostedtb3806b42008-05-12 21:20:46 +02002515 iter->tr->waiter = NULL;
2516
Steven Rostedt107bad82008-05-12 21:21:01 +02002517 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002518 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002519 goto out;
2520 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002521
Steven Rostedt84527992008-05-12 21:20:58 +02002522 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002523 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002524
Steven Rostedtb3806b42008-05-12 21:20:46 +02002525 /*
2526 * We block until we read something and tracing is disabled.
2527 * We still block if tracing is disabled, but we have never
2528 * read anything. This allows a user to cat this file, and
2529 * then enable tracing. But after we have read something,
2530 * we give an EOF when tracing is again disabled.
2531 *
2532 * iter->pos will be 0 if we haven't read anything.
2533 */
2534 if (!tracer_enabled && iter->pos)
2535 break;
2536
2537 continue;
2538 }
2539
2540 /* stop when tracing is finished */
2541 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002542 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002543
2544 if (cnt >= PAGE_SIZE)
2545 cnt = PAGE_SIZE - 1;
2546
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002547 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002548 memset(&iter->seq, 0,
2549 sizeof(struct trace_iterator) -
2550 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002551 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002552
2553 /*
2554 * We need to stop all tracing on all CPUS to read the
2555 * the next buffer. This is a bit expensive, but is
2556 * not done often. We fill all what we can read,
2557 * and then release the locks again.
2558 */
2559
2560 cpus_clear(mask);
2561 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002562#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002563 ftrace_save = ftrace_enabled;
2564 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002565#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002566 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002567 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002568 data = iter->tr->data[cpu];
2569
2570 if (!head_page(data) || !data->trace_idx)
2571 continue;
2572
2573 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002574 cpu_set(cpu, mask);
2575 }
2576
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002577 for_each_cpu_mask(cpu, mask) {
2578 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002579 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002580
2581 if (data->overrun > iter->last_overrun[cpu])
2582 iter->overrun[cpu] +=
2583 data->overrun - iter->last_overrun[cpu];
2584 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002585 }
2586
Steven Rostedt088b1e422008-05-12 21:20:48 +02002587 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002588 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002589 int len = iter->seq.len;
2590
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002591 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002592 if (!ret) {
2593 /* don't print partial lines */
2594 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002595 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002596 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002597
2598 trace_consume(iter);
2599
2600 if (iter->seq.len >= cnt)
2601 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002602 }
2603
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002604 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002605 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002606 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002607 }
2608
2609 for_each_cpu_mask(cpu, mask) {
2610 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002611 atomic_dec(&data->disabled);
2612 }
Ingo Molnar25770462008-05-12 21:20:49 +02002613#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002614 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002615#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002616 local_irq_restore(flags);
2617
2618 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002619 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2620 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002621 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002622 if (sret == -EBUSY)
2623 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002624
Steven Rostedt107bad82008-05-12 21:21:01 +02002625out:
2626 mutex_unlock(&trace_types_lock);
2627
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002628 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002629}
2630
Steven Rostedta98a3c32008-05-12 21:20:59 +02002631static ssize_t
2632tracing_entries_read(struct file *filp, char __user *ubuf,
2633 size_t cnt, loff_t *ppos)
2634{
2635 struct trace_array *tr = filp->private_data;
2636 char buf[64];
2637 int r;
2638
2639 r = sprintf(buf, "%lu\n", tr->entries);
2640 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2641}
2642
2643static ssize_t
2644tracing_entries_write(struct file *filp, const char __user *ubuf,
2645 size_t cnt, loff_t *ppos)
2646{
2647 unsigned long val;
2648 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002649 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002650
Steven Rostedtcffae432008-05-12 21:21:00 +02002651 if (cnt >= sizeof(buf))
2652 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002653
2654 if (copy_from_user(&buf, ubuf, cnt))
2655 return -EFAULT;
2656
2657 buf[cnt] = 0;
2658
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002659 ret = strict_strtoul(buf, 10, &val);
2660 if (ret < 0)
2661 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002662
2663 /* must have at least 1 entry */
2664 if (!val)
2665 return -EINVAL;
2666
2667 mutex_lock(&trace_types_lock);
2668
2669 if (current_trace != &no_tracer) {
2670 cnt = -EBUSY;
2671 pr_info("ftrace: set current_tracer to none"
2672 " before modifying buffer size\n");
2673 goto out;
2674 }
2675
2676 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002677 long pages_requested;
2678 unsigned long freeable_pages;
2679
2680 /* make sure we have enough memory before mapping */
2681 pages_requested =
2682 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2683
2684 /* account for each buffer (and max_tr) */
2685 pages_requested *= tracing_nr_buffers * 2;
2686
2687 /* Check for overflow */
2688 if (pages_requested < 0) {
2689 cnt = -ENOMEM;
2690 goto out;
2691 }
2692
2693 freeable_pages = determine_dirtyable_memory();
2694
2695 /* we only allow to request 1/4 of useable memory */
2696 if (pages_requested >
2697 ((freeable_pages + tracing_pages_allocated) / 4)) {
2698 cnt = -ENOMEM;
2699 goto out;
2700 }
2701
Steven Rostedta98a3c32008-05-12 21:20:59 +02002702 while (global_trace.entries < val) {
2703 if (trace_alloc_page()) {
2704 cnt = -ENOMEM;
2705 goto out;
2706 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002707 /* double check that we don't go over the known pages */
2708 if (tracing_pages_allocated > pages_requested)
2709 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002710 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002711
Steven Rostedta98a3c32008-05-12 21:20:59 +02002712 } else {
2713 /* include the number of entries in val (inc of page entries) */
2714 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2715 trace_free_page();
2716 }
2717
Steven Rostedt19384c02008-05-22 00:22:16 -04002718 /* check integrity */
2719 for_each_tracing_cpu(i)
2720 check_pages(global_trace.data[i]);
2721
Steven Rostedta98a3c32008-05-12 21:20:59 +02002722 filp->f_pos += cnt;
2723
Steven Rostedt19384c02008-05-22 00:22:16 -04002724 /* If check pages failed, return ENOMEM */
2725 if (tracing_disabled)
2726 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002727 out:
2728 max_tr.entries = global_trace.entries;
2729 mutex_unlock(&trace_types_lock);
2730
2731 return cnt;
2732}
2733
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002734static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002735 .open = tracing_open_generic,
2736 .read = tracing_max_lat_read,
2737 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002738};
2739
2740static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002741 .open = tracing_open_generic,
2742 .read = tracing_ctrl_read,
2743 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002744};
2745
2746static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002747 .open = tracing_open_generic,
2748 .read = tracing_set_trace_read,
2749 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002750};
2751
Steven Rostedtb3806b42008-05-12 21:20:46 +02002752static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002753 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002754 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002755 .read = tracing_read_pipe,
2756 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002757};
2758
Steven Rostedta98a3c32008-05-12 21:20:59 +02002759static struct file_operations tracing_entries_fops = {
2760 .open = tracing_open_generic,
2761 .read = tracing_entries_read,
2762 .write = tracing_entries_write,
2763};
2764
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002765#ifdef CONFIG_DYNAMIC_FTRACE
2766
2767static ssize_t
2768tracing_read_long(struct file *filp, char __user *ubuf,
2769 size_t cnt, loff_t *ppos)
2770{
2771 unsigned long *p = filp->private_data;
2772 char buf[64];
2773 int r;
2774
2775 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002776
2777 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002778}
2779
2780static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002781 .open = tracing_open_generic,
2782 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002783};
2784#endif
2785
2786static struct dentry *d_tracer;
2787
2788struct dentry *tracing_init_dentry(void)
2789{
2790 static int once;
2791
2792 if (d_tracer)
2793 return d_tracer;
2794
2795 d_tracer = debugfs_create_dir("tracing", NULL);
2796
2797 if (!d_tracer && !once) {
2798 once = 1;
2799 pr_warning("Could not create debugfs directory 'tracing'\n");
2800 return NULL;
2801 }
2802
2803 return d_tracer;
2804}
2805
Steven Rostedt60a11772008-05-12 21:20:44 +02002806#ifdef CONFIG_FTRACE_SELFTEST
2807/* Let selftest have access to static functions in this file */
2808#include "trace_selftest.c"
2809#endif
2810
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002811static __init void tracer_init_debugfs(void)
2812{
2813 struct dentry *d_tracer;
2814 struct dentry *entry;
2815
2816 d_tracer = tracing_init_dentry();
2817
2818 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2819 &global_trace, &tracing_ctrl_fops);
2820 if (!entry)
2821 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2822
2823 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2824 NULL, &tracing_iter_fops);
2825 if (!entry)
2826 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2827
Ingo Molnarc7078de2008-05-12 21:20:52 +02002828 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2829 NULL, &tracing_cpumask_fops);
2830 if (!entry)
2831 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2832
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002833 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2834 &global_trace, &tracing_lt_fops);
2835 if (!entry)
2836 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2837
2838 entry = debugfs_create_file("trace", 0444, d_tracer,
2839 &global_trace, &tracing_fops);
2840 if (!entry)
2841 pr_warning("Could not create debugfs 'trace' entry\n");
2842
2843 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2844 &global_trace, &show_traces_fops);
2845 if (!entry)
2846 pr_warning("Could not create debugfs 'trace' entry\n");
2847
2848 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2849 &global_trace, &set_tracer_fops);
2850 if (!entry)
2851 pr_warning("Could not create debugfs 'trace' entry\n");
2852
2853 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2854 &tracing_max_latency,
2855 &tracing_max_lat_fops);
2856 if (!entry)
2857 pr_warning("Could not create debugfs "
2858 "'tracing_max_latency' entry\n");
2859
2860 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2861 &tracing_thresh, &tracing_max_lat_fops);
2862 if (!entry)
2863 pr_warning("Could not create debugfs "
2864 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002865 entry = debugfs_create_file("README", 0644, d_tracer,
2866 NULL, &tracing_readme_fops);
2867 if (!entry)
2868 pr_warning("Could not create debugfs 'README' entry\n");
2869
Steven Rostedtb3806b42008-05-12 21:20:46 +02002870 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2871 NULL, &tracing_pipe_fops);
2872 if (!entry)
2873 pr_warning("Could not create debugfs "
2874 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002875
Steven Rostedta98a3c32008-05-12 21:20:59 +02002876 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2877 &global_trace, &tracing_entries_fops);
2878 if (!entry)
2879 pr_warning("Could not create debugfs "
2880 "'tracing_threash' entry\n");
2881
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002882#ifdef CONFIG_DYNAMIC_FTRACE
2883 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2884 &ftrace_update_tot_cnt,
2885 &tracing_read_long_fops);
2886 if (!entry)
2887 pr_warning("Could not create debugfs "
2888 "'dyn_ftrace_total_info' entry\n");
2889#endif
2890}
2891
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002892static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002893{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002894 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002895 struct page *page, *tmp;
2896 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002897 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002898 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002899 int i;
2900
2901 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002902 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002903 array = (void *)__get_free_page(GFP_KERNEL);
2904 if (array == NULL) {
2905 printk(KERN_ERR "tracer: failed to allocate page"
2906 "for trace buffer!\n");
2907 goto free_pages;
2908 }
2909
Steven Rostedt3eefae92008-05-12 21:21:04 +02002910 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002911 page = virt_to_page(array);
2912 list_add(&page->lru, &pages);
2913
2914/* Only allocate if we are actually using the max trace */
2915#ifdef CONFIG_TRACER_MAX_TRACE
2916 array = (void *)__get_free_page(GFP_KERNEL);
2917 if (array == NULL) {
2918 printk(KERN_ERR "tracer: failed to allocate page"
2919 "for trace buffer!\n");
2920 goto free_pages;
2921 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002922 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002923 page = virt_to_page(array);
2924 list_add(&page->lru, &pages);
2925#endif
2926 }
2927
2928 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002929 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002930 data = global_trace.data[i];
2931 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002932 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002933 list_add_tail(&page->lru, &data->trace_pages);
2934 ClearPageLRU(page);
2935
2936#ifdef CONFIG_TRACER_MAX_TRACE
2937 data = max_tr.data[i];
2938 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002939 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002940 list_add_tail(&page->lru, &data->trace_pages);
2941 SetPageLRU(page);
2942#endif
2943 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002944 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002945 global_trace.entries += ENTRIES_PER_PAGE;
2946
2947 return 0;
2948
2949 free_pages:
2950 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002951 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002952 __free_page(page);
2953 }
2954 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002955}
2956
Steven Rostedta98a3c32008-05-12 21:20:59 +02002957static int trace_free_page(void)
2958{
2959 struct trace_array_cpu *data;
2960 struct page *page;
2961 struct list_head *p;
2962 int i;
2963 int ret = 0;
2964
2965 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002966 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002967 data = global_trace.data[i];
2968 p = data->trace_pages.next;
2969 if (p == &data->trace_pages) {
2970 /* should never happen */
2971 WARN_ON(1);
2972 tracing_disabled = 1;
2973 ret = -1;
2974 break;
2975 }
2976 page = list_entry(p, struct page, lru);
2977 ClearPageLRU(page);
2978 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02002979 tracing_pages_allocated--;
2980 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002981 __free_page(page);
2982
2983 tracing_reset(data);
2984
2985#ifdef CONFIG_TRACER_MAX_TRACE
2986 data = max_tr.data[i];
2987 p = data->trace_pages.next;
2988 if (p == &data->trace_pages) {
2989 /* should never happen */
2990 WARN_ON(1);
2991 tracing_disabled = 1;
2992 ret = -1;
2993 break;
2994 }
2995 page = list_entry(p, struct page, lru);
2996 ClearPageLRU(page);
2997 list_del(&page->lru);
2998 __free_page(page);
2999
3000 tracing_reset(data);
3001#endif
3002 }
3003 global_trace.entries -= ENTRIES_PER_PAGE;
3004
3005 return ret;
3006}
3007
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003008__init static int tracer_alloc_buffers(void)
3009{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003010 struct trace_array_cpu *data;
3011 void *array;
3012 struct page *page;
3013 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02003014 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003015 int i;
3016
Steven Rostedtab464282008-05-12 21:21:00 +02003017 /* TODO: make the number of buffers hot pluggable with CPUS */
3018 tracing_nr_buffers = num_possible_cpus();
3019 tracing_buffer_mask = cpu_possible_map;
3020
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003021 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003022 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003023 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003024 max_tr.data[i] = &per_cpu(max_data, i);
3025
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003026 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003027 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003028 printk(KERN_ERR "tracer: failed to allocate page"
3029 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003030 goto free_buffers;
3031 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003032
3033 /* set the array to the list */
3034 INIT_LIST_HEAD(&data->trace_pages);
3035 page = virt_to_page(array);
3036 list_add(&page->lru, &data->trace_pages);
3037 /* use the LRU flag to differentiate the two buffers */
3038 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003039
Steven Rostedta98a3c32008-05-12 21:20:59 +02003040 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3041 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3042
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003043/* Only allocate if we are actually using the max trace */
3044#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003045 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003046 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003047 printk(KERN_ERR "tracer: failed to allocate page"
3048 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003049 goto free_buffers;
3050 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003051
3052 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3053 page = virt_to_page(array);
3054 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3055 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003056#endif
3057 }
3058
3059 /*
3060 * Since we allocate by orders of pages, we may be able to
3061 * round up a bit.
3062 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003063 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003064 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003065
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003066 while (global_trace.entries < trace_nr_entries) {
3067 if (trace_alloc_page())
3068 break;
3069 pages++;
3070 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003071 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003072
Jiri Slaby20764ff2008-06-12 11:27:03 +02003073 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3074 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003075 pr_info(" actual entries %ld\n", global_trace.entries);
3076
3077 tracer_init_debugfs();
3078
3079 trace_init_cmdlines();
3080
3081 register_tracer(&no_tracer);
3082 current_trace = &no_tracer;
3083
Steven Rostedt60a11772008-05-12 21:20:44 +02003084 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003085 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003086 tracing_disabled = 0;
3087
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003088 return 0;
3089
3090 free_buffers:
3091 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003092 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003093 struct trace_array_cpu *data = global_trace.data[i];
3094
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003095 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003096 list_for_each_entry_safe(page, tmp,
3097 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003098 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003099 __free_page(page);
3100 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003101 }
3102
3103#ifdef CONFIG_TRACER_MAX_TRACE
3104 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003105 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003106 list_for_each_entry_safe(page, tmp,
3107 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003108 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003109 __free_page(page);
3110 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003111 }
3112#endif
3113 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003114 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003115}
Steven Rostedt60a11772008-05-12 21:20:44 +02003116fs_initcall(tracer_alloc_buffers);