blob: 24b6238884f0e286bfc09f9b67bb0af5ae53f01e [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 Rostedt0f048702008-11-05 16:05:44 -050046
47/*
48 * Kill all tracing for good (never come back).
49 * It is initialized to 1 but will turn to zero if the initialization
50 * of the tracer is successful. But that is the only place that sets
51 * this back to zero.
52 */
53int tracing_disabled = 1;
54
Steven Rostedtd7690412008-10-01 00:29:53 -040055static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
56
57static inline void ftrace_disable_cpu(void)
58{
59 preempt_disable();
60 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
61}
62
63static inline void ftrace_enable_cpu(void)
64{
65 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
66 preempt_enable();
67}
68
Steven Rostedtab464282008-05-12 21:21:00 +020069static cpumask_t __read_mostly tracing_buffer_mask;
70
71#define for_each_tracing_cpu(cpu) \
72 for_each_cpu_mask(cpu, tracing_buffer_mask)
73
Steven Rostedt944ac422008-10-23 19:26:08 -040074/*
75 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
76 *
77 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
78 * is set, then ftrace_dump is called. This will output the contents
79 * of the ftrace buffers to the console. This is very useful for
80 * capturing traces that lead to crashes and outputing it to a
81 * serial console.
82 *
83 * It is default off, but you can enable it with either specifying
84 * "ftrace_dump_on_oops" in the kernel command line, or setting
85 * /proc/sys/kernel/ftrace_dump_on_oops to true.
86 */
87int ftrace_dump_on_oops;
88
Peter Zijlstrad9e54072008-11-01 19:57:37 +010089static int tracing_set_tracer(char *buf);
90
91static int __init set_ftrace(char *str)
92{
93 tracing_set_tracer(str);
94 return 1;
95}
96__setup("ftrace", set_ftrace);
97
Steven Rostedt944ac422008-10-23 19:26:08 -040098static int __init set_ftrace_dump_on_oops(char *str)
99{
100 ftrace_dump_on_oops = 1;
101 return 1;
102}
103__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
104
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200105long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200106ns2usecs(cycle_t nsec)
107{
108 nsec += 500;
109 do_div(nsec, 1000);
110 return nsec;
111}
112
Ingo Molnare309b412008-05-12 21:20:51 +0200113cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200114{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400115 u64 ts = ring_buffer_time_stamp(cpu);
116 ring_buffer_normalize_time_stamp(cpu, &ts);
117 return ts;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200118}
119
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200120/*
121 * The global_trace is the descriptor that holds the tracing
122 * buffers for the live tracing. For each CPU, it contains
123 * a link list of pages that will store trace entries. The
124 * page descriptor of the pages in the memory is used to hold
125 * the link list by linking the lru item in the page descriptor
126 * to each of the pages in the buffer per CPU.
127 *
128 * For each active CPU there is a data field that holds the
129 * pages for the buffer for that CPU. Each CPU has the same number
130 * of pages allocated for its buffer.
131 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200132static struct trace_array global_trace;
133
134static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
135
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200136/*
137 * The max_tr is used to snapshot the global_trace when a maximum
138 * latency is reached. Some tracers will use this to store a maximum
139 * trace while it continues examining live traces.
140 *
141 * The buffers for the max_tr are set up the same as the global_trace.
142 * When a snapshot is taken, the link list of the max_tr is swapped
143 * with the link list of the global_trace and the buffers are reset for
144 * the global_trace so the tracing can continue.
145 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200146static struct trace_array max_tr;
147
148static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
149
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200150/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200151static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200152
Steven Rostedt90369902008-11-05 16:05:44 -0500153/**
154 * tracing_is_enabled - return tracer_enabled status
155 *
156 * This function is used by other tracers to know the status
157 * of the tracer_enabled flag. Tracers may use this function
158 * to know if it should enable their features when starting
159 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
160 */
161int tracing_is_enabled(void)
162{
163 return tracer_enabled;
164}
165
Steven Rostedt60bc0802008-07-10 20:58:16 -0400166/* function tracing enabled */
167int ftrace_function_enabled;
168
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200169/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400170 * trace_buf_size is the size in bytes that is allocated
171 * for a buffer. Note, the number of bytes is always rounded
172 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400173 *
174 * This number is purposely set to a low number of 16384.
175 * If the dump on oops happens, it will be much appreciated
176 * to not have to wait for all that output. Anyway this can be
177 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200178 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400179#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400180
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400181static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200182
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200183/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200184static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200185
186/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200187static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200188
189/*
190 * max_tracer_type_len is used to simplify the allocating of
191 * buffers to read userspace tracer names. We keep track of
192 * the longest tracer name registered.
193 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200194static int max_tracer_type_len;
195
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200196/*
197 * trace_types_lock is used to protect the trace_types list.
198 * This lock is also used to keep user access serialized.
199 * Accesses from userspace will grab this lock while userspace
200 * activities happen inside the kernel.
201 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200202static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200203
204/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200205static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
206
Steven Rostedtee6bce52008-11-12 17:52:37 -0500207/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500208unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
209 TRACE_ITER_ANNOTATE;
Ingo Molnar4e655512008-05-12 21:20:52 +0200210
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200211/**
212 * trace_wake_up - wake up tasks waiting for trace input
213 *
214 * Simply wakes up any task that is blocked on the trace_wait
215 * queue. These is used with trace_poll for tasks polling the trace.
216 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200217void trace_wake_up(void)
218{
Ingo Molnar017730c2008-05-12 21:20:52 +0200219 /*
220 * The runqueue_is_locked() can fail, but this is the best we
221 * have for now:
222 */
223 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200224 wake_up(&trace_wait);
225}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200226
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400227static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200228{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400229 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200230 int ret;
231
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200232 if (!str)
233 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400234 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200235 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400236 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200237 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400238 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200239 return 1;
240}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400241__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200242
Steven Rostedt57f50be2008-05-12 21:20:44 +0200243unsigned long nsecs_to_usecs(unsigned long nsecs)
244{
245 return nsecs / 1000;
246}
247
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200248/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200249static const char *trace_options[] = {
250 "print-parent",
251 "sym-offset",
252 "sym-addr",
253 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200254 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200255 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200256 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200257 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200258 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200259 "sched-tree",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200260 "ftrace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500261 "ftrace_preempt",
Steven Rostedt2ed84ee2008-11-12 15:24:24 -0500262#ifdef CONFIG_BRANCH_TRACER
Steven Rostedt9f029e82008-11-12 15:24:24 -0500263 "branch",
Steven Rostedt52f232c2008-11-12 00:14:40 -0500264#endif
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500265 "annotate",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200266 NULL
267};
268
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200269/*
270 * ftrace_max_lock is used to protect the swapping of buffers
271 * when taking a max snapshot. The buffers themselves are
272 * protected by per_cpu spinlocks. But the action of the swap
273 * needs its own lock.
274 *
275 * This is defined as a raw_spinlock_t in order to help
276 * with performance when lockdep debugging is enabled.
277 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200278static raw_spinlock_t ftrace_max_lock =
279 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200280
281/*
282 * Copy the new maximum trace into the separate maximum-trace
283 * structure. (this way the maximum trace is permanently saved,
284 * for later retrieval via /debugfs/tracing/latency_trace)
285 */
Ingo Molnare309b412008-05-12 21:20:51 +0200286static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200287__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
288{
289 struct trace_array_cpu *data = tr->data[cpu];
290
291 max_tr.cpu = cpu;
292 max_tr.time_start = data->preempt_timestamp;
293
294 data = max_tr.data[cpu];
295 data->saved_latency = tracing_max_latency;
296
297 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
298 data->pid = tsk->pid;
299 data->uid = tsk->uid;
300 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
301 data->policy = tsk->policy;
302 data->rt_priority = tsk->rt_priority;
303
304 /* record this tasks comm */
305 tracing_record_cmdline(current);
306}
307
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200308/**
309 * trace_seq_printf - sequence printing of trace information
310 * @s: trace sequence descriptor
311 * @fmt: printf format string
312 *
313 * The tracer may use either sequence operations or its own
314 * copy to user routines. To simplify formating of a trace
315 * trace_seq_printf is used to store strings into a special
316 * buffer (@s). Then the output may be either used by
317 * the sequencer or pulled into another buffer.
318 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200319int
Steven Rostedt214023c2008-05-12 21:20:46 +0200320trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
321{
322 int len = (PAGE_SIZE - 1) - s->len;
323 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200324 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200325
326 if (!len)
327 return 0;
328
329 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200330 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200331 va_end(ap);
332
Steven Rostedtb3806b42008-05-12 21:20:46 +0200333 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200334 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200335 return 0;
336
337 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200338
339 return len;
340}
341
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200342/**
343 * trace_seq_puts - trace sequence printing of simple string
344 * @s: trace sequence descriptor
345 * @str: simple string to record
346 *
347 * The tracer may use either the sequence operations or its own
348 * copy to user routines. This function records a simple string
349 * into a special buffer (@s) for later retrieval by a sequencer
350 * or other mechanism.
351 */
Ingo Molnare309b412008-05-12 21:20:51 +0200352static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200353trace_seq_puts(struct trace_seq *s, const char *str)
354{
355 int len = strlen(str);
356
357 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200358 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200359
360 memcpy(s->buffer + s->len, str, len);
361 s->len += len;
362
363 return len;
364}
365
Ingo Molnare309b412008-05-12 21:20:51 +0200366static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200367trace_seq_putc(struct trace_seq *s, unsigned char c)
368{
369 if (s->len >= (PAGE_SIZE - 1))
370 return 0;
371
372 s->buffer[s->len++] = c;
373
374 return 1;
375}
376
Ingo Molnare309b412008-05-12 21:20:51 +0200377static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200378trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
379{
380 if (len > ((PAGE_SIZE - 1) - s->len))
381 return 0;
382
383 memcpy(s->buffer + s->len, mem, len);
384 s->len += len;
385
386 return len;
387}
388
Harvey Harrisonad0a3b62008-10-08 17:44:55 -0700389#define MAX_MEMHEX_BYTES 8
390#define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200391
Ingo Molnare309b412008-05-12 21:20:51 +0200392static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200393trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
394{
395 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200396 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200397 int i, j;
398
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200399#ifdef __BIG_ENDIAN
400 for (i = 0, j = 0; i < len; i++) {
401#else
402 for (i = len-1, j = 0; i >= 0; i--) {
403#endif
Harvey Harrison2fbc4742008-10-08 16:51:49 -0700404 hex[j++] = hex_asc_hi(data[i]);
405 hex[j++] = hex_asc_lo(data[i]);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200406 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200407 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200408
409 return trace_seq_putmem(s, hex, j);
410}
411
Ingo Molnare309b412008-05-12 21:20:51 +0200412static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200413trace_seq_reset(struct trace_seq *s)
414{
415 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200416 s->readpos = 0;
417}
418
419ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
420{
421 int len;
422 int ret;
423
424 if (s->len <= s->readpos)
425 return -EBUSY;
426
427 len = s->len - s->readpos;
428 if (cnt > len)
429 cnt = len;
430 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
431 if (ret)
432 return -EFAULT;
433
434 s->readpos += len;
435 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200436}
437
Ingo Molnare309b412008-05-12 21:20:51 +0200438static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200439trace_print_seq(struct seq_file *m, struct trace_seq *s)
440{
441 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
442
443 s->buffer[len] = 0;
444 seq_puts(m, s->buffer);
445
446 trace_seq_reset(s);
447}
448
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200449/**
450 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
451 * @tr: tracer
452 * @tsk: the task with the latency
453 * @cpu: The cpu that initiated the trace.
454 *
455 * Flip the buffers between the @tr and the max_tr and record information
456 * about which task was the cause of this latency.
457 */
Ingo Molnare309b412008-05-12 21:20:51 +0200458void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200459update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
460{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400461 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200462
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200463 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200464 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400465
466 tr->buffer = max_tr.buffer;
467 max_tr.buffer = buf;
468
Steven Rostedtd7690412008-10-01 00:29:53 -0400469 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400470 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400471 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200472
473 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200474 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200475}
476
477/**
478 * update_max_tr_single - only copy one trace over, and reset the rest
479 * @tr - tracer
480 * @tsk - task with the latency
481 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200482 *
483 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200484 */
Ingo Molnare309b412008-05-12 21:20:51 +0200485void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200486update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
487{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400488 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200489
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200490 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200491 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200492
Steven Rostedtd7690412008-10-01 00:29:53 -0400493 ftrace_disable_cpu();
494
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400495 ring_buffer_reset(max_tr.buffer);
496 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
497
Steven Rostedtd7690412008-10-01 00:29:53 -0400498 ftrace_enable_cpu();
499
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400500 WARN_ON_ONCE(ret);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501
502 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200503 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200504}
505
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200506/**
507 * register_tracer - register a tracer with the ftrace system.
508 * @type - the plugin for the tracer
509 *
510 * Register a new plugin tracer.
511 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200512int register_tracer(struct tracer *type)
513{
514 struct tracer *t;
515 int len;
516 int ret = 0;
517
518 if (!type->name) {
519 pr_info("Tracer must have a name\n");
520 return -1;
521 }
522
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100523 /*
524 * When this gets called we hold the BKL which means that
525 * preemption is disabled. Various trace selftests however
526 * need to disable and enable preemption for successful tests.
527 * So we drop the BKL here and grab it after the tests again.
528 */
529 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200530 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100531
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200532 for (t = trace_types; t; t = t->next) {
533 if (strcmp(type->name, t->name) == 0) {
534 /* already found */
535 pr_info("Trace %s already registered\n",
536 type->name);
537 ret = -1;
538 goto out;
539 }
540 }
541
Steven Rostedt60a11772008-05-12 21:20:44 +0200542#ifdef CONFIG_FTRACE_STARTUP_TEST
543 if (type->selftest) {
544 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200545 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200546 int i;
547 /*
548 * Run a selftest on this tracer.
549 * Here we reset the trace buffer, and set the current
550 * tracer to be this tracer. The tracer can then run some
551 * internal tracing to verify that everything is in order.
552 * If we fail, we do not register this tracer.
553 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100554 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400555 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100556
Steven Rostedt60a11772008-05-12 21:20:44 +0200557 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200558 /* the test is responsible for initializing and enabling */
559 pr_info("Testing tracer %s: ", type->name);
560 ret = type->selftest(type, tr);
561 /* the test is responsible for resetting too */
562 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200563 if (ret) {
564 printk(KERN_CONT "FAILED!\n");
565 goto out;
566 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200567 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100568 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400569 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100570
Steven Rostedt60a11772008-05-12 21:20:44 +0200571 printk(KERN_CONT "PASSED\n");
572 }
573#endif
574
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200575 type->next = trace_types;
576 trace_types = type;
577 len = strlen(type->name);
578 if (len > max_tracer_type_len)
579 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200580
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200581 out:
582 mutex_unlock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100583 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200584
585 return ret;
586}
587
588void unregister_tracer(struct tracer *type)
589{
590 struct tracer **t;
591 int len;
592
593 mutex_lock(&trace_types_lock);
594 for (t = &trace_types; *t; t = &(*t)->next) {
595 if (*t == type)
596 goto found;
597 }
598 pr_info("Trace %s not registered\n", type->name);
599 goto out;
600
601 found:
602 *t = (*t)->next;
603 if (strlen(type->name) != max_tracer_type_len)
604 goto out;
605
606 max_tracer_type_len = 0;
607 for (t = &trace_types; *t; t = &(*t)->next) {
608 len = strlen((*t)->name);
609 if (len > max_tracer_type_len)
610 max_tracer_type_len = len;
611 }
612 out:
613 mutex_unlock(&trace_types_lock);
614}
615
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400616void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200617{
Steven Rostedtd7690412008-10-01 00:29:53 -0400618 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400619 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400620 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200621}
622
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200623#define SAVED_CMDLINES 128
624static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
625static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
626static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
627static int cmdline_idx;
628static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200629
Steven Rostedt25b0b442008-05-12 21:21:00 +0200630/* temporary disable recording */
631atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200632
633static void trace_init_cmdlines(void)
634{
635 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
636 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
637 cmdline_idx = 0;
638}
639
Steven Rostedt0f048702008-11-05 16:05:44 -0500640static int trace_stop_count;
641static DEFINE_SPINLOCK(tracing_start_lock);
642
643/**
644 * tracing_start - quick start of the tracer
645 *
646 * If tracing is enabled but was stopped by tracing_stop,
647 * this will start the tracer back up.
648 */
649void tracing_start(void)
650{
651 struct ring_buffer *buffer;
652 unsigned long flags;
653
654 if (tracing_disabled)
655 return;
656
657 spin_lock_irqsave(&tracing_start_lock, flags);
658 if (--trace_stop_count)
659 goto out;
660
661 if (trace_stop_count < 0) {
662 /* Someone screwed up their debugging */
663 WARN_ON_ONCE(1);
664 trace_stop_count = 0;
665 goto out;
666 }
667
668
669 buffer = global_trace.buffer;
670 if (buffer)
671 ring_buffer_record_enable(buffer);
672
673 buffer = max_tr.buffer;
674 if (buffer)
675 ring_buffer_record_enable(buffer);
676
677 ftrace_start();
678 out:
679 spin_unlock_irqrestore(&tracing_start_lock, flags);
680}
681
682/**
683 * tracing_stop - quick stop of the tracer
684 *
685 * Light weight way to stop tracing. Use in conjunction with
686 * tracing_start.
687 */
688void tracing_stop(void)
689{
690 struct ring_buffer *buffer;
691 unsigned long flags;
692
693 ftrace_stop();
694 spin_lock_irqsave(&tracing_start_lock, flags);
695 if (trace_stop_count++)
696 goto out;
697
698 buffer = global_trace.buffer;
699 if (buffer)
700 ring_buffer_record_disable(buffer);
701
702 buffer = max_tr.buffer;
703 if (buffer)
704 ring_buffer_record_disable(buffer);
705
706 out:
707 spin_unlock_irqrestore(&tracing_start_lock, flags);
708}
709
Ingo Molnare309b412008-05-12 21:20:51 +0200710void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200711
Ingo Molnare309b412008-05-12 21:20:51 +0200712static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200713{
714 unsigned map;
715 unsigned idx;
716
717 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
718 return;
719
720 /*
721 * It's not the end of the world if we don't get
722 * the lock, but we also don't want to spin
723 * nor do we want to disable interrupts,
724 * so if we miss here, then better luck next time.
725 */
726 if (!spin_trylock(&trace_cmdline_lock))
727 return;
728
729 idx = map_pid_to_cmdline[tsk->pid];
730 if (idx >= SAVED_CMDLINES) {
731 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
732
733 map = map_cmdline_to_pid[idx];
734 if (map <= PID_MAX_DEFAULT)
735 map_pid_to_cmdline[map] = (unsigned)-1;
736
737 map_pid_to_cmdline[tsk->pid] = idx;
738
739 cmdline_idx = idx;
740 }
741
742 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
743
744 spin_unlock(&trace_cmdline_lock);
745}
746
Ingo Molnare309b412008-05-12 21:20:51 +0200747static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200748{
749 char *cmdline = "<...>";
750 unsigned map;
751
752 if (!pid)
753 return "<idle>";
754
755 if (pid > PID_MAX_DEFAULT)
756 goto out;
757
758 map = map_pid_to_cmdline[pid];
759 if (map >= SAVED_CMDLINES)
760 goto out;
761
762 cmdline = saved_cmdlines[map];
763
764 out:
765 return cmdline;
766}
767
Ingo Molnare309b412008-05-12 21:20:51 +0200768void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200769{
770 if (atomic_read(&trace_record_cmdline_disabled))
771 return;
772
773 trace_save_cmdline(tsk);
774}
775
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300776void
Steven Rostedt38697052008-10-01 13:14:09 -0400777tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
778 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200779{
780 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781
Steven Rostedt777e2082008-09-29 23:02:42 -0400782 entry->preempt_count = pc & 0xff;
783 entry->pid = (tsk) ? tsk->pid : 0;
784 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400785#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400786 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400787#else
788 TRACE_FLAG_IRQS_NOSUPPORT |
789#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200790 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
791 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
792 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
793}
794
Ingo Molnare309b412008-05-12 21:20:51 +0200795void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200796trace_function(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400797 unsigned long ip, unsigned long parent_ip, unsigned long flags,
798 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200799{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400800 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400801 struct ftrace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200802 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200803
Steven Rostedtd7690412008-10-01 00:29:53 -0400804 /* If we are reading the ring buffer, don't trace */
805 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
806 return;
807
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400808 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
809 &irq_flags);
810 if (!event)
811 return;
812 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400813 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400814 entry->ent.type = TRACE_FN;
815 entry->ip = ip;
816 entry->parent_ip = parent_ip;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400817 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818}
819
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100820#ifdef CONFIG_FUNCTION_RET_TRACER
821static void __trace_function_return(struct trace_array *tr,
822 struct trace_array_cpu *data,
823 struct ftrace_retfunc *trace,
824 unsigned long flags,
825 int pc)
826{
827 struct ring_buffer_event *event;
828 struct ftrace_ret_entry *entry;
829 unsigned long irq_flags;
830
831 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
832 return;
833
834 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
835 &irq_flags);
836 if (!event)
837 return;
838 entry = ring_buffer_event_data(event);
839 tracing_generic_entry_update(&entry->ent, flags, pc);
840 entry->ent.type = TRACE_FN_RET;
841 entry->ip = trace->func;
842 entry->parent_ip = trace->ret;
843 entry->rettime = trace->rettime;
844 entry->calltime = trace->calltime;
845 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
846}
847#endif
848
Ingo Molnare309b412008-05-12 21:20:51 +0200849void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200850ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400851 unsigned long ip, unsigned long parent_ip, unsigned long flags,
852 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200853{
854 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt38697052008-10-01 13:14:09 -0400855 trace_function(tr, data, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200856}
857
Steven Rostedt38697052008-10-01 13:14:09 -0400858static void ftrace_trace_stack(struct trace_array *tr,
859 struct trace_array_cpu *data,
860 unsigned long flags,
861 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200862{
Al Viroc2c80522008-10-31 19:50:41 +0000863#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400864 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400865 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200866 struct stack_trace trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400867 unsigned long irq_flags;
Ingo Molnar86387f72008-05-12 21:20:51 +0200868
869 if (!(trace_flags & TRACE_ITER_STACKTRACE))
870 return;
871
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400872 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
873 &irq_flags);
874 if (!event)
875 return;
876 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400877 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400878 entry->ent.type = TRACE_STACK;
Ingo Molnar86387f72008-05-12 21:20:51 +0200879
Steven Rostedt777e2082008-09-29 23:02:42 -0400880 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200881
882 trace.nr_entries = 0;
883 trace.max_entries = FTRACE_STACK_ENTRIES;
884 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400885 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200886
887 save_stack_trace(&trace);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400888 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Al Viroc2c80522008-10-31 19:50:41 +0000889#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200890}
891
Steven Rostedt38697052008-10-01 13:14:09 -0400892void __trace_stack(struct trace_array *tr,
893 struct trace_array_cpu *data,
894 unsigned long flags,
895 int skip)
896{
897 ftrace_trace_stack(tr, data, flags, skip, preempt_count());
898}
899
900static void
901ftrace_trace_special(void *__tr, void *__data,
902 unsigned long arg1, unsigned long arg2, unsigned long arg3,
903 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +0200904{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400905 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +0200906 struct trace_array_cpu *data = __data;
907 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -0400908 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +0200909 unsigned long irq_flags;
910
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400911 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
912 &irq_flags);
913 if (!event)
914 return;
915 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400916 tracing_generic_entry_update(&entry->ent, 0, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400917 entry->ent.type = TRACE_SPECIAL;
918 entry->arg1 = arg1;
919 entry->arg2 = arg2;
920 entry->arg3 = arg3;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400921 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -0400922 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +0200923
924 trace_wake_up();
925}
926
927void
Steven Rostedt38697052008-10-01 13:14:09 -0400928__trace_special(void *__tr, void *__data,
929 unsigned long arg1, unsigned long arg2, unsigned long arg3)
930{
931 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
932}
933
934void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200935tracing_sched_switch_trace(struct trace_array *tr,
936 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200937 struct task_struct *prev,
938 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -0400939 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200940{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400941 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400942 struct ctx_switch_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200943 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200944
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400945 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
946 &irq_flags);
947 if (!event)
948 return;
949 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400950 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400951 entry->ent.type = TRACE_CTX;
952 entry->prev_pid = prev->pid;
953 entry->prev_prio = prev->prio;
954 entry->prev_state = prev->state;
955 entry->next_pid = next->pid;
956 entry->next_prio = next->prio;
957 entry->next_state = next->state;
958 entry->next_cpu = task_cpu(next);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400959 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -0400960 ftrace_trace_stack(tr, data, flags, 5, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200961}
962
Ingo Molnar57422792008-05-12 21:20:51 +0200963void
964tracing_sched_wakeup_trace(struct trace_array *tr,
965 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200966 struct task_struct *wakee,
967 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -0400968 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +0200969{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400970 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400971 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +0200972 unsigned long irq_flags;
973
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400974 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
975 &irq_flags);
976 if (!event)
977 return;
978 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400979 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400980 entry->ent.type = TRACE_WAKE;
981 entry->prev_pid = curr->pid;
982 entry->prev_prio = curr->prio;
983 entry->prev_state = curr->state;
984 entry->next_pid = wakee->pid;
985 entry->next_prio = wakee->prio;
986 entry->next_state = wakee->state;
987 entry->next_cpu = task_cpu(wakee);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400988 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -0400989 ftrace_trace_stack(tr, data, flags, 6, pc);
Ingo Molnar017730c2008-05-12 21:20:52 +0200990
991 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200992}
993
Steven Rostedt4902f882008-05-22 00:22:18 -0400994void
995ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
996{
997 struct trace_array *tr = &global_trace;
998 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -0500999 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001000 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001001 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001002
Steven Rostedtc76f0692008-11-07 22:36:02 -05001003 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001004 return;
1005
Steven Rostedt38697052008-10-01 13:14:09 -04001006 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001007 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001008 cpu = raw_smp_processor_id();
1009 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001010
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001011 if (likely(atomic_inc_return(&data->disabled) == 1))
Steven Rostedt38697052008-10-01 13:14:09 -04001012 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001013
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001014 atomic_dec(&data->disabled);
1015 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001016}
1017
Steven Rostedt606576c2008-10-06 19:06:12 -04001018#ifdef CONFIG_FUNCTION_TRACER
Ingo Molnare309b412008-05-12 21:20:51 +02001019static void
Steven Rostedtb2a866f2008-11-03 23:15:57 -05001020function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001021{
1022 struct trace_array *tr = &global_trace;
1023 struct trace_array_cpu *data;
1024 unsigned long flags;
1025 long disabled;
Steven Rostedt38697052008-10-01 13:14:09 -04001026 int cpu, resched;
1027 int pc;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001028
Steven Rostedt60bc0802008-07-10 20:58:16 -04001029 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001030 return;
1031
Steven Rostedt38697052008-10-01 13:14:09 -04001032 pc = preempt_count();
Steven Rostedt182e9f52008-11-03 23:15:56 -05001033 resched = ftrace_preempt_disable();
Steven Rostedt38697052008-10-01 13:14:09 -04001034 local_save_flags(flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001035 cpu = raw_smp_processor_id();
1036 data = tr->data[cpu];
1037 disabled = atomic_inc_return(&data->disabled);
1038
1039 if (likely(disabled == 1))
Steven Rostedt38697052008-10-01 13:14:09 -04001040 trace_function(tr, data, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001041
1042 atomic_dec(&data->disabled);
Steven Rostedt182e9f52008-11-03 23:15:56 -05001043 ftrace_preempt_enable(resched);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001044}
1045
Steven Rostedtb2a866f2008-11-03 23:15:57 -05001046static void
1047function_trace_call(unsigned long ip, unsigned long parent_ip)
1048{
1049 struct trace_array *tr = &global_trace;
1050 struct trace_array_cpu *data;
1051 unsigned long flags;
1052 long disabled;
1053 int cpu;
1054 int pc;
1055
1056 if (unlikely(!ftrace_function_enabled))
1057 return;
1058
1059 /*
1060 * Need to use raw, since this must be called before the
1061 * recursive protection is performed.
1062 */
Steven Rostedtd51ad7a2008-11-15 15:48:29 -05001063 local_irq_save(flags);
Steven Rostedtb2a866f2008-11-03 23:15:57 -05001064 cpu = raw_smp_processor_id();
1065 data = tr->data[cpu];
1066 disabled = atomic_inc_return(&data->disabled);
1067
1068 if (likely(disabled == 1)) {
1069 pc = preempt_count();
1070 trace_function(tr, data, ip, parent_ip, flags, pc);
1071 }
1072
1073 atomic_dec(&data->disabled);
Steven Rostedtd51ad7a2008-11-15 15:48:29 -05001074 local_irq_restore(flags);
Steven Rostedtb2a866f2008-11-03 23:15:57 -05001075}
1076
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001077#ifdef CONFIG_FUNCTION_RET_TRACER
1078void trace_function_return(struct ftrace_retfunc *trace)
1079{
1080 struct trace_array *tr = &global_trace;
1081 struct trace_array_cpu *data;
1082 unsigned long flags;
1083 long disabled;
1084 int cpu;
1085 int pc;
1086
1087 raw_local_irq_save(flags);
1088 cpu = raw_smp_processor_id();
1089 data = tr->data[cpu];
1090 disabled = atomic_inc_return(&data->disabled);
1091 if (likely(disabled == 1)) {
1092 pc = preempt_count();
1093 __trace_function_return(tr, data, trace, flags, pc);
1094 }
1095 atomic_dec(&data->disabled);
1096 raw_local_irq_restore(flags);
1097}
1098#endif /* CONFIG_FUNCTION_RET_TRACER */
1099
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001100static struct ftrace_ops trace_ops __read_mostly =
1101{
1102 .func = function_trace_call,
1103};
1104
Ingo Molnare309b412008-05-12 21:20:51 +02001105void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001106{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001107 ftrace_function_enabled = 0;
Steven Rostedtb2a866f2008-11-03 23:15:57 -05001108
1109 if (trace_flags & TRACE_ITER_PREEMPTONLY)
1110 trace_ops.func = function_trace_call_preempt_only;
1111 else
1112 trace_ops.func = function_trace_call;
1113
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001114 register_ftrace_function(&trace_ops);
Steven Rostedt90369902008-11-05 16:05:44 -05001115 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001116}
1117
Ingo Molnare309b412008-05-12 21:20:51 +02001118void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001119{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001120 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001121 unregister_ftrace_function(&trace_ops);
1122}
1123#endif
1124
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001125enum trace_file_type {
1126 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001127 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001128};
1129
Steven Rostedt5a90f572008-09-03 17:42:51 -04001130static void trace_iterator_increment(struct trace_iterator *iter, int cpu)
1131{
Steven Rostedtd7690412008-10-01 00:29:53 -04001132 /* Don't allow ftrace to trace into the ring buffers */
1133 ftrace_disable_cpu();
1134
Steven Rostedt5a90f572008-09-03 17:42:51 -04001135 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001136 if (iter->buffer_iter[iter->cpu])
1137 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1138
1139 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001140}
1141
Ingo Molnare309b412008-05-12 21:20:51 +02001142static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001143peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001144{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001145 struct ring_buffer_event *event;
1146 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001147
Steven Rostedtd7690412008-10-01 00:29:53 -04001148 /* Don't allow ftrace to trace into the ring buffers */
1149 ftrace_disable_cpu();
1150
1151 if (buf_iter)
1152 event = ring_buffer_iter_peek(buf_iter, ts);
1153 else
1154 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1155
1156 ftrace_enable_cpu();
1157
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001158 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001159}
Steven Rostedtd7690412008-10-01 00:29:53 -04001160
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001161static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001162__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001163{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001164 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001165 struct trace_entry *ent, *next = NULL;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001166 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167 int next_cpu = -1;
1168 int cpu;
1169
Steven Rostedtab464282008-05-12 21:21:00 +02001170 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001171
1172 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001173 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001174
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001175 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001176
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001177 /*
1178 * Pick the entry with the smallest timestamp:
1179 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001180 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001181 next = ent;
1182 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001183 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001184 }
1185 }
1186
1187 if (ent_cpu)
1188 *ent_cpu = next_cpu;
1189
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001190 if (ent_ts)
1191 *ent_ts = next_ts;
1192
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001193 return next;
1194}
1195
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001196/* Find the next real entry, without updating the iterator itself */
1197static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001198find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001199{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001200 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001201}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001202
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001203/* Find the next real entry, and increment the iterator to the next entry */
1204static void *find_next_entry_inc(struct trace_iterator *iter)
1205{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001206 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001207
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001208 if (iter->ent)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001209 trace_iterator_increment(iter, iter->cpu);
1210
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001211 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001212}
1213
Ingo Molnare309b412008-05-12 21:20:51 +02001214static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001215{
Steven Rostedtd7690412008-10-01 00:29:53 -04001216 /* Don't allow ftrace to trace into the ring buffers */
1217 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001218 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001219 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001220}
1221
Ingo Molnare309b412008-05-12 21:20:51 +02001222static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001223{
1224 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001225 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001226 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001227
1228 (*pos)++;
1229
1230 /* can't go backwards */
1231 if (iter->idx > i)
1232 return NULL;
1233
1234 if (iter->idx < 0)
1235 ent = find_next_entry_inc(iter);
1236 else
1237 ent = iter;
1238
1239 while (ent && iter->idx < i)
1240 ent = find_next_entry_inc(iter);
1241
1242 iter->pos = *pos;
1243
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001244 return ent;
1245}
1246
1247static void *s_start(struct seq_file *m, loff_t *pos)
1248{
1249 struct trace_iterator *iter = m->private;
1250 void *p = NULL;
1251 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001252 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001253
1254 mutex_lock(&trace_types_lock);
1255
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001256 if (!current_trace || current_trace != iter->trace) {
1257 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001258 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001259 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001260
1261 atomic_inc(&trace_record_cmdline_disabled);
1262
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001263 if (*pos != iter->pos) {
1264 iter->ent = NULL;
1265 iter->cpu = 0;
1266 iter->idx = -1;
1267
Steven Rostedtd7690412008-10-01 00:29:53 -04001268 ftrace_disable_cpu();
1269
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001270 for_each_tracing_cpu(cpu) {
1271 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001272 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001273
Steven Rostedtd7690412008-10-01 00:29:53 -04001274 ftrace_enable_cpu();
1275
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1277 ;
1278
1279 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001280 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001281 p = s_next(m, p, &l);
1282 }
1283
1284 return p;
1285}
1286
1287static void s_stop(struct seq_file *m, void *p)
1288{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001289 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290 mutex_unlock(&trace_types_lock);
1291}
1292
Abhishek Sagar76094a22008-05-28 00:03:18 +05301293#ifdef CONFIG_KRETPROBES
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001294static inline const char *kretprobed(const char *name)
Abhishek Sagar76094a22008-05-28 00:03:18 +05301295{
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001296 static const char tramp_name[] = "kretprobe_trampoline";
1297 int size = sizeof(tramp_name);
1298
1299 if (strncmp(tramp_name, name, size) == 0)
1300 return "[unknown/kretprobe'd]";
1301 return name;
Abhishek Sagar76094a22008-05-28 00:03:18 +05301302}
1303#else
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001304static inline const char *kretprobed(const char *name)
Abhishek Sagar76094a22008-05-28 00:03:18 +05301305{
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001306 return name;
Abhishek Sagar76094a22008-05-28 00:03:18 +05301307}
1308#endif /* CONFIG_KRETPROBES */
1309
Steven Rostedtb3806b42008-05-12 21:20:46 +02001310static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001311seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001312{
1313#ifdef CONFIG_KALLSYMS
1314 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001315 const char *name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316
1317 kallsyms_lookup(address, NULL, NULL, NULL, str);
1318
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001319 name = kretprobed(str);
1320
1321 return trace_seq_printf(s, fmt, name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001323 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001324}
1325
Steven Rostedtb3806b42008-05-12 21:20:46 +02001326static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001327seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1328 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001329{
1330#ifdef CONFIG_KALLSYMS
1331 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001332 const char *name;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001333
1334 sprint_symbol(str, address);
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001335 name = kretprobed(str);
1336
1337 return trace_seq_printf(s, fmt, name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001338#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001339 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001340}
1341
1342#ifndef CONFIG_64BIT
1343# define IP_FMT "%08lx"
1344#else
1345# define IP_FMT "%016lx"
1346#endif
1347
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001348int
Steven Rostedt214023c2008-05-12 21:20:46 +02001349seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001350{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001351 int ret;
1352
1353 if (!ip)
1354 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001355
1356 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001357 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001359 ret = seq_print_sym_short(s, "%s", ip);
1360
1361 if (!ret)
1362 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001363
1364 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001365 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1366 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001367}
1368
Ingo Molnare309b412008-05-12 21:20:51 +02001369static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370{
Michael Ellermana6168352008-08-20 16:36:11 -07001371 seq_puts(m, "# _------=> CPU# \n");
1372 seq_puts(m, "# / _-----=> irqs-off \n");
1373 seq_puts(m, "# | / _----=> need-resched \n");
1374 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1375 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1376 seq_puts(m, "# |||| / \n");
1377 seq_puts(m, "# ||||| delay \n");
1378 seq_puts(m, "# cmd pid ||||| time | caller \n");
1379 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380}
1381
Ingo Molnare309b412008-05-12 21:20:51 +02001382static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001383{
Michael Ellermana6168352008-08-20 16:36:11 -07001384 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1385 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386}
1387
1388
Ingo Molnare309b412008-05-12 21:20:51 +02001389static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001390print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1391{
1392 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1393 struct trace_array *tr = iter->tr;
1394 struct trace_array_cpu *data = tr->data[tr->cpu];
1395 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001396 unsigned long total;
1397 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001398 const char *name = "preemption";
1399
1400 if (type)
1401 name = type->name;
1402
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001403 entries = ring_buffer_entries(iter->tr->buffer);
1404 total = entries +
1405 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001406
1407 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1408 name, UTS_RELEASE);
1409 seq_puts(m, "-----------------------------------"
1410 "---------------------------------\n");
1411 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1412 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001413 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001414 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001415 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001416 tr->cpu,
1417#if defined(CONFIG_PREEMPT_NONE)
1418 "server",
1419#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1420 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001421#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001422 "preempt",
1423#else
1424 "unknown",
1425#endif
1426 /* These are reserved for later use */
1427 0, 0, 0, 0);
1428#ifdef CONFIG_SMP
1429 seq_printf(m, " #P:%d)\n", num_online_cpus());
1430#else
1431 seq_puts(m, ")\n");
1432#endif
1433 seq_puts(m, " -----------------\n");
1434 seq_printf(m, " | task: %.16s-%d "
1435 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1436 data->comm, data->pid, data->uid, data->nice,
1437 data->policy, data->rt_priority);
1438 seq_puts(m, " -----------------\n");
1439
1440 if (data->critical_start) {
1441 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001442 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1443 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001444 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001445 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1446 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001447 seq_puts(m, "\n");
1448 }
1449
1450 seq_puts(m, "\n");
1451}
1452
Ingo Molnare309b412008-05-12 21:20:51 +02001453static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001454lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455{
1456 int hardirq, softirq;
1457 char *comm;
1458
Steven Rostedt777e2082008-09-29 23:02:42 -04001459 comm = trace_find_cmdline(entry->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001460
Steven Rostedt777e2082008-09-29 23:02:42 -04001461 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001462 trace_seq_printf(s, "%3d", cpu);
Steven Rostedt214023c2008-05-12 21:20:46 +02001463 trace_seq_printf(s, "%c%c",
Steven Rostedt92444892008-10-24 09:42:59 -04001464 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1465 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
Steven Rostedt777e2082008-09-29 23:02:42 -04001466 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001467
Steven Rostedt777e2082008-09-29 23:02:42 -04001468 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1469 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001470 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001471 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001472 } else {
1473 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001474 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001475 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001476 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001477 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001478 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001479 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480 }
1481 }
1482
Steven Rostedt777e2082008-09-29 23:02:42 -04001483 if (entry->preempt_count)
1484 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001485 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001486 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001487}
1488
1489unsigned long preempt_mark_thresh = 100;
1490
Ingo Molnare309b412008-05-12 21:20:51 +02001491static void
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001492lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001493 unsigned long rel_usecs)
1494{
Steven Rostedt214023c2008-05-12 21:20:46 +02001495 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001496 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001497 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001498 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001499 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001500 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001501 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001502}
1503
1504static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1505
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001506/*
1507 * The message is supposed to contain an ending newline.
1508 * If the printing stops prematurely, try to add a newline of our own.
1509 */
1510void trace_seq_print_cont(struct trace_seq *s, struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001511{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001512 struct trace_entry *ent;
Steven Rostedt777e2082008-09-29 23:02:42 -04001513 struct trace_field_cont *cont;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001514 bool ok = true;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001515
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001516 ent = peek_next_entry(iter, iter->cpu, NULL);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001517 if (!ent || ent->type != TRACE_CONT) {
1518 trace_seq_putc(s, '\n');
1519 return;
1520 }
1521
1522 do {
Steven Rostedt777e2082008-09-29 23:02:42 -04001523 cont = (struct trace_field_cont *)ent;
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001524 if (ok)
Steven Rostedt777e2082008-09-29 23:02:42 -04001525 ok = (trace_seq_printf(s, "%s", cont->buf) > 0);
Steven Rostedtd7690412008-10-01 00:29:53 -04001526
1527 ftrace_disable_cpu();
1528
1529 if (iter->buffer_iter[iter->cpu])
1530 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1531 else
1532 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
1533
1534 ftrace_enable_cpu();
1535
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001536 ent = peek_next_entry(iter, iter->cpu, NULL);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001537 } while (ent && ent->type == TRACE_CONT);
Pekka Paalanenfc5e27a2008-09-16 22:02:27 +03001538
1539 if (!ok)
1540 trace_seq_putc(s, '\n');
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001541}
1542
Steven Rostedta3097202008-11-07 22:36:02 -05001543static void test_cpu_buff_start(struct trace_iterator *iter)
1544{
1545 struct trace_seq *s = &iter->seq;
1546
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001547 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1548 return;
1549
1550 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1551 return;
1552
Steven Rostedta3097202008-11-07 22:36:02 -05001553 if (cpu_isset(iter->cpu, iter->started))
1554 return;
1555
1556 cpu_set(iter->cpu, iter->started);
1557 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1558}
1559
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001560static enum print_line_t
Steven Rostedt214023c2008-05-12 21:20:46 +02001561print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001562{
Steven Rostedt214023c2008-05-12 21:20:46 +02001563 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001564 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001565 struct trace_entry *next_entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001566 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1567 struct trace_entry *entry = iter->ent;
1568 unsigned long abs_usecs;
1569 unsigned long rel_usecs;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001570 u64 next_ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001571 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001572 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001573 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001574 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001575
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001576 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001577 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001578
Steven Rostedta3097202008-11-07 22:36:02 -05001579 test_cpu_buff_start(iter);
1580
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001581 next_entry = find_next_entry(iter, NULL, &next_ts);
1582 if (!next_entry)
1583 next_ts = iter->ts;
1584 rel_usecs = ns2usecs(next_ts - iter->ts);
1585 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001586
1587 if (verbose) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001588 comm = trace_find_cmdline(entry->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001589 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
Steven Rostedt214023c2008-05-12 21:20:46 +02001590 " %ld.%03ldms (+%ld.%03ldms): ",
1591 comm,
Steven Rostedt777e2082008-09-29 23:02:42 -04001592 entry->pid, cpu, entry->flags,
1593 entry->preempt_count, trace_idx,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001594 ns2usecs(iter->ts),
Steven Rostedt214023c2008-05-12 21:20:46 +02001595 abs_usecs/1000,
1596 abs_usecs % 1000, rel_usecs/1000,
1597 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001598 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001599 lat_print_generic(s, entry, cpu);
1600 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001601 }
1602 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001603 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001604 struct ftrace_entry *field;
1605
1606 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001607
1608 seq_print_ip_sym(s, field->ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001609 trace_seq_puts(s, " (");
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001610 seq_print_ip_sym(s, field->parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001611 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001612 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001613 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001614 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001615 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001616 struct ctx_switch_entry *field;
1617
1618 trace_assign_type(field, entry);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001619
Steven Rostedt777e2082008-09-29 23:02:42 -04001620 T = field->next_state < sizeof(state_to_char) ?
1621 state_to_char[field->next_state] : 'X';
1622
1623 state = field->prev_state ?
1624 __ffs(field->prev_state) + 1 : 0;
Ankita Gargd17d9692008-05-12 21:20:58 +02001625 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedt777e2082008-09-29 23:02:42 -04001626 comm = trace_find_cmdline(field->next_pid);
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001627 trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001628 field->prev_pid,
1629 field->prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001630 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt777e2082008-09-29 23:02:42 -04001631 field->next_cpu,
1632 field->next_pid,
1633 field->next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001634 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001635 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001636 }
1637 case TRACE_SPECIAL: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001638 struct special_entry *field;
1639
1640 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001641
Ingo Molnar88a42162008-05-12 21:20:53 +02001642 trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001643 field->arg1,
1644 field->arg2,
1645 field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001646 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001647 }
1648 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001649 struct stack_entry *field;
1650
1651 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001652
Ingo Molnar86387f72008-05-12 21:20:51 +02001653 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1654 if (i)
1655 trace_seq_puts(s, " <= ");
Steven Rostedt777e2082008-09-29 23:02:42 -04001656 seq_print_ip_sym(s, field->caller[i], sym_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +02001657 }
1658 trace_seq_puts(s, "\n");
1659 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001660 }
1661 case TRACE_PRINT: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001662 struct print_entry *field;
1663
1664 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001665
1666 seq_print_ip_sym(s, field->ip, sym_flags);
1667 trace_seq_printf(s, ": %s", field->buf);
1668 if (entry->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001669 trace_seq_print_cont(s, iter);
1670 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001671 }
Steven Rostedt9f029e82008-11-12 15:24:24 -05001672 case TRACE_BRANCH: {
1673 struct trace_branch *field;
Steven Rostedt52f232c2008-11-12 00:14:40 -05001674
1675 trace_assign_type(field, entry);
1676
1677 trace_seq_printf(s, "[%s] %s:%s:%d\n",
Ingo Molnar68d119f2008-11-12 14:09:30 +01001678 field->correct ? " ok " : " MISS ",
Steven Rostedt52f232c2008-11-12 00:14:40 -05001679 field->func,
1680 field->file,
1681 field->line);
1682 break;
1683 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02001684 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001685 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001686 }
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001687 return TRACE_TYPE_HANDLED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001688}
1689
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001690static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001691{
Steven Rostedt214023c2008-05-12 21:20:46 +02001692 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001693 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001694 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001695 unsigned long usec_rem;
1696 unsigned long long t;
1697 unsigned long secs;
1698 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001699 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001700 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001701 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001702
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001703 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001704
1705 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001706 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001707
Steven Rostedta3097202008-11-07 22:36:02 -05001708 test_cpu_buff_start(iter);
1709
Steven Rostedt777e2082008-09-29 23:02:42 -04001710 comm = trace_find_cmdline(iter->ent->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001711
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001712 t = ns2usecs(iter->ts);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001713 usec_rem = do_div(t, 1000000ULL);
1714 secs = (unsigned long)t;
1715
Steven Rostedt777e2082008-09-29 23:02:42 -04001716 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001717 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001718 return TRACE_TYPE_PARTIAL_LINE;
Michael Ellermana6168352008-08-20 16:36:11 -07001719 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001720 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001721 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001722 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1723 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001724 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001725
1726 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001727 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001728 struct ftrace_entry *field;
1729
1730 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001731
1732 ret = seq_print_ip_sym(s, field->ip, sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001733 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001734 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001735 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
Steven Rostedt777e2082008-09-29 23:02:42 -04001736 field->parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001737 ret = trace_seq_printf(s, " <-");
1738 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001739 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtb3aa5572008-10-31 15:44:07 -04001740 ret = seq_print_ip_sym(s,
1741 field->parent_ip,
1742 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001743 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001744 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001745 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001746 ret = trace_seq_printf(s, "\n");
1747 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001748 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001749 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001750 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001751 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001752 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001753 struct ctx_switch_entry *field;
1754
1755 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001756
1757 S = field->prev_state < sizeof(state_to_char) ?
1758 state_to_char[field->prev_state] : 'X';
1759 T = field->next_state < sizeof(state_to_char) ?
1760 state_to_char[field->next_state] : 'X';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001761 ret = trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001762 field->prev_pid,
1763 field->prev_prio,
Steven Rostedtb3806b42008-05-12 21:20:46 +02001764 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001765 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt777e2082008-09-29 23:02:42 -04001766 field->next_cpu,
1767 field->next_pid,
1768 field->next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001769 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001770 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001771 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001772 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001773 }
1774 case TRACE_SPECIAL: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001775 struct special_entry *field;
1776
1777 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001778
Ingo Molnar88a42162008-05-12 21:20:53 +02001779 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001780 field->arg1,
1781 field->arg2,
1782 field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001783 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001784 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001785 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001786 }
1787 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001788 struct stack_entry *field;
1789
1790 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001791
Ingo Molnar86387f72008-05-12 21:20:51 +02001792 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1793 if (i) {
1794 ret = trace_seq_puts(s, " <= ");
1795 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001796 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnar86387f72008-05-12 21:20:51 +02001797 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001798 ret = seq_print_ip_sym(s, field->caller[i],
Ingo Molnar86387f72008-05-12 21:20:51 +02001799 sym_flags);
1800 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001801 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnar86387f72008-05-12 21:20:51 +02001802 }
1803 ret = trace_seq_puts(s, "\n");
1804 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001805 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnar86387f72008-05-12 21:20:51 +02001806 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001807 }
1808 case TRACE_PRINT: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001809 struct print_entry *field;
1810
1811 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001812
1813 seq_print_ip_sym(s, field->ip, sym_flags);
1814 trace_seq_printf(s, ": %s", field->buf);
1815 if (entry->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001816 trace_seq_print_cont(s, iter);
1817 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001818 }
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001819 case TRACE_FN_RET: {
1820 return print_return_function(iter);
1821 break;
1822 }
Steven Rostedt9f029e82008-11-12 15:24:24 -05001823 case TRACE_BRANCH: {
1824 struct trace_branch *field;
Steven Rostedt52f232c2008-11-12 00:14:40 -05001825
1826 trace_assign_type(field, entry);
1827
1828 trace_seq_printf(s, "[%s] %s:%s:%d\n",
Ingo Molnar68d119f2008-11-12 14:09:30 +01001829 field->correct ? " ok " : " MISS ",
Steven Rostedt52f232c2008-11-12 00:14:40 -05001830 field->func,
1831 field->file,
1832 field->line);
1833 break;
1834 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001835 }
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001836 return TRACE_TYPE_HANDLED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001837}
1838
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001839static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001840{
1841 struct trace_seq *s = &iter->seq;
1842 struct trace_entry *entry;
1843 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001844 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001845
1846 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001847
1848 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001849 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001850
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001851 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt777e2082008-09-29 23:02:42 -04001852 entry->pid, iter->cpu, iter->ts);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001853 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001854 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001855
1856 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001857 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001858 struct ftrace_entry *field;
1859
1860 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001861
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001862 ret = trace_seq_printf(s, "%x %x\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001863 field->ip,
1864 field->parent_ip);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001865 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001866 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001867 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001868 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001869 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001870 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001871 struct ctx_switch_entry *field;
1872
1873 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001874
1875 S = field->prev_state < sizeof(state_to_char) ?
1876 state_to_char[field->prev_state] : 'X';
1877 T = field->next_state < sizeof(state_to_char) ?
1878 state_to_char[field->next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001879 if (entry->type == TRACE_WAKE)
1880 S = '+';
Peter Zijlstra80b5e942008-09-04 10:24:16 +02001881 ret = trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001882 field->prev_pid,
1883 field->prev_prio,
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001884 S,
Steven Rostedt777e2082008-09-29 23:02:42 -04001885 field->next_cpu,
1886 field->next_pid,
1887 field->next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001888 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001889 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001890 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001891 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001892 }
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001893 case TRACE_SPECIAL:
Steven Rostedt777e2082008-09-29 23:02:42 -04001894 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001895 struct special_entry *field;
1896
1897 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001898
Ingo Molnar88a42162008-05-12 21:20:53 +02001899 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt777e2082008-09-29 23:02:42 -04001900 field->arg1,
1901 field->arg2,
1902 field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001903 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001904 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001905 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001906 }
1907 case TRACE_PRINT: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001908 struct print_entry *field;
1909
1910 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001911
1912 trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
1913 if (entry->flags & TRACE_FLAG_CONT)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001914 trace_seq_print_cont(s, iter);
1915 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001916 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001917 }
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001918 return TRACE_TYPE_HANDLED;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001919}
1920
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001921#define SEQ_PUT_FIELD_RET(s, x) \
1922do { \
1923 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1924 return 0; \
1925} while (0)
1926
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001927#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1928do { \
Harvey Harrisonad0a3b62008-10-08 17:44:55 -07001929 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001930 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1931 return 0; \
1932} while (0)
1933
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001934static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001935{
1936 struct trace_seq *s = &iter->seq;
1937 unsigned char newline = '\n';
1938 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001939 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001940
1941 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001942
1943 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001944 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001945
Steven Rostedt777e2082008-09-29 23:02:42 -04001946 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001947 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001948 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001949
1950 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001951 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001952 struct ftrace_entry *field;
1953
1954 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001955
1956 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1957 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001958 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001959 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001960 case TRACE_CTX:
Steven Rostedt777e2082008-09-29 23:02:42 -04001961 case TRACE_WAKE: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001962 struct ctx_switch_entry *field;
1963
1964 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001965
1966 S = field->prev_state < sizeof(state_to_char) ?
1967 state_to_char[field->prev_state] : 'X';
1968 T = field->next_state < sizeof(state_to_char) ?
1969 state_to_char[field->next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001970 if (entry->type == TRACE_WAKE)
1971 S = '+';
Steven Rostedt777e2082008-09-29 23:02:42 -04001972 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1973 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001974 SEQ_PUT_HEX_FIELD_RET(s, S);
Steven Rostedt777e2082008-09-29 23:02:42 -04001975 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1976 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1977 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001978 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001979 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04001980 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001981 case TRACE_SPECIAL:
Steven Rostedt777e2082008-09-29 23:02:42 -04001982 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04001983 struct special_entry *field;
1984
1985 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04001986
1987 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1988 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1989 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001990 break;
1991 }
Steven Rostedt777e2082008-09-29 23:02:42 -04001992 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001993 SEQ_PUT_FIELD_RET(s, newline);
1994
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001995 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001996}
1997
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001998static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001999{
2000 struct trace_seq *s = &iter->seq;
2001 struct trace_entry *entry;
2002
2003 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002004
2005 if (entry->type == TRACE_CONT)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002006 return TRACE_TYPE_HANDLED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002007
Steven Rostedt777e2082008-09-29 23:02:42 -04002008 SEQ_PUT_FIELD_RET(s, entry->pid);
Eric Anholt072ba492008-10-26 15:26:57 -07002009 SEQ_PUT_FIELD_RET(s, entry->cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002010 SEQ_PUT_FIELD_RET(s, iter->ts);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002011
2012 switch (entry->type) {
Steven Rostedt777e2082008-09-29 23:02:42 -04002013 case TRACE_FN: {
Steven Rostedt7104f302008-10-01 10:52:51 -04002014 struct ftrace_entry *field;
2015
2016 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04002017
2018 SEQ_PUT_FIELD_RET(s, field->ip);
2019 SEQ_PUT_FIELD_RET(s, field->parent_ip);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002020 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04002021 }
2022 case TRACE_CTX: {
Steven Rostedt7104f302008-10-01 10:52:51 -04002023 struct ctx_switch_entry *field;
2024
2025 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04002026
2027 SEQ_PUT_FIELD_RET(s, field->prev_pid);
2028 SEQ_PUT_FIELD_RET(s, field->prev_prio);
2029 SEQ_PUT_FIELD_RET(s, field->prev_state);
2030 SEQ_PUT_FIELD_RET(s, field->next_pid);
2031 SEQ_PUT_FIELD_RET(s, field->next_prio);
2032 SEQ_PUT_FIELD_RET(s, field->next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002033 break;
Steven Rostedt777e2082008-09-29 23:02:42 -04002034 }
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002035 case TRACE_SPECIAL:
Steven Rostedt777e2082008-09-29 23:02:42 -04002036 case TRACE_STACK: {
Steven Rostedt7104f302008-10-01 10:52:51 -04002037 struct special_entry *field;
2038
2039 trace_assign_type(field, entry);
Steven Rostedt777e2082008-09-29 23:02:42 -04002040
2041 SEQ_PUT_FIELD_RET(s, field->arg1);
2042 SEQ_PUT_FIELD_RET(s, field->arg2);
2043 SEQ_PUT_FIELD_RET(s, field->arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02002044 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002045 }
Steven Rostedt777e2082008-09-29 23:02:42 -04002046 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002047 return 1;
2048}
2049
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002050static int trace_empty(struct trace_iterator *iter)
2051{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002052 int cpu;
2053
Steven Rostedtab464282008-05-12 21:21:00 +02002054 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04002055 if (iter->buffer_iter[cpu]) {
2056 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2057 return 0;
2058 } else {
2059 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2060 return 0;
2061 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002063
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002064 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002065}
2066
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002067static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002068{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002069 enum print_line_t ret;
2070
2071 if (iter->trace && iter->trace->print_line) {
2072 ret = iter->trace->print_line(iter);
2073 if (ret != TRACE_TYPE_UNHANDLED)
2074 return ret;
2075 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002076
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002077 if (trace_flags & TRACE_ITER_BIN)
2078 return print_bin_fmt(iter);
2079
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002080 if (trace_flags & TRACE_ITER_HEX)
2081 return print_hex_fmt(iter);
2082
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002083 if (trace_flags & TRACE_ITER_RAW)
2084 return print_raw_fmt(iter);
2085
2086 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2087 return print_lat_fmt(iter, iter->idx, iter->cpu);
2088
2089 return print_trace_fmt(iter);
2090}
2091
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002092static int s_show(struct seq_file *m, void *v)
2093{
2094 struct trace_iterator *iter = v;
2095
2096 if (iter->ent == NULL) {
2097 if (iter->tr) {
2098 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2099 seq_puts(m, "#\n");
2100 }
2101 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2102 /* print nothing if the buffers are empty */
2103 if (trace_empty(iter))
2104 return 0;
2105 print_trace_header(m, iter);
2106 if (!(trace_flags & TRACE_ITER_VERBOSE))
2107 print_lat_help_header(m);
2108 } else {
2109 if (!(trace_flags & TRACE_ITER_VERBOSE))
2110 print_func_help_header(m);
2111 }
2112 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002113 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02002114 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002115 }
2116
2117 return 0;
2118}
2119
2120static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002121 .start = s_start,
2122 .next = s_next,
2123 .stop = s_stop,
2124 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002125};
2126
Ingo Molnare309b412008-05-12 21:20:51 +02002127static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002128__tracing_open(struct inode *inode, struct file *file, int *ret)
2129{
2130 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002131 struct seq_file *m;
2132 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002133
Steven Rostedt60a11772008-05-12 21:20:44 +02002134 if (tracing_disabled) {
2135 *ret = -ENODEV;
2136 return NULL;
2137 }
2138
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002139 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2140 if (!iter) {
2141 *ret = -ENOMEM;
2142 goto out;
2143 }
2144
2145 mutex_lock(&trace_types_lock);
2146 if (current_trace && current_trace->print_max)
2147 iter->tr = &max_tr;
2148 else
2149 iter->tr = inode->i_private;
2150 iter->trace = current_trace;
2151 iter->pos = -1;
2152
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002153 /* Annotate start of buffers if we had overruns */
2154 if (ring_buffer_overruns(iter->tr->buffer))
2155 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2156
2157
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002158 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04002159
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002160 iter->buffer_iter[cpu] =
2161 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04002162
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002163 if (!iter->buffer_iter[cpu])
2164 goto fail_buffer;
2165 }
2166
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002167 /* TODO stop tracer */
2168 *ret = seq_open(file, &tracer_seq_ops);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002169 if (*ret)
2170 goto fail_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002171
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002172 m = file->private_data;
2173 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002174
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002175 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05002176 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002177
2178 if (iter->trace && iter->trace->open)
2179 iter->trace->open(iter);
2180
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002181 mutex_unlock(&trace_types_lock);
2182
2183 out:
2184 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002185
2186 fail_buffer:
2187 for_each_tracing_cpu(cpu) {
2188 if (iter->buffer_iter[cpu])
2189 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2190 }
2191 mutex_unlock(&trace_types_lock);
2192
2193 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002194}
2195
2196int tracing_open_generic(struct inode *inode, struct file *filp)
2197{
Steven Rostedt60a11772008-05-12 21:20:44 +02002198 if (tracing_disabled)
2199 return -ENODEV;
2200
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002201 filp->private_data = inode->i_private;
2202 return 0;
2203}
2204
2205int tracing_release(struct inode *inode, struct file *file)
2206{
2207 struct seq_file *m = (struct seq_file *)file->private_data;
2208 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002209 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002210
2211 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002212 for_each_tracing_cpu(cpu) {
2213 if (iter->buffer_iter[cpu])
2214 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2215 }
2216
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002217 if (iter->trace && iter->trace->close)
2218 iter->trace->close(iter);
2219
2220 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05002221 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002222 mutex_unlock(&trace_types_lock);
2223
2224 seq_release(inode, file);
2225 kfree(iter);
2226 return 0;
2227}
2228
2229static int tracing_open(struct inode *inode, struct file *file)
2230{
2231 int ret;
2232
2233 __tracing_open(inode, file, &ret);
2234
2235 return ret;
2236}
2237
2238static int tracing_lt_open(struct inode *inode, struct file *file)
2239{
2240 struct trace_iterator *iter;
2241 int ret;
2242
2243 iter = __tracing_open(inode, file, &ret);
2244
2245 if (!ret)
2246 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2247
2248 return ret;
2249}
2250
2251
Ingo Molnare309b412008-05-12 21:20:51 +02002252static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002253t_next(struct seq_file *m, void *v, loff_t *pos)
2254{
2255 struct tracer *t = m->private;
2256
2257 (*pos)++;
2258
2259 if (t)
2260 t = t->next;
2261
2262 m->private = t;
2263
2264 return t;
2265}
2266
2267static void *t_start(struct seq_file *m, loff_t *pos)
2268{
2269 struct tracer *t = m->private;
2270 loff_t l = 0;
2271
2272 mutex_lock(&trace_types_lock);
2273 for (; t && l < *pos; t = t_next(m, t, &l))
2274 ;
2275
2276 return t;
2277}
2278
2279static void t_stop(struct seq_file *m, void *p)
2280{
2281 mutex_unlock(&trace_types_lock);
2282}
2283
2284static int t_show(struct seq_file *m, void *v)
2285{
2286 struct tracer *t = v;
2287
2288 if (!t)
2289 return 0;
2290
2291 seq_printf(m, "%s", t->name);
2292 if (t->next)
2293 seq_putc(m, ' ');
2294 else
2295 seq_putc(m, '\n');
2296
2297 return 0;
2298}
2299
2300static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002301 .start = t_start,
2302 .next = t_next,
2303 .stop = t_stop,
2304 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002305};
2306
2307static int show_traces_open(struct inode *inode, struct file *file)
2308{
2309 int ret;
2310
Steven Rostedt60a11772008-05-12 21:20:44 +02002311 if (tracing_disabled)
2312 return -ENODEV;
2313
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002314 ret = seq_open(file, &show_traces_seq_ops);
2315 if (!ret) {
2316 struct seq_file *m = file->private_data;
2317 m->private = trace_types;
2318 }
2319
2320 return ret;
2321}
2322
2323static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002324 .open = tracing_open,
2325 .read = seq_read,
2326 .llseek = seq_lseek,
2327 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002328};
2329
2330static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002331 .open = tracing_lt_open,
2332 .read = seq_read,
2333 .llseek = seq_lseek,
2334 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002335};
2336
2337static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002338 .open = show_traces_open,
2339 .read = seq_read,
2340 .release = seq_release,
2341};
2342
Ingo Molnar36dfe922008-05-12 21:20:52 +02002343/*
2344 * Only trace on a CPU if the bitmask is set:
2345 */
2346static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2347
2348/*
2349 * When tracing/tracing_cpu_mask is modified then this holds
2350 * the new bitmask we are about to install:
2351 */
2352static cpumask_t tracing_cpumask_new;
2353
2354/*
2355 * The tracer itself will not take this lock, but still we want
2356 * to provide a consistent cpumask to user-space:
2357 */
2358static DEFINE_MUTEX(tracing_cpumask_update_lock);
2359
2360/*
2361 * Temporary storage for the character representation of the
2362 * CPU bitmask (and one more byte for the newline):
2363 */
2364static char mask_str[NR_CPUS + 1];
2365
Ingo Molnarc7078de2008-05-12 21:20:52 +02002366static ssize_t
2367tracing_cpumask_read(struct file *filp, char __user *ubuf,
2368 size_t count, loff_t *ppos)
2369{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002370 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002371
2372 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002373
2374 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2375 if (count - len < 2) {
2376 count = -EINVAL;
2377 goto out_err;
2378 }
2379 len += sprintf(mask_str + len, "\n");
2380 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2381
2382out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002383 mutex_unlock(&tracing_cpumask_update_lock);
2384
2385 return count;
2386}
2387
2388static ssize_t
2389tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2390 size_t count, loff_t *ppos)
2391{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002392 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002393
2394 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002395 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2396 if (err)
2397 goto err_unlock;
2398
Steven Rostedt92205c22008-05-12 21:20:55 +02002399 raw_local_irq_disable();
2400 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002401 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002402 /*
2403 * Increase/decrease the disabled counter if we are
2404 * about to flip a bit in the cpumask:
2405 */
2406 if (cpu_isset(cpu, tracing_cpumask) &&
2407 !cpu_isset(cpu, tracing_cpumask_new)) {
2408 atomic_inc(&global_trace.data[cpu]->disabled);
2409 }
2410 if (!cpu_isset(cpu, tracing_cpumask) &&
2411 cpu_isset(cpu, tracing_cpumask_new)) {
2412 atomic_dec(&global_trace.data[cpu]->disabled);
2413 }
2414 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002415 __raw_spin_unlock(&ftrace_max_lock);
2416 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002417
2418 tracing_cpumask = tracing_cpumask_new;
2419
Ingo Molnarc7078de2008-05-12 21:20:52 +02002420 mutex_unlock(&tracing_cpumask_update_lock);
2421
Ingo Molnarc7078de2008-05-12 21:20:52 +02002422 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002423
2424err_unlock:
2425 mutex_unlock(&tracing_cpumask_update_lock);
2426
2427 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002428}
2429
2430static struct file_operations tracing_cpumask_fops = {
2431 .open = tracing_open_generic,
2432 .read = tracing_cpumask_read,
2433 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002434};
2435
2436static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002437tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002438 size_t cnt, loff_t *ppos)
2439{
2440 char *buf;
2441 int r = 0;
2442 int len = 0;
2443 int i;
2444
2445 /* calulate max size */
2446 for (i = 0; trace_options[i]; i++) {
2447 len += strlen(trace_options[i]);
2448 len += 3; /* "no" and space */
2449 }
2450
2451 /* +2 for \n and \0 */
2452 buf = kmalloc(len + 2, GFP_KERNEL);
2453 if (!buf)
2454 return -ENOMEM;
2455
2456 for (i = 0; trace_options[i]; i++) {
2457 if (trace_flags & (1 << i))
2458 r += sprintf(buf + r, "%s ", trace_options[i]);
2459 else
2460 r += sprintf(buf + r, "no%s ", trace_options[i]);
2461 }
2462
2463 r += sprintf(buf + r, "\n");
2464 WARN_ON(r >= len + 2);
2465
Ingo Molnar36dfe922008-05-12 21:20:52 +02002466 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002467
2468 kfree(buf);
2469
2470 return r;
2471}
2472
2473static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002474tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002475 size_t cnt, loff_t *ppos)
2476{
2477 char buf[64];
2478 char *cmp = buf;
2479 int neg = 0;
2480 int i;
2481
Steven Rostedtcffae432008-05-12 21:21:00 +02002482 if (cnt >= sizeof(buf))
2483 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002484
2485 if (copy_from_user(&buf, ubuf, cnt))
2486 return -EFAULT;
2487
2488 buf[cnt] = 0;
2489
2490 if (strncmp(buf, "no", 2) == 0) {
2491 neg = 1;
2492 cmp += 2;
2493 }
2494
2495 for (i = 0; trace_options[i]; i++) {
2496 int len = strlen(trace_options[i]);
2497
2498 if (strncmp(cmp, trace_options[i], len) == 0) {
2499 if (neg)
2500 trace_flags &= ~(1 << i);
2501 else
2502 trace_flags |= (1 << i);
2503 break;
2504 }
2505 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002506 /*
2507 * If no option could be set, return an error:
2508 */
2509 if (!trace_options[i])
2510 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002511
2512 filp->f_pos += cnt;
2513
2514 return cnt;
2515}
2516
2517static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002518 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002519 .read = tracing_trace_options_read,
2520 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002521};
2522
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002523static const char readme_msg[] =
2524 "tracing mini-HOWTO:\n\n"
2525 "# mkdir /debug\n"
2526 "# mount -t debugfs nodev /debug\n\n"
2527 "# cat /debug/tracing/available_tracers\n"
2528 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2529 "# cat /debug/tracing/current_tracer\n"
2530 "none\n"
2531 "# echo sched_switch > /debug/tracing/current_tracer\n"
2532 "# cat /debug/tracing/current_tracer\n"
2533 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002534 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002535 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002536 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002537 "# echo 1 > /debug/tracing/tracing_enabled\n"
2538 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2539 "echo 0 > /debug/tracing/tracing_enabled\n"
2540;
2541
2542static ssize_t
2543tracing_readme_read(struct file *filp, char __user *ubuf,
2544 size_t cnt, loff_t *ppos)
2545{
2546 return simple_read_from_buffer(ubuf, cnt, ppos,
2547 readme_msg, strlen(readme_msg));
2548}
2549
2550static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002551 .open = tracing_open_generic,
2552 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002553};
2554
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002555static ssize_t
2556tracing_ctrl_read(struct file *filp, char __user *ubuf,
2557 size_t cnt, loff_t *ppos)
2558{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002559 char buf[64];
2560 int r;
2561
Steven Rostedt90369902008-11-05 16:05:44 -05002562 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002563 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002564}
2565
2566static ssize_t
2567tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2568 size_t cnt, loff_t *ppos)
2569{
2570 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002571 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002572 long val;
2573 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002574
Steven Rostedtcffae432008-05-12 21:21:00 +02002575 if (cnt >= sizeof(buf))
2576 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002577
2578 if (copy_from_user(&buf, ubuf, cnt))
2579 return -EFAULT;
2580
2581 buf[cnt] = 0;
2582
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002583 ret = strict_strtoul(buf, 10, &val);
2584 if (ret < 0)
2585 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002586
2587 val = !!val;
2588
2589 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002590 if (tracer_enabled ^ val) {
2591 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002592 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002593 if (current_trace->start)
2594 current_trace->start(tr);
2595 tracing_start();
2596 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002597 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002598 tracing_stop();
2599 if (current_trace->stop)
2600 current_trace->stop(tr);
2601 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002602 }
2603 mutex_unlock(&trace_types_lock);
2604
2605 filp->f_pos += cnt;
2606
2607 return cnt;
2608}
2609
2610static ssize_t
2611tracing_set_trace_read(struct file *filp, char __user *ubuf,
2612 size_t cnt, loff_t *ppos)
2613{
2614 char buf[max_tracer_type_len+2];
2615 int r;
2616
2617 mutex_lock(&trace_types_lock);
2618 if (current_trace)
2619 r = sprintf(buf, "%s\n", current_trace->name);
2620 else
2621 r = sprintf(buf, "\n");
2622 mutex_unlock(&trace_types_lock);
2623
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002624 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002625}
2626
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002627static int tracing_set_tracer(char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002628{
2629 struct trace_array *tr = &global_trace;
2630 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002631 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002632
2633 mutex_lock(&trace_types_lock);
2634 for (t = trace_types; t; t = t->next) {
2635 if (strcmp(t->name, buf) == 0)
2636 break;
2637 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002638 if (!t) {
2639 ret = -EINVAL;
2640 goto out;
2641 }
2642 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002643 goto out;
2644
Steven Rostedt9f029e82008-11-12 15:24:24 -05002645 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002646 if (current_trace && current_trace->reset)
2647 current_trace->reset(tr);
2648
2649 current_trace = t;
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002650 if (t->init) {
2651 ret = t->init(tr);
2652 if (ret)
2653 goto out;
2654 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002655
Steven Rostedt9f029e82008-11-12 15:24:24 -05002656 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002657 out:
2658 mutex_unlock(&trace_types_lock);
2659
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002660 return ret;
2661}
2662
2663static ssize_t
2664tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2665 size_t cnt, loff_t *ppos)
2666{
2667 char buf[max_tracer_type_len+1];
2668 int i;
2669 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002670 int err;
2671
2672 ret = cnt;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002673
2674 if (cnt > max_tracer_type_len)
2675 cnt = max_tracer_type_len;
2676
2677 if (copy_from_user(&buf, ubuf, cnt))
2678 return -EFAULT;
2679
2680 buf[cnt] = 0;
2681
2682 /* strip ending whitespace. */
2683 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2684 buf[i] = 0;
2685
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002686 err = tracing_set_tracer(buf);
2687 if (err)
2688 return err;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002689
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002690 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002691
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002692 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002693}
2694
2695static ssize_t
2696tracing_max_lat_read(struct file *filp, char __user *ubuf,
2697 size_t cnt, loff_t *ppos)
2698{
2699 unsigned long *ptr = filp->private_data;
2700 char buf[64];
2701 int r;
2702
Steven Rostedtcffae432008-05-12 21:21:00 +02002703 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002704 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002705 if (r > sizeof(buf))
2706 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002707 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002708}
2709
2710static ssize_t
2711tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2712 size_t cnt, loff_t *ppos)
2713{
2714 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002715 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002716 long val;
2717 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002718
Steven Rostedtcffae432008-05-12 21:21:00 +02002719 if (cnt >= sizeof(buf))
2720 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002721
2722 if (copy_from_user(&buf, ubuf, cnt))
2723 return -EFAULT;
2724
2725 buf[cnt] = 0;
2726
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002727 ret = strict_strtoul(buf, 10, &val);
2728 if (ret < 0)
2729 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002730
2731 *ptr = val * 1000;
2732
2733 return cnt;
2734}
2735
Steven Rostedtb3806b42008-05-12 21:20:46 +02002736static atomic_t tracing_reader;
2737
2738static int tracing_open_pipe(struct inode *inode, struct file *filp)
2739{
2740 struct trace_iterator *iter;
2741
2742 if (tracing_disabled)
2743 return -ENODEV;
2744
2745 /* We only allow for reader of the pipe */
2746 if (atomic_inc_return(&tracing_reader) != 1) {
2747 atomic_dec(&tracing_reader);
2748 return -EBUSY;
2749 }
2750
2751 /* create a buffer to store the information to pass to userspace */
2752 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2753 if (!iter)
2754 return -ENOMEM;
2755
Steven Rostedt107bad82008-05-12 21:21:01 +02002756 mutex_lock(&trace_types_lock);
Steven Rostedta3097202008-11-07 22:36:02 -05002757
2758 /* trace pipe does not show start of buffer */
2759 cpus_setall(iter->started);
2760
Steven Rostedtb3806b42008-05-12 21:20:46 +02002761 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002762 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002763 filp->private_data = iter;
2764
Steven Rostedt107bad82008-05-12 21:21:01 +02002765 if (iter->trace->pipe_open)
2766 iter->trace->pipe_open(iter);
2767 mutex_unlock(&trace_types_lock);
2768
Steven Rostedtb3806b42008-05-12 21:20:46 +02002769 return 0;
2770}
2771
2772static int tracing_release_pipe(struct inode *inode, struct file *file)
2773{
2774 struct trace_iterator *iter = file->private_data;
2775
2776 kfree(iter);
2777 atomic_dec(&tracing_reader);
2778
2779 return 0;
2780}
2781
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002782static unsigned int
2783tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2784{
2785 struct trace_iterator *iter = filp->private_data;
2786
2787 if (trace_flags & TRACE_ITER_BLOCK) {
2788 /*
2789 * Always select as readable when in blocking mode
2790 */
2791 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002792 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002793 if (!trace_empty(iter))
2794 return POLLIN | POLLRDNORM;
2795 poll_wait(filp, &trace_wait, poll_table);
2796 if (!trace_empty(iter))
2797 return POLLIN | POLLRDNORM;
2798
2799 return 0;
2800 }
2801}
2802
Steven Rostedtb3806b42008-05-12 21:20:46 +02002803/*
2804 * Consumer reader.
2805 */
2806static ssize_t
2807tracing_read_pipe(struct file *filp, char __user *ubuf,
2808 size_t cnt, loff_t *ppos)
2809{
2810 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002811 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002812
2813 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002814 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2815 if (sret != -EBUSY)
2816 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002817
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002818 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002819
Steven Rostedt107bad82008-05-12 21:21:01 +02002820 mutex_lock(&trace_types_lock);
2821 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002822 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2823 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002824 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002825 }
2826
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002827waitagain:
2828 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002829 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002830
Steven Rostedt107bad82008-05-12 21:21:01 +02002831 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002832 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002833 goto out;
2834 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002835
Steven Rostedtb3806b42008-05-12 21:20:46 +02002836 /*
2837 * This is a make-shift waitqueue. The reason we don't use
2838 * an actual wait queue is because:
2839 * 1) we only ever have one waiter
2840 * 2) the tracing, traces all functions, we don't want
2841 * the overhead of calling wake_up and friends
2842 * (and tracing them too)
2843 * Anyway, this is really very primitive wakeup.
2844 */
2845 set_current_state(TASK_INTERRUPTIBLE);
2846 iter->tr->waiter = current;
2847
Steven Rostedt107bad82008-05-12 21:21:01 +02002848 mutex_unlock(&trace_types_lock);
2849
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002850 /* sleep for 100 msecs, and try again. */
2851 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002852
Steven Rostedt107bad82008-05-12 21:21:01 +02002853 mutex_lock(&trace_types_lock);
2854
Steven Rostedtb3806b42008-05-12 21:20:46 +02002855 iter->tr->waiter = NULL;
2856
Steven Rostedt107bad82008-05-12 21:21:01 +02002857 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002858 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002859 goto out;
2860 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002861
Steven Rostedt84527992008-05-12 21:20:58 +02002862 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002863 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002864
Steven Rostedtb3806b42008-05-12 21:20:46 +02002865 /*
2866 * We block until we read something and tracing is disabled.
2867 * We still block if tracing is disabled, but we have never
2868 * read anything. This allows a user to cat this file, and
2869 * then enable tracing. But after we have read something,
2870 * we give an EOF when tracing is again disabled.
2871 *
2872 * iter->pos will be 0 if we haven't read anything.
2873 */
2874 if (!tracer_enabled && iter->pos)
2875 break;
2876
2877 continue;
2878 }
2879
2880 /* stop when tracing is finished */
2881 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002882 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002883
2884 if (cnt >= PAGE_SIZE)
2885 cnt = PAGE_SIZE - 1;
2886
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002887 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002888 memset(&iter->seq, 0,
2889 sizeof(struct trace_iterator) -
2890 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002891 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002892
Steven Rostedt088b1e422008-05-12 21:20:48 +02002893 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002894 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002895 int len = iter->seq.len;
2896
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002897 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002898 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002899 /* don't print partial lines */
2900 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002901 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002902 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002903
2904 trace_consume(iter);
2905
2906 if (iter->seq.len >= cnt)
2907 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002908 }
2909
Steven Rostedtb3806b42008-05-12 21:20:46 +02002910 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002911 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2912 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002913 trace_seq_reset(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002914
2915 /*
2916 * If there was nothing to send to user, inspite of consuming trace
2917 * entries, go back to wait for more entries.
2918 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002919 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002920 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002921
Steven Rostedt107bad82008-05-12 21:21:01 +02002922out:
2923 mutex_unlock(&trace_types_lock);
2924
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002925 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002926}
2927
Steven Rostedta98a3c32008-05-12 21:20:59 +02002928static ssize_t
2929tracing_entries_read(struct file *filp, char __user *ubuf,
2930 size_t cnt, loff_t *ppos)
2931{
2932 struct trace_array *tr = filp->private_data;
2933 char buf[64];
2934 int r;
2935
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002936 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002937 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2938}
2939
2940static ssize_t
2941tracing_entries_write(struct file *filp, const char __user *ubuf,
2942 size_t cnt, loff_t *ppos)
2943{
2944 unsigned long val;
2945 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002946 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002947
Steven Rostedtcffae432008-05-12 21:21:00 +02002948 if (cnt >= sizeof(buf))
2949 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002950
2951 if (copy_from_user(&buf, ubuf, cnt))
2952 return -EFAULT;
2953
2954 buf[cnt] = 0;
2955
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002956 ret = strict_strtoul(buf, 10, &val);
2957 if (ret < 0)
2958 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002959
2960 /* must have at least 1 entry */
2961 if (!val)
2962 return -EINVAL;
2963
2964 mutex_lock(&trace_types_lock);
2965
Steven Rostedtc76f0692008-11-07 22:36:02 -05002966 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002967
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002968 /* disable all cpu buffers */
2969 for_each_tracing_cpu(cpu) {
2970 if (global_trace.data[cpu])
2971 atomic_inc(&global_trace.data[cpu]->disabled);
2972 if (max_tr.data[cpu])
2973 atomic_inc(&max_tr.data[cpu]->disabled);
2974 }
2975
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002976 /* value is in KB */
2977 val <<= 10;
2978
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002979 if (val != global_trace.entries) {
2980 ret = ring_buffer_resize(global_trace.buffer, val);
2981 if (ret < 0) {
2982 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002983 goto out;
2984 }
2985
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002986 ret = ring_buffer_resize(max_tr.buffer, val);
2987 if (ret < 0) {
2988 int r;
2989 cnt = ret;
2990 r = ring_buffer_resize(global_trace.buffer,
2991 global_trace.entries);
2992 if (r < 0) {
2993 /* AARGH! We are left with different
2994 * size max buffer!!!! */
2995 WARN_ON(1);
2996 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002997 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002998 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002999 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02003000
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003001 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003002 }
3003
3004 filp->f_pos += cnt;
3005
Steven Rostedt19384c02008-05-22 00:22:16 -04003006 /* If check pages failed, return ENOMEM */
3007 if (tracing_disabled)
3008 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003009 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003010 for_each_tracing_cpu(cpu) {
3011 if (global_trace.data[cpu])
3012 atomic_dec(&global_trace.data[cpu]->disabled);
3013 if (max_tr.data[cpu])
3014 atomic_dec(&max_tr.data[cpu]->disabled);
3015 }
3016
Steven Rostedtc76f0692008-11-07 22:36:02 -05003017 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003018 max_tr.entries = global_trace.entries;
3019 mutex_unlock(&trace_types_lock);
3020
3021 return cnt;
3022}
3023
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003024static int mark_printk(const char *fmt, ...)
3025{
3026 int ret;
3027 va_list args;
3028 va_start(args, fmt);
3029 ret = trace_vprintk(0, fmt, args);
3030 va_end(args);
3031 return ret;
3032}
3033
3034static ssize_t
3035tracing_mark_write(struct file *filp, const char __user *ubuf,
3036 size_t cnt, loff_t *fpos)
3037{
3038 char *buf;
3039 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003040
Steven Rostedtc76f0692008-11-07 22:36:02 -05003041 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003042 return -EINVAL;
3043
3044 if (cnt > TRACE_BUF_SIZE)
3045 cnt = TRACE_BUF_SIZE;
3046
3047 buf = kmalloc(cnt + 1, GFP_KERNEL);
3048 if (buf == NULL)
3049 return -ENOMEM;
3050
3051 if (copy_from_user(buf, ubuf, cnt)) {
3052 kfree(buf);
3053 return -EFAULT;
3054 }
3055
3056 /* Cut from the first nil or newline. */
3057 buf[cnt] = '\0';
3058 end = strchr(buf, '\n');
3059 if (end)
3060 *end = '\0';
3061
3062 cnt = mark_printk("%s\n", buf);
3063 kfree(buf);
3064 *fpos += cnt;
3065
3066 return cnt;
3067}
3068
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003069static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003070 .open = tracing_open_generic,
3071 .read = tracing_max_lat_read,
3072 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003073};
3074
3075static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003076 .open = tracing_open_generic,
3077 .read = tracing_ctrl_read,
3078 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003079};
3080
3081static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003082 .open = tracing_open_generic,
3083 .read = tracing_set_trace_read,
3084 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003085};
3086
Steven Rostedtb3806b42008-05-12 21:20:46 +02003087static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003088 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003089 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003090 .read = tracing_read_pipe,
3091 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003092};
3093
Steven Rostedta98a3c32008-05-12 21:20:59 +02003094static struct file_operations tracing_entries_fops = {
3095 .open = tracing_open_generic,
3096 .read = tracing_entries_read,
3097 .write = tracing_entries_write,
3098};
3099
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003100static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003101 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003102 .write = tracing_mark_write,
3103};
3104
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003105#ifdef CONFIG_DYNAMIC_FTRACE
3106
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003107int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003108{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003109 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003110}
3111
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003112static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003113tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003114 size_t cnt, loff_t *ppos)
3115{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003116 static char ftrace_dyn_info_buffer[1024];
3117 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003118 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003119 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003120 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003121 int r;
3122
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003123 mutex_lock(&dyn_info_mutex);
3124 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003125
Steven Rostedta26a2a22008-10-31 00:03:22 -04003126 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003127 buf[r++] = '\n';
3128
3129 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3130
3131 mutex_unlock(&dyn_info_mutex);
3132
3133 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003134}
3135
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003136static struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003137 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003138 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003139};
3140#endif
3141
3142static struct dentry *d_tracer;
3143
3144struct dentry *tracing_init_dentry(void)
3145{
3146 static int once;
3147
3148 if (d_tracer)
3149 return d_tracer;
3150
3151 d_tracer = debugfs_create_dir("tracing", NULL);
3152
3153 if (!d_tracer && !once) {
3154 once = 1;
3155 pr_warning("Could not create debugfs directory 'tracing'\n");
3156 return NULL;
3157 }
3158
3159 return d_tracer;
3160}
3161
Steven Rostedt60a11772008-05-12 21:20:44 +02003162#ifdef CONFIG_FTRACE_SELFTEST
3163/* Let selftest have access to static functions in this file */
3164#include "trace_selftest.c"
3165#endif
3166
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003167static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003168{
3169 struct dentry *d_tracer;
3170 struct dentry *entry;
3171
3172 d_tracer = tracing_init_dentry();
3173
3174 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3175 &global_trace, &tracing_ctrl_fops);
3176 if (!entry)
3177 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3178
Steven Rostedtee6bce52008-11-12 17:52:37 -05003179 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003180 NULL, &tracing_iter_fops);
3181 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003182 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003183
Ingo Molnarc7078de2008-05-12 21:20:52 +02003184 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3185 NULL, &tracing_cpumask_fops);
3186 if (!entry)
3187 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3188
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003189 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
3190 &global_trace, &tracing_lt_fops);
3191 if (!entry)
3192 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3193
3194 entry = debugfs_create_file("trace", 0444, d_tracer,
3195 &global_trace, &tracing_fops);
3196 if (!entry)
3197 pr_warning("Could not create debugfs 'trace' entry\n");
3198
3199 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3200 &global_trace, &show_traces_fops);
3201 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003202 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003203
3204 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3205 &global_trace, &set_tracer_fops);
3206 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003207 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003208
3209 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3210 &tracing_max_latency,
3211 &tracing_max_lat_fops);
3212 if (!entry)
3213 pr_warning("Could not create debugfs "
3214 "'tracing_max_latency' entry\n");
3215
3216 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3217 &tracing_thresh, &tracing_max_lat_fops);
3218 if (!entry)
3219 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003220 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003221 entry = debugfs_create_file("README", 0644, d_tracer,
3222 NULL, &tracing_readme_fops);
3223 if (!entry)
3224 pr_warning("Could not create debugfs 'README' entry\n");
3225
Steven Rostedtb3806b42008-05-12 21:20:46 +02003226 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
3227 NULL, &tracing_pipe_fops);
3228 if (!entry)
3229 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003230 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003231
Steven Rostedta94c80e2008-11-12 17:52:36 -05003232 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003233 &global_trace, &tracing_entries_fops);
3234 if (!entry)
3235 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003236 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003237
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003238 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3239 NULL, &tracing_mark_fops);
3240 if (!entry)
3241 pr_warning("Could not create debugfs "
3242 "'trace_marker' entry\n");
3243
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003244#ifdef CONFIG_DYNAMIC_FTRACE
3245 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3246 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003247 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003248 if (!entry)
3249 pr_warning("Could not create debugfs "
3250 "'dyn_ftrace_total_info' entry\n");
3251#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003252#ifdef CONFIG_SYSPROF_TRACER
3253 init_tracer_sysprof_debugfs(d_tracer);
3254#endif
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003255 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003256}
3257
Pekka Paalanen801fe402008-09-16 21:58:24 +03003258int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003259{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003260 static DEFINE_SPINLOCK(trace_buf_lock);
3261 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003262
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003263 struct ring_buffer_event *event;
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003264 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003265 struct trace_array_cpu *data;
Steven Rostedt777e2082008-09-29 23:02:42 -04003266 struct print_entry *entry;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003267 unsigned long flags, irq_flags;
Steven Rostedt38697052008-10-01 13:14:09 -04003268 int cpu, len = 0, size, pc;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003269
Steven Rostedtc76f0692008-11-07 22:36:02 -05003270 if (tracing_disabled)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003271 return 0;
3272
Steven Rostedt38697052008-10-01 13:14:09 -04003273 pc = preempt_count();
3274 preempt_disable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003275 cpu = raw_smp_processor_id();
3276 data = tr->data[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003277
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -04003278 if (unlikely(atomic_read(&data->disabled)))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003279 goto out;
3280
Steven Rostedt38697052008-10-01 13:14:09 -04003281 spin_lock_irqsave(&trace_buf_lock, flags);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003282 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003283
3284 len = min(len, TRACE_BUF_SIZE-1);
3285 trace_buf[len] = 0;
3286
Steven Rostedt777e2082008-09-29 23:02:42 -04003287 size = sizeof(*entry) + len + 1;
3288 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003289 if (!event)
3290 goto out_unlock;
Steven Rostedt777e2082008-09-29 23:02:42 -04003291 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -04003292 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -04003293 entry->ent.type = TRACE_PRINT;
3294 entry->ip = ip;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003295
Steven Rostedt777e2082008-09-29 23:02:42 -04003296 memcpy(&entry->buf, trace_buf, len);
3297 entry->buf[len] = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003298 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003299
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003300 out_unlock:
Steven Rostedt38697052008-10-01 13:14:09 -04003301 spin_unlock_irqrestore(&trace_buf_lock, flags);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003302
3303 out:
Steven Rostedt38697052008-10-01 13:14:09 -04003304 preempt_enable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003305
3306 return len;
3307}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003308EXPORT_SYMBOL_GPL(trace_vprintk);
3309
3310int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3311{
3312 int ret;
3313 va_list ap;
3314
3315 if (!(trace_flags & TRACE_ITER_PRINTK))
3316 return 0;
3317
3318 va_start(ap, fmt);
3319 ret = trace_vprintk(ip, fmt, ap);
3320 va_end(ap);
3321 return ret;
3322}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003323EXPORT_SYMBOL_GPL(__ftrace_printk);
3324
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003325static int trace_panic_handler(struct notifier_block *this,
3326 unsigned long event, void *unused)
3327{
Steven Rostedt944ac422008-10-23 19:26:08 -04003328 if (ftrace_dump_on_oops)
3329 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003330 return NOTIFY_OK;
3331}
3332
3333static struct notifier_block trace_panic_notifier = {
3334 .notifier_call = trace_panic_handler,
3335 .next = NULL,
3336 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3337};
3338
3339static int trace_die_handler(struct notifier_block *self,
3340 unsigned long val,
3341 void *data)
3342{
3343 switch (val) {
3344 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003345 if (ftrace_dump_on_oops)
3346 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003347 break;
3348 default:
3349 break;
3350 }
3351 return NOTIFY_OK;
3352}
3353
3354static struct notifier_block trace_die_notifier = {
3355 .notifier_call = trace_die_handler,
3356 .priority = 200
3357};
3358
3359/*
3360 * printk is set to max of 1024, we really don't need it that big.
3361 * Nothing should be printing 1000 characters anyway.
3362 */
3363#define TRACE_MAX_PRINT 1000
3364
3365/*
3366 * Define here KERN_TRACE so that we have one place to modify
3367 * it if we decide to change what log level the ftrace dump
3368 * should be at.
3369 */
3370#define KERN_TRACE KERN_INFO
3371
3372static void
3373trace_printk_seq(struct trace_seq *s)
3374{
3375 /* Probably should print a warning here. */
3376 if (s->len >= 1000)
3377 s->len = 1000;
3378
3379 /* should be zero ended, but we are paranoid. */
3380 s->buffer[s->len] = 0;
3381
3382 printk(KERN_TRACE "%s", s->buffer);
3383
3384 trace_seq_reset(s);
3385}
3386
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003387void ftrace_dump(void)
3388{
3389 static DEFINE_SPINLOCK(ftrace_dump_lock);
3390 /* use static because iter can be a bit big for the stack */
3391 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003392 static cpumask_t mask;
3393 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003394 unsigned long flags;
3395 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003396
3397 /* only one dump */
3398 spin_lock_irqsave(&ftrace_dump_lock, flags);
3399 if (dump_ran)
3400 goto out;
3401
3402 dump_ran = 1;
3403
3404 /* No turning back! */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003405 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003406
Steven Rostedtd7690412008-10-01 00:29:53 -04003407 for_each_tracing_cpu(cpu) {
3408 atomic_inc(&global_trace.data[cpu]->disabled);
3409 }
3410
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003411 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3412
3413 iter.tr = &global_trace;
3414 iter.trace = current_trace;
3415
3416 /*
3417 * We need to stop all tracing on all CPUS to read the
3418 * the next buffer. This is a bit expensive, but is
3419 * not done often. We fill all what we can read,
3420 * and then release the locks again.
3421 */
3422
3423 cpus_clear(mask);
3424
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003425 while (!trace_empty(&iter)) {
3426
3427 if (!cnt)
3428 printk(KERN_TRACE "---------------------------------\n");
3429
3430 cnt++;
3431
3432 /* reset all but tr, trace, and overruns */
3433 memset(&iter.seq, 0,
3434 sizeof(struct trace_iterator) -
3435 offsetof(struct trace_iterator, seq));
3436 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3437 iter.pos = -1;
3438
3439 if (find_next_entry_inc(&iter) != NULL) {
3440 print_trace_line(&iter);
3441 trace_consume(&iter);
3442 }
3443
3444 trace_printk_seq(&iter.seq);
3445 }
3446
3447 if (!cnt)
3448 printk(KERN_TRACE " (ftrace buffer empty)\n");
3449 else
3450 printk(KERN_TRACE "---------------------------------\n");
3451
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003452 out:
3453 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3454}
3455
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003456__init static int tracer_alloc_buffers(void)
3457{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003458 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003459 int i;
3460
Steven Rostedtab464282008-05-12 21:21:00 +02003461 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedtab464282008-05-12 21:21:00 +02003462 tracing_buffer_mask = cpu_possible_map;
3463
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003464 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3465 TRACE_BUFFER_FLAGS);
3466 if (!global_trace.buffer) {
3467 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3468 WARN_ON(1);
3469 return 0;
3470 }
3471 global_trace.entries = ring_buffer_size(global_trace.buffer);
3472
3473#ifdef CONFIG_TRACER_MAX_TRACE
3474 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3475 TRACE_BUFFER_FLAGS);
3476 if (!max_tr.buffer) {
3477 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3478 WARN_ON(1);
3479 ring_buffer_free(global_trace.buffer);
3480 return 0;
3481 }
3482 max_tr.entries = ring_buffer_size(max_tr.buffer);
3483 WARN_ON(max_tr.entries != global_trace.entries);
3484#endif
3485
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003486 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003487 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003488 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003489 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003490 }
3491
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003492 trace_init_cmdlines();
3493
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003494 register_tracer(&nop_trace);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003495#ifdef CONFIG_BOOT_TRACER
3496 register_tracer(&boot_tracer);
3497 current_trace = &boot_tracer;
3498 current_trace->init(&global_trace);
3499#else
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003500 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003501#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003502
Steven Rostedt60a11772008-05-12 21:20:44 +02003503 /* All seems OK, enable tracing */
3504 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003505
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003506 atomic_notifier_chain_register(&panic_notifier_list,
3507 &trace_panic_notifier);
3508
3509 register_die_notifier(&trace_die_notifier);
3510
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003511 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003512}
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003513early_initcall(tracer_alloc_buffers);
3514fs_initcall(tracer_init_debugfs);