blob: 40edef4255c5956a7e9737f9a5a570dfdaf26f47 [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"
Steven Rostedtf0868d12008-12-23 23:24:12 -050040#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020041
Steven Rostedt3928a8a2008-09-29 23:02:41 -040042#define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43
Steven Rostedt745b1622009-01-15 23:40:11 -050044unsigned long __read_mostly tracing_max_latency;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020045unsigned long __read_mostly tracing_thresh;
46
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010047/*
48 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010049 * A selftest will lurk into the ring-buffer to count the
50 * entries inserted during the selftest although some concurrent
51 * insertions into the ring-buffer such as ftrace_printk could occurred
52 * at the same time, giving false positive or negative results.
53 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010054static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010055
Steven Rostedtb2821ae2009-02-02 21:38:32 -050056/*
57 * If a tracer is running, we do not want to run SELFTEST.
58 */
59static bool __read_mostly tracing_selftest_disabled;
60
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010061/* For tracers that don't implement custom flags */
62static struct tracer_opt dummy_tracer_opt[] = {
63 { }
64};
65
66static struct tracer_flags dummy_tracer_flags = {
67 .val = 0,
68 .opts = dummy_tracer_opt
69};
70
71static int dummy_set_flag(u32 old_flags, u32 bit, int set)
72{
73 return 0;
74}
Steven Rostedt0f048702008-11-05 16:05:44 -050075
76/*
77 * Kill all tracing for good (never come back).
78 * It is initialized to 1 but will turn to zero if the initialization
79 * of the tracer is successful. But that is the only place that sets
80 * this back to zero.
81 */
82int tracing_disabled = 1;
83
Steven Rostedtd7690412008-10-01 00:29:53 -040084static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
85
86static inline void ftrace_disable_cpu(void)
87{
88 preempt_disable();
89 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
90}
91
92static inline void ftrace_enable_cpu(void)
93{
94 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
95 preempt_enable();
96}
97
Rusty Russell9e01c1b2009-01-01 10:12:22 +103098static cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +020099
100#define for_each_tracing_cpu(cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030101 for_each_cpu(cpu, tracing_buffer_mask)
Steven Rostedtab464282008-05-12 21:21:00 +0200102
Steven Rostedt944ac422008-10-23 19:26:08 -0400103/*
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
105 *
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
110 * serial console.
111 *
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
114 * /proc/sys/kernel/ftrace_dump_on_oops to true.
115 */
116int ftrace_dump_on_oops;
117
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500118static int tracing_set_tracer(const char *buf);
119
120#define BOOTUP_TRACER_SIZE 100
121static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
122static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100123
124static int __init set_ftrace(char *str)
125{
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500126 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
127 default_bootup_tracer = bootup_tracer_buf;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100128 return 1;
129}
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500130__setup("ftrace=", set_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100131
Steven Rostedt944ac422008-10-23 19:26:08 -0400132static int __init set_ftrace_dump_on_oops(char *str)
133{
134 ftrace_dump_on_oops = 1;
135 return 1;
136}
137__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200138
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200139long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200140ns2usecs(cycle_t nsec)
141{
142 nsec += 500;
143 do_div(nsec, 1000);
144 return nsec;
145}
146
Ingo Molnare309b412008-05-12 21:20:51 +0200147cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200148{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400149 u64 ts = ring_buffer_time_stamp(cpu);
150 ring_buffer_normalize_time_stamp(cpu, &ts);
151 return ts;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200152}
153
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200154/*
155 * The global_trace is the descriptor that holds the tracing
156 * buffers for the live tracing. For each CPU, it contains
157 * a link list of pages that will store trace entries. The
158 * page descriptor of the pages in the memory is used to hold
159 * the link list by linking the lru item in the page descriptor
160 * to each of the pages in the buffer per CPU.
161 *
162 * For each active CPU there is a data field that holds the
163 * pages for the buffer for that CPU. Each CPU has the same number
164 * of pages allocated for its buffer.
165 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200166static struct trace_array global_trace;
167
168static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
169
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200170/*
171 * The max_tr is used to snapshot the global_trace when a maximum
172 * latency is reached. Some tracers will use this to store a maximum
173 * trace while it continues examining live traces.
174 *
175 * The buffers for the max_tr are set up the same as the global_trace.
176 * When a snapshot is taken, the link list of the max_tr is swapped
177 * with the link list of the global_trace and the buffers are reset for
178 * the global_trace so the tracing can continue.
179 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200180static struct trace_array max_tr;
181
182static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
183
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200184/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200185static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200186
Steven Rostedt90369902008-11-05 16:05:44 -0500187/**
188 * tracing_is_enabled - return tracer_enabled status
189 *
190 * This function is used by other tracers to know the status
191 * of the tracer_enabled flag. Tracers may use this function
192 * to know if it should enable their features when starting
193 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
194 */
195int tracing_is_enabled(void)
196{
197 return tracer_enabled;
198}
199
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200200/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400201 * trace_buf_size is the size in bytes that is allocated
202 * for a buffer. Note, the number of bytes is always rounded
203 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400204 *
205 * This number is purposely set to a low number of 16384.
206 * If the dump on oops happens, it will be much appreciated
207 * to not have to wait for all that output. Anyway this can be
208 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200209 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400210#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400211
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400212static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200213
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200214/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200215static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200216
217/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200218static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200219
220/*
221 * max_tracer_type_len is used to simplify the allocating of
222 * buffers to read userspace tracer names. We keep track of
223 * the longest tracer name registered.
224 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200225static int max_tracer_type_len;
226
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200227/*
228 * trace_types_lock is used to protect the trace_types list.
229 * This lock is also used to keep user access serialized.
230 * Accesses from userspace will grab this lock while userspace
231 * activities happen inside the kernel.
232 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200233static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200234
235/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200236static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
237
Steven Rostedtee6bce52008-11-12 17:52:37 -0500238/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500239unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
240 TRACE_ITER_ANNOTATE;
Ingo Molnar4e655512008-05-12 21:20:52 +0200241
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200242/**
243 * trace_wake_up - wake up tasks waiting for trace input
244 *
245 * Simply wakes up any task that is blocked on the trace_wait
246 * queue. These is used with trace_poll for tasks polling the trace.
247 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200248void trace_wake_up(void)
249{
Ingo Molnar017730c2008-05-12 21:20:52 +0200250 /*
251 * The runqueue_is_locked() can fail, but this is the best we
252 * have for now:
253 */
254 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200255 wake_up(&trace_wait);
256}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200257
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400258static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200259{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400260 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200261 int ret;
262
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200263 if (!str)
264 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400265 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200266 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400267 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200268 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400269 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200270 return 1;
271}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400272__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200273
Steven Rostedt57f50be2008-05-12 21:20:44 +0200274unsigned long nsecs_to_usecs(unsigned long nsecs)
275{
276 return nsecs / 1000;
277}
278
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200279/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200280static const char *trace_options[] = {
281 "print-parent",
282 "sym-offset",
283 "sym-addr",
284 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200285 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200286 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200287 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200288 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200289 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200290 "sched-tree",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200291 "ftrace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500292 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500293 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500294 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200295 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200296 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100297 "printk-msg-only",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200298 NULL
299};
300
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200301/*
302 * ftrace_max_lock is used to protect the swapping of buffers
303 * when taking a max snapshot. The buffers themselves are
304 * protected by per_cpu spinlocks. But the action of the swap
305 * needs its own lock.
306 *
307 * This is defined as a raw_spinlock_t in order to help
308 * with performance when lockdep debugging is enabled.
309 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200310static raw_spinlock_t ftrace_max_lock =
311 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200312
313/*
314 * Copy the new maximum trace into the separate maximum-trace
315 * structure. (this way the maximum trace is permanently saved,
316 * for later retrieval via /debugfs/tracing/latency_trace)
317 */
Ingo Molnare309b412008-05-12 21:20:51 +0200318static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200319__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
320{
321 struct trace_array_cpu *data = tr->data[cpu];
322
323 max_tr.cpu = cpu;
324 max_tr.time_start = data->preempt_timestamp;
325
326 data = max_tr.data[cpu];
327 data->saved_latency = tracing_max_latency;
328
329 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
330 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100331 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200332 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
333 data->policy = tsk->policy;
334 data->rt_priority = tsk->rt_priority;
335
336 /* record this tasks comm */
337 tracing_record_cmdline(current);
338}
339
Ingo Molnare309b412008-05-12 21:20:51 +0200340static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200341trace_seq_reset(struct trace_seq *s)
342{
343 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200344 s->readpos = 0;
345}
346
347ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
348{
349 int len;
350 int ret;
351
352 if (s->len <= s->readpos)
353 return -EBUSY;
354
355 len = s->len - s->readpos;
356 if (cnt > len)
357 cnt = len;
358 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
359 if (ret)
360 return -EFAULT;
361
362 s->readpos += len;
363 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200364}
365
Ingo Molnare309b412008-05-12 21:20:51 +0200366static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200367trace_print_seq(struct seq_file *m, struct trace_seq *s)
368{
369 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
370
371 s->buffer[len] = 0;
372 seq_puts(m, s->buffer);
373
374 trace_seq_reset(s);
375}
376
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200377/**
378 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
379 * @tr: tracer
380 * @tsk: the task with the latency
381 * @cpu: The cpu that initiated the trace.
382 *
383 * Flip the buffers between the @tr and the max_tr and record information
384 * about which task was the cause of this latency.
385 */
Ingo Molnare309b412008-05-12 21:20:51 +0200386void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200387update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
388{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400389 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200390
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200391 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200392 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400393
394 tr->buffer = max_tr.buffer;
395 max_tr.buffer = buf;
396
Steven Rostedtd7690412008-10-01 00:29:53 -0400397 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400398 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400399 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200400
401 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200402 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200403}
404
405/**
406 * update_max_tr_single - only copy one trace over, and reset the rest
407 * @tr - tracer
408 * @tsk - task with the latency
409 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200410 *
411 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200412 */
Ingo Molnare309b412008-05-12 21:20:51 +0200413void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200414update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
415{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400416 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200417
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200418 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200419 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200420
Steven Rostedtd7690412008-10-01 00:29:53 -0400421 ftrace_disable_cpu();
422
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400423 ring_buffer_reset(max_tr.buffer);
424 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
425
Steven Rostedtd7690412008-10-01 00:29:53 -0400426 ftrace_enable_cpu();
427
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500428 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200429
430 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200431 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200432}
433
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200434/**
435 * register_tracer - register a tracer with the ftrace system.
436 * @type - the plugin for the tracer
437 *
438 * Register a new plugin tracer.
439 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200440int register_tracer(struct tracer *type)
441{
442 struct tracer *t;
443 int len;
444 int ret = 0;
445
446 if (!type->name) {
447 pr_info("Tracer must have a name\n");
448 return -1;
449 }
450
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100451 /*
452 * When this gets called we hold the BKL which means that
453 * preemption is disabled. Various trace selftests however
454 * need to disable and enable preemption for successful tests.
455 * So we drop the BKL here and grab it after the tests again.
456 */
457 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200458 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100459
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100460 tracing_selftest_running = true;
461
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200462 for (t = trace_types; t; t = t->next) {
463 if (strcmp(type->name, t->name) == 0) {
464 /* already found */
465 pr_info("Trace %s already registered\n",
466 type->name);
467 ret = -1;
468 goto out;
469 }
470 }
471
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100472 if (!type->set_flag)
473 type->set_flag = &dummy_set_flag;
474 if (!type->flags)
475 type->flags = &dummy_tracer_flags;
476 else
477 if (!type->flags->opts)
478 type->flags->opts = dummy_tracer_opt;
479
Steven Rostedt60a11772008-05-12 21:20:44 +0200480#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500481 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200482 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200483 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200484 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100485
Steven Rostedt60a11772008-05-12 21:20:44 +0200486 /*
487 * Run a selftest on this tracer.
488 * Here we reset the trace buffer, and set the current
489 * tracer to be this tracer. The tracer can then run some
490 * internal tracing to verify that everything is in order.
491 * If we fail, we do not register this tracer.
492 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100493 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400494 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100495
Steven Rostedt60a11772008-05-12 21:20:44 +0200496 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200497 /* the test is responsible for initializing and enabling */
498 pr_info("Testing tracer %s: ", type->name);
499 ret = type->selftest(type, tr);
500 /* the test is responsible for resetting too */
501 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200502 if (ret) {
503 printk(KERN_CONT "FAILED!\n");
504 goto out;
505 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200506 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100507 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400508 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100509
Steven Rostedt60a11772008-05-12 21:20:44 +0200510 printk(KERN_CONT "PASSED\n");
511 }
512#endif
513
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200514 type->next = trace_types;
515 trace_types = type;
516 len = strlen(type->name);
517 if (len > max_tracer_type_len)
518 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200519
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200520 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100521 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200522 mutex_unlock(&trace_types_lock);
523
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500524 if (!ret && default_bootup_tracer) {
525 if (!strncmp(default_bootup_tracer, type->name,
526 BOOTUP_TRACER_SIZE)) {
527 printk(KERN_INFO "Starting tracer '%s'\n",
528 type->name);
529 /* Do we want this tracer to start on bootup? */
530 tracing_set_tracer(type->name);
531 default_bootup_tracer = NULL;
532 /* disable other selftests, since this will break it. */
533 tracing_selftest_disabled = 1;
534#ifdef CONFIG_FTRACE_STARTUP_TEST
535 printk(KERN_INFO "Disabling FTRACE selftests due"
536 " to running tracer '%s'\n", type->name);
537#endif
538 }
539 }
540
541 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200542 return ret;
543}
544
545void unregister_tracer(struct tracer *type)
546{
547 struct tracer **t;
548 int len;
549
550 mutex_lock(&trace_types_lock);
551 for (t = &trace_types; *t; t = &(*t)->next) {
552 if (*t == type)
553 goto found;
554 }
555 pr_info("Trace %s not registered\n", type->name);
556 goto out;
557
558 found:
559 *t = (*t)->next;
560 if (strlen(type->name) != max_tracer_type_len)
561 goto out;
562
563 max_tracer_type_len = 0;
564 for (t = &trace_types; *t; t = &(*t)->next) {
565 len = strlen((*t)->name);
566 if (len > max_tracer_type_len)
567 max_tracer_type_len = len;
568 }
569 out:
570 mutex_unlock(&trace_types_lock);
571}
572
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400573void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200574{
Steven Rostedtd7690412008-10-01 00:29:53 -0400575 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400576 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400577 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200578}
579
Pekka J Enberg213cc062008-12-19 12:08:39 +0200580void tracing_reset_online_cpus(struct trace_array *tr)
581{
582 int cpu;
583
584 tr->time_start = ftrace_now(tr->cpu);
585
586 for_each_online_cpu(cpu)
587 tracing_reset(tr, cpu);
588}
589
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200590#define SAVED_CMDLINES 128
591static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
592static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
593static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
594static int cmdline_idx;
595static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200596
Steven Rostedt25b0b442008-05-12 21:21:00 +0200597/* temporary disable recording */
598atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200599
600static void trace_init_cmdlines(void)
601{
602 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
603 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
604 cmdline_idx = 0;
605}
606
Steven Rostedt0f048702008-11-05 16:05:44 -0500607static int trace_stop_count;
608static DEFINE_SPINLOCK(tracing_start_lock);
609
610/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500611 * ftrace_off_permanent - disable all ftrace code permanently
612 *
613 * This should only be called when a serious anomally has
614 * been detected. This will turn off the function tracing,
615 * ring buffers, and other tracing utilites. It takes no
616 * locks and can be called from any context.
617 */
618void ftrace_off_permanent(void)
619{
620 tracing_disabled = 1;
621 ftrace_stop();
622 tracing_off_permanent();
623}
624
625/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500626 * tracing_start - quick start of the tracer
627 *
628 * If tracing is enabled but was stopped by tracing_stop,
629 * this will start the tracer back up.
630 */
631void tracing_start(void)
632{
633 struct ring_buffer *buffer;
634 unsigned long flags;
635
636 if (tracing_disabled)
637 return;
638
639 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500640 if (--trace_stop_count) {
641 if (trace_stop_count < 0) {
642 /* Someone screwed up their debugging */
643 WARN_ON_ONCE(1);
644 trace_stop_count = 0;
645 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500646 goto out;
647 }
648
649
650 buffer = global_trace.buffer;
651 if (buffer)
652 ring_buffer_record_enable(buffer);
653
654 buffer = max_tr.buffer;
655 if (buffer)
656 ring_buffer_record_enable(buffer);
657
658 ftrace_start();
659 out:
660 spin_unlock_irqrestore(&tracing_start_lock, flags);
661}
662
663/**
664 * tracing_stop - quick stop of the tracer
665 *
666 * Light weight way to stop tracing. Use in conjunction with
667 * tracing_start.
668 */
669void tracing_stop(void)
670{
671 struct ring_buffer *buffer;
672 unsigned long flags;
673
674 ftrace_stop();
675 spin_lock_irqsave(&tracing_start_lock, flags);
676 if (trace_stop_count++)
677 goto out;
678
679 buffer = global_trace.buffer;
680 if (buffer)
681 ring_buffer_record_disable(buffer);
682
683 buffer = max_tr.buffer;
684 if (buffer)
685 ring_buffer_record_disable(buffer);
686
687 out:
688 spin_unlock_irqrestore(&tracing_start_lock, flags);
689}
690
Ingo Molnare309b412008-05-12 21:20:51 +0200691void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200692
Ingo Molnare309b412008-05-12 21:20:51 +0200693static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200694{
695 unsigned map;
696 unsigned idx;
697
698 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
699 return;
700
701 /*
702 * It's not the end of the world if we don't get
703 * the lock, but we also don't want to spin
704 * nor do we want to disable interrupts,
705 * so if we miss here, then better luck next time.
706 */
707 if (!spin_trylock(&trace_cmdline_lock))
708 return;
709
710 idx = map_pid_to_cmdline[tsk->pid];
711 if (idx >= SAVED_CMDLINES) {
712 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
713
714 map = map_cmdline_to_pid[idx];
715 if (map <= PID_MAX_DEFAULT)
716 map_pid_to_cmdline[map] = (unsigned)-1;
717
718 map_pid_to_cmdline[tsk->pid] = idx;
719
720 cmdline_idx = idx;
721 }
722
723 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
724
725 spin_unlock(&trace_cmdline_lock);
726}
727
Steven Rostedt660c7f92008-11-26 00:16:26 -0500728char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200729{
730 char *cmdline = "<...>";
731 unsigned map;
732
733 if (!pid)
734 return "<idle>";
735
736 if (pid > PID_MAX_DEFAULT)
737 goto out;
738
739 map = map_pid_to_cmdline[pid];
740 if (map >= SAVED_CMDLINES)
741 goto out;
742
743 cmdline = saved_cmdlines[map];
744
745 out:
746 return cmdline;
747}
748
Ingo Molnare309b412008-05-12 21:20:51 +0200749void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200750{
751 if (atomic_read(&trace_record_cmdline_disabled))
752 return;
753
754 trace_save_cmdline(tsk);
755}
756
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300757void
Steven Rostedt38697052008-10-01 13:14:09 -0400758tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
759 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200760{
761 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200762
Steven Rostedt777e2082008-09-29 23:02:42 -0400763 entry->preempt_count = pc & 0xff;
764 entry->pid = (tsk) ? tsk->pid : 0;
Török Edwinb54d3de2008-11-22 13:28:48 +0200765 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400766 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400767#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400768 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400769#else
770 TRACE_FLAG_IRQS_NOSUPPORT |
771#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200772 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
773 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
774 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
775}
776
Ingo Molnare309b412008-05-12 21:20:51 +0200777void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200778trace_function(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400779 unsigned long ip, unsigned long parent_ip, unsigned long flags,
780 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400782 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400783 struct ftrace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200784 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200785
Steven Rostedtd7690412008-10-01 00:29:53 -0400786 /* If we are reading the ring buffer, don't trace */
787 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
788 return;
789
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400790 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
791 &irq_flags);
792 if (!event)
793 return;
794 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400795 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400796 entry->ent.type = TRACE_FN;
797 entry->ip = ip;
798 entry->parent_ip = parent_ip;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400799 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200800}
801
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100802#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100803static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100804 struct trace_array_cpu *data,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100805 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100806 unsigned long flags,
807 int pc)
808{
809 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100810 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100811 unsigned long irq_flags;
812
813 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
814 return;
815
816 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
817 &irq_flags);
818 if (!event)
819 return;
820 entry = ring_buffer_event_data(event);
821 tracing_generic_entry_update(&entry->ent, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100822 entry->ent.type = TRACE_GRAPH_ENT;
823 entry->graph_ent = *trace;
824 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
825}
826
827static void __trace_graph_return(struct trace_array *tr,
828 struct trace_array_cpu *data,
829 struct ftrace_graph_ret *trace,
830 unsigned long flags,
831 int pc)
832{
833 struct ring_buffer_event *event;
834 struct ftrace_graph_ret_entry *entry;
835 unsigned long irq_flags;
836
837 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
838 return;
839
840 event = ring_buffer_lock_reserve(global_trace.buffer, sizeof(*entry),
841 &irq_flags);
842 if (!event)
843 return;
844 entry = ring_buffer_event_data(event);
845 tracing_generic_entry_update(&entry->ent, flags, pc);
846 entry->ent.type = TRACE_GRAPH_RET;
847 entry->ret = *trace;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100848 ring_buffer_unlock_commit(global_trace.buffer, event, irq_flags);
849}
850#endif
851
Ingo Molnare309b412008-05-12 21:20:51 +0200852void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200853ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400854 unsigned long ip, unsigned long parent_ip, unsigned long flags,
855 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200856{
857 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt38697052008-10-01 13:14:09 -0400858 trace_function(tr, data, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200859}
860
Steven Rostedt53614992009-01-15 19:12:40 -0500861static void __ftrace_trace_stack(struct trace_array *tr,
862 struct trace_array_cpu *data,
863 unsigned long flags,
864 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200865{
Al Viroc2c80522008-10-31 19:50:41 +0000866#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400867 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400868 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200869 struct stack_trace trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400870 unsigned long irq_flags;
Ingo Molnar86387f72008-05-12 21:20:51 +0200871
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 Rostedt53614992009-01-15 19:12:40 -0500892static void ftrace_trace_stack(struct trace_array *tr,
893 struct trace_array_cpu *data,
894 unsigned long flags,
895 int skip, int pc)
896{
897 if (!(trace_flags & TRACE_ITER_STACKTRACE))
898 return;
899
900 __ftrace_trace_stack(tr, data, flags, skip, pc);
901}
902
Steven Rostedt38697052008-10-01 13:14:09 -0400903void __trace_stack(struct trace_array *tr,
904 struct trace_array_cpu *data,
905 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500906 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400907{
Steven Rostedt53614992009-01-15 19:12:40 -0500908 __ftrace_trace_stack(tr, data, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400909}
910
Török Edwin02b67512008-11-22 13:28:47 +0200911static void ftrace_trace_userstack(struct trace_array *tr,
912 struct trace_array_cpu *data,
913 unsigned long flags, int pc)
914{
Török Edwinc7425ac2008-11-28 11:17:56 +0200915#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +0200916 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +0200917 struct userstack_entry *entry;
918 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +0200919 unsigned long irq_flags;
920
921 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
922 return;
923
924 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
925 &irq_flags);
926 if (!event)
927 return;
928 entry = ring_buffer_event_data(event);
929 tracing_generic_entry_update(&entry->ent, flags, pc);
930 entry->ent.type = TRACE_USER_STACK;
931
932 memset(&entry->caller, 0, sizeof(entry->caller));
933
934 trace.nr_entries = 0;
935 trace.max_entries = FTRACE_STACK_ENTRIES;
936 trace.skip = 0;
937 trace.entries = entry->caller;
938
939 save_stack_trace_user(&trace);
940 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Török Edwinc7425ac2008-11-28 11:17:56 +0200941#endif
Török Edwin02b67512008-11-22 13:28:47 +0200942}
943
944void __trace_userstack(struct trace_array *tr,
945 struct trace_array_cpu *data,
946 unsigned long flags)
947{
948 ftrace_trace_userstack(tr, data, flags, preempt_count());
949}
950
Steven Rostedt38697052008-10-01 13:14:09 -0400951static void
952ftrace_trace_special(void *__tr, void *__data,
953 unsigned long arg1, unsigned long arg2, unsigned long arg3,
954 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +0200955{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400956 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +0200957 struct trace_array_cpu *data = __data;
958 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -0400959 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +0200960 unsigned long irq_flags;
961
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400962 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
963 &irq_flags);
964 if (!event)
965 return;
966 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -0400967 tracing_generic_entry_update(&entry->ent, 0, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -0400968 entry->ent.type = TRACE_SPECIAL;
969 entry->arg1 = arg1;
970 entry->arg2 = arg2;
971 entry->arg3 = arg3;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400972 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -0400973 ftrace_trace_stack(tr, data, irq_flags, 4, pc);
Török Edwin02b67512008-11-22 13:28:47 +0200974 ftrace_trace_userstack(tr, data, irq_flags, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +0200975
976 trace_wake_up();
977}
978
979void
Steven Rostedt38697052008-10-01 13:14:09 -0400980__trace_special(void *__tr, void *__data,
981 unsigned long arg1, unsigned long arg2, unsigned long arg3)
982{
983 ftrace_trace_special(__tr, __data, arg1, arg2, arg3, preempt_count());
984}
985
986void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200987tracing_sched_switch_trace(struct trace_array *tr,
988 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200989 struct task_struct *prev,
990 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -0400991 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200992{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400993 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400994 struct ctx_switch_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200995 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200996
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400997 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
998 &irq_flags);
999 if (!event)
1000 return;
1001 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -04001002 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -04001003 entry->ent.type = TRACE_CTX;
1004 entry->prev_pid = prev->pid;
1005 entry->prev_prio = prev->prio;
1006 entry->prev_state = prev->state;
1007 entry->next_pid = next->pid;
1008 entry->next_prio = next->prio;
1009 entry->next_state = next->state;
1010 entry->next_cpu = task_cpu(next);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001011 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -04001012 ftrace_trace_stack(tr, data, flags, 5, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001013 ftrace_trace_userstack(tr, data, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001014}
1015
Ingo Molnar57422792008-05-12 21:20:51 +02001016void
1017tracing_sched_wakeup_trace(struct trace_array *tr,
1018 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +02001019 struct task_struct *wakee,
1020 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001021 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001022{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001023 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001024 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001025 unsigned long irq_flags;
1026
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001027 event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
1028 &irq_flags);
1029 if (!event)
1030 return;
1031 entry = ring_buffer_event_data(event);
Steven Rostedt38697052008-10-01 13:14:09 -04001032 tracing_generic_entry_update(&entry->ent, flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -04001033 entry->ent.type = TRACE_WAKE;
1034 entry->prev_pid = curr->pid;
1035 entry->prev_prio = curr->prio;
1036 entry->prev_state = curr->state;
1037 entry->next_pid = wakee->pid;
1038 entry->next_prio = wakee->prio;
1039 entry->next_state = wakee->state;
1040 entry->next_cpu = task_cpu(wakee);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001041 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedt38697052008-10-01 13:14:09 -04001042 ftrace_trace_stack(tr, data, flags, 6, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001043 ftrace_trace_userstack(tr, data, flags, pc);
Ingo Molnar017730c2008-05-12 21:20:52 +02001044
1045 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +02001046}
1047
Steven Rostedt4902f882008-05-22 00:22:18 -04001048void
1049ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1050{
1051 struct trace_array *tr = &global_trace;
1052 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001053 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001054 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001055 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001056
Steven Rostedtc76f0692008-11-07 22:36:02 -05001057 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001058 return;
1059
Steven Rostedt38697052008-10-01 13:14:09 -04001060 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001061 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001062 cpu = raw_smp_processor_id();
1063 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001064
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001065 if (likely(atomic_inc_return(&data->disabled) == 1))
Steven Rostedt38697052008-10-01 13:14:09 -04001066 ftrace_trace_special(tr, data, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001067
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001068 atomic_dec(&data->disabled);
1069 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001070}
1071
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001072#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001073int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001074{
1075 struct trace_array *tr = &global_trace;
1076 struct trace_array_cpu *data;
1077 unsigned long flags;
1078 long disabled;
1079 int cpu;
1080 int pc;
1081
Steven Rostedt804a6852008-12-03 15:36:59 -05001082 if (!ftrace_trace_task(current))
1083 return 0;
1084
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001085 if (!ftrace_graph_addr(trace->func))
1086 return 0;
1087
Steven Rostedta5e25882008-12-02 15:34:05 -05001088 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001089 cpu = raw_smp_processor_id();
1090 data = tr->data[cpu];
1091 disabled = atomic_inc_return(&data->disabled);
1092 if (likely(disabled == 1)) {
1093 pc = preempt_count();
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001094 __trace_graph_entry(tr, data, trace, flags, pc);
1095 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001096 /* Only do the atomic if it is not already set */
1097 if (!test_tsk_trace_graph(current))
1098 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001099 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001100 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001101
1102 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001103}
1104
1105void trace_graph_return(struct ftrace_graph_ret *trace)
1106{
1107 struct trace_array *tr = &global_trace;
1108 struct trace_array_cpu *data;
1109 unsigned long flags;
1110 long disabled;
1111 int cpu;
1112 int pc;
1113
Steven Rostedta5e25882008-12-02 15:34:05 -05001114 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001115 cpu = raw_smp_processor_id();
1116 data = tr->data[cpu];
1117 disabled = atomic_inc_return(&data->disabled);
1118 if (likely(disabled == 1)) {
1119 pc = preempt_count();
1120 __trace_graph_return(tr, data, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001121 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001122 if (!trace->depth)
1123 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001124 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001125 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001126}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001127#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001128
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001129enum trace_file_type {
1130 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001131 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001132};
1133
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001134static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001135{
Steven Rostedtd7690412008-10-01 00:29:53 -04001136 /* Don't allow ftrace to trace into the ring buffers */
1137 ftrace_disable_cpu();
1138
Steven Rostedt5a90f572008-09-03 17:42:51 -04001139 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001140 if (iter->buffer_iter[iter->cpu])
1141 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1142
1143 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001144}
1145
Ingo Molnare309b412008-05-12 21:20:51 +02001146static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001147peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001148{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001149 struct ring_buffer_event *event;
1150 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001151
Steven Rostedtd7690412008-10-01 00:29:53 -04001152 /* Don't allow ftrace to trace into the ring buffers */
1153 ftrace_disable_cpu();
1154
1155 if (buf_iter)
1156 event = ring_buffer_iter_peek(buf_iter, ts);
1157 else
1158 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1159
1160 ftrace_enable_cpu();
1161
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001162 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001163}
Steven Rostedtd7690412008-10-01 00:29:53 -04001164
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001165static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001166__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001168 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001169 struct trace_entry *ent, *next = NULL;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001170 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001171 int next_cpu = -1;
1172 int cpu;
1173
Steven Rostedtab464282008-05-12 21:21:00 +02001174 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001175
1176 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001178
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001179 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001180
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001181 /*
1182 * Pick the entry with the smallest timestamp:
1183 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001184 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185 next = ent;
1186 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001187 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001188 }
1189 }
1190
1191 if (ent_cpu)
1192 *ent_cpu = next_cpu;
1193
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001194 if (ent_ts)
1195 *ent_ts = next_ts;
1196
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197 return next;
1198}
1199
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001200/* Find the next real entry, without updating the iterator itself */
1201static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001202find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001203{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001204 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001205}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001206
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001207/* Find the next real entry, and increment the iterator to the next entry */
1208static void *find_next_entry_inc(struct trace_iterator *iter)
1209{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001210 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001211
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001212 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001213 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001214
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001215 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001216}
1217
Ingo Molnare309b412008-05-12 21:20:51 +02001218static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001219{
Steven Rostedtd7690412008-10-01 00:29:53 -04001220 /* Don't allow ftrace to trace into the ring buffers */
1221 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001222 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001223 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001224}
1225
Ingo Molnare309b412008-05-12 21:20:51 +02001226static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001227{
1228 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001229 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001230 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001231
1232 (*pos)++;
1233
1234 /* can't go backwards */
1235 if (iter->idx > i)
1236 return NULL;
1237
1238 if (iter->idx < 0)
1239 ent = find_next_entry_inc(iter);
1240 else
1241 ent = iter;
1242
1243 while (ent && iter->idx < i)
1244 ent = find_next_entry_inc(iter);
1245
1246 iter->pos = *pos;
1247
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001248 return ent;
1249}
1250
1251static void *s_start(struct seq_file *m, loff_t *pos)
1252{
1253 struct trace_iterator *iter = m->private;
1254 void *p = NULL;
1255 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001256 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001257
1258 mutex_lock(&trace_types_lock);
1259
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001260 if (!current_trace || current_trace != iter->trace) {
1261 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001262 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001263 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001264
1265 atomic_inc(&trace_record_cmdline_disabled);
1266
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001267 if (*pos != iter->pos) {
1268 iter->ent = NULL;
1269 iter->cpu = 0;
1270 iter->idx = -1;
1271
Steven Rostedtd7690412008-10-01 00:29:53 -04001272 ftrace_disable_cpu();
1273
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001274 for_each_tracing_cpu(cpu) {
1275 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001276 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001277
Steven Rostedtd7690412008-10-01 00:29:53 -04001278 ftrace_enable_cpu();
1279
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001280 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1281 ;
1282
1283 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001284 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001285 p = s_next(m, p, &l);
1286 }
1287
1288 return p;
1289}
1290
1291static void s_stop(struct seq_file *m, void *p)
1292{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001293 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001294 mutex_unlock(&trace_types_lock);
1295}
1296
Ingo Molnare309b412008-05-12 21:20:51 +02001297static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001298{
Michael Ellermana6168352008-08-20 16:36:11 -07001299 seq_puts(m, "# _------=> CPU# \n");
1300 seq_puts(m, "# / _-----=> irqs-off \n");
1301 seq_puts(m, "# | / _----=> need-resched \n");
1302 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1303 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1304 seq_puts(m, "# |||| / \n");
1305 seq_puts(m, "# ||||| delay \n");
1306 seq_puts(m, "# cmd pid ||||| time | caller \n");
1307 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308}
1309
Ingo Molnare309b412008-05-12 21:20:51 +02001310static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001311{
Michael Ellermana6168352008-08-20 16:36:11 -07001312 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1313 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001314}
1315
1316
Ingo Molnare309b412008-05-12 21:20:51 +02001317static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1319{
1320 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1321 struct trace_array *tr = iter->tr;
1322 struct trace_array_cpu *data = tr->data[tr->cpu];
1323 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001324 unsigned long total;
1325 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001326 const char *name = "preemption";
1327
1328 if (type)
1329 name = type->name;
1330
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001331 entries = ring_buffer_entries(iter->tr->buffer);
1332 total = entries +
1333 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001334
1335 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1336 name, UTS_RELEASE);
1337 seq_puts(m, "-----------------------------------"
1338 "---------------------------------\n");
1339 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1340 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001341 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001342 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001343 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001344 tr->cpu,
1345#if defined(CONFIG_PREEMPT_NONE)
1346 "server",
1347#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1348 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001349#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001350 "preempt",
1351#else
1352 "unknown",
1353#endif
1354 /* These are reserved for later use */
1355 0, 0, 0, 0);
1356#ifdef CONFIG_SMP
1357 seq_printf(m, " #P:%d)\n", num_online_cpus());
1358#else
1359 seq_puts(m, ")\n");
1360#endif
1361 seq_puts(m, " -----------------\n");
1362 seq_printf(m, " | task: %.16s-%d "
1363 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1364 data->comm, data->pid, data->uid, data->nice,
1365 data->policy, data->rt_priority);
1366 seq_puts(m, " -----------------\n");
1367
1368 if (data->critical_start) {
1369 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001370 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1371 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001372 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001373 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1374 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001375 seq_puts(m, "\n");
1376 }
1377
1378 seq_puts(m, "\n");
1379}
1380
Ingo Molnare309b412008-05-12 21:20:51 +02001381static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001382lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001383{
1384 int hardirq, softirq;
1385 char *comm;
1386
Steven Rostedt777e2082008-09-29 23:02:42 -04001387 comm = trace_find_cmdline(entry->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001388
Steven Rostedt777e2082008-09-29 23:02:42 -04001389 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001390 trace_seq_printf(s, "%3d", cpu);
Steven Rostedt214023c2008-05-12 21:20:46 +02001391 trace_seq_printf(s, "%c%c",
Steven Rostedt92444892008-10-24 09:42:59 -04001392 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
1393 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
Steven Rostedt777e2082008-09-29 23:02:42 -04001394 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001395
Steven Rostedt777e2082008-09-29 23:02:42 -04001396 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1397 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001398 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001399 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001400 } else {
1401 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001402 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001403 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001404 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001405 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001406 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001407 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001408 }
1409 }
1410
Steven Rostedt777e2082008-09-29 23:02:42 -04001411 if (entry->preempt_count)
1412 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001413 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001414 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001415}
1416
1417unsigned long preempt_mark_thresh = 100;
1418
Ingo Molnare309b412008-05-12 21:20:51 +02001419static void
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001420lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001421 unsigned long rel_usecs)
1422{
Steven Rostedt214023c2008-05-12 21:20:46 +02001423 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001424 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001425 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001426 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001427 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001428 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001429 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001430}
1431
Steven Rostedta3097202008-11-07 22:36:02 -05001432static void test_cpu_buff_start(struct trace_iterator *iter)
1433{
1434 struct trace_seq *s = &iter->seq;
1435
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001436 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1437 return;
1438
1439 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1440 return;
1441
Rusty Russell44623442009-01-01 10:12:23 +10301442 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001443 return;
1444
Rusty Russell44623442009-01-01 10:12:23 +10301445 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001446 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1447}
1448
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001449static enum print_line_t
Steven Rostedt214023c2008-05-12 21:20:46 +02001450print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001451{
Steven Rostedt214023c2008-05-12 21:20:46 +02001452 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001453 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001454 struct trace_entry *next_entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001455 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001456 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1457 struct trace_entry *entry = iter->ent;
1458 unsigned long abs_usecs;
1459 unsigned long rel_usecs;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001460 u64 next_ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001461 char *comm;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001462 int ret;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001463
Steven Rostedta3097202008-11-07 22:36:02 -05001464 test_cpu_buff_start(iter);
1465
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001466 next_entry = find_next_entry(iter, NULL, &next_ts);
1467 if (!next_entry)
1468 next_ts = iter->ts;
1469 rel_usecs = ns2usecs(next_ts - iter->ts);
1470 abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001471
1472 if (verbose) {
Steven Rostedt777e2082008-09-29 23:02:42 -04001473 comm = trace_find_cmdline(entry->pid);
Michael Ellermana6168352008-08-20 16:36:11 -07001474 trace_seq_printf(s, "%16s %5d %3d %d %08x %08x [%08lx]"
Steven Rostedt214023c2008-05-12 21:20:46 +02001475 " %ld.%03ldms (+%ld.%03ldms): ",
1476 comm,
Steven Rostedt777e2082008-09-29 23:02:42 -04001477 entry->pid, cpu, entry->flags,
1478 entry->preempt_count, trace_idx,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001479 ns2usecs(iter->ts),
Steven Rostedt214023c2008-05-12 21:20:46 +02001480 abs_usecs/1000,
1481 abs_usecs % 1000, rel_usecs/1000,
1482 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001483 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001484 lat_print_generic(s, entry, cpu);
1485 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001486 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001487
Steven Rostedtf633cef2008-12-23 23:24:13 -05001488 event = ftrace_find_event(entry->type);
1489 if (event && event->latency_trace) {
1490 ret = event->latency_trace(s, entry, sym_flags);
1491 if (ret)
1492 return ret;
1493 return TRACE_TYPE_HANDLED;
Steven Rostedt777e2082008-09-29 23:02:42 -04001494 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001495
Steven Rostedtf633cef2008-12-23 23:24:13 -05001496 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001497 return TRACE_TYPE_HANDLED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001498}
1499
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001500static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001501{
Steven Rostedt214023c2008-05-12 21:20:46 +02001502 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001503 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001504 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001505 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001506 unsigned long usec_rem;
1507 unsigned long long t;
1508 unsigned long secs;
1509 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001510 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001511
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001512 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001513
Steven Rostedta3097202008-11-07 22:36:02 -05001514 test_cpu_buff_start(iter);
1515
Steven Rostedt777e2082008-09-29 23:02:42 -04001516 comm = trace_find_cmdline(iter->ent->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001517
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001518 t = ns2usecs(iter->ts);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001519 usec_rem = do_div(t, 1000000ULL);
1520 secs = (unsigned long)t;
1521
Steven Rostedt777e2082008-09-29 23:02:42 -04001522 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001523 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001524 return TRACE_TYPE_PARTIAL_LINE;
Michael Ellermana6168352008-08-20 16:36:11 -07001525 ret = trace_seq_printf(s, "[%03d] ", iter->cpu);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001526 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001527 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001528 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1529 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001530 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001531
Steven Rostedtf633cef2008-12-23 23:24:13 -05001532 event = ftrace_find_event(entry->type);
1533 if (event && event->trace) {
1534 ret = event->trace(s, entry, sym_flags);
1535 if (ret)
1536 return ret;
1537 return TRACE_TYPE_HANDLED;
Steven Rostedt777e2082008-09-29 23:02:42 -04001538 }
Steven Rostedtf633cef2008-12-23 23:24:13 -05001539 ret = trace_seq_printf(s, "Unknown type %d\n", entry->type);
1540 if (!ret)
1541 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedt7104f302008-10-01 10:52:51 -04001542
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001543 return TRACE_TYPE_HANDLED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001544}
1545
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001546static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001547{
1548 struct trace_seq *s = &iter->seq;
1549 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001550 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001551 int ret;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001552
1553 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001554
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001555 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt777e2082008-09-29 23:02:42 -04001556 entry->pid, iter->cpu, iter->ts);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001557 if (!ret)
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001558 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001559
Steven Rostedtf633cef2008-12-23 23:24:13 -05001560 event = ftrace_find_event(entry->type);
1561 if (event && event->raw) {
1562 ret = event->raw(s, entry, 0);
1563 if (ret)
1564 return ret;
1565 return TRACE_TYPE_HANDLED;
Steven Rostedt777e2082008-09-29 23:02:42 -04001566 }
Steven Rostedtf633cef2008-12-23 23:24:13 -05001567 ret = trace_seq_printf(s, "%d ?\n", entry->type);
1568 if (!ret)
1569 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedt7104f302008-10-01 10:52:51 -04001570
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001571 return TRACE_TYPE_HANDLED;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001572}
1573
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001574static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001575{
1576 struct trace_seq *s = &iter->seq;
1577 unsigned char newline = '\n';
1578 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001579 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001580
1581 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001582
Steven Rostedt777e2082008-09-29 23:02:42 -04001583 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001584 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001585 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001586
Steven Rostedtf633cef2008-12-23 23:24:13 -05001587 event = ftrace_find_event(entry->type);
1588 if (event && event->hex)
1589 event->hex(s, entry, 0);
Steven Rostedt7104f302008-10-01 10:52:51 -04001590
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001591 SEQ_PUT_FIELD_RET(s, newline);
1592
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001593 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001594}
1595
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001596static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1597{
1598 struct trace_seq *s = &iter->seq;
1599 struct trace_entry *entry = iter->ent;
1600 struct print_entry *field;
1601 int ret;
1602
1603 trace_assign_type(field, entry);
1604
1605 ret = trace_seq_printf(s, field->buf);
1606 if (!ret)
1607 return TRACE_TYPE_PARTIAL_LINE;
1608
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001609 return TRACE_TYPE_HANDLED;
1610}
1611
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001612static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001613{
1614 struct trace_seq *s = &iter->seq;
1615 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001616 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001617
1618 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001619
Steven Rostedt777e2082008-09-29 23:02:42 -04001620 SEQ_PUT_FIELD_RET(s, entry->pid);
Eric Anholt072ba492008-10-26 15:26:57 -07001621 SEQ_PUT_FIELD_RET(s, entry->cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001622 SEQ_PUT_FIELD_RET(s, iter->ts);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001623
Steven Rostedtf633cef2008-12-23 23:24:13 -05001624 event = ftrace_find_event(entry->type);
1625 if (event && event->binary)
1626 event->binary(s, entry, 0);
Steven Rostedt7104f302008-10-01 10:52:51 -04001627
Steven Rostedtf633cef2008-12-23 23:24:13 -05001628 return TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001629}
1630
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001631static int trace_empty(struct trace_iterator *iter)
1632{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001633 int cpu;
1634
Steven Rostedtab464282008-05-12 21:21:00 +02001635 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001636 if (iter->buffer_iter[cpu]) {
1637 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1638 return 0;
1639 } else {
1640 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1641 return 0;
1642 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001643 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001644
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001645 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001646}
1647
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001648static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001649{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001650 enum print_line_t ret;
1651
1652 if (iter->trace && iter->trace->print_line) {
1653 ret = iter->trace->print_line(iter);
1654 if (ret != TRACE_TYPE_UNHANDLED)
1655 return ret;
1656 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001657
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001658 if (iter->ent->type == TRACE_PRINT &&
1659 trace_flags & TRACE_ITER_PRINTK &&
1660 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1661 return print_printk_msg_only(iter);
1662
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001663 if (trace_flags & TRACE_ITER_BIN)
1664 return print_bin_fmt(iter);
1665
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001666 if (trace_flags & TRACE_ITER_HEX)
1667 return print_hex_fmt(iter);
1668
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001669 if (trace_flags & TRACE_ITER_RAW)
1670 return print_raw_fmt(iter);
1671
1672 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1673 return print_lat_fmt(iter, iter->idx, iter->cpu);
1674
1675 return print_trace_fmt(iter);
1676}
1677
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001678static int s_show(struct seq_file *m, void *v)
1679{
1680 struct trace_iterator *iter = v;
1681
1682 if (iter->ent == NULL) {
1683 if (iter->tr) {
1684 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1685 seq_puts(m, "#\n");
1686 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001687 if (iter->trace && iter->trace->print_header)
1688 iter->trace->print_header(m);
1689 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001690 /* print nothing if the buffers are empty */
1691 if (trace_empty(iter))
1692 return 0;
1693 print_trace_header(m, iter);
1694 if (!(trace_flags & TRACE_ITER_VERBOSE))
1695 print_lat_help_header(m);
1696 } else {
1697 if (!(trace_flags & TRACE_ITER_VERBOSE))
1698 print_func_help_header(m);
1699 }
1700 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001701 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001702 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001703 }
1704
1705 return 0;
1706}
1707
1708static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001709 .start = s_start,
1710 .next = s_next,
1711 .stop = s_stop,
1712 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001713};
1714
Ingo Molnare309b412008-05-12 21:20:51 +02001715static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001716__tracing_open(struct inode *inode, struct file *file, int *ret)
1717{
1718 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001719 struct seq_file *m;
1720 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001721
Steven Rostedt60a11772008-05-12 21:20:44 +02001722 if (tracing_disabled) {
1723 *ret = -ENODEV;
1724 return NULL;
1725 }
1726
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001727 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1728 if (!iter) {
1729 *ret = -ENOMEM;
1730 goto out;
1731 }
1732
1733 mutex_lock(&trace_types_lock);
1734 if (current_trace && current_trace->print_max)
1735 iter->tr = &max_tr;
1736 else
1737 iter->tr = inode->i_private;
1738 iter->trace = current_trace;
1739 iter->pos = -1;
1740
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001741 /* Notify the tracer early; before we stop tracing. */
1742 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001743 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001744
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001745 /* Annotate start of buffers if we had overruns */
1746 if (ring_buffer_overruns(iter->tr->buffer))
1747 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1748
1749
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001750 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001751
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001752 iter->buffer_iter[cpu] =
1753 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001754
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001755 if (!iter->buffer_iter[cpu])
1756 goto fail_buffer;
1757 }
1758
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001759 /* TODO stop tracer */
1760 *ret = seq_open(file, &tracer_seq_ops);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001761 if (*ret)
1762 goto fail_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001763
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001764 m = file->private_data;
1765 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001766
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001767 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001768 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001769
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001770 mutex_unlock(&trace_types_lock);
1771
1772 out:
1773 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001774
1775 fail_buffer:
1776 for_each_tracing_cpu(cpu) {
1777 if (iter->buffer_iter[cpu])
1778 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1779 }
1780 mutex_unlock(&trace_types_lock);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001781 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001782
1783 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001784}
1785
1786int tracing_open_generic(struct inode *inode, struct file *filp)
1787{
Steven Rostedt60a11772008-05-12 21:20:44 +02001788 if (tracing_disabled)
1789 return -ENODEV;
1790
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001791 filp->private_data = inode->i_private;
1792 return 0;
1793}
1794
1795int tracing_release(struct inode *inode, struct file *file)
1796{
1797 struct seq_file *m = (struct seq_file *)file->private_data;
1798 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001799 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001800
1801 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001802 for_each_tracing_cpu(cpu) {
1803 if (iter->buffer_iter[cpu])
1804 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1805 }
1806
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001807 if (iter->trace && iter->trace->close)
1808 iter->trace->close(iter);
1809
1810 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001811 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001812 mutex_unlock(&trace_types_lock);
1813
1814 seq_release(inode, file);
1815 kfree(iter);
1816 return 0;
1817}
1818
1819static int tracing_open(struct inode *inode, struct file *file)
1820{
1821 int ret;
1822
1823 __tracing_open(inode, file, &ret);
1824
1825 return ret;
1826}
1827
1828static int tracing_lt_open(struct inode *inode, struct file *file)
1829{
1830 struct trace_iterator *iter;
1831 int ret;
1832
1833 iter = __tracing_open(inode, file, &ret);
1834
1835 if (!ret)
1836 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1837
1838 return ret;
1839}
1840
1841
Ingo Molnare309b412008-05-12 21:20:51 +02001842static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001843t_next(struct seq_file *m, void *v, loff_t *pos)
1844{
1845 struct tracer *t = m->private;
1846
1847 (*pos)++;
1848
1849 if (t)
1850 t = t->next;
1851
1852 m->private = t;
1853
1854 return t;
1855}
1856
1857static void *t_start(struct seq_file *m, loff_t *pos)
1858{
1859 struct tracer *t = m->private;
1860 loff_t l = 0;
1861
1862 mutex_lock(&trace_types_lock);
1863 for (; t && l < *pos; t = t_next(m, t, &l))
1864 ;
1865
1866 return t;
1867}
1868
1869static void t_stop(struct seq_file *m, void *p)
1870{
1871 mutex_unlock(&trace_types_lock);
1872}
1873
1874static int t_show(struct seq_file *m, void *v)
1875{
1876 struct tracer *t = v;
1877
1878 if (!t)
1879 return 0;
1880
1881 seq_printf(m, "%s", t->name);
1882 if (t->next)
1883 seq_putc(m, ' ');
1884 else
1885 seq_putc(m, '\n');
1886
1887 return 0;
1888}
1889
1890static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001891 .start = t_start,
1892 .next = t_next,
1893 .stop = t_stop,
1894 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001895};
1896
1897static int show_traces_open(struct inode *inode, struct file *file)
1898{
1899 int ret;
1900
Steven Rostedt60a11772008-05-12 21:20:44 +02001901 if (tracing_disabled)
1902 return -ENODEV;
1903
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001904 ret = seq_open(file, &show_traces_seq_ops);
1905 if (!ret) {
1906 struct seq_file *m = file->private_data;
1907 m->private = trace_types;
1908 }
1909
1910 return ret;
1911}
1912
1913static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001914 .open = tracing_open,
1915 .read = seq_read,
1916 .llseek = seq_lseek,
1917 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001918};
1919
1920static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001921 .open = tracing_lt_open,
1922 .read = seq_read,
1923 .llseek = seq_lseek,
1924 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001925};
1926
1927static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001928 .open = show_traces_open,
1929 .read = seq_read,
1930 .release = seq_release,
1931};
1932
Ingo Molnar36dfe922008-05-12 21:20:52 +02001933/*
1934 * Only trace on a CPU if the bitmask is set:
1935 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301936static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001937
1938/*
1939 * The tracer itself will not take this lock, but still we want
1940 * to provide a consistent cpumask to user-space:
1941 */
1942static DEFINE_MUTEX(tracing_cpumask_update_lock);
1943
1944/*
1945 * Temporary storage for the character representation of the
1946 * CPU bitmask (and one more byte for the newline):
1947 */
1948static char mask_str[NR_CPUS + 1];
1949
Ingo Molnarc7078de2008-05-12 21:20:52 +02001950static ssize_t
1951tracing_cpumask_read(struct file *filp, char __user *ubuf,
1952 size_t count, loff_t *ppos)
1953{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001954 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001955
1956 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001957
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301958 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001959 if (count - len < 2) {
1960 count = -EINVAL;
1961 goto out_err;
1962 }
1963 len += sprintf(mask_str + len, "\n");
1964 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1965
1966out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001967 mutex_unlock(&tracing_cpumask_update_lock);
1968
1969 return count;
1970}
1971
1972static ssize_t
1973tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1974 size_t count, loff_t *ppos)
1975{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001976 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301977 cpumask_var_t tracing_cpumask_new;
1978
1979 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1980 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001981
1982 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301983 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001984 if (err)
1985 goto err_unlock;
1986
Steven Rostedta5e25882008-12-02 15:34:05 -05001987 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02001988 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001989 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001990 /*
1991 * Increase/decrease the disabled counter if we are
1992 * about to flip a bit in the cpumask:
1993 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301994 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1995 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001996 atomic_inc(&global_trace.data[cpu]->disabled);
1997 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301998 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1999 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002000 atomic_dec(&global_trace.data[cpu]->disabled);
2001 }
2002 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002003 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002004 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002005
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302006 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002007
Ingo Molnarc7078de2008-05-12 21:20:52 +02002008 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302009 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002010
Ingo Molnarc7078de2008-05-12 21:20:52 +02002011 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002012
2013err_unlock:
2014 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302015 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002016
2017 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002018}
2019
2020static struct file_operations tracing_cpumask_fops = {
2021 .open = tracing_open_generic,
2022 .read = tracing_cpumask_read,
2023 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002024};
2025
2026static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002027tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002028 size_t cnt, loff_t *ppos)
2029{
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002030 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002031 char *buf;
2032 int r = 0;
2033 int len = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002034 u32 tracer_flags = current_trace->flags->val;
2035 struct tracer_opt *trace_opts = current_trace->flags->opts;
2036
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002037
2038 /* calulate max size */
2039 for (i = 0; trace_options[i]; i++) {
2040 len += strlen(trace_options[i]);
2041 len += 3; /* "no" and space */
2042 }
2043
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002044 /*
2045 * Increase the size with names of options specific
2046 * of the current tracer.
2047 */
2048 for (i = 0; trace_opts[i].name; i++) {
2049 len += strlen(trace_opts[i].name);
2050 len += 3; /* "no" and space */
2051 }
2052
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002053 /* +2 for \n and \0 */
2054 buf = kmalloc(len + 2, GFP_KERNEL);
2055 if (!buf)
2056 return -ENOMEM;
2057
2058 for (i = 0; trace_options[i]; i++) {
2059 if (trace_flags & (1 << i))
2060 r += sprintf(buf + r, "%s ", trace_options[i]);
2061 else
2062 r += sprintf(buf + r, "no%s ", trace_options[i]);
2063 }
2064
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002065 for (i = 0; trace_opts[i].name; i++) {
2066 if (tracer_flags & trace_opts[i].bit)
2067 r += sprintf(buf + r, "%s ",
2068 trace_opts[i].name);
2069 else
2070 r += sprintf(buf + r, "no%s ",
2071 trace_opts[i].name);
2072 }
2073
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002074 r += sprintf(buf + r, "\n");
2075 WARN_ON(r >= len + 2);
2076
Ingo Molnar36dfe922008-05-12 21:20:52 +02002077 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002078
2079 kfree(buf);
2080
2081 return r;
2082}
2083
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002084/* Try to assign a tracer specific option */
2085static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2086{
2087 struct tracer_flags *trace_flags = trace->flags;
2088 struct tracer_opt *opts = NULL;
2089 int ret = 0, i = 0;
2090 int len;
2091
2092 for (i = 0; trace_flags->opts[i].name; i++) {
2093 opts = &trace_flags->opts[i];
2094 len = strlen(opts->name);
2095
2096 if (strncmp(cmp, opts->name, len) == 0) {
2097 ret = trace->set_flag(trace_flags->val,
2098 opts->bit, !neg);
2099 break;
2100 }
2101 }
2102 /* Not found */
2103 if (!trace_flags->opts[i].name)
2104 return -EINVAL;
2105
2106 /* Refused to handle */
2107 if (ret)
2108 return ret;
2109
2110 if (neg)
2111 trace_flags->val &= ~opts->bit;
2112 else
2113 trace_flags->val |= opts->bit;
2114
2115 return 0;
2116}
2117
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002118static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002119tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002120 size_t cnt, loff_t *ppos)
2121{
2122 char buf[64];
2123 char *cmp = buf;
2124 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002125 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002126 int i;
2127
Steven Rostedtcffae432008-05-12 21:21:00 +02002128 if (cnt >= sizeof(buf))
2129 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002130
2131 if (copy_from_user(&buf, ubuf, cnt))
2132 return -EFAULT;
2133
2134 buf[cnt] = 0;
2135
2136 if (strncmp(buf, "no", 2) == 0) {
2137 neg = 1;
2138 cmp += 2;
2139 }
2140
2141 for (i = 0; trace_options[i]; i++) {
2142 int len = strlen(trace_options[i]);
2143
2144 if (strncmp(cmp, trace_options[i], len) == 0) {
2145 if (neg)
2146 trace_flags &= ~(1 << i);
2147 else
2148 trace_flags |= (1 << i);
2149 break;
2150 }
2151 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002152
2153 /* If no option could be set, test the specific tracer options */
2154 if (!trace_options[i]) {
2155 ret = set_tracer_option(current_trace, cmp, neg);
2156 if (ret)
2157 return ret;
2158 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002159
2160 filp->f_pos += cnt;
2161
2162 return cnt;
2163}
2164
2165static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002166 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002167 .read = tracing_trace_options_read,
2168 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002169};
2170
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002171static const char readme_msg[] =
2172 "tracing mini-HOWTO:\n\n"
2173 "# mkdir /debug\n"
2174 "# mount -t debugfs nodev /debug\n\n"
2175 "# cat /debug/tracing/available_tracers\n"
2176 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2177 "# cat /debug/tracing/current_tracer\n"
2178 "none\n"
2179 "# echo sched_switch > /debug/tracing/current_tracer\n"
2180 "# cat /debug/tracing/current_tracer\n"
2181 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002182 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002183 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002184 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002185 "# echo 1 > /debug/tracing/tracing_enabled\n"
2186 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2187 "echo 0 > /debug/tracing/tracing_enabled\n"
2188;
2189
2190static ssize_t
2191tracing_readme_read(struct file *filp, char __user *ubuf,
2192 size_t cnt, loff_t *ppos)
2193{
2194 return simple_read_from_buffer(ubuf, cnt, ppos,
2195 readme_msg, strlen(readme_msg));
2196}
2197
2198static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002199 .open = tracing_open_generic,
2200 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002201};
2202
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002203static ssize_t
2204tracing_ctrl_read(struct file *filp, char __user *ubuf,
2205 size_t cnt, loff_t *ppos)
2206{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002207 char buf[64];
2208 int r;
2209
Steven Rostedt90369902008-11-05 16:05:44 -05002210 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002211 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002212}
2213
2214static ssize_t
2215tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2216 size_t cnt, loff_t *ppos)
2217{
2218 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002219 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002220 long val;
2221 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002222
Steven Rostedtcffae432008-05-12 21:21:00 +02002223 if (cnt >= sizeof(buf))
2224 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002225
2226 if (copy_from_user(&buf, ubuf, cnt))
2227 return -EFAULT;
2228
2229 buf[cnt] = 0;
2230
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002231 ret = strict_strtoul(buf, 10, &val);
2232 if (ret < 0)
2233 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002234
2235 val = !!val;
2236
2237 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002238 if (tracer_enabled ^ val) {
2239 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002240 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002241 if (current_trace->start)
2242 current_trace->start(tr);
2243 tracing_start();
2244 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002245 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002246 tracing_stop();
2247 if (current_trace->stop)
2248 current_trace->stop(tr);
2249 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002250 }
2251 mutex_unlock(&trace_types_lock);
2252
2253 filp->f_pos += cnt;
2254
2255 return cnt;
2256}
2257
2258static ssize_t
2259tracing_set_trace_read(struct file *filp, char __user *ubuf,
2260 size_t cnt, loff_t *ppos)
2261{
2262 char buf[max_tracer_type_len+2];
2263 int r;
2264
2265 mutex_lock(&trace_types_lock);
2266 if (current_trace)
2267 r = sprintf(buf, "%s\n", current_trace->name);
2268 else
2269 r = sprintf(buf, "\n");
2270 mutex_unlock(&trace_types_lock);
2271
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002272 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273}
2274
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002275static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002276{
2277 struct trace_array *tr = &global_trace;
2278 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002279 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002280
2281 mutex_lock(&trace_types_lock);
2282 for (t = trace_types; t; t = t->next) {
2283 if (strcmp(t->name, buf) == 0)
2284 break;
2285 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002286 if (!t) {
2287 ret = -EINVAL;
2288 goto out;
2289 }
2290 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002291 goto out;
2292
Steven Rostedt9f029e82008-11-12 15:24:24 -05002293 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002294 if (current_trace && current_trace->reset)
2295 current_trace->reset(tr);
2296
2297 current_trace = t;
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002298 if (t->init) {
2299 ret = t->init(tr);
2300 if (ret)
2301 goto out;
2302 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002303
Steven Rostedt9f029e82008-11-12 15:24:24 -05002304 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002305 out:
2306 mutex_unlock(&trace_types_lock);
2307
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002308 return ret;
2309}
2310
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002311static ssize_t
2312tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2313 size_t cnt, loff_t *ppos)
2314{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002315 char buf[max_tracer_type_len+1];
2316 int i;
2317 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002318 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002319
Steven Rostedt60063a62008-10-28 10:44:24 -04002320 ret = cnt;
2321
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002322 if (cnt > max_tracer_type_len)
2323 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002324
2325 if (copy_from_user(&buf, ubuf, cnt))
2326 return -EFAULT;
2327
2328 buf[cnt] = 0;
2329
2330 /* strip ending whitespace. */
2331 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2332 buf[i] = 0;
2333
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002334 err = tracing_set_tracer(buf);
2335 if (err)
2336 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002337
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002338 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002339
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002340 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002341}
2342
2343static ssize_t
2344tracing_max_lat_read(struct file *filp, char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2346{
2347 unsigned long *ptr = filp->private_data;
2348 char buf[64];
2349 int r;
2350
Steven Rostedtcffae432008-05-12 21:21:00 +02002351 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002352 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002353 if (r > sizeof(buf))
2354 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002355 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356}
2357
2358static ssize_t
2359tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2360 size_t cnt, loff_t *ppos)
2361{
2362 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002364 long val;
2365 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002366
Steven Rostedtcffae432008-05-12 21:21:00 +02002367 if (cnt >= sizeof(buf))
2368 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002369
2370 if (copy_from_user(&buf, ubuf, cnt))
2371 return -EFAULT;
2372
2373 buf[cnt] = 0;
2374
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002375 ret = strict_strtoul(buf, 10, &val);
2376 if (ret < 0)
2377 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002378
2379 *ptr = val * 1000;
2380
2381 return cnt;
2382}
2383
Steven Rostedtb3806b42008-05-12 21:20:46 +02002384static atomic_t tracing_reader;
2385
2386static int tracing_open_pipe(struct inode *inode, struct file *filp)
2387{
2388 struct trace_iterator *iter;
2389
2390 if (tracing_disabled)
2391 return -ENODEV;
2392
2393 /* We only allow for reader of the pipe */
2394 if (atomic_inc_return(&tracing_reader) != 1) {
2395 atomic_dec(&tracing_reader);
2396 return -EBUSY;
2397 }
2398
2399 /* create a buffer to store the information to pass to userspace */
2400 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2401 if (!iter)
2402 return -ENOMEM;
2403
Rusty Russell44623442009-01-01 10:12:23 +10302404 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2405 kfree(iter);
2406 return -ENOMEM;
2407 }
2408
Steven Rostedt107bad82008-05-12 21:21:01 +02002409 mutex_lock(&trace_types_lock);
Steven Rostedta3097202008-11-07 22:36:02 -05002410
2411 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302412 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002413
Steven Rostedtb3806b42008-05-12 21:20:46 +02002414 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002415 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002416 filp->private_data = iter;
2417
Steven Rostedt107bad82008-05-12 21:21:01 +02002418 if (iter->trace->pipe_open)
2419 iter->trace->pipe_open(iter);
2420 mutex_unlock(&trace_types_lock);
2421
Steven Rostedtb3806b42008-05-12 21:20:46 +02002422 return 0;
2423}
2424
2425static int tracing_release_pipe(struct inode *inode, struct file *file)
2426{
2427 struct trace_iterator *iter = file->private_data;
2428
Rusty Russell44623442009-01-01 10:12:23 +10302429 free_cpumask_var(iter->started);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002430 kfree(iter);
2431 atomic_dec(&tracing_reader);
2432
2433 return 0;
2434}
2435
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002436static unsigned int
2437tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2438{
2439 struct trace_iterator *iter = filp->private_data;
2440
2441 if (trace_flags & TRACE_ITER_BLOCK) {
2442 /*
2443 * Always select as readable when in blocking mode
2444 */
2445 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002446 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002447 if (!trace_empty(iter))
2448 return POLLIN | POLLRDNORM;
2449 poll_wait(filp, &trace_wait, poll_table);
2450 if (!trace_empty(iter))
2451 return POLLIN | POLLRDNORM;
2452
2453 return 0;
2454 }
2455}
2456
Steven Rostedtb3806b42008-05-12 21:20:46 +02002457/*
2458 * Consumer reader.
2459 */
2460static ssize_t
2461tracing_read_pipe(struct file *filp, char __user *ubuf,
2462 size_t cnt, loff_t *ppos)
2463{
2464 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002465 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002466
2467 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002468 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2469 if (sret != -EBUSY)
2470 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002471
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002472 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002473
Steven Rostedt107bad82008-05-12 21:21:01 +02002474 mutex_lock(&trace_types_lock);
2475 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002476 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2477 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002478 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002479 }
2480
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002481waitagain:
2482 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002483 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002484
Steven Rostedt107bad82008-05-12 21:21:01 +02002485 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002486 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002487 goto out;
2488 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002489
Steven Rostedtb3806b42008-05-12 21:20:46 +02002490 /*
2491 * This is a make-shift waitqueue. The reason we don't use
2492 * an actual wait queue is because:
2493 * 1) we only ever have one waiter
2494 * 2) the tracing, traces all functions, we don't want
2495 * the overhead of calling wake_up and friends
2496 * (and tracing them too)
2497 * Anyway, this is really very primitive wakeup.
2498 */
2499 set_current_state(TASK_INTERRUPTIBLE);
2500 iter->tr->waiter = current;
2501
Steven Rostedt107bad82008-05-12 21:21:01 +02002502 mutex_unlock(&trace_types_lock);
2503
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002504 /* sleep for 100 msecs, and try again. */
2505 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002506
Steven Rostedt107bad82008-05-12 21:21:01 +02002507 mutex_lock(&trace_types_lock);
2508
Steven Rostedtb3806b42008-05-12 21:20:46 +02002509 iter->tr->waiter = NULL;
2510
Steven Rostedt107bad82008-05-12 21:21:01 +02002511 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002512 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002513 goto out;
2514 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002515
Steven Rostedt84527992008-05-12 21:20:58 +02002516 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002517 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002518
Steven Rostedtb3806b42008-05-12 21:20:46 +02002519 /*
2520 * We block until we read something and tracing is disabled.
2521 * We still block if tracing is disabled, but we have never
2522 * read anything. This allows a user to cat this file, and
2523 * then enable tracing. But after we have read something,
2524 * we give an EOF when tracing is again disabled.
2525 *
2526 * iter->pos will be 0 if we haven't read anything.
2527 */
2528 if (!tracer_enabled && iter->pos)
2529 break;
2530
2531 continue;
2532 }
2533
2534 /* stop when tracing is finished */
2535 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002536 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002537
2538 if (cnt >= PAGE_SIZE)
2539 cnt = PAGE_SIZE - 1;
2540
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002541 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002542 memset(&iter->seq, 0,
2543 sizeof(struct trace_iterator) -
2544 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002545 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002546
Steven Rostedt088b1e422008-05-12 21:20:48 +02002547 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002548 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002549 int len = iter->seq.len;
2550
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002551 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002552 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002553 /* don't print partial lines */
2554 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002555 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002556 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002557
2558 trace_consume(iter);
2559
2560 if (iter->seq.len >= cnt)
2561 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002562 }
2563
Steven Rostedtb3806b42008-05-12 21:20:46 +02002564 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002565 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2566 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002567 trace_seq_reset(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002568
2569 /*
2570 * If there was nothing to send to user, inspite of consuming trace
2571 * entries, go back to wait for more entries.
2572 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002573 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002574 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002575
Steven Rostedt107bad82008-05-12 21:21:01 +02002576out:
2577 mutex_unlock(&trace_types_lock);
2578
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002579 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002580}
2581
Steven Rostedta98a3c32008-05-12 21:20:59 +02002582static ssize_t
2583tracing_entries_read(struct file *filp, char __user *ubuf,
2584 size_t cnt, loff_t *ppos)
2585{
2586 struct trace_array *tr = filp->private_data;
2587 char buf[64];
2588 int r;
2589
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002590 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002591 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2592}
2593
2594static ssize_t
2595tracing_entries_write(struct file *filp, const char __user *ubuf,
2596 size_t cnt, loff_t *ppos)
2597{
2598 unsigned long val;
2599 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002600 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002601
Steven Rostedtcffae432008-05-12 21:21:00 +02002602 if (cnt >= sizeof(buf))
2603 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002604
2605 if (copy_from_user(&buf, ubuf, cnt))
2606 return -EFAULT;
2607
2608 buf[cnt] = 0;
2609
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002610 ret = strict_strtoul(buf, 10, &val);
2611 if (ret < 0)
2612 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002613
2614 /* must have at least 1 entry */
2615 if (!val)
2616 return -EINVAL;
2617
2618 mutex_lock(&trace_types_lock);
2619
Steven Rostedtc76f0692008-11-07 22:36:02 -05002620 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002621
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002622 /* disable all cpu buffers */
2623 for_each_tracing_cpu(cpu) {
2624 if (global_trace.data[cpu])
2625 atomic_inc(&global_trace.data[cpu]->disabled);
2626 if (max_tr.data[cpu])
2627 atomic_inc(&max_tr.data[cpu]->disabled);
2628 }
2629
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002630 /* value is in KB */
2631 val <<= 10;
2632
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002633 if (val != global_trace.entries) {
2634 ret = ring_buffer_resize(global_trace.buffer, val);
2635 if (ret < 0) {
2636 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002637 goto out;
2638 }
2639
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002640 ret = ring_buffer_resize(max_tr.buffer, val);
2641 if (ret < 0) {
2642 int r;
2643 cnt = ret;
2644 r = ring_buffer_resize(global_trace.buffer,
2645 global_trace.entries);
2646 if (r < 0) {
2647 /* AARGH! We are left with different
2648 * size max buffer!!!! */
2649 WARN_ON(1);
2650 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002651 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002652 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002653 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002654
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002655 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002656 }
2657
2658 filp->f_pos += cnt;
2659
Steven Rostedt19384c02008-05-22 00:22:16 -04002660 /* If check pages failed, return ENOMEM */
2661 if (tracing_disabled)
2662 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002663 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002664 for_each_tracing_cpu(cpu) {
2665 if (global_trace.data[cpu])
2666 atomic_dec(&global_trace.data[cpu]->disabled);
2667 if (max_tr.data[cpu])
2668 atomic_dec(&max_tr.data[cpu]->disabled);
2669 }
2670
Steven Rostedtc76f0692008-11-07 22:36:02 -05002671 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002672 max_tr.entries = global_trace.entries;
2673 mutex_unlock(&trace_types_lock);
2674
2675 return cnt;
2676}
2677
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002678static int mark_printk(const char *fmt, ...)
2679{
2680 int ret;
2681 va_list args;
2682 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002683 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002684 va_end(args);
2685 return ret;
2686}
2687
2688static ssize_t
2689tracing_mark_write(struct file *filp, const char __user *ubuf,
2690 size_t cnt, loff_t *fpos)
2691{
2692 char *buf;
2693 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002694
Steven Rostedtc76f0692008-11-07 22:36:02 -05002695 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002696 return -EINVAL;
2697
2698 if (cnt > TRACE_BUF_SIZE)
2699 cnt = TRACE_BUF_SIZE;
2700
2701 buf = kmalloc(cnt + 1, GFP_KERNEL);
2702 if (buf == NULL)
2703 return -ENOMEM;
2704
2705 if (copy_from_user(buf, ubuf, cnt)) {
2706 kfree(buf);
2707 return -EFAULT;
2708 }
2709
2710 /* Cut from the first nil or newline. */
2711 buf[cnt] = '\0';
2712 end = strchr(buf, '\n');
2713 if (end)
2714 *end = '\0';
2715
2716 cnt = mark_printk("%s\n", buf);
2717 kfree(buf);
2718 *fpos += cnt;
2719
2720 return cnt;
2721}
2722
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002723static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002724 .open = tracing_open_generic,
2725 .read = tracing_max_lat_read,
2726 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002727};
2728
2729static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002730 .open = tracing_open_generic,
2731 .read = tracing_ctrl_read,
2732 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002733};
2734
2735static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002736 .open = tracing_open_generic,
2737 .read = tracing_set_trace_read,
2738 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002739};
2740
Steven Rostedtb3806b42008-05-12 21:20:46 +02002741static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002742 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002743 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002744 .read = tracing_read_pipe,
2745 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002746};
2747
Steven Rostedta98a3c32008-05-12 21:20:59 +02002748static struct file_operations tracing_entries_fops = {
2749 .open = tracing_open_generic,
2750 .read = tracing_entries_read,
2751 .write = tracing_entries_write,
2752};
2753
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002754static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002755 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002756 .write = tracing_mark_write,
2757};
2758
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002759#ifdef CONFIG_DYNAMIC_FTRACE
2760
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002761int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002762{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002763 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002764}
2765
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002766static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002767tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002768 size_t cnt, loff_t *ppos)
2769{
Steven Rostedta26a2a22008-10-31 00:03:22 -04002770 static char ftrace_dyn_info_buffer[1024];
2771 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002772 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002773 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04002774 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002775 int r;
2776
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002777 mutex_lock(&dyn_info_mutex);
2778 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002779
Steven Rostedta26a2a22008-10-31 00:03:22 -04002780 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002781 buf[r++] = '\n';
2782
2783 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2784
2785 mutex_unlock(&dyn_info_mutex);
2786
2787 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002788}
2789
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002790static struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002791 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002792 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002793};
2794#endif
2795
2796static struct dentry *d_tracer;
2797
2798struct dentry *tracing_init_dentry(void)
2799{
2800 static int once;
2801
2802 if (d_tracer)
2803 return d_tracer;
2804
2805 d_tracer = debugfs_create_dir("tracing", NULL);
2806
2807 if (!d_tracer && !once) {
2808 once = 1;
2809 pr_warning("Could not create debugfs directory 'tracing'\n");
2810 return NULL;
2811 }
2812
2813 return d_tracer;
2814}
2815
Steven Rostedt60a11772008-05-12 21:20:44 +02002816#ifdef CONFIG_FTRACE_SELFTEST
2817/* Let selftest have access to static functions in this file */
2818#include "trace_selftest.c"
2819#endif
2820
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01002821static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002822{
2823 struct dentry *d_tracer;
2824 struct dentry *entry;
2825
2826 d_tracer = tracing_init_dentry();
2827
2828 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2829 &global_trace, &tracing_ctrl_fops);
2830 if (!entry)
2831 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2832
Steven Rostedtee6bce52008-11-12 17:52:37 -05002833 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002834 NULL, &tracing_iter_fops);
2835 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05002836 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002837
Ingo Molnarc7078de2008-05-12 21:20:52 +02002838 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2839 NULL, &tracing_cpumask_fops);
2840 if (!entry)
2841 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2842
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002843 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2844 &global_trace, &tracing_lt_fops);
2845 if (!entry)
2846 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2847
2848 entry = debugfs_create_file("trace", 0444, d_tracer,
2849 &global_trace, &tracing_fops);
2850 if (!entry)
2851 pr_warning("Could not create debugfs 'trace' entry\n");
2852
2853 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2854 &global_trace, &show_traces_fops);
2855 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002856 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002857
2858 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2859 &global_trace, &set_tracer_fops);
2860 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002861 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002862
2863 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2864 &tracing_max_latency,
2865 &tracing_max_lat_fops);
2866 if (!entry)
2867 pr_warning("Could not create debugfs "
2868 "'tracing_max_latency' entry\n");
2869
2870 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2871 &tracing_thresh, &tracing_max_lat_fops);
2872 if (!entry)
2873 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002874 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002875 entry = debugfs_create_file("README", 0644, d_tracer,
2876 NULL, &tracing_readme_fops);
2877 if (!entry)
2878 pr_warning("Could not create debugfs 'README' entry\n");
2879
Steven Rostedtb3806b42008-05-12 21:20:46 +02002880 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2881 NULL, &tracing_pipe_fops);
2882 if (!entry)
2883 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002884 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002885
Steven Rostedta94c80e2008-11-12 17:52:36 -05002886 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02002887 &global_trace, &tracing_entries_fops);
2888 if (!entry)
2889 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05002890 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02002891
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002892 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2893 NULL, &tracing_mark_fops);
2894 if (!entry)
2895 pr_warning("Could not create debugfs "
2896 "'trace_marker' entry\n");
2897
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002898#ifdef CONFIG_DYNAMIC_FTRACE
2899 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2900 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002901 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002902 if (!entry)
2903 pr_warning("Could not create debugfs "
2904 "'dyn_ftrace_total_info' entry\n");
2905#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02002906#ifdef CONFIG_SYSPROF_TRACER
2907 init_tracer_sysprof_debugfs(d_tracer);
2908#endif
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01002909 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002910}
2911
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002912int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002913{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002914 static DEFINE_SPINLOCK(trace_buf_lock);
2915 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02002916
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002917 struct ring_buffer_event *event;
Peter Zijlstraf09ce572008-09-04 10:24:14 +02002918 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002919 struct trace_array_cpu *data;
Steven Rostedt38697052008-10-01 13:14:09 -04002920 int cpu, len = 0, size, pc;
Ingo Molnare726f5f2008-12-08 16:55:53 +01002921 struct print_entry *entry;
2922 unsigned long irq_flags;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002923
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01002924 if (tracing_disabled || tracing_selftest_running)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002925 return 0;
2926
Steven Rostedt38697052008-10-01 13:14:09 -04002927 pc = preempt_count();
2928 preempt_disable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002929 cpu = raw_smp_processor_id();
2930 data = tr->data[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002931
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -04002932 if (unlikely(atomic_read(&data->disabled)))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002933 goto out;
2934
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01002935 pause_graph_tracing();
2936 spin_lock_irqsave(&trace_buf_lock, irq_flags);
Pekka Paalanen801fe402008-09-16 21:58:24 +03002937 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002938
2939 len = min(len, TRACE_BUF_SIZE-1);
2940 trace_buf[len] = 0;
2941
Steven Rostedt777e2082008-09-29 23:02:42 -04002942 size = sizeof(*entry) + len + 1;
2943 event = ring_buffer_lock_reserve(tr->buffer, size, &irq_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002944 if (!event)
2945 goto out_unlock;
Steven Rostedt777e2082008-09-29 23:02:42 -04002946 entry = ring_buffer_event_data(event);
Ingo Molnare726f5f2008-12-08 16:55:53 +01002947 tracing_generic_entry_update(&entry->ent, irq_flags, pc);
Steven Rostedt777e2082008-09-29 23:02:42 -04002948 entry->ent.type = TRACE_PRINT;
2949 entry->ip = ip;
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002950 entry->depth = depth;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002951
Steven Rostedt777e2082008-09-29 23:02:42 -04002952 memcpy(&entry->buf, trace_buf, len);
2953 entry->buf[len] = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002954 ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002955
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002956 out_unlock:
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01002957 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
2958 unpause_graph_tracing();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002959 out:
Steven Rostedt38697052008-10-01 13:14:09 -04002960 preempt_enable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002961
2962 return len;
2963}
Pekka Paalanen801fe402008-09-16 21:58:24 +03002964EXPORT_SYMBOL_GPL(trace_vprintk);
2965
2966int __ftrace_printk(unsigned long ip, const char *fmt, ...)
2967{
2968 int ret;
2969 va_list ap;
2970
2971 if (!(trace_flags & TRACE_ITER_PRINTK))
2972 return 0;
2973
2974 va_start(ap, fmt);
Frederic Weisbecker21a8c462008-12-04 23:51:23 +01002975 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
Pekka Paalanen801fe402008-09-16 21:58:24 +03002976 va_end(ap);
2977 return ret;
2978}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002979EXPORT_SYMBOL_GPL(__ftrace_printk);
2980
Arnaldo Carvalho de Melo90112622009-01-23 12:06:23 -02002981int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
2982{
2983 if (!(trace_flags & TRACE_ITER_PRINTK))
2984 return 0;
2985
2986 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
2987}
2988EXPORT_SYMBOL_GPL(__ftrace_vprintk);
2989
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04002990static int trace_panic_handler(struct notifier_block *this,
2991 unsigned long event, void *unused)
2992{
Steven Rostedt944ac422008-10-23 19:26:08 -04002993 if (ftrace_dump_on_oops)
2994 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04002995 return NOTIFY_OK;
2996}
2997
2998static struct notifier_block trace_panic_notifier = {
2999 .notifier_call = trace_panic_handler,
3000 .next = NULL,
3001 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3002};
3003
3004static int trace_die_handler(struct notifier_block *self,
3005 unsigned long val,
3006 void *data)
3007{
3008 switch (val) {
3009 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003010 if (ftrace_dump_on_oops)
3011 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003012 break;
3013 default:
3014 break;
3015 }
3016 return NOTIFY_OK;
3017}
3018
3019static struct notifier_block trace_die_notifier = {
3020 .notifier_call = trace_die_handler,
3021 .priority = 200
3022};
3023
3024/*
3025 * printk is set to max of 1024, we really don't need it that big.
3026 * Nothing should be printing 1000 characters anyway.
3027 */
3028#define TRACE_MAX_PRINT 1000
3029
3030/*
3031 * Define here KERN_TRACE so that we have one place to modify
3032 * it if we decide to change what log level the ftrace dump
3033 * should be at.
3034 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003035#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003036
3037static void
3038trace_printk_seq(struct trace_seq *s)
3039{
3040 /* Probably should print a warning here. */
3041 if (s->len >= 1000)
3042 s->len = 1000;
3043
3044 /* should be zero ended, but we are paranoid. */
3045 s->buffer[s->len] = 0;
3046
3047 printk(KERN_TRACE "%s", s->buffer);
3048
3049 trace_seq_reset(s);
3050}
3051
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003052void ftrace_dump(void)
3053{
3054 static DEFINE_SPINLOCK(ftrace_dump_lock);
3055 /* use static because iter can be a bit big for the stack */
3056 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003057 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003058 unsigned long flags;
3059 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003060
3061 /* only one dump */
3062 spin_lock_irqsave(&ftrace_dump_lock, flags);
3063 if (dump_ran)
3064 goto out;
3065
3066 dump_ran = 1;
3067
3068 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05003069 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003070 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003071
Steven Rostedtd7690412008-10-01 00:29:53 -04003072 for_each_tracing_cpu(cpu) {
3073 atomic_inc(&global_trace.data[cpu]->disabled);
3074 }
3075
Török Edwinb54d3de2008-11-22 13:28:48 +02003076 /* don't look at user memory in panic mode */
3077 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3078
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003079 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3080
3081 iter.tr = &global_trace;
3082 iter.trace = current_trace;
3083
3084 /*
3085 * We need to stop all tracing on all CPUS to read the
3086 * the next buffer. This is a bit expensive, but is
3087 * not done often. We fill all what we can read,
3088 * and then release the locks again.
3089 */
3090
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003091 while (!trace_empty(&iter)) {
3092
3093 if (!cnt)
3094 printk(KERN_TRACE "---------------------------------\n");
3095
3096 cnt++;
3097
3098 /* reset all but tr, trace, and overruns */
3099 memset(&iter.seq, 0,
3100 sizeof(struct trace_iterator) -
3101 offsetof(struct trace_iterator, seq));
3102 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3103 iter.pos = -1;
3104
3105 if (find_next_entry_inc(&iter) != NULL) {
3106 print_trace_line(&iter);
3107 trace_consume(&iter);
3108 }
3109
3110 trace_printk_seq(&iter.seq);
3111 }
3112
3113 if (!cnt)
3114 printk(KERN_TRACE " (ftrace buffer empty)\n");
3115 else
3116 printk(KERN_TRACE "---------------------------------\n");
3117
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003118 out:
3119 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3120}
3121
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003122__init static int tracer_alloc_buffers(void)
3123{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003124 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003125 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303126 int ret = -ENOMEM;
3127
3128 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3129 goto out;
3130
3131 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3132 goto out_free_buffer_mask;
3133
3134 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3135 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003136
Steven Rostedtab464282008-05-12 21:21:00 +02003137 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003138 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3139 TRACE_BUFFER_FLAGS);
3140 if (!global_trace.buffer) {
3141 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3142 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303143 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003144 }
3145 global_trace.entries = ring_buffer_size(global_trace.buffer);
3146
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303147
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003148#ifdef CONFIG_TRACER_MAX_TRACE
3149 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3150 TRACE_BUFFER_FLAGS);
3151 if (!max_tr.buffer) {
3152 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3153 WARN_ON(1);
3154 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303155 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003156 }
3157 max_tr.entries = ring_buffer_size(max_tr.buffer);
3158 WARN_ON(max_tr.entries != global_trace.entries);
3159#endif
3160
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003161 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003162 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003163 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003164 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003165 }
3166
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003167 trace_init_cmdlines();
3168
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003169 register_tracer(&nop_trace);
Steven Rostedt79fb0768f2009-02-02 21:38:33 -05003170 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003171#ifdef CONFIG_BOOT_TRACER
3172 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003173#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02003174 /* All seems OK, enable tracing */
3175 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003176
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003177 atomic_notifier_chain_register(&panic_notifier_list,
3178 &trace_panic_notifier);
3179
3180 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303181 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003182
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303183out_free_cpumask:
3184 free_cpumask_var(tracing_cpumask);
3185out_free_buffer_mask:
3186 free_cpumask_var(tracing_buffer_mask);
3187out:
3188 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003189}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003190
3191__init static int clear_boot_tracer(void)
3192{
3193 /*
3194 * The default tracer at boot buffer is an init section.
3195 * This function is called in lateinit. If we did not
3196 * find the boot tracer, then clear it out, to prevent
3197 * later registration from accessing the buffer that is
3198 * about to be freed.
3199 */
3200 if (!default_bootup_tracer)
3201 return 0;
3202
3203 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
3204 default_bootup_tracer);
3205 default_bootup_tracer = NULL;
3206
3207 return 0;
3208}
3209
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003210early_initcall(tracer_alloc_buffers);
3211fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003212late_initcall(clear_boot_tracer);