blob: eee1fd9648986e767bb21c9a89906f57d891f1ba [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/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200203 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
204 * control the output of kernel symbols.
205 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200206#define TRACE_ITER_SYM_MASK \
207 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
208
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200209/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200210static const char *trace_options[] = {
211 "print-parent",
212 "sym-offset",
213 "sym-addr",
214 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200215 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200216 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200217 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200218 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200219 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200220 "sched-tree",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200221 "ftrace_printk",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200222 NULL
223};
224
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200225/*
226 * ftrace_max_lock is used to protect the swapping of buffers
227 * when taking a max snapshot. The buffers themselves are
228 * protected by per_cpu spinlocks. But the action of the swap
229 * needs its own lock.
230 *
231 * This is defined as a raw_spinlock_t in order to help
232 * with performance when lockdep debugging is enabled.
233 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200234static raw_spinlock_t ftrace_max_lock =
235 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200236
237/*
238 * Copy the new maximum trace into the separate maximum-trace
239 * structure. (this way the maximum trace is permanently saved,
240 * for later retrieval via /debugfs/tracing/latency_trace)
241 */
Ingo Molnare309b412008-05-12 21:20:51 +0200242static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200243__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
244{
245 struct trace_array_cpu *data = tr->data[cpu];
246
247 max_tr.cpu = cpu;
248 max_tr.time_start = data->preempt_timestamp;
249
250 data = max_tr.data[cpu];
251 data->saved_latency = tracing_max_latency;
252
253 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
254 data->pid = tsk->pid;
255 data->uid = tsk->uid;
256 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
257 data->policy = tsk->policy;
258 data->rt_priority = tsk->rt_priority;
259
260 /* record this tasks comm */
261 tracing_record_cmdline(current);
262}
263
Steven Rostedt19384c02008-05-22 00:22:16 -0400264#define CHECK_COND(cond) \
265 if (unlikely(cond)) { \
266 tracing_disabled = 1; \
267 WARN_ON(1); \
268 return -1; \
269 }
270
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200271/**
272 * check_pages - integrity check of trace buffers
273 *
274 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400275 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200276 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400277int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200278{
279 struct page *page, *tmp;
280
Steven Rostedt19384c02008-05-22 00:22:16 -0400281 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
282 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200283
284 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400285 CHECK_COND(page->lru.next->prev != &page->lru);
286 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200287 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400288
289 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200290}
291
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200292/**
293 * head_page - page address of the first page in per_cpu buffer.
294 *
295 * head_page returns the page address of the first page in
296 * a per_cpu buffer. This also preforms various consistency
297 * checks to make sure the buffer has not been corrupted.
298 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200299void *head_page(struct trace_array_cpu *data)
300{
301 struct page *page;
302
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200303 if (list_empty(&data->trace_pages))
304 return NULL;
305
306 page = list_entry(data->trace_pages.next, struct page, lru);
307 BUG_ON(&page->lru == &data->trace_pages);
308
309 return page_address(page);
310}
311
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200312/**
313 * trace_seq_printf - sequence printing of trace information
314 * @s: trace sequence descriptor
315 * @fmt: printf format string
316 *
317 * The tracer may use either sequence operations or its own
318 * copy to user routines. To simplify formating of a trace
319 * trace_seq_printf is used to store strings into a special
320 * buffer (@s). Then the output may be either used by
321 * the sequencer or pulled into another buffer.
322 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200323int
Steven Rostedt214023c2008-05-12 21:20:46 +0200324trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
325{
326 int len = (PAGE_SIZE - 1) - s->len;
327 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200328 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200329
330 if (!len)
331 return 0;
332
333 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200334 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200335 va_end(ap);
336
Steven Rostedtb3806b42008-05-12 21:20:46 +0200337 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200338 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200339 return 0;
340
341 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200342
343 return len;
344}
345
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200346/**
347 * trace_seq_puts - trace sequence printing of simple string
348 * @s: trace sequence descriptor
349 * @str: simple string to record
350 *
351 * The tracer may use either the sequence operations or its own
352 * copy to user routines. This function records a simple string
353 * into a special buffer (@s) for later retrieval by a sequencer
354 * or other mechanism.
355 */
Ingo Molnare309b412008-05-12 21:20:51 +0200356static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200357trace_seq_puts(struct trace_seq *s, const char *str)
358{
359 int len = strlen(str);
360
361 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200362 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200363
364 memcpy(s->buffer + s->len, str, len);
365 s->len += len;
366
367 return len;
368}
369
Ingo Molnare309b412008-05-12 21:20:51 +0200370static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200371trace_seq_putc(struct trace_seq *s, unsigned char c)
372{
373 if (s->len >= (PAGE_SIZE - 1))
374 return 0;
375
376 s->buffer[s->len++] = c;
377
378 return 1;
379}
380
Ingo Molnare309b412008-05-12 21:20:51 +0200381static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200382trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
383{
384 if (len > ((PAGE_SIZE - 1) - s->len))
385 return 0;
386
387 memcpy(s->buffer + s->len, mem, len);
388 s->len += len;
389
390 return len;
391}
392
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200393#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200394static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200395
Ingo Molnare309b412008-05-12 21:20:51 +0200396static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200397trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
398{
399 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200400 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200401 unsigned char byte;
402 int i, j;
403
404 BUG_ON(len >= HEX_CHARS);
405
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200406#ifdef __BIG_ENDIAN
407 for (i = 0, j = 0; i < len; i++) {
408#else
409 for (i = len-1, j = 0; i >= 0; i--) {
410#endif
411 byte = data[i];
412
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200413 hex[j++] = hex2asc[byte & 0x0f];
414 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200415 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200416 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200417
418 return trace_seq_putmem(s, hex, j);
419}
420
Ingo Molnare309b412008-05-12 21:20:51 +0200421static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200422trace_seq_reset(struct trace_seq *s)
423{
424 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200425 s->readpos = 0;
426}
427
428ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
429{
430 int len;
431 int ret;
432
433 if (s->len <= s->readpos)
434 return -EBUSY;
435
436 len = s->len - s->readpos;
437 if (cnt > len)
438 cnt = len;
439 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
440 if (ret)
441 return -EFAULT;
442
443 s->readpos += len;
444 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200445}
446
Ingo Molnare309b412008-05-12 21:20:51 +0200447static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200448trace_print_seq(struct seq_file *m, struct trace_seq *s)
449{
450 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
451
452 s->buffer[len] = 0;
453 seq_puts(m, s->buffer);
454
455 trace_seq_reset(s);
456}
457
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200458/*
459 * flip the trace buffers between two trace descriptors.
460 * This usually is the buffers between the global_trace and
461 * the max_tr to record a snapshot of a current trace.
462 *
463 * The ftrace_max_lock must be held.
464 */
Ingo Molnare309b412008-05-12 21:20:51 +0200465static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200466flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
467{
468 struct list_head flip_pages;
469
470 INIT_LIST_HEAD(&flip_pages);
471
Steven Rostedt93a588f2008-05-12 21:20:45 +0200472 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200473 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200474 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200475
476 check_pages(tr1);
477 check_pages(tr2);
478 list_splice_init(&tr1->trace_pages, &flip_pages);
479 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
480 list_splice_init(&flip_pages, &tr2->trace_pages);
481 BUG_ON(!list_empty(&flip_pages));
482 check_pages(tr1);
483 check_pages(tr2);
484}
485
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200486/**
487 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
488 * @tr: tracer
489 * @tsk: the task with the latency
490 * @cpu: The cpu that initiated the trace.
491 *
492 * Flip the buffers between the @tr and the max_tr and record information
493 * about which task was the cause of this latency.
494 */
Ingo Molnare309b412008-05-12 21:20:51 +0200495void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200496update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
497{
498 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200499 int i;
500
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200501 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200502 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200503 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200504 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200505 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200506 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200507 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200508 }
509
510 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200511 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200512}
513
514/**
515 * update_max_tr_single - only copy one trace over, and reset the rest
516 * @tr - tracer
517 * @tsk - task with the latency
518 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200519 *
520 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200521 */
Ingo Molnare309b412008-05-12 21:20:51 +0200522void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200523update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
524{
525 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200526 int i;
527
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200528 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200529 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200530 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200531 tracing_reset(max_tr.data[i]);
532
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200533 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200534 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200535
536 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200537 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200538}
539
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200540/**
541 * register_tracer - register a tracer with the ftrace system.
542 * @type - the plugin for the tracer
543 *
544 * Register a new plugin tracer.
545 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200546int register_tracer(struct tracer *type)
547{
548 struct tracer *t;
549 int len;
550 int ret = 0;
551
552 if (!type->name) {
553 pr_info("Tracer must have a name\n");
554 return -1;
555 }
556
557 mutex_lock(&trace_types_lock);
558 for (t = trace_types; t; t = t->next) {
559 if (strcmp(type->name, t->name) == 0) {
560 /* already found */
561 pr_info("Trace %s already registered\n",
562 type->name);
563 ret = -1;
564 goto out;
565 }
566 }
567
Steven Rostedt60a11772008-05-12 21:20:44 +0200568#ifdef CONFIG_FTRACE_STARTUP_TEST
569 if (type->selftest) {
570 struct tracer *saved_tracer = current_trace;
571 struct trace_array_cpu *data;
572 struct trace_array *tr = &global_trace;
573 int saved_ctrl = tr->ctrl;
574 int i;
575 /*
576 * Run a selftest on this tracer.
577 * Here we reset the trace buffer, and set the current
578 * tracer to be this tracer. The tracer can then run some
579 * internal tracing to verify that everything is in order.
580 * If we fail, we do not register this tracer.
581 */
Steven Rostedtab464282008-05-12 21:21:00 +0200582 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200583 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200584 if (!head_page(data))
585 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200586 tracing_reset(data);
587 }
588 current_trace = type;
589 tr->ctrl = 0;
590 /* the test is responsible for initializing and enabling */
591 pr_info("Testing tracer %s: ", type->name);
592 ret = type->selftest(type, tr);
593 /* the test is responsible for resetting too */
594 current_trace = saved_tracer;
595 tr->ctrl = saved_ctrl;
596 if (ret) {
597 printk(KERN_CONT "FAILED!\n");
598 goto out;
599 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200600 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200601 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200602 data = tr->data[i];
603 if (!head_page(data))
604 continue;
605 tracing_reset(data);
606 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200607 printk(KERN_CONT "PASSED\n");
608 }
609#endif
610
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200611 type->next = trace_types;
612 trace_types = type;
613 len = strlen(type->name);
614 if (len > max_tracer_type_len)
615 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200616
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200617 out:
618 mutex_unlock(&trace_types_lock);
619
620 return ret;
621}
622
623void unregister_tracer(struct tracer *type)
624{
625 struct tracer **t;
626 int len;
627
628 mutex_lock(&trace_types_lock);
629 for (t = &trace_types; *t; t = &(*t)->next) {
630 if (*t == type)
631 goto found;
632 }
633 pr_info("Trace %s not registered\n", type->name);
634 goto out;
635
636 found:
637 *t = (*t)->next;
638 if (strlen(type->name) != max_tracer_type_len)
639 goto out;
640
641 max_tracer_type_len = 0;
642 for (t = &trace_types; *t; t = &(*t)->next) {
643 len = strlen((*t)->name);
644 if (len > max_tracer_type_len)
645 max_tracer_type_len = len;
646 }
647 out:
648 mutex_unlock(&trace_types_lock);
649}
650
Ingo Molnare309b412008-05-12 21:20:51 +0200651void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652{
653 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200654 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200655 data->trace_head = data->trace_tail = head_page(data);
656 data->trace_head_idx = 0;
657 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200658}
659
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200660#define SAVED_CMDLINES 128
661static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
662static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
663static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
664static int cmdline_idx;
665static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200666
Steven Rostedt25b0b442008-05-12 21:21:00 +0200667/* temporary disable recording */
668atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200669
670static void trace_init_cmdlines(void)
671{
672 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
673 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
674 cmdline_idx = 0;
675}
676
Ingo Molnare309b412008-05-12 21:20:51 +0200677void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200678
Ingo Molnare309b412008-05-12 21:20:51 +0200679static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200680{
681 unsigned map;
682 unsigned idx;
683
684 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
685 return;
686
687 /*
688 * It's not the end of the world if we don't get
689 * the lock, but we also don't want to spin
690 * nor do we want to disable interrupts,
691 * so if we miss here, then better luck next time.
692 */
693 if (!spin_trylock(&trace_cmdline_lock))
694 return;
695
696 idx = map_pid_to_cmdline[tsk->pid];
697 if (idx >= SAVED_CMDLINES) {
698 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
699
700 map = map_cmdline_to_pid[idx];
701 if (map <= PID_MAX_DEFAULT)
702 map_pid_to_cmdline[map] = (unsigned)-1;
703
704 map_pid_to_cmdline[tsk->pid] = idx;
705
706 cmdline_idx = idx;
707 }
708
709 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
710
711 spin_unlock(&trace_cmdline_lock);
712}
713
Ingo Molnare309b412008-05-12 21:20:51 +0200714static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200715{
716 char *cmdline = "<...>";
717 unsigned map;
718
719 if (!pid)
720 return "<idle>";
721
722 if (pid > PID_MAX_DEFAULT)
723 goto out;
724
725 map = map_pid_to_cmdline[pid];
726 if (map >= SAVED_CMDLINES)
727 goto out;
728
729 cmdline = saved_cmdlines[map];
730
731 out:
732 return cmdline;
733}
734
Ingo Molnare309b412008-05-12 21:20:51 +0200735void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200736{
737 if (atomic_read(&trace_record_cmdline_disabled))
738 return;
739
740 trace_save_cmdline(tsk);
741}
742
Ingo Molnare309b412008-05-12 21:20:51 +0200743static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200744trace_next_list(struct trace_array_cpu *data, struct list_head *next)
745{
746 /*
747 * Roundrobin - but skip the head (which is not a real page):
748 */
749 next = next->next;
750 if (unlikely(next == &data->trace_pages))
751 next = next->next;
752 BUG_ON(next == &data->trace_pages);
753
754 return next;
755}
756
Ingo Molnare309b412008-05-12 21:20:51 +0200757static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200758trace_next_page(struct trace_array_cpu *data, void *addr)
759{
760 struct list_head *next;
761 struct page *page;
762
763 page = virt_to_page(addr);
764
765 next = trace_next_list(data, &page->lru);
766 page = list_entry(next, struct page, lru);
767
768 return page_address(page);
769}
770
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300771struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200772tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200773{
774 unsigned long idx, idx_next;
775 struct trace_entry *entry;
776
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200777 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200778 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200779 idx_next = idx + 1;
780
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200781 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
782
Steven Rostedt93a588f2008-05-12 21:20:45 +0200783 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200784
785 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200786 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200787 idx_next = 0;
788 }
789
Steven Rostedt93a588f2008-05-12 21:20:45 +0200790 if (data->trace_head == data->trace_tail &&
791 idx_next == data->trace_tail_idx) {
792 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200793 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200794 data->trace_tail_idx++;
795 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
796 data->trace_tail =
797 trace_next_page(data, data->trace_tail);
798 data->trace_tail_idx = 0;
799 }
800 }
801
802 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200803
804 return entry;
805}
806
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300807void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200808tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200809{
810 struct task_struct *tsk = current;
811 unsigned long pc;
812
813 pc = preempt_count();
814
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400815 entry->field.preempt_count = pc & 0xff;
816 entry->field.pid = (tsk) ? tsk->pid : 0;
817 entry->field.t = ftrace_now(raw_smp_processor_id());
818 entry->field.flags =
819 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200820 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
821 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
822 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
823}
824
Ingo Molnare309b412008-05-12 21:20:51 +0200825void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200826trace_function(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200828{
829 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200830 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200831
Steven Rostedt92205c22008-05-12 21:20:55 +0200832 raw_local_irq_save(irq_flags);
833 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400834 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200835 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400836 entry->type = TRACE_FN;
837 entry->field.fn.ip = ip;
838 entry->field.fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200839 __raw_spin_unlock(&data->lock);
840 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200841}
842
Ingo Molnare309b412008-05-12 21:20:51 +0200843void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200844ftrace(struct trace_array *tr, struct trace_array_cpu *data,
845 unsigned long ip, unsigned long parent_ip, unsigned long flags)
846{
847 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200848 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200849}
850
Ingo Molnar86387f72008-05-12 21:20:51 +0200851void __trace_stack(struct trace_array *tr,
852 struct trace_array_cpu *data,
853 unsigned long flags,
854 int skip)
855{
856 struct trace_entry *entry;
857 struct stack_trace trace;
858
859 if (!(trace_flags & TRACE_ITER_STACKTRACE))
860 return;
861
862 entry = tracing_get_trace_entry(tr, data);
863 tracing_generic_entry_update(entry, flags);
864 entry->type = TRACE_STACK;
865
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400866 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
Ingo Molnar86387f72008-05-12 21:20:51 +0200867
868 trace.nr_entries = 0;
869 trace.max_entries = FTRACE_STACK_ENTRIES;
870 trace.skip = skip;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400871 trace.entries = entry->field.stack.caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200872
873 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200874}
875
Ingo Molnare309b412008-05-12 21:20:51 +0200876void
Ingo Molnara4feb832008-05-12 21:21:02 +0200877__trace_special(void *__tr, void *__data,
878 unsigned long arg1, unsigned long arg2, unsigned long arg3)
879{
880 struct trace_array_cpu *data = __data;
881 struct trace_array *tr = __tr;
882 struct trace_entry *entry;
883 unsigned long irq_flags;
884
885 raw_local_irq_save(irq_flags);
886 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400887 entry = tracing_get_trace_entry(tr, data);
Ingo Molnara4feb832008-05-12 21:21:02 +0200888 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400889 entry->type = TRACE_SPECIAL;
890 entry->field.special.arg1 = arg1;
891 entry->field.special.arg2 = arg2;
892 entry->field.special.arg3 = arg3;
Ingo Molnara4feb832008-05-12 21:21:02 +0200893 __trace_stack(tr, data, irq_flags, 4);
894 __raw_spin_unlock(&data->lock);
895 raw_local_irq_restore(irq_flags);
896
897 trace_wake_up();
898}
899
900void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200901tracing_sched_switch_trace(struct trace_array *tr,
902 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200903 struct task_struct *prev,
904 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200905 unsigned long flags)
906{
907 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200908 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200909
Steven Rostedt92205c22008-05-12 21:20:55 +0200910 raw_local_irq_save(irq_flags);
911 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400912 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200913 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400914 entry->type = TRACE_CTX;
915 entry->field.ctx.prev_pid = prev->pid;
916 entry->field.ctx.prev_prio = prev->prio;
917 entry->field.ctx.prev_state = prev->state;
918 entry->field.ctx.next_pid = next->pid;
919 entry->field.ctx.next_prio = next->prio;
920 entry->field.ctx.next_state = next->state;
Peter Zijlstra80b5e942008-09-04 10:24:16 +0200921 entry->field.ctx.next_cpu = task_cpu(next);
Ingo Molnar74f4e362008-05-12 21:21:15 +0200922 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200923 __raw_spin_unlock(&data->lock);
924 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200925}
926
Ingo Molnar57422792008-05-12 21:20:51 +0200927void
928tracing_sched_wakeup_trace(struct trace_array *tr,
929 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200930 struct task_struct *wakee,
931 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200932 unsigned long flags)
933{
934 struct trace_entry *entry;
935 unsigned long irq_flags;
936
Steven Rostedt92205c22008-05-12 21:20:55 +0200937 raw_local_irq_save(irq_flags);
938 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200939 entry = tracing_get_trace_entry(tr, data);
940 tracing_generic_entry_update(entry, flags);
941 entry->type = TRACE_WAKE;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400942 entry->field.ctx.prev_pid = curr->pid;
943 entry->field.ctx.prev_prio = curr->prio;
944 entry->field.ctx.prev_state = curr->state;
945 entry->field.ctx.next_pid = wakee->pid;
946 entry->field.ctx.next_prio = wakee->prio;
947 entry->field.ctx.next_state = wakee->state;
Peter Zijlstra80b5e942008-09-04 10:24:16 +0200948 entry->field.ctx.next_cpu = task_cpu(wakee);
Ingo Molnar74f4e362008-05-12 21:21:15 +0200949 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200950 __raw_spin_unlock(&data->lock);
951 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200952
953 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200954}
955
Steven Rostedt4902f882008-05-22 00:22:18 -0400956void
957ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
958{
959 struct trace_array *tr = &global_trace;
960 struct trace_array_cpu *data;
961 unsigned long flags;
962 long disabled;
963 int cpu;
964
965 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
966 return;
967
968 local_irq_save(flags);
969 cpu = raw_smp_processor_id();
970 data = tr->data[cpu];
971 disabled = atomic_inc_return(&data->disabled);
972
973 if (likely(disabled == 1))
974 __trace_special(tr, data, arg1, arg2, arg3);
975
976 atomic_dec(&data->disabled);
977 local_irq_restore(flags);
978}
979
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200980#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200981static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200982function_trace_call(unsigned long ip, unsigned long parent_ip)
983{
984 struct trace_array *tr = &global_trace;
985 struct trace_array_cpu *data;
986 unsigned long flags;
987 long disabled;
988 int cpu;
989
Steven Rostedt60bc0802008-07-10 20:58:16 -0400990 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200991 return;
992
Abhishek Sagarecea6562008-06-21 23:47:53 +0530993 if (skip_trace(ip))
994 return;
995
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200996 local_irq_save(flags);
997 cpu = raw_smp_processor_id();
998 data = tr->data[cpu];
999 disabled = atomic_inc_return(&data->disabled);
1000
1001 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +02001002 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001003
1004 atomic_dec(&data->disabled);
1005 local_irq_restore(flags);
1006}
1007
1008static struct ftrace_ops trace_ops __read_mostly =
1009{
1010 .func = function_trace_call,
1011};
1012
Ingo Molnare309b412008-05-12 21:20:51 +02001013void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001014{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001015 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001016 register_ftrace_function(&trace_ops);
Steven Rostedt60bc0802008-07-10 20:58:16 -04001017 if (tracer_enabled)
1018 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001019}
1020
Ingo Molnare309b412008-05-12 21:20:51 +02001021void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001022{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001023 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001024 unregister_ftrace_function(&trace_ops);
1025}
1026#endif
1027
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001028enum trace_file_type {
1029 TRACE_FILE_LAT_FMT = 1,
1030};
1031
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001032/* Return the current entry. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001033static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001034trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1035 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001036{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001037 struct page *page;
1038 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001039
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001040 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001041 iter->next_idx[cpu] >= data->trace_idx ||
1042 (data->trace_head == data->trace_tail &&
1043 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044 return NULL;
1045
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001046 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001047 /* Initialize the iterator for this cpu trace buffer */
1048 WARN_ON(!data->trace_tail);
1049 page = virt_to_page(data->trace_tail);
1050 iter->next_page[cpu] = &page->lru;
1051 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001052 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001053
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001054 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001055 BUG_ON(&data->trace_pages == &page->lru);
1056
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001057 array = page_address(page);
1058
Steven Rostedt93a588f2008-05-12 21:20:45 +02001059 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001060 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001061}
1062
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001063/* Increment the index counter of an iterator by one */
Steven Rostedt5a90f572008-09-03 17:42:51 -04001064static void __trace_iterator_increment(struct trace_iterator *iter, int cpu)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001065{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001066 iter->next_idx[cpu]++;
1067 iter->next_page_idx[cpu]++;
1068
1069 if (iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE) {
1070 struct trace_array_cpu *data = iter->tr->data[cpu];
1071
1072 iter->next_page_idx[cpu] = 0;
1073 iter->next_page[cpu] =
1074 trace_next_list(data, iter->next_page[cpu]);
1075 }
1076}
1077
Steven Rostedt5a90f572008-09-03 17:42:51 -04001078static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1079{
1080 iter->idx++;
1081 __trace_iterator_increment(iter, cpu);
1082}
1083
Ingo Molnare309b412008-05-12 21:20:51 +02001084static struct trace_entry *
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001085trace_entry_next(struct trace_array *tr, struct trace_array_cpu *data,
1086 struct trace_iterator *iter, int cpu)
1087{
1088 struct list_head *next_page;
1089 struct trace_entry *ent;
1090 int idx, next_idx, next_page_idx;
1091
1092 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1093
1094 if (likely(!ent || ent->type != TRACE_CONT))
1095 return ent;
1096
1097 /* save the iterator details */
1098 idx = iter->idx;
1099 next_idx = iter->next_idx[cpu];
1100 next_page_idx = iter->next_page_idx[cpu];
1101 next_page = iter->next_page[cpu];
1102
1103 /* find a real entry */
1104 do {
Steven Rostedt5a90f572008-09-03 17:42:51 -04001105 __trace_iterator_increment(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001106 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
1107 } while (ent && ent->type != TRACE_CONT);
1108
1109 /* reset the iterator */
1110 iter->idx = idx;
1111 iter->next_idx[cpu] = next_idx;
1112 iter->next_page_idx[cpu] = next_page_idx;
1113 iter->next_page[cpu] = next_page;
1114
1115 return ent;
1116}
1117
1118static struct trace_entry *
1119__find_next_entry(struct trace_iterator *iter, int *ent_cpu, int inc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120{
1121 struct trace_array *tr = iter->tr;
1122 struct trace_entry *ent, *next = NULL;
1123 int next_cpu = -1;
1124 int cpu;
1125
Steven Rostedtab464282008-05-12 21:21:00 +02001126 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001127 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001128 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001129
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001130 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001131
1132 if (ent && ent->type == TRACE_CONT) {
1133 struct trace_array_cpu *data = tr->data[cpu];
1134
1135 if (!inc)
1136 ent = trace_entry_next(tr, data, iter, cpu);
1137 else {
1138 while (ent && ent->type == TRACE_CONT) {
Steven Rostedt5a90f572008-09-03 17:42:51 -04001139 __trace_iterator_increment(iter, cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001140 ent = trace_entry_idx(tr, tr->data[cpu],
1141 iter, cpu);
1142 }
1143 }
1144 }
1145
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001146 /*
1147 * Pick the entry with the smallest timestamp:
1148 */
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001149 if (ent && (!next || ent->field.t < next->field.t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001150 next = ent;
1151 next_cpu = cpu;
1152 }
1153 }
1154
1155 if (ent_cpu)
1156 *ent_cpu = next_cpu;
1157
1158 return next;
1159}
1160
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001161/* Find the next real entry, without updating the iterator itself */
1162static struct trace_entry *
1163find_next_entry(struct trace_iterator *iter, int *ent_cpu)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001164{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001165 return __find_next_entry(iter, ent_cpu, 0);
1166}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001167
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001168/* Find the next real entry, and increment the iterator to the next entry */
1169static void *find_next_entry_inc(struct trace_iterator *iter)
1170{
1171 struct trace_entry *next;
1172 int next_cpu = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001173
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001174 next = __find_next_entry(iter, &next_cpu, 1);
1175
1176 iter->prev_ent = iter->ent;
1177 iter->prev_cpu = iter->cpu;
1178
1179 iter->ent = next;
1180 iter->cpu = next_cpu;
1181
1182 if (next)
1183 trace_iterator_increment(iter, iter->cpu);
1184
1185 return next ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001186}
1187
Ingo Molnare309b412008-05-12 21:20:51 +02001188static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001189{
1190 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001191 struct trace_entry *ent;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001192
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001193 again:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001194 data->trace_tail_idx++;
1195 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1196 data->trace_tail = trace_next_page(data, data->trace_tail);
1197 data->trace_tail_idx = 0;
1198 }
1199
1200 /* Check if we empty it, then reset the index */
1201 if (data->trace_head == data->trace_tail &&
1202 data->trace_head_idx == data->trace_tail_idx)
1203 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001204
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001205 ent = trace_entry_idx(iter->tr, iter->tr->data[iter->cpu],
1206 iter, iter->cpu);
1207 if (ent && ent->type == TRACE_CONT)
1208 goto again;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001209}
1210
Ingo Molnare309b412008-05-12 21:20:51 +02001211static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001212{
1213 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001214 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001215 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001216
1217 (*pos)++;
1218
1219 /* can't go backwards */
1220 if (iter->idx > i)
1221 return NULL;
1222
1223 if (iter->idx < 0)
1224 ent = find_next_entry_inc(iter);
1225 else
1226 ent = iter;
1227
1228 while (ent && iter->idx < i)
1229 ent = find_next_entry_inc(iter);
1230
1231 iter->pos = *pos;
1232
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001233 return ent;
1234}
1235
1236static void *s_start(struct seq_file *m, loff_t *pos)
1237{
1238 struct trace_iterator *iter = m->private;
1239 void *p = NULL;
1240 loff_t l = 0;
1241 int i;
1242
1243 mutex_lock(&trace_types_lock);
1244
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001245 if (!current_trace || current_trace != iter->trace) {
1246 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001247 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001248 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001249
1250 atomic_inc(&trace_record_cmdline_disabled);
1251
1252 /* let the tracer grab locks here if needed */
1253 if (current_trace->start)
1254 current_trace->start(iter);
1255
1256 if (*pos != iter->pos) {
1257 iter->ent = NULL;
1258 iter->cpu = 0;
1259 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001260 iter->prev_ent = NULL;
1261 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001262
Steven Rostedtab464282008-05-12 21:21:00 +02001263 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001264 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001265 iter->next_page[i] = NULL;
1266 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001267
1268 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1269 ;
1270
1271 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001272 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001273 p = s_next(m, p, &l);
1274 }
1275
1276 return p;
1277}
1278
1279static void s_stop(struct seq_file *m, void *p)
1280{
1281 struct trace_iterator *iter = m->private;
1282
1283 atomic_dec(&trace_record_cmdline_disabled);
1284
1285 /* let the tracer release locks here if needed */
1286 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1287 iter->trace->stop(iter);
1288
1289 mutex_unlock(&trace_types_lock);
1290}
1291
Abhishek Sagar76094a22008-05-28 00:03:18 +05301292#define KRETPROBE_MSG "[unknown/kretprobe'd]"
1293
1294#ifdef CONFIG_KRETPROBES
1295static inline int kretprobed(unsigned long addr)
1296{
1297 return addr == (unsigned long)kretprobe_trampoline;
1298}
1299#else
1300static inline int kretprobed(unsigned long addr)
1301{
1302 return 0;
1303}
1304#endif /* CONFIG_KRETPROBES */
1305
Steven Rostedtb3806b42008-05-12 21:20:46 +02001306static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001307seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308{
1309#ifdef CONFIG_KALLSYMS
1310 char str[KSYM_SYMBOL_LEN];
1311
1312 kallsyms_lookup(address, NULL, NULL, NULL, str);
1313
Steven Rostedtb3806b42008-05-12 21:20:46 +02001314 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001315#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001316 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001317}
1318
Steven Rostedtb3806b42008-05-12 21:20:46 +02001319static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001320seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1321 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322{
1323#ifdef CONFIG_KALLSYMS
1324 char str[KSYM_SYMBOL_LEN];
1325
1326 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001327 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001328#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001329 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001330}
1331
1332#ifndef CONFIG_64BIT
1333# define IP_FMT "%08lx"
1334#else
1335# define IP_FMT "%016lx"
1336#endif
1337
Ingo Molnare309b412008-05-12 21:20:51 +02001338static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001339seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001340{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001341 int ret;
1342
1343 if (!ip)
1344 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001345
1346 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001347 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001348 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001349 ret = seq_print_sym_short(s, "%s", ip);
1350
1351 if (!ret)
1352 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353
1354 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001355 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1356 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001357}
1358
Ingo Molnare309b412008-05-12 21:20:51 +02001359static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001360{
Michael Ellermana6168352008-08-20 16:36:11 -07001361 seq_puts(m, "# _------=> CPU# \n");
1362 seq_puts(m, "# / _-----=> irqs-off \n");
1363 seq_puts(m, "# | / _----=> need-resched \n");
1364 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1365 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1366 seq_puts(m, "# |||| / \n");
1367 seq_puts(m, "# ||||| delay \n");
1368 seq_puts(m, "# cmd pid ||||| time | caller \n");
1369 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370}
1371
Ingo Molnare309b412008-05-12 21:20:51 +02001372static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373{
Michael Ellermana6168352008-08-20 16:36:11 -07001374 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1375 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001376}
1377
1378
Ingo Molnare309b412008-05-12 21:20:51 +02001379static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1381{
1382 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1383 struct trace_array *tr = iter->tr;
1384 struct trace_array_cpu *data = tr->data[tr->cpu];
1385 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001386 unsigned long total = 0;
1387 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001388 int cpu;
1389 const char *name = "preemption";
1390
1391 if (type)
1392 name = type->name;
1393
Steven Rostedtab464282008-05-12 21:21:00 +02001394 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001395 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001396 total += tr->data[cpu]->trace_idx;
1397 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001398 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001399 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001400 entries += tr->data[cpu]->trace_idx;
1401 }
1402 }
1403
1404 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1405 name, UTS_RELEASE);
1406 seq_puts(m, "-----------------------------------"
1407 "---------------------------------\n");
1408 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1409 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001410 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001411 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001412 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001413 tr->cpu,
1414#if defined(CONFIG_PREEMPT_NONE)
1415 "server",
1416#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1417 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001418#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001419 "preempt",
1420#else
1421 "unknown",
1422#endif
1423 /* These are reserved for later use */
1424 0, 0, 0, 0);
1425#ifdef CONFIG_SMP
1426 seq_printf(m, " #P:%d)\n", num_online_cpus());
1427#else
1428 seq_puts(m, ")\n");
1429#endif
1430 seq_puts(m, " -----------------\n");
1431 seq_printf(m, " | task: %.16s-%d "
1432 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1433 data->comm, data->pid, data->uid, data->nice,
1434 data->policy, data->rt_priority);
1435 seq_puts(m, " -----------------\n");
1436
1437 if (data->critical_start) {
1438 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001439 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1440 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001441 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001442 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1443 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001444 seq_puts(m, "\n");
1445 }
1446
1447 seq_puts(m, "\n");
1448}
1449
Ingo Molnare309b412008-05-12 21:20:51 +02001450static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001451lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001452{
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001453 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001454 int hardirq, softirq;
1455 char *comm;
1456
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001457 comm = trace_find_cmdline(field->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001458
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001459 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001460 trace_seq_printf(s, "%3d", cpu);
Steven Rostedt214023c2008-05-12 21:20:46 +02001461 trace_seq_printf(s, "%c%c",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001462 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1463 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001464
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001465 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1466 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001467 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001468 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001469 } else {
1470 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001471 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001472 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001473 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001474 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001475 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001476 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477 }
1478 }
1479
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001480 if (field->preempt_count)
1481 trace_seq_printf(s, "%x", field->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001482 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001483 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001484}
1485
1486unsigned long preempt_mark_thresh = 100;
1487
Ingo Molnare309b412008-05-12 21:20:51 +02001488static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001489lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001490 unsigned long rel_usecs)
1491{
Steven Rostedt214023c2008-05-12 21:20:46 +02001492 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001493 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001494 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001495 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001496 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001497 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001498 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001499}
1500
1501static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1502
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001503/*
1504 * The message is supposed to contain an ending newline.
1505 * If the printing stops prematurely, try to add a newline of our own.
1506 */
1507void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001508{
1509 struct trace_array *tr = iter->tr;
1510 struct trace_array_cpu *data = tr->data[iter->cpu];
1511 struct trace_entry *ent;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001512 bool ok = true;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001513
1514 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1515 if (!ent || ent->type != TRACE_CONT) {
1516 trace_seq_putc(s, '\n');
1517 return;
1518 }
1519
1520 do {
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001521 if (ok)
1522 ok = (trace_seq_printf(s, "%s", ent->cont.buf) > 0);
Steven Rostedt5a90f572008-09-03 17:42:51 -04001523 __trace_iterator_increment(iter, iter->cpu);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001524 ent = trace_entry_idx(tr, data, iter, iter->cpu);
1525 } while (ent && ent->type == TRACE_CONT);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001526
1527 if (!ok)
1528 trace_seq_putc(s, '\n');
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001529}
1530
Ingo Molnare309b412008-05-12 21:20:51 +02001531static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001532print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001533{
Steven Rostedt214023c2008-05-12 21:20:46 +02001534 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1536 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1537 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1538 struct trace_entry *entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001539 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001540 unsigned long abs_usecs;
1541 unsigned long rel_usecs;
1542 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001543 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001544 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001545 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001546
1547 if (!next_entry)
1548 next_entry = entry;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001549
1550 if (entry->type == TRACE_CONT)
1551 return 1;
1552
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001553 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1554 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001555
1556 if (verbose) {
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001557 comm = trace_find_cmdline(field->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001558 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
Steven Rostedt214023c2008-05-12 21:20:46 +02001559 " %ld.%03ldms (+%ld.%03ldms): ",
1560 comm,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001561 field->pid, cpu, field->flags,
1562 field->preempt_count, trace_idx,
1563 ns2usecs(field->t),
Steven Rostedt214023c2008-05-12 21:20:46 +02001564 abs_usecs/1000,
1565 abs_usecs % 1000, rel_usecs/1000,
1566 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001567 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001568 lat_print_generic(s, entry, cpu);
1569 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001570 }
1571 switch (entry->type) {
1572 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001573 seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001574 trace_seq_puts(s, " (");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001575 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301576 trace_seq_puts(s, KRETPROBE_MSG);
1577 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001578 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001579 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580 break;
1581 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001582 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001583 T = field->ctx.next_state < sizeof(state_to_char) ?
1584 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001585
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001586 state = field->ctx.prev_state ?
1587 __ffs(field->ctx.prev_state) + 1 : 0;
Ankita Gargd17d9692008-05-12 21:20:58 +02001588 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001589 comm = trace_find_cmdline(field->ctx.next_pid);
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001590 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001591 field->ctx.prev_pid,
1592 field->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001593 S, entry->type == TRACE_CTX ? "==>" : " +",
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001594 field->ctx.next_cpu,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001595 field->ctx.next_pid,
1596 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001597 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001598 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001599 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001600 trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001601 field->special.arg1,
1602 field->special.arg2,
1603 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001604 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001605 case TRACE_STACK:
1606 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1607 if (i)
1608 trace_seq_puts(s, " <= ");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001609 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +02001610 }
1611 trace_seq_puts(s, "\n");
1612 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001613 case TRACE_PRINT:
1614 seq_print_ip_sym(s, field->print.ip, sym_flags);
1615 trace_seq_printf(s, ": %s", field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001616 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001617 trace_seq_print_cont(s, iter);
1618 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001619 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001620 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001621 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001622 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001623}
1624
Ingo Molnare309b412008-05-12 21:20:51 +02001625static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001626{
Steven Rostedt214023c2008-05-12 21:20:46 +02001627 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001628 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001629 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001630 struct trace_field *field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001631 unsigned long usec_rem;
1632 unsigned long long t;
1633 unsigned long secs;
1634 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001635 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001636 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001637 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001638
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001639 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001640
1641 if (entry->type == TRACE_CONT)
1642 return 1;
1643
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001644 field = &entry->field;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001645
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001646 comm = trace_find_cmdline(iter->ent->field.pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001647
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001648 t = ns2usecs(field->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001649 usec_rem = do_div(t, 1000000ULL);
1650 secs = (unsigned long)t;
1651
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001652 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001653 if (!ret)
1654 return 0;
Michael Ellermana6168352008-08-20 16:36:11 -07001655 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001656 if (!ret)
1657 return 0;
1658 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1659 if (!ret)
1660 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001661
1662 switch (entry->type) {
1663 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001664 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001665 if (!ret)
1666 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001667 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001668 field->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001669 ret = trace_seq_printf(s, " <-");
1670 if (!ret)
1671 return 0;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001672 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301673 ret = trace_seq_puts(s, KRETPROBE_MSG);
1674 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001675 ret = seq_print_ip_sym(s,
1676 field->fn.parent_ip,
Abhishek Sagar76094a22008-05-28 00:03:18 +05301677 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001678 if (!ret)
1679 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001680 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001681 ret = trace_seq_printf(s, "\n");
1682 if (!ret)
1683 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001684 break;
1685 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001686 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001687 S = field->ctx.prev_state < sizeof(state_to_char) ?
1688 state_to_char[field->ctx.prev_state] : 'X';
1689 T = field->ctx.next_state < sizeof(state_to_char) ?
1690 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001691 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001692 field->ctx.prev_pid,
1693 field->ctx.prev_prio,
Steven Rostedtb3806b42008-05-12 21:20:46 +02001694 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001695 entry->type == TRACE_CTX ? "==>" : " +",
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001696 field->ctx.next_cpu,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001697 field->ctx.next_pid,
1698 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001699 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001700 if (!ret)
1701 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001702 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001703 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001704 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001705 field->special.arg1,
1706 field->special.arg2,
1707 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001708 if (!ret)
1709 return 0;
1710 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001711 case TRACE_STACK:
1712 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1713 if (i) {
1714 ret = trace_seq_puts(s, " <= ");
1715 if (!ret)
1716 return 0;
1717 }
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001718 ret = seq_print_ip_sym(s, field->stack.caller[i],
Ingo Molnar86387f72008-05-12 21:20:51 +02001719 sym_flags);
1720 if (!ret)
1721 return 0;
1722 }
1723 ret = trace_seq_puts(s, "\n");
1724 if (!ret)
1725 return 0;
1726 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001727 case TRACE_PRINT:
1728 seq_print_ip_sym(s, field->print.ip, sym_flags);
1729 trace_seq_printf(s, ": %s", field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001730 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001731 trace_seq_print_cont(s, iter);
1732 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001733 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001734 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001735}
1736
Ingo Molnare309b412008-05-12 21:20:51 +02001737static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001738{
1739 struct trace_seq *s = &iter->seq;
1740 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001741 struct trace_field *field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001742 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001743 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001744
1745 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001746
1747 if (entry->type == TRACE_CONT)
1748 return 1;
1749
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001750 field = &entry->field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001751
1752 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001753 field->pid, iter->cpu, field->t);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001754 if (!ret)
1755 return 0;
1756
1757 switch (entry->type) {
1758 case TRACE_FN:
1759 ret = trace_seq_printf(s, "%x %x\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001760 field->fn.ip,
1761 field->fn.parent_ip);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001762 if (!ret)
1763 return 0;
1764 break;
1765 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001766 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001767 S = field->ctx.prev_state < sizeof(state_to_char) ?
1768 state_to_char[field->ctx.prev_state] : 'X';
1769 T = field->ctx.next_state < sizeof(state_to_char) ?
1770 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001771 if (entry->type == TRACE_WAKE)
1772 S = '+';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001773 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001774 field->ctx.prev_pid,
1775 field->ctx.prev_prio,
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001776 S,
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001777 field->ctx.next_cpu,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001778 field->ctx.next_pid,
1779 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001780 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001781 if (!ret)
1782 return 0;
1783 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001784 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001785 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001786 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001787 field->special.arg1,
1788 field->special.arg2,
1789 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001790 if (!ret)
1791 return 0;
1792 break;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001793 case TRACE_PRINT:
1794 trace_seq_printf(s, "# %lx %s",
1795 field->print.ip, field->print.buf);
Steven Rostedt652567a2008-09-03 17:42:50 -04001796 if (field->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001797 trace_seq_print_cont(s, iter);
1798 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001799 }
1800 return 1;
1801}
1802
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001803#define SEQ_PUT_FIELD_RET(s, x) \
1804do { \
1805 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1806 return 0; \
1807} while (0)
1808
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001809#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1810do { \
1811 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1812 return 0; \
1813} while (0)
1814
Ingo Molnare309b412008-05-12 21:20:51 +02001815static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001816{
1817 struct trace_seq *s = &iter->seq;
1818 unsigned char newline = '\n';
1819 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001820 struct trace_field *field;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001821 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001822
1823 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001824
1825 if (entry->type == TRACE_CONT)
1826 return 1;
1827
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001828 field = &entry->field;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001829
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001830 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001831 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001832 SEQ_PUT_HEX_FIELD_RET(s, field->t);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001833
1834 switch (entry->type) {
1835 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001836 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1837 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001838 break;
1839 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001840 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001841 S = field->ctx.prev_state < sizeof(state_to_char) ?
1842 state_to_char[field->ctx.prev_state] : 'X';
1843 T = field->ctx.next_state < sizeof(state_to_char) ?
1844 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001845 if (entry->type == TRACE_WAKE)
1846 S = '+';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001847 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1848 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001849 SEQ_PUT_HEX_FIELD_RET(s, S);
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001850 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_cpu);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001851 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1852 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001853 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001854 break;
1855 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001856 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001857 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1858 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1859 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001860 break;
1861 }
1862 SEQ_PUT_FIELD_RET(s, newline);
1863
1864 return 1;
1865}
1866
Ingo Molnare309b412008-05-12 21:20:51 +02001867static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001868{
1869 struct trace_seq *s = &iter->seq;
1870 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001871 struct trace_field *field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001872
1873 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001874
1875 if (entry->type == TRACE_CONT)
1876 return 1;
1877
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001878 field = &entry->field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001879
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001880 SEQ_PUT_FIELD_RET(s, field->pid);
1881 SEQ_PUT_FIELD_RET(s, field->cpu);
1882 SEQ_PUT_FIELD_RET(s, field->t);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001883
1884 switch (entry->type) {
1885 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001886 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1887 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001888 break;
1889 case TRACE_CTX:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001890 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1891 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1892 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1893 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1894 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1895 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001896 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001897 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001898 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001899 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1900 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1901 SEQ_PUT_FIELD_RET(s, field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001902 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001903 }
1904 return 1;
1905}
1906
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001907static int trace_empty(struct trace_iterator *iter)
1908{
1909 struct trace_array_cpu *data;
1910 int cpu;
1911
Steven Rostedtab464282008-05-12 21:21:00 +02001912 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001913 data = iter->tr->data[cpu];
1914
Steven Rostedtb3806b42008-05-12 21:20:46 +02001915 if (head_page(data) && data->trace_idx &&
1916 (data->trace_tail != data->trace_head ||
1917 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001918 return 0;
1919 }
1920 return 1;
1921}
1922
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001923static int print_trace_line(struct trace_iterator *iter)
1924{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001925 if (iter->trace && iter->trace->print_line)
1926 return iter->trace->print_line(iter);
1927
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001928 if (trace_flags & TRACE_ITER_BIN)
1929 return print_bin_fmt(iter);
1930
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001931 if (trace_flags & TRACE_ITER_HEX)
1932 return print_hex_fmt(iter);
1933
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001934 if (trace_flags & TRACE_ITER_RAW)
1935 return print_raw_fmt(iter);
1936
1937 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1938 return print_lat_fmt(iter, iter->idx, iter->cpu);
1939
1940 return print_trace_fmt(iter);
1941}
1942
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001943static int s_show(struct seq_file *m, void *v)
1944{
1945 struct trace_iterator *iter = v;
1946
1947 if (iter->ent == NULL) {
1948 if (iter->tr) {
1949 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1950 seq_puts(m, "#\n");
1951 }
1952 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1953 /* print nothing if the buffers are empty */
1954 if (trace_empty(iter))
1955 return 0;
1956 print_trace_header(m, iter);
1957 if (!(trace_flags & TRACE_ITER_VERBOSE))
1958 print_lat_help_header(m);
1959 } else {
1960 if (!(trace_flags & TRACE_ITER_VERBOSE))
1961 print_func_help_header(m);
1962 }
1963 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001964 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001965 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001966 }
1967
1968 return 0;
1969}
1970
1971static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001972 .start = s_start,
1973 .next = s_next,
1974 .stop = s_stop,
1975 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001976};
1977
Ingo Molnare309b412008-05-12 21:20:51 +02001978static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001979__tracing_open(struct inode *inode, struct file *file, int *ret)
1980{
1981 struct trace_iterator *iter;
1982
Steven Rostedt60a11772008-05-12 21:20:44 +02001983 if (tracing_disabled) {
1984 *ret = -ENODEV;
1985 return NULL;
1986 }
1987
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001988 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1989 if (!iter) {
1990 *ret = -ENOMEM;
1991 goto out;
1992 }
1993
1994 mutex_lock(&trace_types_lock);
1995 if (current_trace && current_trace->print_max)
1996 iter->tr = &max_tr;
1997 else
1998 iter->tr = inode->i_private;
1999 iter->trace = current_trace;
2000 iter->pos = -1;
2001
2002 /* TODO stop tracer */
2003 *ret = seq_open(file, &tracer_seq_ops);
2004 if (!*ret) {
2005 struct seq_file *m = file->private_data;
2006 m->private = iter;
2007
2008 /* stop the trace while dumping */
Steven Rostedt60bc0802008-07-10 20:58:16 -04002009 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002010 tracer_enabled = 0;
Steven Rostedt60bc0802008-07-10 20:58:16 -04002011 ftrace_function_enabled = 0;
2012 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002013
2014 if (iter->trace && iter->trace->open)
2015 iter->trace->open(iter);
2016 } else {
2017 kfree(iter);
2018 iter = NULL;
2019 }
2020 mutex_unlock(&trace_types_lock);
2021
2022 out:
2023 return iter;
2024}
2025
2026int tracing_open_generic(struct inode *inode, struct file *filp)
2027{
Steven Rostedt60a11772008-05-12 21:20:44 +02002028 if (tracing_disabled)
2029 return -ENODEV;
2030
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002031 filp->private_data = inode->i_private;
2032 return 0;
2033}
2034
2035int tracing_release(struct inode *inode, struct file *file)
2036{
2037 struct seq_file *m = (struct seq_file *)file->private_data;
2038 struct trace_iterator *iter = m->private;
2039
2040 mutex_lock(&trace_types_lock);
2041 if (iter->trace && iter->trace->close)
2042 iter->trace->close(iter);
2043
2044 /* reenable tracing if it was previously enabled */
Steven Rostedt60bc0802008-07-10 20:58:16 -04002045 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002046 tracer_enabled = 1;
Steven Rostedt60bc0802008-07-10 20:58:16 -04002047 /*
2048 * It is safe to enable function tracing even if it
2049 * isn't used
2050 */
2051 ftrace_function_enabled = 1;
2052 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002053 mutex_unlock(&trace_types_lock);
2054
2055 seq_release(inode, file);
2056 kfree(iter);
2057 return 0;
2058}
2059
2060static int tracing_open(struct inode *inode, struct file *file)
2061{
2062 int ret;
2063
2064 __tracing_open(inode, file, &ret);
2065
2066 return ret;
2067}
2068
2069static int tracing_lt_open(struct inode *inode, struct file *file)
2070{
2071 struct trace_iterator *iter;
2072 int ret;
2073
2074 iter = __tracing_open(inode, file, &ret);
2075
2076 if (!ret)
2077 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2078
2079 return ret;
2080}
2081
2082
Ingo Molnare309b412008-05-12 21:20:51 +02002083static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002084t_next(struct seq_file *m, void *v, loff_t *pos)
2085{
2086 struct tracer *t = m->private;
2087
2088 (*pos)++;
2089
2090 if (t)
2091 t = t->next;
2092
2093 m->private = t;
2094
2095 return t;
2096}
2097
2098static void *t_start(struct seq_file *m, loff_t *pos)
2099{
2100 struct tracer *t = m->private;
2101 loff_t l = 0;
2102
2103 mutex_lock(&trace_types_lock);
2104 for (; t && l < *pos; t = t_next(m, t, &l))
2105 ;
2106
2107 return t;
2108}
2109
2110static void t_stop(struct seq_file *m, void *p)
2111{
2112 mutex_unlock(&trace_types_lock);
2113}
2114
2115static int t_show(struct seq_file *m, void *v)
2116{
2117 struct tracer *t = v;
2118
2119 if (!t)
2120 return 0;
2121
2122 seq_printf(m, "%s", t->name);
2123 if (t->next)
2124 seq_putc(m, ' ');
2125 else
2126 seq_putc(m, '\n');
2127
2128 return 0;
2129}
2130
2131static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002132 .start = t_start,
2133 .next = t_next,
2134 .stop = t_stop,
2135 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002136};
2137
2138static int show_traces_open(struct inode *inode, struct file *file)
2139{
2140 int ret;
2141
Steven Rostedt60a11772008-05-12 21:20:44 +02002142 if (tracing_disabled)
2143 return -ENODEV;
2144
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002145 ret = seq_open(file, &show_traces_seq_ops);
2146 if (!ret) {
2147 struct seq_file *m = file->private_data;
2148 m->private = trace_types;
2149 }
2150
2151 return ret;
2152}
2153
2154static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002155 .open = tracing_open,
2156 .read = seq_read,
2157 .llseek = seq_lseek,
2158 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002159};
2160
2161static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002162 .open = tracing_lt_open,
2163 .read = seq_read,
2164 .llseek = seq_lseek,
2165 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002166};
2167
2168static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002169 .open = show_traces_open,
2170 .read = seq_read,
2171 .release = seq_release,
2172};
2173
Ingo Molnar36dfe922008-05-12 21:20:52 +02002174/*
2175 * Only trace on a CPU if the bitmask is set:
2176 */
2177static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2178
2179/*
2180 * When tracing/tracing_cpu_mask is modified then this holds
2181 * the new bitmask we are about to install:
2182 */
2183static cpumask_t tracing_cpumask_new;
2184
2185/*
2186 * The tracer itself will not take this lock, but still we want
2187 * to provide a consistent cpumask to user-space:
2188 */
2189static DEFINE_MUTEX(tracing_cpumask_update_lock);
2190
2191/*
2192 * Temporary storage for the character representation of the
2193 * CPU bitmask (and one more byte for the newline):
2194 */
2195static char mask_str[NR_CPUS + 1];
2196
Ingo Molnarc7078de2008-05-12 21:20:52 +02002197static ssize_t
2198tracing_cpumask_read(struct file *filp, char __user *ubuf,
2199 size_t count, loff_t *ppos)
2200{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002201 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002202
2203 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002204
2205 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2206 if (count - len < 2) {
2207 count = -EINVAL;
2208 goto out_err;
2209 }
2210 len += sprintf(mask_str + len, "\n");
2211 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2212
2213out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002214 mutex_unlock(&tracing_cpumask_update_lock);
2215
2216 return count;
2217}
2218
2219static ssize_t
2220tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2221 size_t count, loff_t *ppos)
2222{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002223 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002224
2225 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002226 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2227 if (err)
2228 goto err_unlock;
2229
Steven Rostedt92205c22008-05-12 21:20:55 +02002230 raw_local_irq_disable();
2231 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002232 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002233 /*
2234 * Increase/decrease the disabled counter if we are
2235 * about to flip a bit in the cpumask:
2236 */
2237 if (cpu_isset(cpu, tracing_cpumask) &&
2238 !cpu_isset(cpu, tracing_cpumask_new)) {
2239 atomic_inc(&global_trace.data[cpu]->disabled);
2240 }
2241 if (!cpu_isset(cpu, tracing_cpumask) &&
2242 cpu_isset(cpu, tracing_cpumask_new)) {
2243 atomic_dec(&global_trace.data[cpu]->disabled);
2244 }
2245 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002246 __raw_spin_unlock(&ftrace_max_lock);
2247 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002248
2249 tracing_cpumask = tracing_cpumask_new;
2250
Ingo Molnarc7078de2008-05-12 21:20:52 +02002251 mutex_unlock(&tracing_cpumask_update_lock);
2252
Ingo Molnarc7078de2008-05-12 21:20:52 +02002253 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002254
2255err_unlock:
2256 mutex_unlock(&tracing_cpumask_update_lock);
2257
2258 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002259}
2260
2261static struct file_operations tracing_cpumask_fops = {
2262 .open = tracing_open_generic,
2263 .read = tracing_cpumask_read,
2264 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002265};
2266
2267static ssize_t
2268tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2269 size_t cnt, loff_t *ppos)
2270{
2271 char *buf;
2272 int r = 0;
2273 int len = 0;
2274 int i;
2275
2276 /* calulate max size */
2277 for (i = 0; trace_options[i]; i++) {
2278 len += strlen(trace_options[i]);
2279 len += 3; /* "no" and space */
2280 }
2281
2282 /* +2 for \n and \0 */
2283 buf = kmalloc(len + 2, GFP_KERNEL);
2284 if (!buf)
2285 return -ENOMEM;
2286
2287 for (i = 0; trace_options[i]; i++) {
2288 if (trace_flags & (1 << i))
2289 r += sprintf(buf + r, "%s ", trace_options[i]);
2290 else
2291 r += sprintf(buf + r, "no%s ", trace_options[i]);
2292 }
2293
2294 r += sprintf(buf + r, "\n");
2295 WARN_ON(r >= len + 2);
2296
Ingo Molnar36dfe922008-05-12 21:20:52 +02002297 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002298
2299 kfree(buf);
2300
2301 return r;
2302}
2303
2304static ssize_t
2305tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2306 size_t cnt, loff_t *ppos)
2307{
2308 char buf[64];
2309 char *cmp = buf;
2310 int neg = 0;
2311 int i;
2312
Steven Rostedtcffae432008-05-12 21:21:00 +02002313 if (cnt >= sizeof(buf))
2314 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002315
2316 if (copy_from_user(&buf, ubuf, cnt))
2317 return -EFAULT;
2318
2319 buf[cnt] = 0;
2320
2321 if (strncmp(buf, "no", 2) == 0) {
2322 neg = 1;
2323 cmp += 2;
2324 }
2325
2326 for (i = 0; trace_options[i]; i++) {
2327 int len = strlen(trace_options[i]);
2328
2329 if (strncmp(cmp, trace_options[i], len) == 0) {
2330 if (neg)
2331 trace_flags &= ~(1 << i);
2332 else
2333 trace_flags |= (1 << i);
2334 break;
2335 }
2336 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002337 /*
2338 * If no option could be set, return an error:
2339 */
2340 if (!trace_options[i])
2341 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002342
2343 filp->f_pos += cnt;
2344
2345 return cnt;
2346}
2347
2348static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002349 .open = tracing_open_generic,
2350 .read = tracing_iter_ctrl_read,
2351 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002352};
2353
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002354static const char readme_msg[] =
2355 "tracing mini-HOWTO:\n\n"
2356 "# mkdir /debug\n"
2357 "# mount -t debugfs nodev /debug\n\n"
2358 "# cat /debug/tracing/available_tracers\n"
2359 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2360 "# cat /debug/tracing/current_tracer\n"
2361 "none\n"
2362 "# echo sched_switch > /debug/tracing/current_tracer\n"
2363 "# cat /debug/tracing/current_tracer\n"
2364 "sched_switch\n"
2365 "# cat /debug/tracing/iter_ctrl\n"
2366 "noprint-parent nosym-offset nosym-addr noverbose\n"
2367 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2368 "# echo 1 > /debug/tracing/tracing_enabled\n"
2369 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2370 "echo 0 > /debug/tracing/tracing_enabled\n"
2371;
2372
2373static ssize_t
2374tracing_readme_read(struct file *filp, char __user *ubuf,
2375 size_t cnt, loff_t *ppos)
2376{
2377 return simple_read_from_buffer(ubuf, cnt, ppos,
2378 readme_msg, strlen(readme_msg));
2379}
2380
2381static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002382 .open = tracing_open_generic,
2383 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002384};
2385
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002386static ssize_t
2387tracing_ctrl_read(struct file *filp, char __user *ubuf,
2388 size_t cnt, loff_t *ppos)
2389{
2390 struct trace_array *tr = filp->private_data;
2391 char buf[64];
2392 int r;
2393
2394 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002395 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002396}
2397
2398static ssize_t
2399tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2400 size_t cnt, loff_t *ppos)
2401{
2402 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002403 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002404 long val;
2405 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002406
Steven Rostedtcffae432008-05-12 21:21:00 +02002407 if (cnt >= sizeof(buf))
2408 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002409
2410 if (copy_from_user(&buf, ubuf, cnt))
2411 return -EFAULT;
2412
2413 buf[cnt] = 0;
2414
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002415 ret = strict_strtoul(buf, 10, &val);
2416 if (ret < 0)
2417 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002418
2419 val = !!val;
2420
2421 mutex_lock(&trace_types_lock);
2422 if (tr->ctrl ^ val) {
2423 if (val)
2424 tracer_enabled = 1;
2425 else
2426 tracer_enabled = 0;
2427
2428 tr->ctrl = val;
2429
2430 if (current_trace && current_trace->ctrl_update)
2431 current_trace->ctrl_update(tr);
2432 }
2433 mutex_unlock(&trace_types_lock);
2434
2435 filp->f_pos += cnt;
2436
2437 return cnt;
2438}
2439
2440static ssize_t
2441tracing_set_trace_read(struct file *filp, char __user *ubuf,
2442 size_t cnt, loff_t *ppos)
2443{
2444 char buf[max_tracer_type_len+2];
2445 int r;
2446
2447 mutex_lock(&trace_types_lock);
2448 if (current_trace)
2449 r = sprintf(buf, "%s\n", current_trace->name);
2450 else
2451 r = sprintf(buf, "\n");
2452 mutex_unlock(&trace_types_lock);
2453
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002454 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002455}
2456
2457static ssize_t
2458tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2459 size_t cnt, loff_t *ppos)
2460{
2461 struct trace_array *tr = &global_trace;
2462 struct tracer *t;
2463 char buf[max_tracer_type_len+1];
2464 int i;
2465
2466 if (cnt > max_tracer_type_len)
2467 cnt = max_tracer_type_len;
2468
2469 if (copy_from_user(&buf, ubuf, cnt))
2470 return -EFAULT;
2471
2472 buf[cnt] = 0;
2473
2474 /* strip ending whitespace. */
2475 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2476 buf[i] = 0;
2477
2478 mutex_lock(&trace_types_lock);
2479 for (t = trace_types; t; t = t->next) {
2480 if (strcmp(t->name, buf) == 0)
2481 break;
2482 }
2483 if (!t || t == current_trace)
2484 goto out;
2485
2486 if (current_trace && current_trace->reset)
2487 current_trace->reset(tr);
2488
2489 current_trace = t;
2490 if (t->init)
2491 t->init(tr);
2492
2493 out:
2494 mutex_unlock(&trace_types_lock);
2495
2496 filp->f_pos += cnt;
2497
2498 return cnt;
2499}
2500
2501static ssize_t
2502tracing_max_lat_read(struct file *filp, char __user *ubuf,
2503 size_t cnt, loff_t *ppos)
2504{
2505 unsigned long *ptr = filp->private_data;
2506 char buf[64];
2507 int r;
2508
Steven Rostedtcffae432008-05-12 21:21:00 +02002509 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002510 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002511 if (r > sizeof(buf))
2512 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002513 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002514}
2515
2516static ssize_t
2517tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2518 size_t cnt, loff_t *ppos)
2519{
2520 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002521 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002522 long val;
2523 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002524
Steven Rostedtcffae432008-05-12 21:21:00 +02002525 if (cnt >= sizeof(buf))
2526 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002527
2528 if (copy_from_user(&buf, ubuf, cnt))
2529 return -EFAULT;
2530
2531 buf[cnt] = 0;
2532
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002533 ret = strict_strtoul(buf, 10, &val);
2534 if (ret < 0)
2535 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002536
2537 *ptr = val * 1000;
2538
2539 return cnt;
2540}
2541
Steven Rostedtb3806b42008-05-12 21:20:46 +02002542static atomic_t tracing_reader;
2543
2544static int tracing_open_pipe(struct inode *inode, struct file *filp)
2545{
2546 struct trace_iterator *iter;
2547
2548 if (tracing_disabled)
2549 return -ENODEV;
2550
2551 /* We only allow for reader of the pipe */
2552 if (atomic_inc_return(&tracing_reader) != 1) {
2553 atomic_dec(&tracing_reader);
2554 return -EBUSY;
2555 }
2556
2557 /* create a buffer to store the information to pass to userspace */
2558 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2559 if (!iter)
2560 return -ENOMEM;
2561
Steven Rostedt107bad82008-05-12 21:21:01 +02002562 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002563 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002564 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002565 filp->private_data = iter;
2566
Steven Rostedt107bad82008-05-12 21:21:01 +02002567 if (iter->trace->pipe_open)
2568 iter->trace->pipe_open(iter);
2569 mutex_unlock(&trace_types_lock);
2570
Steven Rostedtb3806b42008-05-12 21:20:46 +02002571 return 0;
2572}
2573
2574static int tracing_release_pipe(struct inode *inode, struct file *file)
2575{
2576 struct trace_iterator *iter = file->private_data;
2577
2578 kfree(iter);
2579 atomic_dec(&tracing_reader);
2580
2581 return 0;
2582}
2583
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002584static unsigned int
2585tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2586{
2587 struct trace_iterator *iter = filp->private_data;
2588
2589 if (trace_flags & TRACE_ITER_BLOCK) {
2590 /*
2591 * Always select as readable when in blocking mode
2592 */
2593 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002594 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002595 if (!trace_empty(iter))
2596 return POLLIN | POLLRDNORM;
2597 poll_wait(filp, &trace_wait, poll_table);
2598 if (!trace_empty(iter))
2599 return POLLIN | POLLRDNORM;
2600
2601 return 0;
2602 }
2603}
2604
Steven Rostedtb3806b42008-05-12 21:20:46 +02002605/*
2606 * Consumer reader.
2607 */
2608static ssize_t
2609tracing_read_pipe(struct file *filp, char __user *ubuf,
2610 size_t cnt, loff_t *ppos)
2611{
2612 struct trace_iterator *iter = filp->private_data;
2613 struct trace_array_cpu *data;
2614 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002615 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002616#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002617 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002618#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002619 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002620 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002621
2622 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002623 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2624 if (sret != -EBUSY)
2625 return sret;
2626 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002627
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002628 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002629
Steven Rostedt107bad82008-05-12 21:21:01 +02002630 mutex_lock(&trace_types_lock);
2631 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002632 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2633 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002634 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002635 }
2636
Steven Rostedtb3806b42008-05-12 21:20:46 +02002637 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002638
Steven Rostedt107bad82008-05-12 21:21:01 +02002639 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002640 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002641 goto out;
2642 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002643
Steven Rostedtb3806b42008-05-12 21:20:46 +02002644 /*
2645 * This is a make-shift waitqueue. The reason we don't use
2646 * an actual wait queue is because:
2647 * 1) we only ever have one waiter
2648 * 2) the tracing, traces all functions, we don't want
2649 * the overhead of calling wake_up and friends
2650 * (and tracing them too)
2651 * Anyway, this is really very primitive wakeup.
2652 */
2653 set_current_state(TASK_INTERRUPTIBLE);
2654 iter->tr->waiter = current;
2655
Steven Rostedt107bad82008-05-12 21:21:01 +02002656 mutex_unlock(&trace_types_lock);
2657
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002658 /* sleep for 100 msecs, and try again. */
2659 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002660
Steven Rostedt107bad82008-05-12 21:21:01 +02002661 mutex_lock(&trace_types_lock);
2662
Steven Rostedtb3806b42008-05-12 21:20:46 +02002663 iter->tr->waiter = NULL;
2664
Steven Rostedt107bad82008-05-12 21:21:01 +02002665 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002666 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002667 goto out;
2668 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002669
Steven Rostedt84527992008-05-12 21:20:58 +02002670 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002671 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002672
Steven Rostedtb3806b42008-05-12 21:20:46 +02002673 /*
2674 * We block until we read something and tracing is disabled.
2675 * We still block if tracing is disabled, but we have never
2676 * read anything. This allows a user to cat this file, and
2677 * then enable tracing. But after we have read something,
2678 * we give an EOF when tracing is again disabled.
2679 *
2680 * iter->pos will be 0 if we haven't read anything.
2681 */
2682 if (!tracer_enabled && iter->pos)
2683 break;
2684
2685 continue;
2686 }
2687
2688 /* stop when tracing is finished */
2689 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002690 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002691
2692 if (cnt >= PAGE_SIZE)
2693 cnt = PAGE_SIZE - 1;
2694
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002695 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002696 memset(&iter->seq, 0,
2697 sizeof(struct trace_iterator) -
2698 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002699 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002700
2701 /*
2702 * We need to stop all tracing on all CPUS to read the
2703 * the next buffer. This is a bit expensive, but is
2704 * not done often. We fill all what we can read,
2705 * and then release the locks again.
2706 */
2707
2708 cpus_clear(mask);
2709 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002710#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002711 ftrace_save = ftrace_enabled;
2712 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002713#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002714 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002715 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002716 data = iter->tr->data[cpu];
2717
2718 if (!head_page(data) || !data->trace_idx)
2719 continue;
2720
2721 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002722 cpu_set(cpu, mask);
2723 }
2724
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002725 for_each_cpu_mask(cpu, mask) {
2726 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002727 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002728
2729 if (data->overrun > iter->last_overrun[cpu])
2730 iter->overrun[cpu] +=
2731 data->overrun - iter->last_overrun[cpu];
2732 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002733 }
2734
Steven Rostedt088b1e422008-05-12 21:20:48 +02002735 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002736 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002737 int len = iter->seq.len;
2738
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002739 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002740 if (!ret) {
2741 /* don't print partial lines */
2742 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002743 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002744 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002745
2746 trace_consume(iter);
2747
2748 if (iter->seq.len >= cnt)
2749 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002750 }
2751
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002752 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002753 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002754 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002755 }
2756
2757 for_each_cpu_mask(cpu, mask) {
2758 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002759 atomic_dec(&data->disabled);
2760 }
Ingo Molnar25770462008-05-12 21:20:49 +02002761#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002762 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002763#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002764 local_irq_restore(flags);
2765
2766 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002767 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2768 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002769 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002770 if (sret == -EBUSY)
2771 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002772
Steven Rostedt107bad82008-05-12 21:21:01 +02002773out:
2774 mutex_unlock(&trace_types_lock);
2775
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002776 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002777}
2778
Steven Rostedta98a3c32008-05-12 21:20:59 +02002779static ssize_t
2780tracing_entries_read(struct file *filp, char __user *ubuf,
2781 size_t cnt, loff_t *ppos)
2782{
2783 struct trace_array *tr = filp->private_data;
2784 char buf[64];
2785 int r;
2786
2787 r = sprintf(buf, "%lu\n", tr->entries);
2788 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2789}
2790
2791static ssize_t
2792tracing_entries_write(struct file *filp, const char __user *ubuf,
2793 size_t cnt, loff_t *ppos)
2794{
2795 unsigned long val;
2796 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002797 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002798
Steven Rostedtcffae432008-05-12 21:21:00 +02002799 if (cnt >= sizeof(buf))
2800 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002801
2802 if (copy_from_user(&buf, ubuf, cnt))
2803 return -EFAULT;
2804
2805 buf[cnt] = 0;
2806
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002807 ret = strict_strtoul(buf, 10, &val);
2808 if (ret < 0)
2809 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002810
2811 /* must have at least 1 entry */
2812 if (!val)
2813 return -EINVAL;
2814
2815 mutex_lock(&trace_types_lock);
2816
2817 if (current_trace != &no_tracer) {
2818 cnt = -EBUSY;
2819 pr_info("ftrace: set current_tracer to none"
2820 " before modifying buffer size\n");
2821 goto out;
2822 }
2823
2824 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002825 long pages_requested;
2826 unsigned long freeable_pages;
2827
2828 /* make sure we have enough memory before mapping */
2829 pages_requested =
2830 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2831
2832 /* account for each buffer (and max_tr) */
2833 pages_requested *= tracing_nr_buffers * 2;
2834
2835 /* Check for overflow */
2836 if (pages_requested < 0) {
2837 cnt = -ENOMEM;
2838 goto out;
2839 }
2840
2841 freeable_pages = determine_dirtyable_memory();
2842
2843 /* we only allow to request 1/4 of useable memory */
2844 if (pages_requested >
2845 ((freeable_pages + tracing_pages_allocated) / 4)) {
2846 cnt = -ENOMEM;
2847 goto out;
2848 }
2849
Steven Rostedta98a3c32008-05-12 21:20:59 +02002850 while (global_trace.entries < val) {
2851 if (trace_alloc_page()) {
2852 cnt = -ENOMEM;
2853 goto out;
2854 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002855 /* double check that we don't go over the known pages */
2856 if (tracing_pages_allocated > pages_requested)
2857 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002858 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002859
Steven Rostedta98a3c32008-05-12 21:20:59 +02002860 } else {
2861 /* include the number of entries in val (inc of page entries) */
2862 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2863 trace_free_page();
2864 }
2865
Steven Rostedt19384c02008-05-22 00:22:16 -04002866 /* check integrity */
2867 for_each_tracing_cpu(i)
2868 check_pages(global_trace.data[i]);
2869
Steven Rostedta98a3c32008-05-12 21:20:59 +02002870 filp->f_pos += cnt;
2871
Steven Rostedt19384c02008-05-22 00:22:16 -04002872 /* If check pages failed, return ENOMEM */
2873 if (tracing_disabled)
2874 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002875 out:
2876 max_tr.entries = global_trace.entries;
2877 mutex_unlock(&trace_types_lock);
2878
2879 return cnt;
2880}
2881
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002882static int tracing_open_mark(struct inode *inode, struct file *filp)
2883{
2884 int ret;
2885
2886 ret = tracing_open_generic(inode, filp);
2887 if (ret)
2888 return ret;
2889
2890 if (current_trace == &no_tracer)
2891 return -ENODEV;
2892
2893 return 0;
2894}
2895
2896static int mark_printk(const char *fmt, ...)
2897{
2898 int ret;
2899 va_list args;
2900 va_start(args, fmt);
2901 ret = trace_vprintk(0, fmt, args);
2902 va_end(args);
2903 return ret;
2904}
2905
2906static ssize_t
2907tracing_mark_write(struct file *filp, const char __user *ubuf,
2908 size_t cnt, loff_t *fpos)
2909{
2910 char *buf;
2911 char *end;
2912 struct trace_array *tr = &global_trace;
2913
2914 if (current_trace == &no_tracer || !tr->ctrl || tracing_disabled)
2915 return -EINVAL;
2916
2917 if (cnt > TRACE_BUF_SIZE)
2918 cnt = TRACE_BUF_SIZE;
2919
2920 buf = kmalloc(cnt + 1, GFP_KERNEL);
2921 if (buf == NULL)
2922 return -ENOMEM;
2923
2924 if (copy_from_user(buf, ubuf, cnt)) {
2925 kfree(buf);
2926 return -EFAULT;
2927 }
2928
2929 /* Cut from the first nil or newline. */
2930 buf[cnt] = '\0';
2931 end = strchr(buf, '\n');
2932 if (end)
2933 *end = '\0';
2934
2935 cnt = mark_printk("%s\n", buf);
2936 kfree(buf);
2937 *fpos += cnt;
2938
2939 return cnt;
2940}
2941
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002942static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002943 .open = tracing_open_generic,
2944 .read = tracing_max_lat_read,
2945 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002946};
2947
2948static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002949 .open = tracing_open_generic,
2950 .read = tracing_ctrl_read,
2951 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002952};
2953
2954static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002955 .open = tracing_open_generic,
2956 .read = tracing_set_trace_read,
2957 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002958};
2959
Steven Rostedtb3806b42008-05-12 21:20:46 +02002960static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002961 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002962 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002963 .read = tracing_read_pipe,
2964 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002965};
2966
Steven Rostedta98a3c32008-05-12 21:20:59 +02002967static struct file_operations tracing_entries_fops = {
2968 .open = tracing_open_generic,
2969 .read = tracing_entries_read,
2970 .write = tracing_entries_write,
2971};
2972
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002973static struct file_operations tracing_mark_fops = {
2974 .open = tracing_open_mark,
2975 .write = tracing_mark_write,
2976};
2977
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002978#ifdef CONFIG_DYNAMIC_FTRACE
2979
2980static ssize_t
2981tracing_read_long(struct file *filp, char __user *ubuf,
2982 size_t cnt, loff_t *ppos)
2983{
2984 unsigned long *p = filp->private_data;
2985 char buf[64];
2986 int r;
2987
2988 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002989
2990 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002991}
2992
2993static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002994 .open = tracing_open_generic,
2995 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002996};
2997#endif
2998
2999static struct dentry *d_tracer;
3000
3001struct dentry *tracing_init_dentry(void)
3002{
3003 static int once;
3004
3005 if (d_tracer)
3006 return d_tracer;
3007
3008 d_tracer = debugfs_create_dir("tracing", NULL);
3009
3010 if (!d_tracer && !once) {
3011 once = 1;
3012 pr_warning("Could not create debugfs directory 'tracing'\n");
3013 return NULL;
3014 }
3015
3016 return d_tracer;
3017}
3018
Steven Rostedt60a11772008-05-12 21:20:44 +02003019#ifdef CONFIG_FTRACE_SELFTEST
3020/* Let selftest have access to static functions in this file */
3021#include "trace_selftest.c"
3022#endif
3023
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003024static __init void tracer_init_debugfs(void)
3025{
3026 struct dentry *d_tracer;
3027 struct dentry *entry;
3028
3029 d_tracer = tracing_init_dentry();
3030
3031 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3032 &global_trace, &tracing_ctrl_fops);
3033 if (!entry)
3034 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3035
3036 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
3037 NULL, &tracing_iter_fops);
3038 if (!entry)
3039 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
3040
Ingo Molnarc7078de2008-05-12 21:20:52 +02003041 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3042 NULL, &tracing_cpumask_fops);
3043 if (!entry)
3044 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3045
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003046 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3047 &global_trace, &tracing_lt_fops);
3048 if (!entry)
3049 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3050
3051 entry = debugfs_create_file("trace", 0444, d_tracer,
3052 &global_trace, &tracing_fops);
3053 if (!entry)
3054 pr_warning("Could not create debugfs 'trace' entry\n");
3055
3056 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3057 &global_trace, &show_traces_fops);
3058 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003059 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003060
3061 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3062 &global_trace, &set_tracer_fops);
3063 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003064 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003065
3066 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3067 &tracing_max_latency,
3068 &tracing_max_lat_fops);
3069 if (!entry)
3070 pr_warning("Could not create debugfs "
3071 "'tracing_max_latency' entry\n");
3072
3073 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3074 &tracing_thresh, &tracing_max_lat_fops);
3075 if (!entry)
3076 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003077 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003078 entry = debugfs_create_file("README", 0644, d_tracer,
3079 NULL, &tracing_readme_fops);
3080 if (!entry)
3081 pr_warning("Could not create debugfs 'README' entry\n");
3082
Steven Rostedtb3806b42008-05-12 21:20:46 +02003083 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3084 NULL, &tracing_pipe_fops);
3085 if (!entry)
3086 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003087 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003088
Steven Rostedta98a3c32008-05-12 21:20:59 +02003089 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
3090 &global_trace, &tracing_entries_fops);
3091 if (!entry)
3092 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003093 "'trace_entries' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003094
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003095 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3096 NULL, &tracing_mark_fops);
3097 if (!entry)
3098 pr_warning("Could not create debugfs "
3099 "'trace_marker' entry\n");
3100
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003101#ifdef CONFIG_DYNAMIC_FTRACE
3102 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3103 &ftrace_update_tot_cnt,
3104 &tracing_read_long_fops);
3105 if (!entry)
3106 pr_warning("Could not create debugfs "
3107 "'dyn_ftrace_total_info' entry\n");
3108#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003109#ifdef CONFIG_SYSPROF_TRACER
3110 init_tracer_sysprof_debugfs(d_tracer);
3111#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003112}
3113
Pekka Paalanen801fe402008-09-16 21:58:24 +03003114int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003115{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003116 static DEFINE_SPINLOCK(trace_buf_lock);
3117 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003118
3119 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003120 struct trace_array_cpu *data;
3121 struct trace_entry *entry;
3122 unsigned long flags;
3123 long disabled;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003124 int cpu, len = 0, write, written = 0;
3125
Pekka Paalanen801fe402008-09-16 21:58:24 +03003126 if (current_trace == &no_tracer || !tr->ctrl || tracing_disabled)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003127 return 0;
3128
3129 local_irq_save(flags);
3130 cpu = raw_smp_processor_id();
3131 data = tr->data[cpu];
3132 disabled = atomic_inc_return(&data->disabled);
3133
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003134 if (unlikely(disabled != 1))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003135 goto out;
3136
3137 spin_lock(&trace_buf_lock);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003138 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003139
3140 len = min(len, TRACE_BUF_SIZE-1);
3141 trace_buf[len] = 0;
3142
3143 __raw_spin_lock(&data->lock);
3144 entry = tracing_get_trace_entry(tr, data);
3145 tracing_generic_entry_update(entry, flags);
3146 entry->type = TRACE_PRINT;
3147 entry->field.print.ip = ip;
3148
3149 write = min(len, (int)(TRACE_PRINT_BUF_SIZE-1));
3150
3151 memcpy(&entry->field.print.buf, trace_buf, write);
3152 entry->field.print.buf[write] = 0;
3153 written = write;
3154
3155 if (written != len)
3156 entry->field.flags |= TRACE_FLAG_CONT;
3157
3158 while (written != len) {
3159 entry = tracing_get_trace_entry(tr, data);
3160
3161 entry->type = TRACE_CONT;
3162 write = min(len - written, (int)(TRACE_CONT_BUF_SIZE-1));
3163 memcpy(&entry->cont.buf, trace_buf+written, write);
3164 entry->cont.buf[write] = 0;
3165 written += write;
3166 }
3167 __raw_spin_unlock(&data->lock);
3168
3169 spin_unlock(&trace_buf_lock);
3170
3171 out:
3172 atomic_dec(&data->disabled);
3173 local_irq_restore(flags);
3174
3175 return len;
3176}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003177EXPORT_SYMBOL_GPL(trace_vprintk);
3178
3179int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3180{
3181 int ret;
3182 va_list ap;
3183
3184 if (!(trace_flags & TRACE_ITER_PRINTK))
3185 return 0;
3186
3187 va_start(ap, fmt);
3188 ret = trace_vprintk(ip, fmt, ap);
3189 va_end(ap);
3190 return ret;
3191}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003192EXPORT_SYMBOL_GPL(__ftrace_printk);
3193
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003194static int trace_panic_handler(struct notifier_block *this,
3195 unsigned long event, void *unused)
3196{
3197 ftrace_dump();
3198 return NOTIFY_OK;
3199}
3200
3201static struct notifier_block trace_panic_notifier = {
3202 .notifier_call = trace_panic_handler,
3203 .next = NULL,
3204 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3205};
3206
3207static int trace_die_handler(struct notifier_block *self,
3208 unsigned long val,
3209 void *data)
3210{
3211 switch (val) {
3212 case DIE_OOPS:
3213 ftrace_dump();
3214 break;
3215 default:
3216 break;
3217 }
3218 return NOTIFY_OK;
3219}
3220
3221static struct notifier_block trace_die_notifier = {
3222 .notifier_call = trace_die_handler,
3223 .priority = 200
3224};
3225
3226/*
3227 * printk is set to max of 1024, we really don't need it that big.
3228 * Nothing should be printing 1000 characters anyway.
3229 */
3230#define TRACE_MAX_PRINT 1000
3231
3232/*
3233 * Define here KERN_TRACE so that we have one place to modify
3234 * it if we decide to change what log level the ftrace dump
3235 * should be at.
3236 */
3237#define KERN_TRACE KERN_INFO
3238
3239static void
3240trace_printk_seq(struct trace_seq *s)
3241{
3242 /* Probably should print a warning here. */
3243 if (s->len >= 1000)
3244 s->len = 1000;
3245
3246 /* should be zero ended, but we are paranoid. */
3247 s->buffer[s->len] = 0;
3248
3249 printk(KERN_TRACE "%s", s->buffer);
3250
3251 trace_seq_reset(s);
3252}
3253
3254
3255void ftrace_dump(void)
3256{
3257 static DEFINE_SPINLOCK(ftrace_dump_lock);
3258 /* use static because iter can be a bit big for the stack */
3259 static struct trace_iterator iter;
3260 struct trace_array_cpu *data;
3261 static cpumask_t mask;
3262 static int dump_ran;
3263 unsigned long flags;
3264 int cnt = 0;
3265 int cpu;
3266
3267 /* only one dump */
3268 spin_lock_irqsave(&ftrace_dump_lock, flags);
3269 if (dump_ran)
3270 goto out;
3271
3272 dump_ran = 1;
3273
3274 /* No turning back! */
3275 ftrace_kill_atomic();
3276
3277 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3278
3279 iter.tr = &global_trace;
3280 iter.trace = current_trace;
3281
3282 /*
3283 * We need to stop all tracing on all CPUS to read the
3284 * the next buffer. This is a bit expensive, but is
3285 * not done often. We fill all what we can read,
3286 * and then release the locks again.
3287 */
3288
3289 cpus_clear(mask);
3290
3291 for_each_tracing_cpu(cpu) {
3292 data = iter.tr->data[cpu];
3293
3294 if (!head_page(data) || !data->trace_idx)
3295 continue;
3296
3297 atomic_inc(&data->disabled);
3298 cpu_set(cpu, mask);
3299 }
3300
3301 for_each_cpu_mask(cpu, mask) {
3302 data = iter.tr->data[cpu];
3303 __raw_spin_lock(&data->lock);
3304
3305 if (data->overrun > iter.last_overrun[cpu])
3306 iter.overrun[cpu] +=
3307 data->overrun - iter.last_overrun[cpu];
3308 iter.last_overrun[cpu] = data->overrun;
3309 }
3310
3311 while (!trace_empty(&iter)) {
3312
3313 if (!cnt)
3314 printk(KERN_TRACE "---------------------------------\n");
3315
3316 cnt++;
3317
3318 /* reset all but tr, trace, and overruns */
3319 memset(&iter.seq, 0,
3320 sizeof(struct trace_iterator) -
3321 offsetof(struct trace_iterator, seq));
3322 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3323 iter.pos = -1;
3324
3325 if (find_next_entry_inc(&iter) != NULL) {
3326 print_trace_line(&iter);
3327 trace_consume(&iter);
3328 }
3329
3330 trace_printk_seq(&iter.seq);
3331 }
3332
3333 if (!cnt)
3334 printk(KERN_TRACE " (ftrace buffer empty)\n");
3335 else
3336 printk(KERN_TRACE "---------------------------------\n");
3337
3338 for_each_cpu_mask(cpu, mask) {
3339 data = iter.tr->data[cpu];
3340 __raw_spin_unlock(&data->lock);
3341 }
3342
3343 for_each_cpu_mask(cpu, mask) {
3344 data = iter.tr->data[cpu];
3345 atomic_dec(&data->disabled);
3346 }
3347
3348
3349 out:
3350 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3351}
3352
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003353static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003354{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003355 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003356 struct page *page, *tmp;
3357 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003358 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003359 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003360 int i;
3361
3362 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02003363 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003364 array = (void *)__get_free_page(GFP_KERNEL);
3365 if (array == NULL) {
3366 printk(KERN_ERR "tracer: failed to allocate page"
3367 "for trace buffer!\n");
3368 goto free_pages;
3369 }
3370
Steven Rostedt3eefae92008-05-12 21:21:04 +02003371 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003372 page = virt_to_page(array);
3373 list_add(&page->lru, &pages);
3374
3375/* Only allocate if we are actually using the max trace */
3376#ifdef CONFIG_TRACER_MAX_TRACE
3377 array = (void *)__get_free_page(GFP_KERNEL);
3378 if (array == NULL) {
3379 printk(KERN_ERR "tracer: failed to allocate page"
3380 "for trace buffer!\n");
3381 goto free_pages;
3382 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003383 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003384 page = virt_to_page(array);
3385 list_add(&page->lru, &pages);
3386#endif
3387 }
3388
3389 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02003390 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003391 data = global_trace.data[i];
3392 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003393 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003394 list_add_tail(&page->lru, &data->trace_pages);
3395 ClearPageLRU(page);
3396
3397#ifdef CONFIG_TRACER_MAX_TRACE
3398 data = max_tr.data[i];
3399 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003400 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003401 list_add_tail(&page->lru, &data->trace_pages);
3402 SetPageLRU(page);
3403#endif
3404 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003405 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003406 global_trace.entries += ENTRIES_PER_PAGE;
3407
3408 return 0;
3409
3410 free_pages:
3411 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003412 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003413 __free_page(page);
3414 }
3415 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003416}
3417
Steven Rostedta98a3c32008-05-12 21:20:59 +02003418static int trace_free_page(void)
3419{
3420 struct trace_array_cpu *data;
3421 struct page *page;
3422 struct list_head *p;
3423 int i;
3424 int ret = 0;
3425
3426 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02003427 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003428 data = global_trace.data[i];
3429 p = data->trace_pages.next;
3430 if (p == &data->trace_pages) {
3431 /* should never happen */
3432 WARN_ON(1);
3433 tracing_disabled = 1;
3434 ret = -1;
3435 break;
3436 }
3437 page = list_entry(p, struct page, lru);
3438 ClearPageLRU(page);
3439 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02003440 tracing_pages_allocated--;
3441 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003442 __free_page(page);
3443
3444 tracing_reset(data);
3445
3446#ifdef CONFIG_TRACER_MAX_TRACE
3447 data = max_tr.data[i];
3448 p = data->trace_pages.next;
3449 if (p == &data->trace_pages) {
3450 /* should never happen */
3451 WARN_ON(1);
3452 tracing_disabled = 1;
3453 ret = -1;
3454 break;
3455 }
3456 page = list_entry(p, struct page, lru);
3457 ClearPageLRU(page);
3458 list_del(&page->lru);
3459 __free_page(page);
3460
3461 tracing_reset(data);
3462#endif
3463 }
3464 global_trace.entries -= ENTRIES_PER_PAGE;
3465
3466 return ret;
3467}
3468
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003469__init static int tracer_alloc_buffers(void)
3470{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003471 struct trace_array_cpu *data;
3472 void *array;
3473 struct page *page;
3474 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02003475 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003476 int i;
3477
Steven Rostedtab464282008-05-12 21:21:00 +02003478 /* TODO: make the number of buffers hot pluggable with CPUS */
3479 tracing_nr_buffers = num_possible_cpus();
3480 tracing_buffer_mask = cpu_possible_map;
3481
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003482 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003483 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003484 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003485 max_tr.data[i] = &per_cpu(max_data, i);
3486
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003487 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003488 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003489 printk(KERN_ERR "tracer: failed to allocate page"
3490 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003491 goto free_buffers;
3492 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003493
3494 /* set the array to the list */
3495 INIT_LIST_HEAD(&data->trace_pages);
3496 page = virt_to_page(array);
3497 list_add(&page->lru, &data->trace_pages);
3498 /* use the LRU flag to differentiate the two buffers */
3499 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003500
Steven Rostedta98a3c32008-05-12 21:20:59 +02003501 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3502 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3503
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003504/* Only allocate if we are actually using the max trace */
3505#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003506 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003507 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003508 printk(KERN_ERR "tracer: failed to allocate page"
3509 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003510 goto free_buffers;
3511 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003512
3513 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3514 page = virt_to_page(array);
3515 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3516 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003517#endif
3518 }
3519
3520 /*
3521 * Since we allocate by orders of pages, we may be able to
3522 * round up a bit.
3523 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003524 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003525 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003526
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003527 while (global_trace.entries < trace_nr_entries) {
3528 if (trace_alloc_page())
3529 break;
3530 pages++;
3531 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003532 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003533
Jiri Slaby20764ff2008-06-12 11:27:03 +02003534 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3535 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003536 pr_info(" actual entries %ld\n", global_trace.entries);
3537
3538 tracer_init_debugfs();
3539
3540 trace_init_cmdlines();
3541
3542 register_tracer(&no_tracer);
3543 current_trace = &no_tracer;
3544
Steven Rostedt60a11772008-05-12 21:20:44 +02003545 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003546 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003547 tracing_disabled = 0;
3548
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003549 atomic_notifier_chain_register(&panic_notifier_list,
3550 &trace_panic_notifier);
3551
3552 register_die_notifier(&trace_die_notifier);
3553
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003554 return 0;
3555
3556 free_buffers:
3557 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003558 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003559 struct trace_array_cpu *data = global_trace.data[i];
3560
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003561 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003562 list_for_each_entry_safe(page, tmp,
3563 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003564 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003565 __free_page(page);
3566 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003567 }
3568
3569#ifdef CONFIG_TRACER_MAX_TRACE
3570 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003571 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003572 list_for_each_entry_safe(page, tmp,
3573 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003574 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003575 __free_page(page);
3576 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003577 }
3578#endif
3579 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003580 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003581}
Steven Rostedt60a11772008-05-12 21:20:44 +02003582fs_initcall(tracer_alloc_buffers);