blob: 3be2f788e10d9e0e03ec00e96e581d5e776a1246 [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 Rostedtbc0c38d2008-05-12 21:20:42 +0200318 NULL
319};
320
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200321/*
322 * ftrace_max_lock is used to protect the swapping of buffers
323 * when taking a max snapshot. The buffers themselves are
324 * protected by per_cpu spinlocks. But the action of the swap
325 * needs its own lock.
326 *
327 * This is defined as a raw_spinlock_t in order to help
328 * with performance when lockdep debugging is enabled.
329 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200330static raw_spinlock_t ftrace_max_lock =
331 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200332
333/*
334 * Copy the new maximum trace into the separate maximum-trace
335 * structure. (this way the maximum trace is permanently saved,
336 * for later retrieval via /debugfs/tracing/latency_trace)
337 */
Ingo Molnare309b412008-05-12 21:20:51 +0200338static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200339__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
340{
341 struct trace_array_cpu *data = tr->data[cpu];
342
343 max_tr.cpu = cpu;
344 max_tr.time_start = data->preempt_timestamp;
345
346 data = max_tr.data[cpu];
347 data->saved_latency = tracing_max_latency;
348
349 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
350 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100351 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200352 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
353 data->policy = tsk->policy;
354 data->rt_priority = tsk->rt_priority;
355
356 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500357 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200358}
359
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200360ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
361{
362 int len;
363 int ret;
364
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500365 if (!cnt)
366 return 0;
367
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200368 if (s->len <= s->readpos)
369 return -EBUSY;
370
371 len = s->len - s->readpos;
372 if (cnt > len)
373 cnt = len;
374 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500375 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200376 return -EFAULT;
377
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500378 cnt -= ret;
379
Steven Rostedte74da522009-03-04 20:31:11 -0500380 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200381 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200382}
383
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200384ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
385{
386 int len;
387 void *ret;
388
389 if (s->len <= s->readpos)
390 return -EBUSY;
391
392 len = s->len - s->readpos;
393 if (cnt > len)
394 cnt = len;
395 ret = memcpy(buf, s->buffer + s->readpos, cnt);
396 if (!ret)
397 return -EFAULT;
398
Steven Rostedte74da522009-03-04 20:31:11 -0500399 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200400 return cnt;
401}
402
Ingo Molnare309b412008-05-12 21:20:51 +0200403static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200404trace_print_seq(struct seq_file *m, struct trace_seq *s)
405{
406 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
407
408 s->buffer[len] = 0;
409 seq_puts(m, s->buffer);
410
Steven Rostedtf9520752009-03-02 14:04:40 -0500411 trace_seq_init(s);
Steven Rostedt214023c2008-05-12 21:20:46 +0200412}
413
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200414/**
415 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
416 * @tr: tracer
417 * @tsk: the task with the latency
418 * @cpu: The cpu that initiated the trace.
419 *
420 * Flip the buffers between the @tr and the max_tr and record information
421 * about which task was the cause of this latency.
422 */
Ingo Molnare309b412008-05-12 21:20:51 +0200423void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200424update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
425{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400426 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200427
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200428 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200429 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400430
431 tr->buffer = max_tr.buffer;
432 max_tr.buffer = buf;
433
Steven Rostedtd7690412008-10-01 00:29:53 -0400434 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400435 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400436 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200437
438 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200439 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200440}
441
442/**
443 * update_max_tr_single - only copy one trace over, and reset the rest
444 * @tr - tracer
445 * @tsk - task with the latency
446 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200447 *
448 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200449 */
Ingo Molnare309b412008-05-12 21:20:51 +0200450void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200451update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
452{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400453 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200454
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200455 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200456 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200457
Steven Rostedtd7690412008-10-01 00:29:53 -0400458 ftrace_disable_cpu();
459
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400460 ring_buffer_reset(max_tr.buffer);
461 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
462
Steven Rostedtd7690412008-10-01 00:29:53 -0400463 ftrace_enable_cpu();
464
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500465 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200466
467 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200468 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200469}
470
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200471/**
472 * register_tracer - register a tracer with the ftrace system.
473 * @type - the plugin for the tracer
474 *
475 * Register a new plugin tracer.
476 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200477int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100478__releases(kernel_lock)
479__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200480{
481 struct tracer *t;
482 int len;
483 int ret = 0;
484
485 if (!type->name) {
486 pr_info("Tracer must have a name\n");
487 return -1;
488 }
489
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100490 /*
491 * When this gets called we hold the BKL which means that
492 * preemption is disabled. Various trace selftests however
493 * need to disable and enable preemption for successful tests.
494 * So we drop the BKL here and grab it after the tests again.
495 */
496 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200497 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100498
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100499 tracing_selftest_running = true;
500
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501 for (t = trace_types; t; t = t->next) {
502 if (strcmp(type->name, t->name) == 0) {
503 /* already found */
504 pr_info("Trace %s already registered\n",
505 type->name);
506 ret = -1;
507 goto out;
508 }
509 }
510
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100511 if (!type->set_flag)
512 type->set_flag = &dummy_set_flag;
513 if (!type->flags)
514 type->flags = &dummy_tracer_flags;
515 else
516 if (!type->flags->opts)
517 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100518 if (!type->wait_pipe)
519 type->wait_pipe = default_wait_pipe;
520
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100521
Steven Rostedt60a11772008-05-12 21:20:44 +0200522#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500523 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200524 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200525 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200526 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100527
Steven Rostedt60a11772008-05-12 21:20:44 +0200528 /*
529 * Run a selftest on this tracer.
530 * Here we reset the trace buffer, and set the current
531 * tracer to be this tracer. The tracer can then run some
532 * internal tracing to verify that everything is in order.
533 * If we fail, we do not register this tracer.
534 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100535 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400536 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100537
Steven Rostedt60a11772008-05-12 21:20:44 +0200538 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200539 /* the test is responsible for initializing and enabling */
540 pr_info("Testing tracer %s: ", type->name);
541 ret = type->selftest(type, tr);
542 /* the test is responsible for resetting too */
543 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200544 if (ret) {
545 printk(KERN_CONT "FAILED!\n");
546 goto out;
547 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200548 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100549 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400550 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100551
Steven Rostedt60a11772008-05-12 21:20:44 +0200552 printk(KERN_CONT "PASSED\n");
553 }
554#endif
555
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200556 type->next = trace_types;
557 trace_types = type;
558 len = strlen(type->name);
559 if (len > max_tracer_type_len)
560 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200561
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200562 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100563 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200564 mutex_unlock(&trace_types_lock);
565
Steven Rostedtdac74942009-02-05 01:13:38 -0500566 if (ret || !default_bootup_tracer)
567 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500568
Steven Rostedtdac74942009-02-05 01:13:38 -0500569 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
570 goto out_unlock;
571
572 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
573 /* Do we want this tracer to start on bootup? */
574 tracing_set_tracer(type->name);
575 default_bootup_tracer = NULL;
576 /* disable other selftests, since this will break it. */
577 tracing_selftest_disabled = 1;
578#ifdef CONFIG_FTRACE_STARTUP_TEST
579 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
580 type->name);
581#endif
582
583 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500584 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200585 return ret;
586}
587
588void unregister_tracer(struct tracer *type)
589{
590 struct tracer **t;
591 int len;
592
593 mutex_lock(&trace_types_lock);
594 for (t = &trace_types; *t; t = &(*t)->next) {
595 if (*t == type)
596 goto found;
597 }
598 pr_info("Trace %s not registered\n", type->name);
599 goto out;
600
601 found:
602 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200603
604 if (type == current_trace && tracer_enabled) {
605 tracer_enabled = 0;
606 tracing_stop();
607 if (current_trace->stop)
608 current_trace->stop(&global_trace);
609 current_trace = &nop_trace;
610 }
611
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200612 if (strlen(type->name) != max_tracer_type_len)
613 goto out;
614
615 max_tracer_type_len = 0;
616 for (t = &trace_types; *t; t = &(*t)->next) {
617 len = strlen((*t)->name);
618 if (len > max_tracer_type_len)
619 max_tracer_type_len = len;
620 }
621 out:
622 mutex_unlock(&trace_types_lock);
623}
624
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400625void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200626{
Steven Rostedtd7690412008-10-01 00:29:53 -0400627 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400628 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400629 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200630}
631
Pekka J Enberg213cc062008-12-19 12:08:39 +0200632void tracing_reset_online_cpus(struct trace_array *tr)
633{
634 int cpu;
635
636 tr->time_start = ftrace_now(tr->cpu);
637
638 for_each_online_cpu(cpu)
639 tracing_reset(tr, cpu);
640}
641
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200642#define SAVED_CMDLINES 128
643static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
644static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
645static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
646static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100647static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200648
Steven Rostedt25b0b442008-05-12 21:21:00 +0200649/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100650static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200651
652static void trace_init_cmdlines(void)
653{
654 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
655 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
656 cmdline_idx = 0;
657}
658
Steven Rostedt0f048702008-11-05 16:05:44 -0500659static int trace_stop_count;
660static DEFINE_SPINLOCK(tracing_start_lock);
661
662/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500663 * ftrace_off_permanent - disable all ftrace code permanently
664 *
665 * This should only be called when a serious anomally has
666 * been detected. This will turn off the function tracing,
667 * ring buffers, and other tracing utilites. It takes no
668 * locks and can be called from any context.
669 */
670void ftrace_off_permanent(void)
671{
672 tracing_disabled = 1;
673 ftrace_stop();
674 tracing_off_permanent();
675}
676
677/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500678 * tracing_start - quick start of the tracer
679 *
680 * If tracing is enabled but was stopped by tracing_stop,
681 * this will start the tracer back up.
682 */
683void tracing_start(void)
684{
685 struct ring_buffer *buffer;
686 unsigned long flags;
687
688 if (tracing_disabled)
689 return;
690
691 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500692 if (--trace_stop_count) {
693 if (trace_stop_count < 0) {
694 /* Someone screwed up their debugging */
695 WARN_ON_ONCE(1);
696 trace_stop_count = 0;
697 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500698 goto out;
699 }
700
701
702 buffer = global_trace.buffer;
703 if (buffer)
704 ring_buffer_record_enable(buffer);
705
706 buffer = max_tr.buffer;
707 if (buffer)
708 ring_buffer_record_enable(buffer);
709
710 ftrace_start();
711 out:
712 spin_unlock_irqrestore(&tracing_start_lock, flags);
713}
714
715/**
716 * tracing_stop - quick stop of the tracer
717 *
718 * Light weight way to stop tracing. Use in conjunction with
719 * tracing_start.
720 */
721void tracing_stop(void)
722{
723 struct ring_buffer *buffer;
724 unsigned long flags;
725
726 ftrace_stop();
727 spin_lock_irqsave(&tracing_start_lock, flags);
728 if (trace_stop_count++)
729 goto out;
730
731 buffer = global_trace.buffer;
732 if (buffer)
733 ring_buffer_record_disable(buffer);
734
735 buffer = max_tr.buffer;
736 if (buffer)
737 ring_buffer_record_disable(buffer);
738
739 out:
740 spin_unlock_irqrestore(&tracing_start_lock, flags);
741}
742
Ingo Molnare309b412008-05-12 21:20:51 +0200743void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200744
Ingo Molnare309b412008-05-12 21:20:51 +0200745static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200746{
747 unsigned map;
748 unsigned idx;
749
750 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
751 return;
752
753 /*
754 * It's not the end of the world if we don't get
755 * the lock, but we also don't want to spin
756 * nor do we want to disable interrupts,
757 * so if we miss here, then better luck next time.
758 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100759 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200760 return;
761
762 idx = map_pid_to_cmdline[tsk->pid];
763 if (idx >= SAVED_CMDLINES) {
764 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
765
766 map = map_cmdline_to_pid[idx];
767 if (map <= PID_MAX_DEFAULT)
768 map_pid_to_cmdline[map] = (unsigned)-1;
769
770 map_pid_to_cmdline[tsk->pid] = idx;
771
772 cmdline_idx = idx;
773 }
774
775 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
776
Peter Zijlstraefed7922009-03-04 12:32:55 +0100777 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200778}
779
Steven Rostedt4ca530852009-03-16 19:20:15 -0400780void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200782 unsigned map;
783
Steven Rostedt4ca530852009-03-16 19:20:15 -0400784 if (!pid) {
785 strcpy(comm, "<idle>");
786 return;
787 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200788
Steven Rostedt4ca530852009-03-16 19:20:15 -0400789 if (pid > PID_MAX_DEFAULT) {
790 strcpy(comm, "<...>");
791 return;
792 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793
Steven Rostedt4ca530852009-03-16 19:20:15 -0400794 __raw_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200795 map = map_pid_to_cmdline[pid];
796 if (map >= SAVED_CMDLINES)
797 goto out;
798
Steven Rostedt4ca530852009-03-16 19:20:15 -0400799 strcpy(comm, saved_cmdlines[map]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200800
801 out:
Steven Rostedt4ca530852009-03-16 19:20:15 -0400802 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200803}
804
Ingo Molnare309b412008-05-12 21:20:51 +0200805void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200806{
Steven Rostedt6adaad12009-03-16 21:57:17 -0400807 if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808 return;
809
810 trace_save_cmdline(tsk);
811}
812
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300813void
Steven Rostedt38697052008-10-01 13:14:09 -0400814tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
815 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200816{
817 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818
Steven Rostedt777e2082008-09-29 23:02:42 -0400819 entry->preempt_count = pc & 0xff;
820 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedtef180122009-03-10 14:10:56 -0400821 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400822 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400823#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400824 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400825#else
826 TRACE_FLAG_IRQS_NOSUPPORT |
827#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200828 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
829 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
830 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
831}
832
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200833struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
834 unsigned char type,
835 unsigned long len,
836 unsigned long flags, int pc)
837{
838 struct ring_buffer_event *event;
839
840 event = ring_buffer_lock_reserve(tr->buffer, len);
841 if (event != NULL) {
842 struct trace_entry *ent = ring_buffer_event_data(event);
843
844 tracing_generic_entry_update(ent, flags, pc);
845 ent->type = type;
846 }
847
848 return event;
849}
850static void ftrace_trace_stack(struct trace_array *tr,
851 unsigned long flags, int skip, int pc);
852static void ftrace_trace_userstack(struct trace_array *tr,
853 unsigned long flags, int pc);
854
855void trace_buffer_unlock_commit(struct trace_array *tr,
856 struct ring_buffer_event *event,
857 unsigned long flags, int pc)
858{
859 ring_buffer_unlock_commit(tr->buffer, event);
860
861 ftrace_trace_stack(tr, flags, 6, pc);
862 ftrace_trace_userstack(tr, flags, pc);
863 trace_wake_up();
864}
865
Steven Rostedtef5580d2009-02-27 19:38:04 -0500866struct ring_buffer_event *
867trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
868 unsigned long flags, int pc)
869{
870 return trace_buffer_lock_reserve(&global_trace,
871 type, len, flags, pc);
872}
873
874void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
875 unsigned long flags, int pc)
876{
877 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
878}
879
Ingo Molnare309b412008-05-12 21:20:51 +0200880void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500881trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400882 unsigned long ip, unsigned long parent_ip, unsigned long flags,
883 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200884{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400885 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400886 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200887
Steven Rostedtd7690412008-10-01 00:29:53 -0400888 /* If we are reading the ring buffer, don't trace */
889 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
890 return;
891
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200892 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
893 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400894 if (!event)
895 return;
896 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400897 entry->ip = ip;
898 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200899 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200900}
901
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100902#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100903static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100904 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100905 unsigned long flags,
906 int pc)
907{
908 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100909 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100910
911 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
912 return;
913
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200914 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
915 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100916 if (!event)
917 return;
918 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100919 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200920 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100921}
922
923static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100924 struct ftrace_graph_ret *trace,
925 unsigned long flags,
926 int pc)
927{
928 struct ring_buffer_event *event;
929 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100930
931 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
932 return;
933
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200934 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
935 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100936 if (!event)
937 return;
938 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100939 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200940 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100941}
942#endif
943
Ingo Molnare309b412008-05-12 21:20:51 +0200944void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200945ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400946 unsigned long ip, unsigned long parent_ip, unsigned long flags,
947 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200948{
949 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500950 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200951}
952
Steven Rostedt53614992009-01-15 19:12:40 -0500953static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500954 unsigned long flags,
955 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200956{
Al Viroc2c80522008-10-31 19:50:41 +0000957#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400958 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400959 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200960 struct stack_trace trace;
961
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200962 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
963 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400964 if (!event)
965 return;
966 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400967 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200968
969 trace.nr_entries = 0;
970 trace.max_entries = FTRACE_STACK_ENTRIES;
971 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400972 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200973
974 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200975 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000976#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200977}
978
Steven Rostedt53614992009-01-15 19:12:40 -0500979static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500980 unsigned long flags,
981 int skip, int pc)
982{
983 if (!(trace_flags & TRACE_ITER_STACKTRACE))
984 return;
985
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500986 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500987}
988
Steven Rostedt38697052008-10-01 13:14:09 -0400989void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400990 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500991 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400992{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500993 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400994}
995
Török Edwin02b67512008-11-22 13:28:47 +0200996static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500997 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +0200998{
Török Edwinc7425ac2008-11-28 11:17:56 +0200999#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +02001000 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001001 struct userstack_entry *entry;
1002 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001003
1004 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1005 return;
1006
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001007 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1008 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001009 if (!event)
1010 return;
1011 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001012
1013 memset(&entry->caller, 0, sizeof(entry->caller));
1014
1015 trace.nr_entries = 0;
1016 trace.max_entries = FTRACE_STACK_ENTRIES;
1017 trace.skip = 0;
1018 trace.entries = entry->caller;
1019
1020 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001021 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001022#endif
Török Edwin02b67512008-11-22 13:28:47 +02001023}
1024
Hannes Eder4fd27352009-02-10 19:44:12 +01001025#ifdef UNUSED
1026static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001027{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001028 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001029}
Hannes Eder4fd27352009-02-10 19:44:12 +01001030#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001031
Steven Rostedt38697052008-10-01 13:14:09 -04001032static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001033ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001034 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1035 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +02001036{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001037 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001038 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001039 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001040
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001041 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1042 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001043 if (!event)
1044 return;
1045 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001046 entry->arg1 = arg1;
1047 entry->arg2 = arg2;
1048 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001049 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +02001050}
1051
1052void
Steven Rostedt38697052008-10-01 13:14:09 -04001053__trace_special(void *__tr, void *__data,
1054 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1055{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001056 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001057}
1058
1059void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001060tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001061 struct task_struct *prev,
1062 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001063 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001064{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001065 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001066 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001067
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001068 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1069 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001070 if (!event)
1071 return;
1072 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001073 entry->prev_pid = prev->pid;
1074 entry->prev_prio = prev->prio;
1075 entry->prev_state = prev->state;
1076 entry->next_pid = next->pid;
1077 entry->next_prio = next->prio;
1078 entry->next_state = next->state;
1079 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001080 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001081}
1082
Ingo Molnar57422792008-05-12 21:20:51 +02001083void
1084tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001085 struct task_struct *wakee,
1086 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001087 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001088{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001089 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001090 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001091
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001092 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1093 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001094 if (!event)
1095 return;
1096 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001097 entry->prev_pid = curr->pid;
1098 entry->prev_prio = curr->prio;
1099 entry->prev_state = curr->state;
1100 entry->next_pid = wakee->pid;
1101 entry->next_prio = wakee->prio;
1102 entry->next_state = wakee->state;
1103 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001104
1105 ring_buffer_unlock_commit(tr->buffer, event);
1106 ftrace_trace_stack(tr, flags, 6, pc);
1107 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001108}
1109
Steven Rostedt4902f882008-05-22 00:22:18 -04001110void
1111ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1112{
1113 struct trace_array *tr = &global_trace;
1114 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001115 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001116 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001117 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001118
Steven Rostedtc76f0692008-11-07 22:36:02 -05001119 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001120 return;
1121
Steven Rostedt38697052008-10-01 13:14:09 -04001122 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001123 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001124 cpu = raw_smp_processor_id();
1125 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001126
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001127 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001128 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001129
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001130 atomic_dec(&data->disabled);
1131 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001132}
1133
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001134#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001135int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001136{
1137 struct trace_array *tr = &global_trace;
1138 struct trace_array_cpu *data;
1139 unsigned long flags;
1140 long disabled;
1141 int cpu;
1142 int pc;
1143
Steven Rostedt804a6852008-12-03 15:36:59 -05001144 if (!ftrace_trace_task(current))
1145 return 0;
1146
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001147 if (!ftrace_graph_addr(trace->func))
1148 return 0;
1149
Steven Rostedta5e25882008-12-02 15:34:05 -05001150 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001151 cpu = raw_smp_processor_id();
1152 data = tr->data[cpu];
1153 disabled = atomic_inc_return(&data->disabled);
1154 if (likely(disabled == 1)) {
1155 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001156 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001157 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001158 /* Only do the atomic if it is not already set */
1159 if (!test_tsk_trace_graph(current))
1160 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001161 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001162 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001163
1164 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001165}
1166
1167void trace_graph_return(struct ftrace_graph_ret *trace)
1168{
1169 struct trace_array *tr = &global_trace;
1170 struct trace_array_cpu *data;
1171 unsigned long flags;
1172 long disabled;
1173 int cpu;
1174 int pc;
1175
Steven Rostedta5e25882008-12-02 15:34:05 -05001176 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001177 cpu = raw_smp_processor_id();
1178 data = tr->data[cpu];
1179 disabled = atomic_inc_return(&data->disabled);
1180 if (likely(disabled == 1)) {
1181 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001182 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001183 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001184 if (!trace->depth)
1185 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001186 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001187 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001188}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001189#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001190
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001191
1192/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001193 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001194 *
1195 */
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001196int trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001197{
Steven Rostedt80370cb2009-03-10 17:16:35 -04001198 static raw_spinlock_t trace_buf_lock =
1199 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001200 static u32 trace_buf[TRACE_BUF_SIZE];
1201
1202 struct ring_buffer_event *event;
1203 struct trace_array *tr = &global_trace;
1204 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001205 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001206 unsigned long flags;
1207 int resched;
1208 int cpu, len = 0, size, pc;
1209
1210 if (unlikely(tracing_selftest_running || tracing_disabled))
1211 return 0;
1212
1213 /* Don't pollute graph traces with trace_vprintk internals */
1214 pause_graph_tracing();
1215
1216 pc = preempt_count();
1217 resched = ftrace_preempt_disable();
1218 cpu = raw_smp_processor_id();
1219 data = tr->data[cpu];
1220
1221 if (unlikely(atomic_read(&data->disabled)))
1222 goto out;
1223
Steven Rostedt80370cb2009-03-10 17:16:35 -04001224 /* Lockdep uses trace_printk for lock tracing */
1225 local_irq_save(flags);
1226 __raw_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001227 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1228
1229 if (len > TRACE_BUF_SIZE || len < 0)
1230 goto out_unlock;
1231
1232 size = sizeof(*entry) + sizeof(u32) * len;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001233 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001234 if (!event)
1235 goto out_unlock;
1236 entry = ring_buffer_event_data(event);
1237 entry->ip = ip;
1238 entry->depth = depth;
1239 entry->fmt = fmt;
1240
1241 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1242 ring_buffer_unlock_commit(tr->buffer, event);
1243
1244out_unlock:
Steven Rostedt80370cb2009-03-10 17:16:35 -04001245 __raw_spin_unlock(&trace_buf_lock);
1246 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001247
1248out:
1249 ftrace_preempt_enable(resched);
1250 unpause_graph_tracing();
1251
1252 return len;
1253}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001254EXPORT_SYMBOL_GPL(trace_vbprintk);
1255
1256int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1257{
1258 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1259 static char trace_buf[TRACE_BUF_SIZE];
1260
1261 struct ring_buffer_event *event;
1262 struct trace_array *tr = &global_trace;
1263 struct trace_array_cpu *data;
1264 int cpu, len = 0, size, pc;
1265 struct print_entry *entry;
1266 unsigned long irq_flags;
1267
1268 if (tracing_disabled || tracing_selftest_running)
1269 return 0;
1270
1271 pc = preempt_count();
1272 preempt_disable_notrace();
1273 cpu = raw_smp_processor_id();
1274 data = tr->data[cpu];
1275
1276 if (unlikely(atomic_read(&data->disabled)))
1277 goto out;
1278
1279 pause_graph_tracing();
1280 raw_local_irq_save(irq_flags);
1281 __raw_spin_lock(&trace_buf_lock);
1282 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1283
1284 len = min(len, TRACE_BUF_SIZE-1);
1285 trace_buf[len] = 0;
1286
1287 size = sizeof(*entry) + len + 1;
1288 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1289 if (!event)
1290 goto out_unlock;
1291 entry = ring_buffer_event_data(event);
1292 entry->ip = ip;
1293 entry->depth = depth;
1294
1295 memcpy(&entry->buf, trace_buf, len);
1296 entry->buf[len] = 0;
1297 ring_buffer_unlock_commit(tr->buffer, event);
1298
1299 out_unlock:
1300 __raw_spin_unlock(&trace_buf_lock);
1301 raw_local_irq_restore(irq_flags);
1302 unpause_graph_tracing();
1303 out:
1304 preempt_enable_notrace();
1305
1306 return len;
1307}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001308EXPORT_SYMBOL_GPL(trace_vprintk);
1309
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001310enum trace_file_type {
1311 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001312 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001313};
1314
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001315static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001316{
Steven Rostedtd7690412008-10-01 00:29:53 -04001317 /* Don't allow ftrace to trace into the ring buffers */
1318 ftrace_disable_cpu();
1319
Steven Rostedt5a90f572008-09-03 17:42:51 -04001320 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001321 if (iter->buffer_iter[iter->cpu])
1322 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1323
1324 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001325}
1326
Ingo Molnare309b412008-05-12 21:20:51 +02001327static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001328peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001329{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001330 struct ring_buffer_event *event;
1331 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001332
Steven Rostedtd7690412008-10-01 00:29:53 -04001333 /* Don't allow ftrace to trace into the ring buffers */
1334 ftrace_disable_cpu();
1335
1336 if (buf_iter)
1337 event = ring_buffer_iter_peek(buf_iter, ts);
1338 else
1339 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1340
1341 ftrace_enable_cpu();
1342
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001343 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001344}
Steven Rostedtd7690412008-10-01 00:29:53 -04001345
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001346static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001347__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001348{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001349 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001350 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001351 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001352 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353 int next_cpu = -1;
1354 int cpu;
1355
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001356 /*
1357 * If we are in a per_cpu trace file, don't bother by iterating over
1358 * all cpu and peek directly.
1359 */
1360 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1361 if (ring_buffer_empty_cpu(buffer, cpu_file))
1362 return NULL;
1363 ent = peek_next_entry(iter, cpu_file, ent_ts);
1364 if (ent_cpu)
1365 *ent_cpu = cpu_file;
1366
1367 return ent;
1368 }
1369
Steven Rostedtab464282008-05-12 21:21:00 +02001370 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001371
1372 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001374
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001375 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001376
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001377 /*
1378 * Pick the entry with the smallest timestamp:
1379 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001380 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001381 next = ent;
1382 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001383 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384 }
1385 }
1386
1387 if (ent_cpu)
1388 *ent_cpu = next_cpu;
1389
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001390 if (ent_ts)
1391 *ent_ts = next_ts;
1392
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001393 return next;
1394}
1395
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001396/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001397struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1398 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001399{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001400 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001401}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001402
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001403/* Find the next real entry, and increment the iterator to the next entry */
1404static void *find_next_entry_inc(struct trace_iterator *iter)
1405{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001406 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001407
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001408 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001409 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001410
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001411 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001412}
1413
Ingo Molnare309b412008-05-12 21:20:51 +02001414static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001415{
Steven Rostedtd7690412008-10-01 00:29:53 -04001416 /* Don't allow ftrace to trace into the ring buffers */
1417 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001418 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001419 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001420}
1421
Ingo Molnare309b412008-05-12 21:20:51 +02001422static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001423{
1424 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001425 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001426 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427
1428 (*pos)++;
1429
1430 /* can't go backwards */
1431 if (iter->idx > i)
1432 return NULL;
1433
1434 if (iter->idx < 0)
1435 ent = find_next_entry_inc(iter);
1436 else
1437 ent = iter;
1438
1439 while (ent && iter->idx < i)
1440 ent = find_next_entry_inc(iter);
1441
1442 iter->pos = *pos;
1443
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001444 return ent;
1445}
1446
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001447/*
1448 * No necessary locking here. The worst thing which can
1449 * happen is loosing events consumed at the same time
1450 * by a trace_pipe reader.
1451 * Other than that, we don't risk to crash the ring buffer
1452 * because it serializes the readers.
1453 *
1454 * The current tracer is copied to avoid a global locking
1455 * all around.
1456 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001457static void *s_start(struct seq_file *m, loff_t *pos)
1458{
1459 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001460 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001461 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001462 void *p = NULL;
1463 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001464 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001465
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001466 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001467 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001468 if (unlikely(old_tracer != current_trace && current_trace)) {
1469 old_tracer = current_trace;
1470 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001471 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001472 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001473
1474 atomic_inc(&trace_record_cmdline_disabled);
1475
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001476 if (*pos != iter->pos) {
1477 iter->ent = NULL;
1478 iter->cpu = 0;
1479 iter->idx = -1;
1480
Steven Rostedtd7690412008-10-01 00:29:53 -04001481 ftrace_disable_cpu();
1482
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001483 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1484 for_each_tracing_cpu(cpu)
1485 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1486 } else
1487 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1488
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001489
Steven Rostedtd7690412008-10-01 00:29:53 -04001490 ftrace_enable_cpu();
1491
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001492 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1493 ;
1494
1495 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001496 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001497 p = s_next(m, p, &l);
1498 }
1499
1500 return p;
1501}
1502
1503static void s_stop(struct seq_file *m, void *p)
1504{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001505 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001506}
1507
Ingo Molnare309b412008-05-12 21:20:51 +02001508static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001509{
Michael Ellermana6168352008-08-20 16:36:11 -07001510 seq_puts(m, "# _------=> CPU# \n");
1511 seq_puts(m, "# / _-----=> irqs-off \n");
1512 seq_puts(m, "# | / _----=> need-resched \n");
1513 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1514 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1515 seq_puts(m, "# |||| / \n");
1516 seq_puts(m, "# ||||| delay \n");
1517 seq_puts(m, "# cmd pid ||||| time | caller \n");
1518 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001519}
1520
Ingo Molnare309b412008-05-12 21:20:51 +02001521static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001522{
Michael Ellermana6168352008-08-20 16:36:11 -07001523 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1524 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001525}
1526
1527
Ingo Molnare309b412008-05-12 21:20:51 +02001528static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001529print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1530{
1531 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1532 struct trace_array *tr = iter->tr;
1533 struct trace_array_cpu *data = tr->data[tr->cpu];
1534 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001535 unsigned long total;
1536 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001537 const char *name = "preemption";
1538
1539 if (type)
1540 name = type->name;
1541
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001542 entries = ring_buffer_entries(iter->tr->buffer);
1543 total = entries +
1544 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001545
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001546 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001548 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001549 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001550 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001551 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001552 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001553 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001554 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001555 tr->cpu,
1556#if defined(CONFIG_PREEMPT_NONE)
1557 "server",
1558#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1559 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001560#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001561 "preempt",
1562#else
1563 "unknown",
1564#endif
1565 /* These are reserved for later use */
1566 0, 0, 0, 0);
1567#ifdef CONFIG_SMP
1568 seq_printf(m, " #P:%d)\n", num_online_cpus());
1569#else
1570 seq_puts(m, ")\n");
1571#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001572 seq_puts(m, "# -----------------\n");
1573 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001574 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1575 data->comm, data->pid, data->uid, data->nice,
1576 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001577 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001578
1579 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001580 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001581 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1582 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001583 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001584 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1585 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001586 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001587 }
1588
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001589 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001590}
1591
Steven Rostedta3097202008-11-07 22:36:02 -05001592static void test_cpu_buff_start(struct trace_iterator *iter)
1593{
1594 struct trace_seq *s = &iter->seq;
1595
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001596 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1597 return;
1598
1599 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1600 return;
1601
Rusty Russell44623442009-01-01 10:12:23 +10301602 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001603 return;
1604
Rusty Russell44623442009-01-01 10:12:23 +10301605 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001606 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1607}
1608
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001609static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001610{
Steven Rostedt214023c2008-05-12 21:20:46 +02001611 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001612 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001613 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001614 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001615
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001616 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001617
Steven Rostedta3097202008-11-07 22:36:02 -05001618 test_cpu_buff_start(iter);
1619
Steven Rostedtf633cef2008-12-23 23:24:13 -05001620 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001621
1622 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001623 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1624 if (!trace_print_lat_context(iter))
1625 goto partial;
1626 } else {
1627 if (!trace_print_context(iter))
1628 goto partial;
1629 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001630 }
1631
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001632 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001633 return event->trace(iter, sym_flags);
1634
1635 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1636 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001637
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001638 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001639partial:
1640 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001641}
1642
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001643static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001644{
1645 struct trace_seq *s = &iter->seq;
1646 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001647 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001648
1649 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001650
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001651 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001652 if (!trace_seq_printf(s, "%d %d %llu ",
1653 entry->pid, iter->cpu, iter->ts))
1654 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001655 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001656
Steven Rostedtf633cef2008-12-23 23:24:13 -05001657 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001658 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001659 return event->raw(iter, 0);
1660
1661 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1662 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001663
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001664 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001665partial:
1666 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001667}
1668
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001669static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001670{
1671 struct trace_seq *s = &iter->seq;
1672 unsigned char newline = '\n';
1673 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001674 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001675
1676 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001677
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001678 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1679 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1680 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1681 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1682 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001683
Steven Rostedtf633cef2008-12-23 23:24:13 -05001684 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001685 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001686 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001687 if (ret != TRACE_TYPE_HANDLED)
1688 return ret;
1689 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001690
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001691 SEQ_PUT_FIELD_RET(s, newline);
1692
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001693 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001694}
1695
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001696static enum print_line_t print_bprintk_msg_only(struct trace_iterator *iter)
1697{
1698 struct trace_seq *s = &iter->seq;
1699 struct trace_entry *entry = iter->ent;
1700 struct bprint_entry *field;
1701 int ret;
1702
1703 trace_assign_type(field, entry);
1704
1705 ret = trace_seq_bprintf(s, field->fmt, field->buf);
1706 if (!ret)
1707 return TRACE_TYPE_PARTIAL_LINE;
1708
1709 return TRACE_TYPE_HANDLED;
1710}
1711
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001712static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1713{
1714 struct trace_seq *s = &iter->seq;
1715 struct trace_entry *entry = iter->ent;
1716 struct print_entry *field;
1717 int ret;
1718
1719 trace_assign_type(field, entry);
1720
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001721 ret = trace_seq_printf(s, "%s", field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001722 if (!ret)
1723 return TRACE_TYPE_PARTIAL_LINE;
1724
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001725 return TRACE_TYPE_HANDLED;
1726}
1727
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001728static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001729{
1730 struct trace_seq *s = &iter->seq;
1731 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001732 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001733
1734 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001735
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001736 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1737 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001738 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001739 SEQ_PUT_FIELD_RET(s, iter->ts);
1740 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001741
Steven Rostedtf633cef2008-12-23 23:24:13 -05001742 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001743 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001744}
1745
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001746static int trace_empty(struct trace_iterator *iter)
1747{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001748 int cpu;
1749
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001750 /* If we are looking at one CPU buffer, only check that one */
1751 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1752 cpu = iter->cpu_file;
1753 if (iter->buffer_iter[cpu]) {
1754 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1755 return 0;
1756 } else {
1757 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1758 return 0;
1759 }
1760 return 1;
1761 }
1762
Steven Rostedtab464282008-05-12 21:21:00 +02001763 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001764 if (iter->buffer_iter[cpu]) {
1765 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1766 return 0;
1767 } else {
1768 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1769 return 0;
1770 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001771 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001772
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001773 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001774}
1775
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001776static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001777{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001778 enum print_line_t ret;
1779
1780 if (iter->trace && iter->trace->print_line) {
1781 ret = iter->trace->print_line(iter);
1782 if (ret != TRACE_TYPE_UNHANDLED)
1783 return ret;
1784 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001785
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001786 if (iter->ent->type == TRACE_BPRINT &&
1787 trace_flags & TRACE_ITER_PRINTK &&
1788 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1789 return print_bprintk_msg_only(iter);
1790
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001791 if (iter->ent->type == TRACE_PRINT &&
1792 trace_flags & TRACE_ITER_PRINTK &&
1793 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1794 return print_printk_msg_only(iter);
1795
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001796 if (trace_flags & TRACE_ITER_BIN)
1797 return print_bin_fmt(iter);
1798
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001799 if (trace_flags & TRACE_ITER_HEX)
1800 return print_hex_fmt(iter);
1801
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001802 if (trace_flags & TRACE_ITER_RAW)
1803 return print_raw_fmt(iter);
1804
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001805 return print_trace_fmt(iter);
1806}
1807
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001808static int s_show(struct seq_file *m, void *v)
1809{
1810 struct trace_iterator *iter = v;
1811
1812 if (iter->ent == NULL) {
1813 if (iter->tr) {
1814 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1815 seq_puts(m, "#\n");
1816 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001817 if (iter->trace && iter->trace->print_header)
1818 iter->trace->print_header(m);
1819 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001820 /* print nothing if the buffers are empty */
1821 if (trace_empty(iter))
1822 return 0;
1823 print_trace_header(m, iter);
1824 if (!(trace_flags & TRACE_ITER_VERBOSE))
1825 print_lat_help_header(m);
1826 } else {
1827 if (!(trace_flags & TRACE_ITER_VERBOSE))
1828 print_func_help_header(m);
1829 }
1830 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001831 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001832 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001833 }
1834
1835 return 0;
1836}
1837
1838static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001839 .start = s_start,
1840 .next = s_next,
1841 .stop = s_stop,
1842 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001843};
1844
Ingo Molnare309b412008-05-12 21:20:51 +02001845static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001846__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001847{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001848 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001849 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001850 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001851 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001852 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001853
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001854 if (tracing_disabled)
1855 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001856
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001857 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001858 if (!iter)
1859 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001860
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001861 /*
1862 * We make a copy of the current tracer to avoid concurrent
1863 * changes on it while we are reading.
1864 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001865 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001866 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001867 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001868 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001869
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001870 if (current_trace)
1871 *iter->trace = *current_trace;
1872
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001873 if (current_trace && current_trace->print_max)
1874 iter->tr = &max_tr;
1875 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001876 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001877 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001878 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001879 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001880
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001881 /* Notify the tracer early; before we stop tracing. */
1882 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001883 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001884
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001885 /* Annotate start of buffers if we had overruns */
1886 if (ring_buffer_overruns(iter->tr->buffer))
1887 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1888
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001889 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1890 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001891
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001892 iter->buffer_iter[cpu] =
1893 ring_buffer_read_start(iter->tr->buffer, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001894 }
1895 } else {
1896 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001897 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001898 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001899 }
1900
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001901 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001902 ret = seq_open(file, &tracer_seq_ops);
1903 if (ret < 0) {
1904 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001905 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001906 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001907
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001908 m = file->private_data;
1909 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001910
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001911 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001912 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001913
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001914 mutex_unlock(&trace_types_lock);
1915
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001916 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001917
1918 fail_buffer:
1919 for_each_tracing_cpu(cpu) {
1920 if (iter->buffer_iter[cpu])
1921 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1922 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001923 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001924 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001925 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001926 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001927
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001928 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001929}
1930
1931int tracing_open_generic(struct inode *inode, struct file *filp)
1932{
Steven Rostedt60a11772008-05-12 21:20:44 +02001933 if (tracing_disabled)
1934 return -ENODEV;
1935
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001936 filp->private_data = inode->i_private;
1937 return 0;
1938}
1939
Hannes Eder4fd27352009-02-10 19:44:12 +01001940static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001941{
1942 struct seq_file *m = (struct seq_file *)file->private_data;
1943 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001944 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001945
1946 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001947 for_each_tracing_cpu(cpu) {
1948 if (iter->buffer_iter[cpu])
1949 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1950 }
1951
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001952 if (iter->trace && iter->trace->close)
1953 iter->trace->close(iter);
1954
1955 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001956 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001957 mutex_unlock(&trace_types_lock);
1958
1959 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001960 mutex_destroy(&iter->mutex);
1961 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001962 kfree(iter);
1963 return 0;
1964}
1965
1966static int tracing_open(struct inode *inode, struct file *file)
1967{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001968 struct trace_iterator *iter;
1969 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001970
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001971 iter = __tracing_open(inode, file);
1972 if (IS_ERR(iter))
1973 ret = PTR_ERR(iter);
Steven Rostedtc032ef642009-03-04 20:34:24 -05001974 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001975 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1976
1977 return ret;
1978}
1979
Ingo Molnare309b412008-05-12 21:20:51 +02001980static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001981t_next(struct seq_file *m, void *v, loff_t *pos)
1982{
1983 struct tracer *t = m->private;
1984
1985 (*pos)++;
1986
1987 if (t)
1988 t = t->next;
1989
1990 m->private = t;
1991
1992 return t;
1993}
1994
1995static void *t_start(struct seq_file *m, loff_t *pos)
1996{
1997 struct tracer *t = m->private;
1998 loff_t l = 0;
1999
2000 mutex_lock(&trace_types_lock);
2001 for (; t && l < *pos; t = t_next(m, t, &l))
2002 ;
2003
2004 return t;
2005}
2006
2007static void t_stop(struct seq_file *m, void *p)
2008{
2009 mutex_unlock(&trace_types_lock);
2010}
2011
2012static int t_show(struct seq_file *m, void *v)
2013{
2014 struct tracer *t = v;
2015
2016 if (!t)
2017 return 0;
2018
2019 seq_printf(m, "%s", t->name);
2020 if (t->next)
2021 seq_putc(m, ' ');
2022 else
2023 seq_putc(m, '\n');
2024
2025 return 0;
2026}
2027
2028static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002029 .start = t_start,
2030 .next = t_next,
2031 .stop = t_stop,
2032 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002033};
2034
2035static int show_traces_open(struct inode *inode, struct file *file)
2036{
2037 int ret;
2038
Steven Rostedt60a11772008-05-12 21:20:44 +02002039 if (tracing_disabled)
2040 return -ENODEV;
2041
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002042 ret = seq_open(file, &show_traces_seq_ops);
2043 if (!ret) {
2044 struct seq_file *m = file->private_data;
2045 m->private = trace_types;
2046 }
2047
2048 return ret;
2049}
2050
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002051static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002052 .open = tracing_open,
2053 .read = seq_read,
2054 .llseek = seq_lseek,
2055 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002056};
2057
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002058static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002059 .open = show_traces_open,
2060 .read = seq_read,
2061 .release = seq_release,
2062};
2063
Ingo Molnar36dfe922008-05-12 21:20:52 +02002064/*
2065 * Only trace on a CPU if the bitmask is set:
2066 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302067static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002068
2069/*
2070 * The tracer itself will not take this lock, but still we want
2071 * to provide a consistent cpumask to user-space:
2072 */
2073static DEFINE_MUTEX(tracing_cpumask_update_lock);
2074
2075/*
2076 * Temporary storage for the character representation of the
2077 * CPU bitmask (and one more byte for the newline):
2078 */
2079static char mask_str[NR_CPUS + 1];
2080
Ingo Molnarc7078de2008-05-12 21:20:52 +02002081static ssize_t
2082tracing_cpumask_read(struct file *filp, char __user *ubuf,
2083 size_t count, loff_t *ppos)
2084{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002085 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002086
2087 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002088
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302089 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002090 if (count - len < 2) {
2091 count = -EINVAL;
2092 goto out_err;
2093 }
2094 len += sprintf(mask_str + len, "\n");
2095 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2096
2097out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002098 mutex_unlock(&tracing_cpumask_update_lock);
2099
2100 return count;
2101}
2102
2103static ssize_t
2104tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2105 size_t count, loff_t *ppos)
2106{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002107 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302108 cpumask_var_t tracing_cpumask_new;
2109
2110 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2111 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002112
2113 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302114 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002115 if (err)
2116 goto err_unlock;
2117
Steven Rostedta5e25882008-12-02 15:34:05 -05002118 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02002119 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002120 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002121 /*
2122 * Increase/decrease the disabled counter if we are
2123 * about to flip a bit in the cpumask:
2124 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302125 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2126 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002127 atomic_inc(&global_trace.data[cpu]->disabled);
2128 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302129 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2130 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002131 atomic_dec(&global_trace.data[cpu]->disabled);
2132 }
2133 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002134 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002135 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002136
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302137 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002138
Ingo Molnarc7078de2008-05-12 21:20:52 +02002139 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302140 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002141
Ingo Molnarc7078de2008-05-12 21:20:52 +02002142 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002143
2144err_unlock:
2145 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302146 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002147
2148 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002149}
2150
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002151static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002152 .open = tracing_open_generic,
2153 .read = tracing_cpumask_read,
2154 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002155};
2156
2157static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002158tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002159 size_t cnt, loff_t *ppos)
2160{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002161 struct tracer_opt *trace_opts;
2162 u32 tracer_flags;
2163 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002164 char *buf;
2165 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002166 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002167
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002168
Wenji Huangc3706f02009-02-10 01:03:18 -05002169 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002170 for (i = 0; trace_options[i]; i++) {
2171 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002172 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002173 }
2174
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002175 mutex_lock(&trace_types_lock);
2176 tracer_flags = current_trace->flags->val;
2177 trace_opts = current_trace->flags->opts;
2178
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002179 /*
2180 * Increase the size with names of options specific
2181 * of the current tracer.
2182 */
2183 for (i = 0; trace_opts[i].name; i++) {
2184 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002185 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002186 }
2187
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188 /* +2 for \n and \0 */
2189 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002190 if (!buf) {
2191 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002192 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002193 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002194
2195 for (i = 0; trace_options[i]; i++) {
2196 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002197 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002198 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002199 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002200 }
2201
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002202 for (i = 0; trace_opts[i].name; i++) {
2203 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002204 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002205 trace_opts[i].name);
2206 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002207 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002208 trace_opts[i].name);
2209 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002210 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002211
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002212 WARN_ON(r >= len + 2);
2213
Ingo Molnar36dfe922008-05-12 21:20:52 +02002214 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002215
2216 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002217 return r;
2218}
2219
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002220/* Try to assign a tracer specific option */
2221static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2222{
2223 struct tracer_flags *trace_flags = trace->flags;
2224 struct tracer_opt *opts = NULL;
2225 int ret = 0, i = 0;
2226 int len;
2227
2228 for (i = 0; trace_flags->opts[i].name; i++) {
2229 opts = &trace_flags->opts[i];
2230 len = strlen(opts->name);
2231
2232 if (strncmp(cmp, opts->name, len) == 0) {
2233 ret = trace->set_flag(trace_flags->val,
2234 opts->bit, !neg);
2235 break;
2236 }
2237 }
2238 /* Not found */
2239 if (!trace_flags->opts[i].name)
2240 return -EINVAL;
2241
2242 /* Refused to handle */
2243 if (ret)
2244 return ret;
2245
2246 if (neg)
2247 trace_flags->val &= ~opts->bit;
2248 else
2249 trace_flags->val |= opts->bit;
2250
2251 return 0;
2252}
2253
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002254static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002255tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002256 size_t cnt, loff_t *ppos)
2257{
2258 char buf[64];
2259 char *cmp = buf;
2260 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002261 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002262 int i;
2263
Steven Rostedtcffae432008-05-12 21:21:00 +02002264 if (cnt >= sizeof(buf))
2265 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002266
2267 if (copy_from_user(&buf, ubuf, cnt))
2268 return -EFAULT;
2269
2270 buf[cnt] = 0;
2271
2272 if (strncmp(buf, "no", 2) == 0) {
2273 neg = 1;
2274 cmp += 2;
2275 }
2276
2277 for (i = 0; trace_options[i]; i++) {
2278 int len = strlen(trace_options[i]);
2279
2280 if (strncmp(cmp, trace_options[i], len) == 0) {
2281 if (neg)
2282 trace_flags &= ~(1 << i);
2283 else
2284 trace_flags |= (1 << i);
2285 break;
2286 }
2287 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002288
2289 /* If no option could be set, test the specific tracer options */
2290 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002291 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002292 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002293 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002294 if (ret)
2295 return ret;
2296 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002297
2298 filp->f_pos += cnt;
2299
2300 return cnt;
2301}
2302
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002303static const struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002304 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002305 .read = tracing_trace_options_read,
2306 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002307};
2308
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002309static const char readme_msg[] =
2310 "tracing mini-HOWTO:\n\n"
2311 "# mkdir /debug\n"
2312 "# mount -t debugfs nodev /debug\n\n"
2313 "# cat /debug/tracing/available_tracers\n"
2314 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2315 "# cat /debug/tracing/current_tracer\n"
2316 "none\n"
2317 "# echo sched_switch > /debug/tracing/current_tracer\n"
2318 "# cat /debug/tracing/current_tracer\n"
2319 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002320 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002321 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002322 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002323 "# echo 1 > /debug/tracing/tracing_enabled\n"
2324 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2325 "echo 0 > /debug/tracing/tracing_enabled\n"
2326;
2327
2328static ssize_t
2329tracing_readme_read(struct file *filp, char __user *ubuf,
2330 size_t cnt, loff_t *ppos)
2331{
2332 return simple_read_from_buffer(ubuf, cnt, ppos,
2333 readme_msg, strlen(readme_msg));
2334}
2335
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002336static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002337 .open = tracing_open_generic,
2338 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002339};
2340
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002341static ssize_t
2342tracing_ctrl_read(struct file *filp, char __user *ubuf,
2343 size_t cnt, loff_t *ppos)
2344{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002345 char buf[64];
2346 int r;
2347
Steven Rostedt90369902008-11-05 16:05:44 -05002348 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002349 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002350}
2351
2352static ssize_t
2353tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2354 size_t cnt, loff_t *ppos)
2355{
2356 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002357 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002358 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002359 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002360
Steven Rostedtcffae432008-05-12 21:21:00 +02002361 if (cnt >= sizeof(buf))
2362 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363
2364 if (copy_from_user(&buf, ubuf, cnt))
2365 return -EFAULT;
2366
2367 buf[cnt] = 0;
2368
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002369 ret = strict_strtoul(buf, 10, &val);
2370 if (ret < 0)
2371 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002372
2373 val = !!val;
2374
2375 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002376 if (tracer_enabled ^ val) {
2377 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002378 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002379 if (current_trace->start)
2380 current_trace->start(tr);
2381 tracing_start();
2382 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002383 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002384 tracing_stop();
2385 if (current_trace->stop)
2386 current_trace->stop(tr);
2387 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002388 }
2389 mutex_unlock(&trace_types_lock);
2390
2391 filp->f_pos += cnt;
2392
2393 return cnt;
2394}
2395
2396static ssize_t
2397tracing_set_trace_read(struct file *filp, char __user *ubuf,
2398 size_t cnt, loff_t *ppos)
2399{
2400 char buf[max_tracer_type_len+2];
2401 int r;
2402
2403 mutex_lock(&trace_types_lock);
2404 if (current_trace)
2405 r = sprintf(buf, "%s\n", current_trace->name);
2406 else
2407 r = sprintf(buf, "\n");
2408 mutex_unlock(&trace_types_lock);
2409
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002410 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002411}
2412
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002413int tracer_init(struct tracer *t, struct trace_array *tr)
2414{
2415 tracing_reset_online_cpus(tr);
2416 return t->init(tr);
2417}
2418
Steven Rostedt73c51622009-03-11 13:42:01 -04002419static int tracing_resize_ring_buffer(unsigned long size)
2420{
2421 int ret;
2422
2423 /*
2424 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002425 * we use the size that was given, and we can forget about
2426 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002427 */
2428 ring_buffer_expanded = 1;
2429
2430 ret = ring_buffer_resize(global_trace.buffer, size);
2431 if (ret < 0)
2432 return ret;
2433
2434 ret = ring_buffer_resize(max_tr.buffer, size);
2435 if (ret < 0) {
2436 int r;
2437
2438 r = ring_buffer_resize(global_trace.buffer,
2439 global_trace.entries);
2440 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002441 /*
2442 * AARGH! We are left with different
2443 * size max buffer!!!!
2444 * The max buffer is our "snapshot" buffer.
2445 * When a tracer needs a snapshot (one of the
2446 * latency tracers), it swaps the max buffer
2447 * with the saved snap shot. We succeeded to
2448 * update the size of the main buffer, but failed to
2449 * update the size of the max buffer. But when we tried
2450 * to reset the main buffer to the original size, we
2451 * failed there too. This is very unlikely to
2452 * happen, but if it does, warn and kill all
2453 * tracing.
2454 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002455 WARN_ON(1);
2456 tracing_disabled = 1;
2457 }
2458 return ret;
2459 }
2460
2461 global_trace.entries = size;
2462
2463 return ret;
2464}
2465
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002466/**
2467 * tracing_update_buffers - used by tracing facility to expand ring buffers
2468 *
2469 * To save on memory when the tracing is never used on a system with it
2470 * configured in. The ring buffers are set to a minimum size. But once
2471 * a user starts to use the tracing facility, then they need to grow
2472 * to their default size.
2473 *
2474 * This function is to be called when a tracer is about to be used.
2475 */
2476int tracing_update_buffers(void)
2477{
2478 int ret = 0;
2479
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002480 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002481 if (!ring_buffer_expanded)
2482 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002483 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002484
2485 return ret;
2486}
2487
Steven Rostedt577b7852009-02-26 23:43:05 -05002488struct trace_option_dentry;
2489
2490static struct trace_option_dentry *
2491create_trace_option_files(struct tracer *tracer);
2492
2493static void
2494destroy_trace_option_files(struct trace_option_dentry *topts);
2495
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002496static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002497{
Steven Rostedt577b7852009-02-26 23:43:05 -05002498 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002499 struct trace_array *tr = &global_trace;
2500 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002501 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002502
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002503 mutex_lock(&trace_types_lock);
2504
Steven Rostedt73c51622009-03-11 13:42:01 -04002505 if (!ring_buffer_expanded) {
2506 ret = tracing_resize_ring_buffer(trace_buf_size);
2507 if (ret < 0)
2508 return ret;
2509 ret = 0;
2510 }
2511
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002512 for (t = trace_types; t; t = t->next) {
2513 if (strcmp(t->name, buf) == 0)
2514 break;
2515 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002516 if (!t) {
2517 ret = -EINVAL;
2518 goto out;
2519 }
2520 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002521 goto out;
2522
Steven Rostedt9f029e82008-11-12 15:24:24 -05002523 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002524 if (current_trace && current_trace->reset)
2525 current_trace->reset(tr);
2526
Steven Rostedt577b7852009-02-26 23:43:05 -05002527 destroy_trace_option_files(topts);
2528
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002529 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002530
2531 topts = create_trace_option_files(current_trace);
2532
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002533 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002534 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002535 if (ret)
2536 goto out;
2537 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002538
Steven Rostedt9f029e82008-11-12 15:24:24 -05002539 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002540 out:
2541 mutex_unlock(&trace_types_lock);
2542
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002543 return ret;
2544}
2545
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002546static ssize_t
2547tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2548 size_t cnt, loff_t *ppos)
2549{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002550 char buf[max_tracer_type_len+1];
2551 int i;
2552 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002553 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002554
Steven Rostedt60063a62008-10-28 10:44:24 -04002555 ret = cnt;
2556
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002557 if (cnt > max_tracer_type_len)
2558 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002559
2560 if (copy_from_user(&buf, ubuf, cnt))
2561 return -EFAULT;
2562
2563 buf[cnt] = 0;
2564
2565 /* strip ending whitespace. */
2566 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2567 buf[i] = 0;
2568
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002569 err = tracing_set_tracer(buf);
2570 if (err)
2571 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002572
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002573 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002574
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002575 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002576}
2577
2578static ssize_t
2579tracing_max_lat_read(struct file *filp, char __user *ubuf,
2580 size_t cnt, loff_t *ppos)
2581{
2582 unsigned long *ptr = filp->private_data;
2583 char buf[64];
2584 int r;
2585
Steven Rostedtcffae432008-05-12 21:21:00 +02002586 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002587 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002588 if (r > sizeof(buf))
2589 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002590 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002591}
2592
2593static ssize_t
2594tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2595 size_t cnt, loff_t *ppos)
2596{
Hannes Eder5e398412009-02-10 19:44:34 +01002597 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002598 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002599 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002600 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002601
Steven Rostedtcffae432008-05-12 21:21:00 +02002602 if (cnt >= sizeof(buf))
2603 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002604
2605 if (copy_from_user(&buf, ubuf, cnt))
2606 return -EFAULT;
2607
2608 buf[cnt] = 0;
2609
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002610 ret = strict_strtoul(buf, 10, &val);
2611 if (ret < 0)
2612 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002613
2614 *ptr = val * 1000;
2615
2616 return cnt;
2617}
2618
Steven Rostedtb3806b42008-05-12 21:20:46 +02002619static int tracing_open_pipe(struct inode *inode, struct file *filp)
2620{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002621 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002622 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002623 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002624
2625 if (tracing_disabled)
2626 return -ENODEV;
2627
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002628 mutex_lock(&trace_types_lock);
2629
2630 /* We only allow one reader per cpu */
2631 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2632 if (!cpumask_empty(tracing_reader_cpumask)) {
2633 ret = -EBUSY;
2634 goto out;
2635 }
2636 cpumask_setall(tracing_reader_cpumask);
2637 } else {
2638 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2639 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2640 else {
2641 ret = -EBUSY;
2642 goto out;
2643 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002644 }
2645
2646 /* create a buffer to store the information to pass to userspace */
2647 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002648 if (!iter) {
2649 ret = -ENOMEM;
2650 goto out;
2651 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002652
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002653 /*
2654 * We make a copy of the current tracer to avoid concurrent
2655 * changes on it while we are reading.
2656 */
2657 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2658 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002659 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002660 goto fail;
2661 }
2662 if (current_trace)
2663 *iter->trace = *current_trace;
2664
2665 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2666 ret = -ENOMEM;
2667 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302668 }
2669
Steven Rostedta3097202008-11-07 22:36:02 -05002670 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302671 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002672
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002673 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002674 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002675 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002676 filp->private_data = iter;
2677
Steven Rostedt107bad82008-05-12 21:21:01 +02002678 if (iter->trace->pipe_open)
2679 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002680
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002681out:
2682 mutex_unlock(&trace_types_lock);
2683 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002684
2685fail:
2686 kfree(iter->trace);
2687 kfree(iter);
2688 mutex_unlock(&trace_types_lock);
2689 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002690}
2691
2692static int tracing_release_pipe(struct inode *inode, struct file *file)
2693{
2694 struct trace_iterator *iter = file->private_data;
2695
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002696 mutex_lock(&trace_types_lock);
2697
2698 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2699 cpumask_clear(tracing_reader_cpumask);
2700 else
2701 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2702
2703 mutex_unlock(&trace_types_lock);
2704
Rusty Russell44623442009-01-01 10:12:23 +10302705 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002706 mutex_destroy(&iter->mutex);
2707 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002708 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002709
2710 return 0;
2711}
2712
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002713static unsigned int
2714tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2715{
2716 struct trace_iterator *iter = filp->private_data;
2717
2718 if (trace_flags & TRACE_ITER_BLOCK) {
2719 /*
2720 * Always select as readable when in blocking mode
2721 */
2722 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002723 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002724 if (!trace_empty(iter))
2725 return POLLIN | POLLRDNORM;
2726 poll_wait(filp, &trace_wait, poll_table);
2727 if (!trace_empty(iter))
2728 return POLLIN | POLLRDNORM;
2729
2730 return 0;
2731 }
2732}
2733
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002734
2735void default_wait_pipe(struct trace_iterator *iter)
2736{
2737 DEFINE_WAIT(wait);
2738
2739 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2740
2741 if (trace_empty(iter))
2742 schedule();
2743
2744 finish_wait(&trace_wait, &wait);
2745}
2746
2747/*
2748 * This is a make-shift waitqueue.
2749 * A tracer might use this callback on some rare cases:
2750 *
2751 * 1) the current tracer might hold the runqueue lock when it wakes up
2752 * a reader, hence a deadlock (sched, function, and function graph tracers)
2753 * 2) the function tracers, trace all functions, we don't want
2754 * the overhead of calling wake_up and friends
2755 * (and tracing them too)
2756 *
2757 * Anyway, this is really very primitive wakeup.
2758 */
2759void poll_wait_pipe(struct trace_iterator *iter)
2760{
2761 set_current_state(TASK_INTERRUPTIBLE);
2762 /* sleep for 100 msecs, and try again. */
2763 schedule_timeout(HZ / 10);
2764}
2765
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002766/* Must be called with trace_types_lock mutex held. */
2767static int tracing_wait_pipe(struct file *filp)
2768{
2769 struct trace_iterator *iter = filp->private_data;
2770
2771 while (trace_empty(iter)) {
2772
2773 if ((filp->f_flags & O_NONBLOCK)) {
2774 return -EAGAIN;
2775 }
2776
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002777 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002778
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002779 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002780
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002781 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002782
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002783 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002784 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002785
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002786 /*
2787 * We block until we read something and tracing is disabled.
2788 * We still block if tracing is disabled, but we have never
2789 * read anything. This allows a user to cat this file, and
2790 * then enable tracing. But after we have read something,
2791 * we give an EOF when tracing is again disabled.
2792 *
2793 * iter->pos will be 0 if we haven't read anything.
2794 */
2795 if (!tracer_enabled && iter->pos)
2796 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002797 }
2798
2799 return 1;
2800}
2801
Steven Rostedtb3806b42008-05-12 21:20:46 +02002802/*
2803 * Consumer reader.
2804 */
2805static ssize_t
2806tracing_read_pipe(struct file *filp, char __user *ubuf,
2807 size_t cnt, loff_t *ppos)
2808{
2809 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002810 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002811 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002812
2813 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002814 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2815 if (sret != -EBUSY)
2816 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002817
Steven Rostedtf9520752009-03-02 14:04:40 -05002818 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002819
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002820 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002821 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002822 if (unlikely(old_tracer != current_trace && current_trace)) {
2823 old_tracer = current_trace;
2824 *iter->trace = *current_trace;
2825 }
2826 mutex_unlock(&trace_types_lock);
2827
2828 /*
2829 * Avoid more than one consumer on a single file descriptor
2830 * This is just a matter of traces coherency, the ring buffer itself
2831 * is protected.
2832 */
2833 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002834 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002835 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2836 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002837 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002838 }
2839
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002840waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002841 sret = tracing_wait_pipe(filp);
2842 if (sret <= 0)
2843 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002844
2845 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002846 if (trace_empty(iter)) {
2847 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002848 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002849 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002850
2851 if (cnt >= PAGE_SIZE)
2852 cnt = PAGE_SIZE - 1;
2853
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002854 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002855 memset(&iter->seq, 0,
2856 sizeof(struct trace_iterator) -
2857 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002858 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002859
Steven Rostedt088b1e422008-05-12 21:20:48 +02002860 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002861 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002862 int len = iter->seq.len;
2863
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002864 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002865 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002866 /* don't print partial lines */
2867 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002868 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002869 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002870 if (ret != TRACE_TYPE_NO_CONSUME)
2871 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002872
2873 if (iter->seq.len >= cnt)
2874 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002875 }
2876
Steven Rostedtb3806b42008-05-12 21:20:46 +02002877 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002878 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2879 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002880 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002881
2882 /*
2883 * If there was nothing to send to user, inspite of consuming trace
2884 * entries, go back to wait for more entries.
2885 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002886 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002887 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002888
Steven Rostedt107bad82008-05-12 21:21:01 +02002889out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002890 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002891
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002892 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002893}
2894
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002895static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2896 struct pipe_buffer *buf)
2897{
2898 __free_page(buf->page);
2899}
2900
2901static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2902 unsigned int idx)
2903{
2904 __free_page(spd->pages[idx]);
2905}
2906
2907static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002908 .can_merge = 0,
2909 .map = generic_pipe_buf_map,
2910 .unmap = generic_pipe_buf_unmap,
2911 .confirm = generic_pipe_buf_confirm,
2912 .release = tracing_pipe_buf_release,
2913 .steal = generic_pipe_buf_steal,
2914 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002915};
2916
Steven Rostedt34cd4992009-02-09 12:06:29 -05002917static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002918tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002919{
2920 size_t count;
2921 int ret;
2922
2923 /* Seq buffer is page-sized, exactly what we need. */
2924 for (;;) {
2925 count = iter->seq.len;
2926 ret = print_trace_line(iter);
2927 count = iter->seq.len - count;
2928 if (rem < count) {
2929 rem = 0;
2930 iter->seq.len -= count;
2931 break;
2932 }
2933 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2934 iter->seq.len -= count;
2935 break;
2936 }
2937
2938 trace_consume(iter);
2939 rem -= count;
2940 if (!find_next_entry_inc(iter)) {
2941 rem = 0;
2942 iter->ent = NULL;
2943 break;
2944 }
2945 }
2946
2947 return rem;
2948}
2949
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002950static ssize_t tracing_splice_read_pipe(struct file *filp,
2951 loff_t *ppos,
2952 struct pipe_inode_info *pipe,
2953 size_t len,
2954 unsigned int flags)
2955{
2956 struct page *pages[PIPE_BUFFERS];
2957 struct partial_page partial[PIPE_BUFFERS];
2958 struct trace_iterator *iter = filp->private_data;
2959 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002960 .pages = pages,
2961 .partial = partial,
2962 .nr_pages = 0, /* This gets updated below. */
2963 .flags = flags,
2964 .ops = &tracing_pipe_buf_ops,
2965 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002966 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002967 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002968 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002969 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002970 unsigned int i;
2971
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002972 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002973 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002974 if (unlikely(old_tracer != current_trace && current_trace)) {
2975 old_tracer = current_trace;
2976 *iter->trace = *current_trace;
2977 }
2978 mutex_unlock(&trace_types_lock);
2979
2980 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002981
2982 if (iter->trace->splice_read) {
2983 ret = iter->trace->splice_read(iter, filp,
2984 ppos, pipe, len, flags);
2985 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002986 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002987 }
2988
2989 ret = tracing_wait_pipe(filp);
2990 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002991 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002992
2993 if (!iter->ent && !find_next_entry_inc(iter)) {
2994 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002995 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002996 }
2997
2998 /* Fill as many pages as possible. */
2999 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3000 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003001 if (!pages[i])
3002 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003003
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003004 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003005
3006 /* Copy the data into the page, so we can start over. */
3007 ret = trace_seq_to_buffer(&iter->seq,
3008 page_address(pages[i]),
3009 iter->seq.len);
3010 if (ret < 0) {
3011 __free_page(pages[i]);
3012 break;
3013 }
3014 partial[i].offset = 0;
3015 partial[i].len = iter->seq.len;
3016
Steven Rostedtf9520752009-03-02 14:04:40 -05003017 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003018 }
3019
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003020 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003021
3022 spd.nr_pages = i;
3023
3024 return splice_to_pipe(pipe, &spd);
3025
Steven Rostedt34cd4992009-02-09 12:06:29 -05003026out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003027 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003028
3029 return ret;
3030}
3031
Steven Rostedta98a3c32008-05-12 21:20:59 +02003032static ssize_t
3033tracing_entries_read(struct file *filp, char __user *ubuf,
3034 size_t cnt, loff_t *ppos)
3035{
3036 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003037 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003038 int r;
3039
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003040 mutex_lock(&trace_types_lock);
3041 if (!ring_buffer_expanded)
3042 r = sprintf(buf, "%lu (expanded: %lu)\n",
3043 tr->entries >> 10,
3044 trace_buf_size >> 10);
3045 else
3046 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3047 mutex_unlock(&trace_types_lock);
3048
Steven Rostedta98a3c32008-05-12 21:20:59 +02003049 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3050}
3051
3052static ssize_t
3053tracing_entries_write(struct file *filp, const char __user *ubuf,
3054 size_t cnt, loff_t *ppos)
3055{
3056 unsigned long val;
3057 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003058 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003059
Steven Rostedtcffae432008-05-12 21:21:00 +02003060 if (cnt >= sizeof(buf))
3061 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003062
3063 if (copy_from_user(&buf, ubuf, cnt))
3064 return -EFAULT;
3065
3066 buf[cnt] = 0;
3067
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003068 ret = strict_strtoul(buf, 10, &val);
3069 if (ret < 0)
3070 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003071
3072 /* must have at least 1 entry */
3073 if (!val)
3074 return -EINVAL;
3075
3076 mutex_lock(&trace_types_lock);
3077
Steven Rostedtc76f0692008-11-07 22:36:02 -05003078 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003079
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003080 /* disable all cpu buffers */
3081 for_each_tracing_cpu(cpu) {
3082 if (global_trace.data[cpu])
3083 atomic_inc(&global_trace.data[cpu]->disabled);
3084 if (max_tr.data[cpu])
3085 atomic_inc(&max_tr.data[cpu]->disabled);
3086 }
3087
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003088 /* value is in KB */
3089 val <<= 10;
3090
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003091 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003092 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003093 if (ret < 0) {
3094 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003095 goto out;
3096 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003097 }
3098
3099 filp->f_pos += cnt;
3100
Steven Rostedt19384c02008-05-22 00:22:16 -04003101 /* If check pages failed, return ENOMEM */
3102 if (tracing_disabled)
3103 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003104 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003105 for_each_tracing_cpu(cpu) {
3106 if (global_trace.data[cpu])
3107 atomic_dec(&global_trace.data[cpu]->disabled);
3108 if (max_tr.data[cpu])
3109 atomic_dec(&max_tr.data[cpu]->disabled);
3110 }
3111
Steven Rostedtc76f0692008-11-07 22:36:02 -05003112 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003113 max_tr.entries = global_trace.entries;
3114 mutex_unlock(&trace_types_lock);
3115
3116 return cnt;
3117}
3118
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003119static int mark_printk(const char *fmt, ...)
3120{
3121 int ret;
3122 va_list args;
3123 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003124 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003125 va_end(args);
3126 return ret;
3127}
3128
3129static ssize_t
3130tracing_mark_write(struct file *filp, const char __user *ubuf,
3131 size_t cnt, loff_t *fpos)
3132{
3133 char *buf;
3134 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003135
Steven Rostedtc76f0692008-11-07 22:36:02 -05003136 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003137 return -EINVAL;
3138
3139 if (cnt > TRACE_BUF_SIZE)
3140 cnt = TRACE_BUF_SIZE;
3141
3142 buf = kmalloc(cnt + 1, GFP_KERNEL);
3143 if (buf == NULL)
3144 return -ENOMEM;
3145
3146 if (copy_from_user(buf, ubuf, cnt)) {
3147 kfree(buf);
3148 return -EFAULT;
3149 }
3150
3151 /* Cut from the first nil or newline. */
3152 buf[cnt] = '\0';
3153 end = strchr(buf, '\n');
3154 if (end)
3155 *end = '\0';
3156
3157 cnt = mark_printk("%s\n", buf);
3158 kfree(buf);
3159 *fpos += cnt;
3160
3161 return cnt;
3162}
3163
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003164static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003165 .open = tracing_open_generic,
3166 .read = tracing_max_lat_read,
3167 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003168};
3169
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003170static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003171 .open = tracing_open_generic,
3172 .read = tracing_ctrl_read,
3173 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003174};
3175
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003176static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003177 .open = tracing_open_generic,
3178 .read = tracing_set_trace_read,
3179 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003180};
3181
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003182static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003183 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003184 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003185 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003186 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003187 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003188};
3189
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003190static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003191 .open = tracing_open_generic,
3192 .read = tracing_entries_read,
3193 .write = tracing_entries_write,
3194};
3195
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003196static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003197 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003198 .write = tracing_mark_write,
3199};
3200
Steven Rostedt2cadf912008-12-01 22:20:19 -05003201struct ftrace_buffer_info {
3202 struct trace_array *tr;
3203 void *spare;
3204 int cpu;
3205 unsigned int read;
3206};
3207
3208static int tracing_buffers_open(struct inode *inode, struct file *filp)
3209{
3210 int cpu = (int)(long)inode->i_private;
3211 struct ftrace_buffer_info *info;
3212
3213 if (tracing_disabled)
3214 return -ENODEV;
3215
3216 info = kzalloc(sizeof(*info), GFP_KERNEL);
3217 if (!info)
3218 return -ENOMEM;
3219
3220 info->tr = &global_trace;
3221 info->cpu = cpu;
3222 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3223 /* Force reading ring buffer for first read */
3224 info->read = (unsigned int)-1;
3225 if (!info->spare)
3226 goto out;
3227
3228 filp->private_data = info;
3229
3230 return 0;
3231
3232 out:
3233 kfree(info);
3234 return -ENOMEM;
3235}
3236
3237static ssize_t
3238tracing_buffers_read(struct file *filp, char __user *ubuf,
3239 size_t count, loff_t *ppos)
3240{
3241 struct ftrace_buffer_info *info = filp->private_data;
3242 unsigned int pos;
3243 ssize_t ret;
3244 size_t size;
3245
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003246 if (!count)
3247 return 0;
3248
Steven Rostedt2cadf912008-12-01 22:20:19 -05003249 /* Do we have previous read data to read? */
3250 if (info->read < PAGE_SIZE)
3251 goto read;
3252
3253 info->read = 0;
3254
3255 ret = ring_buffer_read_page(info->tr->buffer,
3256 &info->spare,
3257 count,
3258 info->cpu, 0);
3259 if (ret < 0)
3260 return 0;
3261
3262 pos = ring_buffer_page_len(info->spare);
3263
3264 if (pos < PAGE_SIZE)
3265 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3266
3267read:
3268 size = PAGE_SIZE - info->read;
3269 if (size > count)
3270 size = count;
3271
3272 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003273 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003274 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003275 size -= ret;
3276
Steven Rostedt2cadf912008-12-01 22:20:19 -05003277 *ppos += size;
3278 info->read += size;
3279
3280 return size;
3281}
3282
3283static int tracing_buffers_release(struct inode *inode, struct file *file)
3284{
3285 struct ftrace_buffer_info *info = file->private_data;
3286
3287 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3288 kfree(info);
3289
3290 return 0;
3291}
3292
3293struct buffer_ref {
3294 struct ring_buffer *buffer;
3295 void *page;
3296 int ref;
3297};
3298
3299static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3300 struct pipe_buffer *buf)
3301{
3302 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3303
3304 if (--ref->ref)
3305 return;
3306
3307 ring_buffer_free_read_page(ref->buffer, ref->page);
3308 kfree(ref);
3309 buf->private = 0;
3310}
3311
3312static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3313 struct pipe_buffer *buf)
3314{
3315 return 1;
3316}
3317
3318static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3319 struct pipe_buffer *buf)
3320{
3321 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3322
3323 ref->ref++;
3324}
3325
3326/* Pipe buffer operations for a buffer. */
3327static struct pipe_buf_operations buffer_pipe_buf_ops = {
3328 .can_merge = 0,
3329 .map = generic_pipe_buf_map,
3330 .unmap = generic_pipe_buf_unmap,
3331 .confirm = generic_pipe_buf_confirm,
3332 .release = buffer_pipe_buf_release,
3333 .steal = buffer_pipe_buf_steal,
3334 .get = buffer_pipe_buf_get,
3335};
3336
3337/*
3338 * Callback from splice_to_pipe(), if we need to release some pages
3339 * at the end of the spd in case we error'ed out in filling the pipe.
3340 */
3341static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3342{
3343 struct buffer_ref *ref =
3344 (struct buffer_ref *)spd->partial[i].private;
3345
3346 if (--ref->ref)
3347 return;
3348
3349 ring_buffer_free_read_page(ref->buffer, ref->page);
3350 kfree(ref);
3351 spd->partial[i].private = 0;
3352}
3353
3354static ssize_t
3355tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3356 struct pipe_inode_info *pipe, size_t len,
3357 unsigned int flags)
3358{
3359 struct ftrace_buffer_info *info = file->private_data;
3360 struct partial_page partial[PIPE_BUFFERS];
3361 struct page *pages[PIPE_BUFFERS];
3362 struct splice_pipe_desc spd = {
3363 .pages = pages,
3364 .partial = partial,
3365 .flags = flags,
3366 .ops = &buffer_pipe_buf_ops,
3367 .spd_release = buffer_spd_release,
3368 };
3369 struct buffer_ref *ref;
3370 int size, i;
3371 size_t ret;
3372
3373 /*
3374 * We can't seek on a buffer input
3375 */
3376 if (unlikely(*ppos))
3377 return -ESPIPE;
3378
3379
3380 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3381 struct page *page;
3382 int r;
3383
3384 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3385 if (!ref)
3386 break;
3387
3388 ref->buffer = info->tr->buffer;
3389 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3390 if (!ref->page) {
3391 kfree(ref);
3392 break;
3393 }
3394
3395 r = ring_buffer_read_page(ref->buffer, &ref->page,
3396 len, info->cpu, 0);
3397 if (r < 0) {
3398 ring_buffer_free_read_page(ref->buffer,
3399 ref->page);
3400 kfree(ref);
3401 break;
3402 }
3403
3404 /*
3405 * zero out any left over data, this is going to
3406 * user land.
3407 */
3408 size = ring_buffer_page_len(ref->page);
3409 if (size < PAGE_SIZE)
3410 memset(ref->page + size, 0, PAGE_SIZE - size);
3411
3412 page = virt_to_page(ref->page);
3413
3414 spd.pages[i] = page;
3415 spd.partial[i].len = PAGE_SIZE;
3416 spd.partial[i].offset = 0;
3417 spd.partial[i].private = (unsigned long)ref;
3418 spd.nr_pages++;
3419 }
3420
3421 spd.nr_pages = i;
3422
3423 /* did we read anything? */
3424 if (!spd.nr_pages) {
3425 if (flags & SPLICE_F_NONBLOCK)
3426 ret = -EAGAIN;
3427 else
3428 ret = 0;
3429 /* TODO: block */
3430 return ret;
3431 }
3432
3433 ret = splice_to_pipe(pipe, &spd);
3434
3435 return ret;
3436}
3437
3438static const struct file_operations tracing_buffers_fops = {
3439 .open = tracing_buffers_open,
3440 .read = tracing_buffers_read,
3441 .release = tracing_buffers_release,
3442 .splice_read = tracing_buffers_splice_read,
3443 .llseek = no_llseek,
3444};
3445
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003446#ifdef CONFIG_DYNAMIC_FTRACE
3447
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003448int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003449{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003450 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003451}
3452
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003453static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003454tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003455 size_t cnt, loff_t *ppos)
3456{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003457 static char ftrace_dyn_info_buffer[1024];
3458 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003459 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003460 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003461 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003462 int r;
3463
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003464 mutex_lock(&dyn_info_mutex);
3465 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003466
Steven Rostedta26a2a22008-10-31 00:03:22 -04003467 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003468 buf[r++] = '\n';
3469
3470 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3471
3472 mutex_unlock(&dyn_info_mutex);
3473
3474 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003475}
3476
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003477static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003478 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003479 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003480};
3481#endif
3482
3483static struct dentry *d_tracer;
3484
3485struct dentry *tracing_init_dentry(void)
3486{
3487 static int once;
3488
3489 if (d_tracer)
3490 return d_tracer;
3491
3492 d_tracer = debugfs_create_dir("tracing", NULL);
3493
3494 if (!d_tracer && !once) {
3495 once = 1;
3496 pr_warning("Could not create debugfs directory 'tracing'\n");
3497 return NULL;
3498 }
3499
3500 return d_tracer;
3501}
3502
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003503static struct dentry *d_percpu;
3504
3505struct dentry *tracing_dentry_percpu(void)
3506{
3507 static int once;
3508 struct dentry *d_tracer;
3509
3510 if (d_percpu)
3511 return d_percpu;
3512
3513 d_tracer = tracing_init_dentry();
3514
3515 if (!d_tracer)
3516 return NULL;
3517
3518 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3519
3520 if (!d_percpu && !once) {
3521 once = 1;
3522 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3523 return NULL;
3524 }
3525
3526 return d_percpu;
3527}
3528
3529static void tracing_init_debugfs_percpu(long cpu)
3530{
3531 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003532 struct dentry *entry, *d_cpu;
3533 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3534 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003535
3536 if (cpu > 999 || cpu < 0)
3537 return;
3538
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003539 sprintf(cpu_dir, "cpu%ld", cpu);
3540 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3541 if (!d_cpu) {
3542 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3543 return;
3544 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003545
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003546 /* per cpu trace_pipe */
3547 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003548 (void *) cpu, &tracing_pipe_fops);
3549 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003550 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003551
3552 /* per cpu trace */
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003553 entry = debugfs_create_file("trace", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003554 (void *) cpu, &tracing_fops);
3555 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003556 pr_warning("Could not create debugfs 'trace' entry\n");
Steven Rostedt7f96f932009-03-13 00:37:42 -04003557
3558 entry = debugfs_create_file("trace_pipe_raw", 0444, d_cpu,
3559 (void *) cpu, &tracing_buffers_fops);
3560 if (!entry)
3561 pr_warning("Could not create debugfs 'trace_pipe_raw' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003562}
3563
Steven Rostedt60a11772008-05-12 21:20:44 +02003564#ifdef CONFIG_FTRACE_SELFTEST
3565/* Let selftest have access to static functions in this file */
3566#include "trace_selftest.c"
3567#endif
3568
Steven Rostedt577b7852009-02-26 23:43:05 -05003569struct trace_option_dentry {
3570 struct tracer_opt *opt;
3571 struct tracer_flags *flags;
3572 struct dentry *entry;
3573};
3574
3575static ssize_t
3576trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3577 loff_t *ppos)
3578{
3579 struct trace_option_dentry *topt = filp->private_data;
3580 char *buf;
3581
3582 if (topt->flags->val & topt->opt->bit)
3583 buf = "1\n";
3584 else
3585 buf = "0\n";
3586
3587 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3588}
3589
3590static ssize_t
3591trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3592 loff_t *ppos)
3593{
3594 struct trace_option_dentry *topt = filp->private_data;
3595 unsigned long val;
3596 char buf[64];
3597 int ret;
3598
3599 if (cnt >= sizeof(buf))
3600 return -EINVAL;
3601
3602 if (copy_from_user(&buf, ubuf, cnt))
3603 return -EFAULT;
3604
3605 buf[cnt] = 0;
3606
3607 ret = strict_strtoul(buf, 10, &val);
3608 if (ret < 0)
3609 return ret;
3610
3611 ret = 0;
3612 switch (val) {
3613 case 0:
3614 /* do nothing if already cleared */
3615 if (!(topt->flags->val & topt->opt->bit))
3616 break;
3617
3618 mutex_lock(&trace_types_lock);
3619 if (current_trace->set_flag)
3620 ret = current_trace->set_flag(topt->flags->val,
3621 topt->opt->bit, 0);
3622 mutex_unlock(&trace_types_lock);
3623 if (ret)
3624 return ret;
3625 topt->flags->val &= ~topt->opt->bit;
3626 break;
3627 case 1:
3628 /* do nothing if already set */
3629 if (topt->flags->val & topt->opt->bit)
3630 break;
3631
3632 mutex_lock(&trace_types_lock);
3633 if (current_trace->set_flag)
3634 ret = current_trace->set_flag(topt->flags->val,
3635 topt->opt->bit, 1);
3636 mutex_unlock(&trace_types_lock);
3637 if (ret)
3638 return ret;
3639 topt->flags->val |= topt->opt->bit;
3640 break;
3641
3642 default:
3643 return -EINVAL;
3644 }
3645
3646 *ppos += cnt;
3647
3648 return cnt;
3649}
3650
3651
3652static const struct file_operations trace_options_fops = {
3653 .open = tracing_open_generic,
3654 .read = trace_options_read,
3655 .write = trace_options_write,
3656};
3657
Steven Rostedta8259072009-02-26 22:19:12 -05003658static ssize_t
3659trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3660 loff_t *ppos)
3661{
3662 long index = (long)filp->private_data;
3663 char *buf;
3664
3665 if (trace_flags & (1 << index))
3666 buf = "1\n";
3667 else
3668 buf = "0\n";
3669
3670 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3671}
3672
3673static ssize_t
3674trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3675 loff_t *ppos)
3676{
3677 long index = (long)filp->private_data;
3678 char buf[64];
3679 unsigned long val;
3680 int ret;
3681
3682 if (cnt >= sizeof(buf))
3683 return -EINVAL;
3684
3685 if (copy_from_user(&buf, ubuf, cnt))
3686 return -EFAULT;
3687
3688 buf[cnt] = 0;
3689
3690 ret = strict_strtoul(buf, 10, &val);
3691 if (ret < 0)
3692 return ret;
3693
3694 switch (val) {
3695 case 0:
3696 trace_flags &= ~(1 << index);
3697 break;
3698 case 1:
3699 trace_flags |= 1 << index;
3700 break;
3701
3702 default:
3703 return -EINVAL;
3704 }
3705
3706 *ppos += cnt;
3707
3708 return cnt;
3709}
3710
Steven Rostedta8259072009-02-26 22:19:12 -05003711static const struct file_operations trace_options_core_fops = {
3712 .open = tracing_open_generic,
3713 .read = trace_options_core_read,
3714 .write = trace_options_core_write,
3715};
3716
3717static struct dentry *trace_options_init_dentry(void)
3718{
3719 struct dentry *d_tracer;
3720 static struct dentry *t_options;
3721
3722 if (t_options)
3723 return t_options;
3724
3725 d_tracer = tracing_init_dentry();
3726 if (!d_tracer)
3727 return NULL;
3728
3729 t_options = debugfs_create_dir("options", d_tracer);
3730 if (!t_options) {
3731 pr_warning("Could not create debugfs directory 'options'\n");
3732 return NULL;
3733 }
3734
3735 return t_options;
3736}
3737
Steven Rostedt577b7852009-02-26 23:43:05 -05003738static void
3739create_trace_option_file(struct trace_option_dentry *topt,
3740 struct tracer_flags *flags,
3741 struct tracer_opt *opt)
3742{
3743 struct dentry *t_options;
3744 struct dentry *entry;
3745
3746 t_options = trace_options_init_dentry();
3747 if (!t_options)
3748 return;
3749
3750 topt->flags = flags;
3751 topt->opt = opt;
3752
3753 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3754 &trace_options_fops);
3755
3756 topt->entry = entry;
3757
3758}
3759
3760static struct trace_option_dentry *
3761create_trace_option_files(struct tracer *tracer)
3762{
3763 struct trace_option_dentry *topts;
3764 struct tracer_flags *flags;
3765 struct tracer_opt *opts;
3766 int cnt;
3767
3768 if (!tracer)
3769 return NULL;
3770
3771 flags = tracer->flags;
3772
3773 if (!flags || !flags->opts)
3774 return NULL;
3775
3776 opts = flags->opts;
3777
3778 for (cnt = 0; opts[cnt].name; cnt++)
3779 ;
3780
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003781 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003782 if (!topts)
3783 return NULL;
3784
3785 for (cnt = 0; opts[cnt].name; cnt++)
3786 create_trace_option_file(&topts[cnt], flags,
3787 &opts[cnt]);
3788
3789 return topts;
3790}
3791
3792static void
3793destroy_trace_option_files(struct trace_option_dentry *topts)
3794{
3795 int cnt;
3796
3797 if (!topts)
3798 return;
3799
3800 for (cnt = 0; topts[cnt].opt; cnt++) {
3801 if (topts[cnt].entry)
3802 debugfs_remove(topts[cnt].entry);
3803 }
3804
3805 kfree(topts);
3806}
3807
Steven Rostedta8259072009-02-26 22:19:12 -05003808static struct dentry *
3809create_trace_option_core_file(const char *option, long index)
3810{
3811 struct dentry *t_options;
3812 struct dentry *entry;
3813
3814 t_options = trace_options_init_dentry();
3815 if (!t_options)
3816 return NULL;
3817
3818 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3819 &trace_options_core_fops);
3820
3821 return entry;
3822}
3823
3824static __init void create_trace_options_dir(void)
3825{
3826 struct dentry *t_options;
3827 struct dentry *entry;
3828 int i;
3829
3830 t_options = trace_options_init_dentry();
3831 if (!t_options)
3832 return;
3833
3834 for (i = 0; trace_options[i]; i++) {
3835 entry = create_trace_option_core_file(trace_options[i], i);
3836 if (!entry)
3837 pr_warning("Could not create debugfs %s entry\n",
3838 trace_options[i]);
3839 }
3840}
3841
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003842static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003843{
3844 struct dentry *d_tracer;
3845 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003846 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003847
3848 d_tracer = tracing_init_dentry();
3849
3850 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3851 &global_trace, &tracing_ctrl_fops);
3852 if (!entry)
3853 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3854
Steven Rostedtee6bce52008-11-12 17:52:37 -05003855 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003856 NULL, &tracing_iter_fops);
3857 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003858 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003859
Steven Rostedta8259072009-02-26 22:19:12 -05003860 create_trace_options_dir();
3861
Ingo Molnarc7078de2008-05-12 21:20:52 +02003862 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3863 NULL, &tracing_cpumask_fops);
3864 if (!entry)
3865 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3866
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003867 entry = debugfs_create_file("trace", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003868 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003869 if (!entry)
3870 pr_warning("Could not create debugfs 'trace' entry\n");
3871
3872 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3873 &global_trace, &show_traces_fops);
3874 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003875 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003876
3877 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3878 &global_trace, &set_tracer_fops);
3879 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003880 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003881
3882 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3883 &tracing_max_latency,
3884 &tracing_max_lat_fops);
3885 if (!entry)
3886 pr_warning("Could not create debugfs "
3887 "'tracing_max_latency' entry\n");
3888
3889 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3890 &tracing_thresh, &tracing_max_lat_fops);
3891 if (!entry)
3892 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003893 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003894 entry = debugfs_create_file("README", 0644, d_tracer,
3895 NULL, &tracing_readme_fops);
3896 if (!entry)
3897 pr_warning("Could not create debugfs 'README' entry\n");
3898
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003899 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3900 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003901 if (!entry)
3902 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003903 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003904
Steven Rostedta94c80e2008-11-12 17:52:36 -05003905 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003906 &global_trace, &tracing_entries_fops);
3907 if (!entry)
3908 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003909 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003910
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003911 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3912 NULL, &tracing_mark_fops);
3913 if (!entry)
3914 pr_warning("Could not create debugfs "
3915 "'trace_marker' entry\n");
3916
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003917#ifdef CONFIG_DYNAMIC_FTRACE
3918 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3919 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003920 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003921 if (!entry)
3922 pr_warning("Could not create debugfs "
3923 "'dyn_ftrace_total_info' entry\n");
3924#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003925#ifdef CONFIG_SYSPROF_TRACER
3926 init_tracer_sysprof_debugfs(d_tracer);
3927#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003928
3929 for_each_tracing_cpu(cpu)
3930 tracing_init_debugfs_percpu(cpu);
3931
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003932 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003933}
3934
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003935static int trace_panic_handler(struct notifier_block *this,
3936 unsigned long event, void *unused)
3937{
Steven Rostedt944ac422008-10-23 19:26:08 -04003938 if (ftrace_dump_on_oops)
3939 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003940 return NOTIFY_OK;
3941}
3942
3943static struct notifier_block trace_panic_notifier = {
3944 .notifier_call = trace_panic_handler,
3945 .next = NULL,
3946 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3947};
3948
3949static int trace_die_handler(struct notifier_block *self,
3950 unsigned long val,
3951 void *data)
3952{
3953 switch (val) {
3954 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003955 if (ftrace_dump_on_oops)
3956 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003957 break;
3958 default:
3959 break;
3960 }
3961 return NOTIFY_OK;
3962}
3963
3964static struct notifier_block trace_die_notifier = {
3965 .notifier_call = trace_die_handler,
3966 .priority = 200
3967};
3968
3969/*
3970 * printk is set to max of 1024, we really don't need it that big.
3971 * Nothing should be printing 1000 characters anyway.
3972 */
3973#define TRACE_MAX_PRINT 1000
3974
3975/*
3976 * Define here KERN_TRACE so that we have one place to modify
3977 * it if we decide to change what log level the ftrace dump
3978 * should be at.
3979 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003980#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003981
3982static void
3983trace_printk_seq(struct trace_seq *s)
3984{
3985 /* Probably should print a warning here. */
3986 if (s->len >= 1000)
3987 s->len = 1000;
3988
3989 /* should be zero ended, but we are paranoid. */
3990 s->buffer[s->len] = 0;
3991
3992 printk(KERN_TRACE "%s", s->buffer);
3993
Steven Rostedtf9520752009-03-02 14:04:40 -05003994 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003995}
3996
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003997void ftrace_dump(void)
3998{
3999 static DEFINE_SPINLOCK(ftrace_dump_lock);
4000 /* use static because iter can be a bit big for the stack */
4001 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004002 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004003 unsigned long flags;
4004 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004005
4006 /* only one dump */
4007 spin_lock_irqsave(&ftrace_dump_lock, flags);
4008 if (dump_ran)
4009 goto out;
4010
4011 dump_ran = 1;
4012
4013 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004014 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004015 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004016
Steven Rostedtd7690412008-10-01 00:29:53 -04004017 for_each_tracing_cpu(cpu) {
4018 atomic_inc(&global_trace.data[cpu]->disabled);
4019 }
4020
Török Edwinb54d3de2008-11-22 13:28:48 +02004021 /* don't look at user memory in panic mode */
4022 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4023
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004024 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4025
Steven Rostedte543ad72009-03-04 18:20:36 -05004026 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004027 iter.tr = &global_trace;
4028 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05004029 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004030
4031 /*
4032 * We need to stop all tracing on all CPUS to read the
4033 * the next buffer. This is a bit expensive, but is
4034 * not done often. We fill all what we can read,
4035 * and then release the locks again.
4036 */
4037
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004038 while (!trace_empty(&iter)) {
4039
4040 if (!cnt)
4041 printk(KERN_TRACE "---------------------------------\n");
4042
4043 cnt++;
4044
4045 /* reset all but tr, trace, and overruns */
4046 memset(&iter.seq, 0,
4047 sizeof(struct trace_iterator) -
4048 offsetof(struct trace_iterator, seq));
4049 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4050 iter.pos = -1;
4051
4052 if (find_next_entry_inc(&iter) != NULL) {
4053 print_trace_line(&iter);
4054 trace_consume(&iter);
4055 }
4056
4057 trace_printk_seq(&iter.seq);
4058 }
4059
4060 if (!cnt)
4061 printk(KERN_TRACE " (ftrace buffer empty)\n");
4062 else
4063 printk(KERN_TRACE "---------------------------------\n");
4064
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004065 out:
4066 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4067}
4068
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004069__init static int tracer_alloc_buffers(void)
4070{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004071 struct trace_array_cpu *data;
Steven Rostedt73c51622009-03-11 13:42:01 -04004072 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004073 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304074 int ret = -ENOMEM;
4075
4076 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4077 goto out;
4078
4079 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4080 goto out_free_buffer_mask;
4081
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004082 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4083 goto out_free_tracing_cpumask;
4084
Steven Rostedt73c51622009-03-11 13:42:01 -04004085 /* To save memory, keep the ring buffer size to its minimum */
4086 if (ring_buffer_expanded)
4087 ring_buf_size = trace_buf_size;
4088 else
4089 ring_buf_size = 1;
4090
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304091 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4092 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004093 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004094
Steven Rostedtab464282008-05-12 21:21:00 +02004095 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04004096 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004097 TRACE_BUFFER_FLAGS);
4098 if (!global_trace.buffer) {
4099 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4100 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304101 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004102 }
4103 global_trace.entries = ring_buffer_size(global_trace.buffer);
4104
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304105
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004106#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt73c51622009-03-11 13:42:01 -04004107 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004108 TRACE_BUFFER_FLAGS);
4109 if (!max_tr.buffer) {
4110 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4111 WARN_ON(1);
4112 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304113 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004114 }
4115 max_tr.entries = ring_buffer_size(max_tr.buffer);
4116 WARN_ON(max_tr.entries != global_trace.entries);
4117#endif
4118
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004119 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004120 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004121 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004122 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004123 }
4124
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004125 trace_init_cmdlines();
4126
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004127 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004128 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004129#ifdef CONFIG_BOOT_TRACER
4130 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004131#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004132 /* All seems OK, enable tracing */
4133 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004134
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004135 atomic_notifier_chain_register(&panic_notifier_list,
4136 &trace_panic_notifier);
4137
4138 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304139 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004140
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304141out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004142 free_cpumask_var(tracing_reader_cpumask);
4143out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304144 free_cpumask_var(tracing_cpumask);
4145out_free_buffer_mask:
4146 free_cpumask_var(tracing_buffer_mask);
4147out:
4148 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004149}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004150
4151__init static int clear_boot_tracer(void)
4152{
4153 /*
4154 * The default tracer at boot buffer is an init section.
4155 * This function is called in lateinit. If we did not
4156 * find the boot tracer, then clear it out, to prevent
4157 * later registration from accessing the buffer that is
4158 * about to be freed.
4159 */
4160 if (!default_bootup_tracer)
4161 return 0;
4162
4163 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4164 default_bootup_tracer);
4165 default_bootup_tracer = NULL;
4166
4167 return 0;
4168}
4169
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004170early_initcall(tracer_alloc_buffers);
4171fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004172late_initcall(clear_boot_tracer);