blob: ace685c7018668b58a5b035c5cf7f9b738d555d5 [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
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200158/*
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
165 *
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
169 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200170static struct trace_array global_trace;
171
172static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
173
Steven Rostedt37886f62009-03-17 17:22:06 -0400174cycle_t ftrace_now(int cpu)
175{
176 u64 ts;
177
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace.buffer)
180 return trace_clock_local();
181
182 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
183 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
184
185 return ts;
186}
187
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200188/*
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
192 *
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
197 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200198static struct trace_array max_tr;
199
200static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
201
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200202/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200203static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200204
Steven Rostedt90369902008-11-05 16:05:44 -0500205/**
206 * tracing_is_enabled - return tracer_enabled status
207 *
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
212 */
213int tracing_is_enabled(void)
214{
215 return tracer_enabled;
216}
217
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200218/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
221 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400222 *
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200227 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400228#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400229
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400230static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200231
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200232/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200233static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200234
235/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200236static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200237
238/*
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
242 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200243static int max_tracer_type_len;
244
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200245/*
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
250 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200251static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200252
253/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200254static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
255
Steven Rostedtee6bce52008-11-12 17:52:37 -0500256/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500257unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
Ingo Molnar4e655512008-05-12 21:20:52 +0200259
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200260/**
261 * trace_wake_up - wake up tasks waiting for trace input
262 *
263 * Simply wakes up any task that is blocked on the trace_wait
264 * queue. These is used with trace_poll for tasks polling the trace.
265 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200266void trace_wake_up(void)
267{
Ingo Molnar017730c2008-05-12 21:20:52 +0200268 /*
269 * The runqueue_is_locked() can fail, but this is the best we
270 * have for now:
271 */
272 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200273 wake_up(&trace_wait);
274}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200275
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400276static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200277{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400278 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200279 int ret;
280
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200281 if (!str)
282 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400283 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200284 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400285 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200286 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400287 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200288 return 1;
289}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400290__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200291
Steven Rostedt57f50be2008-05-12 21:20:44 +0200292unsigned long nsecs_to_usecs(unsigned long nsecs)
293{
294 return nsecs / 1000;
295}
296
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200297/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200298static const char *trace_options[] = {
299 "print-parent",
300 "sym-offset",
301 "sym-addr",
302 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200303 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200304 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200305 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200306 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200307 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200308 "sched-tree",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100309 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500310 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500311 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500312 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200313 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200314 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100315 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200316 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500317 "latency-format",
Steven Rostedtaf4617b2009-03-17 18:09:55 -0400318 "global-clock",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200319 NULL
320};
321
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200322/*
323 * ftrace_max_lock is used to protect the swapping of buffers
324 * when taking a max snapshot. The buffers themselves are
325 * protected by per_cpu spinlocks. But the action of the swap
326 * needs its own lock.
327 *
328 * This is defined as a raw_spinlock_t in order to help
329 * with performance when lockdep debugging is enabled.
330 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200331static raw_spinlock_t ftrace_max_lock =
332 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200333
334/*
335 * Copy the new maximum trace into the separate maximum-trace
336 * structure. (this way the maximum trace is permanently saved,
337 * for later retrieval via /debugfs/tracing/latency_trace)
338 */
Ingo Molnare309b412008-05-12 21:20:51 +0200339static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200340__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
341{
342 struct trace_array_cpu *data = tr->data[cpu];
343
344 max_tr.cpu = cpu;
345 max_tr.time_start = data->preempt_timestamp;
346
347 data = max_tr.data[cpu];
348 data->saved_latency = tracing_max_latency;
349
350 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
351 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100352 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200353 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
354 data->policy = tsk->policy;
355 data->rt_priority = tsk->rt_priority;
356
357 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500358 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200359}
360
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200361ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
362{
363 int len;
364 int ret;
365
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500366 if (!cnt)
367 return 0;
368
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200369 if (s->len <= s->readpos)
370 return -EBUSY;
371
372 len = s->len - s->readpos;
373 if (cnt > len)
374 cnt = len;
375 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500376 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200377 return -EFAULT;
378
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500379 cnt -= ret;
380
Steven Rostedte74da522009-03-04 20:31:11 -0500381 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200382 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200383}
384
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200385static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200386{
387 int len;
388 void *ret;
389
390 if (s->len <= s->readpos)
391 return -EBUSY;
392
393 len = s->len - s->readpos;
394 if (cnt > len)
395 cnt = len;
396 ret = memcpy(buf, s->buffer + s->readpos, cnt);
397 if (!ret)
398 return -EFAULT;
399
Steven Rostedte74da522009-03-04 20:31:11 -0500400 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200401 return cnt;
402}
403
Ingo Molnare309b412008-05-12 21:20:51 +0200404static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200405trace_print_seq(struct seq_file *m, struct trace_seq *s)
406{
407 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
408
409 s->buffer[len] = 0;
410 seq_puts(m, s->buffer);
411
Steven Rostedtf9520752009-03-02 14:04:40 -0500412 trace_seq_init(s);
Steven Rostedt214023c2008-05-12 21:20:46 +0200413}
414
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200415/**
416 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
417 * @tr: tracer
418 * @tsk: the task with the latency
419 * @cpu: The cpu that initiated the trace.
420 *
421 * Flip the buffers between the @tr and the max_tr and record information
422 * about which task was the cause of this latency.
423 */
Ingo Molnare309b412008-05-12 21:20:51 +0200424void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200425update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
426{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400427 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200428
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200429 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200430 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400431
432 tr->buffer = max_tr.buffer;
433 max_tr.buffer = buf;
434
Steven Rostedtd7690412008-10-01 00:29:53 -0400435 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400436 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400437 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200438
439 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200440 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200441}
442
443/**
444 * update_max_tr_single - only copy one trace over, and reset the rest
445 * @tr - tracer
446 * @tsk - task with the latency
447 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200448 *
449 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200450 */
Ingo Molnare309b412008-05-12 21:20:51 +0200451void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200452update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
453{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400454 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200455
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200456 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200457 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200458
Steven Rostedtd7690412008-10-01 00:29:53 -0400459 ftrace_disable_cpu();
460
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400461 ring_buffer_reset(max_tr.buffer);
462 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
463
Steven Rostedtd7690412008-10-01 00:29:53 -0400464 ftrace_enable_cpu();
465
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500466 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200467
468 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200469 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200470}
471
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200472/**
473 * register_tracer - register a tracer with the ftrace system.
474 * @type - the plugin for the tracer
475 *
476 * Register a new plugin tracer.
477 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200478int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100479__releases(kernel_lock)
480__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200481{
482 struct tracer *t;
483 int len;
484 int ret = 0;
485
486 if (!type->name) {
487 pr_info("Tracer must have a name\n");
488 return -1;
489 }
490
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100491 /*
492 * When this gets called we hold the BKL which means that
493 * preemption is disabled. Various trace selftests however
494 * need to disable and enable preemption for successful tests.
495 * So we drop the BKL here and grab it after the tests again.
496 */
497 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200498 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100499
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100500 tracing_selftest_running = true;
501
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200502 for (t = trace_types; t; t = t->next) {
503 if (strcmp(type->name, t->name) == 0) {
504 /* already found */
505 pr_info("Trace %s already registered\n",
506 type->name);
507 ret = -1;
508 goto out;
509 }
510 }
511
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100512 if (!type->set_flag)
513 type->set_flag = &dummy_set_flag;
514 if (!type->flags)
515 type->flags = &dummy_tracer_flags;
516 else
517 if (!type->flags->opts)
518 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100519 if (!type->wait_pipe)
520 type->wait_pipe = default_wait_pipe;
521
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100522
Steven Rostedt60a11772008-05-12 21:20:44 +0200523#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500524 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200525 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200526 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200527 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100528
Steven Rostedt60a11772008-05-12 21:20:44 +0200529 /*
530 * Run a selftest on this tracer.
531 * Here we reset the trace buffer, and set the current
532 * tracer to be this tracer. The tracer can then run some
533 * internal tracing to verify that everything is in order.
534 * If we fail, we do not register this tracer.
535 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100536 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400537 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100538
Steven Rostedt60a11772008-05-12 21:20:44 +0200539 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200540 /* the test is responsible for initializing and enabling */
541 pr_info("Testing tracer %s: ", type->name);
542 ret = type->selftest(type, tr);
543 /* the test is responsible for resetting too */
544 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200545 if (ret) {
546 printk(KERN_CONT "FAILED!\n");
547 goto out;
548 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200549 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100550 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400551 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100552
Steven Rostedt60a11772008-05-12 21:20:44 +0200553 printk(KERN_CONT "PASSED\n");
554 }
555#endif
556
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200557 type->next = trace_types;
558 trace_types = type;
559 len = strlen(type->name);
560 if (len > max_tracer_type_len)
561 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200562
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200563 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100564 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200565 mutex_unlock(&trace_types_lock);
566
Steven Rostedtdac74942009-02-05 01:13:38 -0500567 if (ret || !default_bootup_tracer)
568 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500569
Steven Rostedtdac74942009-02-05 01:13:38 -0500570 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
571 goto out_unlock;
572
573 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
574 /* Do we want this tracer to start on bootup? */
575 tracing_set_tracer(type->name);
576 default_bootup_tracer = NULL;
577 /* disable other selftests, since this will break it. */
578 tracing_selftest_disabled = 1;
579#ifdef CONFIG_FTRACE_STARTUP_TEST
580 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
581 type->name);
582#endif
583
584 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500585 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200586 return ret;
587}
588
589void unregister_tracer(struct tracer *type)
590{
591 struct tracer **t;
592 int len;
593
594 mutex_lock(&trace_types_lock);
595 for (t = &trace_types; *t; t = &(*t)->next) {
596 if (*t == type)
597 goto found;
598 }
599 pr_info("Trace %s not registered\n", type->name);
600 goto out;
601
602 found:
603 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200604
605 if (type == current_trace && tracer_enabled) {
606 tracer_enabled = 0;
607 tracing_stop();
608 if (current_trace->stop)
609 current_trace->stop(&global_trace);
610 current_trace = &nop_trace;
611 }
612
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200613 if (strlen(type->name) != max_tracer_type_len)
614 goto out;
615
616 max_tracer_type_len = 0;
617 for (t = &trace_types; *t; t = &(*t)->next) {
618 len = strlen((*t)->name);
619 if (len > max_tracer_type_len)
620 max_tracer_type_len = len;
621 }
622 out:
623 mutex_unlock(&trace_types_lock);
624}
625
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400626void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200627{
Steven Rostedtd7690412008-10-01 00:29:53 -0400628 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400629 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400630 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200631}
632
Pekka J Enberg213cc062008-12-19 12:08:39 +0200633void tracing_reset_online_cpus(struct trace_array *tr)
634{
635 int cpu;
636
637 tr->time_start = ftrace_now(tr->cpu);
638
639 for_each_online_cpu(cpu)
640 tracing_reset(tr, cpu);
641}
642
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200643#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100644#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200645static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
646static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
647static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
648static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100649static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200650
Steven Rostedt25b0b442008-05-12 21:21:00 +0200651/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100652static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653
654static void trace_init_cmdlines(void)
655{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100656 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
657 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200658 cmdline_idx = 0;
659}
660
Steven Rostedt0f048702008-11-05 16:05:44 -0500661static int trace_stop_count;
662static DEFINE_SPINLOCK(tracing_start_lock);
663
664/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500665 * ftrace_off_permanent - disable all ftrace code permanently
666 *
667 * This should only be called when a serious anomally has
668 * been detected. This will turn off the function tracing,
669 * ring buffers, and other tracing utilites. It takes no
670 * locks and can be called from any context.
671 */
672void ftrace_off_permanent(void)
673{
674 tracing_disabled = 1;
675 ftrace_stop();
676 tracing_off_permanent();
677}
678
679/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500680 * tracing_start - quick start of the tracer
681 *
682 * If tracing is enabled but was stopped by tracing_stop,
683 * this will start the tracer back up.
684 */
685void tracing_start(void)
686{
687 struct ring_buffer *buffer;
688 unsigned long flags;
689
690 if (tracing_disabled)
691 return;
692
693 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500694 if (--trace_stop_count) {
695 if (trace_stop_count < 0) {
696 /* Someone screwed up their debugging */
697 WARN_ON_ONCE(1);
698 trace_stop_count = 0;
699 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500700 goto out;
701 }
702
703
704 buffer = global_trace.buffer;
705 if (buffer)
706 ring_buffer_record_enable(buffer);
707
708 buffer = max_tr.buffer;
709 if (buffer)
710 ring_buffer_record_enable(buffer);
711
712 ftrace_start();
713 out:
714 spin_unlock_irqrestore(&tracing_start_lock, flags);
715}
716
717/**
718 * tracing_stop - quick stop of the tracer
719 *
720 * Light weight way to stop tracing. Use in conjunction with
721 * tracing_start.
722 */
723void tracing_stop(void)
724{
725 struct ring_buffer *buffer;
726 unsigned long flags;
727
728 ftrace_stop();
729 spin_lock_irqsave(&tracing_start_lock, flags);
730 if (trace_stop_count++)
731 goto out;
732
733 buffer = global_trace.buffer;
734 if (buffer)
735 ring_buffer_record_disable(buffer);
736
737 buffer = max_tr.buffer;
738 if (buffer)
739 ring_buffer_record_disable(buffer);
740
741 out:
742 spin_unlock_irqrestore(&tracing_start_lock, flags);
743}
744
Ingo Molnare309b412008-05-12 21:20:51 +0200745void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200746
Ingo Molnare309b412008-05-12 21:20:51 +0200747static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200748{
Carsten Emdea635cf02009-03-18 09:00:41 +0100749 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200750
751 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
752 return;
753
754 /*
755 * It's not the end of the world if we don't get
756 * the lock, but we also don't want to spin
757 * nor do we want to disable interrupts,
758 * so if we miss here, then better luck next time.
759 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100760 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200761 return;
762
763 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100764 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200765 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
766
Carsten Emdea635cf02009-03-18 09:00:41 +0100767 /*
768 * Check whether the cmdline buffer at idx has a pid
769 * mapped. We are going to overwrite that entry so we
770 * need to clear the map_pid_to_cmdline. Otherwise we
771 * would read the new comm for the old pid.
772 */
773 pid = map_cmdline_to_pid[idx];
774 if (pid != NO_CMDLINE_MAP)
775 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200776
Carsten Emdea635cf02009-03-18 09:00:41 +0100777 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200778 map_pid_to_cmdline[tsk->pid] = idx;
779
780 cmdline_idx = idx;
781 }
782
783 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
784
Peter Zijlstraefed7922009-03-04 12:32:55 +0100785 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200786}
787
Steven Rostedt4ca530852009-03-16 19:20:15 -0400788void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200789{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200790 unsigned map;
791
Steven Rostedt4ca530852009-03-16 19:20:15 -0400792 if (!pid) {
793 strcpy(comm, "<idle>");
794 return;
795 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200796
Steven Rostedt4ca530852009-03-16 19:20:15 -0400797 if (pid > PID_MAX_DEFAULT) {
798 strcpy(comm, "<...>");
799 return;
800 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200801
Steven Rostedt4ca530852009-03-16 19:20:15 -0400802 __raw_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200803 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +0100804 if (map != NO_CMDLINE_MAP)
805 strcpy(comm, saved_cmdlines[map]);
806 else
807 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808
Steven Rostedt4ca530852009-03-16 19:20:15 -0400809 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200810}
811
Ingo Molnare309b412008-05-12 21:20:51 +0200812void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200813{
Thomas Gleixner18aecd32009-03-18 08:56:58 +0100814 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
815 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200816 return;
817
818 trace_save_cmdline(tsk);
819}
820
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300821void
Steven Rostedt38697052008-10-01 13:14:09 -0400822tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
823 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200824{
825 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200826
Steven Rostedt777e2082008-09-29 23:02:42 -0400827 entry->preempt_count = pc & 0xff;
828 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedtef180122009-03-10 14:10:56 -0400829 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400830 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400831#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400832 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400833#else
834 TRACE_FLAG_IRQS_NOSUPPORT |
835#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200836 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
837 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
838 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
839}
840
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200841struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
842 unsigned char type,
843 unsigned long len,
844 unsigned long flags, int pc)
845{
846 struct ring_buffer_event *event;
847
848 event = ring_buffer_lock_reserve(tr->buffer, len);
849 if (event != NULL) {
850 struct trace_entry *ent = ring_buffer_event_data(event);
851
852 tracing_generic_entry_update(ent, flags, pc);
853 ent->type = type;
854 }
855
856 return event;
857}
858static void ftrace_trace_stack(struct trace_array *tr,
859 unsigned long flags, int skip, int pc);
860static void ftrace_trace_userstack(struct trace_array *tr,
861 unsigned long flags, int pc);
862
863void trace_buffer_unlock_commit(struct trace_array *tr,
864 struct ring_buffer_event *event,
865 unsigned long flags, int pc)
866{
867 ring_buffer_unlock_commit(tr->buffer, event);
868
869 ftrace_trace_stack(tr, flags, 6, pc);
870 ftrace_trace_userstack(tr, flags, pc);
871 trace_wake_up();
872}
873
Steven Rostedtef5580d2009-02-27 19:38:04 -0500874struct ring_buffer_event *
875trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
876 unsigned long flags, int pc)
877{
878 return trace_buffer_lock_reserve(&global_trace,
879 type, len, flags, pc);
880}
881
882void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
883 unsigned long flags, int pc)
884{
885 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
886}
887
Ingo Molnare309b412008-05-12 21:20:51 +0200888void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500889trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400890 unsigned long ip, unsigned long parent_ip, unsigned long flags,
891 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200892{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400893 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400894 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200895
Steven Rostedtd7690412008-10-01 00:29:53 -0400896 /* If we are reading the ring buffer, don't trace */
897 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
898 return;
899
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200900 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
901 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400902 if (!event)
903 return;
904 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400905 entry->ip = ip;
906 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200907 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200908}
909
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100910#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100911static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100912 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100913 unsigned long flags,
914 int pc)
915{
916 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100917 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100918
919 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
920 return;
921
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200922 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
923 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100924 if (!event)
925 return;
926 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100927 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200928 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100929}
930
931static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100932 struct ftrace_graph_ret *trace,
933 unsigned long flags,
934 int pc)
935{
936 struct ring_buffer_event *event;
937 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100938
939 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
940 return;
941
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200942 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
943 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100944 if (!event)
945 return;
946 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100947 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200948 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100949}
950#endif
951
Ingo Molnare309b412008-05-12 21:20:51 +0200952void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200953ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400954 unsigned long ip, unsigned long parent_ip, unsigned long flags,
955 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200956{
957 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500958 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200959}
960
Steven Rostedt53614992009-01-15 19:12:40 -0500961static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500962 unsigned long flags,
963 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200964{
Al Viroc2c80522008-10-31 19:50:41 +0000965#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400966 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400967 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200968 struct stack_trace trace;
969
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200970 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
971 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400972 if (!event)
973 return;
974 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400975 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200976
977 trace.nr_entries = 0;
978 trace.max_entries = FTRACE_STACK_ENTRIES;
979 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400980 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200981
982 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200983 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000984#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200985}
986
Steven Rostedt53614992009-01-15 19:12:40 -0500987static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500988 unsigned long flags,
989 int skip, int pc)
990{
991 if (!(trace_flags & TRACE_ITER_STACKTRACE))
992 return;
993
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500994 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500995}
996
Steven Rostedt38697052008-10-01 13:14:09 -0400997void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400998 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500999 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001000{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001001 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -04001002}
1003
Török Edwin02b67512008-11-22 13:28:47 +02001004static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001005 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001006{
Török Edwinc7425ac2008-11-28 11:17:56 +02001007#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +02001008 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001009 struct userstack_entry *entry;
1010 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001011
1012 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1013 return;
1014
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001015 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1016 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001017 if (!event)
1018 return;
1019 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001020
1021 memset(&entry->caller, 0, sizeof(entry->caller));
1022
1023 trace.nr_entries = 0;
1024 trace.max_entries = FTRACE_STACK_ENTRIES;
1025 trace.skip = 0;
1026 trace.entries = entry->caller;
1027
1028 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001029 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001030#endif
Török Edwin02b67512008-11-22 13:28:47 +02001031}
1032
Hannes Eder4fd27352009-02-10 19:44:12 +01001033#ifdef UNUSED
1034static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001035{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001036 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001037}
Hannes Eder4fd27352009-02-10 19:44:12 +01001038#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001039
Steven Rostedt38697052008-10-01 13:14:09 -04001040static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001041ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001042 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1043 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +02001044{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001045 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001046 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001047 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001048
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001049 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1050 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001051 if (!event)
1052 return;
1053 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001054 entry->arg1 = arg1;
1055 entry->arg2 = arg2;
1056 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001057 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +02001058}
1059
1060void
Steven Rostedt38697052008-10-01 13:14:09 -04001061__trace_special(void *__tr, void *__data,
1062 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1063{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001064 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001065}
1066
1067void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001069 struct task_struct *prev,
1070 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001071 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001072{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001073 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001074 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001075
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001076 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1077 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001078 if (!event)
1079 return;
1080 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001081 entry->prev_pid = prev->pid;
1082 entry->prev_prio = prev->prio;
1083 entry->prev_state = prev->state;
1084 entry->next_pid = next->pid;
1085 entry->next_prio = next->prio;
1086 entry->next_state = next->state;
1087 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001088 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001089}
1090
Ingo Molnar57422792008-05-12 21:20:51 +02001091void
1092tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001093 struct task_struct *wakee,
1094 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001095 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001096{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001097 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001098 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001099
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001100 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1101 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001102 if (!event)
1103 return;
1104 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001105 entry->prev_pid = curr->pid;
1106 entry->prev_prio = curr->prio;
1107 entry->prev_state = curr->state;
1108 entry->next_pid = wakee->pid;
1109 entry->next_prio = wakee->prio;
1110 entry->next_state = wakee->state;
1111 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001112
1113 ring_buffer_unlock_commit(tr->buffer, event);
1114 ftrace_trace_stack(tr, flags, 6, pc);
1115 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001116}
1117
Steven Rostedt4902f882008-05-22 00:22:18 -04001118void
1119ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1120{
1121 struct trace_array *tr = &global_trace;
1122 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001123 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001124 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001125 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001126
Steven Rostedtc76f0692008-11-07 22:36:02 -05001127 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001128 return;
1129
Steven Rostedt38697052008-10-01 13:14:09 -04001130 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001131 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001132 cpu = raw_smp_processor_id();
1133 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001134
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001135 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001136 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001137
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001138 atomic_dec(&data->disabled);
1139 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001140}
1141
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001142#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001143int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001144{
1145 struct trace_array *tr = &global_trace;
1146 struct trace_array_cpu *data;
1147 unsigned long flags;
1148 long disabled;
1149 int cpu;
1150 int pc;
1151
Steven Rostedt804a6852008-12-03 15:36:59 -05001152 if (!ftrace_trace_task(current))
1153 return 0;
1154
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001155 if (!ftrace_graph_addr(trace->func))
1156 return 0;
1157
Steven Rostedta5e25882008-12-02 15:34:05 -05001158 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001159 cpu = raw_smp_processor_id();
1160 data = tr->data[cpu];
1161 disabled = atomic_inc_return(&data->disabled);
1162 if (likely(disabled == 1)) {
1163 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001164 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001165 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001166 /* Only do the atomic if it is not already set */
1167 if (!test_tsk_trace_graph(current))
1168 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001169 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001170 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001171
1172 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001173}
1174
1175void trace_graph_return(struct ftrace_graph_ret *trace)
1176{
1177 struct trace_array *tr = &global_trace;
1178 struct trace_array_cpu *data;
1179 unsigned long flags;
1180 long disabled;
1181 int cpu;
1182 int pc;
1183
Steven Rostedta5e25882008-12-02 15:34:05 -05001184 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001185 cpu = raw_smp_processor_id();
1186 data = tr->data[cpu];
1187 disabled = atomic_inc_return(&data->disabled);
1188 if (likely(disabled == 1)) {
1189 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001190 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001191 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001192 if (!trace->depth)
1193 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001194 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001195 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001196}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001197#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001198
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001199
1200/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001201 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001202 *
1203 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001204int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001205{
Steven Rostedt80370cb2009-03-10 17:16:35 -04001206 static raw_spinlock_t trace_buf_lock =
1207 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001208 static u32 trace_buf[TRACE_BUF_SIZE];
1209
1210 struct ring_buffer_event *event;
1211 struct trace_array *tr = &global_trace;
1212 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001213 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001214 unsigned long flags;
1215 int resched;
1216 int cpu, len = 0, size, pc;
1217
1218 if (unlikely(tracing_selftest_running || tracing_disabled))
1219 return 0;
1220
1221 /* Don't pollute graph traces with trace_vprintk internals */
1222 pause_graph_tracing();
1223
1224 pc = preempt_count();
1225 resched = ftrace_preempt_disable();
1226 cpu = raw_smp_processor_id();
1227 data = tr->data[cpu];
1228
1229 if (unlikely(atomic_read(&data->disabled)))
1230 goto out;
1231
Steven Rostedt80370cb2009-03-10 17:16:35 -04001232 /* Lockdep uses trace_printk for lock tracing */
1233 local_irq_save(flags);
1234 __raw_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001235 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1236
1237 if (len > TRACE_BUF_SIZE || len < 0)
1238 goto out_unlock;
1239
1240 size = sizeof(*entry) + sizeof(u32) * len;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001241 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001242 if (!event)
1243 goto out_unlock;
1244 entry = ring_buffer_event_data(event);
1245 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001246 entry->fmt = fmt;
1247
1248 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1249 ring_buffer_unlock_commit(tr->buffer, event);
1250
1251out_unlock:
Steven Rostedt80370cb2009-03-10 17:16:35 -04001252 __raw_spin_unlock(&trace_buf_lock);
1253 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001254
1255out:
1256 ftrace_preempt_enable(resched);
1257 unpause_graph_tracing();
1258
1259 return len;
1260}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001261EXPORT_SYMBOL_GPL(trace_vbprintk);
1262
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001263int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001264{
1265 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1266 static char trace_buf[TRACE_BUF_SIZE];
1267
1268 struct ring_buffer_event *event;
1269 struct trace_array *tr = &global_trace;
1270 struct trace_array_cpu *data;
1271 int cpu, len = 0, size, pc;
1272 struct print_entry *entry;
1273 unsigned long irq_flags;
1274
1275 if (tracing_disabled || tracing_selftest_running)
1276 return 0;
1277
1278 pc = preempt_count();
1279 preempt_disable_notrace();
1280 cpu = raw_smp_processor_id();
1281 data = tr->data[cpu];
1282
1283 if (unlikely(atomic_read(&data->disabled)))
1284 goto out;
1285
1286 pause_graph_tracing();
1287 raw_local_irq_save(irq_flags);
1288 __raw_spin_lock(&trace_buf_lock);
1289 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1290
1291 len = min(len, TRACE_BUF_SIZE-1);
1292 trace_buf[len] = 0;
1293
1294 size = sizeof(*entry) + len + 1;
1295 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1296 if (!event)
1297 goto out_unlock;
1298 entry = ring_buffer_event_data(event);
1299 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001300
1301 memcpy(&entry->buf, trace_buf, len);
1302 entry->buf[len] = 0;
1303 ring_buffer_unlock_commit(tr->buffer, event);
1304
1305 out_unlock:
1306 __raw_spin_unlock(&trace_buf_lock);
1307 raw_local_irq_restore(irq_flags);
1308 unpause_graph_tracing();
1309 out:
1310 preempt_enable_notrace();
1311
1312 return len;
1313}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001314EXPORT_SYMBOL_GPL(trace_vprintk);
1315
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316enum trace_file_type {
1317 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001318 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001319};
1320
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001321static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001322{
Steven Rostedtd7690412008-10-01 00:29:53 -04001323 /* Don't allow ftrace to trace into the ring buffers */
1324 ftrace_disable_cpu();
1325
Steven Rostedt5a90f572008-09-03 17:42:51 -04001326 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001327 if (iter->buffer_iter[iter->cpu])
1328 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1329
1330 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001331}
1332
Ingo Molnare309b412008-05-12 21:20:51 +02001333static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001334peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001335{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001336 struct ring_buffer_event *event;
1337 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001338
Steven Rostedtd7690412008-10-01 00:29:53 -04001339 /* Don't allow ftrace to trace into the ring buffers */
1340 ftrace_disable_cpu();
1341
1342 if (buf_iter)
1343 event = ring_buffer_iter_peek(buf_iter, ts);
1344 else
1345 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1346
1347 ftrace_enable_cpu();
1348
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001349 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001350}
Steven Rostedtd7690412008-10-01 00:29:53 -04001351
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001352static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001353__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001354{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001355 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001356 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001357 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001358 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001359 int next_cpu = -1;
1360 int cpu;
1361
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001362 /*
1363 * If we are in a per_cpu trace file, don't bother by iterating over
1364 * all cpu and peek directly.
1365 */
1366 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1367 if (ring_buffer_empty_cpu(buffer, cpu_file))
1368 return NULL;
1369 ent = peek_next_entry(iter, cpu_file, ent_ts);
1370 if (ent_cpu)
1371 *ent_cpu = cpu_file;
1372
1373 return ent;
1374 }
1375
Steven Rostedtab464282008-05-12 21:21:00 +02001376 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001377
1378 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001379 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001380
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001381 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001382
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001383 /*
1384 * Pick the entry with the smallest timestamp:
1385 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001386 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387 next = ent;
1388 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001389 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001390 }
1391 }
1392
1393 if (ent_cpu)
1394 *ent_cpu = next_cpu;
1395
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001396 if (ent_ts)
1397 *ent_ts = next_ts;
1398
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001399 return next;
1400}
1401
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001402/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001403struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1404 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001405{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001406 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001407}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001408
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001409/* Find the next real entry, and increment the iterator to the next entry */
1410static void *find_next_entry_inc(struct trace_iterator *iter)
1411{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001412 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001413
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001414 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001415 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001416
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001417 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001418}
1419
Ingo Molnare309b412008-05-12 21:20:51 +02001420static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001421{
Steven Rostedtd7690412008-10-01 00:29:53 -04001422 /* Don't allow ftrace to trace into the ring buffers */
1423 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001424 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001425 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001426}
1427
Ingo Molnare309b412008-05-12 21:20:51 +02001428static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001429{
1430 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001431 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001432 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001433
1434 (*pos)++;
1435
1436 /* can't go backwards */
1437 if (iter->idx > i)
1438 return NULL;
1439
1440 if (iter->idx < 0)
1441 ent = find_next_entry_inc(iter);
1442 else
1443 ent = iter;
1444
1445 while (ent && iter->idx < i)
1446 ent = find_next_entry_inc(iter);
1447
1448 iter->pos = *pos;
1449
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001450 return ent;
1451}
1452
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001453/*
1454 * No necessary locking here. The worst thing which can
1455 * happen is loosing events consumed at the same time
1456 * by a trace_pipe reader.
1457 * Other than that, we don't risk to crash the ring buffer
1458 * because it serializes the readers.
1459 *
1460 * The current tracer is copied to avoid a global locking
1461 * all around.
1462 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001463static void *s_start(struct seq_file *m, loff_t *pos)
1464{
1465 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001466 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001467 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001468 void *p = NULL;
1469 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001470 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001471
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001472 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001473 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001474 if (unlikely(old_tracer != current_trace && current_trace)) {
1475 old_tracer = current_trace;
1476 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001477 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001478 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001479
1480 atomic_inc(&trace_record_cmdline_disabled);
1481
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001482 if (*pos != iter->pos) {
1483 iter->ent = NULL;
1484 iter->cpu = 0;
1485 iter->idx = -1;
1486
Steven Rostedtd7690412008-10-01 00:29:53 -04001487 ftrace_disable_cpu();
1488
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001489 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1490 for_each_tracing_cpu(cpu)
1491 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1492 } else
1493 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1494
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001495
Steven Rostedtd7690412008-10-01 00:29:53 -04001496 ftrace_enable_cpu();
1497
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001498 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1499 ;
1500
1501 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001502 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001503 p = s_next(m, p, &l);
1504 }
1505
1506 return p;
1507}
1508
1509static void s_stop(struct seq_file *m, void *p)
1510{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001511 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001512}
1513
Ingo Molnare309b412008-05-12 21:20:51 +02001514static void print_lat_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, "# _------=> CPU# \n");
1517 seq_puts(m, "# / _-----=> irqs-off \n");
1518 seq_puts(m, "# | / _----=> need-resched \n");
1519 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1520 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1521 seq_puts(m, "# |||| / \n");
1522 seq_puts(m, "# ||||| delay \n");
1523 seq_puts(m, "# cmd pid ||||| time | caller \n");
1524 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001525}
1526
Ingo Molnare309b412008-05-12 21:20:51 +02001527static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001528{
Michael Ellermana6168352008-08-20 16:36:11 -07001529 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1530 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001531}
1532
1533
Ingo Molnare309b412008-05-12 21:20:51 +02001534static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1536{
1537 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1538 struct trace_array *tr = iter->tr;
1539 struct trace_array_cpu *data = tr->data[tr->cpu];
1540 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001541 unsigned long total;
1542 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001543 const char *name = "preemption";
1544
1545 if (type)
1546 name = type->name;
1547
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001548 entries = ring_buffer_entries(iter->tr->buffer);
1549 total = entries +
1550 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001551
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001552 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001553 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001554 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001555 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001556 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001557 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001558 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001559 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001560 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001561 tr->cpu,
1562#if defined(CONFIG_PREEMPT_NONE)
1563 "server",
1564#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1565 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001566#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001567 "preempt",
1568#else
1569 "unknown",
1570#endif
1571 /* These are reserved for later use */
1572 0, 0, 0, 0);
1573#ifdef CONFIG_SMP
1574 seq_printf(m, " #P:%d)\n", num_online_cpus());
1575#else
1576 seq_puts(m, ")\n");
1577#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001578 seq_puts(m, "# -----------------\n");
1579 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1581 data->comm, data->pid, data->uid, data->nice,
1582 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001583 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001584
1585 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001586 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001587 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1588 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001589 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001590 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1591 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001592 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001593 }
1594
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001595 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001596}
1597
Steven Rostedta3097202008-11-07 22:36:02 -05001598static void test_cpu_buff_start(struct trace_iterator *iter)
1599{
1600 struct trace_seq *s = &iter->seq;
1601
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001602 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1603 return;
1604
1605 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1606 return;
1607
Rusty Russell44623442009-01-01 10:12:23 +10301608 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001609 return;
1610
Rusty Russell44623442009-01-01 10:12:23 +10301611 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001612 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1613}
1614
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001615static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001616{
Steven Rostedt214023c2008-05-12 21:20:46 +02001617 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001618 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001619 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001620 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001621
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001622 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001623
Steven Rostedta3097202008-11-07 22:36:02 -05001624 test_cpu_buff_start(iter);
1625
Steven Rostedtf633cef2008-12-23 23:24:13 -05001626 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001627
1628 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001629 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1630 if (!trace_print_lat_context(iter))
1631 goto partial;
1632 } else {
1633 if (!trace_print_context(iter))
1634 goto partial;
1635 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001636 }
1637
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001638 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001639 return event->trace(iter, sym_flags);
1640
1641 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1642 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001643
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001644 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001645partial:
1646 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001647}
1648
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001649static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001650{
1651 struct trace_seq *s = &iter->seq;
1652 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001653 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001654
1655 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001656
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001657 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001658 if (!trace_seq_printf(s, "%d %d %llu ",
1659 entry->pid, iter->cpu, iter->ts))
1660 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001661 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001662
Steven Rostedtf633cef2008-12-23 23:24:13 -05001663 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001664 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001665 return event->raw(iter, 0);
1666
1667 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1668 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001669
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001670 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001671partial:
1672 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001673}
1674
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001675static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001676{
1677 struct trace_seq *s = &iter->seq;
1678 unsigned char newline = '\n';
1679 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001680 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001681
1682 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001683
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001684 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1685 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1686 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1687 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1688 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001689
Steven Rostedtf633cef2008-12-23 23:24:13 -05001690 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001691 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001692 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001693 if (ret != TRACE_TYPE_HANDLED)
1694 return ret;
1695 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001696
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001697 SEQ_PUT_FIELD_RET(s, newline);
1698
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001699 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001700}
1701
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001702static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001703{
1704 struct trace_seq *s = &iter->seq;
1705 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001706 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001707
1708 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001709
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001710 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1711 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001712 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001713 SEQ_PUT_FIELD_RET(s, iter->ts);
1714 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001715
Steven Rostedtf633cef2008-12-23 23:24:13 -05001716 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001717 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001718}
1719
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001720static int trace_empty(struct trace_iterator *iter)
1721{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001722 int cpu;
1723
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001724 /* If we are looking at one CPU buffer, only check that one */
1725 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1726 cpu = iter->cpu_file;
1727 if (iter->buffer_iter[cpu]) {
1728 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1729 return 0;
1730 } else {
1731 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1732 return 0;
1733 }
1734 return 1;
1735 }
1736
Steven Rostedtab464282008-05-12 21:21:00 +02001737 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001738 if (iter->buffer_iter[cpu]) {
1739 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1740 return 0;
1741 } else {
1742 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1743 return 0;
1744 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001745 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001746
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001747 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001748}
1749
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001750static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001751{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001752 enum print_line_t ret;
1753
1754 if (iter->trace && iter->trace->print_line) {
1755 ret = iter->trace->print_line(iter);
1756 if (ret != TRACE_TYPE_UNHANDLED)
1757 return ret;
1758 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001759
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001760 if (iter->ent->type == TRACE_BPRINT &&
1761 trace_flags & TRACE_ITER_PRINTK &&
1762 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04001763 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001764
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001765 if (iter->ent->type == TRACE_PRINT &&
1766 trace_flags & TRACE_ITER_PRINTK &&
1767 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04001768 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001769
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001770 if (trace_flags & TRACE_ITER_BIN)
1771 return print_bin_fmt(iter);
1772
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001773 if (trace_flags & TRACE_ITER_HEX)
1774 return print_hex_fmt(iter);
1775
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001776 if (trace_flags & TRACE_ITER_RAW)
1777 return print_raw_fmt(iter);
1778
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001779 return print_trace_fmt(iter);
1780}
1781
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001782static int s_show(struct seq_file *m, void *v)
1783{
1784 struct trace_iterator *iter = v;
1785
1786 if (iter->ent == NULL) {
1787 if (iter->tr) {
1788 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1789 seq_puts(m, "#\n");
1790 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001791 if (iter->trace && iter->trace->print_header)
1792 iter->trace->print_header(m);
1793 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001794 /* print nothing if the buffers are empty */
1795 if (trace_empty(iter))
1796 return 0;
1797 print_trace_header(m, iter);
1798 if (!(trace_flags & TRACE_ITER_VERBOSE))
1799 print_lat_help_header(m);
1800 } else {
1801 if (!(trace_flags & TRACE_ITER_VERBOSE))
1802 print_func_help_header(m);
1803 }
1804 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001805 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001806 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001807 }
1808
1809 return 0;
1810}
1811
1812static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001813 .start = s_start,
1814 .next = s_next,
1815 .stop = s_stop,
1816 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001817};
1818
Ingo Molnare309b412008-05-12 21:20:51 +02001819static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001820__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001821{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001822 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001823 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001824 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001825 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001826 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001827
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001828 if (tracing_disabled)
1829 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001830
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001831 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001832 if (!iter)
1833 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001834
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001835 /*
1836 * We make a copy of the current tracer to avoid concurrent
1837 * changes on it while we are reading.
1838 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001839 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001840 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001841 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001842 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001843
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001844 if (current_trace)
1845 *iter->trace = *current_trace;
1846
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001847 if (current_trace && current_trace->print_max)
1848 iter->tr = &max_tr;
1849 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001850 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001851 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001852 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001853 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001854
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001855 /* Notify the tracer early; before we stop tracing. */
1856 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001857 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001858
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001859 /* Annotate start of buffers if we had overruns */
1860 if (ring_buffer_overruns(iter->tr->buffer))
1861 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1862
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001863 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1864 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001865
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001866 iter->buffer_iter[cpu] =
1867 ring_buffer_read_start(iter->tr->buffer, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001868 }
1869 } else {
1870 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001871 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001872 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001873 }
1874
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001875 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001876 ret = seq_open(file, &tracer_seq_ops);
1877 if (ret < 0) {
1878 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001879 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001880 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001881
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001882 m = file->private_data;
1883 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001884
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001885 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001886 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001887
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001888 mutex_unlock(&trace_types_lock);
1889
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001890 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001891
1892 fail_buffer:
1893 for_each_tracing_cpu(cpu) {
1894 if (iter->buffer_iter[cpu])
1895 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1896 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001897 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001898 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001899 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001900 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001901
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001902 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001903}
1904
1905int tracing_open_generic(struct inode *inode, struct file *filp)
1906{
Steven Rostedt60a11772008-05-12 21:20:44 +02001907 if (tracing_disabled)
1908 return -ENODEV;
1909
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001910 filp->private_data = inode->i_private;
1911 return 0;
1912}
1913
Hannes Eder4fd27352009-02-10 19:44:12 +01001914static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001915{
1916 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001917 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001918 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001919
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001920 if (!(file->f_mode & FMODE_READ))
1921 return 0;
1922
1923 iter = m->private;
1924
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001925 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001926 for_each_tracing_cpu(cpu) {
1927 if (iter->buffer_iter[cpu])
1928 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1929 }
1930
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001931 if (iter->trace && iter->trace->close)
1932 iter->trace->close(iter);
1933
1934 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001935 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001936 mutex_unlock(&trace_types_lock);
1937
1938 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001939 mutex_destroy(&iter->mutex);
1940 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001941 kfree(iter);
1942 return 0;
1943}
1944
1945static int tracing_open(struct inode *inode, struct file *file)
1946{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001947 struct trace_iterator *iter;
1948 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001949
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001950 /* If this file was open for write, then erase contents */
1951 if ((file->f_mode & FMODE_WRITE) &&
1952 !(file->f_flags & O_APPEND)) {
1953 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001954
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001955 if (cpu == TRACE_PIPE_ALL_CPU)
1956 tracing_reset_online_cpus(&global_trace);
1957 else
1958 tracing_reset(&global_trace, cpu);
1959 }
1960
1961 if (file->f_mode & FMODE_READ) {
1962 iter = __tracing_open(inode, file);
1963 if (IS_ERR(iter))
1964 ret = PTR_ERR(iter);
1965 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1966 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1967 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001968 return ret;
1969}
1970
Ingo Molnare309b412008-05-12 21:20:51 +02001971static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001972t_next(struct seq_file *m, void *v, loff_t *pos)
1973{
1974 struct tracer *t = m->private;
1975
1976 (*pos)++;
1977
1978 if (t)
1979 t = t->next;
1980
1981 m->private = t;
1982
1983 return t;
1984}
1985
1986static void *t_start(struct seq_file *m, loff_t *pos)
1987{
1988 struct tracer *t = m->private;
1989 loff_t l = 0;
1990
1991 mutex_lock(&trace_types_lock);
1992 for (; t && l < *pos; t = t_next(m, t, &l))
1993 ;
1994
1995 return t;
1996}
1997
1998static void t_stop(struct seq_file *m, void *p)
1999{
2000 mutex_unlock(&trace_types_lock);
2001}
2002
2003static int t_show(struct seq_file *m, void *v)
2004{
2005 struct tracer *t = v;
2006
2007 if (!t)
2008 return 0;
2009
2010 seq_printf(m, "%s", t->name);
2011 if (t->next)
2012 seq_putc(m, ' ');
2013 else
2014 seq_putc(m, '\n');
2015
2016 return 0;
2017}
2018
2019static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002020 .start = t_start,
2021 .next = t_next,
2022 .stop = t_stop,
2023 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002024};
2025
2026static int show_traces_open(struct inode *inode, struct file *file)
2027{
2028 int ret;
2029
Steven Rostedt60a11772008-05-12 21:20:44 +02002030 if (tracing_disabled)
2031 return -ENODEV;
2032
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002033 ret = seq_open(file, &show_traces_seq_ops);
2034 if (!ret) {
2035 struct seq_file *m = file->private_data;
2036 m->private = trace_types;
2037 }
2038
2039 return ret;
2040}
2041
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002042static ssize_t
2043tracing_write_stub(struct file *filp, const char __user *ubuf,
2044 size_t count, loff_t *ppos)
2045{
2046 return count;
2047}
2048
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002049static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002050 .open = tracing_open,
2051 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002052 .write = tracing_write_stub,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002053 .llseek = seq_lseek,
2054 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002055};
2056
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002057static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002058 .open = show_traces_open,
2059 .read = seq_read,
2060 .release = seq_release,
2061};
2062
Ingo Molnar36dfe922008-05-12 21:20:52 +02002063/*
2064 * Only trace on a CPU if the bitmask is set:
2065 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302066static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002067
2068/*
2069 * The tracer itself will not take this lock, but still we want
2070 * to provide a consistent cpumask to user-space:
2071 */
2072static DEFINE_MUTEX(tracing_cpumask_update_lock);
2073
2074/*
2075 * Temporary storage for the character representation of the
2076 * CPU bitmask (and one more byte for the newline):
2077 */
2078static char mask_str[NR_CPUS + 1];
2079
Ingo Molnarc7078de2008-05-12 21:20:52 +02002080static ssize_t
2081tracing_cpumask_read(struct file *filp, char __user *ubuf,
2082 size_t count, loff_t *ppos)
2083{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002084 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002085
2086 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002087
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302088 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002089 if (count - len < 2) {
2090 count = -EINVAL;
2091 goto out_err;
2092 }
2093 len += sprintf(mask_str + len, "\n");
2094 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2095
2096out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002097 mutex_unlock(&tracing_cpumask_update_lock);
2098
2099 return count;
2100}
2101
2102static ssize_t
2103tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2104 size_t count, loff_t *ppos)
2105{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002106 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302107 cpumask_var_t tracing_cpumask_new;
2108
2109 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2110 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002111
2112 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302113 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002114 if (err)
2115 goto err_unlock;
2116
Steven Rostedta5e25882008-12-02 15:34:05 -05002117 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02002118 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002119 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002120 /*
2121 * Increase/decrease the disabled counter if we are
2122 * about to flip a bit in the cpumask:
2123 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302124 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2125 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002126 atomic_inc(&global_trace.data[cpu]->disabled);
2127 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302128 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2129 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002130 atomic_dec(&global_trace.data[cpu]->disabled);
2131 }
2132 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002133 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002134 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002135
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302136 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002137
Ingo Molnarc7078de2008-05-12 21:20:52 +02002138 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302139 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002140
Ingo Molnarc7078de2008-05-12 21:20:52 +02002141 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002142
2143err_unlock:
2144 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302145 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002146
2147 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002148}
2149
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002150static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002151 .open = tracing_open_generic,
2152 .read = tracing_cpumask_read,
2153 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002154};
2155
2156static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002157tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002158 size_t cnt, loff_t *ppos)
2159{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002160 struct tracer_opt *trace_opts;
2161 u32 tracer_flags;
2162 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002163 char *buf;
2164 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002165 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002166
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002167
Wenji Huangc3706f02009-02-10 01:03:18 -05002168 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002169 for (i = 0; trace_options[i]; i++) {
2170 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002171 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002172 }
2173
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002174 mutex_lock(&trace_types_lock);
2175 tracer_flags = current_trace->flags->val;
2176 trace_opts = current_trace->flags->opts;
2177
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002178 /*
2179 * Increase the size with names of options specific
2180 * of the current tracer.
2181 */
2182 for (i = 0; trace_opts[i].name; i++) {
2183 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002184 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002185 }
2186
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002187 /* +2 for \n and \0 */
2188 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002189 if (!buf) {
2190 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002191 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002192 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002193
2194 for (i = 0; trace_options[i]; i++) {
2195 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002196 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002197 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002198 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002199 }
2200
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002201 for (i = 0; trace_opts[i].name; i++) {
2202 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002203 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002204 trace_opts[i].name);
2205 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002206 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002207 trace_opts[i].name);
2208 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002209 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002210
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002211 WARN_ON(r >= len + 2);
2212
Ingo Molnar36dfe922008-05-12 21:20:52 +02002213 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002214
2215 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002216 return r;
2217}
2218
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002219/* Try to assign a tracer specific option */
2220static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2221{
2222 struct tracer_flags *trace_flags = trace->flags;
2223 struct tracer_opt *opts = NULL;
2224 int ret = 0, i = 0;
2225 int len;
2226
2227 for (i = 0; trace_flags->opts[i].name; i++) {
2228 opts = &trace_flags->opts[i];
2229 len = strlen(opts->name);
2230
2231 if (strncmp(cmp, opts->name, len) == 0) {
2232 ret = trace->set_flag(trace_flags->val,
2233 opts->bit, !neg);
2234 break;
2235 }
2236 }
2237 /* Not found */
2238 if (!trace_flags->opts[i].name)
2239 return -EINVAL;
2240
2241 /* Refused to handle */
2242 if (ret)
2243 return ret;
2244
2245 if (neg)
2246 trace_flags->val &= ~opts->bit;
2247 else
2248 trace_flags->val |= opts->bit;
2249
2250 return 0;
2251}
2252
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002253static void set_tracer_flags(unsigned int mask, int enabled)
2254{
2255 /* do nothing if flag is already set */
2256 if (!!(trace_flags & mask) == !!enabled)
2257 return;
2258
2259 if (enabled)
2260 trace_flags |= mask;
2261 else
2262 trace_flags &= ~mask;
2263
2264 if (mask == TRACE_ITER_GLOBAL_CLK) {
2265 u64 (*func)(void);
2266
2267 if (enabled)
2268 func = trace_clock_global;
2269 else
2270 func = trace_clock_local;
2271
2272 mutex_lock(&trace_types_lock);
2273 ring_buffer_set_clock(global_trace.buffer, func);
2274
2275 if (max_tr.buffer)
2276 ring_buffer_set_clock(max_tr.buffer, func);
2277 mutex_unlock(&trace_types_lock);
2278 }
2279}
2280
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002281static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002282tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002283 size_t cnt, loff_t *ppos)
2284{
2285 char buf[64];
2286 char *cmp = buf;
2287 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002288 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002289 int i;
2290
Steven Rostedtcffae432008-05-12 21:21:00 +02002291 if (cnt >= sizeof(buf))
2292 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002293
2294 if (copy_from_user(&buf, ubuf, cnt))
2295 return -EFAULT;
2296
2297 buf[cnt] = 0;
2298
2299 if (strncmp(buf, "no", 2) == 0) {
2300 neg = 1;
2301 cmp += 2;
2302 }
2303
2304 for (i = 0; trace_options[i]; i++) {
2305 int len = strlen(trace_options[i]);
2306
2307 if (strncmp(cmp, trace_options[i], len) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002308 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002309 break;
2310 }
2311 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002312
2313 /* If no option could be set, test the specific tracer options */
2314 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002315 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002316 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002317 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002318 if (ret)
2319 return ret;
2320 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002321
2322 filp->f_pos += cnt;
2323
2324 return cnt;
2325}
2326
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002327static const struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002328 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002329 .read = tracing_trace_options_read,
2330 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002331};
2332
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002333static const char readme_msg[] =
2334 "tracing mini-HOWTO:\n\n"
2335 "# mkdir /debug\n"
2336 "# mount -t debugfs nodev /debug\n\n"
2337 "# cat /debug/tracing/available_tracers\n"
2338 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2339 "# cat /debug/tracing/current_tracer\n"
2340 "none\n"
2341 "# echo sched_switch > /debug/tracing/current_tracer\n"
2342 "# cat /debug/tracing/current_tracer\n"
2343 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002344 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002345 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002346 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002347 "# echo 1 > /debug/tracing/tracing_enabled\n"
2348 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2349 "echo 0 > /debug/tracing/tracing_enabled\n"
2350;
2351
2352static ssize_t
2353tracing_readme_read(struct file *filp, char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2355{
2356 return simple_read_from_buffer(ubuf, cnt, ppos,
2357 readme_msg, strlen(readme_msg));
2358}
2359
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002360static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002361 .open = tracing_open_generic,
2362 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002363};
2364
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002365static ssize_t
2366tracing_ctrl_read(struct file *filp, char __user *ubuf,
2367 size_t cnt, loff_t *ppos)
2368{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002369 char buf[64];
2370 int r;
2371
Steven Rostedt90369902008-11-05 16:05:44 -05002372 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002373 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002374}
2375
2376static ssize_t
2377tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2378 size_t cnt, loff_t *ppos)
2379{
2380 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002381 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002382 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002383 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002384
Steven Rostedtcffae432008-05-12 21:21:00 +02002385 if (cnt >= sizeof(buf))
2386 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002387
2388 if (copy_from_user(&buf, ubuf, cnt))
2389 return -EFAULT;
2390
2391 buf[cnt] = 0;
2392
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002393 ret = strict_strtoul(buf, 10, &val);
2394 if (ret < 0)
2395 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002396
2397 val = !!val;
2398
2399 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002400 if (tracer_enabled ^ val) {
2401 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002402 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002403 if (current_trace->start)
2404 current_trace->start(tr);
2405 tracing_start();
2406 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002407 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002408 tracing_stop();
2409 if (current_trace->stop)
2410 current_trace->stop(tr);
2411 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002412 }
2413 mutex_unlock(&trace_types_lock);
2414
2415 filp->f_pos += cnt;
2416
2417 return cnt;
2418}
2419
2420static ssize_t
2421tracing_set_trace_read(struct file *filp, char __user *ubuf,
2422 size_t cnt, loff_t *ppos)
2423{
2424 char buf[max_tracer_type_len+2];
2425 int r;
2426
2427 mutex_lock(&trace_types_lock);
2428 if (current_trace)
2429 r = sprintf(buf, "%s\n", current_trace->name);
2430 else
2431 r = sprintf(buf, "\n");
2432 mutex_unlock(&trace_types_lock);
2433
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002434 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002435}
2436
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002437int tracer_init(struct tracer *t, struct trace_array *tr)
2438{
2439 tracing_reset_online_cpus(tr);
2440 return t->init(tr);
2441}
2442
Steven Rostedt73c51622009-03-11 13:42:01 -04002443static int tracing_resize_ring_buffer(unsigned long size)
2444{
2445 int ret;
2446
2447 /*
2448 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002449 * we use the size that was given, and we can forget about
2450 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002451 */
2452 ring_buffer_expanded = 1;
2453
2454 ret = ring_buffer_resize(global_trace.buffer, size);
2455 if (ret < 0)
2456 return ret;
2457
2458 ret = ring_buffer_resize(max_tr.buffer, size);
2459 if (ret < 0) {
2460 int r;
2461
2462 r = ring_buffer_resize(global_trace.buffer,
2463 global_trace.entries);
2464 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002465 /*
2466 * AARGH! We are left with different
2467 * size max buffer!!!!
2468 * The max buffer is our "snapshot" buffer.
2469 * When a tracer needs a snapshot (one of the
2470 * latency tracers), it swaps the max buffer
2471 * with the saved snap shot. We succeeded to
2472 * update the size of the main buffer, but failed to
2473 * update the size of the max buffer. But when we tried
2474 * to reset the main buffer to the original size, we
2475 * failed there too. This is very unlikely to
2476 * happen, but if it does, warn and kill all
2477 * tracing.
2478 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002479 WARN_ON(1);
2480 tracing_disabled = 1;
2481 }
2482 return ret;
2483 }
2484
2485 global_trace.entries = size;
2486
2487 return ret;
2488}
2489
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002490/**
2491 * tracing_update_buffers - used by tracing facility to expand ring buffers
2492 *
2493 * To save on memory when the tracing is never used on a system with it
2494 * configured in. The ring buffers are set to a minimum size. But once
2495 * a user starts to use the tracing facility, then they need to grow
2496 * to their default size.
2497 *
2498 * This function is to be called when a tracer is about to be used.
2499 */
2500int tracing_update_buffers(void)
2501{
2502 int ret = 0;
2503
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002504 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002505 if (!ring_buffer_expanded)
2506 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002507 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002508
2509 return ret;
2510}
2511
Steven Rostedt577b7852009-02-26 23:43:05 -05002512struct trace_option_dentry;
2513
2514static struct trace_option_dentry *
2515create_trace_option_files(struct tracer *tracer);
2516
2517static void
2518destroy_trace_option_files(struct trace_option_dentry *topts);
2519
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002520static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002521{
Steven Rostedt577b7852009-02-26 23:43:05 -05002522 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002523 struct trace_array *tr = &global_trace;
2524 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002525 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002526
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002527 mutex_lock(&trace_types_lock);
2528
Steven Rostedt73c51622009-03-11 13:42:01 -04002529 if (!ring_buffer_expanded) {
2530 ret = tracing_resize_ring_buffer(trace_buf_size);
2531 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01002532 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04002533 ret = 0;
2534 }
2535
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002536 for (t = trace_types; t; t = t->next) {
2537 if (strcmp(t->name, buf) == 0)
2538 break;
2539 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002540 if (!t) {
2541 ret = -EINVAL;
2542 goto out;
2543 }
2544 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002545 goto out;
2546
Steven Rostedt9f029e82008-11-12 15:24:24 -05002547 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002548 if (current_trace && current_trace->reset)
2549 current_trace->reset(tr);
2550
Steven Rostedt577b7852009-02-26 23:43:05 -05002551 destroy_trace_option_files(topts);
2552
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002553 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002554
2555 topts = create_trace_option_files(current_trace);
2556
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002557 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002558 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002559 if (ret)
2560 goto out;
2561 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002562
Steven Rostedt9f029e82008-11-12 15:24:24 -05002563 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002564 out:
2565 mutex_unlock(&trace_types_lock);
2566
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002567 return ret;
2568}
2569
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002570static ssize_t
2571tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2572 size_t cnt, loff_t *ppos)
2573{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002574 char buf[max_tracer_type_len+1];
2575 int i;
2576 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002577 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002578
Steven Rostedt60063a62008-10-28 10:44:24 -04002579 ret = cnt;
2580
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002581 if (cnt > max_tracer_type_len)
2582 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002583
2584 if (copy_from_user(&buf, ubuf, cnt))
2585 return -EFAULT;
2586
2587 buf[cnt] = 0;
2588
2589 /* strip ending whitespace. */
2590 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2591 buf[i] = 0;
2592
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002593 err = tracing_set_tracer(buf);
2594 if (err)
2595 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002596
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002597 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002598
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002599 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002600}
2601
2602static ssize_t
2603tracing_max_lat_read(struct file *filp, char __user *ubuf,
2604 size_t cnt, loff_t *ppos)
2605{
2606 unsigned long *ptr = filp->private_data;
2607 char buf[64];
2608 int r;
2609
Steven Rostedtcffae432008-05-12 21:21:00 +02002610 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002611 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002612 if (r > sizeof(buf))
2613 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002614 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002615}
2616
2617static ssize_t
2618tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2619 size_t cnt, loff_t *ppos)
2620{
Hannes Eder5e398412009-02-10 19:44:34 +01002621 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002622 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002623 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002624 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002625
Steven Rostedtcffae432008-05-12 21:21:00 +02002626 if (cnt >= sizeof(buf))
2627 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002628
2629 if (copy_from_user(&buf, ubuf, cnt))
2630 return -EFAULT;
2631
2632 buf[cnt] = 0;
2633
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002634 ret = strict_strtoul(buf, 10, &val);
2635 if (ret < 0)
2636 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002637
2638 *ptr = val * 1000;
2639
2640 return cnt;
2641}
2642
Steven Rostedtb3806b42008-05-12 21:20:46 +02002643static int tracing_open_pipe(struct inode *inode, struct file *filp)
2644{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002645 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002646 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002647 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002648
2649 if (tracing_disabled)
2650 return -ENODEV;
2651
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002652 mutex_lock(&trace_types_lock);
2653
2654 /* We only allow one reader per cpu */
2655 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2656 if (!cpumask_empty(tracing_reader_cpumask)) {
2657 ret = -EBUSY;
2658 goto out;
2659 }
2660 cpumask_setall(tracing_reader_cpumask);
2661 } else {
2662 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2663 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2664 else {
2665 ret = -EBUSY;
2666 goto out;
2667 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002668 }
2669
2670 /* create a buffer to store the information to pass to userspace */
2671 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002672 if (!iter) {
2673 ret = -ENOMEM;
2674 goto out;
2675 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002676
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002677 /*
2678 * We make a copy of the current tracer to avoid concurrent
2679 * changes on it while we are reading.
2680 */
2681 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2682 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002683 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002684 goto fail;
2685 }
2686 if (current_trace)
2687 *iter->trace = *current_trace;
2688
2689 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2690 ret = -ENOMEM;
2691 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302692 }
2693
Steven Rostedta3097202008-11-07 22:36:02 -05002694 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302695 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002696
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002697 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002698 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002699 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002700 filp->private_data = iter;
2701
Steven Rostedt107bad82008-05-12 21:21:01 +02002702 if (iter->trace->pipe_open)
2703 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002704
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002705out:
2706 mutex_unlock(&trace_types_lock);
2707 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002708
2709fail:
2710 kfree(iter->trace);
2711 kfree(iter);
2712 mutex_unlock(&trace_types_lock);
2713 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002714}
2715
2716static int tracing_release_pipe(struct inode *inode, struct file *file)
2717{
2718 struct trace_iterator *iter = file->private_data;
2719
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002720 mutex_lock(&trace_types_lock);
2721
2722 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2723 cpumask_clear(tracing_reader_cpumask);
2724 else
2725 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2726
2727 mutex_unlock(&trace_types_lock);
2728
Rusty Russell44623442009-01-01 10:12:23 +10302729 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002730 mutex_destroy(&iter->mutex);
2731 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002732 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002733
2734 return 0;
2735}
2736
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002737static unsigned int
2738tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2739{
2740 struct trace_iterator *iter = filp->private_data;
2741
2742 if (trace_flags & TRACE_ITER_BLOCK) {
2743 /*
2744 * Always select as readable when in blocking mode
2745 */
2746 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002747 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002748 if (!trace_empty(iter))
2749 return POLLIN | POLLRDNORM;
2750 poll_wait(filp, &trace_wait, poll_table);
2751 if (!trace_empty(iter))
2752 return POLLIN | POLLRDNORM;
2753
2754 return 0;
2755 }
2756}
2757
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002758
2759void default_wait_pipe(struct trace_iterator *iter)
2760{
2761 DEFINE_WAIT(wait);
2762
2763 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2764
2765 if (trace_empty(iter))
2766 schedule();
2767
2768 finish_wait(&trace_wait, &wait);
2769}
2770
2771/*
2772 * This is a make-shift waitqueue.
2773 * A tracer might use this callback on some rare cases:
2774 *
2775 * 1) the current tracer might hold the runqueue lock when it wakes up
2776 * a reader, hence a deadlock (sched, function, and function graph tracers)
2777 * 2) the function tracers, trace all functions, we don't want
2778 * the overhead of calling wake_up and friends
2779 * (and tracing them too)
2780 *
2781 * Anyway, this is really very primitive wakeup.
2782 */
2783void poll_wait_pipe(struct trace_iterator *iter)
2784{
2785 set_current_state(TASK_INTERRUPTIBLE);
2786 /* sleep for 100 msecs, and try again. */
2787 schedule_timeout(HZ / 10);
2788}
2789
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002790/* Must be called with trace_types_lock mutex held. */
2791static int tracing_wait_pipe(struct file *filp)
2792{
2793 struct trace_iterator *iter = filp->private_data;
2794
2795 while (trace_empty(iter)) {
2796
2797 if ((filp->f_flags & O_NONBLOCK)) {
2798 return -EAGAIN;
2799 }
2800
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002801 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002802
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002803 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002804
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002805 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002806
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002807 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002808 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002809
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002810 /*
2811 * We block until we read something and tracing is disabled.
2812 * We still block if tracing is disabled, but we have never
2813 * read anything. This allows a user to cat this file, and
2814 * then enable tracing. But after we have read something,
2815 * we give an EOF when tracing is again disabled.
2816 *
2817 * iter->pos will be 0 if we haven't read anything.
2818 */
2819 if (!tracer_enabled && iter->pos)
2820 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002821 }
2822
2823 return 1;
2824}
2825
Steven Rostedtb3806b42008-05-12 21:20:46 +02002826/*
2827 * Consumer reader.
2828 */
2829static ssize_t
2830tracing_read_pipe(struct file *filp, char __user *ubuf,
2831 size_t cnt, loff_t *ppos)
2832{
2833 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002834 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002835 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002836
2837 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002838 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2839 if (sret != -EBUSY)
2840 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002841
Steven Rostedtf9520752009-03-02 14:04:40 -05002842 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002843
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002844 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002845 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002846 if (unlikely(old_tracer != current_trace && current_trace)) {
2847 old_tracer = current_trace;
2848 *iter->trace = *current_trace;
2849 }
2850 mutex_unlock(&trace_types_lock);
2851
2852 /*
2853 * Avoid more than one consumer on a single file descriptor
2854 * This is just a matter of traces coherency, the ring buffer itself
2855 * is protected.
2856 */
2857 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002858 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002859 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2860 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002861 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002862 }
2863
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002864waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002865 sret = tracing_wait_pipe(filp);
2866 if (sret <= 0)
2867 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002868
2869 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002870 if (trace_empty(iter)) {
2871 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002872 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002873 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002874
2875 if (cnt >= PAGE_SIZE)
2876 cnt = PAGE_SIZE - 1;
2877
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002878 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002879 memset(&iter->seq, 0,
2880 sizeof(struct trace_iterator) -
2881 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002882 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002883
Steven Rostedt088b1e422008-05-12 21:20:48 +02002884 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002885 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002886 int len = iter->seq.len;
2887
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002888 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002889 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002890 /* don't print partial lines */
2891 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002892 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002893 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002894 if (ret != TRACE_TYPE_NO_CONSUME)
2895 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002896
2897 if (iter->seq.len >= cnt)
2898 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002899 }
2900
Steven Rostedtb3806b42008-05-12 21:20:46 +02002901 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002902 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2903 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002904 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002905
2906 /*
2907 * If there was nothing to send to user, inspite of consuming trace
2908 * entries, go back to wait for more entries.
2909 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002910 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002911 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002912
Steven Rostedt107bad82008-05-12 21:21:01 +02002913out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002914 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002915
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002916 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002917}
2918
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002919static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2920 struct pipe_buffer *buf)
2921{
2922 __free_page(buf->page);
2923}
2924
2925static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2926 unsigned int idx)
2927{
2928 __free_page(spd->pages[idx]);
2929}
2930
2931static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002932 .can_merge = 0,
2933 .map = generic_pipe_buf_map,
2934 .unmap = generic_pipe_buf_unmap,
2935 .confirm = generic_pipe_buf_confirm,
2936 .release = tracing_pipe_buf_release,
2937 .steal = generic_pipe_buf_steal,
2938 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002939};
2940
Steven Rostedt34cd4992009-02-09 12:06:29 -05002941static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002942tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002943{
2944 size_t count;
2945 int ret;
2946
2947 /* Seq buffer is page-sized, exactly what we need. */
2948 for (;;) {
2949 count = iter->seq.len;
2950 ret = print_trace_line(iter);
2951 count = iter->seq.len - count;
2952 if (rem < count) {
2953 rem = 0;
2954 iter->seq.len -= count;
2955 break;
2956 }
2957 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2958 iter->seq.len -= count;
2959 break;
2960 }
2961
2962 trace_consume(iter);
2963 rem -= count;
2964 if (!find_next_entry_inc(iter)) {
2965 rem = 0;
2966 iter->ent = NULL;
2967 break;
2968 }
2969 }
2970
2971 return rem;
2972}
2973
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002974static ssize_t tracing_splice_read_pipe(struct file *filp,
2975 loff_t *ppos,
2976 struct pipe_inode_info *pipe,
2977 size_t len,
2978 unsigned int flags)
2979{
2980 struct page *pages[PIPE_BUFFERS];
2981 struct partial_page partial[PIPE_BUFFERS];
2982 struct trace_iterator *iter = filp->private_data;
2983 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002984 .pages = pages,
2985 .partial = partial,
2986 .nr_pages = 0, /* This gets updated below. */
2987 .flags = flags,
2988 .ops = &tracing_pipe_buf_ops,
2989 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002990 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002991 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002992 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002993 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002994 unsigned int i;
2995
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002996 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002997 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002998 if (unlikely(old_tracer != current_trace && current_trace)) {
2999 old_tracer = current_trace;
3000 *iter->trace = *current_trace;
3001 }
3002 mutex_unlock(&trace_types_lock);
3003
3004 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003005
3006 if (iter->trace->splice_read) {
3007 ret = iter->trace->splice_read(iter, filp,
3008 ppos, pipe, len, flags);
3009 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003010 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003011 }
3012
3013 ret = tracing_wait_pipe(filp);
3014 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003015 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003016
3017 if (!iter->ent && !find_next_entry_inc(iter)) {
3018 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003019 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003020 }
3021
3022 /* Fill as many pages as possible. */
3023 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3024 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003025 if (!pages[i])
3026 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003027
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003028 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003029
3030 /* Copy the data into the page, so we can start over. */
3031 ret = trace_seq_to_buffer(&iter->seq,
3032 page_address(pages[i]),
3033 iter->seq.len);
3034 if (ret < 0) {
3035 __free_page(pages[i]);
3036 break;
3037 }
3038 partial[i].offset = 0;
3039 partial[i].len = iter->seq.len;
3040
Steven Rostedtf9520752009-03-02 14:04:40 -05003041 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003042 }
3043
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003044 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003045
3046 spd.nr_pages = i;
3047
3048 return splice_to_pipe(pipe, &spd);
3049
Steven Rostedt34cd4992009-02-09 12:06:29 -05003050out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003051 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003052
3053 return ret;
3054}
3055
Steven Rostedta98a3c32008-05-12 21:20:59 +02003056static ssize_t
3057tracing_entries_read(struct file *filp, char __user *ubuf,
3058 size_t cnt, loff_t *ppos)
3059{
3060 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003061 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003062 int r;
3063
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003064 mutex_lock(&trace_types_lock);
3065 if (!ring_buffer_expanded)
3066 r = sprintf(buf, "%lu (expanded: %lu)\n",
3067 tr->entries >> 10,
3068 trace_buf_size >> 10);
3069 else
3070 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3071 mutex_unlock(&trace_types_lock);
3072
Steven Rostedta98a3c32008-05-12 21:20:59 +02003073 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3074}
3075
3076static ssize_t
3077tracing_entries_write(struct file *filp, const char __user *ubuf,
3078 size_t cnt, loff_t *ppos)
3079{
3080 unsigned long val;
3081 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003082 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003083
Steven Rostedtcffae432008-05-12 21:21:00 +02003084 if (cnt >= sizeof(buf))
3085 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003086
3087 if (copy_from_user(&buf, ubuf, cnt))
3088 return -EFAULT;
3089
3090 buf[cnt] = 0;
3091
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003092 ret = strict_strtoul(buf, 10, &val);
3093 if (ret < 0)
3094 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003095
3096 /* must have at least 1 entry */
3097 if (!val)
3098 return -EINVAL;
3099
3100 mutex_lock(&trace_types_lock);
3101
Steven Rostedtc76f0692008-11-07 22:36:02 -05003102 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003103
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003104 /* disable all cpu buffers */
3105 for_each_tracing_cpu(cpu) {
3106 if (global_trace.data[cpu])
3107 atomic_inc(&global_trace.data[cpu]->disabled);
3108 if (max_tr.data[cpu])
3109 atomic_inc(&max_tr.data[cpu]->disabled);
3110 }
3111
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003112 /* value is in KB */
3113 val <<= 10;
3114
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003115 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003116 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003117 if (ret < 0) {
3118 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003119 goto out;
3120 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003121 }
3122
3123 filp->f_pos += cnt;
3124
Steven Rostedt19384c02008-05-22 00:22:16 -04003125 /* If check pages failed, return ENOMEM */
3126 if (tracing_disabled)
3127 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003128 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003129 for_each_tracing_cpu(cpu) {
3130 if (global_trace.data[cpu])
3131 atomic_dec(&global_trace.data[cpu]->disabled);
3132 if (max_tr.data[cpu])
3133 atomic_dec(&max_tr.data[cpu]->disabled);
3134 }
3135
Steven Rostedtc76f0692008-11-07 22:36:02 -05003136 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003137 max_tr.entries = global_trace.entries;
3138 mutex_unlock(&trace_types_lock);
3139
3140 return cnt;
3141}
3142
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003143static int mark_printk(const char *fmt, ...)
3144{
3145 int ret;
3146 va_list args;
3147 va_start(args, fmt);
Steven Rostedt40ce74f2009-03-19 14:03:53 -04003148 ret = trace_vprintk(0, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003149 va_end(args);
3150 return ret;
3151}
3152
3153static ssize_t
3154tracing_mark_write(struct file *filp, const char __user *ubuf,
3155 size_t cnt, loff_t *fpos)
3156{
3157 char *buf;
3158 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003159
Steven Rostedtc76f0692008-11-07 22:36:02 -05003160 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003161 return -EINVAL;
3162
3163 if (cnt > TRACE_BUF_SIZE)
3164 cnt = TRACE_BUF_SIZE;
3165
3166 buf = kmalloc(cnt + 1, GFP_KERNEL);
3167 if (buf == NULL)
3168 return -ENOMEM;
3169
3170 if (copy_from_user(buf, ubuf, cnt)) {
3171 kfree(buf);
3172 return -EFAULT;
3173 }
3174
3175 /* Cut from the first nil or newline. */
3176 buf[cnt] = '\0';
3177 end = strchr(buf, '\n');
3178 if (end)
3179 *end = '\0';
3180
3181 cnt = mark_printk("%s\n", buf);
3182 kfree(buf);
3183 *fpos += cnt;
3184
3185 return cnt;
3186}
3187
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003188static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003189 .open = tracing_open_generic,
3190 .read = tracing_max_lat_read,
3191 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003192};
3193
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003194static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003195 .open = tracing_open_generic,
3196 .read = tracing_ctrl_read,
3197 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003198};
3199
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003200static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003201 .open = tracing_open_generic,
3202 .read = tracing_set_trace_read,
3203 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003204};
3205
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003206static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003207 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003208 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003209 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003210 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003211 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003212};
3213
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003214static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003215 .open = tracing_open_generic,
3216 .read = tracing_entries_read,
3217 .write = tracing_entries_write,
3218};
3219
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003220static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003221 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003222 .write = tracing_mark_write,
3223};
3224
Steven Rostedt2cadf912008-12-01 22:20:19 -05003225struct ftrace_buffer_info {
3226 struct trace_array *tr;
3227 void *spare;
3228 int cpu;
3229 unsigned int read;
3230};
3231
3232static int tracing_buffers_open(struct inode *inode, struct file *filp)
3233{
3234 int cpu = (int)(long)inode->i_private;
3235 struct ftrace_buffer_info *info;
3236
3237 if (tracing_disabled)
3238 return -ENODEV;
3239
3240 info = kzalloc(sizeof(*info), GFP_KERNEL);
3241 if (!info)
3242 return -ENOMEM;
3243
3244 info->tr = &global_trace;
3245 info->cpu = cpu;
3246 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3247 /* Force reading ring buffer for first read */
3248 info->read = (unsigned int)-1;
3249 if (!info->spare)
3250 goto out;
3251
3252 filp->private_data = info;
3253
3254 return 0;
3255
3256 out:
3257 kfree(info);
3258 return -ENOMEM;
3259}
3260
3261static ssize_t
3262tracing_buffers_read(struct file *filp, char __user *ubuf,
3263 size_t count, loff_t *ppos)
3264{
3265 struct ftrace_buffer_info *info = filp->private_data;
3266 unsigned int pos;
3267 ssize_t ret;
3268 size_t size;
3269
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003270 if (!count)
3271 return 0;
3272
Steven Rostedt2cadf912008-12-01 22:20:19 -05003273 /* Do we have previous read data to read? */
3274 if (info->read < PAGE_SIZE)
3275 goto read;
3276
3277 info->read = 0;
3278
3279 ret = ring_buffer_read_page(info->tr->buffer,
3280 &info->spare,
3281 count,
3282 info->cpu, 0);
3283 if (ret < 0)
3284 return 0;
3285
3286 pos = ring_buffer_page_len(info->spare);
3287
3288 if (pos < PAGE_SIZE)
3289 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3290
3291read:
3292 size = PAGE_SIZE - info->read;
3293 if (size > count)
3294 size = count;
3295
3296 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003297 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003298 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003299 size -= ret;
3300
Steven Rostedt2cadf912008-12-01 22:20:19 -05003301 *ppos += size;
3302 info->read += size;
3303
3304 return size;
3305}
3306
3307static int tracing_buffers_release(struct inode *inode, struct file *file)
3308{
3309 struct ftrace_buffer_info *info = file->private_data;
3310
3311 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3312 kfree(info);
3313
3314 return 0;
3315}
3316
3317struct buffer_ref {
3318 struct ring_buffer *buffer;
3319 void *page;
3320 int ref;
3321};
3322
3323static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3324 struct pipe_buffer *buf)
3325{
3326 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3327
3328 if (--ref->ref)
3329 return;
3330
3331 ring_buffer_free_read_page(ref->buffer, ref->page);
3332 kfree(ref);
3333 buf->private = 0;
3334}
3335
3336static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3337 struct pipe_buffer *buf)
3338{
3339 return 1;
3340}
3341
3342static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3343 struct pipe_buffer *buf)
3344{
3345 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3346
3347 ref->ref++;
3348}
3349
3350/* Pipe buffer operations for a buffer. */
3351static struct pipe_buf_operations buffer_pipe_buf_ops = {
3352 .can_merge = 0,
3353 .map = generic_pipe_buf_map,
3354 .unmap = generic_pipe_buf_unmap,
3355 .confirm = generic_pipe_buf_confirm,
3356 .release = buffer_pipe_buf_release,
3357 .steal = buffer_pipe_buf_steal,
3358 .get = buffer_pipe_buf_get,
3359};
3360
3361/*
3362 * Callback from splice_to_pipe(), if we need to release some pages
3363 * at the end of the spd in case we error'ed out in filling the pipe.
3364 */
3365static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3366{
3367 struct buffer_ref *ref =
3368 (struct buffer_ref *)spd->partial[i].private;
3369
3370 if (--ref->ref)
3371 return;
3372
3373 ring_buffer_free_read_page(ref->buffer, ref->page);
3374 kfree(ref);
3375 spd->partial[i].private = 0;
3376}
3377
3378static ssize_t
3379tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3380 struct pipe_inode_info *pipe, size_t len,
3381 unsigned int flags)
3382{
3383 struct ftrace_buffer_info *info = file->private_data;
3384 struct partial_page partial[PIPE_BUFFERS];
3385 struct page *pages[PIPE_BUFFERS];
3386 struct splice_pipe_desc spd = {
3387 .pages = pages,
3388 .partial = partial,
3389 .flags = flags,
3390 .ops = &buffer_pipe_buf_ops,
3391 .spd_release = buffer_spd_release,
3392 };
3393 struct buffer_ref *ref;
3394 int size, i;
3395 size_t ret;
3396
3397 /*
3398 * We can't seek on a buffer input
3399 */
3400 if (unlikely(*ppos))
3401 return -ESPIPE;
3402
3403
3404 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3405 struct page *page;
3406 int r;
3407
3408 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3409 if (!ref)
3410 break;
3411
3412 ref->buffer = info->tr->buffer;
3413 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3414 if (!ref->page) {
3415 kfree(ref);
3416 break;
3417 }
3418
3419 r = ring_buffer_read_page(ref->buffer, &ref->page,
3420 len, info->cpu, 0);
3421 if (r < 0) {
3422 ring_buffer_free_read_page(ref->buffer,
3423 ref->page);
3424 kfree(ref);
3425 break;
3426 }
3427
3428 /*
3429 * zero out any left over data, this is going to
3430 * user land.
3431 */
3432 size = ring_buffer_page_len(ref->page);
3433 if (size < PAGE_SIZE)
3434 memset(ref->page + size, 0, PAGE_SIZE - size);
3435
3436 page = virt_to_page(ref->page);
3437
3438 spd.pages[i] = page;
3439 spd.partial[i].len = PAGE_SIZE;
3440 spd.partial[i].offset = 0;
3441 spd.partial[i].private = (unsigned long)ref;
3442 spd.nr_pages++;
3443 }
3444
3445 spd.nr_pages = i;
3446
3447 /* did we read anything? */
3448 if (!spd.nr_pages) {
3449 if (flags & SPLICE_F_NONBLOCK)
3450 ret = -EAGAIN;
3451 else
3452 ret = 0;
3453 /* TODO: block */
3454 return ret;
3455 }
3456
3457 ret = splice_to_pipe(pipe, &spd);
3458
3459 return ret;
3460}
3461
3462static const struct file_operations tracing_buffers_fops = {
3463 .open = tracing_buffers_open,
3464 .read = tracing_buffers_read,
3465 .release = tracing_buffers_release,
3466 .splice_read = tracing_buffers_splice_read,
3467 .llseek = no_llseek,
3468};
3469
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003470#ifdef CONFIG_DYNAMIC_FTRACE
3471
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003472int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003473{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003474 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003475}
3476
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003477static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003478tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003479 size_t cnt, loff_t *ppos)
3480{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003481 static char ftrace_dyn_info_buffer[1024];
3482 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003483 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003484 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003485 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003486 int r;
3487
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003488 mutex_lock(&dyn_info_mutex);
3489 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003490
Steven Rostedta26a2a22008-10-31 00:03:22 -04003491 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003492 buf[r++] = '\n';
3493
3494 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3495
3496 mutex_unlock(&dyn_info_mutex);
3497
3498 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003499}
3500
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003501static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003502 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003503 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003504};
3505#endif
3506
3507static struct dentry *d_tracer;
3508
3509struct dentry *tracing_init_dentry(void)
3510{
3511 static int once;
3512
3513 if (d_tracer)
3514 return d_tracer;
3515
3516 d_tracer = debugfs_create_dir("tracing", NULL);
3517
3518 if (!d_tracer && !once) {
3519 once = 1;
3520 pr_warning("Could not create debugfs directory 'tracing'\n");
3521 return NULL;
3522 }
3523
3524 return d_tracer;
3525}
3526
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003527static struct dentry *d_percpu;
3528
3529struct dentry *tracing_dentry_percpu(void)
3530{
3531 static int once;
3532 struct dentry *d_tracer;
3533
3534 if (d_percpu)
3535 return d_percpu;
3536
3537 d_tracer = tracing_init_dentry();
3538
3539 if (!d_tracer)
3540 return NULL;
3541
3542 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3543
3544 if (!d_percpu && !once) {
3545 once = 1;
3546 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3547 return NULL;
3548 }
3549
3550 return d_percpu;
3551}
3552
3553static void tracing_init_debugfs_percpu(long cpu)
3554{
3555 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003556 struct dentry *entry, *d_cpu;
3557 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3558 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003559
3560 if (cpu > 999 || cpu < 0)
3561 return;
3562
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003563 sprintf(cpu_dir, "cpu%ld", cpu);
3564 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3565 if (!d_cpu) {
3566 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3567 return;
3568 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003569
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003570 /* per cpu trace_pipe */
3571 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003572 (void *) cpu, &tracing_pipe_fops);
3573 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003574 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003575
3576 /* per cpu trace */
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003577 entry = debugfs_create_file("trace", 0644, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003578 (void *) cpu, &tracing_fops);
3579 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003580 pr_warning("Could not create debugfs 'trace' entry\n");
Steven Rostedt7f96f932009-03-13 00:37:42 -04003581
3582 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3583 (void *) cpu, &tracing_buffers_fops);
3584 if (!entry)
3585 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003586}
3587
Steven Rostedt60a11772008-05-12 21:20:44 +02003588#ifdef CONFIG_FTRACE_SELFTEST
3589/* Let selftest have access to static functions in this file */
3590#include "trace_selftest.c"
3591#endif
3592
Steven Rostedt577b7852009-02-26 23:43:05 -05003593struct trace_option_dentry {
3594 struct tracer_opt *opt;
3595 struct tracer_flags *flags;
3596 struct dentry *entry;
3597};
3598
3599static ssize_t
3600trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3601 loff_t *ppos)
3602{
3603 struct trace_option_dentry *topt = filp->private_data;
3604 char *buf;
3605
3606 if (topt->flags->val & topt->opt->bit)
3607 buf = "1\n";
3608 else
3609 buf = "0\n";
3610
3611 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3612}
3613
3614static ssize_t
3615trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3616 loff_t *ppos)
3617{
3618 struct trace_option_dentry *topt = filp->private_data;
3619 unsigned long val;
3620 char buf[64];
3621 int ret;
3622
3623 if (cnt >= sizeof(buf))
3624 return -EINVAL;
3625
3626 if (copy_from_user(&buf, ubuf, cnt))
3627 return -EFAULT;
3628
3629 buf[cnt] = 0;
3630
3631 ret = strict_strtoul(buf, 10, &val);
3632 if (ret < 0)
3633 return ret;
3634
3635 ret = 0;
3636 switch (val) {
3637 case 0:
3638 /* do nothing if already cleared */
3639 if (!(topt->flags->val & topt->opt->bit))
3640 break;
3641
3642 mutex_lock(&trace_types_lock);
3643 if (current_trace->set_flag)
3644 ret = current_trace->set_flag(topt->flags->val,
3645 topt->opt->bit, 0);
3646 mutex_unlock(&trace_types_lock);
3647 if (ret)
3648 return ret;
3649 topt->flags->val &= ~topt->opt->bit;
3650 break;
3651 case 1:
3652 /* do nothing if already set */
3653 if (topt->flags->val & topt->opt->bit)
3654 break;
3655
3656 mutex_lock(&trace_types_lock);
3657 if (current_trace->set_flag)
3658 ret = current_trace->set_flag(topt->flags->val,
3659 topt->opt->bit, 1);
3660 mutex_unlock(&trace_types_lock);
3661 if (ret)
3662 return ret;
3663 topt->flags->val |= topt->opt->bit;
3664 break;
3665
3666 default:
3667 return -EINVAL;
3668 }
3669
3670 *ppos += cnt;
3671
3672 return cnt;
3673}
3674
3675
3676static const struct file_operations trace_options_fops = {
3677 .open = tracing_open_generic,
3678 .read = trace_options_read,
3679 .write = trace_options_write,
3680};
3681
Steven Rostedta8259072009-02-26 22:19:12 -05003682static ssize_t
3683trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3684 loff_t *ppos)
3685{
3686 long index = (long)filp->private_data;
3687 char *buf;
3688
3689 if (trace_flags & (1 << index))
3690 buf = "1\n";
3691 else
3692 buf = "0\n";
3693
3694 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3695}
3696
3697static ssize_t
3698trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3699 loff_t *ppos)
3700{
3701 long index = (long)filp->private_data;
3702 char buf[64];
3703 unsigned long val;
3704 int ret;
3705
3706 if (cnt >= sizeof(buf))
3707 return -EINVAL;
3708
3709 if (copy_from_user(&buf, ubuf, cnt))
3710 return -EFAULT;
3711
3712 buf[cnt] = 0;
3713
3714 ret = strict_strtoul(buf, 10, &val);
3715 if (ret < 0)
3716 return ret;
3717
3718 switch (val) {
3719 case 0:
3720 trace_flags &= ~(1 << index);
3721 break;
3722 case 1:
3723 trace_flags |= 1 << index;
3724 break;
3725
3726 default:
3727 return -EINVAL;
3728 }
3729
3730 *ppos += cnt;
3731
3732 return cnt;
3733}
3734
Steven Rostedta8259072009-02-26 22:19:12 -05003735static const struct file_operations trace_options_core_fops = {
3736 .open = tracing_open_generic,
3737 .read = trace_options_core_read,
3738 .write = trace_options_core_write,
3739};
3740
3741static struct dentry *trace_options_init_dentry(void)
3742{
3743 struct dentry *d_tracer;
3744 static struct dentry *t_options;
3745
3746 if (t_options)
3747 return t_options;
3748
3749 d_tracer = tracing_init_dentry();
3750 if (!d_tracer)
3751 return NULL;
3752
3753 t_options = debugfs_create_dir("options", d_tracer);
3754 if (!t_options) {
3755 pr_warning("Could not create debugfs directory 'options'\n");
3756 return NULL;
3757 }
3758
3759 return t_options;
3760}
3761
Steven Rostedt577b7852009-02-26 23:43:05 -05003762static void
3763create_trace_option_file(struct trace_option_dentry *topt,
3764 struct tracer_flags *flags,
3765 struct tracer_opt *opt)
3766{
3767 struct dentry *t_options;
3768 struct dentry *entry;
3769
3770 t_options = trace_options_init_dentry();
3771 if (!t_options)
3772 return;
3773
3774 topt->flags = flags;
3775 topt->opt = opt;
3776
3777 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3778 &trace_options_fops);
3779
3780 topt->entry = entry;
3781
3782}
3783
3784static struct trace_option_dentry *
3785create_trace_option_files(struct tracer *tracer)
3786{
3787 struct trace_option_dentry *topts;
3788 struct tracer_flags *flags;
3789 struct tracer_opt *opts;
3790 int cnt;
3791
3792 if (!tracer)
3793 return NULL;
3794
3795 flags = tracer->flags;
3796
3797 if (!flags || !flags->opts)
3798 return NULL;
3799
3800 opts = flags->opts;
3801
3802 for (cnt = 0; opts[cnt].name; cnt++)
3803 ;
3804
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003805 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003806 if (!topts)
3807 return NULL;
3808
3809 for (cnt = 0; opts[cnt].name; cnt++)
3810 create_trace_option_file(&topts[cnt], flags,
3811 &opts[cnt]);
3812
3813 return topts;
3814}
3815
3816static void
3817destroy_trace_option_files(struct trace_option_dentry *topts)
3818{
3819 int cnt;
3820
3821 if (!topts)
3822 return;
3823
3824 for (cnt = 0; topts[cnt].opt; cnt++) {
3825 if (topts[cnt].entry)
3826 debugfs_remove(topts[cnt].entry);
3827 }
3828
3829 kfree(topts);
3830}
3831
Steven Rostedta8259072009-02-26 22:19:12 -05003832static struct dentry *
3833create_trace_option_core_file(const char *option, long index)
3834{
3835 struct dentry *t_options;
3836 struct dentry *entry;
3837
3838 t_options = trace_options_init_dentry();
3839 if (!t_options)
3840 return NULL;
3841
3842 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3843 &trace_options_core_fops);
3844
3845 return entry;
3846}
3847
3848static __init void create_trace_options_dir(void)
3849{
3850 struct dentry *t_options;
3851 struct dentry *entry;
3852 int i;
3853
3854 t_options = trace_options_init_dentry();
3855 if (!t_options)
3856 return;
3857
3858 for (i = 0; trace_options[i]; i++) {
3859 entry = create_trace_option_core_file(trace_options[i], i);
3860 if (!entry)
3861 pr_warning("Could not create debugfs %s entry\n",
3862 trace_options[i]);
3863 }
3864}
3865
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003866static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003867{
3868 struct dentry *d_tracer;
3869 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003870 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003871
3872 d_tracer = tracing_init_dentry();
3873
3874 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3875 &global_trace, &tracing_ctrl_fops);
3876 if (!entry)
3877 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3878
Steven Rostedtee6bce52008-11-12 17:52:37 -05003879 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003880 NULL, &tracing_iter_fops);
3881 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003882 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003883
Steven Rostedta8259072009-02-26 22:19:12 -05003884 create_trace_options_dir();
3885
Ingo Molnarc7078de2008-05-12 21:20:52 +02003886 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3887 NULL, &tracing_cpumask_fops);
3888 if (!entry)
3889 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3890
Steven Rostedt4acd4d02009-03-18 10:40:24 -04003891 entry = debugfs_create_file("trace", 0644, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003892 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003893 if (!entry)
3894 pr_warning("Could not create debugfs 'trace' entry\n");
3895
3896 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3897 &global_trace, &show_traces_fops);
3898 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003899 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003900
3901 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3902 &global_trace, &set_tracer_fops);
3903 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003904 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003905
3906 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3907 &tracing_max_latency,
3908 &tracing_max_lat_fops);
3909 if (!entry)
3910 pr_warning("Could not create debugfs "
3911 "'tracing_max_latency' entry\n");
3912
3913 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3914 &tracing_thresh, &tracing_max_lat_fops);
3915 if (!entry)
3916 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003917 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003918 entry = debugfs_create_file("README", 0644, d_tracer,
3919 NULL, &tracing_readme_fops);
3920 if (!entry)
3921 pr_warning("Could not create debugfs 'README' entry\n");
3922
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003923 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3924 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003925 if (!entry)
3926 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003927 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003928
Steven Rostedta94c80e2008-11-12 17:52:36 -05003929 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003930 &global_trace, &tracing_entries_fops);
3931 if (!entry)
3932 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003933 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003934
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003935 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3936 NULL, &tracing_mark_fops);
3937 if (!entry)
3938 pr_warning("Could not create debugfs "
3939 "'trace_marker' entry\n");
3940
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003941#ifdef CONFIG_DYNAMIC_FTRACE
3942 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3943 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003944 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003945 if (!entry)
3946 pr_warning("Could not create debugfs "
3947 "'dyn_ftrace_total_info' entry\n");
3948#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003949#ifdef CONFIG_SYSPROF_TRACER
3950 init_tracer_sysprof_debugfs(d_tracer);
3951#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003952
3953 for_each_tracing_cpu(cpu)
3954 tracing_init_debugfs_percpu(cpu);
3955
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003956 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003957}
3958
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003959static int trace_panic_handler(struct notifier_block *this,
3960 unsigned long event, void *unused)
3961{
Steven Rostedt944ac422008-10-23 19:26:08 -04003962 if (ftrace_dump_on_oops)
3963 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003964 return NOTIFY_OK;
3965}
3966
3967static struct notifier_block trace_panic_notifier = {
3968 .notifier_call = trace_panic_handler,
3969 .next = NULL,
3970 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3971};
3972
3973static int trace_die_handler(struct notifier_block *self,
3974 unsigned long val,
3975 void *data)
3976{
3977 switch (val) {
3978 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003979 if (ftrace_dump_on_oops)
3980 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003981 break;
3982 default:
3983 break;
3984 }
3985 return NOTIFY_OK;
3986}
3987
3988static struct notifier_block trace_die_notifier = {
3989 .notifier_call = trace_die_handler,
3990 .priority = 200
3991};
3992
3993/*
3994 * printk is set to max of 1024, we really don't need it that big.
3995 * Nothing should be printing 1000 characters anyway.
3996 */
3997#define TRACE_MAX_PRINT 1000
3998
3999/*
4000 * Define here KERN_TRACE so that we have one place to modify
4001 * it if we decide to change what log level the ftrace dump
4002 * should be at.
4003 */
Steven Rostedt428aee12009-01-14 12:24:42 -05004004#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004005
4006static void
4007trace_printk_seq(struct trace_seq *s)
4008{
4009 /* Probably should print a warning here. */
4010 if (s->len >= 1000)
4011 s->len = 1000;
4012
4013 /* should be zero ended, but we are paranoid. */
4014 s->buffer[s->len] = 0;
4015
4016 printk(KERN_TRACE "%s", s->buffer);
4017
Steven Rostedtf9520752009-03-02 14:04:40 -05004018 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004019}
4020
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004021static void __ftrace_dump(bool disable_tracing)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004022{
4023 static DEFINE_SPINLOCK(ftrace_dump_lock);
4024 /* use static because iter can be a bit big for the stack */
4025 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004026 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004027 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004028 unsigned long flags;
4029 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004030
4031 /* only one dump */
4032 spin_lock_irqsave(&ftrace_dump_lock, flags);
4033 if (dump_ran)
4034 goto out;
4035
4036 dump_ran = 1;
4037
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004038 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004039
4040 if (disable_tracing)
4041 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004042
Steven Rostedtd7690412008-10-01 00:29:53 -04004043 for_each_tracing_cpu(cpu) {
4044 atomic_inc(&global_trace.data[cpu]->disabled);
4045 }
4046
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004047 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4048
Török Edwinb54d3de2008-11-22 13:28:48 +02004049 /* don't look at user memory in panic mode */
4050 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4051
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004052 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4053
Steven Rostedte543ad72009-03-04 18:20:36 -05004054 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004055 iter.tr = &global_trace;
4056 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05004057 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004058
4059 /*
4060 * We need to stop all tracing on all CPUS to read the
4061 * the next buffer. This is a bit expensive, but is
4062 * not done often. We fill all what we can read,
4063 * and then release the locks again.
4064 */
4065
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004066 while (!trace_empty(&iter)) {
4067
4068 if (!cnt)
4069 printk(KERN_TRACE "---------------------------------\n");
4070
4071 cnt++;
4072
4073 /* reset all but tr, trace, and overruns */
4074 memset(&iter.seq, 0,
4075 sizeof(struct trace_iterator) -
4076 offsetof(struct trace_iterator, seq));
4077 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4078 iter.pos = -1;
4079
4080 if (find_next_entry_inc(&iter) != NULL) {
4081 print_trace_line(&iter);
4082 trace_consume(&iter);
4083 }
4084
4085 trace_printk_seq(&iter.seq);
4086 }
4087
4088 if (!cnt)
4089 printk(KERN_TRACE " (ftrace buffer empty)\n");
4090 else
4091 printk(KERN_TRACE "---------------------------------\n");
4092
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004093 /* Re-enable tracing if requested */
4094 if (!disable_tracing) {
4095 trace_flags |= old_userobj;
4096
4097 for_each_tracing_cpu(cpu) {
4098 atomic_dec(&global_trace.data[cpu]->disabled);
4099 }
4100 tracing_on();
4101 }
4102
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004103 out:
4104 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4105}
4106
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004107/* By default: disable tracing after the dump */
4108void ftrace_dump(void)
4109{
4110 __ftrace_dump(true);
4111}
4112
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004113__init static int tracer_alloc_buffers(void)
4114{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004115 struct trace_array_cpu *data;
Steven Rostedt73c51622009-03-11 13:42:01 -04004116 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004117 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304118 int ret = -ENOMEM;
4119
4120 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4121 goto out;
4122
4123 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4124 goto out_free_buffer_mask;
4125
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004126 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4127 goto out_free_tracing_cpumask;
4128
Steven Rostedt73c51622009-03-11 13:42:01 -04004129 /* To save memory, keep the ring buffer size to its minimum */
4130 if (ring_buffer_expanded)
4131 ring_buf_size = trace_buf_size;
4132 else
4133 ring_buf_size = 1;
4134
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304135 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4136 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004137 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004138
Steven Rostedtab464282008-05-12 21:21:00 +02004139 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04004140 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004141 TRACE_BUFFER_FLAGS);
4142 if (!global_trace.buffer) {
4143 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4144 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304145 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004146 }
4147 global_trace.entries = ring_buffer_size(global_trace.buffer);
4148
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304149
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004150#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt73c51622009-03-11 13:42:01 -04004151 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004152 TRACE_BUFFER_FLAGS);
4153 if (!max_tr.buffer) {
4154 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4155 WARN_ON(1);
4156 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304157 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004158 }
4159 max_tr.entries = ring_buffer_size(max_tr.buffer);
4160 WARN_ON(max_tr.entries != global_trace.entries);
4161#endif
4162
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004163 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004164 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004165 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004166 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004167 }
4168
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004169 trace_init_cmdlines();
4170
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004171 register_tracer(&nop_trace);
Steven Rostedt79fb0768f2009-02-02 21:38:33 -05004172 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004173#ifdef CONFIG_BOOT_TRACER
4174 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004175#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004176 /* All seems OK, enable tracing */
4177 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004178
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004179 atomic_notifier_chain_register(&panic_notifier_list,
4180 &trace_panic_notifier);
4181
4182 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01004183
4184 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004185
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304186out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004187 free_cpumask_var(tracing_reader_cpumask);
4188out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304189 free_cpumask_var(tracing_cpumask);
4190out_free_buffer_mask:
4191 free_cpumask_var(tracing_buffer_mask);
4192out:
4193 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004194}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004195
4196__init static int clear_boot_tracer(void)
4197{
4198 /*
4199 * The default tracer at boot buffer is an init section.
4200 * This function is called in lateinit. If we did not
4201 * find the boot tracer, then clear it out, to prevent
4202 * later registration from accessing the buffer that is
4203 * about to be freed.
4204 */
4205 if (!default_bootup_tracer)
4206 return 0;
4207
4208 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4209 default_bootup_tracer);
4210 default_bootup_tracer = NULL;
4211
4212 return 0;
4213}
4214
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004215early_initcall(tracer_alloc_buffers);
4216fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004217late_initcall(clear_boot_tracer);