blob: abfa8103d04673c66e9dc10d468ba1d6fe8f6b00 [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>
Steven Rostedt3928a8a2008-09-29 23:02:41 -040036#include <linux/ring_buffer.h>
Frederic Weisbecker21798a82008-10-28 09:43:26 +010037#include <linux/irqflags.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020038
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020039#include "trace.h"
40
Steven Rostedt3928a8a2008-09-29 23:02:41 -040041#define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
42
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020043unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
44unsigned long __read_mostly tracing_thresh;
45
Steven Rostedtd7690412008-10-01 00:29:53 -040046static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
47
48static inline void ftrace_disable_cpu(void)
49{
50 preempt_disable();
51 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
52}
53
54static inline void ftrace_enable_cpu(void)
55{
56 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
57 preempt_enable();
58}
59
Steven Rostedtab464282008-05-12 21:21:00 +020060static cpumask_t __read_mostly tracing_buffer_mask;
61
62#define for_each_tracing_cpu(cpu) \
63 for_each_cpu_mask(cpu, tracing_buffer_mask)
64
Steven Rostedt60a11772008-05-12 21:20:44 +020065static int tracing_disabled = 1;
66
Thomas Gleixner72829bc2008-05-23 21:37:28 +020067long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020068ns2usecs(cycle_t nsec)
69{
70 nsec += 500;
71 do_div(nsec, 1000);
72 return nsec;
73}
74
Ingo Molnare309b412008-05-12 21:20:51 +020075cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020076{
Steven Rostedt3928a8a2008-09-29 23:02:41 -040077 u64 ts = ring_buffer_time_stamp(cpu);
78 ring_buffer_normalize_time_stamp(cpu, &ts);
79 return ts;
Ingo Molnar750ed1a2008-05-12 21:20:46 +020080}
81
Steven Rostedt4fcdae82008-05-12 21:21:00 +020082/*
83 * The global_trace is the descriptor that holds the tracing
84 * buffers for the live tracing. For each CPU, it contains
85 * a link list of pages that will store trace entries. The
86 * page descriptor of the pages in the memory is used to hold
87 * the link list by linking the lru item in the page descriptor
88 * to each of the pages in the buffer per CPU.
89 *
90 * For each active CPU there is a data field that holds the
91 * pages for the buffer for that CPU. Each CPU has the same number
92 * of pages allocated for its buffer.
93 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020094static struct trace_array global_trace;
95
96static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
97
Steven Rostedt4fcdae82008-05-12 21:21:00 +020098/*
99 * The max_tr is used to snapshot the global_trace when a maximum
100 * latency is reached. Some tracers will use this to store a maximum
101 * trace while it continues examining live traces.
102 *
103 * The buffers for the max_tr are set up the same as the global_trace.
104 * When a snapshot is taken, the link list of the max_tr is swapped
105 * with the link list of the global_trace and the buffers are reset for
106 * the global_trace so the tracing can continue.
107 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200108static struct trace_array max_tr;
109
110static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
111
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200112/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200113static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200114
Steven Rostedt60bc0802008-07-10 20:58:16 -0400115/* function tracing enabled */
116int ftrace_function_enabled;
117
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200118/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400119 * trace_buf_size is the size in bytes that is allocated
120 * for a buffer. Note, the number of bytes is always rounded
121 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400122 *
123 * This number is purposely set to a low number of 16384.
124 * If the dump on oops happens, it will be much appreciated
125 * to not have to wait for all that output. Anyway this can be
126 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200127 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400128#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400129
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400130static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200131
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200132/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200133static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200134
135/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200136static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200137
138/*
139 * max_tracer_type_len is used to simplify the allocating of
140 * buffers to read userspace tracer names. We keep track of
141 * the longest tracer name registered.
142 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200143static int max_tracer_type_len;
144
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200145/*
146 * trace_types_lock is used to protect the trace_types list.
147 * This lock is also used to keep user access serialized.
148 * Accesses from userspace will grab this lock while userspace
149 * activities happen inside the kernel.
150 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200151static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200152
153/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200154static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
155
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200156/* trace_flags holds iter_ctrl options */
Ingo Molnar4e655512008-05-12 21:20:52 +0200157unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
158
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200159/**
160 * trace_wake_up - wake up tasks waiting for trace input
161 *
162 * Simply wakes up any task that is blocked on the trace_wait
163 * queue. These is used with trace_poll for tasks polling the trace.
164 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200165void trace_wake_up(void)
166{
Ingo Molnar017730c2008-05-12 21:20:52 +0200167 /*
168 * The runqueue_is_locked() can fail, but this is the best we
169 * have for now:
170 */
171 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200172 wake_up(&trace_wait);
173}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200174
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400175static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200176{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400177 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200178 int ret;
179
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200180 if (!str)
181 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400182 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200183 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400184 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200185 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400186 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200187 return 1;
188}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400189__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200190
Steven Rostedt57f50be2008-05-12 21:20:44 +0200191unsigned long nsecs_to_usecs(unsigned long nsecs)
192{
193 return nsecs / 1000;
194}
195
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200196/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200197 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
198 * control the output of kernel symbols.
199 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200200#define TRACE_ITER_SYM_MASK \
201 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
202
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200203/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200204static const char *trace_options[] = {
205 "print-parent",
206 "sym-offset",
207 "sym-addr",
208 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200209 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200210 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200211 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200212 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200213 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200214 "sched-tree",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200215 "ftrace_printk",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200216 NULL
217};
218
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200219/*
220 * ftrace_max_lock is used to protect the swapping of buffers
221 * when taking a max snapshot. The buffers themselves are
222 * protected by per_cpu spinlocks. But the action of the swap
223 * needs its own lock.
224 *
225 * This is defined as a raw_spinlock_t in order to help
226 * with performance when lockdep debugging is enabled.
227 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200228static raw_spinlock_t ftrace_max_lock =
229 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200230
231/*
232 * Copy the new maximum trace into the separate maximum-trace
233 * structure. (this way the maximum trace is permanently saved,
234 * for later retrieval via /debugfs/tracing/latency_trace)
235 */
Ingo Molnare309b412008-05-12 21:20:51 +0200236static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200237__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
238{
239 struct trace_array_cpu *data = tr->data[cpu];
240
241 max_tr.cpu = cpu;
242 max_tr.time_start = data->preempt_timestamp;
243
244 data = max_tr.data[cpu];
245 data->saved_latency = tracing_max_latency;
246
247 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
248 data->pid = tsk->pid;
249 data->uid = tsk->uid;
250 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
251 data->policy = tsk->policy;
252 data->rt_priority = tsk->rt_priority;
253
254 /* record this tasks comm */
255 tracing_record_cmdline(current);
256}
257
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200258/**
259 * trace_seq_printf - sequence printing of trace information
260 * @s: trace sequence descriptor
261 * @fmt: printf format string
262 *
263 * The tracer may use either sequence operations or its own
264 * copy to user routines. To simplify formating of a trace
265 * trace_seq_printf is used to store strings into a special
266 * buffer (@s). Then the output may be either used by
267 * the sequencer or pulled into another buffer.
268 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200269int
Steven Rostedt214023c2008-05-12 21:20:46 +0200270trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
271{
272 int len = (PAGE_SIZE - 1) - s->len;
273 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200274 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200275
276 if (!len)
277 return 0;
278
279 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200280 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200281 va_end(ap);
282
Steven Rostedtb3806b42008-05-12 21:20:46 +0200283 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200284 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200285 return 0;
286
287 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200288
289 return len;
290}
291
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200292/**
293 * trace_seq_puts - trace sequence printing of simple string
294 * @s: trace sequence descriptor
295 * @str: simple string to record
296 *
297 * The tracer may use either the sequence operations or its own
298 * copy to user routines. This function records a simple string
299 * into a special buffer (@s) for later retrieval by a sequencer
300 * or other mechanism.
301 */
Ingo Molnare309b412008-05-12 21:20:51 +0200302static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200303trace_seq_puts(struct trace_seq *s, const char *str)
304{
305 int len = strlen(str);
306
307 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200308 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200309
310 memcpy(s->buffer + s->len, str, len);
311 s->len += len;
312
313 return len;
314}
315
Ingo Molnare309b412008-05-12 21:20:51 +0200316static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200317trace_seq_putc(struct trace_seq *s, unsigned char c)
318{
319 if (s->len >= (PAGE_SIZE - 1))
320 return 0;
321
322 s->buffer[s->len++] = c;
323
324 return 1;
325}
326
Ingo Molnare309b412008-05-12 21:20:51 +0200327static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200328trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
329{
330 if (len > ((PAGE_SIZE - 1) - s->len))
331 return 0;
332
333 memcpy(s->buffer + s->len, mem, len);
334 s->len += len;
335
336 return len;
337}
338
Harvey Harrisonad0a3b62008-10-08 17:44:55 -0700339#define MAX_MEMHEX_BYTES 8
340#define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200341
Ingo Molnare309b412008-05-12 21:20:51 +0200342static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200343trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
344{
345 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200346 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200347 int i, j;
348
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200349#ifdef __BIG_ENDIAN
350 for (i = 0, j = 0; i < len; i++) {
351#else
352 for (i = len-1, j = 0; i >= 0; i--) {
353#endif
Harvey Harrison2fbc4742008-10-08 16:51:49 -0700354 hex[j++] = hex_asc_hi(data[i]);
355 hex[j++] = hex_asc_lo(data[i]);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200356 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200357 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200358
359 return trace_seq_putmem(s, hex, j);
360}
361
Ingo Molnare309b412008-05-12 21:20:51 +0200362static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200363trace_seq_reset(struct trace_seq *s)
364{
365 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200366 s->readpos = 0;
367}
368
369ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
370{
371 int len;
372 int ret;
373
374 if (s->len <= s->readpos)
375 return -EBUSY;
376
377 len = s->len - s->readpos;
378 if (cnt > len)
379 cnt = len;
380 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
381 if (ret)
382 return -EFAULT;
383
384 s->readpos += len;
385 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200386}
387
Ingo Molnare309b412008-05-12 21:20:51 +0200388static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200389trace_print_seq(struct seq_file *m, struct trace_seq *s)
390{
391 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
392
393 s->buffer[len] = 0;
394 seq_puts(m, s->buffer);
395
396 trace_seq_reset(s);
397}
398
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200399/**
400 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
401 * @tr: tracer
402 * @tsk: the task with the latency
403 * @cpu: The cpu that initiated the trace.
404 *
405 * Flip the buffers between the @tr and the max_tr and record information
406 * about which task was the cause of this latency.
407 */
Ingo Molnare309b412008-05-12 21:20:51 +0200408void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200409update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
410{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400411 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200412
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200413 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200414 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400415
416 tr->buffer = max_tr.buffer;
417 max_tr.buffer = buf;
418
Steven Rostedtd7690412008-10-01 00:29:53 -0400419 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400420 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400421 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200422
423 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200424 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200425}
426
427/**
428 * update_max_tr_single - only copy one trace over, and reset the rest
429 * @tr - tracer
430 * @tsk - task with the latency
431 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200432 *
433 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200434 */
Ingo Molnare309b412008-05-12 21:20:51 +0200435void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200436update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
437{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400438 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200439
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200440 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200441 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200442
Steven Rostedtd7690412008-10-01 00:29:53 -0400443 ftrace_disable_cpu();
444
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400445 ring_buffer_reset(max_tr.buffer);
446 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
447
Steven Rostedtd7690412008-10-01 00:29:53 -0400448 ftrace_enable_cpu();
449
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400450 WARN_ON_ONCE(ret);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200451
452 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200453 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200454}
455
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200456/**
457 * register_tracer - register a tracer with the ftrace system.
458 * @type - the plugin for the tracer
459 *
460 * Register a new plugin tracer.
461 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200462int register_tracer(struct tracer *type)
463{
464 struct tracer *t;
465 int len;
466 int ret = 0;
467
468 if (!type->name) {
469 pr_info("Tracer must have a name\n");
470 return -1;
471 }
472
473 mutex_lock(&trace_types_lock);
474 for (t = trace_types; t; t = t->next) {
475 if (strcmp(type->name, t->name) == 0) {
476 /* already found */
477 pr_info("Trace %s already registered\n",
478 type->name);
479 ret = -1;
480 goto out;
481 }
482 }
483
Steven Rostedt60a11772008-05-12 21:20:44 +0200484#ifdef CONFIG_FTRACE_STARTUP_TEST
485 if (type->selftest) {
486 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200487 struct trace_array *tr = &global_trace;
488 int saved_ctrl = tr->ctrl;
489 int i;
490 /*
491 * Run a selftest on this tracer.
492 * Here we reset the trace buffer, and set the current
493 * tracer to be this tracer. The tracer can then run some
494 * internal tracing to verify that everything is in order.
495 * If we fail, we do not register this tracer.
496 */
Steven Rostedtab464282008-05-12 21:21:00 +0200497 for_each_tracing_cpu(i) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400498 tracing_reset(tr, i);
Steven Rostedt60a11772008-05-12 21:20:44 +0200499 }
500 current_trace = type;
501 tr->ctrl = 0;
502 /* the test is responsible for initializing and enabling */
503 pr_info("Testing tracer %s: ", type->name);
504 ret = type->selftest(type, tr);
505 /* the test is responsible for resetting too */
506 current_trace = saved_tracer;
507 tr->ctrl = saved_ctrl;
508 if (ret) {
509 printk(KERN_CONT "FAILED!\n");
510 goto out;
511 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200512 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200513 for_each_tracing_cpu(i) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400514 tracing_reset(tr, i);
Steven Rostedt1d4db002008-05-12 21:20:45 +0200515 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200516 printk(KERN_CONT "PASSED\n");
517 }
518#endif
519
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200520 type->next = trace_types;
521 trace_types = type;
522 len = strlen(type->name);
523 if (len > max_tracer_type_len)
524 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200525
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200526 out:
527 mutex_unlock(&trace_types_lock);
528
529 return ret;
530}
531
532void unregister_tracer(struct tracer *type)
533{
534 struct tracer **t;
535 int len;
536
537 mutex_lock(&trace_types_lock);
538 for (t = &trace_types; *t; t = &(*t)->next) {
539 if (*t == type)
540 goto found;
541 }
542 pr_info("Trace %s not registered\n", type->name);
543 goto out;
544
545 found:
546 *t = (*t)->next;
547 if (strlen(type->name) != max_tracer_type_len)
548 goto out;
549
550 max_tracer_type_len = 0;
551 for (t = &trace_types; *t; t = &(*t)->next) {
552 len = strlen((*t)->name);
553 if (len > max_tracer_type_len)
554 max_tracer_type_len = len;
555 }
556 out:
557 mutex_unlock(&trace_types_lock);
558}
559
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400560void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200561{
Steven Rostedtd7690412008-10-01 00:29:53 -0400562 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400563 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400564 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200565}
566
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200567#define SAVED_CMDLINES 128
568static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
569static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
570static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
571static int cmdline_idx;
572static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200573
Steven Rostedt25b0b442008-05-12 21:21:00 +0200574/* temporary disable recording */
575atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200576
577static void trace_init_cmdlines(void)
578{
579 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
580 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
581 cmdline_idx = 0;
582}
583
Ingo Molnare309b412008-05-12 21:20:51 +0200584void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200585
Ingo Molnare309b412008-05-12 21:20:51 +0200586static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200587{
588 unsigned map;
589 unsigned idx;
590
591 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
592 return;
593
594 /*
595 * It's not the end of the world if we don't get
596 * the lock, but we also don't want to spin
597 * nor do we want to disable interrupts,
598 * so if we miss here, then better luck next time.
599 */
600 if (!spin_trylock(&trace_cmdline_lock))
601 return;
602
603 idx = map_pid_to_cmdline[tsk->pid];
604 if (idx >= SAVED_CMDLINES) {
605 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
606
607 map = map_cmdline_to_pid[idx];
608 if (map <= PID_MAX_DEFAULT)
609 map_pid_to_cmdline[map] = (unsigned)-1;
610
611 map_pid_to_cmdline[tsk->pid] = idx;
612
613 cmdline_idx = idx;
614 }
615
616 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
617
618 spin_unlock(&trace_cmdline_lock);
619}
620
Ingo Molnare309b412008-05-12 21:20:51 +0200621static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200622{
623 char *cmdline = "<...>";
624 unsigned map;
625
626 if (!pid)
627 return "<idle>";
628
629 if (pid > PID_MAX_DEFAULT)
630 goto out;
631
632 map = map_pid_to_cmdline[pid];
633 if (map >= SAVED_CMDLINES)
634 goto out;
635
636 cmdline = saved_cmdlines[map];
637
638 out:
639 return cmdline;
640}
641
Ingo Molnare309b412008-05-12 21:20:51 +0200642void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200643{
644 if (atomic_read(&trace_record_cmdline_disabled))
645 return;
646
647 trace_save_cmdline(tsk);
648}
649
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300650void
Steven Rostedt38697052008-10-01 13:14:09 -0400651tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
652 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653{
654 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200655
Steven Rostedt777e2082008-09-29 23:02:42 -0400656 entry->preempt_count = pc & 0xff;
657 entry->pid = (tsk) ? tsk->pid : 0;
658 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400659#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400660 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400661#else
662 TRACE_FLAG_IRQS_NOSUPPORT |
663#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200664 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
665 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
666 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
667}
668
Ingo Molnare309b412008-05-12 21:20:51 +0200669void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200670trace_function(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400671 unsigned long ip, unsigned long parent_ip, unsigned long flags,
672 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200673{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400674 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400675 struct ftrace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200676 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200677
Steven Rostedtd7690412008-10-01 00:29:53 -0400678 /* If we are reading the ring buffer, don't trace */
679 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
680 return;
681
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400682 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
683 &irq_flags);
684 if (!event)
685 return;
686 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400687 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400688 entry->ent.type = TRACE_FN;
689 entry->ip = ip;
690 entry->parent_ip = parent_ip;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400691 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200692}
693
Ingo Molnare309b412008-05-12 21:20:51 +0200694void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200695ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400696 unsigned long ip, unsigned long parent_ip, unsigned long flags,
697 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200698{
699 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt38697052008-10-01 13:14:09 -0400700 trace_function(tr, data, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200701}
702
Steven Rostedt38697052008-10-01 13:14:09 -0400703static void ftrace_trace_stack(struct trace_array *tr,
704 struct trace_array_cpu *data,
705 unsigned long flags,
706 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200707{
Al Viroc2c80522008-10-31 19:50:41 +0000708#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400709 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400710 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200711 struct stack_trace trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400712 unsigned long irq_flags;
Ingo Molnar86387f72008-05-12 21:20:51 +0200713
714 if (!(trace_flags & TRACE_ITER_STACKTRACE))
715 return;
716
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400717 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
718 &irq_flags);
719 if (!event)
720 return;
721 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400722 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400723 entry->ent.type = TRACE_STACK;
Ingo Molnar86387f72008-05-12 21:20:51 +0200724
Steven Rostedt777e2082008-09-29 23:02:42 -0400725 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200726
727 trace.nr_entries = 0;
728 trace.max_entries = FTRACE_STACK_ENTRIES;
729 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400730 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200731
732 save_stack_trace(&trace);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400733 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Al Viroc2c80522008-10-31 19:50:41 +0000734#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200735}
736
Steven Rostedt38697052008-10-01 13:14:09 -0400737void __trace_stack(struct trace_array *tr,
738 struct trace_array_cpu *data,
739 unsigned long flags,
740 int skip)
741{
742 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
743}
744
745static void
746ftrace_trace_special(void *__tr, void *__data,
747 unsigned long arg1, unsigned long arg2, unsigned long arg3,
748 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +0200749{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400750 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +0200751 struct trace_array_cpu *data = __data;
752 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -0400753 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +0200754 unsigned long irq_flags;
755
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400756 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
757 &irq_flags);
758 if (!event)
759 return;
760 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400761 tracing_generic_entry_update(&entry->ent, 0, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400762 entry->ent.type = TRACE_SPECIAL;
763 entry->arg1 = arg1;
764 entry->arg2 = arg2;
765 entry->arg3 = arg3;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400766 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -0400767 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +0200768
769 trace_wake_up();
770}
771
772void
Steven Rostedt38697052008-10-01 13:14:09 -0400773__trace_special(void *__tr, void *__data,
774 unsigned long arg1, unsigned long arg2, unsigned long arg3)
775{
776 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
777}
778
779void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200780tracing_sched_switch_trace(struct trace_array *tr,
781 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200782 struct task_struct *prev,
783 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -0400784 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200785{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400786 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400787 struct ctx_switch_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200788 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200789
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400790 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
791 &irq_flags);
792 if (!event)
793 return;
794 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400795 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400796 entry->ent.type = TRACE_CTX;
797 entry->prev_pid = prev->pid;
798 entry->prev_prio = prev->prio;
799 entry->prev_state = prev->state;
800 entry->next_pid = next->pid;
801 entry->next_prio = next->prio;
802 entry->next_state = next->state;
803 entry->next_cpu = task_cpu(next);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400804 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -0400805 ftrace_trace_stack(tr, data, flags, 5, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200806}
807
Ingo Molnar57422792008-05-12 21:20:51 +0200808void
809tracing_sched_wakeup_trace(struct trace_array *tr,
810 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200811 struct task_struct *wakee,
812 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -0400813 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +0200814{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400815 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400816 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +0200817 unsigned long irq_flags;
818
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400819 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
820 &irq_flags);
821 if (!event)
822 return;
823 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400824 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400825 entry->ent.type = TRACE_WAKE;
826 entry->prev_pid = curr->pid;
827 entry->prev_prio = curr->prio;
828 entry->prev_state = curr->state;
829 entry->next_pid = wakee->pid;
830 entry->next_prio = wakee->prio;
831 entry->next_state = wakee->state;
832 entry->next_cpu = task_cpu(wakee);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400833 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -0400834 ftrace_trace_stack(tr, data, flags, 6, pc);
Ingo Molnar017730c2008-05-12 21:20:52 +0200835
836 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200837}
838
Steven Rostedt4902f882008-05-22 00:22:18 -0400839void
840ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
841{
842 struct trace_array *tr = &global_trace;
843 struct trace_array_cpu *data;
Steven Rostedt4902f882008-05-22 00:22:18 -0400844 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -0400845 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -0400846
Frédéric Weisbecker43a15382008-09-21 20:16:30 +0200847 if (tracing_disabled || !tr->ctrl)
Steven Rostedt4902f882008-05-22 00:22:18 -0400848 return;
849
Steven Rostedt38697052008-10-01 13:14:09 -0400850 pc = preempt_count();
851 preempt_disable_notrace();
Steven Rostedt4902f882008-05-22 00:22:18 -0400852 cpu = raw_smp_processor_id();
853 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -0400854
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -0400855 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt38697052008-10-01 13:14:09 -0400856 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -0400857
Steven Rostedt38697052008-10-01 13:14:09 -0400858 preempt_enable_notrace();
Steven Rostedt4902f882008-05-22 00:22:18 -0400859}
860
Steven Rostedt606576c2008-10-06 19:06:12 -0400861#ifdef CONFIG_FUNCTION_TRACER
Ingo Molnare309b412008-05-12 21:20:51 +0200862static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200863function_trace_call(unsigned long ip, unsigned long parent_ip)
864{
865 struct trace_array *tr = &global_trace;
866 struct trace_array_cpu *data;
867 unsigned long flags;
868 long disabled;
Steven Rostedt38697052008-10-01 13:14:09 -0400869 int cpu, resched;
870 int pc;
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200871
Steven Rostedt60bc0802008-07-10 20:58:16 -0400872 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200873 return;
874
Steven Rostedt38697052008-10-01 13:14:09 -0400875 pc = preempt_count();
876 resched = need_resched();
877 preempt_disable_notrace();
878 local_save_flags(flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200879 cpu = raw_smp_processor_id();
880 data = tr->data[cpu];
881 disabled = atomic_inc_return(&data->disabled);
882
883 if (likely(disabled == 1))
Steven Rostedt38697052008-10-01 13:14:09 -0400884 trace_function(tr, data, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200885
886 atomic_dec(&data->disabled);
Steven Rostedt38697052008-10-01 13:14:09 -0400887 if (resched)
888 preempt_enable_no_resched_notrace();
889 else
890 preempt_enable_notrace();
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200891}
892
893static struct ftrace_ops trace_ops __read_mostly =
894{
895 .func = function_trace_call,
896};
897
Ingo Molnare309b412008-05-12 21:20:51 +0200898void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200899{
Steven Rostedt60bc0802008-07-10 20:58:16 -0400900 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200901 register_ftrace_function(&trace_ops);
Steven Rostedt60bc0802008-07-10 20:58:16 -0400902 if (tracer_enabled)
903 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200904}
905
Ingo Molnare309b412008-05-12 21:20:51 +0200906void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200907{
Steven Rostedt60bc0802008-07-10 20:58:16 -0400908 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200909 unregister_ftrace_function(&trace_ops);
910}
911#endif
912
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200913enum trace_file_type {
914 TRACE_FILE_LAT_FMT = 1,
915};
916
Steven Rostedt5a90f572008-09-03 17:42:51 -0400917static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
918{
Steven Rostedtd7690412008-10-01 00:29:53 -0400919 /* Don't allow ftrace to trace into the ring buffers */
920 ftrace_disable_cpu();
921
Steven Rostedt5a90f572008-09-03 17:42:51 -0400922 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -0400923 if (iter->buffer_iter[iter->cpu])
924 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
925
926 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -0400927}
928
Ingo Molnare309b412008-05-12 21:20:51 +0200929static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400930peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400931{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400932 struct ring_buffer_event *event;
933 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400934
Steven Rostedtd7690412008-10-01 00:29:53 -0400935 /* Don't allow ftrace to trace into the ring buffers */
936 ftrace_disable_cpu();
937
938 if (buf_iter)
939 event = ring_buffer_iter_peek(buf_iter, ts);
940 else
941 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
942
943 ftrace_enable_cpu();
944
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400945 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400946}
Steven Rostedtd7690412008-10-01 00:29:53 -0400947
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400948static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400949__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200950{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400951 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200952 struct trace_entry *ent, *next = NULL;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400953 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200954 int next_cpu = -1;
955 int cpu;
956
Steven Rostedtab464282008-05-12 21:21:00 +0200957 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400958
959 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200960 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400961
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400962 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400963
Ingo Molnarcdd31cd22008-05-12 21:20:46 +0200964 /*
965 * Pick the entry with the smallest timestamp:
966 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400967 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200968 next = ent;
969 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400970 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200971 }
972 }
973
974 if (ent_cpu)
975 *ent_cpu = next_cpu;
976
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400977 if (ent_ts)
978 *ent_ts = next_ts;
979
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200980 return next;
981}
982
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400983/* Find the next real entry, without updating the iterator itself */
984static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400985find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200986{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400987 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400988}
Ingo Molnar8c523a92008-05-12 21:20:46 +0200989
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400990/* Find the next real entry, and increment the iterator to the next entry */
991static void *find_next_entry_inc(struct trace_iterator *iter)
992{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400993 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200994
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400995 if (iter->ent)
Steven Rostedtdd0e5452008-08-01 12:26:41 -0400996 trace_iterator_increment(iter, iter->cpu);
997
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400998 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200999}
1000
Ingo Molnare309b412008-05-12 21:20:51 +02001001static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001002{
Steven Rostedtd7690412008-10-01 00:29:53 -04001003 /* Don't allow ftrace to trace into the ring buffers */
1004 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001005 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001006 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001007}
1008
Ingo Molnare309b412008-05-12 21:20:51 +02001009static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001010{
1011 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001012 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001013 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001014
1015 (*pos)++;
1016
1017 /* can't go backwards */
1018 if (iter->idx > i)
1019 return NULL;
1020
1021 if (iter->idx < 0)
1022 ent = find_next_entry_inc(iter);
1023 else
1024 ent = iter;
1025
1026 while (ent && iter->idx < i)
1027 ent = find_next_entry_inc(iter);
1028
1029 iter->pos = *pos;
1030
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001031 return ent;
1032}
1033
1034static void *s_start(struct seq_file *m, loff_t *pos)
1035{
1036 struct trace_iterator *iter = m->private;
1037 void *p = NULL;
1038 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001039 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001040
1041 mutex_lock(&trace_types_lock);
1042
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001043 if (!current_trace || current_trace != iter->trace) {
1044 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001045 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001046 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001047
1048 atomic_inc(&trace_record_cmdline_disabled);
1049
1050 /* let the tracer grab locks here if needed */
1051 if (current_trace->start)
1052 current_trace->start(iter);
1053
1054 if (*pos != iter->pos) {
1055 iter->ent = NULL;
1056 iter->cpu = 0;
1057 iter->idx = -1;
1058
Steven Rostedtd7690412008-10-01 00:29:53 -04001059 ftrace_disable_cpu();
1060
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001061 for_each_tracing_cpu(cpu) {
1062 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001063 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001064
Steven Rostedtd7690412008-10-01 00:29:53 -04001065 ftrace_enable_cpu();
1066
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001067 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1068 ;
1069
1070 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001071 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001072 p = s_next(m, p, &l);
1073 }
1074
1075 return p;
1076}
1077
1078static void s_stop(struct seq_file *m, void *p)
1079{
1080 struct trace_iterator *iter = m->private;
1081
1082 atomic_dec(&trace_record_cmdline_disabled);
1083
1084 /* let the tracer release locks here if needed */
1085 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1086 iter->trace->stop(iter);
1087
1088 mutex_unlock(&trace_types_lock);
1089}
1090
Abhishek Sagar76094a22008-05-28 00:03:18 +05301091#ifdef CONFIG_KRETPROBES
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001092static inline const char *kretprobed(const char *name)
Abhishek Sagar76094a22008-05-28 00:03:18 +05301093{
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001094 static const char tramp_name[] = "kretprobe_trampoline";
1095 int size = sizeof(tramp_name);
1096
1097 if (strncmp(tramp_name, name, size) == 0)
1098 return "[unknown/kretprobe'd]";
1099 return name;
Abhishek Sagar76094a22008-05-28 00:03:18 +05301100}
1101#else
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001102static inline const char *kretprobed(const char *name)
Abhishek Sagar76094a22008-05-28 00:03:18 +05301103{
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001104 return name;
Abhishek Sagar76094a22008-05-28 00:03:18 +05301105}
1106#endif /* CONFIG_KRETPROBES */
1107
Steven Rostedtb3806b42008-05-12 21:20:46 +02001108static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001109seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001110{
1111#ifdef CONFIG_KALLSYMS
1112 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001113 const char *name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001114
1115 kallsyms_lookup(address, NULL, NULL, NULL, str);
1116
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001117 name = kretprobed(str);
1118
1119 return trace_seq_printf(s, fmt, name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001121 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001122}
1123
Steven Rostedtb3806b42008-05-12 21:20:46 +02001124static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001125seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1126 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001127{
1128#ifdef CONFIG_KALLSYMS
1129 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001130 const char *name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001131
1132 sprint_symbol(str, address);
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001133 name = kretprobed(str);
1134
1135 return trace_seq_printf(s, fmt, name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001136#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001137 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001138}
1139
1140#ifndef CONFIG_64BIT
1141# define IP_FMT "%08lx"
1142#else
1143# define IP_FMT "%016lx"
1144#endif
1145
Ingo Molnare309b412008-05-12 21:20:51 +02001146static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001147seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001148{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001149 int ret;
1150
1151 if (!ip)
1152 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001153
1154 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001155 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001156 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001157 ret = seq_print_sym_short(s, "%s", ip);
1158
1159 if (!ret)
1160 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001161
1162 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001163 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1164 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001165}
1166
Ingo Molnare309b412008-05-12 21:20:51 +02001167static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001168{
Michael Ellermana6168352008-08-20 16:36:11 -07001169 seq_puts(m, "# _------=> CPU# \n");
1170 seq_puts(m, "# / _-----=> irqs-off \n");
1171 seq_puts(m, "# | / _----=> need-resched \n");
1172 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1173 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1174 seq_puts(m, "# |||| / \n");
1175 seq_puts(m, "# ||||| delay \n");
1176 seq_puts(m, "# cmd pid ||||| time | caller \n");
1177 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001178}
1179
Ingo Molnare309b412008-05-12 21:20:51 +02001180static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001181{
Michael Ellermana6168352008-08-20 16:36:11 -07001182 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1183 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001184}
1185
1186
Ingo Molnare309b412008-05-12 21:20:51 +02001187static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001188print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1189{
1190 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1191 struct trace_array *tr = iter->tr;
1192 struct trace_array_cpu *data = tr->data[tr->cpu];
1193 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001194 unsigned long total;
1195 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001196 const char *name = "preemption";
1197
1198 if (type)
1199 name = type->name;
1200
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001201 entries = ring_buffer_entries(iter->tr->buffer);
1202 total = entries +
1203 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001204
1205 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1206 name, UTS_RELEASE);
1207 seq_puts(m, "-----------------------------------"
1208 "---------------------------------\n");
1209 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1210 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001211 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001212 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001213 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001214 tr->cpu,
1215#if defined(CONFIG_PREEMPT_NONE)
1216 "server",
1217#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1218 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001219#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001220 "preempt",
1221#else
1222 "unknown",
1223#endif
1224 /* These are reserved for later use */
1225 0, 0, 0, 0);
1226#ifdef CONFIG_SMP
1227 seq_printf(m, " #P:%d)\n", num_online_cpus());
1228#else
1229 seq_puts(m, ")\n");
1230#endif
1231 seq_puts(m, " -----------------\n");
1232 seq_printf(m, " | task: %.16s-%d "
1233 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1234 data->comm, data->pid, data->uid, data->nice,
1235 data->policy, data->rt_priority);
1236 seq_puts(m, " -----------------\n");
1237
1238 if (data->critical_start) {
1239 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001240 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1241 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001242 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001243 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1244 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001245 seq_puts(m, "\n");
1246 }
1247
1248 seq_puts(m, "\n");
1249}
1250
Ingo Molnare309b412008-05-12 21:20:51 +02001251static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001252lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001253{
1254 int hardirq, softirq;
1255 char *comm;
1256
Steven Rostedt777e2082008-09-29 23:02:42 -04001257 comm = trace_find_cmdline(entry->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001258
Steven Rostedt777e2082008-09-29 23:02:42 -04001259 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001260 trace_seq_printf(s, "%3d", cpu);
Steven Rostedt214023c2008-05-12 21:20:46 +02001261 trace_seq_printf(s, "%c%c",
Steven Rostedt92444892008-10-24 09:42:59 -04001262 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1263 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
Steven Rostedt777e2082008-09-29 23:02:42 -04001264 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001265
Steven Rostedt777e2082008-09-29 23:02:42 -04001266 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1267 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001268 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001269 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001270 } else {
1271 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001272 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001273 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001274 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001275 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001277 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001278 }
1279 }
1280
Steven Rostedt777e2082008-09-29 23:02:42 -04001281 if (entry->preempt_count)
1282 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001284 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001285}
1286
1287unsigned long preempt_mark_thresh = 100;
1288
Ingo Molnare309b412008-05-12 21:20:51 +02001289static void
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001290lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001291 unsigned long rel_usecs)
1292{
Steven Rostedt214023c2008-05-12 21:20:46 +02001293 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001294 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001295 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001296 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001297 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001298 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001299 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001300}
1301
1302static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1303
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001304/*
1305 * The message is supposed to contain an ending newline.
1306 * If the printing stops prematurely, try to add a newline of our own.
1307 */
1308void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001309{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001310 struct trace_entry *ent;
Steven Rostedt777e2082008-09-29 23:02:42 -04001311 struct trace_field_cont *cont;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001312 bool ok = true;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001313
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001314 ent = peek_next_entry(iter, iter->cpu, NULL);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001315 if (!ent || ent->type != TRACE_CONT) {
1316 trace_seq_putc(s, '\n');
1317 return;
1318 }
1319
1320 do {
Steven Rostedt777e2082008-09-29 23:02:42 -04001321 cont = (struct trace_field_cont *)ent;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001322 if (ok)
Steven Rostedt777e2082008-09-29 23:02:42 -04001323 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04001324
1325 ftrace_disable_cpu();
1326
1327 if (iter->buffer_iter[iter->cpu])
1328 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1329 else
1330 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1331
1332 ftrace_enable_cpu();
1333
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001334 ent = peek_next_entry(iter, iter->cpu, NULL);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001335 } while (ent && ent->type == TRACE_CONT);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001336
1337 if (!ok)
1338 trace_seq_putc(s, '\n');
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001339}
1340
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001341static enum print_line_t
Steven Rostedt214023c2008-05-12 21:20:46 +02001342print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001343{
Steven Rostedt214023c2008-05-12 21:20:46 +02001344 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001345 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001346 struct trace_entry *next_entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001347 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1348 struct trace_entry *entry = iter->ent;
1349 unsigned long abs_usecs;
1350 unsigned long rel_usecs;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001351 u64 next_ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001352 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001353 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001354 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001355 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001356
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001357 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001358 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001359
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001360 next_entry = find_next_entry(iter, NULL, &next_ts);
1361 if (!next_entry)
1362 next_ts = iter->ts;
1363 rel_usecs = ns2usecs(next_ts - iter->ts);
1364 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001365
1366 if (verbose) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001367 comm = trace_find_cmdline(entry->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001368 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
Steven Rostedt214023c2008-05-12 21:20:46 +02001369 " %ld.%03ldms (+%ld.%03ldms): ",
1370 comm,
Steven Rostedt777e2082008-09-29 23:02:42 -04001371 entry->pid, cpu, entry->flags,
1372 entry->preempt_count, trace_idx,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001373 ns2usecs(iter->ts),
Steven Rostedt214023c2008-05-12 21:20:46 +02001374 abs_usecs/1000,
1375 abs_usecs % 1000, rel_usecs/1000,
1376 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001377 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001378 lat_print_generic(s, entry, cpu);
1379 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380 }
1381 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001382 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001383 struct ftrace_entry *field;
1384
1385 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001386
1387 seq_print_ip_sym(s, field->ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001388 trace_seq_puts(s, " (");
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001389 seq_print_ip_sym(s, field->parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001390 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001391 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001392 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001393 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001394 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001395 struct ctx_switch_entry *field;
1396
1397 trace_assign_type(field, entry);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001398
Steven Rostedt777e2082008-09-29 23:02:42 -04001399 T = field->next_state < sizeof(state_to_char) ?
1400 state_to_char[field->next_state] : 'X';
1401
1402 state = field->prev_state ?
1403 __ffs(field->prev_state) + 1 : 0;
Ankita Gargd17d9692008-05-12 21:20:58 +02001404 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedt777e2082008-09-29 23:02:42 -04001405 comm = trace_find_cmdline(field->next_pid);
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001406 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001407 field->prev_pid,
1408 field->prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001409 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt777e2082008-09-29 23:02:42 -04001410 field->next_cpu,
1411 field->next_pid,
1412 field->next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001413 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001414 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001415 }
1416 case TRACE_SPECIAL: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001417 struct special_entry *field;
1418
1419 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001420
Ingo Molnar88a42162008-05-12 21:20:53 +02001421 trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001422 field->arg1,
1423 field->arg2,
1424 field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001425 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001426 }
1427 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001428 struct stack_entry *field;
1429
1430 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001431
Ingo Molnar86387f72008-05-12 21:20:51 +02001432 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1433 if (i)
1434 trace_seq_puts(s, " <= ");
Steven Rostedt777e2082008-09-29 23:02:42 -04001435 seq_print_ip_sym(s, field->caller[i], sym_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +02001436 }
1437 trace_seq_puts(s, "\n");
1438 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001439 }
1440 case TRACE_PRINT: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001441 struct print_entry *field;
1442
1443 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001444
1445 seq_print_ip_sym(s, field->ip, sym_flags);
1446 trace_seq_printf(s, ": %s", field->buf);
1447 if (entry->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001448 trace_seq_print_cont(s, iter);
1449 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001450 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02001451 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001452 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001453 }
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001454 return TRACE_TYPE_HANDLED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455}
1456
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001457static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001458{
Steven Rostedt214023c2008-05-12 21:20:46 +02001459 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001460 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001461 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001462 unsigned long usec_rem;
1463 unsigned long long t;
1464 unsigned long secs;
1465 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001466 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001467 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001468 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001469
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001470 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001471
1472 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001473 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001474
Steven Rostedt777e2082008-09-29 23:02:42 -04001475 comm = trace_find_cmdline(iter->ent->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001476
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001477 t = ns2usecs(iter->ts);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001478 usec_rem = do_div(t, 1000000ULL);
1479 secs = (unsigned long)t;
1480
Steven Rostedt777e2082008-09-29 23:02:42 -04001481 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001482 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001483 return TRACE_TYPE_PARTIAL_LINE;
Michael Ellermana6168352008-08-20 16:36:11 -07001484 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001485 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001486 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001487 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1488 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001489 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001490
1491 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001492 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001493 struct ftrace_entry *field;
1494
1495 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001496
1497 ret = seq_print_ip_sym(s, field->ip, sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001498 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001499 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001500 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
Steven Rostedt777e2082008-09-29 23:02:42 -04001501 field->parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001502 ret = trace_seq_printf(s, " <-");
1503 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001504 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001505 ret = seq_print_ip_sym(s,
1506 field->parent_ip,
1507 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001508 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001509 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001510 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001511 ret = trace_seq_printf(s, "\n");
1512 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001513 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001514 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001515 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001516 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001517 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001518 struct ctx_switch_entry *field;
1519
1520 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001521
1522 S = field->prev_state < sizeof(state_to_char) ?
1523 state_to_char[field->prev_state] : 'X';
1524 T = field->next_state < sizeof(state_to_char) ?
1525 state_to_char[field->next_state] : 'X';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001526 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001527 field->prev_pid,
1528 field->prev_prio,
Steven Rostedtb3806b42008-05-12 21:20:46 +02001529 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001530 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt777e2082008-09-29 23:02:42 -04001531 field->next_cpu,
1532 field->next_pid,
1533 field->next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001534 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001535 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001536 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001537 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001538 }
1539 case TRACE_SPECIAL: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001540 struct special_entry *field;
1541
1542 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001543
Ingo Molnar88a42162008-05-12 21:20:53 +02001544 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001545 field->arg1,
1546 field->arg2,
1547 field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001548 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001549 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001550 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001551 }
1552 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001553 struct stack_entry *field;
1554
1555 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001556
Ingo Molnar86387f72008-05-12 21:20:51 +02001557 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1558 if (i) {
1559 ret = trace_seq_puts(s, " <= ");
1560 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001561 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnar86387f72008-05-12 21:20:51 +02001562 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001563 ret = seq_print_ip_sym(s, field->caller[i],
Ingo Molnar86387f72008-05-12 21:20:51 +02001564 sym_flags);
1565 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001566 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnar86387f72008-05-12 21:20:51 +02001567 }
1568 ret = trace_seq_puts(s, "\n");
1569 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001570 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnar86387f72008-05-12 21:20:51 +02001571 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001572 }
1573 case TRACE_PRINT: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001574 struct print_entry *field;
1575
1576 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001577
1578 seq_print_ip_sym(s, field->ip, sym_flags);
1579 trace_seq_printf(s, ": %s", field->buf);
1580 if (entry->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001581 trace_seq_print_cont(s, iter);
1582 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001583 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001584 }
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001585 return TRACE_TYPE_HANDLED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001586}
1587
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001588static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001589{
1590 struct trace_seq *s = &iter->seq;
1591 struct trace_entry *entry;
1592 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001593 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001594
1595 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001596
1597 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001598 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001599
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001600 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt777e2082008-09-29 23:02:42 -04001601 entry->pid, iter->cpu, iter->ts);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001602 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001603 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001604
1605 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001606 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001607 struct ftrace_entry *field;
1608
1609 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001610
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001611 ret = trace_seq_printf(s, "%x %x\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001612 field->ip,
1613 field->parent_ip);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001614 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001615 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001616 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001617 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001618 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001619 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001620 struct ctx_switch_entry *field;
1621
1622 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001623
1624 S = field->prev_state < sizeof(state_to_char) ?
1625 state_to_char[field->prev_state] : 'X';
1626 T = field->next_state < sizeof(state_to_char) ?
1627 state_to_char[field->next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001628 if (entry->type == TRACE_WAKE)
1629 S = '+';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001630 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001631 field->prev_pid,
1632 field->prev_prio,
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001633 S,
Steven Rostedt777e2082008-09-29 23:02:42 -04001634 field->next_cpu,
1635 field->next_pid,
1636 field->next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001637 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001638 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001639 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001640 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001641 }
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001642 case TRACE_SPECIAL:
Steven Rostedt777e2082008-09-29 23:02:42 -04001643 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001644 struct special_entry *field;
1645
1646 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001647
Ingo Molnar88a42162008-05-12 21:20:53 +02001648 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001649 field->arg1,
1650 field->arg2,
1651 field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001652 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001653 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001654 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001655 }
1656 case TRACE_PRINT: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001657 struct print_entry *field;
1658
1659 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001660
1661 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1662 if (entry->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001663 trace_seq_print_cont(s, iter);
1664 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001665 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001666 }
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001667 return TRACE_TYPE_HANDLED;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001668}
1669
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001670#define SEQ_PUT_FIELD_RET(s, x) \
1671do { \
1672 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1673 return 0; \
1674} while (0)
1675
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001676#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1677do { \
Harvey Harrisonad0a3b62008-10-08 17:44:55 -07001678 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001679 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1680 return 0; \
1681} while (0)
1682
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001683static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001684{
1685 struct trace_seq *s = &iter->seq;
1686 unsigned char newline = '\n';
1687 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001688 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001689
1690 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001691
1692 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001693 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001694
Steven Rostedt777e2082008-09-29 23:02:42 -04001695 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001696 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001697 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001698
1699 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001700 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001701 struct ftrace_entry *field;
1702
1703 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001704
1705 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1706 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001707 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001708 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001709 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001710 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001711 struct ctx_switch_entry *field;
1712
1713 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001714
1715 S = field->prev_state < sizeof(state_to_char) ?
1716 state_to_char[field->prev_state] : 'X';
1717 T = field->next_state < sizeof(state_to_char) ?
1718 state_to_char[field->next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001719 if (entry->type == TRACE_WAKE)
1720 S = '+';
Steven Rostedt777e2082008-09-29 23:02:42 -04001721 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1722 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001723 SEQ_PUT_HEX_FIELD_RET(s, S);
Steven Rostedt777e2082008-09-29 23:02:42 -04001724 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1725 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1726 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001727 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001728 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001729 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001730 case TRACE_SPECIAL:
Steven Rostedt777e2082008-09-29 23:02:42 -04001731 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001732 struct special_entry *field;
1733
1734 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001735
1736 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1737 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1738 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001739 break;
1740 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001741 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001742 SEQ_PUT_FIELD_RET(s, newline);
1743
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001744 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001745}
1746
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001747static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001748{
1749 struct trace_seq *s = &iter->seq;
1750 struct trace_entry *entry;
1751
1752 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001753
1754 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001755 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001756
Steven Rostedt777e2082008-09-29 23:02:42 -04001757 SEQ_PUT_FIELD_RET(s, entry->pid);
1758 SEQ_PUT_FIELD_RET(s, iter->cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001759 SEQ_PUT_FIELD_RET(s, iter->ts);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001760
1761 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001762 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001763 struct ftrace_entry *field;
1764
1765 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001766
1767 SEQ_PUT_FIELD_RET(s, field->ip);
1768 SEQ_PUT_FIELD_RET(s, field->parent_ip);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001769 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001770 }
1771 case TRACE_CTX: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001772 struct ctx_switch_entry *field;
1773
1774 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001775
1776 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1777 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1778 SEQ_PUT_FIELD_RET(s, field->prev_state);
1779 SEQ_PUT_FIELD_RET(s, field->next_pid);
1780 SEQ_PUT_FIELD_RET(s, field->next_prio);
1781 SEQ_PUT_FIELD_RET(s, field->next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001782 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001783 }
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001784 case TRACE_SPECIAL:
Steven Rostedt777e2082008-09-29 23:02:42 -04001785 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001786 struct special_entry *field;
1787
1788 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001789
1790 SEQ_PUT_FIELD_RET(s, field->arg1);
1791 SEQ_PUT_FIELD_RET(s, field->arg2);
1792 SEQ_PUT_FIELD_RET(s, field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001793 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001794 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001795 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001796 return 1;
1797}
1798
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001799static int trace_empty(struct trace_iterator *iter)
1800{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001801 int cpu;
1802
Steven Rostedtab464282008-05-12 21:21:00 +02001803 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001804 if (iter->buffer_iter[cpu]) {
1805 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1806 return 0;
1807 } else {
1808 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1809 return 0;
1810 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001811 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001812
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001813 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001814}
1815
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001816static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001817{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001818 enum print_line_t ret;
1819
1820 if (iter->trace && iter->trace->print_line) {
1821 ret = iter->trace->print_line(iter);
1822 if (ret != TRACE_TYPE_UNHANDLED)
1823 return ret;
1824 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001825
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001826 if (trace_flags & TRACE_ITER_BIN)
1827 return print_bin_fmt(iter);
1828
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001829 if (trace_flags & TRACE_ITER_HEX)
1830 return print_hex_fmt(iter);
1831
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001832 if (trace_flags & TRACE_ITER_RAW)
1833 return print_raw_fmt(iter);
1834
1835 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1836 return print_lat_fmt(iter, iter->idx, iter->cpu);
1837
1838 return print_trace_fmt(iter);
1839}
1840
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001841static int s_show(struct seq_file *m, void *v)
1842{
1843 struct trace_iterator *iter = v;
1844
1845 if (iter->ent == NULL) {
1846 if (iter->tr) {
1847 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1848 seq_puts(m, "#\n");
1849 }
1850 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1851 /* print nothing if the buffers are empty */
1852 if (trace_empty(iter))
1853 return 0;
1854 print_trace_header(m, iter);
1855 if (!(trace_flags & TRACE_ITER_VERBOSE))
1856 print_lat_help_header(m);
1857 } else {
1858 if (!(trace_flags & TRACE_ITER_VERBOSE))
1859 print_func_help_header(m);
1860 }
1861 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001862 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001863 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001864 }
1865
1866 return 0;
1867}
1868
1869static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001870 .start = s_start,
1871 .next = s_next,
1872 .stop = s_stop,
1873 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001874};
1875
Ingo Molnare309b412008-05-12 21:20:51 +02001876static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001877__tracing_open(struct inode *inode, struct file *file, int *ret)
1878{
1879 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001880 struct seq_file *m;
1881 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001882
Steven Rostedt60a11772008-05-12 21:20:44 +02001883 if (tracing_disabled) {
1884 *ret = -ENODEV;
1885 return NULL;
1886 }
1887
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001888 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1889 if (!iter) {
1890 *ret = -ENOMEM;
1891 goto out;
1892 }
1893
1894 mutex_lock(&trace_types_lock);
1895 if (current_trace && current_trace->print_max)
1896 iter->tr = &max_tr;
1897 else
1898 iter->tr = inode->i_private;
1899 iter->trace = current_trace;
1900 iter->pos = -1;
1901
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001902 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001903
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001904 iter->buffer_iter[cpu] =
1905 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001906
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001907 if (!iter->buffer_iter[cpu])
1908 goto fail_buffer;
1909 }
1910
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001911 /* TODO stop tracer */
1912 *ret = seq_open(file, &tracer_seq_ops);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001913 if (*ret)
1914 goto fail_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001915
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001916 m = file->private_data;
1917 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001918
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001919 /* stop the trace while dumping */
1920 if (iter->tr->ctrl) {
1921 tracer_enabled = 0;
1922 ftrace_function_enabled = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001923 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001924
1925 if (iter->trace && iter->trace->open)
1926 iter->trace->open(iter);
1927
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001928 mutex_unlock(&trace_types_lock);
1929
1930 out:
1931 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001932
1933 fail_buffer:
1934 for_each_tracing_cpu(cpu) {
1935 if (iter->buffer_iter[cpu])
1936 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1937 }
1938 mutex_unlock(&trace_types_lock);
1939
1940 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001941}
1942
1943int tracing_open_generic(struct inode *inode, struct file *filp)
1944{
Steven Rostedt60a11772008-05-12 21:20:44 +02001945 if (tracing_disabled)
1946 return -ENODEV;
1947
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001948 filp->private_data = inode->i_private;
1949 return 0;
1950}
1951
1952int tracing_release(struct inode *inode, struct file *file)
1953{
1954 struct seq_file *m = (struct seq_file *)file->private_data;
1955 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001956 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001957
1958 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001959 for_each_tracing_cpu(cpu) {
1960 if (iter->buffer_iter[cpu])
1961 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1962 }
1963
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001964 if (iter->trace && iter->trace->close)
1965 iter->trace->close(iter);
1966
1967 /* reenable tracing if it was previously enabled */
Steven Rostedt60bc0802008-07-10 20:58:16 -04001968 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001969 tracer_enabled = 1;
Steven Rostedt60bc0802008-07-10 20:58:16 -04001970 /*
1971 * It is safe to enable function tracing even if it
1972 * isn't used
1973 */
1974 ftrace_function_enabled = 1;
1975 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001976 mutex_unlock(&trace_types_lock);
1977
1978 seq_release(inode, file);
1979 kfree(iter);
1980 return 0;
1981}
1982
1983static int tracing_open(struct inode *inode, struct file *file)
1984{
1985 int ret;
1986
1987 __tracing_open(inode, file, &ret);
1988
1989 return ret;
1990}
1991
1992static int tracing_lt_open(struct inode *inode, struct file *file)
1993{
1994 struct trace_iterator *iter;
1995 int ret;
1996
1997 iter = __tracing_open(inode, file, &ret);
1998
1999 if (!ret)
2000 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2001
2002 return ret;
2003}
2004
2005
Ingo Molnare309b412008-05-12 21:20:51 +02002006static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002007t_next(struct seq_file *m, void *v, loff_t *pos)
2008{
2009 struct tracer *t = m->private;
2010
2011 (*pos)++;
2012
2013 if (t)
2014 t = t->next;
2015
2016 m->private = t;
2017
2018 return t;
2019}
2020
2021static void *t_start(struct seq_file *m, loff_t *pos)
2022{
2023 struct tracer *t = m->private;
2024 loff_t l = 0;
2025
2026 mutex_lock(&trace_types_lock);
2027 for (; t && l < *pos; t = t_next(m, t, &l))
2028 ;
2029
2030 return t;
2031}
2032
2033static void t_stop(struct seq_file *m, void *p)
2034{
2035 mutex_unlock(&trace_types_lock);
2036}
2037
2038static int t_show(struct seq_file *m, void *v)
2039{
2040 struct tracer *t = v;
2041
2042 if (!t)
2043 return 0;
2044
2045 seq_printf(m, "%s", t->name);
2046 if (t->next)
2047 seq_putc(m, ' ');
2048 else
2049 seq_putc(m, '\n');
2050
2051 return 0;
2052}
2053
2054static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002055 .start = t_start,
2056 .next = t_next,
2057 .stop = t_stop,
2058 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002059};
2060
2061static int show_traces_open(struct inode *inode, struct file *file)
2062{
2063 int ret;
2064
Steven Rostedt60a11772008-05-12 21:20:44 +02002065 if (tracing_disabled)
2066 return -ENODEV;
2067
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002068 ret = seq_open(file, &show_traces_seq_ops);
2069 if (!ret) {
2070 struct seq_file *m = file->private_data;
2071 m->private = trace_types;
2072 }
2073
2074 return ret;
2075}
2076
2077static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002078 .open = tracing_open,
2079 .read = seq_read,
2080 .llseek = seq_lseek,
2081 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002082};
2083
2084static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002085 .open = tracing_lt_open,
2086 .read = seq_read,
2087 .llseek = seq_lseek,
2088 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002089};
2090
2091static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002092 .open = show_traces_open,
2093 .read = seq_read,
2094 .release = seq_release,
2095};
2096
Ingo Molnar36dfe922008-05-12 21:20:52 +02002097/*
2098 * Only trace on a CPU if the bitmask is set:
2099 */
2100static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2101
2102/*
2103 * When tracing/tracing_cpu_mask is modified then this holds
2104 * the new bitmask we are about to install:
2105 */
2106static cpumask_t tracing_cpumask_new;
2107
2108/*
2109 * The tracer itself will not take this lock, but still we want
2110 * to provide a consistent cpumask to user-space:
2111 */
2112static DEFINE_MUTEX(tracing_cpumask_update_lock);
2113
2114/*
2115 * Temporary storage for the character representation of the
2116 * CPU bitmask (and one more byte for the newline):
2117 */
2118static char mask_str[NR_CPUS + 1];
2119
Ingo Molnarc7078de2008-05-12 21:20:52 +02002120static ssize_t
2121tracing_cpumask_read(struct file *filp, char __user *ubuf,
2122 size_t count, loff_t *ppos)
2123{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002124 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002125
2126 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002127
2128 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2129 if (count - len < 2) {
2130 count = -EINVAL;
2131 goto out_err;
2132 }
2133 len += sprintf(mask_str + len, "\n");
2134 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2135
2136out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002137 mutex_unlock(&tracing_cpumask_update_lock);
2138
2139 return count;
2140}
2141
2142static ssize_t
2143tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2144 size_t count, loff_t *ppos)
2145{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002146 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002147
2148 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002149 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2150 if (err)
2151 goto err_unlock;
2152
Steven Rostedt92205c22008-05-12 21:20:55 +02002153 raw_local_irq_disable();
2154 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002155 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002156 /*
2157 * Increase/decrease the disabled counter if we are
2158 * about to flip a bit in the cpumask:
2159 */
2160 if (cpu_isset(cpu, tracing_cpumask) &&
2161 !cpu_isset(cpu, tracing_cpumask_new)) {
2162 atomic_inc(&global_trace.data[cpu]->disabled);
2163 }
2164 if (!cpu_isset(cpu, tracing_cpumask) &&
2165 cpu_isset(cpu, tracing_cpumask_new)) {
2166 atomic_dec(&global_trace.data[cpu]->disabled);
2167 }
2168 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002169 __raw_spin_unlock(&ftrace_max_lock);
2170 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002171
2172 tracing_cpumask = tracing_cpumask_new;
2173
Ingo Molnarc7078de2008-05-12 21:20:52 +02002174 mutex_unlock(&tracing_cpumask_update_lock);
2175
Ingo Molnarc7078de2008-05-12 21:20:52 +02002176 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002177
2178err_unlock:
2179 mutex_unlock(&tracing_cpumask_update_lock);
2180
2181 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002182}
2183
2184static struct file_operations tracing_cpumask_fops = {
2185 .open = tracing_open_generic,
2186 .read = tracing_cpumask_read,
2187 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188};
2189
2190static ssize_t
2191tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2192 size_t cnt, loff_t *ppos)
2193{
2194 char *buf;
2195 int r = 0;
2196 int len = 0;
2197 int i;
2198
2199 /* calulate max size */
2200 for (i = 0; trace_options[i]; i++) {
2201 len += strlen(trace_options[i]);
2202 len += 3; /* "no" and space */
2203 }
2204
2205 /* +2 for \n and \0 */
2206 buf = kmalloc(len + 2, GFP_KERNEL);
2207 if (!buf)
2208 return -ENOMEM;
2209
2210 for (i = 0; trace_options[i]; i++) {
2211 if (trace_flags & (1 << i))
2212 r += sprintf(buf + r, "%s ", trace_options[i]);
2213 else
2214 r += sprintf(buf + r, "no%s ", trace_options[i]);
2215 }
2216
2217 r += sprintf(buf + r, "\n");
2218 WARN_ON(r >= len + 2);
2219
Ingo Molnar36dfe922008-05-12 21:20:52 +02002220 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002221
2222 kfree(buf);
2223
2224 return r;
2225}
2226
2227static ssize_t
2228tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2229 size_t cnt, loff_t *ppos)
2230{
2231 char buf[64];
2232 char *cmp = buf;
2233 int neg = 0;
2234 int i;
2235
Steven Rostedtcffae432008-05-12 21:21:00 +02002236 if (cnt >= sizeof(buf))
2237 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002238
2239 if (copy_from_user(&buf, ubuf, cnt))
2240 return -EFAULT;
2241
2242 buf[cnt] = 0;
2243
2244 if (strncmp(buf, "no", 2) == 0) {
2245 neg = 1;
2246 cmp += 2;
2247 }
2248
2249 for (i = 0; trace_options[i]; i++) {
2250 int len = strlen(trace_options[i]);
2251
2252 if (strncmp(cmp, trace_options[i], len) == 0) {
2253 if (neg)
2254 trace_flags &= ~(1 << i);
2255 else
2256 trace_flags |= (1 << i);
2257 break;
2258 }
2259 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002260 /*
2261 * If no option could be set, return an error:
2262 */
2263 if (!trace_options[i])
2264 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002265
2266 filp->f_pos += cnt;
2267
2268 return cnt;
2269}
2270
2271static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002272 .open = tracing_open_generic,
2273 .read = tracing_iter_ctrl_read,
2274 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002275};
2276
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002277static const char readme_msg[] =
2278 "tracing mini-HOWTO:\n\n"
2279 "# mkdir /debug\n"
2280 "# mount -t debugfs nodev /debug\n\n"
2281 "# cat /debug/tracing/available_tracers\n"
2282 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2283 "# cat /debug/tracing/current_tracer\n"
2284 "none\n"
2285 "# echo sched_switch > /debug/tracing/current_tracer\n"
2286 "# cat /debug/tracing/current_tracer\n"
2287 "sched_switch\n"
2288 "# cat /debug/tracing/iter_ctrl\n"
2289 "noprint-parent nosym-offset nosym-addr noverbose\n"
2290 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2291 "# echo 1 > /debug/tracing/tracing_enabled\n"
2292 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2293 "echo 0 > /debug/tracing/tracing_enabled\n"
2294;
2295
2296static ssize_t
2297tracing_readme_read(struct file *filp, char __user *ubuf,
2298 size_t cnt, loff_t *ppos)
2299{
2300 return simple_read_from_buffer(ubuf, cnt, ppos,
2301 readme_msg, strlen(readme_msg));
2302}
2303
2304static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002305 .open = tracing_open_generic,
2306 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002307};
2308
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002309static ssize_t
2310tracing_ctrl_read(struct file *filp, char __user *ubuf,
2311 size_t cnt, loff_t *ppos)
2312{
2313 struct trace_array *tr = filp->private_data;
2314 char buf[64];
2315 int r;
2316
2317 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002318 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002319}
2320
2321static ssize_t
2322tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2323 size_t cnt, loff_t *ppos)
2324{
2325 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002326 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002327 long val;
2328 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002329
Steven Rostedtcffae432008-05-12 21:21:00 +02002330 if (cnt >= sizeof(buf))
2331 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002332
2333 if (copy_from_user(&buf, ubuf, cnt))
2334 return -EFAULT;
2335
2336 buf[cnt] = 0;
2337
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002338 ret = strict_strtoul(buf, 10, &val);
2339 if (ret < 0)
2340 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002341
2342 val = !!val;
2343
2344 mutex_lock(&trace_types_lock);
2345 if (tr->ctrl ^ val) {
2346 if (val)
2347 tracer_enabled = 1;
2348 else
2349 tracer_enabled = 0;
2350
2351 tr->ctrl = val;
2352
2353 if (current_trace && current_trace->ctrl_update)
2354 current_trace->ctrl_update(tr);
2355 }
2356 mutex_unlock(&trace_types_lock);
2357
2358 filp->f_pos += cnt;
2359
2360 return cnt;
2361}
2362
2363static ssize_t
2364tracing_set_trace_read(struct file *filp, char __user *ubuf,
2365 size_t cnt, loff_t *ppos)
2366{
2367 char buf[max_tracer_type_len+2];
2368 int r;
2369
2370 mutex_lock(&trace_types_lock);
2371 if (current_trace)
2372 r = sprintf(buf, "%s\n", current_trace->name);
2373 else
2374 r = sprintf(buf, "\n");
2375 mutex_unlock(&trace_types_lock);
2376
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002377 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002378}
2379
2380static ssize_t
2381tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2382 size_t cnt, loff_t *ppos)
2383{
2384 struct trace_array *tr = &global_trace;
2385 struct tracer *t;
2386 char buf[max_tracer_type_len+1];
2387 int i;
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002388 size_t ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002389
Steven Rostedt60063a62008-10-28 10:44:24 -04002390 ret = cnt;
2391
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002392 if (cnt > max_tracer_type_len)
2393 cnt = max_tracer_type_len;
2394
2395 if (copy_from_user(&buf, ubuf, cnt))
2396 return -EFAULT;
2397
2398 buf[cnt] = 0;
2399
2400 /* strip ending whitespace. */
2401 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2402 buf[i] = 0;
2403
2404 mutex_lock(&trace_types_lock);
2405 for (t = trace_types; t; t = t->next) {
2406 if (strcmp(t->name, buf) == 0)
2407 break;
2408 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002409 if (!t) {
2410 ret = -EINVAL;
2411 goto out;
2412 }
2413 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002414 goto out;
2415
2416 if (current_trace && current_trace->reset)
2417 current_trace->reset(tr);
2418
2419 current_trace = t;
2420 if (t->init)
2421 t->init(tr);
2422
2423 out:
2424 mutex_unlock(&trace_types_lock);
2425
Steven Rostedt60063a62008-10-28 10:44:24 -04002426 if (ret > 0)
2427 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002428
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002429 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002430}
2431
2432static ssize_t
2433tracing_max_lat_read(struct file *filp, char __user *ubuf,
2434 size_t cnt, loff_t *ppos)
2435{
2436 unsigned long *ptr = filp->private_data;
2437 char buf[64];
2438 int r;
2439
Steven Rostedtcffae432008-05-12 21:21:00 +02002440 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002441 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002442 if (r > sizeof(buf))
2443 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002444 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002445}
2446
2447static ssize_t
2448tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2449 size_t cnt, loff_t *ppos)
2450{
2451 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002452 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002453 long val;
2454 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002455
Steven Rostedtcffae432008-05-12 21:21:00 +02002456 if (cnt >= sizeof(buf))
2457 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002458
2459 if (copy_from_user(&buf, ubuf, cnt))
2460 return -EFAULT;
2461
2462 buf[cnt] = 0;
2463
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002464 ret = strict_strtoul(buf, 10, &val);
2465 if (ret < 0)
2466 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002467
2468 *ptr = val * 1000;
2469
2470 return cnt;
2471}
2472
Steven Rostedtb3806b42008-05-12 21:20:46 +02002473static atomic_t tracing_reader;
2474
2475static int tracing_open_pipe(struct inode *inode, struct file *filp)
2476{
2477 struct trace_iterator *iter;
2478
2479 if (tracing_disabled)
2480 return -ENODEV;
2481
2482 /* We only allow for reader of the pipe */
2483 if (atomic_inc_return(&tracing_reader) != 1) {
2484 atomic_dec(&tracing_reader);
2485 return -EBUSY;
2486 }
2487
2488 /* create a buffer to store the information to pass to userspace */
2489 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2490 if (!iter)
2491 return -ENOMEM;
2492
Steven Rostedt107bad82008-05-12 21:21:01 +02002493 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002494 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002495 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002496 filp->private_data = iter;
2497
Steven Rostedt107bad82008-05-12 21:21:01 +02002498 if (iter->trace->pipe_open)
2499 iter->trace->pipe_open(iter);
2500 mutex_unlock(&trace_types_lock);
2501
Steven Rostedtb3806b42008-05-12 21:20:46 +02002502 return 0;
2503}
2504
2505static int tracing_release_pipe(struct inode *inode, struct file *file)
2506{
2507 struct trace_iterator *iter = file->private_data;
2508
2509 kfree(iter);
2510 atomic_dec(&tracing_reader);
2511
2512 return 0;
2513}
2514
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002515static unsigned int
2516tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2517{
2518 struct trace_iterator *iter = filp->private_data;
2519
2520 if (trace_flags & TRACE_ITER_BLOCK) {
2521 /*
2522 * Always select as readable when in blocking mode
2523 */
2524 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002525 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002526 if (!trace_empty(iter))
2527 return POLLIN | POLLRDNORM;
2528 poll_wait(filp, &trace_wait, poll_table);
2529 if (!trace_empty(iter))
2530 return POLLIN | POLLRDNORM;
2531
2532 return 0;
2533 }
2534}
2535
Steven Rostedtb3806b42008-05-12 21:20:46 +02002536/*
2537 * Consumer reader.
2538 */
2539static ssize_t
2540tracing_read_pipe(struct file *filp, char __user *ubuf,
2541 size_t cnt, loff_t *ppos)
2542{
2543 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002544 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002545
2546 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002547 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2548 if (sret != -EBUSY)
2549 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002550
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002551 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002552
Steven Rostedt107bad82008-05-12 21:21:01 +02002553 mutex_lock(&trace_types_lock);
2554 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002555 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2556 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002557 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002558 }
2559
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002560waitagain:
2561 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002562 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002563
Steven Rostedt107bad82008-05-12 21:21:01 +02002564 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002565 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002566 goto out;
2567 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002568
Steven Rostedtb3806b42008-05-12 21:20:46 +02002569 /*
2570 * This is a make-shift waitqueue. The reason we don't use
2571 * an actual wait queue is because:
2572 * 1) we only ever have one waiter
2573 * 2) the tracing, traces all functions, we don't want
2574 * the overhead of calling wake_up and friends
2575 * (and tracing them too)
2576 * Anyway, this is really very primitive wakeup.
2577 */
2578 set_current_state(TASK_INTERRUPTIBLE);
2579 iter->tr->waiter = current;
2580
Steven Rostedt107bad82008-05-12 21:21:01 +02002581 mutex_unlock(&trace_types_lock);
2582
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002583 /* sleep for 100 msecs, and try again. */
2584 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002585
Steven Rostedt107bad82008-05-12 21:21:01 +02002586 mutex_lock(&trace_types_lock);
2587
Steven Rostedtb3806b42008-05-12 21:20:46 +02002588 iter->tr->waiter = NULL;
2589
Steven Rostedt107bad82008-05-12 21:21:01 +02002590 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002591 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002592 goto out;
2593 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002594
Steven Rostedt84527992008-05-12 21:20:58 +02002595 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002596 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002597
Steven Rostedtb3806b42008-05-12 21:20:46 +02002598 /*
2599 * We block until we read something and tracing is disabled.
2600 * We still block if tracing is disabled, but we have never
2601 * read anything. This allows a user to cat this file, and
2602 * then enable tracing. But after we have read something,
2603 * we give an EOF when tracing is again disabled.
2604 *
2605 * iter->pos will be 0 if we haven't read anything.
2606 */
2607 if (!tracer_enabled && iter->pos)
2608 break;
2609
2610 continue;
2611 }
2612
2613 /* stop when tracing is finished */
2614 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002615 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002616
2617 if (cnt >= PAGE_SIZE)
2618 cnt = PAGE_SIZE - 1;
2619
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002620 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002621 memset(&iter->seq, 0,
2622 sizeof(struct trace_iterator) -
2623 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002624 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002625
Steven Rostedt088b1e422008-05-12 21:20:48 +02002626 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002627 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002628 int len = iter->seq.len;
2629
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002630 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002631 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002632 /* don't print partial lines */
2633 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002634 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002635 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002636
2637 trace_consume(iter);
2638
2639 if (iter->seq.len >= cnt)
2640 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002641 }
2642
Steven Rostedtb3806b42008-05-12 21:20:46 +02002643 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002644 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2645 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002646 trace_seq_reset(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002647
2648 /*
2649 * If there was nothing to send to user, inspite of consuming trace
2650 * entries, go back to wait for more entries.
2651 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002652 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002653 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002654
Steven Rostedt107bad82008-05-12 21:21:01 +02002655out:
2656 mutex_unlock(&trace_types_lock);
2657
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002658 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002659}
2660
Steven Rostedta98a3c32008-05-12 21:20:59 +02002661static ssize_t
2662tracing_entries_read(struct file *filp, char __user *ubuf,
2663 size_t cnt, loff_t *ppos)
2664{
2665 struct trace_array *tr = filp->private_data;
2666 char buf[64];
2667 int r;
2668
2669 r = sprintf(buf, "%lu\n", tr->entries);
2670 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2671}
2672
2673static ssize_t
2674tracing_entries_write(struct file *filp, const char __user *ubuf,
2675 size_t cnt, loff_t *ppos)
2676{
2677 unsigned long val;
2678 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002679 int ret, cpu;
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002680 struct trace_array *tr = filp->private_data;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002681
Steven Rostedtcffae432008-05-12 21:21:00 +02002682 if (cnt >= sizeof(buf))
2683 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002684
2685 if (copy_from_user(&buf, ubuf, cnt))
2686 return -EFAULT;
2687
2688 buf[cnt] = 0;
2689
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002690 ret = strict_strtoul(buf, 10, &val);
2691 if (ret < 0)
2692 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002693
2694 /* must have at least 1 entry */
2695 if (!val)
2696 return -EINVAL;
2697
2698 mutex_lock(&trace_types_lock);
2699
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002700 if (tr->ctrl) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002701 cnt = -EBUSY;
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002702 pr_info("ftrace: please disable tracing"
Steven Rostedta98a3c32008-05-12 21:20:59 +02002703 " before modifying buffer size\n");
2704 goto out;
2705 }
2706
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002707 /* disable all cpu buffers */
2708 for_each_tracing_cpu(cpu) {
2709 if (global_trace.data[cpu])
2710 atomic_inc(&global_trace.data[cpu]->disabled);
2711 if (max_tr.data[cpu])
2712 atomic_inc(&max_tr.data[cpu]->disabled);
2713 }
2714
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002715 if (val != global_trace.entries) {
2716 ret = ring_buffer_resize(global_trace.buffer, val);
2717 if (ret < 0) {
2718 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002719 goto out;
2720 }
2721
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002722 ret = ring_buffer_resize(max_tr.buffer, val);
2723 if (ret < 0) {
2724 int r;
2725 cnt = ret;
2726 r = ring_buffer_resize(global_trace.buffer,
2727 global_trace.entries);
2728 if (r < 0) {
2729 /* AARGH! We are left with different
2730 * size max buffer!!!! */
2731 WARN_ON(1);
2732 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002733 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002734 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002735 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002736
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002737 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002738 }
2739
2740 filp->f_pos += cnt;
2741
Steven Rostedt19384c02008-05-22 00:22:16 -04002742 /* If check pages failed, return ENOMEM */
2743 if (tracing_disabled)
2744 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002745 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002746 for_each_tracing_cpu(cpu) {
2747 if (global_trace.data[cpu])
2748 atomic_dec(&global_trace.data[cpu]->disabled);
2749 if (max_tr.data[cpu])
2750 atomic_dec(&max_tr.data[cpu]->disabled);
2751 }
2752
Steven Rostedta98a3c32008-05-12 21:20:59 +02002753 max_tr.entries = global_trace.entries;
2754 mutex_unlock(&trace_types_lock);
2755
2756 return cnt;
2757}
2758
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002759static int mark_printk(const char *fmt, ...)
2760{
2761 int ret;
2762 va_list args;
2763 va_start(args, fmt);
2764 ret = trace_vprintk(0, fmt, args);
2765 va_end(args);
2766 return ret;
2767}
2768
2769static ssize_t
2770tracing_mark_write(struct file *filp, const char __user *ubuf,
2771 size_t cnt, loff_t *fpos)
2772{
2773 char *buf;
2774 char *end;
2775 struct trace_array *tr = &global_trace;
2776
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002777 if (!tr->ctrl || tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002778 return -EINVAL;
2779
2780 if (cnt > TRACE_BUF_SIZE)
2781 cnt = TRACE_BUF_SIZE;
2782
2783 buf = kmalloc(cnt + 1, GFP_KERNEL);
2784 if (buf == NULL)
2785 return -ENOMEM;
2786
2787 if (copy_from_user(buf, ubuf, cnt)) {
2788 kfree(buf);
2789 return -EFAULT;
2790 }
2791
2792 /* Cut from the first nil or newline. */
2793 buf[cnt] = '\0';
2794 end = strchr(buf, '\n');
2795 if (end)
2796 *end = '\0';
2797
2798 cnt = mark_printk("%s\n", buf);
2799 kfree(buf);
2800 *fpos += cnt;
2801
2802 return cnt;
2803}
2804
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002805static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002806 .open = tracing_open_generic,
2807 .read = tracing_max_lat_read,
2808 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002809};
2810
2811static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002812 .open = tracing_open_generic,
2813 .read = tracing_ctrl_read,
2814 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002815};
2816
2817static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002818 .open = tracing_open_generic,
2819 .read = tracing_set_trace_read,
2820 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002821};
2822
Steven Rostedtb3806b42008-05-12 21:20:46 +02002823static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002824 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002825 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002826 .read = tracing_read_pipe,
2827 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002828};
2829
Steven Rostedta98a3c32008-05-12 21:20:59 +02002830static struct file_operations tracing_entries_fops = {
2831 .open = tracing_open_generic,
2832 .read = tracing_entries_read,
2833 .write = tracing_entries_write,
2834};
2835
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002836static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002837 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002838 .write = tracing_mark_write,
2839};
2840
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002841#ifdef CONFIG_DYNAMIC_FTRACE
2842
2843static ssize_t
2844tracing_read_long(struct file *filp, char __user *ubuf,
2845 size_t cnt, loff_t *ppos)
2846{
2847 unsigned long *p = filp->private_data;
2848 char buf[64];
2849 int r;
2850
2851 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002852
2853 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002854}
2855
2856static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002857 .open = tracing_open_generic,
2858 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002859};
2860#endif
2861
2862static struct dentry *d_tracer;
2863
2864struct dentry *tracing_init_dentry(void)
2865{
2866 static int once;
2867
2868 if (d_tracer)
2869 return d_tracer;
2870
2871 d_tracer = debugfs_create_dir("tracing", NULL);
2872
2873 if (!d_tracer && !once) {
2874 once = 1;
2875 pr_warning("Could not create debugfs directory 'tracing'\n");
2876 return NULL;
2877 }
2878
2879 return d_tracer;
2880}
2881
Steven Rostedt60a11772008-05-12 21:20:44 +02002882#ifdef CONFIG_FTRACE_SELFTEST
2883/* Let selftest have access to static functions in this file */
2884#include "trace_selftest.c"
2885#endif
2886
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01002887static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002888{
2889 struct dentry *d_tracer;
2890 struct dentry *entry;
2891
2892 d_tracer = tracing_init_dentry();
2893
2894 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2895 &global_trace, &tracing_ctrl_fops);
2896 if (!entry)
2897 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2898
2899 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2900 NULL, &tracing_iter_fops);
2901 if (!entry)
2902 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2903
Ingo Molnarc7078de2008-05-12 21:20:52 +02002904 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2905 NULL, &tracing_cpumask_fops);
2906 if (!entry)
2907 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2908
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002909 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2910 &global_trace, &tracing_lt_fops);
2911 if (!entry)
2912 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2913
2914 entry = debugfs_create_file("trace", 0444, d_tracer,
2915 &global_trace, &tracing_fops);
2916 if (!entry)
2917 pr_warning("Could not create debugfs 'trace' entry\n");
2918
2919 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2920 &global_trace, &show_traces_fops);
2921 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002922 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002923
2924 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2925 &global_trace, &set_tracer_fops);
2926 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002927 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002928
2929 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2930 &tracing_max_latency,
2931 &tracing_max_lat_fops);
2932 if (!entry)
2933 pr_warning("Could not create debugfs "
2934 "'tracing_max_latency' entry\n");
2935
2936 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2937 &tracing_thresh, &tracing_max_lat_fops);
2938 if (!entry)
2939 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002940 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002941 entry = debugfs_create_file("README", 0644, d_tracer,
2942 NULL, &tracing_readme_fops);
2943 if (!entry)
2944 pr_warning("Could not create debugfs 'README' entry\n");
2945
Steven Rostedtb3806b42008-05-12 21:20:46 +02002946 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2947 NULL, &tracing_pipe_fops);
2948 if (!entry)
2949 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002950 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002951
Steven Rostedta98a3c32008-05-12 21:20:59 +02002952 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2953 &global_trace, &tracing_entries_fops);
2954 if (!entry)
2955 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002956 "'trace_entries' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02002957
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002958 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2959 NULL, &tracing_mark_fops);
2960 if (!entry)
2961 pr_warning("Could not create debugfs "
2962 "'trace_marker' entry\n");
2963
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002964#ifdef CONFIG_DYNAMIC_FTRACE
2965 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2966 &ftrace_update_tot_cnt,
2967 &tracing_read_long_fops);
2968 if (!entry)
2969 pr_warning("Could not create debugfs "
2970 "'dyn_ftrace_total_info' entry\n");
2971#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02002972#ifdef CONFIG_SYSPROF_TRACER
2973 init_tracer_sysprof_debugfs(d_tracer);
2974#endif
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01002975 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002976}
2977
Pekka Paalanen801fe402008-09-16 21:58:24 +03002978int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002979{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002980 static DEFINE_SPINLOCK(trace_buf_lock);
2981 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02002982
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002983 struct ring_buffer_event *event;
Peter Zijlstraf09ce572008-09-04 10:24:14 +02002984 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002985 struct trace_array_cpu *data;
Steven Rostedt777e2082008-09-29 23:02:42 -04002986 struct print_entry *entry;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002987 unsigned long flags, irq_flags;
Steven Rostedt38697052008-10-01 13:14:09 -04002988 int cpu, len = 0, size, pc;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002989
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002990 if (!tr->ctrl || tracing_disabled)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002991 return 0;
2992
Steven Rostedt38697052008-10-01 13:14:09 -04002993 pc = preempt_count();
2994 preempt_disable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002995 cpu = raw_smp_processor_id();
2996 data = tr->data[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002997
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -04002998 if (unlikely(atomic_read(&data->disabled)))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002999 goto out;
3000
Steven Rostedt38697052008-10-01 13:14:09 -04003001 spin_lock_irqsave(&trace_buf_lock, flags);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003002 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003003
3004 len = min(len, TRACE_BUF_SIZE-1);
3005 trace_buf[len] = 0;
3006
Steven Rostedt777e2082008-09-29 23:02:42 -04003007 size = sizeof(*entry) + len + 1;
3008 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003009 if (!event)
3010 goto out_unlock;
Steven Rostedt777e2082008-09-29 23:02:42 -04003011 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -04003012 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -04003013 entry->ent.type = TRACE_PRINT;
3014 entry->ip = ip;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003015
Steven Rostedt777e2082008-09-29 23:02:42 -04003016 memcpy(&entry->buf, trace_buf, len);
3017 entry->buf[len] = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003018 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003019
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003020 out_unlock:
Steven Rostedt38697052008-10-01 13:14:09 -04003021 spin_unlock_irqrestore(&trace_buf_lock, flags);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003022
3023 out:
Steven Rostedt38697052008-10-01 13:14:09 -04003024 preempt_enable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003025
3026 return len;
3027}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003028EXPORT_SYMBOL_GPL(trace_vprintk);
3029
3030int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3031{
3032 int ret;
3033 va_list ap;
3034
3035 if (!(trace_flags & TRACE_ITER_PRINTK))
3036 return 0;
3037
3038 va_start(ap, fmt);
3039 ret = trace_vprintk(ip, fmt, ap);
3040 va_end(ap);
3041 return ret;
3042}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003043EXPORT_SYMBOL_GPL(__ftrace_printk);
3044
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003045static int trace_panic_handler(struct notifier_block *this,
3046 unsigned long event, void *unused)
3047{
3048 ftrace_dump();
3049 return NOTIFY_OK;
3050}
3051
3052static struct notifier_block trace_panic_notifier = {
3053 .notifier_call = trace_panic_handler,
3054 .next = NULL,
3055 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3056};
3057
3058static int trace_die_handler(struct notifier_block *self,
3059 unsigned long val,
3060 void *data)
3061{
3062 switch (val) {
3063 case DIE_OOPS:
3064 ftrace_dump();
3065 break;
3066 default:
3067 break;
3068 }
3069 return NOTIFY_OK;
3070}
3071
3072static struct notifier_block trace_die_notifier = {
3073 .notifier_call = trace_die_handler,
3074 .priority = 200
3075};
3076
3077/*
3078 * printk is set to max of 1024, we really don't need it that big.
3079 * Nothing should be printing 1000 characters anyway.
3080 */
3081#define TRACE_MAX_PRINT 1000
3082
3083/*
3084 * Define here KERN_TRACE so that we have one place to modify
3085 * it if we decide to change what log level the ftrace dump
3086 * should be at.
3087 */
3088#define KERN_TRACE KERN_INFO
3089
3090static void
3091trace_printk_seq(struct trace_seq *s)
3092{
3093 /* Probably should print a warning here. */
3094 if (s->len >= 1000)
3095 s->len = 1000;
3096
3097 /* should be zero ended, but we are paranoid. */
3098 s->buffer[s->len] = 0;
3099
3100 printk(KERN_TRACE "%s", s->buffer);
3101
3102 trace_seq_reset(s);
3103}
3104
3105
3106void ftrace_dump(void)
3107{
3108 static DEFINE_SPINLOCK(ftrace_dump_lock);
3109 /* use static because iter can be a bit big for the stack */
3110 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003111 static cpumask_t mask;
3112 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003113 unsigned long flags;
3114 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003115
3116 /* only one dump */
3117 spin_lock_irqsave(&ftrace_dump_lock, flags);
3118 if (dump_ran)
3119 goto out;
3120
3121 dump_ran = 1;
3122
3123 /* No turning back! */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003124 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003125
Steven Rostedtd7690412008-10-01 00:29:53 -04003126 for_each_tracing_cpu(cpu) {
3127 atomic_inc(&global_trace.data[cpu]->disabled);
3128 }
3129
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003130 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3131
3132 iter.tr = &global_trace;
3133 iter.trace = current_trace;
3134
3135 /*
3136 * We need to stop all tracing on all CPUS to read the
3137 * the next buffer. This is a bit expensive, but is
3138 * not done often. We fill all what we can read,
3139 * and then release the locks again.
3140 */
3141
3142 cpus_clear(mask);
3143
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003144 while (!trace_empty(&iter)) {
3145
3146 if (!cnt)
3147 printk(KERN_TRACE "---------------------------------\n");
3148
3149 cnt++;
3150
3151 /* reset all but tr, trace, and overruns */
3152 memset(&iter.seq, 0,
3153 sizeof(struct trace_iterator) -
3154 offsetof(struct trace_iterator, seq));
3155 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3156 iter.pos = -1;
3157
3158 if (find_next_entry_inc(&iter) != NULL) {
3159 print_trace_line(&iter);
3160 trace_consume(&iter);
3161 }
3162
3163 trace_printk_seq(&iter.seq);
3164 }
3165
3166 if (!cnt)
3167 printk(KERN_TRACE " (ftrace buffer empty)\n");
3168 else
3169 printk(KERN_TRACE "---------------------------------\n");
3170
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003171 out:
3172 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3173}
3174
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003175__init static int tracer_alloc_buffers(void)
3176{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003177 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003178 int i;
3179
Steven Rostedtab464282008-05-12 21:21:00 +02003180 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedtab464282008-05-12 21:21:00 +02003181 tracing_buffer_mask = cpu_possible_map;
3182
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003183 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3184 TRACE_BUFFER_FLAGS);
3185 if (!global_trace.buffer) {
3186 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3187 WARN_ON(1);
3188 return 0;
3189 }
3190 global_trace.entries = ring_buffer_size(global_trace.buffer);
3191
3192#ifdef CONFIG_TRACER_MAX_TRACE
3193 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3194 TRACE_BUFFER_FLAGS);
3195 if (!max_tr.buffer) {
3196 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3197 WARN_ON(1);
3198 ring_buffer_free(global_trace.buffer);
3199 return 0;
3200 }
3201 max_tr.entries = ring_buffer_size(max_tr.buffer);
3202 WARN_ON(max_tr.entries != global_trace.entries);
3203#endif
3204
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003205 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003206 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003207 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003208 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003209 }
3210
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003211 trace_init_cmdlines();
3212
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003213 register_tracer(&nop_trace);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003214#ifdef CONFIG_BOOT_TRACER
3215 register_tracer(&boot_tracer);
3216 current_trace = &boot_tracer;
3217 current_trace->init(&global_trace);
3218#else
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003219 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003220#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003221
Steven Rostedt60a11772008-05-12 21:20:44 +02003222 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003223 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003224 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003225
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003226 atomic_notifier_chain_register(&panic_notifier_list,
3227 &trace_panic_notifier);
3228
3229 register_die_notifier(&trace_die_notifier);
3230
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003231 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003232}
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003233early_initcall(tracer_alloc_buffers);
3234fs_initcall(tracer_init_debugfs);