blob: 6ada059832a6606a8e0ed7012d5d1583b9b11fa8 [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>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040017#include <linux/notifier.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020018#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020019#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020020#include <linux/hardirq.h>
21#include <linux/linkage.h>
22#include <linux/uaccess.h>
23#include <linux/ftrace.h>
24#include <linux/module.h>
25#include <linux/percpu.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040026#include <linux/kdebug.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020027#include <linux/ctype.h>
28#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020029#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020030#include <linux/gfp.h>
31#include <linux/fs.h>
Abhishek Sagar76094a22008-05-28 00:03:18 +053032#include <linux/kprobes.h>
Steven Rostedt3eefae92008-05-12 21:21:04 +020033#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020034
Ingo Molnar86387f72008-05-12 21:20:51 +020035#include <linux/stacktrace.h>
36
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020037#include "trace.h"
38
39unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
40unsigned long __read_mostly tracing_thresh;
41
Steven Rostedtab464282008-05-12 21:21:00 +020042static unsigned long __read_mostly tracing_nr_buffers;
43static cpumask_t __read_mostly tracing_buffer_mask;
44
45#define for_each_tracing_cpu(cpu) \
46 for_each_cpu_mask(cpu, tracing_buffer_mask)
47
Steven Rostedta98a3c32008-05-12 21:20:59 +020048static int trace_alloc_page(void);
49static int trace_free_page(void);
50
Steven Rostedt60a11772008-05-12 21:20:44 +020051static int tracing_disabled = 1;
52
Steven Rostedt3eefae92008-05-12 21:21:04 +020053static unsigned long tracing_pages_allocated;
54
Thomas Gleixner72829bc2008-05-23 21:37:28 +020055long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020056ns2usecs(cycle_t nsec)
57{
58 nsec += 500;
59 do_div(nsec, 1000);
60 return nsec;
61}
62
Ingo Molnare309b412008-05-12 21:20:51 +020063cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020064{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020065 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020066}
67
Steven Rostedt4fcdae82008-05-12 21:21:00 +020068/*
69 * The global_trace is the descriptor that holds the tracing
70 * buffers for the live tracing. For each CPU, it contains
71 * a link list of pages that will store trace entries. The
72 * page descriptor of the pages in the memory is used to hold
73 * the link list by linking the lru item in the page descriptor
74 * to each of the pages in the buffer per CPU.
75 *
76 * For each active CPU there is a data field that holds the
77 * pages for the buffer for that CPU. Each CPU has the same number
78 * of pages allocated for its buffer.
79 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020080static struct trace_array global_trace;
81
82static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
83
Steven Rostedt4fcdae82008-05-12 21:21:00 +020084/*
85 * The max_tr is used to snapshot the global_trace when a maximum
86 * latency is reached. Some tracers will use this to store a maximum
87 * trace while it continues examining live traces.
88 *
89 * The buffers for the max_tr are set up the same as the global_trace.
90 * When a snapshot is taken, the link list of the max_tr is swapped
91 * with the link list of the global_trace and the buffers are reset for
92 * the global_trace so the tracing can continue.
93 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020094static struct trace_array max_tr;
95
96static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
97
Steven Rostedt4fcdae82008-05-12 21:21:00 +020098/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +020099static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200100
Steven Rostedt60bc0802008-07-10 20:58:16 -0400101/* function tracing enabled */
102int ftrace_function_enabled;
103
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200104/*
105 * trace_nr_entries is the number of entries that is allocated
106 * for a buffer. Note, the number of entries is always rounded
107 * to ENTRIES_PER_PAGE.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400108 *
109 * This number is purposely set to a low number of 16384.
110 * If the dump on oops happens, it will be much appreciated
111 * to not have to wait for all that output. Anyway this can be
112 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200113 */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400114#define TRACE_ENTRIES_DEFAULT 16384UL
115
116static unsigned long trace_nr_entries = TRACE_ENTRIES_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200117
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200118/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200119static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200120
121/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200122static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200123
124/*
125 * max_tracer_type_len is used to simplify the allocating of
126 * buffers to read userspace tracer names. We keep track of
127 * the longest tracer name registered.
128 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200129static int max_tracer_type_len;
130
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200131/*
132 * trace_types_lock is used to protect the trace_types list.
133 * This lock is also used to keep user access serialized.
134 * Accesses from userspace will grab this lock while userspace
135 * activities happen inside the kernel.
136 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200137static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200138
139/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200140static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
141
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200142/* trace_flags holds iter_ctrl options */
Ingo Molnar4e655512008-05-12 21:20:52 +0200143unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
144
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200145/**
146 * trace_wake_up - wake up tasks waiting for trace input
147 *
148 * Simply wakes up any task that is blocked on the trace_wait
149 * queue. These is used with trace_poll for tasks polling the trace.
150 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200151void trace_wake_up(void)
152{
Ingo Molnar017730c2008-05-12 21:20:52 +0200153 /*
154 * The runqueue_is_locked() can fail, but this is the best we
155 * have for now:
156 */
157 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200158 wake_up(&trace_wait);
159}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200160
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200161#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
162
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200163static int __init set_nr_entries(char *str)
164{
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200165 unsigned long nr_entries;
166 int ret;
167
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200168 if (!str)
169 return 0;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200170 ret = strict_strtoul(str, 0, &nr_entries);
171 /* nr_entries can not be zero */
172 if (ret < 0 || nr_entries == 0)
173 return 0;
174 trace_nr_entries = nr_entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200175 return 1;
176}
177__setup("trace_entries=", set_nr_entries);
178
Steven Rostedt57f50be2008-05-12 21:20:44 +0200179unsigned long nsecs_to_usecs(unsigned long nsecs)
180{
181 return nsecs / 1000;
182}
183
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200184/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200185 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
186 * control the output of kernel symbols.
187 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200188#define TRACE_ITER_SYM_MASK \
189 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
190
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200191/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200192static const char *trace_options[] = {
193 "print-parent",
194 "sym-offset",
195 "sym-addr",
196 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200197 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200198 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200199 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200200 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200201 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200202 "sched-tree",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200203 "ftrace_printk",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200204 NULL
205};
206
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200207/*
208 * ftrace_max_lock is used to protect the swapping of buffers
209 * when taking a max snapshot. The buffers themselves are
210 * protected by per_cpu spinlocks. But the action of the swap
211 * needs its own lock.
212 *
213 * This is defined as a raw_spinlock_t in order to help
214 * with performance when lockdep debugging is enabled.
215 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200216static raw_spinlock_t ftrace_max_lock =
217 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200218
219/*
220 * Copy the new maximum trace into the separate maximum-trace
221 * structure. (this way the maximum trace is permanently saved,
222 * for later retrieval via /debugfs/tracing/latency_trace)
223 */
Ingo Molnare309b412008-05-12 21:20:51 +0200224static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200225__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
226{
227 struct trace_array_cpu *data = tr->data[cpu];
228
229 max_tr.cpu = cpu;
230 max_tr.time_start = data->preempt_timestamp;
231
232 data = max_tr.data[cpu];
233 data->saved_latency = tracing_max_latency;
234
235 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
236 data->pid = tsk->pid;
237 data->uid = tsk->uid;
238 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
239 data->policy = tsk->policy;
240 data->rt_priority = tsk->rt_priority;
241
242 /* record this tasks comm */
243 tracing_record_cmdline(current);
244}
245
Steven Rostedt19384c02008-05-22 00:22:16 -0400246#define CHECK_COND(cond) \
247 if (unlikely(cond)) { \
248 tracing_disabled = 1; \
249 WARN_ON(1); \
250 return -1; \
251 }
252
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200253/**
254 * check_pages - integrity check of trace buffers
255 *
256 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400257 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200258 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400259int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200260{
261 struct page *page, *tmp;
262
Steven Rostedt19384c02008-05-22 00:22:16 -0400263 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
264 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200265
266 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400267 CHECK_COND(page->lru.next->prev != &page->lru);
268 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200269 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400270
271 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200272}
273
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200274/**
275 * head_page - page address of the first page in per_cpu buffer.
276 *
277 * head_page returns the page address of the first page in
278 * a per_cpu buffer. This also preforms various consistency
279 * checks to make sure the buffer has not been corrupted.
280 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200281void *head_page(struct trace_array_cpu *data)
282{
283 struct page *page;
284
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200285 if (list_empty(&data->trace_pages))
286 return NULL;
287
288 page = list_entry(data->trace_pages.next, struct page, lru);
289 BUG_ON(&page->lru == &data->trace_pages);
290
291 return page_address(page);
292}
293
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200294/**
295 * trace_seq_printf - sequence printing of trace information
296 * @s: trace sequence descriptor
297 * @fmt: printf format string
298 *
299 * The tracer may use either sequence operations or its own
300 * copy to user routines. To simplify formating of a trace
301 * trace_seq_printf is used to store strings into a special
302 * buffer (@s). Then the output may be either used by
303 * the sequencer or pulled into another buffer.
304 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200305int
Steven Rostedt214023c2008-05-12 21:20:46 +0200306trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
307{
308 int len = (PAGE_SIZE - 1) - s->len;
309 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200310 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200311
312 if (!len)
313 return 0;
314
315 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200316 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200317 va_end(ap);
318
Steven Rostedtb3806b42008-05-12 21:20:46 +0200319 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200320 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200321 return 0;
322
323 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200324
325 return len;
326}
327
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200328/**
329 * trace_seq_puts - trace sequence printing of simple string
330 * @s: trace sequence descriptor
331 * @str: simple string to record
332 *
333 * The tracer may use either the sequence operations or its own
334 * copy to user routines. This function records a simple string
335 * into a special buffer (@s) for later retrieval by a sequencer
336 * or other mechanism.
337 */
Ingo Molnare309b412008-05-12 21:20:51 +0200338static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200339trace_seq_puts(struct trace_seq *s, const char *str)
340{
341 int len = strlen(str);
342
343 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200344 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200345
346 memcpy(s->buffer + s->len, str, len);
347 s->len += len;
348
349 return len;
350}
351
Ingo Molnare309b412008-05-12 21:20:51 +0200352static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200353trace_seq_putc(struct trace_seq *s, unsigned char c)
354{
355 if (s->len >= (PAGE_SIZE - 1))
356 return 0;
357
358 s->buffer[s->len++] = c;
359
360 return 1;
361}
362
Ingo Molnare309b412008-05-12 21:20:51 +0200363static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200364trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
365{
366 if (len > ((PAGE_SIZE - 1) - s->len))
367 return 0;
368
369 memcpy(s->buffer + s->len, mem, len);
370 s->len += len;
371
372 return len;
373}
374
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200375#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200376static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200377
Ingo Molnare309b412008-05-12 21:20:51 +0200378static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200379trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
380{
381 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200382 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200383 unsigned char byte;
384 int i, j;
385
386 BUG_ON(len >= HEX_CHARS);
387
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200388#ifdef __BIG_ENDIAN
389 for (i = 0, j = 0; i < len; i++) {
390#else
391 for (i = len-1, j = 0; i >= 0; i--) {
392#endif
393 byte = data[i];
394
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200395 hex[j++] = hex2asc[byte & 0x0f];
396 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200397 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200398 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200399
400 return trace_seq_putmem(s, hex, j);
401}
402
Ingo Molnare309b412008-05-12 21:20:51 +0200403static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200404trace_seq_reset(struct trace_seq *s)
405{
406 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200407 s->readpos = 0;
408}
409
410ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
411{
412 int len;
413 int ret;
414
415 if (s->len <= s->readpos)
416 return -EBUSY;
417
418 len = s->len - s->readpos;
419 if (cnt > len)
420 cnt = len;
421 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
422 if (ret)
423 return -EFAULT;
424
425 s->readpos += len;
426 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200427}
428
Ingo Molnare309b412008-05-12 21:20:51 +0200429static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200430trace_print_seq(struct seq_file *m, struct trace_seq *s)
431{
432 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
433
434 s->buffer[len] = 0;
435 seq_puts(m, s->buffer);
436
437 trace_seq_reset(s);
438}
439
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200440/*
441 * flip the trace buffers between two trace descriptors.
442 * This usually is the buffers between the global_trace and
443 * the max_tr to record a snapshot of a current trace.
444 *
445 * The ftrace_max_lock must be held.
446 */
Ingo Molnare309b412008-05-12 21:20:51 +0200447static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200448flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
449{
450 struct list_head flip_pages;
451
452 INIT_LIST_HEAD(&flip_pages);
453
Steven Rostedt93a588f2008-05-12 21:20:45 +0200454 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200455 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200456 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200457
458 check_pages(tr1);
459 check_pages(tr2);
460 list_splice_init(&tr1->trace_pages, &flip_pages);
461 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
462 list_splice_init(&flip_pages, &tr2->trace_pages);
463 BUG_ON(!list_empty(&flip_pages));
464 check_pages(tr1);
465 check_pages(tr2);
466}
467
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200468/**
469 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
470 * @tr: tracer
471 * @tsk: the task with the latency
472 * @cpu: The cpu that initiated the trace.
473 *
474 * Flip the buffers between the @tr and the max_tr and record information
475 * about which task was the cause of this latency.
476 */
Ingo Molnare309b412008-05-12 21:20:51 +0200477void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200478update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
479{
480 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200481 int i;
482
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200483 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200484 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200485 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200486 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200487 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200488 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200489 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200490 }
491
492 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200493 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200494}
495
496/**
497 * update_max_tr_single - only copy one trace over, and reset the rest
498 * @tr - tracer
499 * @tsk - task with the latency
500 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200501 *
502 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200503 */
Ingo Molnare309b412008-05-12 21:20:51 +0200504void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200505update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
506{
507 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200508 int i;
509
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200510 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200511 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200512 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200513 tracing_reset(max_tr.data[i]);
514
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200515 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200516 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200517
518 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200519 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200520}
521
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200522/**
523 * register_tracer - register a tracer with the ftrace system.
524 * @type - the plugin for the tracer
525 *
526 * Register a new plugin tracer.
527 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200528int register_tracer(struct tracer *type)
529{
530 struct tracer *t;
531 int len;
532 int ret = 0;
533
534 if (!type->name) {
535 pr_info("Tracer must have a name\n");
536 return -1;
537 }
538
539 mutex_lock(&trace_types_lock);
540 for (t = trace_types; t; t = t->next) {
541 if (strcmp(type->name, t->name) == 0) {
542 /* already found */
543 pr_info("Trace %s already registered\n",
544 type->name);
545 ret = -1;
546 goto out;
547 }
548 }
549
Steven Rostedt60a11772008-05-12 21:20:44 +0200550#ifdef CONFIG_FTRACE_STARTUP_TEST
551 if (type->selftest) {
552 struct tracer *saved_tracer = current_trace;
553 struct trace_array_cpu *data;
554 struct trace_array *tr = &global_trace;
555 int saved_ctrl = tr->ctrl;
556 int i;
557 /*
558 * Run a selftest on this tracer.
559 * Here we reset the trace buffer, and set the current
560 * tracer to be this tracer. The tracer can then run some
561 * internal tracing to verify that everything is in order.
562 * If we fail, we do not register this tracer.
563 */
Steven Rostedtab464282008-05-12 21:21:00 +0200564 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200565 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200566 if (!head_page(data))
567 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200568 tracing_reset(data);
569 }
570 current_trace = type;
571 tr->ctrl = 0;
572 /* the test is responsible for initializing and enabling */
573 pr_info("Testing tracer %s: ", type->name);
574 ret = type->selftest(type, tr);
575 /* the test is responsible for resetting too */
576 current_trace = saved_tracer;
577 tr->ctrl = saved_ctrl;
578 if (ret) {
579 printk(KERN_CONT "FAILED!\n");
580 goto out;
581 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200582 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200583 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200584 data = tr->data[i];
585 if (!head_page(data))
586 continue;
587 tracing_reset(data);
588 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200589 printk(KERN_CONT "PASSED\n");
590 }
591#endif
592
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200593 type->next = trace_types;
594 trace_types = type;
595 len = strlen(type->name);
596 if (len > max_tracer_type_len)
597 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200598
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200599 out:
600 mutex_unlock(&trace_types_lock);
601
602 return ret;
603}
604
605void unregister_tracer(struct tracer *type)
606{
607 struct tracer **t;
608 int len;
609
610 mutex_lock(&trace_types_lock);
611 for (t = &trace_types; *t; t = &(*t)->next) {
612 if (*t == type)
613 goto found;
614 }
615 pr_info("Trace %s not registered\n", type->name);
616 goto out;
617
618 found:
619 *t = (*t)->next;
620 if (strlen(type->name) != max_tracer_type_len)
621 goto out;
622
623 max_tracer_type_len = 0;
624 for (t = &trace_types; *t; t = &(*t)->next) {
625 len = strlen((*t)->name);
626 if (len > max_tracer_type_len)
627 max_tracer_type_len = len;
628 }
629 out:
630 mutex_unlock(&trace_types_lock);
631}
632
Ingo Molnare309b412008-05-12 21:20:51 +0200633void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200634{
635 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200636 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200637 data->trace_head = data->trace_tail = head_page(data);
638 data->trace_head_idx = 0;
639 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200640}
641
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200642#define SAVED_CMDLINES 128
643static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
644static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
645static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
646static int cmdline_idx;
647static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200648
Steven Rostedt25b0b442008-05-12 21:21:00 +0200649/* temporary disable recording */
650atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200651
652static void trace_init_cmdlines(void)
653{
654 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
655 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
656 cmdline_idx = 0;
657}
658
Ingo Molnare309b412008-05-12 21:20:51 +0200659void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200660
Ingo Molnare309b412008-05-12 21:20:51 +0200661static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200662{
663 unsigned map;
664 unsigned idx;
665
666 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
667 return;
668
669 /*
670 * It's not the end of the world if we don't get
671 * the lock, but we also don't want to spin
672 * nor do we want to disable interrupts,
673 * so if we miss here, then better luck next time.
674 */
675 if (!spin_trylock(&trace_cmdline_lock))
676 return;
677
678 idx = map_pid_to_cmdline[tsk->pid];
679 if (idx >= SAVED_CMDLINES) {
680 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
681
682 map = map_cmdline_to_pid[idx];
683 if (map <= PID_MAX_DEFAULT)
684 map_pid_to_cmdline[map] = (unsigned)-1;
685
686 map_pid_to_cmdline[tsk->pid] = idx;
687
688 cmdline_idx = idx;
689 }
690
691 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
692
693 spin_unlock(&trace_cmdline_lock);
694}
695
Ingo Molnare309b412008-05-12 21:20:51 +0200696static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200697{
698 char *cmdline = "<...>";
699 unsigned map;
700
701 if (!pid)
702 return "<idle>";
703
704 if (pid > PID_MAX_DEFAULT)
705 goto out;
706
707 map = map_pid_to_cmdline[pid];
708 if (map >= SAVED_CMDLINES)
709 goto out;
710
711 cmdline = saved_cmdlines[map];
712
713 out:
714 return cmdline;
715}
716
Ingo Molnare309b412008-05-12 21:20:51 +0200717void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200718{
719 if (atomic_read(&trace_record_cmdline_disabled))
720 return;
721
722 trace_save_cmdline(tsk);
723}
724
Ingo Molnare309b412008-05-12 21:20:51 +0200725static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200726trace_next_list(struct trace_array_cpu *data, struct list_head *next)
727{
728 /*
729 * Roundrobin - but skip the head (which is not a real page):
730 */
731 next = next->next;
732 if (unlikely(next == &data->trace_pages))
733 next = next->next;
734 BUG_ON(next == &data->trace_pages);
735
736 return next;
737}
738
Ingo Molnare309b412008-05-12 21:20:51 +0200739static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200740trace_next_page(struct trace_array_cpu *data, void *addr)
741{
742 struct list_head *next;
743 struct page *page;
744
745 page = virt_to_page(addr);
746
747 next = trace_next_list(data, &page->lru);
748 page = list_entry(next, struct page, lru);
749
750 return page_address(page);
751}
752
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300753struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200754tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200755{
756 unsigned long idx, idx_next;
757 struct trace_entry *entry;
758
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200759 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200760 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200761 idx_next = idx + 1;
762
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200763 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
764
Steven Rostedt93a588f2008-05-12 21:20:45 +0200765 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200766
767 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200768 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200769 idx_next = 0;
770 }
771
Steven Rostedt93a588f2008-05-12 21:20:45 +0200772 if (data->trace_head == data->trace_tail &&
773 idx_next == data->trace_tail_idx) {
774 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200775 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200776 data->trace_tail_idx++;
777 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
778 data->trace_tail =
779 trace_next_page(data, data->trace_tail);
780 data->trace_tail_idx = 0;
781 }
782 }
783
784 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200785
786 return entry;
787}
788
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300789void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200790tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200791{
792 struct task_struct *tsk = current;
793 unsigned long pc;
794
795 pc = preempt_count();
796
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400797 entry->field.preempt_count = pc & 0xff;
798 entry->field.pid = (tsk) ? tsk->pid : 0;
799 entry->field.t = ftrace_now(raw_smp_processor_id());
800 entry->field.flags =
801 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200802 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
803 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
804 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
805}
806
Ingo Molnare309b412008-05-12 21:20:51 +0200807void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200808trace_function(struct trace_array *tr, struct trace_array_cpu *data,
809 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200810{
811 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200812 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200813
Steven Rostedt92205c22008-05-12 21:20:55 +0200814 raw_local_irq_save(irq_flags);
815 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400816 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200817 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400818 entry->type = TRACE_FN;
819 entry->field.fn.ip = ip;
820 entry->field.fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200821 __raw_spin_unlock(&data->lock);
822 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200823}
824
Ingo Molnare309b412008-05-12 21:20:51 +0200825void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200826ftrace(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags)
828{
829 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200830 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200831}
832
Ingo Molnar86387f72008-05-12 21:20:51 +0200833void __trace_stack(struct trace_array *tr,
834 struct trace_array_cpu *data,
835 unsigned long flags,
836 int skip)
837{
838 struct trace_entry *entry;
839 struct stack_trace trace;
840
841 if (!(trace_flags & TRACE_ITER_STACKTRACE))
842 return;
843
844 entry = tracing_get_trace_entry(tr, data);
845 tracing_generic_entry_update(entry, flags);
846 entry->type = TRACE_STACK;
847
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400848 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
Ingo Molnar86387f72008-05-12 21:20:51 +0200849
850 trace.nr_entries = 0;
851 trace.max_entries = FTRACE_STACK_ENTRIES;
852 trace.skip = skip;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400853 trace.entries = entry->field.stack.caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200854
855 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200856}
857
Ingo Molnare309b412008-05-12 21:20:51 +0200858void
Ingo Molnara4feb832008-05-12 21:21:02 +0200859__trace_special(void *__tr, void *__data,
860 unsigned long arg1, unsigned long arg2, unsigned long arg3)
861{
862 struct trace_array_cpu *data = __data;
863 struct trace_array *tr = __tr;
864 struct trace_entry *entry;
865 unsigned long irq_flags;
866
867 raw_local_irq_save(irq_flags);
868 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400869 entry = tracing_get_trace_entry(tr, data);
Ingo Molnara4feb832008-05-12 21:21:02 +0200870 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400871 entry->type = TRACE_SPECIAL;
872 entry->field.special.arg1 = arg1;
873 entry->field.special.arg2 = arg2;
874 entry->field.special.arg3 = arg3;
Ingo Molnara4feb832008-05-12 21:21:02 +0200875 __trace_stack(tr, data, irq_flags, 4);
876 __raw_spin_unlock(&data->lock);
877 raw_local_irq_restore(irq_flags);
878
879 trace_wake_up();
880}
881
882void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200883tracing_sched_switch_trace(struct trace_array *tr,
884 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200885 struct task_struct *prev,
886 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200887 unsigned long flags)
888{
889 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200890 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200891
Steven Rostedt92205c22008-05-12 21:20:55 +0200892 raw_local_irq_save(irq_flags);
893 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400894 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200895 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400896 entry->type = TRACE_CTX;
897 entry->field.ctx.prev_pid = prev->pid;
898 entry->field.ctx.prev_prio = prev->prio;
899 entry->field.ctx.prev_state = prev->state;
900 entry->field.ctx.next_pid = next->pid;
901 entry->field.ctx.next_prio = next->prio;
902 entry->field.ctx.next_state = next->state;
Peter Zijlstra80b5e942008-09-04 10:24:16 +0200903 entry->field.ctx.next_cpu = task_cpu(next);
Ingo Molnar74f4e362008-05-12 21:21:15 +0200904 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200905 __raw_spin_unlock(&data->lock);
906 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200907}
908
Ingo Molnar57422792008-05-12 21:20:51 +0200909void
910tracing_sched_wakeup_trace(struct trace_array *tr,
911 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200912 struct task_struct *wakee,
913 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200914 unsigned long flags)
915{
916 struct trace_entry *entry;
917 unsigned long irq_flags;
918
Steven Rostedt92205c22008-05-12 21:20:55 +0200919 raw_local_irq_save(irq_flags);
920 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200921 entry = tracing_get_trace_entry(tr, data);
922 tracing_generic_entry_update(entry, flags);
923 entry->type = TRACE_WAKE;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400924 entry->field.ctx.prev_pid = curr->pid;
925 entry->field.ctx.prev_prio = curr->prio;
926 entry->field.ctx.prev_state = curr->state;
927 entry->field.ctx.next_pid = wakee->pid;
928 entry->field.ctx.next_prio = wakee->prio;
929 entry->field.ctx.next_state = wakee->state;
Peter Zijlstra80b5e942008-09-04 10:24:16 +0200930 entry->field.ctx.next_cpu = task_cpu(wakee);
Ingo Molnar74f4e362008-05-12 21:21:15 +0200931 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200932 __raw_spin_unlock(&data->lock);
933 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200934
935 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200936}
937
Steven Rostedt4902f882008-05-22 00:22:18 -0400938void
939ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
940{
941 struct trace_array *tr = &global_trace;
942 struct trace_array_cpu *data;
943 unsigned long flags;
944 long disabled;
945 int cpu;
946
Frédéric Weisbecker43a15382008-09-21 20:16:30 +0200947 if (tracing_disabled || !tr->ctrl)
Steven Rostedt4902f882008-05-22 00:22:18 -0400948 return;
949
950 local_irq_save(flags);
951 cpu = raw_smp_processor_id();
952 data = tr->data[cpu];
953 disabled = atomic_inc_return(&data->disabled);
954
955 if (likely(disabled == 1))
956 __trace_special(tr, data, arg1, arg2, arg3);
957
958 atomic_dec(&data->disabled);
959 local_irq_restore(flags);
960}
961
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200962#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200963static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200964function_trace_call(unsigned long ip, unsigned long parent_ip)
965{
966 struct trace_array *tr = &global_trace;
967 struct trace_array_cpu *data;
968 unsigned long flags;
969 long disabled;
970 int cpu;
971
Steven Rostedt60bc0802008-07-10 20:58:16 -0400972 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200973 return;
974
Abhishek Sagarecea6562008-06-21 23:47:53 +0530975 if (skip_trace(ip))
976 return;
977
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200978 local_irq_save(flags);
979 cpu = raw_smp_processor_id();
980 data = tr->data[cpu];
981 disabled = atomic_inc_return(&data->disabled);
982
983 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200984 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200985
986 atomic_dec(&data->disabled);
987 local_irq_restore(flags);
988}
989
990static struct ftrace_ops trace_ops __read_mostly =
991{
992 .func = function_trace_call,
993};
994
Ingo Molnare309b412008-05-12 21:20:51 +0200995void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200996{
Steven Rostedt60bc0802008-07-10 20:58:16 -0400997 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200998 register_ftrace_function(&trace_ops);
Steven Rostedt60bc0802008-07-10 20:58:16 -0400999 if (tracer_enabled)
1000 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001001}
1002
Ingo Molnare309b412008-05-12 21:20:51 +02001003void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001004{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001005 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001006 unregister_ftrace_function(&trace_ops);
1007}
1008#endif
1009
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001010enum trace_file_type {
1011 TRACE_FILE_LAT_FMT = 1,
1012};
1013
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001014/* Return the current entry. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001015static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001016trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1017 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001018{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001019 struct page *page;
1020 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001021
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001022 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001023 iter->next_idx[cpu] >= data->trace_idx ||
1024 (data->trace_head == data->trace_tail &&
1025 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001026 return NULL;
1027
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001028 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001029 /* Initialize the iterator for this cpu trace buffer */
1030 WARN_ON(!data->trace_tail);
1031 page = virt_to_page(data->trace_tail);
1032 iter->next_page[cpu] = &page->lru;
1033 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001034 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001035
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001036 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001037 BUG_ON(&data->trace_pages == &page->lru);
1038
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001039 array = page_address(page);
1040
Steven Rostedt93a588f2008-05-12 21:20:45 +02001041 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001042 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001043}
1044
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001045/* Increment the index counter of an iterator by one */
Steven Rostedt5a90f572008-09-03 17:42:51 -04001046static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001047{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001048 iter->next_idx[cpu]++;
1049 iter->next_page_idx[cpu]++;
1050
1051 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1052 struct trace_array_cpu *data = iter->tr->data[cpu];
1053
1054 iter->next_page_idx[cpu] = 0;
1055 iter->next_page[cpu] =
1056 trace_next_list(data, iter->next_page[cpu]);
1057 }
1058}
1059
Steven Rostedt5a90f572008-09-03 17:42:51 -04001060static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1061{
1062 iter->idx++;
1063 __trace_iterator_increment(iter, cpu);
1064}
1065
Ingo Molnare309b412008-05-12 21:20:51 +02001066static struct trace_entry *
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001067trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1068 struct trace_iterator *iter, int cpu)
1069{
1070 struct list_head *next_page;
1071 struct trace_entry *ent;
1072 int idx, next_idx, next_page_idx;
1073
1074 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1075
1076 if (likely(!ent || ent->type != TRACE_CONT))
1077 return ent;
1078
1079 /* save the iterator details */
1080 idx = iter->idx;
1081 next_idx = iter->next_idx[cpu];
1082 next_page_idx = iter->next_page_idx[cpu];
1083 next_page = iter->next_page[cpu];
1084
1085 /* find a real entry */
1086 do {
Steven Rostedt5a90f572008-09-03 17:42:51 -04001087 __trace_iterator_increment(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001088 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1089 } while (ent && ent->type != TRACE_CONT);
1090
1091 /* reset the iterator */
1092 iter->idx = idx;
1093 iter->next_idx[cpu] = next_idx;
1094 iter->next_page_idx[cpu] = next_page_idx;
1095 iter->next_page[cpu] = next_page;
1096
1097 return ent;
1098}
1099
1100static struct trace_entry *
1101__find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001102{
1103 struct trace_array *tr = iter->tr;
1104 struct trace_entry *ent, *next = NULL;
1105 int next_cpu = -1;
1106 int cpu;
1107
Steven Rostedtab464282008-05-12 21:21:00 +02001108 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001109 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001110 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001111
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001112 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001113
1114 if (ent && ent->type == TRACE_CONT) {
1115 struct trace_array_cpu *data = tr->data[cpu];
1116
1117 if (!inc)
1118 ent = trace_entry_next(tr, data, iter, cpu);
1119 else {
1120 while (ent && ent->type == TRACE_CONT) {
Steven Rostedt5a90f572008-09-03 17:42:51 -04001121 __trace_iterator_increment(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001122 ent = trace_entry_idx(tr, tr->data[cpu],
1123 iter, cpu);
1124 }
1125 }
1126 }
1127
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001128 /*
1129 * Pick the entry with the smallest timestamp:
1130 */
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001131 if (ent && (!next || ent->field.t < next->field.t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001132 next = ent;
1133 next_cpu = cpu;
1134 }
1135 }
1136
1137 if (ent_cpu)
1138 *ent_cpu = next_cpu;
1139
1140 return next;
1141}
1142
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001143/* Find the next real entry, without updating the iterator itself */
1144static struct trace_entry *
1145find_next_entry(struct trace_iterator *iter, int *ent_cpu)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001146{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001147 return __find_next_entry(iter, ent_cpu, 0);
1148}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001149
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001150/* Find the next real entry, and increment the iterator to the next entry */
1151static void *find_next_entry_inc(struct trace_iterator *iter)
1152{
1153 struct trace_entry *next;
1154 int next_cpu = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001155
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001156 next = __find_next_entry(iter, &next_cpu, 1);
1157
1158 iter->prev_ent = iter->ent;
1159 iter->prev_cpu = iter->cpu;
1160
1161 iter->ent = next;
1162 iter->cpu = next_cpu;
1163
1164 if (next)
1165 trace_iterator_increment(iter, iter->cpu);
1166
1167 return next ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001168}
1169
Ingo Molnare309b412008-05-12 21:20:51 +02001170static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001171{
1172 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001173 struct trace_entry *ent;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001174
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001175 again:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001176 data->trace_tail_idx++;
1177 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1178 data->trace_tail = trace_next_page(data, data->trace_tail);
1179 data->trace_tail_idx = 0;
1180 }
1181
1182 /* Check if we empty it, then reset the index */
1183 if (data->trace_head == data->trace_tail &&
1184 data->trace_head_idx == data->trace_tail_idx)
1185 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001186
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001187 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1188 iter, iter->cpu);
1189 if (ent && ent->type == TRACE_CONT)
1190 goto again;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001191}
1192
Ingo Molnare309b412008-05-12 21:20:51 +02001193static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001194{
1195 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001196 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001197 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001198
1199 (*pos)++;
1200
1201 /* can't go backwards */
1202 if (iter->idx > i)
1203 return NULL;
1204
1205 if (iter->idx < 0)
1206 ent = find_next_entry_inc(iter);
1207 else
1208 ent = iter;
1209
1210 while (ent && iter->idx < i)
1211 ent = find_next_entry_inc(iter);
1212
1213 iter->pos = *pos;
1214
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001215 return ent;
1216}
1217
1218static void *s_start(struct seq_file *m, loff_t *pos)
1219{
1220 struct trace_iterator *iter = m->private;
1221 void *p = NULL;
1222 loff_t l = 0;
1223 int i;
1224
1225 mutex_lock(&trace_types_lock);
1226
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001227 if (!current_trace || current_trace != iter->trace) {
1228 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001229 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001230 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001231
1232 atomic_inc(&trace_record_cmdline_disabled);
1233
1234 /* let the tracer grab locks here if needed */
1235 if (current_trace->start)
1236 current_trace->start(iter);
1237
1238 if (*pos != iter->pos) {
1239 iter->ent = NULL;
1240 iter->cpu = 0;
1241 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001242 iter->prev_ent = NULL;
1243 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001244
Steven Rostedtab464282008-05-12 21:21:00 +02001245 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001246 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001247 iter->next_page[i] = NULL;
1248 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001249
1250 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1251 ;
1252
1253 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001254 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001255 p = s_next(m, p, &l);
1256 }
1257
1258 return p;
1259}
1260
1261static void s_stop(struct seq_file *m, void *p)
1262{
1263 struct trace_iterator *iter = m->private;
1264
1265 atomic_dec(&trace_record_cmdline_disabled);
1266
1267 /* let the tracer release locks here if needed */
1268 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1269 iter->trace->stop(iter);
1270
1271 mutex_unlock(&trace_types_lock);
1272}
1273
Abhishek Sagar76094a22008-05-28 00:03:18 +05301274#define KRETPROBE_MSG "[unknown/kretprobe'd]"
1275
1276#ifdef CONFIG_KRETPROBES
1277static inline int kretprobed(unsigned long addr)
1278{
1279 return addr == (unsigned long)kretprobe_trampoline;
1280}
1281#else
1282static inline int kretprobed(unsigned long addr)
1283{
1284 return 0;
1285}
1286#endif /* CONFIG_KRETPROBES */
1287
Steven Rostedtb3806b42008-05-12 21:20:46 +02001288static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001289seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290{
1291#ifdef CONFIG_KALLSYMS
1292 char str[KSYM_SYMBOL_LEN];
1293
1294 kallsyms_lookup(address, NULL, NULL, NULL, str);
1295
Steven Rostedtb3806b42008-05-12 21:20:46 +02001296 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001297#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001298 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001299}
1300
Steven Rostedtb3806b42008-05-12 21:20:46 +02001301static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001302seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1303 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001304{
1305#ifdef CONFIG_KALLSYMS
1306 char str[KSYM_SYMBOL_LEN];
1307
1308 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001309 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001310#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001311 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001312}
1313
1314#ifndef CONFIG_64BIT
1315# define IP_FMT "%08lx"
1316#else
1317# define IP_FMT "%016lx"
1318#endif
1319
Ingo Molnare309b412008-05-12 21:20:51 +02001320static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001321seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001323 int ret;
1324
1325 if (!ip)
1326 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001327
1328 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001329 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001330 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001331 ret = seq_print_sym_short(s, "%s", ip);
1332
1333 if (!ret)
1334 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001335
1336 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001337 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1338 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001339}
1340
Ingo Molnare309b412008-05-12 21:20:51 +02001341static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001342{
Michael Ellermana6168352008-08-20 16:36:11 -07001343 seq_puts(m, "# _------=> CPU# \n");
1344 seq_puts(m, "# / _-----=> irqs-off \n");
1345 seq_puts(m, "# | / _----=> need-resched \n");
1346 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1347 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1348 seq_puts(m, "# |||| / \n");
1349 seq_puts(m, "# ||||| delay \n");
1350 seq_puts(m, "# cmd pid ||||| time | caller \n");
1351 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001352}
1353
Ingo Molnare309b412008-05-12 21:20:51 +02001354static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001355{
Michael Ellermana6168352008-08-20 16:36:11 -07001356 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1357 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358}
1359
1360
Ingo Molnare309b412008-05-12 21:20:51 +02001361static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1363{
1364 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1365 struct trace_array *tr = iter->tr;
1366 struct trace_array_cpu *data = tr->data[tr->cpu];
1367 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001368 unsigned long total = 0;
1369 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370 int cpu;
1371 const char *name = "preemption";
1372
1373 if (type)
1374 name = type->name;
1375
Steven Rostedtab464282008-05-12 21:21:00 +02001376 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001377 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001378 total += tr->data[cpu]->trace_idx;
1379 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001381 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001382 entries += tr->data[cpu]->trace_idx;
1383 }
1384 }
1385
1386 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1387 name, UTS_RELEASE);
1388 seq_puts(m, "-----------------------------------"
1389 "---------------------------------\n");
1390 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1391 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001392 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001393 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001394 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001395 tr->cpu,
1396#if defined(CONFIG_PREEMPT_NONE)
1397 "server",
1398#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1399 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001400#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001401 "preempt",
1402#else
1403 "unknown",
1404#endif
1405 /* These are reserved for later use */
1406 0, 0, 0, 0);
1407#ifdef CONFIG_SMP
1408 seq_printf(m, " #P:%d)\n", num_online_cpus());
1409#else
1410 seq_puts(m, ")\n");
1411#endif
1412 seq_puts(m, " -----------------\n");
1413 seq_printf(m, " | task: %.16s-%d "
1414 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1415 data->comm, data->pid, data->uid, data->nice,
1416 data->policy, data->rt_priority);
1417 seq_puts(m, " -----------------\n");
1418
1419 if (data->critical_start) {
1420 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001421 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1422 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001423 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001424 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1425 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001426 seq_puts(m, "\n");
1427 }
1428
1429 seq_puts(m, "\n");
1430}
1431
Ingo Molnare309b412008-05-12 21:20:51 +02001432static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001433lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001434{
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001435 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001436 int hardirq, softirq;
1437 char *comm;
1438
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001439 comm = trace_find_cmdline(field->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001440
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001441 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001442 trace_seq_printf(s, "%3d", cpu);
Steven Rostedt214023c2008-05-12 21:20:46 +02001443 trace_seq_printf(s, "%c%c",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001444 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1445 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001446
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001447 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1448 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001449 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001450 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001451 } else {
1452 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001453 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001454 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001456 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001457 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001458 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001459 }
1460 }
1461
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001462 if (field->preempt_count)
1463 trace_seq_printf(s, "%x", field->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001464 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001465 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001466}
1467
1468unsigned long preempt_mark_thresh = 100;
1469
Ingo Molnare309b412008-05-12 21:20:51 +02001470static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001471lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001472 unsigned long rel_usecs)
1473{
Steven Rostedt214023c2008-05-12 21:20:46 +02001474 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001475 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001476 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001478 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001479 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001480 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001481}
1482
1483static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1484
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001485/*
1486 * The message is supposed to contain an ending newline.
1487 * If the printing stops prematurely, try to add a newline of our own.
1488 */
1489void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001490{
1491 struct trace_array *tr = iter->tr;
1492 struct trace_array_cpu *data = tr->data[iter->cpu];
1493 struct trace_entry *ent;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001494 bool ok = true;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001495
1496 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1497 if (!ent || ent->type != TRACE_CONT) {
1498 trace_seq_putc(s, '\n');
1499 return;
1500 }
1501
1502 do {
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001503 if (ok)
1504 ok = (trace_seq_printf(s, "%s", ent->cont.buf) > 0);
Steven Rostedt5a90f572008-09-03 17:42:51 -04001505 __trace_iterator_increment(iter, iter->cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001506 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1507 } while (ent && ent->type == TRACE_CONT);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001508
1509 if (!ok)
1510 trace_seq_putc(s, '\n');
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001511}
1512
Ingo Molnare309b412008-05-12 21:20:51 +02001513static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001514print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001515{
Steven Rostedt214023c2008-05-12 21:20:46 +02001516 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001517 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1518 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1519 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1520 struct trace_entry *entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001521 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001522 unsigned long abs_usecs;
1523 unsigned long rel_usecs;
1524 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001525 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001526 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001527 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001528
1529 if (!next_entry)
1530 next_entry = entry;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001531
1532 if (entry->type == TRACE_CONT)
1533 return 1;
1534
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001535 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1536 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001537
1538 if (verbose) {
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001539 comm = trace_find_cmdline(field->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001540 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
Steven Rostedt214023c2008-05-12 21:20:46 +02001541 " %ld.%03ldms (+%ld.%03ldms): ",
1542 comm,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001543 field->pid, cpu, field->flags,
1544 field->preempt_count, trace_idx,
1545 ns2usecs(field->t),
Steven Rostedt214023c2008-05-12 21:20:46 +02001546 abs_usecs/1000,
1547 abs_usecs % 1000, rel_usecs/1000,
1548 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001549 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001550 lat_print_generic(s, entry, cpu);
1551 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001552 }
1553 switch (entry->type) {
1554 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001555 seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001556 trace_seq_puts(s, " (");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001557 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301558 trace_seq_puts(s, KRETPROBE_MSG);
1559 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001560 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001561 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001562 break;
1563 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001564 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001565 T = field->ctx.next_state < sizeof(state_to_char) ?
1566 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001567
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001568 state = field->ctx.prev_state ?
1569 __ffs(field->ctx.prev_state) + 1 : 0;
Ankita Gargd17d9692008-05-12 21:20:58 +02001570 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001571 comm = trace_find_cmdline(field->ctx.next_pid);
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001572 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001573 field->ctx.prev_pid,
1574 field->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001575 S, entry->type == TRACE_CTX ? "==>" : " +",
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001576 field->ctx.next_cpu,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001577 field->ctx.next_pid,
1578 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001579 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001581 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001582 trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001583 field->special.arg1,
1584 field->special.arg2,
1585 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001586 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001587 case TRACE_STACK:
1588 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1589 if (i)
1590 trace_seq_puts(s, " <= ");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001591 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +02001592 }
1593 trace_seq_puts(s, "\n");
1594 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001595 case TRACE_PRINT:
1596 seq_print_ip_sym(s, field->print.ip, sym_flags);
1597 trace_seq_printf(s, ": %s", field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001598 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001599 trace_seq_print_cont(s, iter);
1600 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001601 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001602 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001603 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001604 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001605}
1606
Ingo Molnare309b412008-05-12 21:20:51 +02001607static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001608{
Steven Rostedt214023c2008-05-12 21:20:46 +02001609 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001610 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001611 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001612 struct trace_field *field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001613 unsigned long usec_rem;
1614 unsigned long long t;
1615 unsigned long secs;
1616 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001617 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001618 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001619 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001620
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001621 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001622
1623 if (entry->type == TRACE_CONT)
1624 return 1;
1625
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001626 field = &entry->field;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001627
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001628 comm = trace_find_cmdline(iter->ent->field.pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001629
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001630 t = ns2usecs(field->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001631 usec_rem = do_div(t, 1000000ULL);
1632 secs = (unsigned long)t;
1633
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001634 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001635 if (!ret)
1636 return 0;
Michael Ellermana6168352008-08-20 16:36:11 -07001637 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001638 if (!ret)
1639 return 0;
1640 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1641 if (!ret)
1642 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001643
1644 switch (entry->type) {
1645 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001646 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001647 if (!ret)
1648 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001649 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001650 field->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001651 ret = trace_seq_printf(s, " <-");
1652 if (!ret)
1653 return 0;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001654 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301655 ret = trace_seq_puts(s, KRETPROBE_MSG);
1656 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001657 ret = seq_print_ip_sym(s,
1658 field->fn.parent_ip,
Abhishek Sagar76094a22008-05-28 00:03:18 +05301659 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001660 if (!ret)
1661 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001662 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001663 ret = trace_seq_printf(s, "\n");
1664 if (!ret)
1665 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001666 break;
1667 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001668 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001669 S = field->ctx.prev_state < sizeof(state_to_char) ?
1670 state_to_char[field->ctx.prev_state] : 'X';
1671 T = field->ctx.next_state < sizeof(state_to_char) ?
1672 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001673 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001674 field->ctx.prev_pid,
1675 field->ctx.prev_prio,
Steven Rostedtb3806b42008-05-12 21:20:46 +02001676 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001677 entry->type == TRACE_CTX ? "==>" : " +",
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001678 field->ctx.next_cpu,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001679 field->ctx.next_pid,
1680 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001681 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001682 if (!ret)
1683 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001684 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001685 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001686 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001687 field->special.arg1,
1688 field->special.arg2,
1689 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001690 if (!ret)
1691 return 0;
1692 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001693 case TRACE_STACK:
1694 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1695 if (i) {
1696 ret = trace_seq_puts(s, " <= ");
1697 if (!ret)
1698 return 0;
1699 }
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001700 ret = seq_print_ip_sym(s, field->stack.caller[i],
Ingo Molnar86387f72008-05-12 21:20:51 +02001701 sym_flags);
1702 if (!ret)
1703 return 0;
1704 }
1705 ret = trace_seq_puts(s, "\n");
1706 if (!ret)
1707 return 0;
1708 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001709 case TRACE_PRINT:
1710 seq_print_ip_sym(s, field->print.ip, sym_flags);
1711 trace_seq_printf(s, ": %s", field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001712 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001713 trace_seq_print_cont(s, iter);
1714 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001715 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001716 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001717}
1718
Ingo Molnare309b412008-05-12 21:20:51 +02001719static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001720{
1721 struct trace_seq *s = &iter->seq;
1722 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001723 struct trace_field *field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001724 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001725 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001726
1727 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001728
1729 if (entry->type == TRACE_CONT)
1730 return 1;
1731
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001732 field = &entry->field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001733
1734 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001735 field->pid, iter->cpu, field->t);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001736 if (!ret)
1737 return 0;
1738
1739 switch (entry->type) {
1740 case TRACE_FN:
1741 ret = trace_seq_printf(s, "%x %x\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001742 field->fn.ip,
1743 field->fn.parent_ip);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001744 if (!ret)
1745 return 0;
1746 break;
1747 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001748 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001749 S = field->ctx.prev_state < sizeof(state_to_char) ?
1750 state_to_char[field->ctx.prev_state] : 'X';
1751 T = field->ctx.next_state < sizeof(state_to_char) ?
1752 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001753 if (entry->type == TRACE_WAKE)
1754 S = '+';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001755 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001756 field->ctx.prev_pid,
1757 field->ctx.prev_prio,
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001758 S,
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001759 field->ctx.next_cpu,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001760 field->ctx.next_pid,
1761 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001762 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001763 if (!ret)
1764 return 0;
1765 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001766 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001767 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001768 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001769 field->special.arg1,
1770 field->special.arg2,
1771 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001772 if (!ret)
1773 return 0;
1774 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001775 case TRACE_PRINT:
1776 trace_seq_printf(s, "# %lx %s",
1777 field->print.ip, field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001778 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001779 trace_seq_print_cont(s, iter);
1780 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001781 }
1782 return 1;
1783}
1784
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001785#define SEQ_PUT_FIELD_RET(s, x) \
1786do { \
1787 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1788 return 0; \
1789} while (0)
1790
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001791#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1792do { \
1793 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1794 return 0; \
1795} while (0)
1796
Ingo Molnare309b412008-05-12 21:20:51 +02001797static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001798{
1799 struct trace_seq *s = &iter->seq;
1800 unsigned char newline = '\n';
1801 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001802 struct trace_field *field;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001803 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001804
1805 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001806
1807 if (entry->type == TRACE_CONT)
1808 return 1;
1809
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001810 field = &entry->field;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001811
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001812 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001813 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001814 SEQ_PUT_HEX_FIELD_RET(s, field->t);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001815
1816 switch (entry->type) {
1817 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001818 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1819 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001820 break;
1821 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001822 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001823 S = field->ctx.prev_state < sizeof(state_to_char) ?
1824 state_to_char[field->ctx.prev_state] : 'X';
1825 T = field->ctx.next_state < sizeof(state_to_char) ?
1826 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001827 if (entry->type == TRACE_WAKE)
1828 S = '+';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001829 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1830 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001831 SEQ_PUT_HEX_FIELD_RET(s, S);
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001832 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_cpu);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001833 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1834 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001835 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001836 break;
1837 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001838 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001839 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1840 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1841 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001842 break;
1843 }
1844 SEQ_PUT_FIELD_RET(s, newline);
1845
1846 return 1;
1847}
1848
Ingo Molnare309b412008-05-12 21:20:51 +02001849static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001850{
1851 struct trace_seq *s = &iter->seq;
1852 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001853 struct trace_field *field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001854
1855 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001856
1857 if (entry->type == TRACE_CONT)
1858 return 1;
1859
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001860 field = &entry->field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001861
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001862 SEQ_PUT_FIELD_RET(s, field->pid);
1863 SEQ_PUT_FIELD_RET(s, field->cpu);
1864 SEQ_PUT_FIELD_RET(s, field->t);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001865
1866 switch (entry->type) {
1867 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001868 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1869 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001870 break;
1871 case TRACE_CTX:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001872 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1873 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1874 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1875 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1876 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1877 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001878 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001879 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001880 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001881 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1882 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1883 SEQ_PUT_FIELD_RET(s, field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001884 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001885 }
1886 return 1;
1887}
1888
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001889static int trace_empty(struct trace_iterator *iter)
1890{
1891 struct trace_array_cpu *data;
1892 int cpu;
1893
Steven Rostedtab464282008-05-12 21:21:00 +02001894 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001895 data = iter->tr->data[cpu];
1896
Steven Rostedtb3806b42008-05-12 21:20:46 +02001897 if (head_page(data) && data->trace_idx &&
1898 (data->trace_tail != data->trace_head ||
1899 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001900 return 0;
1901 }
1902 return 1;
1903}
1904
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001905static int print_trace_line(struct trace_iterator *iter)
1906{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001907 if (iter->trace && iter->trace->print_line)
1908 return iter->trace->print_line(iter);
1909
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001910 if (trace_flags & TRACE_ITER_BIN)
1911 return print_bin_fmt(iter);
1912
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001913 if (trace_flags & TRACE_ITER_HEX)
1914 return print_hex_fmt(iter);
1915
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001916 if (trace_flags & TRACE_ITER_RAW)
1917 return print_raw_fmt(iter);
1918
1919 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1920 return print_lat_fmt(iter, iter->idx, iter->cpu);
1921
1922 return print_trace_fmt(iter);
1923}
1924
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001925static int s_show(struct seq_file *m, void *v)
1926{
1927 struct trace_iterator *iter = v;
1928
1929 if (iter->ent == NULL) {
1930 if (iter->tr) {
1931 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1932 seq_puts(m, "#\n");
1933 }
1934 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1935 /* print nothing if the buffers are empty */
1936 if (trace_empty(iter))
1937 return 0;
1938 print_trace_header(m, iter);
1939 if (!(trace_flags & TRACE_ITER_VERBOSE))
1940 print_lat_help_header(m);
1941 } else {
1942 if (!(trace_flags & TRACE_ITER_VERBOSE))
1943 print_func_help_header(m);
1944 }
1945 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001946 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001947 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001948 }
1949
1950 return 0;
1951}
1952
1953static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001954 .start = s_start,
1955 .next = s_next,
1956 .stop = s_stop,
1957 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001958};
1959
Ingo Molnare309b412008-05-12 21:20:51 +02001960static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001961__tracing_open(struct inode *inode, struct file *file, int *ret)
1962{
1963 struct trace_iterator *iter;
1964
Steven Rostedt60a11772008-05-12 21:20:44 +02001965 if (tracing_disabled) {
1966 *ret = -ENODEV;
1967 return NULL;
1968 }
1969
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001970 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1971 if (!iter) {
1972 *ret = -ENOMEM;
1973 goto out;
1974 }
1975
1976 mutex_lock(&trace_types_lock);
1977 if (current_trace && current_trace->print_max)
1978 iter->tr = &max_tr;
1979 else
1980 iter->tr = inode->i_private;
1981 iter->trace = current_trace;
1982 iter->pos = -1;
1983
1984 /* TODO stop tracer */
1985 *ret = seq_open(file, &tracer_seq_ops);
1986 if (!*ret) {
1987 struct seq_file *m = file->private_data;
1988 m->private = iter;
1989
1990 /* stop the trace while dumping */
Steven Rostedt60bc0802008-07-10 20:58:16 -04001991 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001992 tracer_enabled = 0;
Steven Rostedt60bc0802008-07-10 20:58:16 -04001993 ftrace_function_enabled = 0;
1994 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001995
1996 if (iter->trace && iter->trace->open)
1997 iter->trace->open(iter);
1998 } else {
1999 kfree(iter);
2000 iter = NULL;
2001 }
2002 mutex_unlock(&trace_types_lock);
2003
2004 out:
2005 return iter;
2006}
2007
2008int tracing_open_generic(struct inode *inode, struct file *filp)
2009{
Steven Rostedt60a11772008-05-12 21:20:44 +02002010 if (tracing_disabled)
2011 return -ENODEV;
2012
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002013 filp->private_data = inode->i_private;
2014 return 0;
2015}
2016
2017int tracing_release(struct inode *inode, struct file *file)
2018{
2019 struct seq_file *m = (struct seq_file *)file->private_data;
2020 struct trace_iterator *iter = m->private;
2021
2022 mutex_lock(&trace_types_lock);
2023 if (iter->trace && iter->trace->close)
2024 iter->trace->close(iter);
2025
2026 /* reenable tracing if it was previously enabled */
Steven Rostedt60bc0802008-07-10 20:58:16 -04002027 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002028 tracer_enabled = 1;
Steven Rostedt60bc0802008-07-10 20:58:16 -04002029 /*
2030 * It is safe to enable function tracing even if it
2031 * isn't used
2032 */
2033 ftrace_function_enabled = 1;
2034 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002035 mutex_unlock(&trace_types_lock);
2036
2037 seq_release(inode, file);
2038 kfree(iter);
2039 return 0;
2040}
2041
2042static int tracing_open(struct inode *inode, struct file *file)
2043{
2044 int ret;
2045
2046 __tracing_open(inode, file, &ret);
2047
2048 return ret;
2049}
2050
2051static int tracing_lt_open(struct inode *inode, struct file *file)
2052{
2053 struct trace_iterator *iter;
2054 int ret;
2055
2056 iter = __tracing_open(inode, file, &ret);
2057
2058 if (!ret)
2059 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2060
2061 return ret;
2062}
2063
2064
Ingo Molnare309b412008-05-12 21:20:51 +02002065static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002066t_next(struct seq_file *m, void *v, loff_t *pos)
2067{
2068 struct tracer *t = m->private;
2069
2070 (*pos)++;
2071
2072 if (t)
2073 t = t->next;
2074
2075 m->private = t;
2076
2077 return t;
2078}
2079
2080static void *t_start(struct seq_file *m, loff_t *pos)
2081{
2082 struct tracer *t = m->private;
2083 loff_t l = 0;
2084
2085 mutex_lock(&trace_types_lock);
2086 for (; t && l < *pos; t = t_next(m, t, &l))
2087 ;
2088
2089 return t;
2090}
2091
2092static void t_stop(struct seq_file *m, void *p)
2093{
2094 mutex_unlock(&trace_types_lock);
2095}
2096
2097static int t_show(struct seq_file *m, void *v)
2098{
2099 struct tracer *t = v;
2100
2101 if (!t)
2102 return 0;
2103
2104 seq_printf(m, "%s", t->name);
2105 if (t->next)
2106 seq_putc(m, ' ');
2107 else
2108 seq_putc(m, '\n');
2109
2110 return 0;
2111}
2112
2113static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002114 .start = t_start,
2115 .next = t_next,
2116 .stop = t_stop,
2117 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002118};
2119
2120static int show_traces_open(struct inode *inode, struct file *file)
2121{
2122 int ret;
2123
Steven Rostedt60a11772008-05-12 21:20:44 +02002124 if (tracing_disabled)
2125 return -ENODEV;
2126
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002127 ret = seq_open(file, &show_traces_seq_ops);
2128 if (!ret) {
2129 struct seq_file *m = file->private_data;
2130 m->private = trace_types;
2131 }
2132
2133 return ret;
2134}
2135
2136static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002137 .open = tracing_open,
2138 .read = seq_read,
2139 .llseek = seq_lseek,
2140 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002141};
2142
2143static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002144 .open = tracing_lt_open,
2145 .read = seq_read,
2146 .llseek = seq_lseek,
2147 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002148};
2149
2150static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002151 .open = show_traces_open,
2152 .read = seq_read,
2153 .release = seq_release,
2154};
2155
Ingo Molnar36dfe922008-05-12 21:20:52 +02002156/*
2157 * Only trace on a CPU if the bitmask is set:
2158 */
2159static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2160
2161/*
2162 * When tracing/tracing_cpu_mask is modified then this holds
2163 * the new bitmask we are about to install:
2164 */
2165static cpumask_t tracing_cpumask_new;
2166
2167/*
2168 * The tracer itself will not take this lock, but still we want
2169 * to provide a consistent cpumask to user-space:
2170 */
2171static DEFINE_MUTEX(tracing_cpumask_update_lock);
2172
2173/*
2174 * Temporary storage for the character representation of the
2175 * CPU bitmask (and one more byte for the newline):
2176 */
2177static char mask_str[NR_CPUS + 1];
2178
Ingo Molnarc7078de2008-05-12 21:20:52 +02002179static ssize_t
2180tracing_cpumask_read(struct file *filp, char __user *ubuf,
2181 size_t count, loff_t *ppos)
2182{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002183 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002184
2185 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002186
2187 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2188 if (count - len < 2) {
2189 count = -EINVAL;
2190 goto out_err;
2191 }
2192 len += sprintf(mask_str + len, "\n");
2193 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2194
2195out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002196 mutex_unlock(&tracing_cpumask_update_lock);
2197
2198 return count;
2199}
2200
2201static ssize_t
2202tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2203 size_t count, loff_t *ppos)
2204{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002205 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002206
2207 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002208 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2209 if (err)
2210 goto err_unlock;
2211
Steven Rostedt92205c22008-05-12 21:20:55 +02002212 raw_local_irq_disable();
2213 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002214 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002215 /*
2216 * Increase/decrease the disabled counter if we are
2217 * about to flip a bit in the cpumask:
2218 */
2219 if (cpu_isset(cpu, tracing_cpumask) &&
2220 !cpu_isset(cpu, tracing_cpumask_new)) {
2221 atomic_inc(&global_trace.data[cpu]->disabled);
2222 }
2223 if (!cpu_isset(cpu, tracing_cpumask) &&
2224 cpu_isset(cpu, tracing_cpumask_new)) {
2225 atomic_dec(&global_trace.data[cpu]->disabled);
2226 }
2227 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002228 __raw_spin_unlock(&ftrace_max_lock);
2229 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002230
2231 tracing_cpumask = tracing_cpumask_new;
2232
Ingo Molnarc7078de2008-05-12 21:20:52 +02002233 mutex_unlock(&tracing_cpumask_update_lock);
2234
Ingo Molnarc7078de2008-05-12 21:20:52 +02002235 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002236
2237err_unlock:
2238 mutex_unlock(&tracing_cpumask_update_lock);
2239
2240 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002241}
2242
2243static struct file_operations tracing_cpumask_fops = {
2244 .open = tracing_open_generic,
2245 .read = tracing_cpumask_read,
2246 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002247};
2248
2249static ssize_t
2250tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2251 size_t cnt, loff_t *ppos)
2252{
2253 char *buf;
2254 int r = 0;
2255 int len = 0;
2256 int i;
2257
2258 /* calulate max size */
2259 for (i = 0; trace_options[i]; i++) {
2260 len += strlen(trace_options[i]);
2261 len += 3; /* "no" and space */
2262 }
2263
2264 /* +2 for \n and \0 */
2265 buf = kmalloc(len + 2, GFP_KERNEL);
2266 if (!buf)
2267 return -ENOMEM;
2268
2269 for (i = 0; trace_options[i]; i++) {
2270 if (trace_flags & (1 << i))
2271 r += sprintf(buf + r, "%s ", trace_options[i]);
2272 else
2273 r += sprintf(buf + r, "no%s ", trace_options[i]);
2274 }
2275
2276 r += sprintf(buf + r, "\n");
2277 WARN_ON(r >= len + 2);
2278
Ingo Molnar36dfe922008-05-12 21:20:52 +02002279 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002280
2281 kfree(buf);
2282
2283 return r;
2284}
2285
2286static ssize_t
2287tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2288 size_t cnt, loff_t *ppos)
2289{
2290 char buf[64];
2291 char *cmp = buf;
2292 int neg = 0;
2293 int i;
2294
Steven Rostedtcffae432008-05-12 21:21:00 +02002295 if (cnt >= sizeof(buf))
2296 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002297
2298 if (copy_from_user(&buf, ubuf, cnt))
2299 return -EFAULT;
2300
2301 buf[cnt] = 0;
2302
2303 if (strncmp(buf, "no", 2) == 0) {
2304 neg = 1;
2305 cmp += 2;
2306 }
2307
2308 for (i = 0; trace_options[i]; i++) {
2309 int len = strlen(trace_options[i]);
2310
2311 if (strncmp(cmp, trace_options[i], len) == 0) {
2312 if (neg)
2313 trace_flags &= ~(1 << i);
2314 else
2315 trace_flags |= (1 << i);
2316 break;
2317 }
2318 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002319 /*
2320 * If no option could be set, return an error:
2321 */
2322 if (!trace_options[i])
2323 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002324
2325 filp->f_pos += cnt;
2326
2327 return cnt;
2328}
2329
2330static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002331 .open = tracing_open_generic,
2332 .read = tracing_iter_ctrl_read,
2333 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002334};
2335
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002336static const char readme_msg[] =
2337 "tracing mini-HOWTO:\n\n"
2338 "# mkdir /debug\n"
2339 "# mount -t debugfs nodev /debug\n\n"
2340 "# cat /debug/tracing/available_tracers\n"
2341 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2342 "# cat /debug/tracing/current_tracer\n"
2343 "none\n"
2344 "# echo sched_switch > /debug/tracing/current_tracer\n"
2345 "# cat /debug/tracing/current_tracer\n"
2346 "sched_switch\n"
2347 "# cat /debug/tracing/iter_ctrl\n"
2348 "noprint-parent nosym-offset nosym-addr noverbose\n"
2349 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2350 "# echo 1 > /debug/tracing/tracing_enabled\n"
2351 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2352 "echo 0 > /debug/tracing/tracing_enabled\n"
2353;
2354
2355static ssize_t
2356tracing_readme_read(struct file *filp, char __user *ubuf,
2357 size_t cnt, loff_t *ppos)
2358{
2359 return simple_read_from_buffer(ubuf, cnt, ppos,
2360 readme_msg, strlen(readme_msg));
2361}
2362
2363static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002364 .open = tracing_open_generic,
2365 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002366};
2367
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002368static ssize_t
2369tracing_ctrl_read(struct file *filp, char __user *ubuf,
2370 size_t cnt, loff_t *ppos)
2371{
2372 struct trace_array *tr = filp->private_data;
2373 char buf[64];
2374 int r;
2375
2376 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002377 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002378}
2379
2380static ssize_t
2381tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2382 size_t cnt, loff_t *ppos)
2383{
2384 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002385 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002386 long val;
2387 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002388
Steven Rostedtcffae432008-05-12 21:21:00 +02002389 if (cnt >= sizeof(buf))
2390 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002391
2392 if (copy_from_user(&buf, ubuf, cnt))
2393 return -EFAULT;
2394
2395 buf[cnt] = 0;
2396
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002397 ret = strict_strtoul(buf, 10, &val);
2398 if (ret < 0)
2399 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002400
2401 val = !!val;
2402
2403 mutex_lock(&trace_types_lock);
2404 if (tr->ctrl ^ val) {
2405 if (val)
2406 tracer_enabled = 1;
2407 else
2408 tracer_enabled = 0;
2409
2410 tr->ctrl = val;
2411
2412 if (current_trace && current_trace->ctrl_update)
2413 current_trace->ctrl_update(tr);
2414 }
2415 mutex_unlock(&trace_types_lock);
2416
2417 filp->f_pos += cnt;
2418
2419 return cnt;
2420}
2421
2422static ssize_t
2423tracing_set_trace_read(struct file *filp, char __user *ubuf,
2424 size_t cnt, loff_t *ppos)
2425{
2426 char buf[max_tracer_type_len+2];
2427 int r;
2428
2429 mutex_lock(&trace_types_lock);
2430 if (current_trace)
2431 r = sprintf(buf, "%s\n", current_trace->name);
2432 else
2433 r = sprintf(buf, "\n");
2434 mutex_unlock(&trace_types_lock);
2435
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002436 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002437}
2438
2439static ssize_t
2440tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2441 size_t cnt, loff_t *ppos)
2442{
2443 struct trace_array *tr = &global_trace;
2444 struct tracer *t;
2445 char buf[max_tracer_type_len+1];
2446 int i;
2447
2448 if (cnt > max_tracer_type_len)
2449 cnt = max_tracer_type_len;
2450
2451 if (copy_from_user(&buf, ubuf, cnt))
2452 return -EFAULT;
2453
2454 buf[cnt] = 0;
2455
2456 /* strip ending whitespace. */
2457 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2458 buf[i] = 0;
2459
2460 mutex_lock(&trace_types_lock);
2461 for (t = trace_types; t; t = t->next) {
2462 if (strcmp(t->name, buf) == 0)
2463 break;
2464 }
2465 if (!t || t == current_trace)
2466 goto out;
2467
2468 if (current_trace && current_trace->reset)
2469 current_trace->reset(tr);
2470
2471 current_trace = t;
2472 if (t->init)
2473 t->init(tr);
2474
2475 out:
2476 mutex_unlock(&trace_types_lock);
2477
2478 filp->f_pos += cnt;
2479
2480 return cnt;
2481}
2482
2483static ssize_t
2484tracing_max_lat_read(struct file *filp, char __user *ubuf,
2485 size_t cnt, loff_t *ppos)
2486{
2487 unsigned long *ptr = filp->private_data;
2488 char buf[64];
2489 int r;
2490
Steven Rostedtcffae432008-05-12 21:21:00 +02002491 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002492 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002493 if (r > sizeof(buf))
2494 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002495 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002496}
2497
2498static ssize_t
2499tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2500 size_t cnt, loff_t *ppos)
2501{
2502 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002503 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002504 long val;
2505 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002506
Steven Rostedtcffae432008-05-12 21:21:00 +02002507 if (cnt >= sizeof(buf))
2508 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002509
2510 if (copy_from_user(&buf, ubuf, cnt))
2511 return -EFAULT;
2512
2513 buf[cnt] = 0;
2514
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002515 ret = strict_strtoul(buf, 10, &val);
2516 if (ret < 0)
2517 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002518
2519 *ptr = val * 1000;
2520
2521 return cnt;
2522}
2523
Steven Rostedtb3806b42008-05-12 21:20:46 +02002524static atomic_t tracing_reader;
2525
2526static int tracing_open_pipe(struct inode *inode, struct file *filp)
2527{
2528 struct trace_iterator *iter;
2529
2530 if (tracing_disabled)
2531 return -ENODEV;
2532
2533 /* We only allow for reader of the pipe */
2534 if (atomic_inc_return(&tracing_reader) != 1) {
2535 atomic_dec(&tracing_reader);
2536 return -EBUSY;
2537 }
2538
2539 /* create a buffer to store the information to pass to userspace */
2540 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2541 if (!iter)
2542 return -ENOMEM;
2543
Steven Rostedt107bad82008-05-12 21:21:01 +02002544 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002545 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002546 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002547 filp->private_data = iter;
2548
Steven Rostedt107bad82008-05-12 21:21:01 +02002549 if (iter->trace->pipe_open)
2550 iter->trace->pipe_open(iter);
2551 mutex_unlock(&trace_types_lock);
2552
Steven Rostedtb3806b42008-05-12 21:20:46 +02002553 return 0;
2554}
2555
2556static int tracing_release_pipe(struct inode *inode, struct file *file)
2557{
2558 struct trace_iterator *iter = file->private_data;
2559
2560 kfree(iter);
2561 atomic_dec(&tracing_reader);
2562
2563 return 0;
2564}
2565
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002566static unsigned int
2567tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2568{
2569 struct trace_iterator *iter = filp->private_data;
2570
2571 if (trace_flags & TRACE_ITER_BLOCK) {
2572 /*
2573 * Always select as readable when in blocking mode
2574 */
2575 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002576 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002577 if (!trace_empty(iter))
2578 return POLLIN | POLLRDNORM;
2579 poll_wait(filp, &trace_wait, poll_table);
2580 if (!trace_empty(iter))
2581 return POLLIN | POLLRDNORM;
2582
2583 return 0;
2584 }
2585}
2586
Steven Rostedtb3806b42008-05-12 21:20:46 +02002587/*
2588 * Consumer reader.
2589 */
2590static ssize_t
2591tracing_read_pipe(struct file *filp, char __user *ubuf,
2592 size_t cnt, loff_t *ppos)
2593{
2594 struct trace_iterator *iter = filp->private_data;
2595 struct trace_array_cpu *data;
2596 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002597 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002598#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002599 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002600#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002601 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002602 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002603
2604 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002605 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2606 if (sret != -EBUSY)
2607 return sret;
2608 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002609
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002610 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002611
Steven Rostedt107bad82008-05-12 21:21:01 +02002612 mutex_lock(&trace_types_lock);
2613 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002614 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2615 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002616 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002617 }
2618
Steven Rostedtb3806b42008-05-12 21:20:46 +02002619 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002620
Steven Rostedt107bad82008-05-12 21:21:01 +02002621 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002622 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002623 goto out;
2624 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002625
Steven Rostedtb3806b42008-05-12 21:20:46 +02002626 /*
2627 * This is a make-shift waitqueue. The reason we don't use
2628 * an actual wait queue is because:
2629 * 1) we only ever have one waiter
2630 * 2) the tracing, traces all functions, we don't want
2631 * the overhead of calling wake_up and friends
2632 * (and tracing them too)
2633 * Anyway, this is really very primitive wakeup.
2634 */
2635 set_current_state(TASK_INTERRUPTIBLE);
2636 iter->tr->waiter = current;
2637
Steven Rostedt107bad82008-05-12 21:21:01 +02002638 mutex_unlock(&trace_types_lock);
2639
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002640 /* sleep for 100 msecs, and try again. */
2641 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002642
Steven Rostedt107bad82008-05-12 21:21:01 +02002643 mutex_lock(&trace_types_lock);
2644
Steven Rostedtb3806b42008-05-12 21:20:46 +02002645 iter->tr->waiter = NULL;
2646
Steven Rostedt107bad82008-05-12 21:21:01 +02002647 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002648 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002649 goto out;
2650 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002651
Steven Rostedt84527992008-05-12 21:20:58 +02002652 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002653 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002654
Steven Rostedtb3806b42008-05-12 21:20:46 +02002655 /*
2656 * We block until we read something and tracing is disabled.
2657 * We still block if tracing is disabled, but we have never
2658 * read anything. This allows a user to cat this file, and
2659 * then enable tracing. But after we have read something,
2660 * we give an EOF when tracing is again disabled.
2661 *
2662 * iter->pos will be 0 if we haven't read anything.
2663 */
2664 if (!tracer_enabled && iter->pos)
2665 break;
2666
2667 continue;
2668 }
2669
2670 /* stop when tracing is finished */
2671 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002672 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002673
2674 if (cnt >= PAGE_SIZE)
2675 cnt = PAGE_SIZE - 1;
2676
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002677 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002678 memset(&iter->seq, 0,
2679 sizeof(struct trace_iterator) -
2680 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002681 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002682
2683 /*
2684 * We need to stop all tracing on all CPUS to read the
2685 * the next buffer. This is a bit expensive, but is
2686 * not done often. We fill all what we can read,
2687 * and then release the locks again.
2688 */
2689
2690 cpus_clear(mask);
2691 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002692#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002693 ftrace_save = ftrace_enabled;
2694 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002695#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002696 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002697 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002698 data = iter->tr->data[cpu];
2699
2700 if (!head_page(data) || !data->trace_idx)
2701 continue;
2702
2703 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002704 cpu_set(cpu, mask);
2705 }
2706
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002707 for_each_cpu_mask(cpu, mask) {
2708 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002709 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002710
2711 if (data->overrun > iter->last_overrun[cpu])
2712 iter->overrun[cpu] +=
2713 data->overrun - iter->last_overrun[cpu];
2714 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002715 }
2716
Steven Rostedt088b1e42008-05-12 21:20:48 +02002717 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002718 int ret;
Steven Rostedt088b1e42008-05-12 21:20:48 +02002719 int len = iter->seq.len;
2720
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002721 ret = print_trace_line(iter);
Steven Rostedt088b1e42008-05-12 21:20:48 +02002722 if (!ret) {
2723 /* don't print partial lines */
2724 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002725 break;
Steven Rostedt088b1e42008-05-12 21:20:48 +02002726 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002727
2728 trace_consume(iter);
2729
2730 if (iter->seq.len >= cnt)
2731 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002732 }
2733
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002734 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002735 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002736 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002737 }
2738
2739 for_each_cpu_mask(cpu, mask) {
2740 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002741 atomic_dec(&data->disabled);
2742 }
Ingo Molnar25770462008-05-12 21:20:49 +02002743#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002744 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002745#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002746 local_irq_restore(flags);
2747
2748 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002749 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2750 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002751 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002752 if (sret == -EBUSY)
2753 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002754
Steven Rostedt107bad82008-05-12 21:21:01 +02002755out:
2756 mutex_unlock(&trace_types_lock);
2757
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002758 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002759}
2760
Steven Rostedta98a3c32008-05-12 21:20:59 +02002761static ssize_t
2762tracing_entries_read(struct file *filp, char __user *ubuf,
2763 size_t cnt, loff_t *ppos)
2764{
2765 struct trace_array *tr = filp->private_data;
2766 char buf[64];
2767 int r;
2768
2769 r = sprintf(buf, "%lu\n", tr->entries);
2770 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2771}
2772
2773static ssize_t
2774tracing_entries_write(struct file *filp, const char __user *ubuf,
2775 size_t cnt, loff_t *ppos)
2776{
2777 unsigned long val;
2778 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002779 int i, ret;
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002780 struct trace_array *tr = filp->private_data;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002781
Steven Rostedtcffae432008-05-12 21:21:00 +02002782 if (cnt >= sizeof(buf))
2783 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002784
2785 if (copy_from_user(&buf, ubuf, cnt))
2786 return -EFAULT;
2787
2788 buf[cnt] = 0;
2789
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002790 ret = strict_strtoul(buf, 10, &val);
2791 if (ret < 0)
2792 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002793
2794 /* must have at least 1 entry */
2795 if (!val)
2796 return -EINVAL;
2797
2798 mutex_lock(&trace_types_lock);
2799
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002800 if (tr->ctrl) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002801 cnt = -EBUSY;
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002802 pr_info("ftrace: please disable tracing"
Steven Rostedta98a3c32008-05-12 21:20:59 +02002803 " before modifying buffer size\n");
2804 goto out;
2805 }
2806
2807 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002808 long pages_requested;
2809 unsigned long freeable_pages;
2810
2811 /* make sure we have enough memory before mapping */
2812 pages_requested =
2813 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2814
2815 /* account for each buffer (and max_tr) */
2816 pages_requested *= tracing_nr_buffers * 2;
2817
2818 /* Check for overflow */
2819 if (pages_requested < 0) {
2820 cnt = -ENOMEM;
2821 goto out;
2822 }
2823
2824 freeable_pages = determine_dirtyable_memory();
2825
2826 /* we only allow to request 1/4 of useable memory */
2827 if (pages_requested >
2828 ((freeable_pages + tracing_pages_allocated) / 4)) {
2829 cnt = -ENOMEM;
2830 goto out;
2831 }
2832
Steven Rostedta98a3c32008-05-12 21:20:59 +02002833 while (global_trace.entries < val) {
2834 if (trace_alloc_page()) {
2835 cnt = -ENOMEM;
2836 goto out;
2837 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002838 /* double check that we don't go over the known pages */
2839 if (tracing_pages_allocated > pages_requested)
2840 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002841 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002842
Steven Rostedta98a3c32008-05-12 21:20:59 +02002843 } else {
2844 /* include the number of entries in val (inc of page entries) */
2845 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2846 trace_free_page();
2847 }
2848
Steven Rostedt19384c02008-05-22 00:22:16 -04002849 /* check integrity */
2850 for_each_tracing_cpu(i)
2851 check_pages(global_trace.data[i]);
2852
Steven Rostedta98a3c32008-05-12 21:20:59 +02002853 filp->f_pos += cnt;
2854
Steven Rostedt19384c02008-05-22 00:22:16 -04002855 /* If check pages failed, return ENOMEM */
2856 if (tracing_disabled)
2857 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002858 out:
2859 max_tr.entries = global_trace.entries;
2860 mutex_unlock(&trace_types_lock);
2861
2862 return cnt;
2863}
2864
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002865static int mark_printk(const char *fmt, ...)
2866{
2867 int ret;
2868 va_list args;
2869 va_start(args, fmt);
2870 ret = trace_vprintk(0, fmt, args);
2871 va_end(args);
2872 return ret;
2873}
2874
2875static ssize_t
2876tracing_mark_write(struct file *filp, const char __user *ubuf,
2877 size_t cnt, loff_t *fpos)
2878{
2879 char *buf;
2880 char *end;
2881 struct trace_array *tr = &global_trace;
2882
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002883 if (!tr->ctrl || tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002884 return -EINVAL;
2885
2886 if (cnt > TRACE_BUF_SIZE)
2887 cnt = TRACE_BUF_SIZE;
2888
2889 buf = kmalloc(cnt + 1, GFP_KERNEL);
2890 if (buf == NULL)
2891 return -ENOMEM;
2892
2893 if (copy_from_user(buf, ubuf, cnt)) {
2894 kfree(buf);
2895 return -EFAULT;
2896 }
2897
2898 /* Cut from the first nil or newline. */
2899 buf[cnt] = '\0';
2900 end = strchr(buf, '\n');
2901 if (end)
2902 *end = '\0';
2903
2904 cnt = mark_printk("%s\n", buf);
2905 kfree(buf);
2906 *fpos += cnt;
2907
2908 return cnt;
2909}
2910
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002911static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002912 .open = tracing_open_generic,
2913 .read = tracing_max_lat_read,
2914 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002915};
2916
2917static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002918 .open = tracing_open_generic,
2919 .read = tracing_ctrl_read,
2920 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002921};
2922
2923static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002924 .open = tracing_open_generic,
2925 .read = tracing_set_trace_read,
2926 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002927};
2928
Steven Rostedtb3806b42008-05-12 21:20:46 +02002929static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002930 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002931 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002932 .read = tracing_read_pipe,
2933 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002934};
2935
Steven Rostedta98a3c32008-05-12 21:20:59 +02002936static struct file_operations tracing_entries_fops = {
2937 .open = tracing_open_generic,
2938 .read = tracing_entries_read,
2939 .write = tracing_entries_write,
2940};
2941
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002942static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002943 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002944 .write = tracing_mark_write,
2945};
2946
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002947#ifdef CONFIG_DYNAMIC_FTRACE
2948
2949static ssize_t
2950tracing_read_long(struct file *filp, char __user *ubuf,
2951 size_t cnt, loff_t *ppos)
2952{
2953 unsigned long *p = filp->private_data;
2954 char buf[64];
2955 int r;
2956
2957 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002958
2959 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002960}
2961
2962static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002963 .open = tracing_open_generic,
2964 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002965};
2966#endif
2967
2968static struct dentry *d_tracer;
2969
2970struct dentry *tracing_init_dentry(void)
2971{
2972 static int once;
2973
2974 if (d_tracer)
2975 return d_tracer;
2976
2977 d_tracer = debugfs_create_dir("tracing", NULL);
2978
2979 if (!d_tracer && !once) {
2980 once = 1;
2981 pr_warning("Could not create debugfs directory 'tracing'\n");
2982 return NULL;
2983 }
2984
2985 return d_tracer;
2986}
2987
Steven Rostedt60a11772008-05-12 21:20:44 +02002988#ifdef CONFIG_FTRACE_SELFTEST
2989/* Let selftest have access to static functions in this file */
2990#include "trace_selftest.c"
2991#endif
2992
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01002993static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002994{
2995 struct dentry *d_tracer;
2996 struct dentry *entry;
2997
2998 d_tracer = tracing_init_dentry();
2999
3000 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3001 &global_trace, &tracing_ctrl_fops);
3002 if (!entry)
3003 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3004
3005 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3006 NULL, &tracing_iter_fops);
3007 if (!entry)
3008 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3009
Ingo Molnarc7078de2008-05-12 21:20:52 +02003010 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3011 NULL, &tracing_cpumask_fops);
3012 if (!entry)
3013 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3014
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003015 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3016 &global_trace, &tracing_lt_fops);
3017 if (!entry)
3018 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3019
3020 entry = debugfs_create_file("trace", 0444, d_tracer,
3021 &global_trace, &tracing_fops);
3022 if (!entry)
3023 pr_warning("Could not create debugfs 'trace' entry\n");
3024
3025 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3026 &global_trace, &show_traces_fops);
3027 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003028 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003029
3030 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3031 &global_trace, &set_tracer_fops);
3032 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003033 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003034
3035 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3036 &tracing_max_latency,
3037 &tracing_max_lat_fops);
3038 if (!entry)
3039 pr_warning("Could not create debugfs "
3040 "'tracing_max_latency' entry\n");
3041
3042 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3043 &tracing_thresh, &tracing_max_lat_fops);
3044 if (!entry)
3045 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003046 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003047 entry = debugfs_create_file("README", 0644, d_tracer,
3048 NULL, &tracing_readme_fops);
3049 if (!entry)
3050 pr_warning("Could not create debugfs 'README' entry\n");
3051
Steven Rostedtb3806b42008-05-12 21:20:46 +02003052 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3053 NULL, &tracing_pipe_fops);
3054 if (!entry)
3055 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003056 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003057
Steven Rostedta98a3c32008-05-12 21:20:59 +02003058 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3059 &global_trace, &tracing_entries_fops);
3060 if (!entry)
3061 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003062 "'trace_entries' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003063
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003064 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3065 NULL, &tracing_mark_fops);
3066 if (!entry)
3067 pr_warning("Could not create debugfs "
3068 "'trace_marker' entry\n");
3069
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003070#ifdef CONFIG_DYNAMIC_FTRACE
3071 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3072 &ftrace_update_tot_cnt,
3073 &tracing_read_long_fops);
3074 if (!entry)
3075 pr_warning("Could not create debugfs "
3076 "'dyn_ftrace_total_info' entry\n");
3077#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003078#ifdef CONFIG_SYSPROF_TRACER
3079 init_tracer_sysprof_debugfs(d_tracer);
3080#endif
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003081 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003082}
3083
Pekka Paalanen801fe402008-09-16 21:58:24 +03003084int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003085{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003086 static DEFINE_SPINLOCK(trace_buf_lock);
3087 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003088
3089 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003090 struct trace_array_cpu *data;
3091 struct trace_entry *entry;
3092 unsigned long flags;
3093 long disabled;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003094 int cpu, len = 0, write, written = 0;
3095
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003096 if (!tr->ctrl || tracing_disabled)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003097 return 0;
3098
3099 local_irq_save(flags);
3100 cpu = raw_smp_processor_id();
3101 data = tr->data[cpu];
3102 disabled = atomic_inc_return(&data->disabled);
3103
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003104 if (unlikely(disabled != 1))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003105 goto out;
3106
3107 spin_lock(&trace_buf_lock);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003108 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003109
3110 len = min(len, TRACE_BUF_SIZE-1);
3111 trace_buf[len] = 0;
3112
3113 __raw_spin_lock(&data->lock);
3114 entry = tracing_get_trace_entry(tr, data);
3115 tracing_generic_entry_update(entry, flags);
3116 entry->type = TRACE_PRINT;
3117 entry->field.print.ip = ip;
3118
3119 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3120
3121 memcpy(&entry->field.print.buf, trace_buf, write);
3122 entry->field.print.buf[write] = 0;
3123 written = write;
3124
3125 if (written != len)
3126 entry->field.flags |= TRACE_FLAG_CONT;
3127
3128 while (written != len) {
3129 entry = tracing_get_trace_entry(tr, data);
3130
3131 entry->type = TRACE_CONT;
3132 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3133 memcpy(&entry->cont.buf, trace_buf+written, write);
3134 entry->cont.buf[write] = 0;
3135 written += write;
3136 }
3137 __raw_spin_unlock(&data->lock);
3138
3139 spin_unlock(&trace_buf_lock);
3140
3141 out:
3142 atomic_dec(&data->disabled);
3143 local_irq_restore(flags);
3144
3145 return len;
3146}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003147EXPORT_SYMBOL_GPL(trace_vprintk);
3148
3149int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3150{
3151 int ret;
3152 va_list ap;
3153
3154 if (!(trace_flags & TRACE_ITER_PRINTK))
3155 return 0;
3156
3157 va_start(ap, fmt);
3158 ret = trace_vprintk(ip, fmt, ap);
3159 va_end(ap);
3160 return ret;
3161}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003162EXPORT_SYMBOL_GPL(__ftrace_printk);
3163
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003164static int trace_panic_handler(struct notifier_block *this,
3165 unsigned long event, void *unused)
3166{
3167 ftrace_dump();
3168 return NOTIFY_OK;
3169}
3170
3171static struct notifier_block trace_panic_notifier = {
3172 .notifier_call = trace_panic_handler,
3173 .next = NULL,
3174 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3175};
3176
3177static int trace_die_handler(struct notifier_block *self,
3178 unsigned long val,
3179 void *data)
3180{
3181 switch (val) {
3182 case DIE_OOPS:
3183 ftrace_dump();
3184 break;
3185 default:
3186 break;
3187 }
3188 return NOTIFY_OK;
3189}
3190
3191static struct notifier_block trace_die_notifier = {
3192 .notifier_call = trace_die_handler,
3193 .priority = 200
3194};
3195
3196/*
3197 * printk is set to max of 1024, we really don't need it that big.
3198 * Nothing should be printing 1000 characters anyway.
3199 */
3200#define TRACE_MAX_PRINT 1000
3201
3202/*
3203 * Define here KERN_TRACE so that we have one place to modify
3204 * it if we decide to change what log level the ftrace dump
3205 * should be at.
3206 */
3207#define KERN_TRACE KERN_INFO
3208
3209static void
3210trace_printk_seq(struct trace_seq *s)
3211{
3212 /* Probably should print a warning here. */
3213 if (s->len >= 1000)
3214 s->len = 1000;
3215
3216 /* should be zero ended, but we are paranoid. */
3217 s->buffer[s->len] = 0;
3218
3219 printk(KERN_TRACE "%s", s->buffer);
3220
3221 trace_seq_reset(s);
3222}
3223
3224
3225void ftrace_dump(void)
3226{
3227 static DEFINE_SPINLOCK(ftrace_dump_lock);
3228 /* use static because iter can be a bit big for the stack */
3229 static struct trace_iterator iter;
3230 struct trace_array_cpu *data;
3231 static cpumask_t mask;
3232 static int dump_ran;
3233 unsigned long flags;
3234 int cnt = 0;
3235 int cpu;
3236
3237 /* only one dump */
3238 spin_lock_irqsave(&ftrace_dump_lock, flags);
3239 if (dump_ran)
3240 goto out;
3241
3242 dump_ran = 1;
3243
3244 /* No turning back! */
3245 ftrace_kill_atomic();
3246
3247 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3248
3249 iter.tr = &global_trace;
3250 iter.trace = current_trace;
3251
3252 /*
3253 * We need to stop all tracing on all CPUS to read the
3254 * the next buffer. This is a bit expensive, but is
3255 * not done often. We fill all what we can read,
3256 * and then release the locks again.
3257 */
3258
3259 cpus_clear(mask);
3260
3261 for_each_tracing_cpu(cpu) {
3262 data = iter.tr->data[cpu];
3263
3264 if (!head_page(data) || !data->trace_idx)
3265 continue;
3266
3267 atomic_inc(&data->disabled);
3268 cpu_set(cpu, mask);
3269 }
3270
3271 for_each_cpu_mask(cpu, mask) {
3272 data = iter.tr->data[cpu];
3273 __raw_spin_lock(&data->lock);
3274
3275 if (data->overrun > iter.last_overrun[cpu])
3276 iter.overrun[cpu] +=
3277 data->overrun - iter.last_overrun[cpu];
3278 iter.last_overrun[cpu] = data->overrun;
3279 }
3280
3281 while (!trace_empty(&iter)) {
3282
3283 if (!cnt)
3284 printk(KERN_TRACE "---------------------------------\n");
3285
3286 cnt++;
3287
3288 /* reset all but tr, trace, and overruns */
3289 memset(&iter.seq, 0,
3290 sizeof(struct trace_iterator) -
3291 offsetof(struct trace_iterator, seq));
3292 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3293 iter.pos = -1;
3294
3295 if (find_next_entry_inc(&iter) != NULL) {
3296 print_trace_line(&iter);
3297 trace_consume(&iter);
3298 }
3299
3300 trace_printk_seq(&iter.seq);
3301 }
3302
3303 if (!cnt)
3304 printk(KERN_TRACE " (ftrace buffer empty)\n");
3305 else
3306 printk(KERN_TRACE "---------------------------------\n");
3307
3308 for_each_cpu_mask(cpu, mask) {
3309 data = iter.tr->data[cpu];
3310 __raw_spin_unlock(&data->lock);
3311 }
3312
3313 for_each_cpu_mask(cpu, mask) {
3314 data = iter.tr->data[cpu];
3315 atomic_dec(&data->disabled);
3316 }
3317
3318
3319 out:
3320 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3321}
3322
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003323static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003324{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003325 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003326 struct page *page, *tmp;
3327 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003328 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003329 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003330 int i;
3331
3332 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02003333 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003334 array = (void *)__get_free_page(GFP_KERNEL);
3335 if (array == NULL) {
3336 printk(KERN_ERR "tracer: failed to allocate page"
3337 "for trace buffer!\n");
3338 goto free_pages;
3339 }
3340
Steven Rostedt3eefae92008-05-12 21:21:04 +02003341 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003342 page = virt_to_page(array);
3343 list_add(&page->lru, &pages);
3344
3345/* Only allocate if we are actually using the max trace */
3346#ifdef CONFIG_TRACER_MAX_TRACE
3347 array = (void *)__get_free_page(GFP_KERNEL);
3348 if (array == NULL) {
3349 printk(KERN_ERR "tracer: failed to allocate page"
3350 "for trace buffer!\n");
3351 goto free_pages;
3352 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003353 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003354 page = virt_to_page(array);
3355 list_add(&page->lru, &pages);
3356#endif
3357 }
3358
3359 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02003360 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003361 data = global_trace.data[i];
3362 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003363 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003364 list_add_tail(&page->lru, &data->trace_pages);
3365 ClearPageLRU(page);
3366
3367#ifdef CONFIG_TRACER_MAX_TRACE
3368 data = max_tr.data[i];
3369 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003370 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003371 list_add_tail(&page->lru, &data->trace_pages);
3372 SetPageLRU(page);
3373#endif
3374 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003375 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003376 global_trace.entries += ENTRIES_PER_PAGE;
3377
3378 return 0;
3379
3380 free_pages:
3381 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003382 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003383 __free_page(page);
3384 }
3385 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003386}
3387
Steven Rostedta98a3c32008-05-12 21:20:59 +02003388static int trace_free_page(void)
3389{
3390 struct trace_array_cpu *data;
3391 struct page *page;
3392 struct list_head *p;
3393 int i;
3394 int ret = 0;
3395
3396 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02003397 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003398 data = global_trace.data[i];
3399 p = data->trace_pages.next;
3400 if (p == &data->trace_pages) {
3401 /* should never happen */
3402 WARN_ON(1);
3403 tracing_disabled = 1;
3404 ret = -1;
3405 break;
3406 }
3407 page = list_entry(p, struct page, lru);
3408 ClearPageLRU(page);
3409 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02003410 tracing_pages_allocated--;
3411 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003412 __free_page(page);
3413
3414 tracing_reset(data);
3415
3416#ifdef CONFIG_TRACER_MAX_TRACE
3417 data = max_tr.data[i];
3418 p = data->trace_pages.next;
3419 if (p == &data->trace_pages) {
3420 /* should never happen */
3421 WARN_ON(1);
3422 tracing_disabled = 1;
3423 ret = -1;
3424 break;
3425 }
3426 page = list_entry(p, struct page, lru);
3427 ClearPageLRU(page);
3428 list_del(&page->lru);
3429 __free_page(page);
3430
3431 tracing_reset(data);
3432#endif
3433 }
3434 global_trace.entries -= ENTRIES_PER_PAGE;
3435
3436 return ret;
3437}
3438
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003439__init static int tracer_alloc_buffers(void)
3440{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003441 struct trace_array_cpu *data;
3442 void *array;
3443 struct page *page;
3444 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02003445 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003446 int i;
3447
Steven Rostedtab464282008-05-12 21:21:00 +02003448 /* TODO: make the number of buffers hot pluggable with CPUS */
3449 tracing_nr_buffers = num_possible_cpus();
3450 tracing_buffer_mask = cpu_possible_map;
3451
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003452 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003453 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003454 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003455 max_tr.data[i] = &per_cpu(max_data, i);
3456
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003457 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003458 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003459 printk(KERN_ERR "tracer: failed to allocate page"
3460 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003461 goto free_buffers;
3462 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003463
3464 /* set the array to the list */
3465 INIT_LIST_HEAD(&data->trace_pages);
3466 page = virt_to_page(array);
3467 list_add(&page->lru, &data->trace_pages);
3468 /* use the LRU flag to differentiate the two buffers */
3469 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003470
Steven Rostedta98a3c32008-05-12 21:20:59 +02003471 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3472 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3473
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003474/* Only allocate if we are actually using the max trace */
3475#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003476 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003477 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003478 printk(KERN_ERR "tracer: failed to allocate page"
3479 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003480 goto free_buffers;
3481 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003482
3483 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3484 page = virt_to_page(array);
3485 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3486 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003487#endif
3488 }
3489
3490 /*
3491 * Since we allocate by orders of pages, we may be able to
3492 * round up a bit.
3493 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003494 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003495 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003496
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003497 while (global_trace.entries < trace_nr_entries) {
3498 if (trace_alloc_page())
3499 break;
3500 pages++;
3501 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003502 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003503
Jiri Slaby20764ff2008-06-12 11:27:03 +02003504 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3505 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003506 pr_info(" actual entries %ld\n", global_trace.entries);
3507
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003508 trace_init_cmdlines();
3509
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003510 register_tracer(&nop_trace);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003511#ifdef CONFIG_BOOT_TRACER
3512 register_tracer(&boot_tracer);
3513 current_trace = &boot_tracer;
3514 current_trace->init(&global_trace);
3515#else
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003516 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003517#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003518
Steven Rostedt60a11772008-05-12 21:20:44 +02003519 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003520 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003521 tracing_disabled = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003522 atomic_notifier_chain_register(&panic_notifier_list,
3523 &trace_panic_notifier);
3524
3525 register_die_notifier(&trace_die_notifier);
3526
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003527 return 0;
3528
3529 free_buffers:
3530 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003531 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003532 struct trace_array_cpu *data = global_trace.data[i];
3533
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003534 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003535 list_for_each_entry_safe(page, tmp,
3536 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003537 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003538 __free_page(page);
3539 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003540 }
3541
3542#ifdef CONFIG_TRACER_MAX_TRACE
3543 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003544 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003545 list_for_each_entry_safe(page, tmp,
3546 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003547 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003548 __free_page(page);
3549 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003550 }
3551#endif
3552 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003553 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003554}
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003555early_initcall(tracer_alloc_buffers);
3556fs_initcall(tracer_init_debugfs);