blob: 2796bd2b17e4c4ad7e928691b4ca42c80cfd42ea [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 */
Steven Rostedt2cadf912008-12-01 22:20:19 -050014#include <linux/ring_buffer.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020015#include <linux/utsrelease.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050016#include <linux/stacktrace.h>
17#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020018#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040020#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050021#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020022#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020023#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020024#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050027#include <linux/kprobes.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050031#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040032#include <linux/kdebug.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020033#include <linux/ctype.h>
34#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020035#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020036#include <linux/gfp.h>
37#include <linux/fs.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/*
Steven Rostedt73c51622009-03-11 13:42:01 -040048 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
50 */
51static int ring_buffer_expanded;
52
53/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010054 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010055 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010057 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010058 * at the same time, giving false positive or negative results.
59 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010060static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010061
Steven Rostedtb2821ae2009-02-02 21:38:32 -050062/*
63 * If a tracer is running, we do not want to run SELFTEST.
64 */
65static bool __read_mostly tracing_selftest_disabled;
66
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010067/* For tracers that don't implement custom flags */
68static struct tracer_opt dummy_tracer_opt[] = {
69 { }
70};
71
72static struct tracer_flags dummy_tracer_flags = {
73 .val = 0,
74 .opts = dummy_tracer_opt
75};
76
77static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78{
79 return 0;
80}
Steven Rostedt0f048702008-11-05 16:05:44 -050081
82/*
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
86 * this back to zero.
87 */
Hannes Eder4fd27352009-02-10 19:44:12 +010088static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050089
Steven Rostedtd7690412008-10-01 00:29:53 -040090static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
91
92static inline void ftrace_disable_cpu(void)
93{
94 preempt_disable();
95 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
96}
97
98static inline void ftrace_enable_cpu(void)
99{
100 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
101 preempt_enable();
102}
103
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030104static cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200105
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +0100106/* Define which cpu buffers are currently read in trace_pipe */
107static cpumask_var_t tracing_reader_cpumask;
108
Steven Rostedtab464282008-05-12 21:21:00 +0200109#define for_each_tracing_cpu(cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030110 for_each_cpu(cpu, tracing_buffer_mask)
Steven Rostedtab464282008-05-12 21:21:00 +0200111
Steven Rostedt944ac422008-10-23 19:26:08 -0400112/*
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 *
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
119 * serial console.
120 *
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
123 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 */
125int ftrace_dump_on_oops;
126
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500127static int tracing_set_tracer(const char *buf);
128
129#define BOOTUP_TRACER_SIZE 100
130static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
131static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100132
133static int __init set_ftrace(char *str)
134{
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500135 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
136 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100139 return 1;
140}
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500141__setup("ftrace=", set_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100142
Steven Rostedt944ac422008-10-23 19:26:08 -0400143static int __init set_ftrace_dump_on_oops(char *str)
144{
145 ftrace_dump_on_oops = 1;
146 return 1;
147}
148__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200149
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200150long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200151ns2usecs(cycle_t nsec)
152{
153 nsec += 500;
154 do_div(nsec, 1000);
155 return nsec;
156}
157
Ingo Molnare309b412008-05-12 21:20:51 +0200158cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200159{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400160 u64 ts = ring_buffer_time_stamp(cpu);
161 ring_buffer_normalize_time_stamp(cpu, &ts);
162 return ts;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200163}
164
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200165/*
166 * The global_trace is the descriptor that holds the tracing
167 * buffers for the live tracing. For each CPU, it contains
168 * a link list of pages that will store trace entries. The
169 * page descriptor of the pages in the memory is used to hold
170 * the link list by linking the lru item in the page descriptor
171 * to each of the pages in the buffer per CPU.
172 *
173 * For each active CPU there is a data field that holds the
174 * pages for the buffer for that CPU. Each CPU has the same number
175 * of pages allocated for its buffer.
176 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200177static struct trace_array global_trace;
178
179static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
180
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200181/*
182 * The max_tr is used to snapshot the global_trace when a maximum
183 * latency is reached. Some tracers will use this to store a maximum
184 * trace while it continues examining live traces.
185 *
186 * The buffers for the max_tr are set up the same as the global_trace.
187 * When a snapshot is taken, the link list of the max_tr is swapped
188 * with the link list of the global_trace and the buffers are reset for
189 * the global_trace so the tracing can continue.
190 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200191static struct trace_array max_tr;
192
193static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
194
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200195/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200196static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200197
Steven Rostedt90369902008-11-05 16:05:44 -0500198/**
199 * tracing_is_enabled - return tracer_enabled status
200 *
201 * This function is used by other tracers to know the status
202 * of the tracer_enabled flag. Tracers may use this function
203 * to know if it should enable their features when starting
204 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
205 */
206int tracing_is_enabled(void)
207{
208 return tracer_enabled;
209}
210
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200211/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400212 * trace_buf_size is the size in bytes that is allocated
213 * for a buffer. Note, the number of bytes is always rounded
214 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400215 *
216 * This number is purposely set to a low number of 16384.
217 * If the dump on oops happens, it will be much appreciated
218 * to not have to wait for all that output. Anyway this can be
219 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200220 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400221#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400222
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400223static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200224
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200225/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200226static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200227
228/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200229static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200230
231/*
232 * max_tracer_type_len is used to simplify the allocating of
233 * buffers to read userspace tracer names. We keep track of
234 * the longest tracer name registered.
235 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200236static int max_tracer_type_len;
237
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200238/*
239 * trace_types_lock is used to protect the trace_types list.
240 * This lock is also used to keep user access serialized.
241 * Accesses from userspace will grab this lock while userspace
242 * activities happen inside the kernel.
243 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200244static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200245
246/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200247static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
248
Steven Rostedtee6bce52008-11-12 17:52:37 -0500249/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500250unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200251 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
Ingo Molnar4e655512008-05-12 21:20:52 +0200252
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200253/**
254 * trace_wake_up - wake up tasks waiting for trace input
255 *
256 * Simply wakes up any task that is blocked on the trace_wait
257 * queue. These is used with trace_poll for tasks polling the trace.
258 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200259void trace_wake_up(void)
260{
Ingo Molnar017730c2008-05-12 21:20:52 +0200261 /*
262 * The runqueue_is_locked() can fail, but this is the best we
263 * have for now:
264 */
265 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200266 wake_up(&trace_wait);
267}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200268
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400269static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200270{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400271 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200272 int ret;
273
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200274 if (!str)
275 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400276 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200277 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400278 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200279 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400280 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200281 return 1;
282}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400283__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200284
Steven Rostedt57f50be2008-05-12 21:20:44 +0200285unsigned long nsecs_to_usecs(unsigned long nsecs)
286{
287 return nsecs / 1000;
288}
289
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200290/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200291static const char *trace_options[] = {
292 "print-parent",
293 "sym-offset",
294 "sym-addr",
295 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200296 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200297 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200298 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200299 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200300 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200301 "sched-tree",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100302 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500303 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500304 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500305 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200306 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200307 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100308 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200309 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500310 "latency-format",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200311 NULL
312};
313
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200314/*
315 * ftrace_max_lock is used to protect the swapping of buffers
316 * when taking a max snapshot. The buffers themselves are
317 * protected by per_cpu spinlocks. But the action of the swap
318 * needs its own lock.
319 *
320 * This is defined as a raw_spinlock_t in order to help
321 * with performance when lockdep debugging is enabled.
322 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200323static raw_spinlock_t ftrace_max_lock =
324 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200325
326/*
327 * Copy the new maximum trace into the separate maximum-trace
328 * structure. (this way the maximum trace is permanently saved,
329 * for later retrieval via /debugfs/tracing/latency_trace)
330 */
Ingo Molnare309b412008-05-12 21:20:51 +0200331static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200332__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
333{
334 struct trace_array_cpu *data = tr->data[cpu];
335
336 max_tr.cpu = cpu;
337 max_tr.time_start = data->preempt_timestamp;
338
339 data = max_tr.data[cpu];
340 data->saved_latency = tracing_max_latency;
341
342 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
343 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100344 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200345 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
346 data->policy = tsk->policy;
347 data->rt_priority = tsk->rt_priority;
348
349 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500350 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200351}
352
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200353ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
354{
355 int len;
356 int ret;
357
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500358 if (!cnt)
359 return 0;
360
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200361 if (s->len <= s->readpos)
362 return -EBUSY;
363
364 len = s->len - s->readpos;
365 if (cnt > len)
366 cnt = len;
367 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500368 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200369 return -EFAULT;
370
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500371 cnt -= ret;
372
Steven Rostedte74da522009-03-04 20:31:11 -0500373 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200374 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200375}
376
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200377ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
378{
379 int len;
380 void *ret;
381
382 if (s->len <= s->readpos)
383 return -EBUSY;
384
385 len = s->len - s->readpos;
386 if (cnt > len)
387 cnt = len;
388 ret = memcpy(buf, s->buffer + s->readpos, cnt);
389 if (!ret)
390 return -EFAULT;
391
Steven Rostedte74da522009-03-04 20:31:11 -0500392 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200393 return cnt;
394}
395
Ingo Molnare309b412008-05-12 21:20:51 +0200396static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200397trace_print_seq(struct seq_file *m, struct trace_seq *s)
398{
399 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
400
401 s->buffer[len] = 0;
402 seq_puts(m, s->buffer);
403
Steven Rostedtf9520752009-03-02 14:04:40 -0500404 trace_seq_init(s);
Steven Rostedt214023c2008-05-12 21:20:46 +0200405}
406
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200407/**
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
409 * @tr: tracer
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
412 *
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
415 */
Ingo Molnare309b412008-05-12 21:20:51 +0200416void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200417update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
418{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400419 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200420
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200421 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200422 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400423
424 tr->buffer = max_tr.buffer;
425 max_tr.buffer = buf;
426
Steven Rostedtd7690412008-10-01 00:29:53 -0400427 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400428 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400429 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200430
431 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200432 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200433}
434
435/**
436 * update_max_tr_single - only copy one trace over, and reset the rest
437 * @tr - tracer
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200440 *
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200442 */
Ingo Molnare309b412008-05-12 21:20:51 +0200443void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200444update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
445{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400446 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200447
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200448 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200449 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200450
Steven Rostedtd7690412008-10-01 00:29:53 -0400451 ftrace_disable_cpu();
452
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
455
Steven Rostedtd7690412008-10-01 00:29:53 -0400456 ftrace_enable_cpu();
457
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500458 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200459
460 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200461 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200462}
463
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200464/**
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
467 *
468 * Register a new plugin tracer.
469 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200470int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100471__releases(kernel_lock)
472__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200473{
474 struct tracer *t;
475 int len;
476 int ret = 0;
477
478 if (!type->name) {
479 pr_info("Tracer must have a name\n");
480 return -1;
481 }
482
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100483 /*
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
488 */
489 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200490 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100491
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100492 tracing_selftest_running = true;
493
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
496 /* already found */
497 pr_info("Trace %s already registered\n",
498 type->name);
499 ret = -1;
500 goto out;
501 }
502 }
503
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100504 if (!type->set_flag)
505 type->set_flag = &dummy_set_flag;
506 if (!type->flags)
507 type->flags = &dummy_tracer_flags;
508 else
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
513
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100514
Steven Rostedt60a11772008-05-12 21:20:44 +0200515#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500516 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200517 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200518 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200519 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100520
Steven Rostedt60a11772008-05-12 21:20:44 +0200521 /*
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
527 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100528 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400529 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100530
Steven Rostedt60a11772008-05-12 21:20:44 +0200531 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200537 if (ret) {
538 printk(KERN_CONT "FAILED!\n");
539 goto out;
540 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200541 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100542 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400543 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100544
Steven Rostedt60a11772008-05-12 21:20:44 +0200545 printk(KERN_CONT "PASSED\n");
546 }
547#endif
548
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200549 type->next = trace_types;
550 trace_types = type;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200554
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200555 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100556 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200557 mutex_unlock(&trace_types_lock);
558
Steven Rostedtdac74942009-02-05 01:13:38 -0500559 if (ret || !default_bootup_tracer)
560 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500561
Steven Rostedtdac74942009-02-05 01:13:38 -0500562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
563 goto out_unlock;
564
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571#ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
573 type->name);
574#endif
575
576 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500577 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200578 return ret;
579}
580
581void unregister_tracer(struct tracer *type)
582{
583 struct tracer **t;
584 int len;
585
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
588 if (*t == type)
589 goto found;
590 }
591 pr_info("Trace %s not registered\n", type->name);
592 goto out;
593
594 found:
595 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200596
597 if (type == current_trace && tracer_enabled) {
598 tracer_enabled = 0;
599 tracing_stop();
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
603 }
604
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200605 if (strlen(type->name) != max_tracer_type_len)
606 goto out;
607
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
613 }
614 out:
615 mutex_unlock(&trace_types_lock);
616}
617
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400618void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200619{
Steven Rostedtd7690412008-10-01 00:29:53 -0400620 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400621 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400622 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200623}
624
Pekka J Enberg213cc062008-12-19 12:08:39 +0200625void tracing_reset_online_cpus(struct trace_array *tr)
626{
627 int cpu;
628
629 tr->time_start = ftrace_now(tr->cpu);
630
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
633}
634
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200635#define SAVED_CMDLINES 128
636static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
637static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
638static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
639static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100640static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200641
Steven Rostedt25b0b442008-05-12 21:21:00 +0200642/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100643static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200644
645static void trace_init_cmdlines(void)
646{
647 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
648 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
649 cmdline_idx = 0;
650}
651
Steven Rostedt0f048702008-11-05 16:05:44 -0500652static int trace_stop_count;
653static DEFINE_SPINLOCK(tracing_start_lock);
654
655/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500656 * ftrace_off_permanent - disable all ftrace code permanently
657 *
658 * This should only be called when a serious anomally has
659 * been detected. This will turn off the function tracing,
660 * ring buffers, and other tracing utilites. It takes no
661 * locks and can be called from any context.
662 */
663void ftrace_off_permanent(void)
664{
665 tracing_disabled = 1;
666 ftrace_stop();
667 tracing_off_permanent();
668}
669
670/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500671 * tracing_start - quick start of the tracer
672 *
673 * If tracing is enabled but was stopped by tracing_stop,
674 * this will start the tracer back up.
675 */
676void tracing_start(void)
677{
678 struct ring_buffer *buffer;
679 unsigned long flags;
680
681 if (tracing_disabled)
682 return;
683
684 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500685 if (--trace_stop_count) {
686 if (trace_stop_count < 0) {
687 /* Someone screwed up their debugging */
688 WARN_ON_ONCE(1);
689 trace_stop_count = 0;
690 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500691 goto out;
692 }
693
694
695 buffer = global_trace.buffer;
696 if (buffer)
697 ring_buffer_record_enable(buffer);
698
699 buffer = max_tr.buffer;
700 if (buffer)
701 ring_buffer_record_enable(buffer);
702
703 ftrace_start();
704 out:
705 spin_unlock_irqrestore(&tracing_start_lock, flags);
706}
707
708/**
709 * tracing_stop - quick stop of the tracer
710 *
711 * Light weight way to stop tracing. Use in conjunction with
712 * tracing_start.
713 */
714void tracing_stop(void)
715{
716 struct ring_buffer *buffer;
717 unsigned long flags;
718
719 ftrace_stop();
720 spin_lock_irqsave(&tracing_start_lock, flags);
721 if (trace_stop_count++)
722 goto out;
723
724 buffer = global_trace.buffer;
725 if (buffer)
726 ring_buffer_record_disable(buffer);
727
728 buffer = max_tr.buffer;
729 if (buffer)
730 ring_buffer_record_disable(buffer);
731
732 out:
733 spin_unlock_irqrestore(&tracing_start_lock, flags);
734}
735
Ingo Molnare309b412008-05-12 21:20:51 +0200736void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200737
Ingo Molnare309b412008-05-12 21:20:51 +0200738static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200739{
740 unsigned map;
741 unsigned idx;
742
743 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
744 return;
745
746 /*
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
751 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100752 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200753 return;
754
755 idx = map_pid_to_cmdline[tsk->pid];
756 if (idx >= SAVED_CMDLINES) {
757 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
758
759 map = map_cmdline_to_pid[idx];
760 if (map <= PID_MAX_DEFAULT)
761 map_pid_to_cmdline[map] = (unsigned)-1;
762
763 map_pid_to_cmdline[tsk->pid] = idx;
764
765 cmdline_idx = idx;
766 }
767
768 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
769
Peter Zijlstraefed7922009-03-04 12:32:55 +0100770 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200771}
772
Steven Rostedt4ca530852009-03-16 19:20:15 -0400773void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200774{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200775 unsigned map;
776
Steven Rostedt4ca530852009-03-16 19:20:15 -0400777 if (!pid) {
778 strcpy(comm, "<idle>");
779 return;
780 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781
Steven Rostedt4ca530852009-03-16 19:20:15 -0400782 if (pid > PID_MAX_DEFAULT) {
783 strcpy(comm, "<...>");
784 return;
785 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200786
Steven Rostedt4ca530852009-03-16 19:20:15 -0400787 __raw_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200788 map = map_pid_to_cmdline[pid];
789 if (map >= SAVED_CMDLINES)
790 goto out;
791
Steven Rostedt4ca530852009-03-16 19:20:15 -0400792 strcpy(comm, saved_cmdlines[map]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793
794 out:
Steven Rostedt4ca530852009-03-16 19:20:15 -0400795 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200796}
797
Ingo Molnare309b412008-05-12 21:20:51 +0200798void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200799{
800 if (atomic_read(&trace_record_cmdline_disabled))
801 return;
802
803 trace_save_cmdline(tsk);
804}
805
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300806void
Steven Rostedt38697052008-10-01 13:14:09 -0400807tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
808 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200809{
810 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200811
Steven Rostedt777e2082008-09-29 23:02:42 -0400812 entry->preempt_count = pc & 0xff;
813 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedtef180122009-03-10 14:10:56 -0400814 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400815 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400816#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400817 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400818#else
819 TRACE_FLAG_IRQS_NOSUPPORT |
820#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200821 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
822 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
823 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
824}
825
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200826struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
827 unsigned char type,
828 unsigned long len,
829 unsigned long flags, int pc)
830{
831 struct ring_buffer_event *event;
832
833 event = ring_buffer_lock_reserve(tr->buffer, len);
834 if (event != NULL) {
835 struct trace_entry *ent = ring_buffer_event_data(event);
836
837 tracing_generic_entry_update(ent, flags, pc);
838 ent->type = type;
839 }
840
841 return event;
842}
843static void ftrace_trace_stack(struct trace_array *tr,
844 unsigned long flags, int skip, int pc);
845static void ftrace_trace_userstack(struct trace_array *tr,
846 unsigned long flags, int pc);
847
848void trace_buffer_unlock_commit(struct trace_array *tr,
849 struct ring_buffer_event *event,
850 unsigned long flags, int pc)
851{
852 ring_buffer_unlock_commit(tr->buffer, event);
853
854 ftrace_trace_stack(tr, flags, 6, pc);
855 ftrace_trace_userstack(tr, flags, pc);
856 trace_wake_up();
857}
858
Steven Rostedtef5580d2009-02-27 19:38:04 -0500859struct ring_buffer_event *
860trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
861 unsigned long flags, int pc)
862{
863 return trace_buffer_lock_reserve(&global_trace,
864 type, len, flags, pc);
865}
866
867void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
868 unsigned long flags, int pc)
869{
870 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
871}
872
Ingo Molnare309b412008-05-12 21:20:51 +0200873void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500874trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400875 unsigned long ip, unsigned long parent_ip, unsigned long flags,
876 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200877{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400878 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400879 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200880
Steven Rostedtd7690412008-10-01 00:29:53 -0400881 /* If we are reading the ring buffer, don't trace */
882 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
883 return;
884
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200885 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
886 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400887 if (!event)
888 return;
889 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400890 entry->ip = ip;
891 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200892 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200893}
894
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100895#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100896static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100897 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100898 unsigned long flags,
899 int pc)
900{
901 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100902 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100903
904 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
905 return;
906
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200907 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
908 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100909 if (!event)
910 return;
911 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100912 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200913 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100914}
915
916static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100917 struct ftrace_graph_ret *trace,
918 unsigned long flags,
919 int pc)
920{
921 struct ring_buffer_event *event;
922 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100923
924 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
925 return;
926
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200927 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
928 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100929 if (!event)
930 return;
931 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100932 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200933 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100934}
935#endif
936
Ingo Molnare309b412008-05-12 21:20:51 +0200937void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200938ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400939 unsigned long ip, unsigned long parent_ip, unsigned long flags,
940 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200941{
942 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500943 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200944}
945
Steven Rostedt53614992009-01-15 19:12:40 -0500946static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500947 unsigned long flags,
948 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200949{
Al Viroc2c80522008-10-31 19:50:41 +0000950#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400951 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400952 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200953 struct stack_trace trace;
954
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200955 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
956 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400957 if (!event)
958 return;
959 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400960 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200961
962 trace.nr_entries = 0;
963 trace.max_entries = FTRACE_STACK_ENTRIES;
964 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400965 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200966
967 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200968 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000969#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200970}
971
Steven Rostedt53614992009-01-15 19:12:40 -0500972static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500973 unsigned long flags,
974 int skip, int pc)
975{
976 if (!(trace_flags & TRACE_ITER_STACKTRACE))
977 return;
978
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500979 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500980}
981
Steven Rostedt38697052008-10-01 13:14:09 -0400982void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400983 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500984 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400985{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500986 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400987}
988
Török Edwin02b67512008-11-22 13:28:47 +0200989static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500990 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +0200991{
Török Edwinc7425ac2008-11-28 11:17:56 +0200992#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +0200993 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +0200994 struct userstack_entry *entry;
995 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +0200996
997 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
998 return;
999
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001000 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1001 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001002 if (!event)
1003 return;
1004 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001005
1006 memset(&entry->caller, 0, sizeof(entry->caller));
1007
1008 trace.nr_entries = 0;
1009 trace.max_entries = FTRACE_STACK_ENTRIES;
1010 trace.skip = 0;
1011 trace.entries = entry->caller;
1012
1013 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001014 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001015#endif
Török Edwin02b67512008-11-22 13:28:47 +02001016}
1017
Hannes Eder4fd27352009-02-10 19:44:12 +01001018#ifdef UNUSED
1019static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001020{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001021 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001022}
Hannes Eder4fd27352009-02-10 19:44:12 +01001023#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001024
Steven Rostedt38697052008-10-01 13:14:09 -04001025static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001026ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001027 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1028 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +02001029{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001030 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001031 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001032 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001033
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001034 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1035 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001036 if (!event)
1037 return;
1038 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001039 entry->arg1 = arg1;
1040 entry->arg2 = arg2;
1041 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001042 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +02001043}
1044
1045void
Steven Rostedt38697052008-10-01 13:14:09 -04001046__trace_special(void *__tr, void *__data,
1047 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1048{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001049 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001050}
1051
1052void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001053tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001054 struct task_struct *prev,
1055 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001056 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001057{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001058 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001059 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001060
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001061 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1062 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001063 if (!event)
1064 return;
1065 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001066 entry->prev_pid = prev->pid;
1067 entry->prev_prio = prev->prio;
1068 entry->prev_state = prev->state;
1069 entry->next_pid = next->pid;
1070 entry->next_prio = next->prio;
1071 entry->next_state = next->state;
1072 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001073 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001074}
1075
Ingo Molnar57422792008-05-12 21:20:51 +02001076void
1077tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001078 struct task_struct *wakee,
1079 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001080 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001081{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001082 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001083 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001084
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001085 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1086 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001087 if (!event)
1088 return;
1089 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001090 entry->prev_pid = curr->pid;
1091 entry->prev_prio = curr->prio;
1092 entry->prev_state = curr->state;
1093 entry->next_pid = wakee->pid;
1094 entry->next_prio = wakee->prio;
1095 entry->next_state = wakee->state;
1096 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001097
1098 ring_buffer_unlock_commit(tr->buffer, event);
1099 ftrace_trace_stack(tr, flags, 6, pc);
1100 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001101}
1102
Steven Rostedt4902f882008-05-22 00:22:18 -04001103void
1104ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1105{
1106 struct trace_array *tr = &global_trace;
1107 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001108 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001109 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001110 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001111
Steven Rostedtc76f0692008-11-07 22:36:02 -05001112 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001113 return;
1114
Steven Rostedt38697052008-10-01 13:14:09 -04001115 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001116 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001117 cpu = raw_smp_processor_id();
1118 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001119
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001120 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001121 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001122
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001123 atomic_dec(&data->disabled);
1124 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001125}
1126
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001127#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001128int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001129{
1130 struct trace_array *tr = &global_trace;
1131 struct trace_array_cpu *data;
1132 unsigned long flags;
1133 long disabled;
1134 int cpu;
1135 int pc;
1136
Steven Rostedt804a6852008-12-03 15:36:59 -05001137 if (!ftrace_trace_task(current))
1138 return 0;
1139
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001140 if (!ftrace_graph_addr(trace->func))
1141 return 0;
1142
Steven Rostedta5e25882008-12-02 15:34:05 -05001143 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001144 cpu = raw_smp_processor_id();
1145 data = tr->data[cpu];
1146 disabled = atomic_inc_return(&data->disabled);
1147 if (likely(disabled == 1)) {
1148 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001149 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001150 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001151 /* Only do the atomic if it is not already set */
1152 if (!test_tsk_trace_graph(current))
1153 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001154 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001155 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001156
1157 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001158}
1159
1160void trace_graph_return(struct ftrace_graph_ret *trace)
1161{
1162 struct trace_array *tr = &global_trace;
1163 struct trace_array_cpu *data;
1164 unsigned long flags;
1165 long disabled;
1166 int cpu;
1167 int pc;
1168
Steven Rostedta5e25882008-12-02 15:34:05 -05001169 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001170 cpu = raw_smp_processor_id();
1171 data = tr->data[cpu];
1172 disabled = atomic_inc_return(&data->disabled);
1173 if (likely(disabled == 1)) {
1174 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001175 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001176 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001177 if (!trace->depth)
1178 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001179 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001180 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001181}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001182#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001183
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001184
1185/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001186 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001187 *
1188 */
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001189int trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001190{
Steven Rostedt80370cb2009-03-10 17:16:35 -04001191 static raw_spinlock_t trace_buf_lock =
1192 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001193 static u32 trace_buf[TRACE_BUF_SIZE];
1194
1195 struct ring_buffer_event *event;
1196 struct trace_array *tr = &global_trace;
1197 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001198 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001199 unsigned long flags;
1200 int resched;
1201 int cpu, len = 0, size, pc;
1202
1203 if (unlikely(tracing_selftest_running || tracing_disabled))
1204 return 0;
1205
1206 /* Don't pollute graph traces with trace_vprintk internals */
1207 pause_graph_tracing();
1208
1209 pc = preempt_count();
1210 resched = ftrace_preempt_disable();
1211 cpu = raw_smp_processor_id();
1212 data = tr->data[cpu];
1213
1214 if (unlikely(atomic_read(&data->disabled)))
1215 goto out;
1216
Steven Rostedt80370cb2009-03-10 17:16:35 -04001217 /* Lockdep uses trace_printk for lock tracing */
1218 local_irq_save(flags);
1219 __raw_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001220 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1221
1222 if (len > TRACE_BUF_SIZE || len < 0)
1223 goto out_unlock;
1224
1225 size = sizeof(*entry) + sizeof(u32) * len;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001226 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001227 if (!event)
1228 goto out_unlock;
1229 entry = ring_buffer_event_data(event);
1230 entry->ip = ip;
1231 entry->depth = depth;
1232 entry->fmt = fmt;
1233
1234 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1235 ring_buffer_unlock_commit(tr->buffer, event);
1236
1237out_unlock:
Steven Rostedt80370cb2009-03-10 17:16:35 -04001238 __raw_spin_unlock(&trace_buf_lock);
1239 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001240
1241out:
1242 ftrace_preempt_enable(resched);
1243 unpause_graph_tracing();
1244
1245 return len;
1246}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001247EXPORT_SYMBOL_GPL(trace_vbprintk);
1248
1249int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1250{
1251 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1252 static char trace_buf[TRACE_BUF_SIZE];
1253
1254 struct ring_buffer_event *event;
1255 struct trace_array *tr = &global_trace;
1256 struct trace_array_cpu *data;
1257 int cpu, len = 0, size, pc;
1258 struct print_entry *entry;
1259 unsigned long irq_flags;
1260
1261 if (tracing_disabled || tracing_selftest_running)
1262 return 0;
1263
1264 pc = preempt_count();
1265 preempt_disable_notrace();
1266 cpu = raw_smp_processor_id();
1267 data = tr->data[cpu];
1268
1269 if (unlikely(atomic_read(&data->disabled)))
1270 goto out;
1271
1272 pause_graph_tracing();
1273 raw_local_irq_save(irq_flags);
1274 __raw_spin_lock(&trace_buf_lock);
1275 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1276
1277 len = min(len, TRACE_BUF_SIZE-1);
1278 trace_buf[len] = 0;
1279
1280 size = sizeof(*entry) + len + 1;
1281 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1282 if (!event)
1283 goto out_unlock;
1284 entry = ring_buffer_event_data(event);
1285 entry->ip = ip;
1286 entry->depth = depth;
1287
1288 memcpy(&entry->buf, trace_buf, len);
1289 entry->buf[len] = 0;
1290 ring_buffer_unlock_commit(tr->buffer, event);
1291
1292 out_unlock:
1293 __raw_spin_unlock(&trace_buf_lock);
1294 raw_local_irq_restore(irq_flags);
1295 unpause_graph_tracing();
1296 out:
1297 preempt_enable_notrace();
1298
1299 return len;
1300}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001301EXPORT_SYMBOL_GPL(trace_vprintk);
1302
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001303enum trace_file_type {
1304 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001305 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001306};
1307
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001308static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001309{
Steven Rostedtd7690412008-10-01 00:29:53 -04001310 /* Don't allow ftrace to trace into the ring buffers */
1311 ftrace_disable_cpu();
1312
Steven Rostedt5a90f572008-09-03 17:42:51 -04001313 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001314 if (iter->buffer_iter[iter->cpu])
1315 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1316
1317 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001318}
1319
Ingo Molnare309b412008-05-12 21:20:51 +02001320static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001321peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001322{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001323 struct ring_buffer_event *event;
1324 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001325
Steven Rostedtd7690412008-10-01 00:29:53 -04001326 /* Don't allow ftrace to trace into the ring buffers */
1327 ftrace_disable_cpu();
1328
1329 if (buf_iter)
1330 event = ring_buffer_iter_peek(buf_iter, ts);
1331 else
1332 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1333
1334 ftrace_enable_cpu();
1335
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001336 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001337}
Steven Rostedtd7690412008-10-01 00:29:53 -04001338
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001339static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001340__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001341{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001342 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001343 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001344 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001345 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001346 int next_cpu = -1;
1347 int cpu;
1348
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001349 /*
1350 * If we are in a per_cpu trace file, don't bother by iterating over
1351 * all cpu and peek directly.
1352 */
1353 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1354 if (ring_buffer_empty_cpu(buffer, cpu_file))
1355 return NULL;
1356 ent = peek_next_entry(iter, cpu_file, ent_ts);
1357 if (ent_cpu)
1358 *ent_cpu = cpu_file;
1359
1360 return ent;
1361 }
1362
Steven Rostedtab464282008-05-12 21:21:00 +02001363 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001364
1365 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001366 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001367
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001368 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001369
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001370 /*
1371 * Pick the entry with the smallest timestamp:
1372 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001373 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001374 next = ent;
1375 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001376 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001377 }
1378 }
1379
1380 if (ent_cpu)
1381 *ent_cpu = next_cpu;
1382
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001383 if (ent_ts)
1384 *ent_ts = next_ts;
1385
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386 return next;
1387}
1388
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001389/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001390struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1391 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001392{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001393 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001394}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001395
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001396/* Find the next real entry, and increment the iterator to the next entry */
1397static void *find_next_entry_inc(struct trace_iterator *iter)
1398{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001399 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001400
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001401 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001402 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001403
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001404 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001405}
1406
Ingo Molnare309b412008-05-12 21:20:51 +02001407static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001408{
Steven Rostedtd7690412008-10-01 00:29:53 -04001409 /* Don't allow ftrace to trace into the ring buffers */
1410 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001411 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001412 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001413}
1414
Ingo Molnare309b412008-05-12 21:20:51 +02001415static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001416{
1417 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001418 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001419 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001420
1421 (*pos)++;
1422
1423 /* can't go backwards */
1424 if (iter->idx > i)
1425 return NULL;
1426
1427 if (iter->idx < 0)
1428 ent = find_next_entry_inc(iter);
1429 else
1430 ent = iter;
1431
1432 while (ent && iter->idx < i)
1433 ent = find_next_entry_inc(iter);
1434
1435 iter->pos = *pos;
1436
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001437 return ent;
1438}
1439
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001440/*
1441 * No necessary locking here. The worst thing which can
1442 * happen is loosing events consumed at the same time
1443 * by a trace_pipe reader.
1444 * Other than that, we don't risk to crash the ring buffer
1445 * because it serializes the readers.
1446 *
1447 * The current tracer is copied to avoid a global locking
1448 * all around.
1449 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001450static void *s_start(struct seq_file *m, loff_t *pos)
1451{
1452 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001453 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001454 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 void *p = NULL;
1456 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001457 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001458
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001459 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001460 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001461 if (unlikely(old_tracer != current_trace && current_trace)) {
1462 old_tracer = current_trace;
1463 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001464 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001465 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001466
1467 atomic_inc(&trace_record_cmdline_disabled);
1468
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001469 if (*pos != iter->pos) {
1470 iter->ent = NULL;
1471 iter->cpu = 0;
1472 iter->idx = -1;
1473
Steven Rostedtd7690412008-10-01 00:29:53 -04001474 ftrace_disable_cpu();
1475
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001476 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1477 for_each_tracing_cpu(cpu)
1478 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1479 } else
1480 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1481
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001482
Steven Rostedtd7690412008-10-01 00:29:53 -04001483 ftrace_enable_cpu();
1484
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001485 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1486 ;
1487
1488 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001489 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001490 p = s_next(m, p, &l);
1491 }
1492
1493 return p;
1494}
1495
1496static void s_stop(struct seq_file *m, void *p)
1497{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001498 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001499}
1500
Ingo Molnare309b412008-05-12 21:20:51 +02001501static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001502{
Michael Ellermana6168352008-08-20 16:36:11 -07001503 seq_puts(m, "# _------=> CPU# \n");
1504 seq_puts(m, "# / _-----=> irqs-off \n");
1505 seq_puts(m, "# | / _----=> need-resched \n");
1506 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1507 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1508 seq_puts(m, "# |||| / \n");
1509 seq_puts(m, "# ||||| delay \n");
1510 seq_puts(m, "# cmd pid ||||| time | caller \n");
1511 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001512}
1513
Ingo Molnare309b412008-05-12 21:20:51 +02001514static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001515{
Michael Ellermana6168352008-08-20 16:36:11 -07001516 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1517 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001518}
1519
1520
Ingo Molnare309b412008-05-12 21:20:51 +02001521static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001522print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1523{
1524 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1525 struct trace_array *tr = iter->tr;
1526 struct trace_array_cpu *data = tr->data[tr->cpu];
1527 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001528 unsigned long total;
1529 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001530 const char *name = "preemption";
1531
1532 if (type)
1533 name = type->name;
1534
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001535 entries = ring_buffer_entries(iter->tr->buffer);
1536 total = entries +
1537 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001538
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001539 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001540 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001541 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001543 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001544 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001545 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001546 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001547 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001548 tr->cpu,
1549#if defined(CONFIG_PREEMPT_NONE)
1550 "server",
1551#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1552 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001553#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001554 "preempt",
1555#else
1556 "unknown",
1557#endif
1558 /* These are reserved for later use */
1559 0, 0, 0, 0);
1560#ifdef CONFIG_SMP
1561 seq_printf(m, " #P:%d)\n", num_online_cpus());
1562#else
1563 seq_puts(m, ")\n");
1564#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001565 seq_puts(m, "# -----------------\n");
1566 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001567 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1568 data->comm, data->pid, data->uid, data->nice,
1569 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001570 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001571
1572 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001573 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001574 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1575 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001576 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001577 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1578 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001579 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580 }
1581
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001582 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001583}
1584
Steven Rostedta3097202008-11-07 22:36:02 -05001585static void test_cpu_buff_start(struct trace_iterator *iter)
1586{
1587 struct trace_seq *s = &iter->seq;
1588
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001589 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1590 return;
1591
1592 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1593 return;
1594
Rusty Russell44623442009-01-01 10:12:23 +10301595 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001596 return;
1597
Rusty Russell44623442009-01-01 10:12:23 +10301598 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001599 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1600}
1601
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001602static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001603{
Steven Rostedt214023c2008-05-12 21:20:46 +02001604 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001605 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001606 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001607 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001608
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001609 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001610
Steven Rostedta3097202008-11-07 22:36:02 -05001611 test_cpu_buff_start(iter);
1612
Steven Rostedtf633cef2008-12-23 23:24:13 -05001613 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001614
1615 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001616 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1617 if (!trace_print_lat_context(iter))
1618 goto partial;
1619 } else {
1620 if (!trace_print_context(iter))
1621 goto partial;
1622 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001623 }
1624
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001625 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001626 return event->trace(iter, sym_flags);
1627
1628 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1629 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001630
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001631 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001632partial:
1633 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001634}
1635
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001636static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001637{
1638 struct trace_seq *s = &iter->seq;
1639 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001640 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001641
1642 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001643
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001644 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001645 if (!trace_seq_printf(s, "%d %d %llu ",
1646 entry->pid, iter->cpu, iter->ts))
1647 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001648 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001649
Steven Rostedtf633cef2008-12-23 23:24:13 -05001650 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001651 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001652 return event->raw(iter, 0);
1653
1654 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1655 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001656
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001657 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001658partial:
1659 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001660}
1661
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001662static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001663{
1664 struct trace_seq *s = &iter->seq;
1665 unsigned char newline = '\n';
1666 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001667 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001668
1669 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001670
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001671 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1672 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1673 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1674 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1675 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001676
Steven Rostedtf633cef2008-12-23 23:24:13 -05001677 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001678 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001679 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001680 if (ret != TRACE_TYPE_HANDLED)
1681 return ret;
1682 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001683
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001684 SEQ_PUT_FIELD_RET(s, newline);
1685
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001686 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001687}
1688
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001689static enum print_line_t print_bprintk_msg_only(struct trace_iterator *iter)
1690{
1691 struct trace_seq *s = &iter->seq;
1692 struct trace_entry *entry = iter->ent;
1693 struct bprint_entry *field;
1694 int ret;
1695
1696 trace_assign_type(field, entry);
1697
1698 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1699 if (!ret)
1700 return TRACE_TYPE_PARTIAL_LINE;
1701
1702 return TRACE_TYPE_HANDLED;
1703}
1704
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001705static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1706{
1707 struct trace_seq *s = &iter->seq;
1708 struct trace_entry *entry = iter->ent;
1709 struct print_entry *field;
1710 int ret;
1711
1712 trace_assign_type(field, entry);
1713
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001714 ret = trace_seq_printf(s, "%s", field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001715 if (!ret)
1716 return TRACE_TYPE_PARTIAL_LINE;
1717
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001718 return TRACE_TYPE_HANDLED;
1719}
1720
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001721static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001722{
1723 struct trace_seq *s = &iter->seq;
1724 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001725 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001726
1727 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001728
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001729 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1730 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001731 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001732 SEQ_PUT_FIELD_RET(s, iter->ts);
1733 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001734
Steven Rostedtf633cef2008-12-23 23:24:13 -05001735 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001736 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001737}
1738
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001739static int trace_empty(struct trace_iterator *iter)
1740{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001741 int cpu;
1742
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001743 /* If we are looking at one CPU buffer, only check that one */
1744 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1745 cpu = iter->cpu_file;
1746 if (iter->buffer_iter[cpu]) {
1747 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1748 return 0;
1749 } else {
1750 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1751 return 0;
1752 }
1753 return 1;
1754 }
1755
Steven Rostedtab464282008-05-12 21:21:00 +02001756 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001757 if (iter->buffer_iter[cpu]) {
1758 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1759 return 0;
1760 } else {
1761 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1762 return 0;
1763 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001764 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001765
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001766 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001767}
1768
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001769static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001770{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001771 enum print_line_t ret;
1772
1773 if (iter->trace && iter->trace->print_line) {
1774 ret = iter->trace->print_line(iter);
1775 if (ret != TRACE_TYPE_UNHANDLED)
1776 return ret;
1777 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001778
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001779 if (iter->ent->type == TRACE_BPRINT &&
1780 trace_flags & TRACE_ITER_PRINTK &&
1781 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1782 return print_bprintk_msg_only(iter);
1783
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001784 if (iter->ent->type == TRACE_PRINT &&
1785 trace_flags & TRACE_ITER_PRINTK &&
1786 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1787 return print_printk_msg_only(iter);
1788
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001789 if (trace_flags & TRACE_ITER_BIN)
1790 return print_bin_fmt(iter);
1791
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001792 if (trace_flags & TRACE_ITER_HEX)
1793 return print_hex_fmt(iter);
1794
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001795 if (trace_flags & TRACE_ITER_RAW)
1796 return print_raw_fmt(iter);
1797
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001798 return print_trace_fmt(iter);
1799}
1800
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001801static int s_show(struct seq_file *m, void *v)
1802{
1803 struct trace_iterator *iter = v;
1804
1805 if (iter->ent == NULL) {
1806 if (iter->tr) {
1807 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1808 seq_puts(m, "#\n");
1809 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001810 if (iter->trace && iter->trace->print_header)
1811 iter->trace->print_header(m);
1812 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001813 /* print nothing if the buffers are empty */
1814 if (trace_empty(iter))
1815 return 0;
1816 print_trace_header(m, iter);
1817 if (!(trace_flags & TRACE_ITER_VERBOSE))
1818 print_lat_help_header(m);
1819 } else {
1820 if (!(trace_flags & TRACE_ITER_VERBOSE))
1821 print_func_help_header(m);
1822 }
1823 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001824 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001825 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001826 }
1827
1828 return 0;
1829}
1830
1831static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001832 .start = s_start,
1833 .next = s_next,
1834 .stop = s_stop,
1835 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001836};
1837
Ingo Molnare309b412008-05-12 21:20:51 +02001838static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001839__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001840{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001841 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001842 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001843 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001844 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001845 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001846
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001847 if (tracing_disabled)
1848 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001849
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001850 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001851 if (!iter)
1852 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001853
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001854 /*
1855 * We make a copy of the current tracer to avoid concurrent
1856 * changes on it while we are reading.
1857 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001858 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001859 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001860 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001861 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001862
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001863 if (current_trace)
1864 *iter->trace = *current_trace;
1865
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001866 if (current_trace && current_trace->print_max)
1867 iter->tr = &max_tr;
1868 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001869 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001870 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001871 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001872 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001873
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001874 /* Notify the tracer early; before we stop tracing. */
1875 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001876 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001877
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001878 /* Annotate start of buffers if we had overruns */
1879 if (ring_buffer_overruns(iter->tr->buffer))
1880 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1881
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001882 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1883 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001884
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001885 iter->buffer_iter[cpu] =
1886 ring_buffer_read_start(iter->tr->buffer, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001887 }
1888 } else {
1889 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001890 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001891 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001892 }
1893
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001894 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001895 ret = seq_open(file, &tracer_seq_ops);
1896 if (ret < 0) {
1897 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001898 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001899 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001900
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001901 m = file->private_data;
1902 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001903
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001904 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001905 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001906
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001907 mutex_unlock(&trace_types_lock);
1908
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001909 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001910
1911 fail_buffer:
1912 for_each_tracing_cpu(cpu) {
1913 if (iter->buffer_iter[cpu])
1914 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1915 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001916 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001917 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001918 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001919 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001920
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001921 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001922}
1923
1924int tracing_open_generic(struct inode *inode, struct file *filp)
1925{
Steven Rostedt60a11772008-05-12 21:20:44 +02001926 if (tracing_disabled)
1927 return -ENODEV;
1928
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001929 filp->private_data = inode->i_private;
1930 return 0;
1931}
1932
Hannes Eder4fd27352009-02-10 19:44:12 +01001933static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001934{
1935 struct seq_file *m = (struct seq_file *)file->private_data;
1936 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001937 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001938
1939 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001940 for_each_tracing_cpu(cpu) {
1941 if (iter->buffer_iter[cpu])
1942 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1943 }
1944
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001945 if (iter->trace && iter->trace->close)
1946 iter->trace->close(iter);
1947
1948 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001949 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001950 mutex_unlock(&trace_types_lock);
1951
1952 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001953 mutex_destroy(&iter->mutex);
1954 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001955 kfree(iter);
1956 return 0;
1957}
1958
1959static int tracing_open(struct inode *inode, struct file *file)
1960{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001961 struct trace_iterator *iter;
1962 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001963
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001964 iter = __tracing_open(inode, file);
1965 if (IS_ERR(iter))
1966 ret = PTR_ERR(iter);
Steven Rostedtc032ef642009-03-04 20:34:24 -05001967 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001968 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1969
1970 return ret;
1971}
1972
Ingo Molnare309b412008-05-12 21:20:51 +02001973static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001974t_next(struct seq_file *m, void *v, loff_t *pos)
1975{
1976 struct tracer *t = m->private;
1977
1978 (*pos)++;
1979
1980 if (t)
1981 t = t->next;
1982
1983 m->private = t;
1984
1985 return t;
1986}
1987
1988static void *t_start(struct seq_file *m, loff_t *pos)
1989{
1990 struct tracer *t = m->private;
1991 loff_t l = 0;
1992
1993 mutex_lock(&trace_types_lock);
1994 for (; t && l < *pos; t = t_next(m, t, &l))
1995 ;
1996
1997 return t;
1998}
1999
2000static void t_stop(struct seq_file *m, void *p)
2001{
2002 mutex_unlock(&trace_types_lock);
2003}
2004
2005static int t_show(struct seq_file *m, void *v)
2006{
2007 struct tracer *t = v;
2008
2009 if (!t)
2010 return 0;
2011
2012 seq_printf(m, "%s", t->name);
2013 if (t->next)
2014 seq_putc(m, ' ');
2015 else
2016 seq_putc(m, '\n');
2017
2018 return 0;
2019}
2020
2021static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002022 .start = t_start,
2023 .next = t_next,
2024 .stop = t_stop,
2025 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002026};
2027
2028static int show_traces_open(struct inode *inode, struct file *file)
2029{
2030 int ret;
2031
Steven Rostedt60a11772008-05-12 21:20:44 +02002032 if (tracing_disabled)
2033 return -ENODEV;
2034
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002035 ret = seq_open(file, &show_traces_seq_ops);
2036 if (!ret) {
2037 struct seq_file *m = file->private_data;
2038 m->private = trace_types;
2039 }
2040
2041 return ret;
2042}
2043
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002044static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002045 .open = tracing_open,
2046 .read = seq_read,
2047 .llseek = seq_lseek,
2048 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002049};
2050
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002051static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002052 .open = show_traces_open,
2053 .read = seq_read,
2054 .release = seq_release,
2055};
2056
Ingo Molnar36dfe922008-05-12 21:20:52 +02002057/*
2058 * Only trace on a CPU if the bitmask is set:
2059 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302060static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002061
2062/*
2063 * The tracer itself will not take this lock, but still we want
2064 * to provide a consistent cpumask to user-space:
2065 */
2066static DEFINE_MUTEX(tracing_cpumask_update_lock);
2067
2068/*
2069 * Temporary storage for the character representation of the
2070 * CPU bitmask (and one more byte for the newline):
2071 */
2072static char mask_str[NR_CPUS + 1];
2073
Ingo Molnarc7078de2008-05-12 21:20:52 +02002074static ssize_t
2075tracing_cpumask_read(struct file *filp, char __user *ubuf,
2076 size_t count, loff_t *ppos)
2077{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002078 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002079
2080 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002081
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302082 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002083 if (count - len < 2) {
2084 count = -EINVAL;
2085 goto out_err;
2086 }
2087 len += sprintf(mask_str + len, "\n");
2088 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2089
2090out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002091 mutex_unlock(&tracing_cpumask_update_lock);
2092
2093 return count;
2094}
2095
2096static ssize_t
2097tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2098 size_t count, loff_t *ppos)
2099{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002100 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302101 cpumask_var_t tracing_cpumask_new;
2102
2103 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2104 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002105
2106 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302107 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002108 if (err)
2109 goto err_unlock;
2110
Steven Rostedta5e25882008-12-02 15:34:05 -05002111 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02002112 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002113 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002114 /*
2115 * Increase/decrease the disabled counter if we are
2116 * about to flip a bit in the cpumask:
2117 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302118 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2119 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002120 atomic_inc(&global_trace.data[cpu]->disabled);
2121 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302122 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2123 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002124 atomic_dec(&global_trace.data[cpu]->disabled);
2125 }
2126 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002127 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002128 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002129
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302130 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002131
Ingo Molnarc7078de2008-05-12 21:20:52 +02002132 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302133 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002134
Ingo Molnarc7078de2008-05-12 21:20:52 +02002135 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002136
2137err_unlock:
2138 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302139 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002140
2141 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002142}
2143
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002144static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002145 .open = tracing_open_generic,
2146 .read = tracing_cpumask_read,
2147 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002148};
2149
2150static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002151tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002152 size_t cnt, loff_t *ppos)
2153{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002154 struct tracer_opt *trace_opts;
2155 u32 tracer_flags;
2156 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002157 char *buf;
2158 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002159 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002160
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002161
Wenji Huangc3706f02009-02-10 01:03:18 -05002162 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002163 for (i = 0; trace_options[i]; i++) {
2164 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002165 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002166 }
2167
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002168 mutex_lock(&trace_types_lock);
2169 tracer_flags = current_trace->flags->val;
2170 trace_opts = current_trace->flags->opts;
2171
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002172 /*
2173 * Increase the size with names of options specific
2174 * of the current tracer.
2175 */
2176 for (i = 0; trace_opts[i].name; i++) {
2177 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002178 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002179 }
2180
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002181 /* +2 for \n and \0 */
2182 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002183 if (!buf) {
2184 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002185 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002186 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002187
2188 for (i = 0; trace_options[i]; i++) {
2189 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002190 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002191 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002192 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002193 }
2194
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002195 for (i = 0; trace_opts[i].name; i++) {
2196 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002197 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002198 trace_opts[i].name);
2199 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002200 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002201 trace_opts[i].name);
2202 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002203 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002204
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002205 WARN_ON(r >= len + 2);
2206
Ingo Molnar36dfe922008-05-12 21:20:52 +02002207 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002208
2209 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002210 return r;
2211}
2212
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002213/* Try to assign a tracer specific option */
2214static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2215{
2216 struct tracer_flags *trace_flags = trace->flags;
2217 struct tracer_opt *opts = NULL;
2218 int ret = 0, i = 0;
2219 int len;
2220
2221 for (i = 0; trace_flags->opts[i].name; i++) {
2222 opts = &trace_flags->opts[i];
2223 len = strlen(opts->name);
2224
2225 if (strncmp(cmp, opts->name, len) == 0) {
2226 ret = trace->set_flag(trace_flags->val,
2227 opts->bit, !neg);
2228 break;
2229 }
2230 }
2231 /* Not found */
2232 if (!trace_flags->opts[i].name)
2233 return -EINVAL;
2234
2235 /* Refused to handle */
2236 if (ret)
2237 return ret;
2238
2239 if (neg)
2240 trace_flags->val &= ~opts->bit;
2241 else
2242 trace_flags->val |= opts->bit;
2243
2244 return 0;
2245}
2246
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002247static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002248tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002249 size_t cnt, loff_t *ppos)
2250{
2251 char buf[64];
2252 char *cmp = buf;
2253 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002254 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255 int i;
2256
Steven Rostedtcffae432008-05-12 21:21:00 +02002257 if (cnt >= sizeof(buf))
2258 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002259
2260 if (copy_from_user(&buf, ubuf, cnt))
2261 return -EFAULT;
2262
2263 buf[cnt] = 0;
2264
2265 if (strncmp(buf, "no", 2) == 0) {
2266 neg = 1;
2267 cmp += 2;
2268 }
2269
2270 for (i = 0; trace_options[i]; i++) {
2271 int len = strlen(trace_options[i]);
2272
2273 if (strncmp(cmp, trace_options[i], len) == 0) {
2274 if (neg)
2275 trace_flags &= ~(1 << i);
2276 else
2277 trace_flags |= (1 << i);
2278 break;
2279 }
2280 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002281
2282 /* If no option could be set, test the specific tracer options */
2283 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002284 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002285 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002286 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002287 if (ret)
2288 return ret;
2289 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002290
2291 filp->f_pos += cnt;
2292
2293 return cnt;
2294}
2295
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002296static const struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002297 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002298 .read = tracing_trace_options_read,
2299 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002300};
2301
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002302static const char readme_msg[] =
2303 "tracing mini-HOWTO:\n\n"
2304 "# mkdir /debug\n"
2305 "# mount -t debugfs nodev /debug\n\n"
2306 "# cat /debug/tracing/available_tracers\n"
2307 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2308 "# cat /debug/tracing/current_tracer\n"
2309 "none\n"
2310 "# echo sched_switch > /debug/tracing/current_tracer\n"
2311 "# cat /debug/tracing/current_tracer\n"
2312 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002313 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002314 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002315 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002316 "# echo 1 > /debug/tracing/tracing_enabled\n"
2317 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2318 "echo 0 > /debug/tracing/tracing_enabled\n"
2319;
2320
2321static ssize_t
2322tracing_readme_read(struct file *filp, char __user *ubuf,
2323 size_t cnt, loff_t *ppos)
2324{
2325 return simple_read_from_buffer(ubuf, cnt, ppos,
2326 readme_msg, strlen(readme_msg));
2327}
2328
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002329static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002330 .open = tracing_open_generic,
2331 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002332};
2333
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002334static ssize_t
2335tracing_ctrl_read(struct file *filp, char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2337{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002338 char buf[64];
2339 int r;
2340
Steven Rostedt90369902008-11-05 16:05:44 -05002341 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002342 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002343}
2344
2345static ssize_t
2346tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2347 size_t cnt, loff_t *ppos)
2348{
2349 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002350 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002351 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002352 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002353
Steven Rostedtcffae432008-05-12 21:21:00 +02002354 if (cnt >= sizeof(buf))
2355 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356
2357 if (copy_from_user(&buf, ubuf, cnt))
2358 return -EFAULT;
2359
2360 buf[cnt] = 0;
2361
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002362 ret = strict_strtoul(buf, 10, &val);
2363 if (ret < 0)
2364 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002365
2366 val = !!val;
2367
2368 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002369 if (tracer_enabled ^ val) {
2370 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002371 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002372 if (current_trace->start)
2373 current_trace->start(tr);
2374 tracing_start();
2375 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002376 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002377 tracing_stop();
2378 if (current_trace->stop)
2379 current_trace->stop(tr);
2380 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002381 }
2382 mutex_unlock(&trace_types_lock);
2383
2384 filp->f_pos += cnt;
2385
2386 return cnt;
2387}
2388
2389static ssize_t
2390tracing_set_trace_read(struct file *filp, char __user *ubuf,
2391 size_t cnt, loff_t *ppos)
2392{
2393 char buf[max_tracer_type_len+2];
2394 int r;
2395
2396 mutex_lock(&trace_types_lock);
2397 if (current_trace)
2398 r = sprintf(buf, "%s\n", current_trace->name);
2399 else
2400 r = sprintf(buf, "\n");
2401 mutex_unlock(&trace_types_lock);
2402
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002403 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002404}
2405
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002406int tracer_init(struct tracer *t, struct trace_array *tr)
2407{
2408 tracing_reset_online_cpus(tr);
2409 return t->init(tr);
2410}
2411
Steven Rostedt73c51622009-03-11 13:42:01 -04002412static int tracing_resize_ring_buffer(unsigned long size)
2413{
2414 int ret;
2415
2416 /*
2417 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002418 * we use the size that was given, and we can forget about
2419 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002420 */
2421 ring_buffer_expanded = 1;
2422
2423 ret = ring_buffer_resize(global_trace.buffer, size);
2424 if (ret < 0)
2425 return ret;
2426
2427 ret = ring_buffer_resize(max_tr.buffer, size);
2428 if (ret < 0) {
2429 int r;
2430
2431 r = ring_buffer_resize(global_trace.buffer,
2432 global_trace.entries);
2433 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002434 /*
2435 * AARGH! We are left with different
2436 * size max buffer!!!!
2437 * The max buffer is our "snapshot" buffer.
2438 * When a tracer needs a snapshot (one of the
2439 * latency tracers), it swaps the max buffer
2440 * with the saved snap shot. We succeeded to
2441 * update the size of the main buffer, but failed to
2442 * update the size of the max buffer. But when we tried
2443 * to reset the main buffer to the original size, we
2444 * failed there too. This is very unlikely to
2445 * happen, but if it does, warn and kill all
2446 * tracing.
2447 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002448 WARN_ON(1);
2449 tracing_disabled = 1;
2450 }
2451 return ret;
2452 }
2453
2454 global_trace.entries = size;
2455
2456 return ret;
2457}
2458
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002459/**
2460 * tracing_update_buffers - used by tracing facility to expand ring buffers
2461 *
2462 * To save on memory when the tracing is never used on a system with it
2463 * configured in. The ring buffers are set to a minimum size. But once
2464 * a user starts to use the tracing facility, then they need to grow
2465 * to their default size.
2466 *
2467 * This function is to be called when a tracer is about to be used.
2468 */
2469int tracing_update_buffers(void)
2470{
2471 int ret = 0;
2472
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002473 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002474 if (!ring_buffer_expanded)
2475 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002476 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002477
2478 return ret;
2479}
2480
Steven Rostedt577b7852009-02-26 23:43:05 -05002481struct trace_option_dentry;
2482
2483static struct trace_option_dentry *
2484create_trace_option_files(struct tracer *tracer);
2485
2486static void
2487destroy_trace_option_files(struct trace_option_dentry *topts);
2488
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002489static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002490{
Steven Rostedt577b7852009-02-26 23:43:05 -05002491 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002492 struct trace_array *tr = &global_trace;
2493 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002494 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002495
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002496 mutex_lock(&trace_types_lock);
2497
Steven Rostedt73c51622009-03-11 13:42:01 -04002498 if (!ring_buffer_expanded) {
2499 ret = tracing_resize_ring_buffer(trace_buf_size);
2500 if (ret < 0)
2501 return ret;
2502 ret = 0;
2503 }
2504
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002505 for (t = trace_types; t; t = t->next) {
2506 if (strcmp(t->name, buf) == 0)
2507 break;
2508 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002509 if (!t) {
2510 ret = -EINVAL;
2511 goto out;
2512 }
2513 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002514 goto out;
2515
Steven Rostedt9f029e82008-11-12 15:24:24 -05002516 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002517 if (current_trace && current_trace->reset)
2518 current_trace->reset(tr);
2519
Steven Rostedt577b7852009-02-26 23:43:05 -05002520 destroy_trace_option_files(topts);
2521
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002522 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002523
2524 topts = create_trace_option_files(current_trace);
2525
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002526 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002527 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002528 if (ret)
2529 goto out;
2530 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002531
Steven Rostedt9f029e82008-11-12 15:24:24 -05002532 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002533 out:
2534 mutex_unlock(&trace_types_lock);
2535
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002536 return ret;
2537}
2538
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002539static ssize_t
2540tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2541 size_t cnt, loff_t *ppos)
2542{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002543 char buf[max_tracer_type_len+1];
2544 int i;
2545 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002546 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002547
Steven Rostedt60063a62008-10-28 10:44:24 -04002548 ret = cnt;
2549
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002550 if (cnt > max_tracer_type_len)
2551 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002552
2553 if (copy_from_user(&buf, ubuf, cnt))
2554 return -EFAULT;
2555
2556 buf[cnt] = 0;
2557
2558 /* strip ending whitespace. */
2559 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2560 buf[i] = 0;
2561
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002562 err = tracing_set_tracer(buf);
2563 if (err)
2564 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002565
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002566 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002567
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002568 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002569}
2570
2571static ssize_t
2572tracing_max_lat_read(struct file *filp, char __user *ubuf,
2573 size_t cnt, loff_t *ppos)
2574{
2575 unsigned long *ptr = filp->private_data;
2576 char buf[64];
2577 int r;
2578
Steven Rostedtcffae432008-05-12 21:21:00 +02002579 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002580 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002581 if (r > sizeof(buf))
2582 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002583 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002584}
2585
2586static ssize_t
2587tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2588 size_t cnt, loff_t *ppos)
2589{
Hannes Eder5e398412009-02-10 19:44:34 +01002590 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002591 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002592 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002593 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002594
Steven Rostedtcffae432008-05-12 21:21:00 +02002595 if (cnt >= sizeof(buf))
2596 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002597
2598 if (copy_from_user(&buf, ubuf, cnt))
2599 return -EFAULT;
2600
2601 buf[cnt] = 0;
2602
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002603 ret = strict_strtoul(buf, 10, &val);
2604 if (ret < 0)
2605 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002606
2607 *ptr = val * 1000;
2608
2609 return cnt;
2610}
2611
Steven Rostedtb3806b42008-05-12 21:20:46 +02002612static int tracing_open_pipe(struct inode *inode, struct file *filp)
2613{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002614 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002615 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002616 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002617
2618 if (tracing_disabled)
2619 return -ENODEV;
2620
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002621 mutex_lock(&trace_types_lock);
2622
2623 /* We only allow one reader per cpu */
2624 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2625 if (!cpumask_empty(tracing_reader_cpumask)) {
2626 ret = -EBUSY;
2627 goto out;
2628 }
2629 cpumask_setall(tracing_reader_cpumask);
2630 } else {
2631 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2632 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2633 else {
2634 ret = -EBUSY;
2635 goto out;
2636 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002637 }
2638
2639 /* create a buffer to store the information to pass to userspace */
2640 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002641 if (!iter) {
2642 ret = -ENOMEM;
2643 goto out;
2644 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002645
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002646 /*
2647 * We make a copy of the current tracer to avoid concurrent
2648 * changes on it while we are reading.
2649 */
2650 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2651 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002652 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002653 goto fail;
2654 }
2655 if (current_trace)
2656 *iter->trace = *current_trace;
2657
2658 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2659 ret = -ENOMEM;
2660 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302661 }
2662
Steven Rostedta3097202008-11-07 22:36:02 -05002663 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302664 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002665
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002666 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002667 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002668 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002669 filp->private_data = iter;
2670
Steven Rostedt107bad82008-05-12 21:21:01 +02002671 if (iter->trace->pipe_open)
2672 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002673
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002674out:
2675 mutex_unlock(&trace_types_lock);
2676 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002677
2678fail:
2679 kfree(iter->trace);
2680 kfree(iter);
2681 mutex_unlock(&trace_types_lock);
2682 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002683}
2684
2685static int tracing_release_pipe(struct inode *inode, struct file *file)
2686{
2687 struct trace_iterator *iter = file->private_data;
2688
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002689 mutex_lock(&trace_types_lock);
2690
2691 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2692 cpumask_clear(tracing_reader_cpumask);
2693 else
2694 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2695
2696 mutex_unlock(&trace_types_lock);
2697
Rusty Russell44623442009-01-01 10:12:23 +10302698 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002699 mutex_destroy(&iter->mutex);
2700 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002701 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002702
2703 return 0;
2704}
2705
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002706static unsigned int
2707tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2708{
2709 struct trace_iterator *iter = filp->private_data;
2710
2711 if (trace_flags & TRACE_ITER_BLOCK) {
2712 /*
2713 * Always select as readable when in blocking mode
2714 */
2715 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002716 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002717 if (!trace_empty(iter))
2718 return POLLIN | POLLRDNORM;
2719 poll_wait(filp, &trace_wait, poll_table);
2720 if (!trace_empty(iter))
2721 return POLLIN | POLLRDNORM;
2722
2723 return 0;
2724 }
2725}
2726
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002727
2728void default_wait_pipe(struct trace_iterator *iter)
2729{
2730 DEFINE_WAIT(wait);
2731
2732 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2733
2734 if (trace_empty(iter))
2735 schedule();
2736
2737 finish_wait(&trace_wait, &wait);
2738}
2739
2740/*
2741 * This is a make-shift waitqueue.
2742 * A tracer might use this callback on some rare cases:
2743 *
2744 * 1) the current tracer might hold the runqueue lock when it wakes up
2745 * a reader, hence a deadlock (sched, function, and function graph tracers)
2746 * 2) the function tracers, trace all functions, we don't want
2747 * the overhead of calling wake_up and friends
2748 * (and tracing them too)
2749 *
2750 * Anyway, this is really very primitive wakeup.
2751 */
2752void poll_wait_pipe(struct trace_iterator *iter)
2753{
2754 set_current_state(TASK_INTERRUPTIBLE);
2755 /* sleep for 100 msecs, and try again. */
2756 schedule_timeout(HZ / 10);
2757}
2758
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002759/* Must be called with trace_types_lock mutex held. */
2760static int tracing_wait_pipe(struct file *filp)
2761{
2762 struct trace_iterator *iter = filp->private_data;
2763
2764 while (trace_empty(iter)) {
2765
2766 if ((filp->f_flags & O_NONBLOCK)) {
2767 return -EAGAIN;
2768 }
2769
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002770 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002771
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002772 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002773
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002774 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002775
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002776 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002777 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002778
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002779 /*
2780 * We block until we read something and tracing is disabled.
2781 * We still block if tracing is disabled, but we have never
2782 * read anything. This allows a user to cat this file, and
2783 * then enable tracing. But after we have read something,
2784 * we give an EOF when tracing is again disabled.
2785 *
2786 * iter->pos will be 0 if we haven't read anything.
2787 */
2788 if (!tracer_enabled && iter->pos)
2789 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002790 }
2791
2792 return 1;
2793}
2794
Steven Rostedtb3806b42008-05-12 21:20:46 +02002795/*
2796 * Consumer reader.
2797 */
2798static ssize_t
2799tracing_read_pipe(struct file *filp, char __user *ubuf,
2800 size_t cnt, loff_t *ppos)
2801{
2802 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002803 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002804 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002805
2806 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002807 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2808 if (sret != -EBUSY)
2809 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002810
Steven Rostedtf9520752009-03-02 14:04:40 -05002811 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002812
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002813 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002814 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002815 if (unlikely(old_tracer != current_trace && current_trace)) {
2816 old_tracer = current_trace;
2817 *iter->trace = *current_trace;
2818 }
2819 mutex_unlock(&trace_types_lock);
2820
2821 /*
2822 * Avoid more than one consumer on a single file descriptor
2823 * This is just a matter of traces coherency, the ring buffer itself
2824 * is protected.
2825 */
2826 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002827 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002828 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2829 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002830 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002831 }
2832
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002833waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002834 sret = tracing_wait_pipe(filp);
2835 if (sret <= 0)
2836 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002837
2838 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002839 if (trace_empty(iter)) {
2840 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002841 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002842 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002843
2844 if (cnt >= PAGE_SIZE)
2845 cnt = PAGE_SIZE - 1;
2846
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002847 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002848 memset(&iter->seq, 0,
2849 sizeof(struct trace_iterator) -
2850 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002851 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002852
Steven Rostedt088b1e422008-05-12 21:20:48 +02002853 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002854 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002855 int len = iter->seq.len;
2856
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002857 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002858 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002859 /* don't print partial lines */
2860 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002861 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002862 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002863 if (ret != TRACE_TYPE_NO_CONSUME)
2864 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002865
2866 if (iter->seq.len >= cnt)
2867 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002868 }
2869
Steven Rostedtb3806b42008-05-12 21:20:46 +02002870 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002871 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2872 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002873 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002874
2875 /*
2876 * If there was nothing to send to user, inspite of consuming trace
2877 * entries, go back to wait for more entries.
2878 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002879 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002880 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002881
Steven Rostedt107bad82008-05-12 21:21:01 +02002882out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002883 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002884
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002885 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002886}
2887
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002888static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2889 struct pipe_buffer *buf)
2890{
2891 __free_page(buf->page);
2892}
2893
2894static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2895 unsigned int idx)
2896{
2897 __free_page(spd->pages[idx]);
2898}
2899
2900static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002901 .can_merge = 0,
2902 .map = generic_pipe_buf_map,
2903 .unmap = generic_pipe_buf_unmap,
2904 .confirm = generic_pipe_buf_confirm,
2905 .release = tracing_pipe_buf_release,
2906 .steal = generic_pipe_buf_steal,
2907 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002908};
2909
Steven Rostedt34cd4992009-02-09 12:06:29 -05002910static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002911tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002912{
2913 size_t count;
2914 int ret;
2915
2916 /* Seq buffer is page-sized, exactly what we need. */
2917 for (;;) {
2918 count = iter->seq.len;
2919 ret = print_trace_line(iter);
2920 count = iter->seq.len - count;
2921 if (rem < count) {
2922 rem = 0;
2923 iter->seq.len -= count;
2924 break;
2925 }
2926 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2927 iter->seq.len -= count;
2928 break;
2929 }
2930
2931 trace_consume(iter);
2932 rem -= count;
2933 if (!find_next_entry_inc(iter)) {
2934 rem = 0;
2935 iter->ent = NULL;
2936 break;
2937 }
2938 }
2939
2940 return rem;
2941}
2942
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002943static ssize_t tracing_splice_read_pipe(struct file *filp,
2944 loff_t *ppos,
2945 struct pipe_inode_info *pipe,
2946 size_t len,
2947 unsigned int flags)
2948{
2949 struct page *pages[PIPE_BUFFERS];
2950 struct partial_page partial[PIPE_BUFFERS];
2951 struct trace_iterator *iter = filp->private_data;
2952 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002953 .pages = pages,
2954 .partial = partial,
2955 .nr_pages = 0, /* This gets updated below. */
2956 .flags = flags,
2957 .ops = &tracing_pipe_buf_ops,
2958 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002959 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002960 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002961 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002962 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002963 unsigned int i;
2964
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002965 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002966 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002967 if (unlikely(old_tracer != current_trace && current_trace)) {
2968 old_tracer = current_trace;
2969 *iter->trace = *current_trace;
2970 }
2971 mutex_unlock(&trace_types_lock);
2972
2973 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002974
2975 if (iter->trace->splice_read) {
2976 ret = iter->trace->splice_read(iter, filp,
2977 ppos, pipe, len, flags);
2978 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002979 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002980 }
2981
2982 ret = tracing_wait_pipe(filp);
2983 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002984 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002985
2986 if (!iter->ent && !find_next_entry_inc(iter)) {
2987 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002988 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002989 }
2990
2991 /* Fill as many pages as possible. */
2992 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2993 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05002994 if (!pages[i])
2995 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002996
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002997 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002998
2999 /* Copy the data into the page, so we can start over. */
3000 ret = trace_seq_to_buffer(&iter->seq,
3001 page_address(pages[i]),
3002 iter->seq.len);
3003 if (ret < 0) {
3004 __free_page(pages[i]);
3005 break;
3006 }
3007 partial[i].offset = 0;
3008 partial[i].len = iter->seq.len;
3009
Steven Rostedtf9520752009-03-02 14:04:40 -05003010 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003011 }
3012
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003013 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003014
3015 spd.nr_pages = i;
3016
3017 return splice_to_pipe(pipe, &spd);
3018
Steven Rostedt34cd4992009-02-09 12:06:29 -05003019out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003020 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003021
3022 return ret;
3023}
3024
Steven Rostedta98a3c32008-05-12 21:20:59 +02003025static ssize_t
3026tracing_entries_read(struct file *filp, char __user *ubuf,
3027 size_t cnt, loff_t *ppos)
3028{
3029 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003030 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003031 int r;
3032
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003033 mutex_lock(&trace_types_lock);
3034 if (!ring_buffer_expanded)
3035 r = sprintf(buf, "%lu (expanded: %lu)\n",
3036 tr->entries >> 10,
3037 trace_buf_size >> 10);
3038 else
3039 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3040 mutex_unlock(&trace_types_lock);
3041
Steven Rostedta98a3c32008-05-12 21:20:59 +02003042 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3043}
3044
3045static ssize_t
3046tracing_entries_write(struct file *filp, const char __user *ubuf,
3047 size_t cnt, loff_t *ppos)
3048{
3049 unsigned long val;
3050 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003051 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003052
Steven Rostedtcffae432008-05-12 21:21:00 +02003053 if (cnt >= sizeof(buf))
3054 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003055
3056 if (copy_from_user(&buf, ubuf, cnt))
3057 return -EFAULT;
3058
3059 buf[cnt] = 0;
3060
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003061 ret = strict_strtoul(buf, 10, &val);
3062 if (ret < 0)
3063 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003064
3065 /* must have at least 1 entry */
3066 if (!val)
3067 return -EINVAL;
3068
3069 mutex_lock(&trace_types_lock);
3070
Steven Rostedtc76f0692008-11-07 22:36:02 -05003071 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003072
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003073 /* disable all cpu buffers */
3074 for_each_tracing_cpu(cpu) {
3075 if (global_trace.data[cpu])
3076 atomic_inc(&global_trace.data[cpu]->disabled);
3077 if (max_tr.data[cpu])
3078 atomic_inc(&max_tr.data[cpu]->disabled);
3079 }
3080
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003081 /* value is in KB */
3082 val <<= 10;
3083
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003084 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003085 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003086 if (ret < 0) {
3087 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003088 goto out;
3089 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003090 }
3091
3092 filp->f_pos += cnt;
3093
Steven Rostedt19384c02008-05-22 00:22:16 -04003094 /* If check pages failed, return ENOMEM */
3095 if (tracing_disabled)
3096 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003097 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003098 for_each_tracing_cpu(cpu) {
3099 if (global_trace.data[cpu])
3100 atomic_dec(&global_trace.data[cpu]->disabled);
3101 if (max_tr.data[cpu])
3102 atomic_dec(&max_tr.data[cpu]->disabled);
3103 }
3104
Steven Rostedtc76f0692008-11-07 22:36:02 -05003105 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003106 max_tr.entries = global_trace.entries;
3107 mutex_unlock(&trace_types_lock);
3108
3109 return cnt;
3110}
3111
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003112static int mark_printk(const char *fmt, ...)
3113{
3114 int ret;
3115 va_list args;
3116 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003117 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003118 va_end(args);
3119 return ret;
3120}
3121
3122static ssize_t
3123tracing_mark_write(struct file *filp, const char __user *ubuf,
3124 size_t cnt, loff_t *fpos)
3125{
3126 char *buf;
3127 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003128
Steven Rostedtc76f0692008-11-07 22:36:02 -05003129 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003130 return -EINVAL;
3131
3132 if (cnt > TRACE_BUF_SIZE)
3133 cnt = TRACE_BUF_SIZE;
3134
3135 buf = kmalloc(cnt + 1, GFP_KERNEL);
3136 if (buf == NULL)
3137 return -ENOMEM;
3138
3139 if (copy_from_user(buf, ubuf, cnt)) {
3140 kfree(buf);
3141 return -EFAULT;
3142 }
3143
3144 /* Cut from the first nil or newline. */
3145 buf[cnt] = '\0';
3146 end = strchr(buf, '\n');
3147 if (end)
3148 *end = '\0';
3149
3150 cnt = mark_printk("%s\n", buf);
3151 kfree(buf);
3152 *fpos += cnt;
3153
3154 return cnt;
3155}
3156
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003157static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003158 .open = tracing_open_generic,
3159 .read = tracing_max_lat_read,
3160 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003161};
3162
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003163static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003164 .open = tracing_open_generic,
3165 .read = tracing_ctrl_read,
3166 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003167};
3168
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003169static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003170 .open = tracing_open_generic,
3171 .read = tracing_set_trace_read,
3172 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003173};
3174
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003175static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003176 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003177 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003178 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003179 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003180 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003181};
3182
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003183static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003184 .open = tracing_open_generic,
3185 .read = tracing_entries_read,
3186 .write = tracing_entries_write,
3187};
3188
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003189static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003190 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003191 .write = tracing_mark_write,
3192};
3193
Steven Rostedt2cadf912008-12-01 22:20:19 -05003194struct ftrace_buffer_info {
3195 struct trace_array *tr;
3196 void *spare;
3197 int cpu;
3198 unsigned int read;
3199};
3200
3201static int tracing_buffers_open(struct inode *inode, struct file *filp)
3202{
3203 int cpu = (int)(long)inode->i_private;
3204 struct ftrace_buffer_info *info;
3205
3206 if (tracing_disabled)
3207 return -ENODEV;
3208
3209 info = kzalloc(sizeof(*info), GFP_KERNEL);
3210 if (!info)
3211 return -ENOMEM;
3212
3213 info->tr = &global_trace;
3214 info->cpu = cpu;
3215 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3216 /* Force reading ring buffer for first read */
3217 info->read = (unsigned int)-1;
3218 if (!info->spare)
3219 goto out;
3220
3221 filp->private_data = info;
3222
3223 return 0;
3224
3225 out:
3226 kfree(info);
3227 return -ENOMEM;
3228}
3229
3230static ssize_t
3231tracing_buffers_read(struct file *filp, char __user *ubuf,
3232 size_t count, loff_t *ppos)
3233{
3234 struct ftrace_buffer_info *info = filp->private_data;
3235 unsigned int pos;
3236 ssize_t ret;
3237 size_t size;
3238
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003239 if (!count)
3240 return 0;
3241
Steven Rostedt2cadf912008-12-01 22:20:19 -05003242 /* Do we have previous read data to read? */
3243 if (info->read < PAGE_SIZE)
3244 goto read;
3245
3246 info->read = 0;
3247
3248 ret = ring_buffer_read_page(info->tr->buffer,
3249 &info->spare,
3250 count,
3251 info->cpu, 0);
3252 if (ret < 0)
3253 return 0;
3254
3255 pos = ring_buffer_page_len(info->spare);
3256
3257 if (pos < PAGE_SIZE)
3258 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3259
3260read:
3261 size = PAGE_SIZE - info->read;
3262 if (size > count)
3263 size = count;
3264
3265 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003266 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003267 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003268 size -= ret;
3269
Steven Rostedt2cadf912008-12-01 22:20:19 -05003270 *ppos += size;
3271 info->read += size;
3272
3273 return size;
3274}
3275
3276static int tracing_buffers_release(struct inode *inode, struct file *file)
3277{
3278 struct ftrace_buffer_info *info = file->private_data;
3279
3280 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3281 kfree(info);
3282
3283 return 0;
3284}
3285
3286struct buffer_ref {
3287 struct ring_buffer *buffer;
3288 void *page;
3289 int ref;
3290};
3291
3292static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3293 struct pipe_buffer *buf)
3294{
3295 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3296
3297 if (--ref->ref)
3298 return;
3299
3300 ring_buffer_free_read_page(ref->buffer, ref->page);
3301 kfree(ref);
3302 buf->private = 0;
3303}
3304
3305static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3306 struct pipe_buffer *buf)
3307{
3308 return 1;
3309}
3310
3311static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3312 struct pipe_buffer *buf)
3313{
3314 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3315
3316 ref->ref++;
3317}
3318
3319/* Pipe buffer operations for a buffer. */
3320static struct pipe_buf_operations buffer_pipe_buf_ops = {
3321 .can_merge = 0,
3322 .map = generic_pipe_buf_map,
3323 .unmap = generic_pipe_buf_unmap,
3324 .confirm = generic_pipe_buf_confirm,
3325 .release = buffer_pipe_buf_release,
3326 .steal = buffer_pipe_buf_steal,
3327 .get = buffer_pipe_buf_get,
3328};
3329
3330/*
3331 * Callback from splice_to_pipe(), if we need to release some pages
3332 * at the end of the spd in case we error'ed out in filling the pipe.
3333 */
3334static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3335{
3336 struct buffer_ref *ref =
3337 (struct buffer_ref *)spd->partial[i].private;
3338
3339 if (--ref->ref)
3340 return;
3341
3342 ring_buffer_free_read_page(ref->buffer, ref->page);
3343 kfree(ref);
3344 spd->partial[i].private = 0;
3345}
3346
3347static ssize_t
3348tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3349 struct pipe_inode_info *pipe, size_t len,
3350 unsigned int flags)
3351{
3352 struct ftrace_buffer_info *info = file->private_data;
3353 struct partial_page partial[PIPE_BUFFERS];
3354 struct page *pages[PIPE_BUFFERS];
3355 struct splice_pipe_desc spd = {
3356 .pages = pages,
3357 .partial = partial,
3358 .flags = flags,
3359 .ops = &buffer_pipe_buf_ops,
3360 .spd_release = buffer_spd_release,
3361 };
3362 struct buffer_ref *ref;
3363 int size, i;
3364 size_t ret;
3365
3366 /*
3367 * We can't seek on a buffer input
3368 */
3369 if (unlikely(*ppos))
3370 return -ESPIPE;
3371
3372
3373 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3374 struct page *page;
3375 int r;
3376
3377 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3378 if (!ref)
3379 break;
3380
3381 ref->buffer = info->tr->buffer;
3382 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3383 if (!ref->page) {
3384 kfree(ref);
3385 break;
3386 }
3387
3388 r = ring_buffer_read_page(ref->buffer, &ref->page,
3389 len, info->cpu, 0);
3390 if (r < 0) {
3391 ring_buffer_free_read_page(ref->buffer,
3392 ref->page);
3393 kfree(ref);
3394 break;
3395 }
3396
3397 /*
3398 * zero out any left over data, this is going to
3399 * user land.
3400 */
3401 size = ring_buffer_page_len(ref->page);
3402 if (size < PAGE_SIZE)
3403 memset(ref->page + size, 0, PAGE_SIZE - size);
3404
3405 page = virt_to_page(ref->page);
3406
3407 spd.pages[i] = page;
3408 spd.partial[i].len = PAGE_SIZE;
3409 spd.partial[i].offset = 0;
3410 spd.partial[i].private = (unsigned long)ref;
3411 spd.nr_pages++;
3412 }
3413
3414 spd.nr_pages = i;
3415
3416 /* did we read anything? */
3417 if (!spd.nr_pages) {
3418 if (flags & SPLICE_F_NONBLOCK)
3419 ret = -EAGAIN;
3420 else
3421 ret = 0;
3422 /* TODO: block */
3423 return ret;
3424 }
3425
3426 ret = splice_to_pipe(pipe, &spd);
3427
3428 return ret;
3429}
3430
3431static const struct file_operations tracing_buffers_fops = {
3432 .open = tracing_buffers_open,
3433 .read = tracing_buffers_read,
3434 .release = tracing_buffers_release,
3435 .splice_read = tracing_buffers_splice_read,
3436 .llseek = no_llseek,
3437};
3438
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003439#ifdef CONFIG_DYNAMIC_FTRACE
3440
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003441int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003442{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003443 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003444}
3445
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003446static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003447tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003448 size_t cnt, loff_t *ppos)
3449{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003450 static char ftrace_dyn_info_buffer[1024];
3451 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003452 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003453 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003454 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003455 int r;
3456
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003457 mutex_lock(&dyn_info_mutex);
3458 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003459
Steven Rostedta26a2a22008-10-31 00:03:22 -04003460 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003461 buf[r++] = '\n';
3462
3463 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3464
3465 mutex_unlock(&dyn_info_mutex);
3466
3467 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003468}
3469
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003470static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003471 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003472 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003473};
3474#endif
3475
3476static struct dentry *d_tracer;
3477
3478struct dentry *tracing_init_dentry(void)
3479{
3480 static int once;
3481
3482 if (d_tracer)
3483 return d_tracer;
3484
3485 d_tracer = debugfs_create_dir("tracing", NULL);
3486
3487 if (!d_tracer && !once) {
3488 once = 1;
3489 pr_warning("Could not create debugfs directory 'tracing'\n");
3490 return NULL;
3491 }
3492
3493 return d_tracer;
3494}
3495
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003496static struct dentry *d_percpu;
3497
3498struct dentry *tracing_dentry_percpu(void)
3499{
3500 static int once;
3501 struct dentry *d_tracer;
3502
3503 if (d_percpu)
3504 return d_percpu;
3505
3506 d_tracer = tracing_init_dentry();
3507
3508 if (!d_tracer)
3509 return NULL;
3510
3511 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3512
3513 if (!d_percpu && !once) {
3514 once = 1;
3515 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3516 return NULL;
3517 }
3518
3519 return d_percpu;
3520}
3521
3522static void tracing_init_debugfs_percpu(long cpu)
3523{
3524 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003525 struct dentry *entry, *d_cpu;
3526 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3527 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003528
3529 if (cpu > 999 || cpu < 0)
3530 return;
3531
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003532 sprintf(cpu_dir, "cpu%ld", cpu);
3533 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3534 if (!d_cpu) {
3535 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3536 return;
3537 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003538
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003539 /* per cpu trace_pipe */
3540 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003541 (void *) cpu, &tracing_pipe_fops);
3542 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003543 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003544
3545 /* per cpu trace */
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003546 entry = debugfs_create_file("trace", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003547 (void *) cpu, &tracing_fops);
3548 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003549 pr_warning("Could not create debugfs 'trace' entry\n");
Steven Rostedt7f96f932009-03-13 00:37:42 -04003550
3551 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3552 (void *) cpu, &tracing_buffers_fops);
3553 if (!entry)
3554 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003555}
3556
Steven Rostedt60a11772008-05-12 21:20:44 +02003557#ifdef CONFIG_FTRACE_SELFTEST
3558/* Let selftest have access to static functions in this file */
3559#include "trace_selftest.c"
3560#endif
3561
Steven Rostedt577b7852009-02-26 23:43:05 -05003562struct trace_option_dentry {
3563 struct tracer_opt *opt;
3564 struct tracer_flags *flags;
3565 struct dentry *entry;
3566};
3567
3568static ssize_t
3569trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3570 loff_t *ppos)
3571{
3572 struct trace_option_dentry *topt = filp->private_data;
3573 char *buf;
3574
3575 if (topt->flags->val & topt->opt->bit)
3576 buf = "1\n";
3577 else
3578 buf = "0\n";
3579
3580 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3581}
3582
3583static ssize_t
3584trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3585 loff_t *ppos)
3586{
3587 struct trace_option_dentry *topt = filp->private_data;
3588 unsigned long val;
3589 char buf[64];
3590 int ret;
3591
3592 if (cnt >= sizeof(buf))
3593 return -EINVAL;
3594
3595 if (copy_from_user(&buf, ubuf, cnt))
3596 return -EFAULT;
3597
3598 buf[cnt] = 0;
3599
3600 ret = strict_strtoul(buf, 10, &val);
3601 if (ret < 0)
3602 return ret;
3603
3604 ret = 0;
3605 switch (val) {
3606 case 0:
3607 /* do nothing if already cleared */
3608 if (!(topt->flags->val & topt->opt->bit))
3609 break;
3610
3611 mutex_lock(&trace_types_lock);
3612 if (current_trace->set_flag)
3613 ret = current_trace->set_flag(topt->flags->val,
3614 topt->opt->bit, 0);
3615 mutex_unlock(&trace_types_lock);
3616 if (ret)
3617 return ret;
3618 topt->flags->val &= ~topt->opt->bit;
3619 break;
3620 case 1:
3621 /* do nothing if already set */
3622 if (topt->flags->val & topt->opt->bit)
3623 break;
3624
3625 mutex_lock(&trace_types_lock);
3626 if (current_trace->set_flag)
3627 ret = current_trace->set_flag(topt->flags->val,
3628 topt->opt->bit, 1);
3629 mutex_unlock(&trace_types_lock);
3630 if (ret)
3631 return ret;
3632 topt->flags->val |= topt->opt->bit;
3633 break;
3634
3635 default:
3636 return -EINVAL;
3637 }
3638
3639 *ppos += cnt;
3640
3641 return cnt;
3642}
3643
3644
3645static const struct file_operations trace_options_fops = {
3646 .open = tracing_open_generic,
3647 .read = trace_options_read,
3648 .write = trace_options_write,
3649};
3650
Steven Rostedta8259072009-02-26 22:19:12 -05003651static ssize_t
3652trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3653 loff_t *ppos)
3654{
3655 long index = (long)filp->private_data;
3656 char *buf;
3657
3658 if (trace_flags & (1 << index))
3659 buf = "1\n";
3660 else
3661 buf = "0\n";
3662
3663 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3664}
3665
3666static ssize_t
3667trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3668 loff_t *ppos)
3669{
3670 long index = (long)filp->private_data;
3671 char buf[64];
3672 unsigned long val;
3673 int ret;
3674
3675 if (cnt >= sizeof(buf))
3676 return -EINVAL;
3677
3678 if (copy_from_user(&buf, ubuf, cnt))
3679 return -EFAULT;
3680
3681 buf[cnt] = 0;
3682
3683 ret = strict_strtoul(buf, 10, &val);
3684 if (ret < 0)
3685 return ret;
3686
3687 switch (val) {
3688 case 0:
3689 trace_flags &= ~(1 << index);
3690 break;
3691 case 1:
3692 trace_flags |= 1 << index;
3693 break;
3694
3695 default:
3696 return -EINVAL;
3697 }
3698
3699 *ppos += cnt;
3700
3701 return cnt;
3702}
3703
Steven Rostedta8259072009-02-26 22:19:12 -05003704static const struct file_operations trace_options_core_fops = {
3705 .open = tracing_open_generic,
3706 .read = trace_options_core_read,
3707 .write = trace_options_core_write,
3708};
3709
3710static struct dentry *trace_options_init_dentry(void)
3711{
3712 struct dentry *d_tracer;
3713 static struct dentry *t_options;
3714
3715 if (t_options)
3716 return t_options;
3717
3718 d_tracer = tracing_init_dentry();
3719 if (!d_tracer)
3720 return NULL;
3721
3722 t_options = debugfs_create_dir("options", d_tracer);
3723 if (!t_options) {
3724 pr_warning("Could not create debugfs directory 'options'\n");
3725 return NULL;
3726 }
3727
3728 return t_options;
3729}
3730
Steven Rostedt577b7852009-02-26 23:43:05 -05003731static void
3732create_trace_option_file(struct trace_option_dentry *topt,
3733 struct tracer_flags *flags,
3734 struct tracer_opt *opt)
3735{
3736 struct dentry *t_options;
3737 struct dentry *entry;
3738
3739 t_options = trace_options_init_dentry();
3740 if (!t_options)
3741 return;
3742
3743 topt->flags = flags;
3744 topt->opt = opt;
3745
3746 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3747 &trace_options_fops);
3748
3749 topt->entry = entry;
3750
3751}
3752
3753static struct trace_option_dentry *
3754create_trace_option_files(struct tracer *tracer)
3755{
3756 struct trace_option_dentry *topts;
3757 struct tracer_flags *flags;
3758 struct tracer_opt *opts;
3759 int cnt;
3760
3761 if (!tracer)
3762 return NULL;
3763
3764 flags = tracer->flags;
3765
3766 if (!flags || !flags->opts)
3767 return NULL;
3768
3769 opts = flags->opts;
3770
3771 for (cnt = 0; opts[cnt].name; cnt++)
3772 ;
3773
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003774 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003775 if (!topts)
3776 return NULL;
3777
3778 for (cnt = 0; opts[cnt].name; cnt++)
3779 create_trace_option_file(&topts[cnt], flags,
3780 &opts[cnt]);
3781
3782 return topts;
3783}
3784
3785static void
3786destroy_trace_option_files(struct trace_option_dentry *topts)
3787{
3788 int cnt;
3789
3790 if (!topts)
3791 return;
3792
3793 for (cnt = 0; topts[cnt].opt; cnt++) {
3794 if (topts[cnt].entry)
3795 debugfs_remove(topts[cnt].entry);
3796 }
3797
3798 kfree(topts);
3799}
3800
Steven Rostedta8259072009-02-26 22:19:12 -05003801static struct dentry *
3802create_trace_option_core_file(const char *option, long index)
3803{
3804 struct dentry *t_options;
3805 struct dentry *entry;
3806
3807 t_options = trace_options_init_dentry();
3808 if (!t_options)
3809 return NULL;
3810
3811 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3812 &trace_options_core_fops);
3813
3814 return entry;
3815}
3816
3817static __init void create_trace_options_dir(void)
3818{
3819 struct dentry *t_options;
3820 struct dentry *entry;
3821 int i;
3822
3823 t_options = trace_options_init_dentry();
3824 if (!t_options)
3825 return;
3826
3827 for (i = 0; trace_options[i]; i++) {
3828 entry = create_trace_option_core_file(trace_options[i], i);
3829 if (!entry)
3830 pr_warning("Could not create debugfs %s entry\n",
3831 trace_options[i]);
3832 }
3833}
3834
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003835static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003836{
3837 struct dentry *d_tracer;
3838 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003839 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003840
3841 d_tracer = tracing_init_dentry();
3842
3843 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3844 &global_trace, &tracing_ctrl_fops);
3845 if (!entry)
3846 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3847
Steven Rostedtee6bce52008-11-12 17:52:37 -05003848 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003849 NULL, &tracing_iter_fops);
3850 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003851 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003852
Steven Rostedta8259072009-02-26 22:19:12 -05003853 create_trace_options_dir();
3854
Ingo Molnarc7078de2008-05-12 21:20:52 +02003855 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3856 NULL, &tracing_cpumask_fops);
3857 if (!entry)
3858 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3859
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003860 entry = debugfs_create_file("trace", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003861 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003862 if (!entry)
3863 pr_warning("Could not create debugfs 'trace' entry\n");
3864
3865 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3866 &global_trace, &show_traces_fops);
3867 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003868 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003869
3870 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3871 &global_trace, &set_tracer_fops);
3872 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003873 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003874
3875 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3876 &tracing_max_latency,
3877 &tracing_max_lat_fops);
3878 if (!entry)
3879 pr_warning("Could not create debugfs "
3880 "'tracing_max_latency' entry\n");
3881
3882 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3883 &tracing_thresh, &tracing_max_lat_fops);
3884 if (!entry)
3885 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003886 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003887 entry = debugfs_create_file("README", 0644, d_tracer,
3888 NULL, &tracing_readme_fops);
3889 if (!entry)
3890 pr_warning("Could not create debugfs 'README' entry\n");
3891
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003892 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3893 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003894 if (!entry)
3895 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003896 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003897
Steven Rostedta94c80e2008-11-12 17:52:36 -05003898 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003899 &global_trace, &tracing_entries_fops);
3900 if (!entry)
3901 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003902 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003903
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003904 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3905 NULL, &tracing_mark_fops);
3906 if (!entry)
3907 pr_warning("Could not create debugfs "
3908 "'trace_marker' entry\n");
3909
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003910#ifdef CONFIG_DYNAMIC_FTRACE
3911 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3912 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003913 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003914 if (!entry)
3915 pr_warning("Could not create debugfs "
3916 "'dyn_ftrace_total_info' entry\n");
3917#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003918#ifdef CONFIG_SYSPROF_TRACER
3919 init_tracer_sysprof_debugfs(d_tracer);
3920#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003921
3922 for_each_tracing_cpu(cpu)
3923 tracing_init_debugfs_percpu(cpu);
3924
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003925 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003926}
3927
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003928static int trace_panic_handler(struct notifier_block *this,
3929 unsigned long event, void *unused)
3930{
Steven Rostedt944ac422008-10-23 19:26:08 -04003931 if (ftrace_dump_on_oops)
3932 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003933 return NOTIFY_OK;
3934}
3935
3936static struct notifier_block trace_panic_notifier = {
3937 .notifier_call = trace_panic_handler,
3938 .next = NULL,
3939 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3940};
3941
3942static int trace_die_handler(struct notifier_block *self,
3943 unsigned long val,
3944 void *data)
3945{
3946 switch (val) {
3947 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003948 if (ftrace_dump_on_oops)
3949 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003950 break;
3951 default:
3952 break;
3953 }
3954 return NOTIFY_OK;
3955}
3956
3957static struct notifier_block trace_die_notifier = {
3958 .notifier_call = trace_die_handler,
3959 .priority = 200
3960};
3961
3962/*
3963 * printk is set to max of 1024, we really don't need it that big.
3964 * Nothing should be printing 1000 characters anyway.
3965 */
3966#define TRACE_MAX_PRINT 1000
3967
3968/*
3969 * Define here KERN_TRACE so that we have one place to modify
3970 * it if we decide to change what log level the ftrace dump
3971 * should be at.
3972 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003973#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003974
3975static void
3976trace_printk_seq(struct trace_seq *s)
3977{
3978 /* Probably should print a warning here. */
3979 if (s->len >= 1000)
3980 s->len = 1000;
3981
3982 /* should be zero ended, but we are paranoid. */
3983 s->buffer[s->len] = 0;
3984
3985 printk(KERN_TRACE "%s", s->buffer);
3986
Steven Rostedtf9520752009-03-02 14:04:40 -05003987 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003988}
3989
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003990void ftrace_dump(void)
3991{
3992 static DEFINE_SPINLOCK(ftrace_dump_lock);
3993 /* use static because iter can be a bit big for the stack */
3994 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003995 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003996 unsigned long flags;
3997 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003998
3999 /* only one dump */
4000 spin_lock_irqsave(&ftrace_dump_lock, flags);
4001 if (dump_ran)
4002 goto out;
4003
4004 dump_ran = 1;
4005
4006 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004007 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004008 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004009
Steven Rostedtd7690412008-10-01 00:29:53 -04004010 for_each_tracing_cpu(cpu) {
4011 atomic_inc(&global_trace.data[cpu]->disabled);
4012 }
4013
Török Edwinb54d3de2008-11-22 13:28:48 +02004014 /* don't look at user memory in panic mode */
4015 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4016
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004017 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4018
Steven Rostedte543ad72009-03-04 18:20:36 -05004019 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004020 iter.tr = &global_trace;
4021 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05004022 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004023
4024 /*
4025 * We need to stop all tracing on all CPUS to read the
4026 * the next buffer. This is a bit expensive, but is
4027 * not done often. We fill all what we can read,
4028 * and then release the locks again.
4029 */
4030
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004031 while (!trace_empty(&iter)) {
4032
4033 if (!cnt)
4034 printk(KERN_TRACE "---------------------------------\n");
4035
4036 cnt++;
4037
4038 /* reset all but tr, trace, and overruns */
4039 memset(&iter.seq, 0,
4040 sizeof(struct trace_iterator) -
4041 offsetof(struct trace_iterator, seq));
4042 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4043 iter.pos = -1;
4044
4045 if (find_next_entry_inc(&iter) != NULL) {
4046 print_trace_line(&iter);
4047 trace_consume(&iter);
4048 }
4049
4050 trace_printk_seq(&iter.seq);
4051 }
4052
4053 if (!cnt)
4054 printk(KERN_TRACE " (ftrace buffer empty)\n");
4055 else
4056 printk(KERN_TRACE "---------------------------------\n");
4057
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004058 out:
4059 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4060}
4061
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004062__init static int tracer_alloc_buffers(void)
4063{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004064 struct trace_array_cpu *data;
Steven Rostedt73c51622009-03-11 13:42:01 -04004065 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004066 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304067 int ret = -ENOMEM;
4068
4069 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4070 goto out;
4071
4072 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4073 goto out_free_buffer_mask;
4074
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004075 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4076 goto out_free_tracing_cpumask;
4077
Steven Rostedt73c51622009-03-11 13:42:01 -04004078 /* To save memory, keep the ring buffer size to its minimum */
4079 if (ring_buffer_expanded)
4080 ring_buf_size = trace_buf_size;
4081 else
4082 ring_buf_size = 1;
4083
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304084 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4085 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004086 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004087
Steven Rostedtab464282008-05-12 21:21:00 +02004088 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04004089 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004090 TRACE_BUFFER_FLAGS);
4091 if (!global_trace.buffer) {
4092 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4093 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304094 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004095 }
4096 global_trace.entries = ring_buffer_size(global_trace.buffer);
4097
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304098
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004099#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt73c51622009-03-11 13:42:01 -04004100 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004101 TRACE_BUFFER_FLAGS);
4102 if (!max_tr.buffer) {
4103 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4104 WARN_ON(1);
4105 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304106 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004107 }
4108 max_tr.entries = ring_buffer_size(max_tr.buffer);
4109 WARN_ON(max_tr.entries != global_trace.entries);
4110#endif
4111
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004112 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004113 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004114 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004115 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004116 }
4117
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004118 trace_init_cmdlines();
4119
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004120 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004121 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004122#ifdef CONFIG_BOOT_TRACER
4123 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004124#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004125 /* All seems OK, enable tracing */
4126 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004127
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004128 atomic_notifier_chain_register(&panic_notifier_list,
4129 &trace_panic_notifier);
4130
4131 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304132 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004133
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304134out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004135 free_cpumask_var(tracing_reader_cpumask);
4136out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304137 free_cpumask_var(tracing_cpumask);
4138out_free_buffer_mask:
4139 free_cpumask_var(tracing_buffer_mask);
4140out:
4141 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004142}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004143
4144__init static int clear_boot_tracer(void)
4145{
4146 /*
4147 * The default tracer at boot buffer is an init section.
4148 * This function is called in lateinit. If we did not
4149 * find the boot tracer, then clear it out, to prevent
4150 * later registration from accessing the buffer that is
4151 * about to be freed.
4152 */
4153 if (!default_bootup_tracer)
4154 return 0;
4155
4156 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4157 default_bootup_tracer);
4158 default_bootup_tracer = NULL;
4159
4160 return 0;
4161}
4162
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004163early_initcall(tracer_alloc_buffers);
4164fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004165late_initcall(clear_boot_tracer);