blob: 8a00d6c5c0f5eab2d2318ae66e3d513870e4fbd4 [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
Ankita Garg2b1bce12008-06-09 14:10:25 +0530145static notrace void no_trace_init(struct trace_array *tr)
146{
147 int cpu;
148
Steven Rostedt60bc0802008-07-10 20:58:16 -0400149 ftrace_function_enabled = 0;
Ankita Garg2b1bce12008-06-09 14:10:25 +0530150 if(tr->ctrl)
151 for_each_online_cpu(cpu)
152 tracing_reset(tr->data[cpu]);
153 tracer_enabled = 0;
154}
155
156/* dummy trace to disable tracing */
157static struct tracer no_tracer __read_mostly = {
158 .name = "none",
159 .init = no_trace_init
160};
161
162
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200163/**
164 * trace_wake_up - wake up tasks waiting for trace input
165 *
166 * Simply wakes up any task that is blocked on the trace_wait
167 * queue. These is used with trace_poll for tasks polling the trace.
168 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200169void trace_wake_up(void)
170{
Ingo Molnar017730c2008-05-12 21:20:52 +0200171 /*
172 * The runqueue_is_locked() can fail, but this is the best we
173 * have for now:
174 */
175 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200176 wake_up(&trace_wait);
177}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200178
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200179#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
180
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200181static int __init set_nr_entries(char *str)
182{
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200183 unsigned long nr_entries;
184 int ret;
185
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200186 if (!str)
187 return 0;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200188 ret = strict_strtoul(str, 0, &nr_entries);
189 /* nr_entries can not be zero */
190 if (ret < 0 || nr_entries == 0)
191 return 0;
192 trace_nr_entries = nr_entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200193 return 1;
194}
195__setup("trace_entries=", set_nr_entries);
196
Steven Rostedt57f50be2008-05-12 21:20:44 +0200197unsigned long nsecs_to_usecs(unsigned long nsecs)
198{
199 return nsecs / 1000;
200}
201
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200202/*
203 * trace_flag_type is an enumeration that holds different
204 * states when a trace occurs. These are:
205 * IRQS_OFF - interrupts were disabled
206 * NEED_RESCED - reschedule is requested
207 * HARDIRQ - inside an interrupt handler
208 * SOFTIRQ - inside a softirq handler
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400209 * CONT - multiple entries hold the trace item
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200210 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200211enum trace_flag_type {
212 TRACE_FLAG_IRQS_OFF = 0x01,
213 TRACE_FLAG_NEED_RESCHED = 0x02,
214 TRACE_FLAG_HARDIRQ = 0x04,
215 TRACE_FLAG_SOFTIRQ = 0x08,
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400216 TRACE_FLAG_CONT = 0x10,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200217};
218
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200219/*
220 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
221 * control the output of kernel symbols.
222 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200223#define TRACE_ITER_SYM_MASK \
224 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
225
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200226/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200227static const char *trace_options[] = {
228 "print-parent",
229 "sym-offset",
230 "sym-addr",
231 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200232 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200233 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200234 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200235 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200236 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200237 "sched-tree",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200238 "ftrace_printk",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200239 NULL
240};
241
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200242/*
243 * ftrace_max_lock is used to protect the swapping of buffers
244 * when taking a max snapshot. The buffers themselves are
245 * protected by per_cpu spinlocks. But the action of the swap
246 * needs its own lock.
247 *
248 * This is defined as a raw_spinlock_t in order to help
249 * with performance when lockdep debugging is enabled.
250 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200251static raw_spinlock_t ftrace_max_lock =
252 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200253
254/*
255 * Copy the new maximum trace into the separate maximum-trace
256 * structure. (this way the maximum trace is permanently saved,
257 * for later retrieval via /debugfs/tracing/latency_trace)
258 */
Ingo Molnare309b412008-05-12 21:20:51 +0200259static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200260__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
261{
262 struct trace_array_cpu *data = tr->data[cpu];
263
264 max_tr.cpu = cpu;
265 max_tr.time_start = data->preempt_timestamp;
266
267 data = max_tr.data[cpu];
268 data->saved_latency = tracing_max_latency;
269
270 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
271 data->pid = tsk->pid;
272 data->uid = tsk->uid;
273 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
274 data->policy = tsk->policy;
275 data->rt_priority = tsk->rt_priority;
276
277 /* record this tasks comm */
278 tracing_record_cmdline(current);
279}
280
Steven Rostedt19384c02008-05-22 00:22:16 -0400281#define CHECK_COND(cond) \
282 if (unlikely(cond)) { \
283 tracing_disabled = 1; \
284 WARN_ON(1); \
285 return -1; \
286 }
287
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200288/**
289 * check_pages - integrity check of trace buffers
290 *
291 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400292 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200293 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400294int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200295{
296 struct page *page, *tmp;
297
Steven Rostedt19384c02008-05-22 00:22:16 -0400298 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
299 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200300
301 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400302 CHECK_COND(page->lru.next->prev != &page->lru);
303 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200304 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400305
306 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200307}
308
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200309/**
310 * head_page - page address of the first page in per_cpu buffer.
311 *
312 * head_page returns the page address of the first page in
313 * a per_cpu buffer. This also preforms various consistency
314 * checks to make sure the buffer has not been corrupted.
315 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200316void *head_page(struct trace_array_cpu *data)
317{
318 struct page *page;
319
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200320 if (list_empty(&data->trace_pages))
321 return NULL;
322
323 page = list_entry(data->trace_pages.next, struct page, lru);
324 BUG_ON(&page->lru == &data->trace_pages);
325
326 return page_address(page);
327}
328
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200329/**
330 * trace_seq_printf - sequence printing of trace information
331 * @s: trace sequence descriptor
332 * @fmt: printf format string
333 *
334 * The tracer may use either sequence operations or its own
335 * copy to user routines. To simplify formating of a trace
336 * trace_seq_printf is used to store strings into a special
337 * buffer (@s). Then the output may be either used by
338 * the sequencer or pulled into another buffer.
339 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200340int
Steven Rostedt214023c2008-05-12 21:20:46 +0200341trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
342{
343 int len = (PAGE_SIZE - 1) - s->len;
344 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200345 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200346
347 if (!len)
348 return 0;
349
350 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200351 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200352 va_end(ap);
353
Steven Rostedtb3806b42008-05-12 21:20:46 +0200354 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200355 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200356 return 0;
357
358 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200359
360 return len;
361}
362
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200363/**
364 * trace_seq_puts - trace sequence printing of simple string
365 * @s: trace sequence descriptor
366 * @str: simple string to record
367 *
368 * The tracer may use either the sequence operations or its own
369 * copy to user routines. This function records a simple string
370 * into a special buffer (@s) for later retrieval by a sequencer
371 * or other mechanism.
372 */
Ingo Molnare309b412008-05-12 21:20:51 +0200373static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200374trace_seq_puts(struct trace_seq *s, const char *str)
375{
376 int len = strlen(str);
377
378 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200379 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200380
381 memcpy(s->buffer + s->len, str, len);
382 s->len += len;
383
384 return len;
385}
386
Ingo Molnare309b412008-05-12 21:20:51 +0200387static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200388trace_seq_putc(struct trace_seq *s, unsigned char c)
389{
390 if (s->len >= (PAGE_SIZE - 1))
391 return 0;
392
393 s->buffer[s->len++] = c;
394
395 return 1;
396}
397
Ingo Molnare309b412008-05-12 21:20:51 +0200398static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200399trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
400{
401 if (len > ((PAGE_SIZE - 1) - s->len))
402 return 0;
403
404 memcpy(s->buffer + s->len, mem, len);
405 s->len += len;
406
407 return len;
408}
409
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200410#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200411static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200412
Ingo Molnare309b412008-05-12 21:20:51 +0200413static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200414trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
415{
416 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200417 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200418 unsigned char byte;
419 int i, j;
420
421 BUG_ON(len >= HEX_CHARS);
422
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200423#ifdef __BIG_ENDIAN
424 for (i = 0, j = 0; i < len; i++) {
425#else
426 for (i = len-1, j = 0; i >= 0; i--) {
427#endif
428 byte = data[i];
429
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200430 hex[j++] = hex2asc[byte & 0x0f];
431 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200432 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200433 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200434
435 return trace_seq_putmem(s, hex, j);
436}
437
Ingo Molnare309b412008-05-12 21:20:51 +0200438static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200439trace_seq_reset(struct trace_seq *s)
440{
441 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200442 s->readpos = 0;
443}
444
445ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
446{
447 int len;
448 int ret;
449
450 if (s->len <= s->readpos)
451 return -EBUSY;
452
453 len = s->len - s->readpos;
454 if (cnt > len)
455 cnt = len;
456 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
457 if (ret)
458 return -EFAULT;
459
460 s->readpos += len;
461 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200462}
463
Ingo Molnare309b412008-05-12 21:20:51 +0200464static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200465trace_print_seq(struct seq_file *m, struct trace_seq *s)
466{
467 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
468
469 s->buffer[len] = 0;
470 seq_puts(m, s->buffer);
471
472 trace_seq_reset(s);
473}
474
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200475/*
476 * flip the trace buffers between two trace descriptors.
477 * This usually is the buffers between the global_trace and
478 * the max_tr to record a snapshot of a current trace.
479 *
480 * The ftrace_max_lock must be held.
481 */
Ingo Molnare309b412008-05-12 21:20:51 +0200482static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200483flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
484{
485 struct list_head flip_pages;
486
487 INIT_LIST_HEAD(&flip_pages);
488
Steven Rostedt93a588f2008-05-12 21:20:45 +0200489 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200490 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200491 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200492
493 check_pages(tr1);
494 check_pages(tr2);
495 list_splice_init(&tr1->trace_pages, &flip_pages);
496 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
497 list_splice_init(&flip_pages, &tr2->trace_pages);
498 BUG_ON(!list_empty(&flip_pages));
499 check_pages(tr1);
500 check_pages(tr2);
501}
502
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200503/**
504 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
505 * @tr: tracer
506 * @tsk: the task with the latency
507 * @cpu: The cpu that initiated the trace.
508 *
509 * Flip the buffers between the @tr and the max_tr and record information
510 * about which task was the cause of this latency.
511 */
Ingo Molnare309b412008-05-12 21:20:51 +0200512void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200513update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
514{
515 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200516 int i;
517
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200518 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200519 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200520 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200521 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200522 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200523 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200524 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200525 }
526
527 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200528 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200529}
530
531/**
532 * update_max_tr_single - only copy one trace over, and reset the rest
533 * @tr - tracer
534 * @tsk - task with the latency
535 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200536 *
537 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200538 */
Ingo Molnare309b412008-05-12 21:20:51 +0200539void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200540update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
541{
542 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200543 int i;
544
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200545 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200546 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200547 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200548 tracing_reset(max_tr.data[i]);
549
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200550 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200551 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200552
553 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200554 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200555}
556
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200557/**
558 * register_tracer - register a tracer with the ftrace system.
559 * @type - the plugin for the tracer
560 *
561 * Register a new plugin tracer.
562 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200563int register_tracer(struct tracer *type)
564{
565 struct tracer *t;
566 int len;
567 int ret = 0;
568
569 if (!type->name) {
570 pr_info("Tracer must have a name\n");
571 return -1;
572 }
573
574 mutex_lock(&trace_types_lock);
575 for (t = trace_types; t; t = t->next) {
576 if (strcmp(type->name, t->name) == 0) {
577 /* already found */
578 pr_info("Trace %s already registered\n",
579 type->name);
580 ret = -1;
581 goto out;
582 }
583 }
584
Steven Rostedt60a11772008-05-12 21:20:44 +0200585#ifdef CONFIG_FTRACE_STARTUP_TEST
586 if (type->selftest) {
587 struct tracer *saved_tracer = current_trace;
588 struct trace_array_cpu *data;
589 struct trace_array *tr = &global_trace;
590 int saved_ctrl = tr->ctrl;
591 int i;
592 /*
593 * Run a selftest on this tracer.
594 * Here we reset the trace buffer, and set the current
595 * tracer to be this tracer. The tracer can then run some
596 * internal tracing to verify that everything is in order.
597 * If we fail, we do not register this tracer.
598 */
Steven Rostedtab464282008-05-12 21:21:00 +0200599 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200600 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200601 if (!head_page(data))
602 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200603 tracing_reset(data);
604 }
605 current_trace = type;
606 tr->ctrl = 0;
607 /* the test is responsible for initializing and enabling */
608 pr_info("Testing tracer %s: ", type->name);
609 ret = type->selftest(type, tr);
610 /* the test is responsible for resetting too */
611 current_trace = saved_tracer;
612 tr->ctrl = saved_ctrl;
613 if (ret) {
614 printk(KERN_CONT "FAILED!\n");
615 goto out;
616 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200617 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200618 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200619 data = tr->data[i];
620 if (!head_page(data))
621 continue;
622 tracing_reset(data);
623 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200624 printk(KERN_CONT "PASSED\n");
625 }
626#endif
627
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200628 type->next = trace_types;
629 trace_types = type;
630 len = strlen(type->name);
631 if (len > max_tracer_type_len)
632 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200633
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200634 out:
635 mutex_unlock(&trace_types_lock);
636
637 return ret;
638}
639
640void unregister_tracer(struct tracer *type)
641{
642 struct tracer **t;
643 int len;
644
645 mutex_lock(&trace_types_lock);
646 for (t = &trace_types; *t; t = &(*t)->next) {
647 if (*t == type)
648 goto found;
649 }
650 pr_info("Trace %s not registered\n", type->name);
651 goto out;
652
653 found:
654 *t = (*t)->next;
655 if (strlen(type->name) != max_tracer_type_len)
656 goto out;
657
658 max_tracer_type_len = 0;
659 for (t = &trace_types; *t; t = &(*t)->next) {
660 len = strlen((*t)->name);
661 if (len > max_tracer_type_len)
662 max_tracer_type_len = len;
663 }
664 out:
665 mutex_unlock(&trace_types_lock);
666}
667
Ingo Molnare309b412008-05-12 21:20:51 +0200668void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200669{
670 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200671 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200672 data->trace_head = data->trace_tail = head_page(data);
673 data->trace_head_idx = 0;
674 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200675}
676
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200677#define SAVED_CMDLINES 128
678static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
679static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
680static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
681static int cmdline_idx;
682static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200683
Steven Rostedt25b0b442008-05-12 21:21:00 +0200684/* temporary disable recording */
685atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200686
687static void trace_init_cmdlines(void)
688{
689 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
690 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
691 cmdline_idx = 0;
692}
693
Ingo Molnare309b412008-05-12 21:20:51 +0200694void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200695
Ingo Molnare309b412008-05-12 21:20:51 +0200696static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200697{
698 unsigned map;
699 unsigned idx;
700
701 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
702 return;
703
704 /*
705 * It's not the end of the world if we don't get
706 * the lock, but we also don't want to spin
707 * nor do we want to disable interrupts,
708 * so if we miss here, then better luck next time.
709 */
710 if (!spin_trylock(&trace_cmdline_lock))
711 return;
712
713 idx = map_pid_to_cmdline[tsk->pid];
714 if (idx >= SAVED_CMDLINES) {
715 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
716
717 map = map_cmdline_to_pid[idx];
718 if (map <= PID_MAX_DEFAULT)
719 map_pid_to_cmdline[map] = (unsigned)-1;
720
721 map_pid_to_cmdline[tsk->pid] = idx;
722
723 cmdline_idx = idx;
724 }
725
726 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
727
728 spin_unlock(&trace_cmdline_lock);
729}
730
Ingo Molnare309b412008-05-12 21:20:51 +0200731static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200732{
733 char *cmdline = "<...>";
734 unsigned map;
735
736 if (!pid)
737 return "<idle>";
738
739 if (pid > PID_MAX_DEFAULT)
740 goto out;
741
742 map = map_pid_to_cmdline[pid];
743 if (map >= SAVED_CMDLINES)
744 goto out;
745
746 cmdline = saved_cmdlines[map];
747
748 out:
749 return cmdline;
750}
751
Ingo Molnare309b412008-05-12 21:20:51 +0200752void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200753{
754 if (atomic_read(&trace_record_cmdline_disabled))
755 return;
756
757 trace_save_cmdline(tsk);
758}
759
Ingo Molnare309b412008-05-12 21:20:51 +0200760static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200761trace_next_list(struct trace_array_cpu *data, struct list_head *next)
762{
763 /*
764 * Roundrobin - but skip the head (which is not a real page):
765 */
766 next = next->next;
767 if (unlikely(next == &data->trace_pages))
768 next = next->next;
769 BUG_ON(next == &data->trace_pages);
770
771 return next;
772}
773
Ingo Molnare309b412008-05-12 21:20:51 +0200774static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200775trace_next_page(struct trace_array_cpu *data, void *addr)
776{
777 struct list_head *next;
778 struct page *page;
779
780 page = virt_to_page(addr);
781
782 next = trace_next_list(data, &page->lru);
783 page = list_entry(next, struct page, lru);
784
785 return page_address(page);
786}
787
Ingo Molnare309b412008-05-12 21:20:51 +0200788static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200789tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200790{
791 unsigned long idx, idx_next;
792 struct trace_entry *entry;
793
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200794 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200795 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200796 idx_next = idx + 1;
797
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200798 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
799
Steven Rostedt93a588f2008-05-12 21:20:45 +0200800 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200801
802 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200803 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200804 idx_next = 0;
805 }
806
Steven Rostedt93a588f2008-05-12 21:20:45 +0200807 if (data->trace_head == data->trace_tail &&
808 idx_next == data->trace_tail_idx) {
809 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200810 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200811 data->trace_tail_idx++;
812 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
813 data->trace_tail =
814 trace_next_page(data, data->trace_tail);
815 data->trace_tail_idx = 0;
816 }
817 }
818
819 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200820
821 return entry;
822}
823
Ingo Molnare309b412008-05-12 21:20:51 +0200824static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200825tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200826{
827 struct task_struct *tsk = current;
828 unsigned long pc;
829
830 pc = preempt_count();
831
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400832 entry->field.preempt_count = pc & 0xff;
833 entry->field.pid = (tsk) ? tsk->pid : 0;
834 entry->field.t = ftrace_now(raw_smp_processor_id());
835 entry->field.flags =
836 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200837 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
838 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
839 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
840}
841
Ingo Molnare309b412008-05-12 21:20:51 +0200842void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200843trace_function(struct trace_array *tr, struct trace_array_cpu *data,
844 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200845{
846 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200847 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200848
Steven Rostedt92205c22008-05-12 21:20:55 +0200849 raw_local_irq_save(irq_flags);
850 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400851 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200852 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400853 entry->type = TRACE_FN;
854 entry->field.fn.ip = ip;
855 entry->field.fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200856 __raw_spin_unlock(&data->lock);
857 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200858}
859
Ingo Molnare309b412008-05-12 21:20:51 +0200860void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200861ftrace(struct trace_array *tr, struct trace_array_cpu *data,
862 unsigned long ip, unsigned long parent_ip, unsigned long flags)
863{
864 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200865 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200866}
867
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200868#ifdef CONFIG_MMIOTRACE
869void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
870 struct mmiotrace_rw *rw)
871{
872 struct trace_entry *entry;
873 unsigned long irq_flags;
874
Ingo Molnar801a1752008-05-12 21:20:58 +0200875 raw_local_irq_save(irq_flags);
876 __raw_spin_lock(&data->lock);
877
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400878 entry = tracing_get_trace_entry(tr, data);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200879 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400880 entry->type = TRACE_MMIO_RW;
881 entry->field.mmiorw = *rw;
Ingo Molnar801a1752008-05-12 21:20:58 +0200882
883 __raw_spin_unlock(&data->lock);
884 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200885
886 trace_wake_up();
887}
888
889void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
890 struct mmiotrace_map *map)
891{
892 struct trace_entry *entry;
893 unsigned long irq_flags;
894
Ingo Molnar801a1752008-05-12 21:20:58 +0200895 raw_local_irq_save(irq_flags);
896 __raw_spin_lock(&data->lock);
897
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400898 entry = tracing_get_trace_entry(tr, data);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200899 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400900 entry->type = TRACE_MMIO_MAP;
901 entry->field.mmiomap = *map;
Ingo Molnar801a1752008-05-12 21:20:58 +0200902
903 __raw_spin_unlock(&data->lock);
904 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200905
906 trace_wake_up();
907}
908#endif
909
Ingo Molnar86387f72008-05-12 21:20:51 +0200910void __trace_stack(struct trace_array *tr,
911 struct trace_array_cpu *data,
912 unsigned long flags,
913 int skip)
914{
915 struct trace_entry *entry;
916 struct stack_trace trace;
917
918 if (!(trace_flags & TRACE_ITER_STACKTRACE))
919 return;
920
921 entry = tracing_get_trace_entry(tr, data);
922 tracing_generic_entry_update(entry, flags);
923 entry->type = TRACE_STACK;
924
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400925 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
Ingo Molnar86387f72008-05-12 21:20:51 +0200926
927 trace.nr_entries = 0;
928 trace.max_entries = FTRACE_STACK_ENTRIES;
929 trace.skip = skip;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400930 trace.entries = entry->field.stack.caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200931
932 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200933}
934
Ingo Molnare309b412008-05-12 21:20:51 +0200935void
Ingo Molnara4feb8342008-05-12 21:21:02 +0200936__trace_special(void *__tr, void *__data,
937 unsigned long arg1, unsigned long arg2, unsigned long arg3)
938{
939 struct trace_array_cpu *data = __data;
940 struct trace_array *tr = __tr;
941 struct trace_entry *entry;
942 unsigned long irq_flags;
943
944 raw_local_irq_save(irq_flags);
945 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400946 entry = tracing_get_trace_entry(tr, data);
Ingo Molnara4feb8342008-05-12 21:21:02 +0200947 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400948 entry->type = TRACE_SPECIAL;
949 entry->field.special.arg1 = arg1;
950 entry->field.special.arg2 = arg2;
951 entry->field.special.arg3 = arg3;
Ingo Molnara4feb8342008-05-12 21:21:02 +0200952 __trace_stack(tr, data, irq_flags, 4);
953 __raw_spin_unlock(&data->lock);
954 raw_local_irq_restore(irq_flags);
955
956 trace_wake_up();
957}
958
959void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200960tracing_sched_switch_trace(struct trace_array *tr,
961 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200962 struct task_struct *prev,
963 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200964 unsigned long flags)
965{
966 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200967 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200968
Steven Rostedt92205c22008-05-12 21:20:55 +0200969 raw_local_irq_save(irq_flags);
970 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400971 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200972 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400973 entry->type = TRACE_CTX;
974 entry->field.ctx.prev_pid = prev->pid;
975 entry->field.ctx.prev_prio = prev->prio;
976 entry->field.ctx.prev_state = prev->state;
977 entry->field.ctx.next_pid = next->pid;
978 entry->field.ctx.next_prio = next->prio;
979 entry->field.ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200980 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200981 __raw_spin_unlock(&data->lock);
982 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200983}
984
Ingo Molnar57422792008-05-12 21:20:51 +0200985void
986tracing_sched_wakeup_trace(struct trace_array *tr,
987 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200988 struct task_struct *wakee,
989 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200990 unsigned long flags)
991{
992 struct trace_entry *entry;
993 unsigned long irq_flags;
994
Steven Rostedt92205c22008-05-12 21:20:55 +0200995 raw_local_irq_save(irq_flags);
996 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200997 entry = tracing_get_trace_entry(tr, data);
998 tracing_generic_entry_update(entry, flags);
999 entry->type = TRACE_WAKE;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001000 entry->field.ctx.prev_pid = curr->pid;
1001 entry->field.ctx.prev_prio = curr->prio;
1002 entry->field.ctx.prev_state = curr->state;
1003 entry->field.ctx.next_pid = wakee->pid;
1004 entry->field.ctx.next_prio = wakee->prio;
1005 entry->field.ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +02001006 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +02001007 __raw_spin_unlock(&data->lock);
1008 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +02001009
1010 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +02001011}
1012
Steven Rostedt4902f882008-05-22 00:22:18 -04001013void
1014ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1015{
1016 struct trace_array *tr = &global_trace;
1017 struct trace_array_cpu *data;
1018 unsigned long flags;
1019 long disabled;
1020 int cpu;
1021
1022 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1023 return;
1024
1025 local_irq_save(flags);
1026 cpu = raw_smp_processor_id();
1027 data = tr->data[cpu];
1028 disabled = atomic_inc_return(&data->disabled);
1029
1030 if (likely(disabled == 1))
1031 __trace_special(tr, data, arg1, arg2, arg3);
1032
1033 atomic_dec(&data->disabled);
1034 local_irq_restore(flags);
1035}
1036
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001037#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +02001038static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001039function_trace_call(unsigned long ip, unsigned long parent_ip)
1040{
1041 struct trace_array *tr = &global_trace;
1042 struct trace_array_cpu *data;
1043 unsigned long flags;
1044 long disabled;
1045 int cpu;
1046
Steven Rostedt60bc0802008-07-10 20:58:16 -04001047 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001048 return;
1049
Abhishek Sagarecea6562008-06-21 23:47:53 +05301050 if (skip_trace(ip))
1051 return;
1052
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001053 local_irq_save(flags);
1054 cpu = raw_smp_processor_id();
1055 data = tr->data[cpu];
1056 disabled = atomic_inc_return(&data->disabled);
1057
1058 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +02001059 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001060
1061 atomic_dec(&data->disabled);
1062 local_irq_restore(flags);
1063}
1064
1065static struct ftrace_ops trace_ops __read_mostly =
1066{
1067 .func = function_trace_call,
1068};
1069
Ingo Molnare309b412008-05-12 21:20:51 +02001070void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001071{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001072 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001073 register_ftrace_function(&trace_ops);
Steven Rostedt60bc0802008-07-10 20:58:16 -04001074 if (tracer_enabled)
1075 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001076}
1077
Ingo Molnare309b412008-05-12 21:20:51 +02001078void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001079{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001080 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001081 unregister_ftrace_function(&trace_ops);
1082}
1083#endif
1084
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001085enum trace_file_type {
1086 TRACE_FILE_LAT_FMT = 1,
1087};
1088
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001089/* Return the current entry. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001090static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001091trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1092 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001093{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001094 struct page *page;
1095 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001096
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001097 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001098 iter->next_idx[cpu] >= data->trace_idx ||
1099 (data->trace_head == data->trace_tail &&
1100 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001101 return NULL;
1102
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001103 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001104 /* Initialize the iterator for this cpu trace buffer */
1105 WARN_ON(!data->trace_tail);
1106 page = virt_to_page(data->trace_tail);
1107 iter->next_page[cpu] = &page->lru;
1108 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001109 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001110
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001111 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001112 BUG_ON(&data->trace_pages == &page->lru);
1113
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001114 array = page_address(page);
1115
Steven Rostedt93a588f2008-05-12 21:20:45 +02001116 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001117 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118}
1119
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001120/* Increment the index counter of an iterator by one */
Steven Rostedt5a90f572008-09-03 17:42:51 -04001121static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001122{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001123 iter->next_idx[cpu]++;
1124 iter->next_page_idx[cpu]++;
1125
1126 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1127 struct trace_array_cpu *data = iter->tr->data[cpu];
1128
1129 iter->next_page_idx[cpu] = 0;
1130 iter->next_page[cpu] =
1131 trace_next_list(data, iter->next_page[cpu]);
1132 }
1133}
1134
Steven Rostedt5a90f572008-09-03 17:42:51 -04001135static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1136{
1137 iter->idx++;
1138 __trace_iterator_increment(iter, cpu);
1139}
1140
Ingo Molnare309b412008-05-12 21:20:51 +02001141static struct trace_entry *
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001142trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1143 struct trace_iterator *iter, int cpu)
1144{
1145 struct list_head *next_page;
1146 struct trace_entry *ent;
1147 int idx, next_idx, next_page_idx;
1148
1149 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1150
1151 if (likely(!ent || ent->type != TRACE_CONT))
1152 return ent;
1153
1154 /* save the iterator details */
1155 idx = iter->idx;
1156 next_idx = iter->next_idx[cpu];
1157 next_page_idx = iter->next_page_idx[cpu];
1158 next_page = iter->next_page[cpu];
1159
1160 /* find a real entry */
1161 do {
Steven Rostedt5a90f572008-09-03 17:42:51 -04001162 __trace_iterator_increment(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001163 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1164 } while (ent && ent->type != TRACE_CONT);
1165
1166 /* reset the iterator */
1167 iter->idx = idx;
1168 iter->next_idx[cpu] = next_idx;
1169 iter->next_page_idx[cpu] = next_page_idx;
1170 iter->next_page[cpu] = next_page;
1171
1172 return ent;
1173}
1174
1175static struct trace_entry *
1176__find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177{
1178 struct trace_array *tr = iter->tr;
1179 struct trace_entry *ent, *next = NULL;
1180 int next_cpu = -1;
1181 int cpu;
1182
Steven Rostedtab464282008-05-12 21:21:00 +02001183 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001184 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001186
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001187 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001188
1189 if (ent && ent->type == TRACE_CONT) {
1190 struct trace_array_cpu *data = tr->data[cpu];
1191
1192 if (!inc)
1193 ent = trace_entry_next(tr, data, iter, cpu);
1194 else {
1195 while (ent && ent->type == TRACE_CONT) {
Steven Rostedt5a90f572008-09-03 17:42:51 -04001196 __trace_iterator_increment(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001197 ent = trace_entry_idx(tr, tr->data[cpu],
1198 iter, cpu);
1199 }
1200 }
1201 }
1202
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001203 /*
1204 * Pick the entry with the smallest timestamp:
1205 */
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001206 if (ent && (!next || ent->field.t < next->field.t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001207 next = ent;
1208 next_cpu = cpu;
1209 }
1210 }
1211
1212 if (ent_cpu)
1213 *ent_cpu = next_cpu;
1214
1215 return next;
1216}
1217
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001218/* Find the next real entry, without updating the iterator itself */
1219static struct trace_entry *
1220find_next_entry(struct trace_iterator *iter, int *ent_cpu)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001221{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001222 return __find_next_entry(iter, ent_cpu, 0);
1223}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001224
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001225/* Find the next real entry, and increment the iterator to the next entry */
1226static void *find_next_entry_inc(struct trace_iterator *iter)
1227{
1228 struct trace_entry *next;
1229 int next_cpu = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001230
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001231 next = __find_next_entry(iter, &next_cpu, 1);
1232
1233 iter->prev_ent = iter->ent;
1234 iter->prev_cpu = iter->cpu;
1235
1236 iter->ent = next;
1237 iter->cpu = next_cpu;
1238
1239 if (next)
1240 trace_iterator_increment(iter, iter->cpu);
1241
1242 return next ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001243}
1244
Ingo Molnare309b412008-05-12 21:20:51 +02001245static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001246{
1247 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001248 struct trace_entry *ent;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001249
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001250 again:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001251 data->trace_tail_idx++;
1252 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1253 data->trace_tail = trace_next_page(data, data->trace_tail);
1254 data->trace_tail_idx = 0;
1255 }
1256
1257 /* Check if we empty it, then reset the index */
1258 if (data->trace_head == data->trace_tail &&
1259 data->trace_head_idx == data->trace_tail_idx)
1260 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001261
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001262 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1263 iter, iter->cpu);
1264 if (ent && ent->type == TRACE_CONT)
1265 goto again;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001266}
1267
Ingo Molnare309b412008-05-12 21:20:51 +02001268static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001269{
1270 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001271 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001272 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001273
1274 (*pos)++;
1275
1276 /* can't go backwards */
1277 if (iter->idx > i)
1278 return NULL;
1279
1280 if (iter->idx < 0)
1281 ent = find_next_entry_inc(iter);
1282 else
1283 ent = iter;
1284
1285 while (ent && iter->idx < i)
1286 ent = find_next_entry_inc(iter);
1287
1288 iter->pos = *pos;
1289
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290 return ent;
1291}
1292
1293static void *s_start(struct seq_file *m, loff_t *pos)
1294{
1295 struct trace_iterator *iter = m->private;
1296 void *p = NULL;
1297 loff_t l = 0;
1298 int i;
1299
1300 mutex_lock(&trace_types_lock);
1301
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001302 if (!current_trace || current_trace != iter->trace) {
1303 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001304 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001305 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001306
1307 atomic_inc(&trace_record_cmdline_disabled);
1308
1309 /* let the tracer grab locks here if needed */
1310 if (current_trace->start)
1311 current_trace->start(iter);
1312
1313 if (*pos != iter->pos) {
1314 iter->ent = NULL;
1315 iter->cpu = 0;
1316 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001317 iter->prev_ent = NULL;
1318 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001319
Steven Rostedtab464282008-05-12 21:21:00 +02001320 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001322 iter->next_page[i] = NULL;
1323 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001324
1325 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1326 ;
1327
1328 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001329 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001330 p = s_next(m, p, &l);
1331 }
1332
1333 return p;
1334}
1335
1336static void s_stop(struct seq_file *m, void *p)
1337{
1338 struct trace_iterator *iter = m->private;
1339
1340 atomic_dec(&trace_record_cmdline_disabled);
1341
1342 /* let the tracer release locks here if needed */
1343 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1344 iter->trace->stop(iter);
1345
1346 mutex_unlock(&trace_types_lock);
1347}
1348
Abhishek Sagar76094a22008-05-28 00:03:18 +05301349#define KRETPROBE_MSG "[unknown/kretprobe'd]"
1350
1351#ifdef CONFIG_KRETPROBES
1352static inline int kretprobed(unsigned long addr)
1353{
1354 return addr == (unsigned long)kretprobe_trampoline;
1355}
1356#else
1357static inline int kretprobed(unsigned long addr)
1358{
1359 return 0;
1360}
1361#endif /* CONFIG_KRETPROBES */
1362
Steven Rostedtb3806b42008-05-12 21:20:46 +02001363static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001364seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001365{
1366#ifdef CONFIG_KALLSYMS
1367 char str[KSYM_SYMBOL_LEN];
1368
1369 kallsyms_lookup(address, NULL, NULL, NULL, str);
1370
Steven Rostedtb3806b42008-05-12 21:20:46 +02001371 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001372#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001373 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001374}
1375
Steven Rostedtb3806b42008-05-12 21:20:46 +02001376static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001377seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1378 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001379{
1380#ifdef CONFIG_KALLSYMS
1381 char str[KSYM_SYMBOL_LEN];
1382
1383 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001384 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001385#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001386 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387}
1388
1389#ifndef CONFIG_64BIT
1390# define IP_FMT "%08lx"
1391#else
1392# define IP_FMT "%016lx"
1393#endif
1394
Ingo Molnare309b412008-05-12 21:20:51 +02001395static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001396seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001397{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001398 int ret;
1399
1400 if (!ip)
1401 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001402
1403 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001404 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001405 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001406 ret = seq_print_sym_short(s, "%s", ip);
1407
1408 if (!ret)
1409 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001410
1411 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001412 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1413 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001414}
1415
Ingo Molnare309b412008-05-12 21:20:51 +02001416static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001417{
Michael Ellermana6168352008-08-20 16:36:11 -07001418 seq_puts(m, "# _------=> CPU# \n");
1419 seq_puts(m, "# / _-----=> irqs-off \n");
1420 seq_puts(m, "# | / _----=> need-resched \n");
1421 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1422 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1423 seq_puts(m, "# |||| / \n");
1424 seq_puts(m, "# ||||| delay \n");
1425 seq_puts(m, "# cmd pid ||||| time | caller \n");
1426 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427}
1428
Ingo Molnare309b412008-05-12 21:20:51 +02001429static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001430{
Michael Ellermana6168352008-08-20 16:36:11 -07001431 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1432 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001433}
1434
1435
Ingo Molnare309b412008-05-12 21:20:51 +02001436static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001437print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1438{
1439 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1440 struct trace_array *tr = iter->tr;
1441 struct trace_array_cpu *data = tr->data[tr->cpu];
1442 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001443 unsigned long total = 0;
1444 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001445 int cpu;
1446 const char *name = "preemption";
1447
1448 if (type)
1449 name = type->name;
1450
Steven Rostedtab464282008-05-12 21:21:00 +02001451 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001452 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001453 total += tr->data[cpu]->trace_idx;
1454 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001456 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001457 entries += tr->data[cpu]->trace_idx;
1458 }
1459 }
1460
1461 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1462 name, UTS_RELEASE);
1463 seq_puts(m, "-----------------------------------"
1464 "---------------------------------\n");
1465 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1466 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001467 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001468 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001469 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001470 tr->cpu,
1471#if defined(CONFIG_PREEMPT_NONE)
1472 "server",
1473#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1474 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001475#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001476 "preempt",
1477#else
1478 "unknown",
1479#endif
1480 /* These are reserved for later use */
1481 0, 0, 0, 0);
1482#ifdef CONFIG_SMP
1483 seq_printf(m, " #P:%d)\n", num_online_cpus());
1484#else
1485 seq_puts(m, ")\n");
1486#endif
1487 seq_puts(m, " -----------------\n");
1488 seq_printf(m, " | task: %.16s-%d "
1489 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1490 data->comm, data->pid, data->uid, data->nice,
1491 data->policy, data->rt_priority);
1492 seq_puts(m, " -----------------\n");
1493
1494 if (data->critical_start) {
1495 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001496 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1497 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001498 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001499 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1500 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001501 seq_puts(m, "\n");
1502 }
1503
1504 seq_puts(m, "\n");
1505}
1506
Ingo Molnare309b412008-05-12 21:20:51 +02001507static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001508lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001509{
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001510 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001511 int hardirq, softirq;
1512 char *comm;
1513
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001514 comm = trace_find_cmdline(field->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001515
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001516 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001517 trace_seq_printf(s, "%3d", cpu);
Steven Rostedt214023c2008-05-12 21:20:46 +02001518 trace_seq_printf(s, "%c%c",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001519 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1520 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001522 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1523 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001524 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001525 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001526 } else {
1527 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001528 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001529 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001530 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001531 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001532 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001533 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001534 }
1535 }
1536
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001537 if (field->preempt_count)
1538 trace_seq_printf(s, "%x", field->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001539 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001540 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001541}
1542
1543unsigned long preempt_mark_thresh = 100;
1544
Ingo Molnare309b412008-05-12 21:20:51 +02001545static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001546lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547 unsigned long rel_usecs)
1548{
Steven Rostedt214023c2008-05-12 21:20:46 +02001549 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001550 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001551 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001552 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001553 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001554 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001555 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001556}
1557
1558static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1559
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001560static void
1561trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
1562{
1563 struct trace_array *tr = iter->tr;
1564 struct trace_array_cpu *data = tr->data[iter->cpu];
1565 struct trace_entry *ent;
1566
1567 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1568 if (!ent || ent->type != TRACE_CONT) {
1569 trace_seq_putc(s, '\n');
1570 return;
1571 }
1572
1573 do {
1574 trace_seq_printf(s, "%s", ent->cont.buf);
Steven Rostedt5a90f572008-09-03 17:42:51 -04001575 __trace_iterator_increment(iter, iter->cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001576 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1577 } while (ent && ent->type == TRACE_CONT);
1578}
1579
Ingo Molnare309b412008-05-12 21:20:51 +02001580static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001581print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001582{
Steven Rostedt214023c2008-05-12 21:20:46 +02001583 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001584 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1585 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1586 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1587 struct trace_entry *entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001588 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001589 unsigned long abs_usecs;
1590 unsigned long rel_usecs;
1591 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001592 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001593 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001594 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001595
1596 if (!next_entry)
1597 next_entry = entry;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001598
1599 if (entry->type == TRACE_CONT)
1600 return 1;
1601
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001602 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1603 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001604
1605 if (verbose) {
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001606 comm = trace_find_cmdline(field->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001607 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
Steven Rostedt214023c2008-05-12 21:20:46 +02001608 " %ld.%03ldms (+%ld.%03ldms): ",
1609 comm,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001610 field->pid, cpu, field->flags,
1611 field->preempt_count, trace_idx,
1612 ns2usecs(field->t),
Steven Rostedt214023c2008-05-12 21:20:46 +02001613 abs_usecs/1000,
1614 abs_usecs % 1000, rel_usecs/1000,
1615 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001616 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001617 lat_print_generic(s, entry, cpu);
1618 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001619 }
1620 switch (entry->type) {
1621 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001622 seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001623 trace_seq_puts(s, " (");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001624 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301625 trace_seq_puts(s, KRETPROBE_MSG);
1626 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001627 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001628 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001629 break;
1630 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001631 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001632 T = field->ctx.next_state < sizeof(state_to_char) ?
1633 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001634
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001635 state = field->ctx.prev_state ?
1636 __ffs(field->ctx.prev_state) + 1 : 0;
Ankita Gargd17d9692008-05-12 21:20:58 +02001637 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001638 comm = trace_find_cmdline(field->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001639 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001640 field->ctx.prev_pid,
1641 field->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001642 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001643 field->ctx.next_pid,
1644 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001645 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001646 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001647 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001648 trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001649 field->special.arg1,
1650 field->special.arg2,
1651 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001652 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001653 case TRACE_STACK:
1654 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1655 if (i)
1656 trace_seq_puts(s, " <= ");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001657 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +02001658 }
1659 trace_seq_puts(s, "\n");
1660 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001661 case TRACE_PRINT:
1662 seq_print_ip_sym(s, field->print.ip, sym_flags);
1663 trace_seq_printf(s, ": %s", field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001664 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001665 trace_seq_print_cont(s, iter);
1666 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001667 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001668 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001669 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001670 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001671}
1672
Ingo Molnare309b412008-05-12 21:20:51 +02001673static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001674{
Steven Rostedt214023c2008-05-12 21:20:46 +02001675 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001676 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001677 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001678 struct trace_field *field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001679 unsigned long usec_rem;
1680 unsigned long long t;
1681 unsigned long secs;
1682 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001683 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001684 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001685 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001686
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001687 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001688
1689 if (entry->type == TRACE_CONT)
1690 return 1;
1691
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001692 field = &entry->field;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001693
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001694 comm = trace_find_cmdline(iter->ent->field.pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001695
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001696 t = ns2usecs(field->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001697 usec_rem = do_div(t, 1000000ULL);
1698 secs = (unsigned long)t;
1699
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001700 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001701 if (!ret)
1702 return 0;
Michael Ellermana6168352008-08-20 16:36:11 -07001703 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001704 if (!ret)
1705 return 0;
1706 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1707 if (!ret)
1708 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001709
1710 switch (entry->type) {
1711 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001712 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001713 if (!ret)
1714 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001715 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001716 field->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001717 ret = trace_seq_printf(s, " <-");
1718 if (!ret)
1719 return 0;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001720 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301721 ret = trace_seq_puts(s, KRETPROBE_MSG);
1722 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001723 ret = seq_print_ip_sym(s,
1724 field->fn.parent_ip,
Abhishek Sagar76094a22008-05-28 00:03:18 +05301725 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001726 if (!ret)
1727 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001728 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001729 ret = trace_seq_printf(s, "\n");
1730 if (!ret)
1731 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732 break;
1733 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001734 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001735 S = field->ctx.prev_state < sizeof(state_to_char) ?
1736 state_to_char[field->ctx.prev_state] : 'X';
1737 T = field->ctx.next_state < sizeof(state_to_char) ?
1738 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001739 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001740 field->ctx.prev_pid,
1741 field->ctx.prev_prio,
Steven Rostedtb3806b42008-05-12 21:20:46 +02001742 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001743 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001744 field->ctx.next_pid,
1745 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001746 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001747 if (!ret)
1748 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001749 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001750 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001751 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001752 field->special.arg1,
1753 field->special.arg2,
1754 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001755 if (!ret)
1756 return 0;
1757 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001758 case TRACE_STACK:
1759 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1760 if (i) {
1761 ret = trace_seq_puts(s, " <= ");
1762 if (!ret)
1763 return 0;
1764 }
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001765 ret = seq_print_ip_sym(s, field->stack.caller[i],
Ingo Molnar86387f72008-05-12 21:20:51 +02001766 sym_flags);
1767 if (!ret)
1768 return 0;
1769 }
1770 ret = trace_seq_puts(s, "\n");
1771 if (!ret)
1772 return 0;
1773 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001774 case TRACE_PRINT:
1775 seq_print_ip_sym(s, field->print.ip, sym_flags);
1776 trace_seq_printf(s, ": %s", field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001777 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001778 trace_seq_print_cont(s, iter);
1779 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001780 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001781 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001782}
1783
Ingo Molnare309b412008-05-12 21:20:51 +02001784static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001785{
1786 struct trace_seq *s = &iter->seq;
1787 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001788 struct trace_field *field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001789 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001790 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001791
1792 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001793
1794 if (entry->type == TRACE_CONT)
1795 return 1;
1796
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001797 field = &entry->field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001798
1799 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001800 field->pid, iter->cpu, field->t);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001801 if (!ret)
1802 return 0;
1803
1804 switch (entry->type) {
1805 case TRACE_FN:
1806 ret = trace_seq_printf(s, "%x %x\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001807 field->fn.ip,
1808 field->fn.parent_ip);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001809 if (!ret)
1810 return 0;
1811 break;
1812 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001813 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001814 S = field->ctx.prev_state < sizeof(state_to_char) ?
1815 state_to_char[field->ctx.prev_state] : 'X';
1816 T = field->ctx.next_state < sizeof(state_to_char) ?
1817 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001818 if (entry->type == TRACE_WAKE)
1819 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001820 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001821 field->ctx.prev_pid,
1822 field->ctx.prev_prio,
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001823 S,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001824 field->ctx.next_pid,
1825 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001826 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001827 if (!ret)
1828 return 0;
1829 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001830 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001831 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001832 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001833 field->special.arg1,
1834 field->special.arg2,
1835 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001836 if (!ret)
1837 return 0;
1838 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001839 case TRACE_PRINT:
1840 trace_seq_printf(s, "# %lx %s",
1841 field->print.ip, field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001842 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001843 trace_seq_print_cont(s, iter);
1844 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001845 }
1846 return 1;
1847}
1848
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001849#define SEQ_PUT_FIELD_RET(s, x) \
1850do { \
1851 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1852 return 0; \
1853} while (0)
1854
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001855#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1856do { \
1857 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1858 return 0; \
1859} while (0)
1860
Ingo Molnare309b412008-05-12 21:20:51 +02001861static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001862{
1863 struct trace_seq *s = &iter->seq;
1864 unsigned char newline = '\n';
1865 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001866 struct trace_field *field;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001867 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001868
1869 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001870
1871 if (entry->type == TRACE_CONT)
1872 return 1;
1873
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001874 field = &entry->field;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001875
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001876 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001877 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001878 SEQ_PUT_HEX_FIELD_RET(s, field->t);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001879
1880 switch (entry->type) {
1881 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001882 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1883 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001884 break;
1885 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001886 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001887 S = field->ctx.prev_state < sizeof(state_to_char) ?
1888 state_to_char[field->ctx.prev_state] : 'X';
1889 T = field->ctx.next_state < sizeof(state_to_char) ?
1890 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001891 if (entry->type == TRACE_WAKE)
1892 S = '+';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001893 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1894 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001895 SEQ_PUT_HEX_FIELD_RET(s, S);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001896 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1897 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001898 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001899 break;
1900 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001901 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001902 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1903 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1904 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001905 break;
1906 }
1907 SEQ_PUT_FIELD_RET(s, newline);
1908
1909 return 1;
1910}
1911
Ingo Molnare309b412008-05-12 21:20:51 +02001912static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001913{
1914 struct trace_seq *s = &iter->seq;
1915 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001916 struct trace_field *field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001917
1918 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001919
1920 if (entry->type == TRACE_CONT)
1921 return 1;
1922
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001923 field = &entry->field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001924
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001925 SEQ_PUT_FIELD_RET(s, field->pid);
1926 SEQ_PUT_FIELD_RET(s, field->cpu);
1927 SEQ_PUT_FIELD_RET(s, field->t);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001928
1929 switch (entry->type) {
1930 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001931 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1932 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001933 break;
1934 case TRACE_CTX:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001935 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1936 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1937 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1938 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1939 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1940 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001941 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001942 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001943 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001944 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1945 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1946 SEQ_PUT_FIELD_RET(s, field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001947 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001948 }
1949 return 1;
1950}
1951
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001952static int trace_empty(struct trace_iterator *iter)
1953{
1954 struct trace_array_cpu *data;
1955 int cpu;
1956
Steven Rostedtab464282008-05-12 21:21:00 +02001957 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001958 data = iter->tr->data[cpu];
1959
Steven Rostedtb3806b42008-05-12 21:20:46 +02001960 if (head_page(data) && data->trace_idx &&
1961 (data->trace_tail != data->trace_head ||
1962 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001963 return 0;
1964 }
1965 return 1;
1966}
1967
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001968static int print_trace_line(struct trace_iterator *iter)
1969{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001970 if (iter->trace && iter->trace->print_line)
1971 return iter->trace->print_line(iter);
1972
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001973 if (trace_flags & TRACE_ITER_BIN)
1974 return print_bin_fmt(iter);
1975
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001976 if (trace_flags & TRACE_ITER_HEX)
1977 return print_hex_fmt(iter);
1978
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001979 if (trace_flags & TRACE_ITER_RAW)
1980 return print_raw_fmt(iter);
1981
1982 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1983 return print_lat_fmt(iter, iter->idx, iter->cpu);
1984
1985 return print_trace_fmt(iter);
1986}
1987
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001988static int s_show(struct seq_file *m, void *v)
1989{
1990 struct trace_iterator *iter = v;
1991
1992 if (iter->ent == NULL) {
1993 if (iter->tr) {
1994 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1995 seq_puts(m, "#\n");
1996 }
1997 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1998 /* print nothing if the buffers are empty */
1999 if (trace_empty(iter))
2000 return 0;
2001 print_trace_header(m, iter);
2002 if (!(trace_flags & TRACE_ITER_VERBOSE))
2003 print_lat_help_header(m);
2004 } else {
2005 if (!(trace_flags & TRACE_ITER_VERBOSE))
2006 print_func_help_header(m);
2007 }
2008 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002009 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02002010 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002011 }
2012
2013 return 0;
2014}
2015
2016static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002017 .start = s_start,
2018 .next = s_next,
2019 .stop = s_stop,
2020 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002021};
2022
Ingo Molnare309b412008-05-12 21:20:51 +02002023static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002024__tracing_open(struct inode *inode, struct file *file, int *ret)
2025{
2026 struct trace_iterator *iter;
2027
Steven Rostedt60a11772008-05-12 21:20:44 +02002028 if (tracing_disabled) {
2029 *ret = -ENODEV;
2030 return NULL;
2031 }
2032
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002033 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2034 if (!iter) {
2035 *ret = -ENOMEM;
2036 goto out;
2037 }
2038
2039 mutex_lock(&trace_types_lock);
2040 if (current_trace && current_trace->print_max)
2041 iter->tr = &max_tr;
2042 else
2043 iter->tr = inode->i_private;
2044 iter->trace = current_trace;
2045 iter->pos = -1;
2046
2047 /* TODO stop tracer */
2048 *ret = seq_open(file, &tracer_seq_ops);
2049 if (!*ret) {
2050 struct seq_file *m = file->private_data;
2051 m->private = iter;
2052
2053 /* stop the trace while dumping */
Steven Rostedt60bc0802008-07-10 20:58:16 -04002054 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002055 tracer_enabled = 0;
Steven Rostedt60bc0802008-07-10 20:58:16 -04002056 ftrace_function_enabled = 0;
2057 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002058
2059 if (iter->trace && iter->trace->open)
2060 iter->trace->open(iter);
2061 } else {
2062 kfree(iter);
2063 iter = NULL;
2064 }
2065 mutex_unlock(&trace_types_lock);
2066
2067 out:
2068 return iter;
2069}
2070
2071int tracing_open_generic(struct inode *inode, struct file *filp)
2072{
Steven Rostedt60a11772008-05-12 21:20:44 +02002073 if (tracing_disabled)
2074 return -ENODEV;
2075
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002076 filp->private_data = inode->i_private;
2077 return 0;
2078}
2079
2080int tracing_release(struct inode *inode, struct file *file)
2081{
2082 struct seq_file *m = (struct seq_file *)file->private_data;
2083 struct trace_iterator *iter = m->private;
2084
2085 mutex_lock(&trace_types_lock);
2086 if (iter->trace && iter->trace->close)
2087 iter->trace->close(iter);
2088
2089 /* reenable tracing if it was previously enabled */
Steven Rostedt60bc0802008-07-10 20:58:16 -04002090 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002091 tracer_enabled = 1;
Steven Rostedt60bc0802008-07-10 20:58:16 -04002092 /*
2093 * It is safe to enable function tracing even if it
2094 * isn't used
2095 */
2096 ftrace_function_enabled = 1;
2097 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002098 mutex_unlock(&trace_types_lock);
2099
2100 seq_release(inode, file);
2101 kfree(iter);
2102 return 0;
2103}
2104
2105static int tracing_open(struct inode *inode, struct file *file)
2106{
2107 int ret;
2108
2109 __tracing_open(inode, file, &ret);
2110
2111 return ret;
2112}
2113
2114static int tracing_lt_open(struct inode *inode, struct file *file)
2115{
2116 struct trace_iterator *iter;
2117 int ret;
2118
2119 iter = __tracing_open(inode, file, &ret);
2120
2121 if (!ret)
2122 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2123
2124 return ret;
2125}
2126
2127
Ingo Molnare309b412008-05-12 21:20:51 +02002128static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002129t_next(struct seq_file *m, void *v, loff_t *pos)
2130{
2131 struct tracer *t = m->private;
2132
2133 (*pos)++;
2134
2135 if (t)
2136 t = t->next;
2137
2138 m->private = t;
2139
2140 return t;
2141}
2142
2143static void *t_start(struct seq_file *m, loff_t *pos)
2144{
2145 struct tracer *t = m->private;
2146 loff_t l = 0;
2147
2148 mutex_lock(&trace_types_lock);
2149 for (; t && l < *pos; t = t_next(m, t, &l))
2150 ;
2151
2152 return t;
2153}
2154
2155static void t_stop(struct seq_file *m, void *p)
2156{
2157 mutex_unlock(&trace_types_lock);
2158}
2159
2160static int t_show(struct seq_file *m, void *v)
2161{
2162 struct tracer *t = v;
2163
2164 if (!t)
2165 return 0;
2166
2167 seq_printf(m, "%s", t->name);
2168 if (t->next)
2169 seq_putc(m, ' ');
2170 else
2171 seq_putc(m, '\n');
2172
2173 return 0;
2174}
2175
2176static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002177 .start = t_start,
2178 .next = t_next,
2179 .stop = t_stop,
2180 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002181};
2182
2183static int show_traces_open(struct inode *inode, struct file *file)
2184{
2185 int ret;
2186
Steven Rostedt60a11772008-05-12 21:20:44 +02002187 if (tracing_disabled)
2188 return -ENODEV;
2189
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002190 ret = seq_open(file, &show_traces_seq_ops);
2191 if (!ret) {
2192 struct seq_file *m = file->private_data;
2193 m->private = trace_types;
2194 }
2195
2196 return ret;
2197}
2198
2199static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002200 .open = tracing_open,
2201 .read = seq_read,
2202 .llseek = seq_lseek,
2203 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002204};
2205
2206static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002207 .open = tracing_lt_open,
2208 .read = seq_read,
2209 .llseek = seq_lseek,
2210 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002211};
2212
2213static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002214 .open = show_traces_open,
2215 .read = seq_read,
2216 .release = seq_release,
2217};
2218
Ingo Molnar36dfe922008-05-12 21:20:52 +02002219/*
2220 * Only trace on a CPU if the bitmask is set:
2221 */
2222static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2223
2224/*
2225 * When tracing/tracing_cpu_mask is modified then this holds
2226 * the new bitmask we are about to install:
2227 */
2228static cpumask_t tracing_cpumask_new;
2229
2230/*
2231 * The tracer itself will not take this lock, but still we want
2232 * to provide a consistent cpumask to user-space:
2233 */
2234static DEFINE_MUTEX(tracing_cpumask_update_lock);
2235
2236/*
2237 * Temporary storage for the character representation of the
2238 * CPU bitmask (and one more byte for the newline):
2239 */
2240static char mask_str[NR_CPUS + 1];
2241
Ingo Molnarc7078de2008-05-12 21:20:52 +02002242static ssize_t
2243tracing_cpumask_read(struct file *filp, char __user *ubuf,
2244 size_t count, loff_t *ppos)
2245{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002246 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002247
2248 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002249
2250 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2251 if (count - len < 2) {
2252 count = -EINVAL;
2253 goto out_err;
2254 }
2255 len += sprintf(mask_str + len, "\n");
2256 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2257
2258out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002259 mutex_unlock(&tracing_cpumask_update_lock);
2260
2261 return count;
2262}
2263
2264static ssize_t
2265tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2266 size_t count, loff_t *ppos)
2267{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002268 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002269
2270 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002271 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2272 if (err)
2273 goto err_unlock;
2274
Steven Rostedt92205c22008-05-12 21:20:55 +02002275 raw_local_irq_disable();
2276 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002277 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002278 /*
2279 * Increase/decrease the disabled counter if we are
2280 * about to flip a bit in the cpumask:
2281 */
2282 if (cpu_isset(cpu, tracing_cpumask) &&
2283 !cpu_isset(cpu, tracing_cpumask_new)) {
2284 atomic_inc(&global_trace.data[cpu]->disabled);
2285 }
2286 if (!cpu_isset(cpu, tracing_cpumask) &&
2287 cpu_isset(cpu, tracing_cpumask_new)) {
2288 atomic_dec(&global_trace.data[cpu]->disabled);
2289 }
2290 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002291 __raw_spin_unlock(&ftrace_max_lock);
2292 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002293
2294 tracing_cpumask = tracing_cpumask_new;
2295
Ingo Molnarc7078de2008-05-12 21:20:52 +02002296 mutex_unlock(&tracing_cpumask_update_lock);
2297
Ingo Molnarc7078de2008-05-12 21:20:52 +02002298 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002299
2300err_unlock:
2301 mutex_unlock(&tracing_cpumask_update_lock);
2302
2303 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002304}
2305
2306static struct file_operations tracing_cpumask_fops = {
2307 .open = tracing_open_generic,
2308 .read = tracing_cpumask_read,
2309 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002310};
2311
2312static ssize_t
2313tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2314 size_t cnt, loff_t *ppos)
2315{
2316 char *buf;
2317 int r = 0;
2318 int len = 0;
2319 int i;
2320
2321 /* calulate max size */
2322 for (i = 0; trace_options[i]; i++) {
2323 len += strlen(trace_options[i]);
2324 len += 3; /* "no" and space */
2325 }
2326
2327 /* +2 for \n and \0 */
2328 buf = kmalloc(len + 2, GFP_KERNEL);
2329 if (!buf)
2330 return -ENOMEM;
2331
2332 for (i = 0; trace_options[i]; i++) {
2333 if (trace_flags & (1 << i))
2334 r += sprintf(buf + r, "%s ", trace_options[i]);
2335 else
2336 r += sprintf(buf + r, "no%s ", trace_options[i]);
2337 }
2338
2339 r += sprintf(buf + r, "\n");
2340 WARN_ON(r >= len + 2);
2341
Ingo Molnar36dfe922008-05-12 21:20:52 +02002342 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002343
2344 kfree(buf);
2345
2346 return r;
2347}
2348
2349static ssize_t
2350tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2351 size_t cnt, loff_t *ppos)
2352{
2353 char buf[64];
2354 char *cmp = buf;
2355 int neg = 0;
2356 int i;
2357
Steven Rostedtcffae432008-05-12 21:21:00 +02002358 if (cnt >= sizeof(buf))
2359 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002360
2361 if (copy_from_user(&buf, ubuf, cnt))
2362 return -EFAULT;
2363
2364 buf[cnt] = 0;
2365
2366 if (strncmp(buf, "no", 2) == 0) {
2367 neg = 1;
2368 cmp += 2;
2369 }
2370
2371 for (i = 0; trace_options[i]; i++) {
2372 int len = strlen(trace_options[i]);
2373
2374 if (strncmp(cmp, trace_options[i], len) == 0) {
2375 if (neg)
2376 trace_flags &= ~(1 << i);
2377 else
2378 trace_flags |= (1 << i);
2379 break;
2380 }
2381 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002382 /*
2383 * If no option could be set, return an error:
2384 */
2385 if (!trace_options[i])
2386 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002387
2388 filp->f_pos += cnt;
2389
2390 return cnt;
2391}
2392
2393static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002394 .open = tracing_open_generic,
2395 .read = tracing_iter_ctrl_read,
2396 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002397};
2398
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002399static const char readme_msg[] =
2400 "tracing mini-HOWTO:\n\n"
2401 "# mkdir /debug\n"
2402 "# mount -t debugfs nodev /debug\n\n"
2403 "# cat /debug/tracing/available_tracers\n"
2404 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2405 "# cat /debug/tracing/current_tracer\n"
2406 "none\n"
2407 "# echo sched_switch > /debug/tracing/current_tracer\n"
2408 "# cat /debug/tracing/current_tracer\n"
2409 "sched_switch\n"
2410 "# cat /debug/tracing/iter_ctrl\n"
2411 "noprint-parent nosym-offset nosym-addr noverbose\n"
2412 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2413 "# echo 1 > /debug/tracing/tracing_enabled\n"
2414 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2415 "echo 0 > /debug/tracing/tracing_enabled\n"
2416;
2417
2418static ssize_t
2419tracing_readme_read(struct file *filp, char __user *ubuf,
2420 size_t cnt, loff_t *ppos)
2421{
2422 return simple_read_from_buffer(ubuf, cnt, ppos,
2423 readme_msg, strlen(readme_msg));
2424}
2425
2426static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002427 .open = tracing_open_generic,
2428 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002429};
2430
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002431static ssize_t
2432tracing_ctrl_read(struct file *filp, char __user *ubuf,
2433 size_t cnt, loff_t *ppos)
2434{
2435 struct trace_array *tr = filp->private_data;
2436 char buf[64];
2437 int r;
2438
2439 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002440 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002441}
2442
2443static ssize_t
2444tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2445 size_t cnt, loff_t *ppos)
2446{
2447 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002448 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002449 long val;
2450 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002451
Steven Rostedtcffae432008-05-12 21:21:00 +02002452 if (cnt >= sizeof(buf))
2453 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002454
2455 if (copy_from_user(&buf, ubuf, cnt))
2456 return -EFAULT;
2457
2458 buf[cnt] = 0;
2459
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002460 ret = strict_strtoul(buf, 10, &val);
2461 if (ret < 0)
2462 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002463
2464 val = !!val;
2465
2466 mutex_lock(&trace_types_lock);
2467 if (tr->ctrl ^ val) {
2468 if (val)
2469 tracer_enabled = 1;
2470 else
2471 tracer_enabled = 0;
2472
2473 tr->ctrl = val;
2474
2475 if (current_trace && current_trace->ctrl_update)
2476 current_trace->ctrl_update(tr);
2477 }
2478 mutex_unlock(&trace_types_lock);
2479
2480 filp->f_pos += cnt;
2481
2482 return cnt;
2483}
2484
2485static ssize_t
2486tracing_set_trace_read(struct file *filp, char __user *ubuf,
2487 size_t cnt, loff_t *ppos)
2488{
2489 char buf[max_tracer_type_len+2];
2490 int r;
2491
2492 mutex_lock(&trace_types_lock);
2493 if (current_trace)
2494 r = sprintf(buf, "%s\n", current_trace->name);
2495 else
2496 r = sprintf(buf, "\n");
2497 mutex_unlock(&trace_types_lock);
2498
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002499 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002500}
2501
2502static ssize_t
2503tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2504 size_t cnt, loff_t *ppos)
2505{
2506 struct trace_array *tr = &global_trace;
2507 struct tracer *t;
2508 char buf[max_tracer_type_len+1];
2509 int i;
2510
2511 if (cnt > max_tracer_type_len)
2512 cnt = max_tracer_type_len;
2513
2514 if (copy_from_user(&buf, ubuf, cnt))
2515 return -EFAULT;
2516
2517 buf[cnt] = 0;
2518
2519 /* strip ending whitespace. */
2520 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2521 buf[i] = 0;
2522
2523 mutex_lock(&trace_types_lock);
2524 for (t = trace_types; t; t = t->next) {
2525 if (strcmp(t->name, buf) == 0)
2526 break;
2527 }
2528 if (!t || t == current_trace)
2529 goto out;
2530
2531 if (current_trace && current_trace->reset)
2532 current_trace->reset(tr);
2533
2534 current_trace = t;
2535 if (t->init)
2536 t->init(tr);
2537
2538 out:
2539 mutex_unlock(&trace_types_lock);
2540
2541 filp->f_pos += cnt;
2542
2543 return cnt;
2544}
2545
2546static ssize_t
2547tracing_max_lat_read(struct file *filp, char __user *ubuf,
2548 size_t cnt, loff_t *ppos)
2549{
2550 unsigned long *ptr = filp->private_data;
2551 char buf[64];
2552 int r;
2553
Steven Rostedtcffae432008-05-12 21:21:00 +02002554 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002555 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002556 if (r > sizeof(buf))
2557 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002558 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002559}
2560
2561static ssize_t
2562tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2563 size_t cnt, loff_t *ppos)
2564{
2565 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002566 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002567 long val;
2568 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002569
Steven Rostedtcffae432008-05-12 21:21:00 +02002570 if (cnt >= sizeof(buf))
2571 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002572
2573 if (copy_from_user(&buf, ubuf, cnt))
2574 return -EFAULT;
2575
2576 buf[cnt] = 0;
2577
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002578 ret = strict_strtoul(buf, 10, &val);
2579 if (ret < 0)
2580 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002581
2582 *ptr = val * 1000;
2583
2584 return cnt;
2585}
2586
Steven Rostedtb3806b42008-05-12 21:20:46 +02002587static atomic_t tracing_reader;
2588
2589static int tracing_open_pipe(struct inode *inode, struct file *filp)
2590{
2591 struct trace_iterator *iter;
2592
2593 if (tracing_disabled)
2594 return -ENODEV;
2595
2596 /* We only allow for reader of the pipe */
2597 if (atomic_inc_return(&tracing_reader) != 1) {
2598 atomic_dec(&tracing_reader);
2599 return -EBUSY;
2600 }
2601
2602 /* create a buffer to store the information to pass to userspace */
2603 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2604 if (!iter)
2605 return -ENOMEM;
2606
Steven Rostedt107bad82008-05-12 21:21:01 +02002607 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002608 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002609 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002610 filp->private_data = iter;
2611
Steven Rostedt107bad82008-05-12 21:21:01 +02002612 if (iter->trace->pipe_open)
2613 iter->trace->pipe_open(iter);
2614 mutex_unlock(&trace_types_lock);
2615
Steven Rostedtb3806b42008-05-12 21:20:46 +02002616 return 0;
2617}
2618
2619static int tracing_release_pipe(struct inode *inode, struct file *file)
2620{
2621 struct trace_iterator *iter = file->private_data;
2622
2623 kfree(iter);
2624 atomic_dec(&tracing_reader);
2625
2626 return 0;
2627}
2628
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002629static unsigned int
2630tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2631{
2632 struct trace_iterator *iter = filp->private_data;
2633
2634 if (trace_flags & TRACE_ITER_BLOCK) {
2635 /*
2636 * Always select as readable when in blocking mode
2637 */
2638 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002639 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002640 if (!trace_empty(iter))
2641 return POLLIN | POLLRDNORM;
2642 poll_wait(filp, &trace_wait, poll_table);
2643 if (!trace_empty(iter))
2644 return POLLIN | POLLRDNORM;
2645
2646 return 0;
2647 }
2648}
2649
Steven Rostedtb3806b42008-05-12 21:20:46 +02002650/*
2651 * Consumer reader.
2652 */
2653static ssize_t
2654tracing_read_pipe(struct file *filp, char __user *ubuf,
2655 size_t cnt, loff_t *ppos)
2656{
2657 struct trace_iterator *iter = filp->private_data;
2658 struct trace_array_cpu *data;
2659 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002660 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002661#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002662 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002663#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002664 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002665 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002666
2667 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002668 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2669 if (sret != -EBUSY)
2670 return sret;
2671 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002672
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002673 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002674
Steven Rostedt107bad82008-05-12 21:21:01 +02002675 mutex_lock(&trace_types_lock);
2676 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002677 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2678 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002679 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002680 }
2681
Steven Rostedtb3806b42008-05-12 21:20:46 +02002682 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002683
Steven Rostedt107bad82008-05-12 21:21:01 +02002684 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002685 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002686 goto out;
2687 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002688
Steven Rostedtb3806b42008-05-12 21:20:46 +02002689 /*
2690 * This is a make-shift waitqueue. The reason we don't use
2691 * an actual wait queue is because:
2692 * 1) we only ever have one waiter
2693 * 2) the tracing, traces all functions, we don't want
2694 * the overhead of calling wake_up and friends
2695 * (and tracing them too)
2696 * Anyway, this is really very primitive wakeup.
2697 */
2698 set_current_state(TASK_INTERRUPTIBLE);
2699 iter->tr->waiter = current;
2700
Steven Rostedt107bad82008-05-12 21:21:01 +02002701 mutex_unlock(&trace_types_lock);
2702
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002703 /* sleep for 100 msecs, and try again. */
2704 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002705
Steven Rostedt107bad82008-05-12 21:21:01 +02002706 mutex_lock(&trace_types_lock);
2707
Steven Rostedtb3806b42008-05-12 21:20:46 +02002708 iter->tr->waiter = NULL;
2709
Steven Rostedt107bad82008-05-12 21:21:01 +02002710 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002711 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002712 goto out;
2713 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002714
Steven Rostedt84527992008-05-12 21:20:58 +02002715 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002716 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002717
Steven Rostedtb3806b42008-05-12 21:20:46 +02002718 /*
2719 * We block until we read something and tracing is disabled.
2720 * We still block if tracing is disabled, but we have never
2721 * read anything. This allows a user to cat this file, and
2722 * then enable tracing. But after we have read something,
2723 * we give an EOF when tracing is again disabled.
2724 *
2725 * iter->pos will be 0 if we haven't read anything.
2726 */
2727 if (!tracer_enabled && iter->pos)
2728 break;
2729
2730 continue;
2731 }
2732
2733 /* stop when tracing is finished */
2734 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002735 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002736
2737 if (cnt >= PAGE_SIZE)
2738 cnt = PAGE_SIZE - 1;
2739
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002740 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002741 memset(&iter->seq, 0,
2742 sizeof(struct trace_iterator) -
2743 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002744 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002745
2746 /*
2747 * We need to stop all tracing on all CPUS to read the
2748 * the next buffer. This is a bit expensive, but is
2749 * not done often. We fill all what we can read,
2750 * and then release the locks again.
2751 */
2752
2753 cpus_clear(mask);
2754 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002755#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002756 ftrace_save = ftrace_enabled;
2757 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002758#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002759 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002760 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002761 data = iter->tr->data[cpu];
2762
2763 if (!head_page(data) || !data->trace_idx)
2764 continue;
2765
2766 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002767 cpu_set(cpu, mask);
2768 }
2769
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002770 for_each_cpu_mask(cpu, mask) {
2771 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002772 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002773
2774 if (data->overrun > iter->last_overrun[cpu])
2775 iter->overrun[cpu] +=
2776 data->overrun - iter->last_overrun[cpu];
2777 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002778 }
2779
Steven Rostedt088b1e422008-05-12 21:20:48 +02002780 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002781 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002782 int len = iter->seq.len;
2783
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002784 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002785 if (!ret) {
2786 /* don't print partial lines */
2787 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002788 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002789 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002790
2791 trace_consume(iter);
2792
2793 if (iter->seq.len >= cnt)
2794 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002795 }
2796
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002797 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002798 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002799 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002800 }
2801
2802 for_each_cpu_mask(cpu, mask) {
2803 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002804 atomic_dec(&data->disabled);
2805 }
Ingo Molnar25770462008-05-12 21:20:49 +02002806#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002807 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002808#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002809 local_irq_restore(flags);
2810
2811 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002812 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2813 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002814 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002815 if (sret == -EBUSY)
2816 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002817
Steven Rostedt107bad82008-05-12 21:21:01 +02002818out:
2819 mutex_unlock(&trace_types_lock);
2820
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002821 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002822}
2823
Steven Rostedta98a3c32008-05-12 21:20:59 +02002824static ssize_t
2825tracing_entries_read(struct file *filp, char __user *ubuf,
2826 size_t cnt, loff_t *ppos)
2827{
2828 struct trace_array *tr = filp->private_data;
2829 char buf[64];
2830 int r;
2831
2832 r = sprintf(buf, "%lu\n", tr->entries);
2833 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2834}
2835
2836static ssize_t
2837tracing_entries_write(struct file *filp, const char __user *ubuf,
2838 size_t cnt, loff_t *ppos)
2839{
2840 unsigned long val;
2841 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002842 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002843
Steven Rostedtcffae432008-05-12 21:21:00 +02002844 if (cnt >= sizeof(buf))
2845 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002846
2847 if (copy_from_user(&buf, ubuf, cnt))
2848 return -EFAULT;
2849
2850 buf[cnt] = 0;
2851
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002852 ret = strict_strtoul(buf, 10, &val);
2853 if (ret < 0)
2854 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002855
2856 /* must have at least 1 entry */
2857 if (!val)
2858 return -EINVAL;
2859
2860 mutex_lock(&trace_types_lock);
2861
2862 if (current_trace != &no_tracer) {
2863 cnt = -EBUSY;
2864 pr_info("ftrace: set current_tracer to none"
2865 " before modifying buffer size\n");
2866 goto out;
2867 }
2868
2869 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002870 long pages_requested;
2871 unsigned long freeable_pages;
2872
2873 /* make sure we have enough memory before mapping */
2874 pages_requested =
2875 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2876
2877 /* account for each buffer (and max_tr) */
2878 pages_requested *= tracing_nr_buffers * 2;
2879
2880 /* Check for overflow */
2881 if (pages_requested < 0) {
2882 cnt = -ENOMEM;
2883 goto out;
2884 }
2885
2886 freeable_pages = determine_dirtyable_memory();
2887
2888 /* we only allow to request 1/4 of useable memory */
2889 if (pages_requested >
2890 ((freeable_pages + tracing_pages_allocated) / 4)) {
2891 cnt = -ENOMEM;
2892 goto out;
2893 }
2894
Steven Rostedta98a3c32008-05-12 21:20:59 +02002895 while (global_trace.entries < val) {
2896 if (trace_alloc_page()) {
2897 cnt = -ENOMEM;
2898 goto out;
2899 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002900 /* double check that we don't go over the known pages */
2901 if (tracing_pages_allocated > pages_requested)
2902 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002903 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002904
Steven Rostedta98a3c32008-05-12 21:20:59 +02002905 } else {
2906 /* include the number of entries in val (inc of page entries) */
2907 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2908 trace_free_page();
2909 }
2910
Steven Rostedt19384c02008-05-22 00:22:16 -04002911 /* check integrity */
2912 for_each_tracing_cpu(i)
2913 check_pages(global_trace.data[i]);
2914
Steven Rostedta98a3c32008-05-12 21:20:59 +02002915 filp->f_pos += cnt;
2916
Steven Rostedt19384c02008-05-22 00:22:16 -04002917 /* If check pages failed, return ENOMEM */
2918 if (tracing_disabled)
2919 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002920 out:
2921 max_tr.entries = global_trace.entries;
2922 mutex_unlock(&trace_types_lock);
2923
2924 return cnt;
2925}
2926
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002927static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002928 .open = tracing_open_generic,
2929 .read = tracing_max_lat_read,
2930 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002931};
2932
2933static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002934 .open = tracing_open_generic,
2935 .read = tracing_ctrl_read,
2936 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002937};
2938
2939static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002940 .open = tracing_open_generic,
2941 .read = tracing_set_trace_read,
2942 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002943};
2944
Steven Rostedtb3806b42008-05-12 21:20:46 +02002945static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002946 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002947 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002948 .read = tracing_read_pipe,
2949 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002950};
2951
Steven Rostedta98a3c32008-05-12 21:20:59 +02002952static struct file_operations tracing_entries_fops = {
2953 .open = tracing_open_generic,
2954 .read = tracing_entries_read,
2955 .write = tracing_entries_write,
2956};
2957
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002958#ifdef CONFIG_DYNAMIC_FTRACE
2959
2960static ssize_t
2961tracing_read_long(struct file *filp, char __user *ubuf,
2962 size_t cnt, loff_t *ppos)
2963{
2964 unsigned long *p = filp->private_data;
2965 char buf[64];
2966 int r;
2967
2968 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002969
2970 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002971}
2972
2973static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002974 .open = tracing_open_generic,
2975 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002976};
2977#endif
2978
2979static struct dentry *d_tracer;
2980
2981struct dentry *tracing_init_dentry(void)
2982{
2983 static int once;
2984
2985 if (d_tracer)
2986 return d_tracer;
2987
2988 d_tracer = debugfs_create_dir("tracing", NULL);
2989
2990 if (!d_tracer && !once) {
2991 once = 1;
2992 pr_warning("Could not create debugfs directory 'tracing'\n");
2993 return NULL;
2994 }
2995
2996 return d_tracer;
2997}
2998
Steven Rostedt60a11772008-05-12 21:20:44 +02002999#ifdef CONFIG_FTRACE_SELFTEST
3000/* Let selftest have access to static functions in this file */
3001#include "trace_selftest.c"
3002#endif
3003
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003004static __init void tracer_init_debugfs(void)
3005{
3006 struct dentry *d_tracer;
3007 struct dentry *entry;
3008
3009 d_tracer = tracing_init_dentry();
3010
3011 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3012 &global_trace, &tracing_ctrl_fops);
3013 if (!entry)
3014 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3015
3016 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3017 NULL, &tracing_iter_fops);
3018 if (!entry)
3019 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3020
Ingo Molnarc7078de2008-05-12 21:20:52 +02003021 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3022 NULL, &tracing_cpumask_fops);
3023 if (!entry)
3024 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3025
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003026 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3027 &global_trace, &tracing_lt_fops);
3028 if (!entry)
3029 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3030
3031 entry = debugfs_create_file("trace", 0444, d_tracer,
3032 &global_trace, &tracing_fops);
3033 if (!entry)
3034 pr_warning("Could not create debugfs 'trace' entry\n");
3035
3036 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3037 &global_trace, &show_traces_fops);
3038 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003039 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003040
3041 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3042 &global_trace, &set_tracer_fops);
3043 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003044 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003045
3046 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3047 &tracing_max_latency,
3048 &tracing_max_lat_fops);
3049 if (!entry)
3050 pr_warning("Could not create debugfs "
3051 "'tracing_max_latency' entry\n");
3052
3053 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3054 &tracing_thresh, &tracing_max_lat_fops);
3055 if (!entry)
3056 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003057 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003058 entry = debugfs_create_file("README", 0644, d_tracer,
3059 NULL, &tracing_readme_fops);
3060 if (!entry)
3061 pr_warning("Could not create debugfs 'README' entry\n");
3062
Steven Rostedtb3806b42008-05-12 21:20:46 +02003063 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3064 NULL, &tracing_pipe_fops);
3065 if (!entry)
3066 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003067 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003068
Steven Rostedta98a3c32008-05-12 21:20:59 +02003069 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3070 &global_trace, &tracing_entries_fops);
3071 if (!entry)
3072 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003073 "'trace_entries' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003074
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003075#ifdef CONFIG_DYNAMIC_FTRACE
3076 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3077 &ftrace_update_tot_cnt,
3078 &tracing_read_long_fops);
3079 if (!entry)
3080 pr_warning("Could not create debugfs "
3081 "'dyn_ftrace_total_info' entry\n");
3082#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003083#ifdef CONFIG_SYSPROF_TRACER
3084 init_tracer_sysprof_debugfs(d_tracer);
3085#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003086}
3087
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003088#define TRACE_BUF_SIZE 1024
3089#define TRACE_PRINT_BUF_SIZE \
3090 (sizeof(struct trace_field) - offsetof(struct trace_field, print.buf))
3091#define TRACE_CONT_BUF_SIZE sizeof(struct trace_field)
3092
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003093int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3094{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003095 static DEFINE_SPINLOCK(trace_buf_lock);
3096 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003097
3098 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003099 struct trace_array_cpu *data;
3100 struct trace_entry *entry;
3101 unsigned long flags;
3102 long disabled;
3103 va_list ap;
3104 int cpu, len = 0, write, written = 0;
3105
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003106 if (!(trace_flags & TRACE_ITER_PRINTK) || !tr->ctrl || tracing_disabled)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003107 return 0;
3108
3109 local_irq_save(flags);
3110 cpu = raw_smp_processor_id();
3111 data = tr->data[cpu];
3112 disabled = atomic_inc_return(&data->disabled);
3113
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003114 if (unlikely(disabled != 1))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003115 goto out;
3116
3117 spin_lock(&trace_buf_lock);
3118 va_start(ap, fmt);
3119 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, ap);
3120 va_end(ap);
3121
3122 len = min(len, TRACE_BUF_SIZE-1);
3123 trace_buf[len] = 0;
3124
3125 __raw_spin_lock(&data->lock);
3126 entry = tracing_get_trace_entry(tr, data);
3127 tracing_generic_entry_update(entry, flags);
3128 entry->type = TRACE_PRINT;
3129 entry->field.print.ip = ip;
3130
3131 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3132
3133 memcpy(&entry->field.print.buf, trace_buf, write);
3134 entry->field.print.buf[write] = 0;
3135 written = write;
3136
3137 if (written != len)
3138 entry->field.flags |= TRACE_FLAG_CONT;
3139
3140 while (written != len) {
3141 entry = tracing_get_trace_entry(tr, data);
3142
3143 entry->type = TRACE_CONT;
3144 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3145 memcpy(&entry->cont.buf, trace_buf+written, write);
3146 entry->cont.buf[write] = 0;
3147 written += write;
3148 }
3149 __raw_spin_unlock(&data->lock);
3150
3151 spin_unlock(&trace_buf_lock);
3152
3153 out:
3154 atomic_dec(&data->disabled);
3155 local_irq_restore(flags);
3156
3157 return len;
3158}
3159EXPORT_SYMBOL_GPL(__ftrace_printk);
3160
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003161static int trace_panic_handler(struct notifier_block *this,
3162 unsigned long event, void *unused)
3163{
3164 ftrace_dump();
3165 return NOTIFY_OK;
3166}
3167
3168static struct notifier_block trace_panic_notifier = {
3169 .notifier_call = trace_panic_handler,
3170 .next = NULL,
3171 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3172};
3173
3174static int trace_die_handler(struct notifier_block *self,
3175 unsigned long val,
3176 void *data)
3177{
3178 switch (val) {
3179 case DIE_OOPS:
3180 ftrace_dump();
3181 break;
3182 default:
3183 break;
3184 }
3185 return NOTIFY_OK;
3186}
3187
3188static struct notifier_block trace_die_notifier = {
3189 .notifier_call = trace_die_handler,
3190 .priority = 200
3191};
3192
3193/*
3194 * printk is set to max of 1024, we really don't need it that big.
3195 * Nothing should be printing 1000 characters anyway.
3196 */
3197#define TRACE_MAX_PRINT 1000
3198
3199/*
3200 * Define here KERN_TRACE so that we have one place to modify
3201 * it if we decide to change what log level the ftrace dump
3202 * should be at.
3203 */
3204#define KERN_TRACE KERN_INFO
3205
3206static void
3207trace_printk_seq(struct trace_seq *s)
3208{
3209 /* Probably should print a warning here. */
3210 if (s->len >= 1000)
3211 s->len = 1000;
3212
3213 /* should be zero ended, but we are paranoid. */
3214 s->buffer[s->len] = 0;
3215
3216 printk(KERN_TRACE "%s", s->buffer);
3217
3218 trace_seq_reset(s);
3219}
3220
3221
3222void ftrace_dump(void)
3223{
3224 static DEFINE_SPINLOCK(ftrace_dump_lock);
3225 /* use static because iter can be a bit big for the stack */
3226 static struct trace_iterator iter;
3227 struct trace_array_cpu *data;
3228 static cpumask_t mask;
3229 static int dump_ran;
3230 unsigned long flags;
3231 int cnt = 0;
3232 int cpu;
3233
3234 /* only one dump */
3235 spin_lock_irqsave(&ftrace_dump_lock, flags);
3236 if (dump_ran)
3237 goto out;
3238
3239 dump_ran = 1;
3240
3241 /* No turning back! */
3242 ftrace_kill_atomic();
3243
3244 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3245
3246 iter.tr = &global_trace;
3247 iter.trace = current_trace;
3248
3249 /*
3250 * We need to stop all tracing on all CPUS to read the
3251 * the next buffer. This is a bit expensive, but is
3252 * not done often. We fill all what we can read,
3253 * and then release the locks again.
3254 */
3255
3256 cpus_clear(mask);
3257
3258 for_each_tracing_cpu(cpu) {
3259 data = iter.tr->data[cpu];
3260
3261 if (!head_page(data) || !data->trace_idx)
3262 continue;
3263
3264 atomic_inc(&data->disabled);
3265 cpu_set(cpu, mask);
3266 }
3267
3268 for_each_cpu_mask(cpu, mask) {
3269 data = iter.tr->data[cpu];
3270 __raw_spin_lock(&data->lock);
3271
3272 if (data->overrun > iter.last_overrun[cpu])
3273 iter.overrun[cpu] +=
3274 data->overrun - iter.last_overrun[cpu];
3275 iter.last_overrun[cpu] = data->overrun;
3276 }
3277
3278 while (!trace_empty(&iter)) {
3279
3280 if (!cnt)
3281 printk(KERN_TRACE "---------------------------------\n");
3282
3283 cnt++;
3284
3285 /* reset all but tr, trace, and overruns */
3286 memset(&iter.seq, 0,
3287 sizeof(struct trace_iterator) -
3288 offsetof(struct trace_iterator, seq));
3289 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3290 iter.pos = -1;
3291
3292 if (find_next_entry_inc(&iter) != NULL) {
3293 print_trace_line(&iter);
3294 trace_consume(&iter);
3295 }
3296
3297 trace_printk_seq(&iter.seq);
3298 }
3299
3300 if (!cnt)
3301 printk(KERN_TRACE " (ftrace buffer empty)\n");
3302 else
3303 printk(KERN_TRACE "---------------------------------\n");
3304
3305 for_each_cpu_mask(cpu, mask) {
3306 data = iter.tr->data[cpu];
3307 __raw_spin_unlock(&data->lock);
3308 }
3309
3310 for_each_cpu_mask(cpu, mask) {
3311 data = iter.tr->data[cpu];
3312 atomic_dec(&data->disabled);
3313 }
3314
3315
3316 out:
3317 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3318}
3319
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003320static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003321{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003322 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003323 struct page *page, *tmp;
3324 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003325 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003326 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003327 int i;
3328
3329 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02003330 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003331 array = (void *)__get_free_page(GFP_KERNEL);
3332 if (array == NULL) {
3333 printk(KERN_ERR "tracer: failed to allocate page"
3334 "for trace buffer!\n");
3335 goto free_pages;
3336 }
3337
Steven Rostedt3eefae92008-05-12 21:21:04 +02003338 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003339 page = virt_to_page(array);
3340 list_add(&page->lru, &pages);
3341
3342/* Only allocate if we are actually using the max trace */
3343#ifdef CONFIG_TRACER_MAX_TRACE
3344 array = (void *)__get_free_page(GFP_KERNEL);
3345 if (array == NULL) {
3346 printk(KERN_ERR "tracer: failed to allocate page"
3347 "for trace buffer!\n");
3348 goto free_pages;
3349 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003350 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003351 page = virt_to_page(array);
3352 list_add(&page->lru, &pages);
3353#endif
3354 }
3355
3356 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02003357 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003358 data = global_trace.data[i];
3359 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003360 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003361 list_add_tail(&page->lru, &data->trace_pages);
3362 ClearPageLRU(page);
3363
3364#ifdef CONFIG_TRACER_MAX_TRACE
3365 data = max_tr.data[i];
3366 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003367 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003368 list_add_tail(&page->lru, &data->trace_pages);
3369 SetPageLRU(page);
3370#endif
3371 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003372 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003373 global_trace.entries += ENTRIES_PER_PAGE;
3374
3375 return 0;
3376
3377 free_pages:
3378 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003379 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003380 __free_page(page);
3381 }
3382 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003383}
3384
Steven Rostedta98a3c32008-05-12 21:20:59 +02003385static int trace_free_page(void)
3386{
3387 struct trace_array_cpu *data;
3388 struct page *page;
3389 struct list_head *p;
3390 int i;
3391 int ret = 0;
3392
3393 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02003394 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003395 data = global_trace.data[i];
3396 p = data->trace_pages.next;
3397 if (p == &data->trace_pages) {
3398 /* should never happen */
3399 WARN_ON(1);
3400 tracing_disabled = 1;
3401 ret = -1;
3402 break;
3403 }
3404 page = list_entry(p, struct page, lru);
3405 ClearPageLRU(page);
3406 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02003407 tracing_pages_allocated--;
3408 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003409 __free_page(page);
3410
3411 tracing_reset(data);
3412
3413#ifdef CONFIG_TRACER_MAX_TRACE
3414 data = max_tr.data[i];
3415 p = data->trace_pages.next;
3416 if (p == &data->trace_pages) {
3417 /* should never happen */
3418 WARN_ON(1);
3419 tracing_disabled = 1;
3420 ret = -1;
3421 break;
3422 }
3423 page = list_entry(p, struct page, lru);
3424 ClearPageLRU(page);
3425 list_del(&page->lru);
3426 __free_page(page);
3427
3428 tracing_reset(data);
3429#endif
3430 }
3431 global_trace.entries -= ENTRIES_PER_PAGE;
3432
3433 return ret;
3434}
3435
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003436__init static int tracer_alloc_buffers(void)
3437{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003438 struct trace_array_cpu *data;
3439 void *array;
3440 struct page *page;
3441 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02003442 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003443 int i;
3444
Steven Rostedtab464282008-05-12 21:21:00 +02003445 /* TODO: make the number of buffers hot pluggable with CPUS */
3446 tracing_nr_buffers = num_possible_cpus();
3447 tracing_buffer_mask = cpu_possible_map;
3448
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003449 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003450 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003451 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003452 max_tr.data[i] = &per_cpu(max_data, i);
3453
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003454 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003455 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003456 printk(KERN_ERR "tracer: failed to allocate page"
3457 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003458 goto free_buffers;
3459 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003460
3461 /* set the array to the list */
3462 INIT_LIST_HEAD(&data->trace_pages);
3463 page = virt_to_page(array);
3464 list_add(&page->lru, &data->trace_pages);
3465 /* use the LRU flag to differentiate the two buffers */
3466 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003467
Steven Rostedta98a3c32008-05-12 21:20:59 +02003468 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3469 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3470
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003471/* Only allocate if we are actually using the max trace */
3472#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003473 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003474 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003475 printk(KERN_ERR "tracer: failed to allocate page"
3476 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003477 goto free_buffers;
3478 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003479
3480 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3481 page = virt_to_page(array);
3482 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3483 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003484#endif
3485 }
3486
3487 /*
3488 * Since we allocate by orders of pages, we may be able to
3489 * round up a bit.
3490 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003491 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003492 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003493
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003494 while (global_trace.entries < trace_nr_entries) {
3495 if (trace_alloc_page())
3496 break;
3497 pages++;
3498 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003499 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003500
Jiri Slaby20764ff2008-06-12 11:27:03 +02003501 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3502 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003503 pr_info(" actual entries %ld\n", global_trace.entries);
3504
3505 tracer_init_debugfs();
3506
3507 trace_init_cmdlines();
3508
3509 register_tracer(&no_tracer);
3510 current_trace = &no_tracer;
3511
Steven Rostedt60a11772008-05-12 21:20:44 +02003512 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003513 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003514 tracing_disabled = 0;
3515
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003516 atomic_notifier_chain_register(&panic_notifier_list,
3517 &trace_panic_notifier);
3518
3519 register_die_notifier(&trace_die_notifier);
3520
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003521 return 0;
3522
3523 free_buffers:
3524 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003525 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003526 struct trace_array_cpu *data = global_trace.data[i];
3527
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003528 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003529 list_for_each_entry_safe(page, tmp,
3530 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003531 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003532 __free_page(page);
3533 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003534 }
3535
3536#ifdef CONFIG_TRACER_MAX_TRACE
3537 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003538 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003539 list_for_each_entry_safe(page, tmp,
3540 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003541 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003542 __free_page(page);
3543 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003544 }
3545#endif
3546 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003547 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003548}
Steven Rostedt60a11772008-05-12 21:20:44 +02003549fs_initcall(tracer_alloc_buffers);