blob: 35ee63ae41224627bee4a5bcd4cee8e4a8c67fa5 [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
Steven Rostedt2cadf912008-12-01 22:20:19 -050014#include <linux/ring_buffer.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020015#include <linux/utsrelease.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050016#include <linux/stacktrace.h>
17#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020018#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040020#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050021#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020022#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020023#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020024#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050027#include <linux/kprobes.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050031#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040032#include <linux/kdebug.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020033#include <linux/ctype.h>
34#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020035#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020036#include <linux/gfp.h>
37#include <linux/fs.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020038
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020039#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050040#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020041
Steven Rostedt3928a8a2008-09-29 23:02:41 -040042#define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43
Steven Rostedt745b1622009-01-15 23:40:11 -050044unsigned long __read_mostly tracing_max_latency;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020045unsigned long __read_mostly tracing_thresh;
46
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010047/*
Steven Rostedt73c51622009-03-11 13:42:01 -040048 * On boot up, the ring buffer is set to the minimum size, so that
49 * we do not waste memory on systems that are not using tracing.
50 */
51static int ring_buffer_expanded;
52
53/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010054 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010055 * A selftest will lurk into the ring-buffer to count the
56 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010057 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010058 * at the same time, giving false positive or negative results.
59 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010060static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010061
Steven Rostedtb2821ae2009-02-02 21:38:32 -050062/*
63 * If a tracer is running, we do not want to run SELFTEST.
64 */
65static bool __read_mostly tracing_selftest_disabled;
66
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010067/* For tracers that don't implement custom flags */
68static struct tracer_opt dummy_tracer_opt[] = {
69 { }
70};
71
72static struct tracer_flags dummy_tracer_flags = {
73 .val = 0,
74 .opts = dummy_tracer_opt
75};
76
77static int dummy_set_flag(u32 old_flags, u32 bit, int set)
78{
79 return 0;
80}
Steven Rostedt0f048702008-11-05 16:05:44 -050081
82/*
83 * Kill all tracing for good (never come back).
84 * It is initialized to 1 but will turn to zero if the initialization
85 * of the tracer is successful. But that is the only place that sets
86 * this back to zero.
87 */
Hannes Eder4fd27352009-02-10 19:44:12 +010088static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050089
Steven Rostedtd7690412008-10-01 00:29:53 -040090static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
91
92static inline void ftrace_disable_cpu(void)
93{
94 preempt_disable();
95 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
96}
97
98static inline void ftrace_enable_cpu(void)
99{
100 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
101 preempt_enable();
102}
103
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030104static cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200105
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +0100106/* Define which cpu buffers are currently read in trace_pipe */
107static cpumask_var_t tracing_reader_cpumask;
108
Steven Rostedtab464282008-05-12 21:21:00 +0200109#define for_each_tracing_cpu(cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030110 for_each_cpu(cpu, tracing_buffer_mask)
Steven Rostedtab464282008-05-12 21:21:00 +0200111
Steven Rostedt944ac422008-10-23 19:26:08 -0400112/*
113 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
114 *
115 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
116 * is set, then ftrace_dump is called. This will output the contents
117 * of the ftrace buffers to the console. This is very useful for
118 * capturing traces that lead to crashes and outputing it to a
119 * serial console.
120 *
121 * It is default off, but you can enable it with either specifying
122 * "ftrace_dump_on_oops" in the kernel command line, or setting
123 * /proc/sys/kernel/ftrace_dump_on_oops to true.
124 */
125int ftrace_dump_on_oops;
126
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500127static int tracing_set_tracer(const char *buf);
128
129#define BOOTUP_TRACER_SIZE 100
130static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
131static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100132
133static int __init set_ftrace(char *str)
134{
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500135 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
136 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400137 /* We are using ftrace early, expand it */
138 ring_buffer_expanded = 1;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100139 return 1;
140}
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500141__setup("ftrace=", set_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100142
Steven Rostedt944ac422008-10-23 19:26:08 -0400143static int __init set_ftrace_dump_on_oops(char *str)
144{
145 ftrace_dump_on_oops = 1;
146 return 1;
147}
148__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200149
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200150long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200151ns2usecs(cycle_t nsec)
152{
153 nsec += 500;
154 do_div(nsec, 1000);
155 return nsec;
156}
157
Ingo Molnare309b412008-05-12 21:20:51 +0200158cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200159{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400160 u64 ts = ring_buffer_time_stamp(cpu);
161 ring_buffer_normalize_time_stamp(cpu, &ts);
162 return ts;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200163}
164
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200165/*
166 * The global_trace is the descriptor that holds the tracing
167 * buffers for the live tracing. For each CPU, it contains
168 * a link list of pages that will store trace entries. The
169 * page descriptor of the pages in the memory is used to hold
170 * the link list by linking the lru item in the page descriptor
171 * to each of the pages in the buffer per CPU.
172 *
173 * For each active CPU there is a data field that holds the
174 * pages for the buffer for that CPU. Each CPU has the same number
175 * of pages allocated for its buffer.
176 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200177static struct trace_array global_trace;
178
179static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
180
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200181/*
182 * The max_tr is used to snapshot the global_trace when a maximum
183 * latency is reached. Some tracers will use this to store a maximum
184 * trace while it continues examining live traces.
185 *
186 * The buffers for the max_tr are set up the same as the global_trace.
187 * When a snapshot is taken, the link list of the max_tr is swapped
188 * with the link list of the global_trace and the buffers are reset for
189 * the global_trace so the tracing can continue.
190 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200191static struct trace_array max_tr;
192
193static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
194
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200195/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200196static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200197
Steven Rostedt90369902008-11-05 16:05:44 -0500198/**
199 * tracing_is_enabled - return tracer_enabled status
200 *
201 * This function is used by other tracers to know the status
202 * of the tracer_enabled flag. Tracers may use this function
203 * to know if it should enable their features when starting
204 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
205 */
206int tracing_is_enabled(void)
207{
208 return tracer_enabled;
209}
210
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200211/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400212 * trace_buf_size is the size in bytes that is allocated
213 * for a buffer. Note, the number of bytes is always rounded
214 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400215 *
216 * This number is purposely set to a low number of 16384.
217 * If the dump on oops happens, it will be much appreciated
218 * to not have to wait for all that output. Anyway this can be
219 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200220 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400221#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400222
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400223static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200224
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200225/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200226static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200227
228/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200229static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200230
231/*
232 * max_tracer_type_len is used to simplify the allocating of
233 * buffers to read userspace tracer names. We keep track of
234 * the longest tracer name registered.
235 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200236static int max_tracer_type_len;
237
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200238/*
239 * trace_types_lock is used to protect the trace_types list.
240 * This lock is also used to keep user access serialized.
241 * Accesses from userspace will grab this lock while userspace
242 * activities happen inside the kernel.
243 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200244static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200245
246/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200247static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
248
Steven Rostedtee6bce52008-11-12 17:52:37 -0500249/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500250unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200251 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
Ingo Molnar4e655512008-05-12 21:20:52 +0200252
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200253/**
254 * trace_wake_up - wake up tasks waiting for trace input
255 *
256 * Simply wakes up any task that is blocked on the trace_wait
257 * queue. These is used with trace_poll for tasks polling the trace.
258 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200259void trace_wake_up(void)
260{
Ingo Molnar017730c2008-05-12 21:20:52 +0200261 /*
262 * The runqueue_is_locked() can fail, but this is the best we
263 * have for now:
264 */
265 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200266 wake_up(&trace_wait);
267}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200268
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400269static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200270{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400271 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200272 int ret;
273
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200274 if (!str)
275 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400276 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200277 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400278 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200279 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400280 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200281 return 1;
282}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400283__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200284
Steven Rostedt57f50be2008-05-12 21:20:44 +0200285unsigned long nsecs_to_usecs(unsigned long nsecs)
286{
287 return nsecs / 1000;
288}
289
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200290/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200291static const char *trace_options[] = {
292 "print-parent",
293 "sym-offset",
294 "sym-addr",
295 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200296 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200297 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200298 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200299 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200300 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200301 "sched-tree",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100302 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500303 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500304 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500305 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200306 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200307 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100308 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200309 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500310 "latency-format",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200311 NULL
312};
313
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200314/*
315 * ftrace_max_lock is used to protect the swapping of buffers
316 * when taking a max snapshot. The buffers themselves are
317 * protected by per_cpu spinlocks. But the action of the swap
318 * needs its own lock.
319 *
320 * This is defined as a raw_spinlock_t in order to help
321 * with performance when lockdep debugging is enabled.
322 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200323static raw_spinlock_t ftrace_max_lock =
324 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200325
326/*
327 * Copy the new maximum trace into the separate maximum-trace
328 * structure. (this way the maximum trace is permanently saved,
329 * for later retrieval via /debugfs/tracing/latency_trace)
330 */
Ingo Molnare309b412008-05-12 21:20:51 +0200331static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200332__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
333{
334 struct trace_array_cpu *data = tr->data[cpu];
335
336 max_tr.cpu = cpu;
337 max_tr.time_start = data->preempt_timestamp;
338
339 data = max_tr.data[cpu];
340 data->saved_latency = tracing_max_latency;
341
342 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
343 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100344 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200345 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
346 data->policy = tsk->policy;
347 data->rt_priority = tsk->rt_priority;
348
349 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500350 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200351}
352
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200353ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
354{
355 int len;
356 int ret;
357
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500358 if (!cnt)
359 return 0;
360
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200361 if (s->len <= s->readpos)
362 return -EBUSY;
363
364 len = s->len - s->readpos;
365 if (cnt > len)
366 cnt = len;
367 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500368 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200369 return -EFAULT;
370
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500371 cnt -= ret;
372
Steven Rostedte74da522009-03-04 20:31:11 -0500373 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200374 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200375}
376
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200377ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
378{
379 int len;
380 void *ret;
381
382 if (s->len <= s->readpos)
383 return -EBUSY;
384
385 len = s->len - s->readpos;
386 if (cnt > len)
387 cnt = len;
388 ret = memcpy(buf, s->buffer + s->readpos, cnt);
389 if (!ret)
390 return -EFAULT;
391
Steven Rostedte74da522009-03-04 20:31:11 -0500392 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200393 return cnt;
394}
395
Ingo Molnare309b412008-05-12 21:20:51 +0200396static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200397trace_print_seq(struct seq_file *m, struct trace_seq *s)
398{
399 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
400
401 s->buffer[len] = 0;
402 seq_puts(m, s->buffer);
403
Steven Rostedtf9520752009-03-02 14:04:40 -0500404 trace_seq_init(s);
Steven Rostedt214023c2008-05-12 21:20:46 +0200405}
406
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200407/**
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
409 * @tr: tracer
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
412 *
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
415 */
Ingo Molnare309b412008-05-12 21:20:51 +0200416void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200417update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
418{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400419 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200420
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200421 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200422 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400423
424 tr->buffer = max_tr.buffer;
425 max_tr.buffer = buf;
426
Steven Rostedtd7690412008-10-01 00:29:53 -0400427 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400428 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400429 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200430
431 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200432 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200433}
434
435/**
436 * update_max_tr_single - only copy one trace over, and reset the rest
437 * @tr - tracer
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200440 *
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200442 */
Ingo Molnare309b412008-05-12 21:20:51 +0200443void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200444update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
445{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400446 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200447
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200448 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200449 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200450
Steven Rostedtd7690412008-10-01 00:29:53 -0400451 ftrace_disable_cpu();
452
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
455
Steven Rostedtd7690412008-10-01 00:29:53 -0400456 ftrace_enable_cpu();
457
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500458 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200459
460 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200461 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200462}
463
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200464/**
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
467 *
468 * Register a new plugin tracer.
469 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200470int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100471__releases(kernel_lock)
472__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200473{
474 struct tracer *t;
475 int len;
476 int ret = 0;
477
478 if (!type->name) {
479 pr_info("Tracer must have a name\n");
480 return -1;
481 }
482
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100483 /*
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
488 */
489 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200490 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100491
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100492 tracing_selftest_running = true;
493
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
496 /* already found */
497 pr_info("Trace %s already registered\n",
498 type->name);
499 ret = -1;
500 goto out;
501 }
502 }
503
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100504 if (!type->set_flag)
505 type->set_flag = &dummy_set_flag;
506 if (!type->flags)
507 type->flags = &dummy_tracer_flags;
508 else
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
513
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100514
Steven Rostedt60a11772008-05-12 21:20:44 +0200515#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500516 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200517 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200518 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200519 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100520
Steven Rostedt60a11772008-05-12 21:20:44 +0200521 /*
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
527 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100528 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400529 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100530
Steven Rostedt60a11772008-05-12 21:20:44 +0200531 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200537 if (ret) {
538 printk(KERN_CONT "FAILED!\n");
539 goto out;
540 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200541 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100542 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400543 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100544
Steven Rostedt60a11772008-05-12 21:20:44 +0200545 printk(KERN_CONT "PASSED\n");
546 }
547#endif
548
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200549 type->next = trace_types;
550 trace_types = type;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200554
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200555 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100556 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200557 mutex_unlock(&trace_types_lock);
558
Steven Rostedtdac74942009-02-05 01:13:38 -0500559 if (ret || !default_bootup_tracer)
560 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500561
Steven Rostedtdac74942009-02-05 01:13:38 -0500562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
563 goto out_unlock;
564
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571#ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
573 type->name);
574#endif
575
576 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500577 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200578 return ret;
579}
580
581void unregister_tracer(struct tracer *type)
582{
583 struct tracer **t;
584 int len;
585
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
588 if (*t == type)
589 goto found;
590 }
591 pr_info("Trace %s not registered\n", type->name);
592 goto out;
593
594 found:
595 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200596
597 if (type == current_trace && tracer_enabled) {
598 tracer_enabled = 0;
599 tracing_stop();
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
603 }
604
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200605 if (strlen(type->name) != max_tracer_type_len)
606 goto out;
607
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
613 }
614 out:
615 mutex_unlock(&trace_types_lock);
616}
617
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400618void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200619{
Steven Rostedtd7690412008-10-01 00:29:53 -0400620 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400621 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400622 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200623}
624
Pekka J Enberg213cc062008-12-19 12:08:39 +0200625void tracing_reset_online_cpus(struct trace_array *tr)
626{
627 int cpu;
628
629 tr->time_start = ftrace_now(tr->cpu);
630
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
633}
634
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200635#define SAVED_CMDLINES 128
636static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
637static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
638static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
639static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100640static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200641
Steven Rostedt25b0b442008-05-12 21:21:00 +0200642/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100643static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200644
645static void trace_init_cmdlines(void)
646{
647 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
648 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
649 cmdline_idx = 0;
650}
651
Steven Rostedt0f048702008-11-05 16:05:44 -0500652static int trace_stop_count;
653static DEFINE_SPINLOCK(tracing_start_lock);
654
655/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500656 * ftrace_off_permanent - disable all ftrace code permanently
657 *
658 * This should only be called when a serious anomally has
659 * been detected. This will turn off the function tracing,
660 * ring buffers, and other tracing utilites. It takes no
661 * locks and can be called from any context.
662 */
663void ftrace_off_permanent(void)
664{
665 tracing_disabled = 1;
666 ftrace_stop();
667 tracing_off_permanent();
668}
669
670/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500671 * tracing_start - quick start of the tracer
672 *
673 * If tracing is enabled but was stopped by tracing_stop,
674 * this will start the tracer back up.
675 */
676void tracing_start(void)
677{
678 struct ring_buffer *buffer;
679 unsigned long flags;
680
681 if (tracing_disabled)
682 return;
683
684 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500685 if (--trace_stop_count) {
686 if (trace_stop_count < 0) {
687 /* Someone screwed up their debugging */
688 WARN_ON_ONCE(1);
689 trace_stop_count = 0;
690 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500691 goto out;
692 }
693
694
695 buffer = global_trace.buffer;
696 if (buffer)
697 ring_buffer_record_enable(buffer);
698
699 buffer = max_tr.buffer;
700 if (buffer)
701 ring_buffer_record_enable(buffer);
702
703 ftrace_start();
704 out:
705 spin_unlock_irqrestore(&tracing_start_lock, flags);
706}
707
708/**
709 * tracing_stop - quick stop of the tracer
710 *
711 * Light weight way to stop tracing. Use in conjunction with
712 * tracing_start.
713 */
714void tracing_stop(void)
715{
716 struct ring_buffer *buffer;
717 unsigned long flags;
718
719 ftrace_stop();
720 spin_lock_irqsave(&tracing_start_lock, flags);
721 if (trace_stop_count++)
722 goto out;
723
724 buffer = global_trace.buffer;
725 if (buffer)
726 ring_buffer_record_disable(buffer);
727
728 buffer = max_tr.buffer;
729 if (buffer)
730 ring_buffer_record_disable(buffer);
731
732 out:
733 spin_unlock_irqrestore(&tracing_start_lock, flags);
734}
735
Ingo Molnare309b412008-05-12 21:20:51 +0200736void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200737
Ingo Molnare309b412008-05-12 21:20:51 +0200738static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200739{
740 unsigned map;
741 unsigned idx;
742
743 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
744 return;
745
746 /*
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
751 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100752 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200753 return;
754
755 idx = map_pid_to_cmdline[tsk->pid];
756 if (idx >= SAVED_CMDLINES) {
757 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
758
759 map = map_cmdline_to_pid[idx];
760 if (map <= PID_MAX_DEFAULT)
761 map_pid_to_cmdline[map] = (unsigned)-1;
762
763 map_pid_to_cmdline[tsk->pid] = idx;
764
765 cmdline_idx = idx;
766 }
767
768 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
769
Peter Zijlstraefed7922009-03-04 12:32:55 +0100770 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200771}
772
Steven Rostedt660c7f92008-11-26 00:16:26 -0500773char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200774{
775 char *cmdline = "<...>";
776 unsigned map;
777
778 if (!pid)
779 return "<idle>";
780
781 if (pid > PID_MAX_DEFAULT)
782 goto out;
783
784 map = map_pid_to_cmdline[pid];
785 if (map >= SAVED_CMDLINES)
786 goto out;
787
788 cmdline = saved_cmdlines[map];
789
790 out:
791 return cmdline;
792}
793
Ingo Molnare309b412008-05-12 21:20:51 +0200794void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200795{
796 if (atomic_read(&trace_record_cmdline_disabled))
797 return;
798
799 trace_save_cmdline(tsk);
800}
801
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300802void
Steven Rostedt38697052008-10-01 13:14:09 -0400803tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
804 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200805{
806 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200807
Steven Rostedt777e2082008-09-29 23:02:42 -0400808 entry->preempt_count = pc & 0xff;
809 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedtef180122009-03-10 14:10:56 -0400810 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400811 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400812#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400813 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400814#else
815 TRACE_FLAG_IRQS_NOSUPPORT |
816#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200817 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
818 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
819 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
820}
821
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200822struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
823 unsigned char type,
824 unsigned long len,
825 unsigned long flags, int pc)
826{
827 struct ring_buffer_event *event;
828
829 event = ring_buffer_lock_reserve(tr->buffer, len);
830 if (event != NULL) {
831 struct trace_entry *ent = ring_buffer_event_data(event);
832
833 tracing_generic_entry_update(ent, flags, pc);
834 ent->type = type;
835 }
836
837 return event;
838}
839static void ftrace_trace_stack(struct trace_array *tr,
840 unsigned long flags, int skip, int pc);
841static void ftrace_trace_userstack(struct trace_array *tr,
842 unsigned long flags, int pc);
843
844void trace_buffer_unlock_commit(struct trace_array *tr,
845 struct ring_buffer_event *event,
846 unsigned long flags, int pc)
847{
848 ring_buffer_unlock_commit(tr->buffer, event);
849
850 ftrace_trace_stack(tr, flags, 6, pc);
851 ftrace_trace_userstack(tr, flags, pc);
852 trace_wake_up();
853}
854
Steven Rostedtef5580d2009-02-27 19:38:04 -0500855struct ring_buffer_event *
856trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
857 unsigned long flags, int pc)
858{
859 return trace_buffer_lock_reserve(&global_trace,
860 type, len, flags, pc);
861}
862
863void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
864 unsigned long flags, int pc)
865{
866 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
867}
868
Ingo Molnare309b412008-05-12 21:20:51 +0200869void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500870trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400871 unsigned long ip, unsigned long parent_ip, unsigned long flags,
872 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200873{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400874 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400875 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200876
Steven Rostedtd7690412008-10-01 00:29:53 -0400877 /* If we are reading the ring buffer, don't trace */
878 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
879 return;
880
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200881 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
882 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400883 if (!event)
884 return;
885 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400886 entry->ip = ip;
887 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200888 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200889}
890
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100891#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100892static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100893 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100894 unsigned long flags,
895 int pc)
896{
897 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100898 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100899
900 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
901 return;
902
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200903 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
904 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100905 if (!event)
906 return;
907 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100908 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200909 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100910}
911
912static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100913 struct ftrace_graph_ret *trace,
914 unsigned long flags,
915 int pc)
916{
917 struct ring_buffer_event *event;
918 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100919
920 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
921 return;
922
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200923 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
924 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100925 if (!event)
926 return;
927 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100928 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200929 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100930}
931#endif
932
Ingo Molnare309b412008-05-12 21:20:51 +0200933void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200934ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400935 unsigned long ip, unsigned long parent_ip, unsigned long flags,
936 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200937{
938 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500939 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200940}
941
Steven Rostedt53614992009-01-15 19:12:40 -0500942static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500943 unsigned long flags,
944 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200945{
Al Viroc2c80522008-10-31 19:50:41 +0000946#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400947 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400948 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200949 struct stack_trace trace;
950
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200951 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
952 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400953 if (!event)
954 return;
955 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400956 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200957
958 trace.nr_entries = 0;
959 trace.max_entries = FTRACE_STACK_ENTRIES;
960 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400961 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200962
963 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200964 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000965#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200966}
967
Steven Rostedt53614992009-01-15 19:12:40 -0500968static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500969 unsigned long flags,
970 int skip, int pc)
971{
972 if (!(trace_flags & TRACE_ITER_STACKTRACE))
973 return;
974
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500975 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500976}
977
Steven Rostedt38697052008-10-01 13:14:09 -0400978void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400979 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500980 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400981{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500982 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400983}
984
Török Edwin02b67512008-11-22 13:28:47 +0200985static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500986 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +0200987{
Török Edwinc7425ac2008-11-28 11:17:56 +0200988#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +0200989 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +0200990 struct userstack_entry *entry;
991 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +0200992
993 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
994 return;
995
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200996 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
997 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +0200998 if (!event)
999 return;
1000 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001001
1002 memset(&entry->caller, 0, sizeof(entry->caller));
1003
1004 trace.nr_entries = 0;
1005 trace.max_entries = FTRACE_STACK_ENTRIES;
1006 trace.skip = 0;
1007 trace.entries = entry->caller;
1008
1009 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001010 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001011#endif
Török Edwin02b67512008-11-22 13:28:47 +02001012}
1013
Hannes Eder4fd27352009-02-10 19:44:12 +01001014#ifdef UNUSED
1015static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001016{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001017 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001018}
Hannes Eder4fd27352009-02-10 19:44:12 +01001019#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001020
Steven Rostedt38697052008-10-01 13:14:09 -04001021static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001022ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001023 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1024 int pc)
Ingo Molnara4feb832008-05-12 21:21:02 +02001025{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001026 struct ring_buffer_event *event;
Ingo Molnara4feb832008-05-12 21:21:02 +02001027 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001028 struct special_entry *entry;
Ingo Molnara4feb832008-05-12 21:21:02 +02001029
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001030 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1031 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001032 if (!event)
1033 return;
1034 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001035 entry->arg1 = arg1;
1036 entry->arg2 = arg2;
1037 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001038 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb832008-05-12 21:21:02 +02001039}
1040
1041void
Steven Rostedt38697052008-10-01 13:14:09 -04001042__trace_special(void *__tr, void *__data,
1043 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1044{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001045 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001046}
1047
1048void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001049tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001050 struct task_struct *prev,
1051 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001052 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001053{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001054 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001055 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001057 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1058 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001059 if (!event)
1060 return;
1061 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001062 entry->prev_pid = prev->pid;
1063 entry->prev_prio = prev->prio;
1064 entry->prev_state = prev->state;
1065 entry->next_pid = next->pid;
1066 entry->next_prio = next->prio;
1067 entry->next_state = next->state;
1068 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001069 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001070}
1071
Ingo Molnar57422792008-05-12 21:20:51 +02001072void
1073tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001074 struct task_struct *wakee,
1075 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001076 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001077{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001078 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001079 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001080
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001081 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1082 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001083 if (!event)
1084 return;
1085 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001086 entry->prev_pid = curr->pid;
1087 entry->prev_prio = curr->prio;
1088 entry->prev_state = curr->state;
1089 entry->next_pid = wakee->pid;
1090 entry->next_prio = wakee->prio;
1091 entry->next_state = wakee->state;
1092 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001093
1094 ring_buffer_unlock_commit(tr->buffer, event);
1095 ftrace_trace_stack(tr, flags, 6, pc);
1096 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001097}
1098
Steven Rostedt4902f882008-05-22 00:22:18 -04001099void
1100ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1101{
1102 struct trace_array *tr = &global_trace;
1103 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001104 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001105 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001106 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001107
Steven Rostedtc76f0692008-11-07 22:36:02 -05001108 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001109 return;
1110
Steven Rostedt38697052008-10-01 13:14:09 -04001111 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001112 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001113 cpu = raw_smp_processor_id();
1114 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001115
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001116 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001117 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001118
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001119 atomic_dec(&data->disabled);
1120 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001121}
1122
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001123#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001124int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001125{
1126 struct trace_array *tr = &global_trace;
1127 struct trace_array_cpu *data;
1128 unsigned long flags;
1129 long disabled;
1130 int cpu;
1131 int pc;
1132
Steven Rostedt804a6852008-12-03 15:36:59 -05001133 if (!ftrace_trace_task(current))
1134 return 0;
1135
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001136 if (!ftrace_graph_addr(trace->func))
1137 return 0;
1138
Steven Rostedta5e25882008-12-02 15:34:05 -05001139 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001140 cpu = raw_smp_processor_id();
1141 data = tr->data[cpu];
1142 disabled = atomic_inc_return(&data->disabled);
1143 if (likely(disabled == 1)) {
1144 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001145 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001146 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001147 /* Only do the atomic if it is not already set */
1148 if (!test_tsk_trace_graph(current))
1149 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001150 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001151 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001152
1153 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001154}
1155
1156void trace_graph_return(struct ftrace_graph_ret *trace)
1157{
1158 struct trace_array *tr = &global_trace;
1159 struct trace_array_cpu *data;
1160 unsigned long flags;
1161 long disabled;
1162 int cpu;
1163 int pc;
1164
Steven Rostedta5e25882008-12-02 15:34:05 -05001165 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001166 cpu = raw_smp_processor_id();
1167 data = tr->data[cpu];
1168 disabled = atomic_inc_return(&data->disabled);
1169 if (likely(disabled == 1)) {
1170 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001171 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001172 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001173 if (!trace->depth)
1174 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001175 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001176 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001177}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001178#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001179
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001180
1181/**
1182 * trace_vprintk - write binary msg to tracing buffer
1183 *
1184 */
1185int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1186{
Steven Rostedt80370cb2009-03-10 17:16:35 -04001187 static raw_spinlock_t trace_buf_lock =
1188 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001189 static u32 trace_buf[TRACE_BUF_SIZE];
1190
1191 struct ring_buffer_event *event;
1192 struct trace_array *tr = &global_trace;
1193 struct trace_array_cpu *data;
1194 struct print_entry *entry;
1195 unsigned long flags;
1196 int resched;
1197 int cpu, len = 0, size, pc;
1198
1199 if (unlikely(tracing_selftest_running || tracing_disabled))
1200 return 0;
1201
1202 /* Don't pollute graph traces with trace_vprintk internals */
1203 pause_graph_tracing();
1204
1205 pc = preempt_count();
1206 resched = ftrace_preempt_disable();
1207 cpu = raw_smp_processor_id();
1208 data = tr->data[cpu];
1209
1210 if (unlikely(atomic_read(&data->disabled)))
1211 goto out;
1212
Steven Rostedt80370cb2009-03-10 17:16:35 -04001213 /* Lockdep uses trace_printk for lock tracing */
1214 local_irq_save(flags);
1215 __raw_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001216 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1217
1218 if (len > TRACE_BUF_SIZE || len < 0)
1219 goto out_unlock;
1220
1221 size = sizeof(*entry) + sizeof(u32) * len;
1222 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
1223 if (!event)
1224 goto out_unlock;
1225 entry = ring_buffer_event_data(event);
1226 entry->ip = ip;
1227 entry->depth = depth;
1228 entry->fmt = fmt;
1229
1230 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1231 ring_buffer_unlock_commit(tr->buffer, event);
1232
1233out_unlock:
Steven Rostedt80370cb2009-03-10 17:16:35 -04001234 __raw_spin_unlock(&trace_buf_lock);
1235 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001236
1237out:
1238 ftrace_preempt_enable(resched);
1239 unpause_graph_tracing();
1240
1241 return len;
1242}
1243EXPORT_SYMBOL_GPL(trace_vprintk);
1244
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001245enum trace_file_type {
1246 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001247 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001248};
1249
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001250static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001251{
Steven Rostedtd7690412008-10-01 00:29:53 -04001252 /* Don't allow ftrace to trace into the ring buffers */
1253 ftrace_disable_cpu();
1254
Steven Rostedt5a90f572008-09-03 17:42:51 -04001255 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001256 if (iter->buffer_iter[iter->cpu])
1257 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1258
1259 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001260}
1261
Ingo Molnare309b412008-05-12 21:20:51 +02001262static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001263peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001264{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001265 struct ring_buffer_event *event;
1266 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001267
Steven Rostedtd7690412008-10-01 00:29:53 -04001268 /* Don't allow ftrace to trace into the ring buffers */
1269 ftrace_disable_cpu();
1270
1271 if (buf_iter)
1272 event = ring_buffer_iter_peek(buf_iter, ts);
1273 else
1274 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1275
1276 ftrace_enable_cpu();
1277
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001278 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001279}
Steven Rostedtd7690412008-10-01 00:29:53 -04001280
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001281static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001282__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001284 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001285 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001286 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001287 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288 int next_cpu = -1;
1289 int cpu;
1290
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001291 /*
1292 * If we are in a per_cpu trace file, don't bother by iterating over
1293 * all cpu and peek directly.
1294 */
1295 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1296 if (ring_buffer_empty_cpu(buffer, cpu_file))
1297 return NULL;
1298 ent = peek_next_entry(iter, cpu_file, ent_ts);
1299 if (ent_cpu)
1300 *ent_cpu = cpu_file;
1301
1302 return ent;
1303 }
1304
Steven Rostedtab464282008-05-12 21:21:00 +02001305 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001306
1307 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001309
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001310 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001311
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001312 /*
1313 * Pick the entry with the smallest timestamp:
1314 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001315 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316 next = ent;
1317 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001318 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001319 }
1320 }
1321
1322 if (ent_cpu)
1323 *ent_cpu = next_cpu;
1324
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001325 if (ent_ts)
1326 *ent_ts = next_ts;
1327
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001328 return next;
1329}
1330
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001331/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001332struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1333 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001334{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001335 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001336}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001337
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001338/* Find the next real entry, and increment the iterator to the next entry */
1339static void *find_next_entry_inc(struct trace_iterator *iter)
1340{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001341 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001342
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001343 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001344 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001345
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001346 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001347}
1348
Ingo Molnare309b412008-05-12 21:20:51 +02001349static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001350{
Steven Rostedtd7690412008-10-01 00:29:53 -04001351 /* Don't allow ftrace to trace into the ring buffers */
1352 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001353 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001354 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001355}
1356
Ingo Molnare309b412008-05-12 21:20:51 +02001357static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358{
1359 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001360 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001361 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362
1363 (*pos)++;
1364
1365 /* can't go backwards */
1366 if (iter->idx > i)
1367 return NULL;
1368
1369 if (iter->idx < 0)
1370 ent = find_next_entry_inc(iter);
1371 else
1372 ent = iter;
1373
1374 while (ent && iter->idx < i)
1375 ent = find_next_entry_inc(iter);
1376
1377 iter->pos = *pos;
1378
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001379 return ent;
1380}
1381
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001382/*
1383 * No necessary locking here. The worst thing which can
1384 * happen is loosing events consumed at the same time
1385 * by a trace_pipe reader.
1386 * Other than that, we don't risk to crash the ring buffer
1387 * because it serializes the readers.
1388 *
1389 * The current tracer is copied to avoid a global locking
1390 * all around.
1391 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001392static void *s_start(struct seq_file *m, loff_t *pos)
1393{
1394 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001395 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001396 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001397 void *p = NULL;
1398 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001399 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001400
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001401 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001402 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001403 if (unlikely(old_tracer != current_trace && current_trace)) {
1404 old_tracer = current_trace;
1405 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001406 }
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001407 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001408
1409 atomic_inc(&trace_record_cmdline_disabled);
1410
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001411 if (*pos != iter->pos) {
1412 iter->ent = NULL;
1413 iter->cpu = 0;
1414 iter->idx = -1;
1415
Steven Rostedtd7690412008-10-01 00:29:53 -04001416 ftrace_disable_cpu();
1417
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001418 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1419 for_each_tracing_cpu(cpu)
1420 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1421 } else
1422 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1423
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001424
Steven Rostedtd7690412008-10-01 00:29:53 -04001425 ftrace_enable_cpu();
1426
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1428 ;
1429
1430 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001431 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001432 p = s_next(m, p, &l);
1433 }
1434
1435 return p;
1436}
1437
1438static void s_stop(struct seq_file *m, void *p)
1439{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001440 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001441}
1442
Ingo Molnare309b412008-05-12 21:20:51 +02001443static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001444{
Michael Ellermana6168352008-08-20 16:36:11 -07001445 seq_puts(m, "# _------=> CPU# \n");
1446 seq_puts(m, "# / _-----=> irqs-off \n");
1447 seq_puts(m, "# | / _----=> need-resched \n");
1448 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1449 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1450 seq_puts(m, "# |||| / \n");
1451 seq_puts(m, "# ||||| delay \n");
1452 seq_puts(m, "# cmd pid ||||| time | caller \n");
1453 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001454}
1455
Ingo Molnare309b412008-05-12 21:20:51 +02001456static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001457{
Michael Ellermana6168352008-08-20 16:36:11 -07001458 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1459 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001460}
1461
1462
Ingo Molnare309b412008-05-12 21:20:51 +02001463static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001464print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1465{
1466 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1467 struct trace_array *tr = iter->tr;
1468 struct trace_array_cpu *data = tr->data[tr->cpu];
1469 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001470 unsigned long total;
1471 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001472 const char *name = "preemption";
1473
1474 if (type)
1475 name = type->name;
1476
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001477 entries = ring_buffer_entries(iter->tr->buffer);
1478 total = entries +
1479 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480
1481 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1482 name, UTS_RELEASE);
1483 seq_puts(m, "-----------------------------------"
1484 "---------------------------------\n");
1485 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1486 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001487 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001489 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001490 tr->cpu,
1491#if defined(CONFIG_PREEMPT_NONE)
1492 "server",
1493#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1494 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001495#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001496 "preempt",
1497#else
1498 "unknown",
1499#endif
1500 /* These are reserved for later use */
1501 0, 0, 0, 0);
1502#ifdef CONFIG_SMP
1503 seq_printf(m, " #P:%d)\n", num_online_cpus());
1504#else
1505 seq_puts(m, ")\n");
1506#endif
1507 seq_puts(m, " -----------------\n");
1508 seq_printf(m, " | task: %.16s-%d "
1509 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1510 data->comm, data->pid, data->uid, data->nice,
1511 data->policy, data->rt_priority);
1512 seq_puts(m, " -----------------\n");
1513
1514 if (data->critical_start) {
1515 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001516 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1517 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001518 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001519 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1520 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521 seq_puts(m, "\n");
1522 }
1523
1524 seq_puts(m, "\n");
1525}
1526
Steven Rostedta3097202008-11-07 22:36:02 -05001527static void test_cpu_buff_start(struct trace_iterator *iter)
1528{
1529 struct trace_seq *s = &iter->seq;
1530
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001531 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1532 return;
1533
1534 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1535 return;
1536
Rusty Russell44623442009-01-01 10:12:23 +10301537 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001538 return;
1539
Rusty Russell44623442009-01-01 10:12:23 +10301540 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001541 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1542}
1543
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001544static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001545{
Steven Rostedt214023c2008-05-12 21:20:46 +02001546 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001548 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001549 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001550
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001551 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001552
Steven Rostedta3097202008-11-07 22:36:02 -05001553 test_cpu_buff_start(iter);
1554
Steven Rostedtf633cef2008-12-23 23:24:13 -05001555 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001556
1557 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001558 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1559 if (!trace_print_lat_context(iter))
1560 goto partial;
1561 } else {
1562 if (!trace_print_context(iter))
1563 goto partial;
1564 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001565 }
1566
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001567 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001568 return event->trace(iter, sym_flags);
1569
1570 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1571 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001572
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001573 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001574partial:
1575 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001576}
1577
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001578static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001579{
1580 struct trace_seq *s = &iter->seq;
1581 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001582 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001583
1584 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001585
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001586 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001587 if (!trace_seq_printf(s, "%d %d %llu ",
1588 entry->pid, iter->cpu, iter->ts))
1589 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001590 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001591
Steven Rostedtf633cef2008-12-23 23:24:13 -05001592 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001593 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001594 return event->raw(iter, 0);
1595
1596 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1597 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001598
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001599 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001600partial:
1601 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001602}
1603
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001604static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001605{
1606 struct trace_seq *s = &iter->seq;
1607 unsigned char newline = '\n';
1608 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001609 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001610
1611 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001612
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001613 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1614 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1615 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1616 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1617 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001618
Steven Rostedtf633cef2008-12-23 23:24:13 -05001619 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001620 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001621 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001622 if (ret != TRACE_TYPE_HANDLED)
1623 return ret;
1624 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001625
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001626 SEQ_PUT_FIELD_RET(s, newline);
1627
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001628 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001629}
1630
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001631static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1632{
1633 struct trace_seq *s = &iter->seq;
1634 struct trace_entry *entry = iter->ent;
1635 struct print_entry *field;
1636 int ret;
1637
1638 trace_assign_type(field, entry);
1639
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001640 ret = trace_seq_bprintf(s, field->fmt, field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001641 if (!ret)
1642 return TRACE_TYPE_PARTIAL_LINE;
1643
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001644 return TRACE_TYPE_HANDLED;
1645}
1646
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001647static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001648{
1649 struct trace_seq *s = &iter->seq;
1650 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001651 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001652
1653 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001654
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001655 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1656 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001657 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001658 SEQ_PUT_FIELD_RET(s, iter->ts);
1659 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001660
Steven Rostedtf633cef2008-12-23 23:24:13 -05001661 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001662 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001663}
1664
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001665static int trace_empty(struct trace_iterator *iter)
1666{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001667 int cpu;
1668
Steven Rostedtab464282008-05-12 21:21:00 +02001669 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001670 if (iter->buffer_iter[cpu]) {
1671 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1672 return 0;
1673 } else {
1674 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1675 return 0;
1676 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001677 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001678
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001679 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001680}
1681
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001682static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001683{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001684 enum print_line_t ret;
1685
1686 if (iter->trace && iter->trace->print_line) {
1687 ret = iter->trace->print_line(iter);
1688 if (ret != TRACE_TYPE_UNHANDLED)
1689 return ret;
1690 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001691
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001692 if (iter->ent->type == TRACE_PRINT &&
1693 trace_flags & TRACE_ITER_PRINTK &&
1694 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1695 return print_printk_msg_only(iter);
1696
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001697 if (trace_flags & TRACE_ITER_BIN)
1698 return print_bin_fmt(iter);
1699
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001700 if (trace_flags & TRACE_ITER_HEX)
1701 return print_hex_fmt(iter);
1702
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001703 if (trace_flags & TRACE_ITER_RAW)
1704 return print_raw_fmt(iter);
1705
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001706 return print_trace_fmt(iter);
1707}
1708
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001709static int s_show(struct seq_file *m, void *v)
1710{
1711 struct trace_iterator *iter = v;
1712
1713 if (iter->ent == NULL) {
1714 if (iter->tr) {
1715 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1716 seq_puts(m, "#\n");
1717 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001718 if (iter->trace && iter->trace->print_header)
1719 iter->trace->print_header(m);
1720 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001721 /* print nothing if the buffers are empty */
1722 if (trace_empty(iter))
1723 return 0;
1724 print_trace_header(m, iter);
1725 if (!(trace_flags & TRACE_ITER_VERBOSE))
1726 print_lat_help_header(m);
1727 } else {
1728 if (!(trace_flags & TRACE_ITER_VERBOSE))
1729 print_func_help_header(m);
1730 }
1731 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001732 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001733 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001734 }
1735
1736 return 0;
1737}
1738
1739static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001740 .start = s_start,
1741 .next = s_next,
1742 .stop = s_stop,
1743 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001744};
1745
Ingo Molnare309b412008-05-12 21:20:51 +02001746static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001747__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001748{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001749 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001750 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001751 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001752 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001753 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001754
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001755 if (tracing_disabled)
1756 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001757
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001758 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001759 if (!iter)
1760 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001761
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001762 /*
1763 * We make a copy of the current tracer to avoid concurrent
1764 * changes on it while we are reading.
1765 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001766 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001767 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001768 if (!iter->trace)
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001769 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001770
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001771 if (current_trace)
1772 *iter->trace = *current_trace;
1773
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001774 if (current_trace && current_trace->print_max)
1775 iter->tr = &max_tr;
1776 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001777 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001778 iter->pos = -1;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001779 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001780 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001781
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001782 /* Notify the tracer early; before we stop tracing. */
1783 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001784 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001785
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001786 /* Annotate start of buffers if we had overruns */
1787 if (ring_buffer_overruns(iter->tr->buffer))
1788 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1789
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001790 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1791 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001792
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001793 iter->buffer_iter[cpu] =
1794 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001795
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001796 if (!iter->buffer_iter[cpu])
1797 goto fail_buffer;
1798 }
1799 } else {
1800 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001801 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001802 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001803
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001804 if (!iter->buffer_iter[cpu])
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001805 goto fail;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001806 }
1807
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001808 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001809 ret = seq_open(file, &tracer_seq_ops);
1810 if (ret < 0) {
1811 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001812 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001813 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001814
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001815 m = file->private_data;
1816 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001817
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001818 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001819 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001820
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001821 mutex_unlock(&trace_types_lock);
1822
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001823 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001824
1825 fail_buffer:
1826 for_each_tracing_cpu(cpu) {
1827 if (iter->buffer_iter[cpu])
1828 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1829 }
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001830 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001831 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001832 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001833 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001834
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001835 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001836}
1837
1838int tracing_open_generic(struct inode *inode, struct file *filp)
1839{
Steven Rostedt60a11772008-05-12 21:20:44 +02001840 if (tracing_disabled)
1841 return -ENODEV;
1842
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001843 filp->private_data = inode->i_private;
1844 return 0;
1845}
1846
Hannes Eder4fd27352009-02-10 19:44:12 +01001847static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001848{
1849 struct seq_file *m = (struct seq_file *)file->private_data;
1850 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001851 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001852
1853 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001854 for_each_tracing_cpu(cpu) {
1855 if (iter->buffer_iter[cpu])
1856 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1857 }
1858
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001859 if (iter->trace && iter->trace->close)
1860 iter->trace->close(iter);
1861
1862 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001863 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001864 mutex_unlock(&trace_types_lock);
1865
1866 seq_release(inode, file);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001867 mutex_destroy(&iter->mutex);
1868 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001869 kfree(iter);
1870 return 0;
1871}
1872
1873static int tracing_open(struct inode *inode, struct file *file)
1874{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001875 struct trace_iterator *iter;
1876 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001877
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001878 iter = __tracing_open(inode, file);
1879 if (IS_ERR(iter))
1880 ret = PTR_ERR(iter);
Steven Rostedtc032ef642009-03-04 20:34:24 -05001881 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001882 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1883
1884 return ret;
1885}
1886
Ingo Molnare309b412008-05-12 21:20:51 +02001887static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001888t_next(struct seq_file *m, void *v, loff_t *pos)
1889{
1890 struct tracer *t = m->private;
1891
1892 (*pos)++;
1893
1894 if (t)
1895 t = t->next;
1896
1897 m->private = t;
1898
1899 return t;
1900}
1901
1902static void *t_start(struct seq_file *m, loff_t *pos)
1903{
1904 struct tracer *t = m->private;
1905 loff_t l = 0;
1906
1907 mutex_lock(&trace_types_lock);
1908 for (; t && l < *pos; t = t_next(m, t, &l))
1909 ;
1910
1911 return t;
1912}
1913
1914static void t_stop(struct seq_file *m, void *p)
1915{
1916 mutex_unlock(&trace_types_lock);
1917}
1918
1919static int t_show(struct seq_file *m, void *v)
1920{
1921 struct tracer *t = v;
1922
1923 if (!t)
1924 return 0;
1925
1926 seq_printf(m, "%s", t->name);
1927 if (t->next)
1928 seq_putc(m, ' ');
1929 else
1930 seq_putc(m, '\n');
1931
1932 return 0;
1933}
1934
1935static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001936 .start = t_start,
1937 .next = t_next,
1938 .stop = t_stop,
1939 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001940};
1941
1942static int show_traces_open(struct inode *inode, struct file *file)
1943{
1944 int ret;
1945
Steven Rostedt60a11772008-05-12 21:20:44 +02001946 if (tracing_disabled)
1947 return -ENODEV;
1948
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001949 ret = seq_open(file, &show_traces_seq_ops);
1950 if (!ret) {
1951 struct seq_file *m = file->private_data;
1952 m->private = trace_types;
1953 }
1954
1955 return ret;
1956}
1957
Steven Rostedt5e2336a2009-03-05 21:44:55 -05001958static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001959 .open = tracing_open,
1960 .read = seq_read,
1961 .llseek = seq_lseek,
1962 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001963};
1964
Steven Rostedt5e2336a2009-03-05 21:44:55 -05001965static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001966 .open = show_traces_open,
1967 .read = seq_read,
1968 .release = seq_release,
1969};
1970
Ingo Molnar36dfe922008-05-12 21:20:52 +02001971/*
1972 * Only trace on a CPU if the bitmask is set:
1973 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301974static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001975
1976/*
1977 * The tracer itself will not take this lock, but still we want
1978 * to provide a consistent cpumask to user-space:
1979 */
1980static DEFINE_MUTEX(tracing_cpumask_update_lock);
1981
1982/*
1983 * Temporary storage for the character representation of the
1984 * CPU bitmask (and one more byte for the newline):
1985 */
1986static char mask_str[NR_CPUS + 1];
1987
Ingo Molnarc7078de2008-05-12 21:20:52 +02001988static ssize_t
1989tracing_cpumask_read(struct file *filp, char __user *ubuf,
1990 size_t count, loff_t *ppos)
1991{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001992 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001993
1994 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001995
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301996 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001997 if (count - len < 2) {
1998 count = -EINVAL;
1999 goto out_err;
2000 }
2001 len += sprintf(mask_str + len, "\n");
2002 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2003
2004out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002005 mutex_unlock(&tracing_cpumask_update_lock);
2006
2007 return count;
2008}
2009
2010static ssize_t
2011tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2012 size_t count, loff_t *ppos)
2013{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002014 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302015 cpumask_var_t tracing_cpumask_new;
2016
2017 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2018 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002019
2020 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302021 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002022 if (err)
2023 goto err_unlock;
2024
Steven Rostedta5e25882008-12-02 15:34:05 -05002025 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02002026 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002027 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002028 /*
2029 * Increase/decrease the disabled counter if we are
2030 * about to flip a bit in the cpumask:
2031 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302032 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2033 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002034 atomic_inc(&global_trace.data[cpu]->disabled);
2035 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302036 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2037 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002038 atomic_dec(&global_trace.data[cpu]->disabled);
2039 }
2040 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002041 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002042 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002043
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302044 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002045
Ingo Molnarc7078de2008-05-12 21:20:52 +02002046 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302047 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002048
Ingo Molnarc7078de2008-05-12 21:20:52 +02002049 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002050
2051err_unlock:
2052 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302053 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002054
2055 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002056}
2057
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002058static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002059 .open = tracing_open_generic,
2060 .read = tracing_cpumask_read,
2061 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062};
2063
2064static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002065tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002066 size_t cnt, loff_t *ppos)
2067{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002068 struct tracer_opt *trace_opts;
2069 u32 tracer_flags;
2070 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002071 char *buf;
2072 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002073 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002074
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002075
Wenji Huangc3706f02009-02-10 01:03:18 -05002076 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002077 for (i = 0; trace_options[i]; i++) {
2078 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002079 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002080 }
2081
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002082 mutex_lock(&trace_types_lock);
2083 tracer_flags = current_trace->flags->val;
2084 trace_opts = current_trace->flags->opts;
2085
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002086 /*
2087 * Increase the size with names of options specific
2088 * of the current tracer.
2089 */
2090 for (i = 0; trace_opts[i].name; i++) {
2091 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002092 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002093 }
2094
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002095 /* +2 for \n and \0 */
2096 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002097 if (!buf) {
2098 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002099 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002100 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002101
2102 for (i = 0; trace_options[i]; i++) {
2103 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002104 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002105 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002106 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002107 }
2108
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002109 for (i = 0; trace_opts[i].name; i++) {
2110 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002111 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002112 trace_opts[i].name);
2113 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002114 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002115 trace_opts[i].name);
2116 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002117 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002118
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002119 WARN_ON(r >= len + 2);
2120
Ingo Molnar36dfe922008-05-12 21:20:52 +02002121 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002122
2123 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002124 return r;
2125}
2126
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002127/* Try to assign a tracer specific option */
2128static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2129{
2130 struct tracer_flags *trace_flags = trace->flags;
2131 struct tracer_opt *opts = NULL;
2132 int ret = 0, i = 0;
2133 int len;
2134
2135 for (i = 0; trace_flags->opts[i].name; i++) {
2136 opts = &trace_flags->opts[i];
2137 len = strlen(opts->name);
2138
2139 if (strncmp(cmp, opts->name, len) == 0) {
2140 ret = trace->set_flag(trace_flags->val,
2141 opts->bit, !neg);
2142 break;
2143 }
2144 }
2145 /* Not found */
2146 if (!trace_flags->opts[i].name)
2147 return -EINVAL;
2148
2149 /* Refused to handle */
2150 if (ret)
2151 return ret;
2152
2153 if (neg)
2154 trace_flags->val &= ~opts->bit;
2155 else
2156 trace_flags->val |= opts->bit;
2157
2158 return 0;
2159}
2160
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002161static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002162tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002163 size_t cnt, loff_t *ppos)
2164{
2165 char buf[64];
2166 char *cmp = buf;
2167 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002168 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002169 int i;
2170
Steven Rostedtcffae432008-05-12 21:21:00 +02002171 if (cnt >= sizeof(buf))
2172 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002173
2174 if (copy_from_user(&buf, ubuf, cnt))
2175 return -EFAULT;
2176
2177 buf[cnt] = 0;
2178
2179 if (strncmp(buf, "no", 2) == 0) {
2180 neg = 1;
2181 cmp += 2;
2182 }
2183
2184 for (i = 0; trace_options[i]; i++) {
2185 int len = strlen(trace_options[i]);
2186
2187 if (strncmp(cmp, trace_options[i], len) == 0) {
2188 if (neg)
2189 trace_flags &= ~(1 << i);
2190 else
2191 trace_flags |= (1 << i);
2192 break;
2193 }
2194 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002195
2196 /* If no option could be set, test the specific tracer options */
2197 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002198 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002199 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002200 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002201 if (ret)
2202 return ret;
2203 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002204
2205 filp->f_pos += cnt;
2206
2207 return cnt;
2208}
2209
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002210static const struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002211 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002212 .read = tracing_trace_options_read,
2213 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002214};
2215
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002216static const char readme_msg[] =
2217 "tracing mini-HOWTO:\n\n"
2218 "# mkdir /debug\n"
2219 "# mount -t debugfs nodev /debug\n\n"
2220 "# cat /debug/tracing/available_tracers\n"
2221 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2222 "# cat /debug/tracing/current_tracer\n"
2223 "none\n"
2224 "# echo sched_switch > /debug/tracing/current_tracer\n"
2225 "# cat /debug/tracing/current_tracer\n"
2226 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002227 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002228 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002229 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002230 "# echo 1 > /debug/tracing/tracing_enabled\n"
2231 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2232 "echo 0 > /debug/tracing/tracing_enabled\n"
2233;
2234
2235static ssize_t
2236tracing_readme_read(struct file *filp, char __user *ubuf,
2237 size_t cnt, loff_t *ppos)
2238{
2239 return simple_read_from_buffer(ubuf, cnt, ppos,
2240 readme_msg, strlen(readme_msg));
2241}
2242
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002243static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002244 .open = tracing_open_generic,
2245 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002246};
2247
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002248static ssize_t
2249tracing_ctrl_read(struct file *filp, char __user *ubuf,
2250 size_t cnt, loff_t *ppos)
2251{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002252 char buf[64];
2253 int r;
2254
Steven Rostedt90369902008-11-05 16:05:44 -05002255 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002256 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002257}
2258
2259static ssize_t
2260tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2261 size_t cnt, loff_t *ppos)
2262{
2263 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002264 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002265 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002266 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002267
Steven Rostedtcffae432008-05-12 21:21:00 +02002268 if (cnt >= sizeof(buf))
2269 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270
2271 if (copy_from_user(&buf, ubuf, cnt))
2272 return -EFAULT;
2273
2274 buf[cnt] = 0;
2275
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002276 ret = strict_strtoul(buf, 10, &val);
2277 if (ret < 0)
2278 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002279
2280 val = !!val;
2281
2282 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002283 if (tracer_enabled ^ val) {
2284 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002285 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002286 if (current_trace->start)
2287 current_trace->start(tr);
2288 tracing_start();
2289 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002290 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002291 tracing_stop();
2292 if (current_trace->stop)
2293 current_trace->stop(tr);
2294 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002295 }
2296 mutex_unlock(&trace_types_lock);
2297
2298 filp->f_pos += cnt;
2299
2300 return cnt;
2301}
2302
2303static ssize_t
2304tracing_set_trace_read(struct file *filp, char __user *ubuf,
2305 size_t cnt, loff_t *ppos)
2306{
2307 char buf[max_tracer_type_len+2];
2308 int r;
2309
2310 mutex_lock(&trace_types_lock);
2311 if (current_trace)
2312 r = sprintf(buf, "%s\n", current_trace->name);
2313 else
2314 r = sprintf(buf, "\n");
2315 mutex_unlock(&trace_types_lock);
2316
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002317 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002318}
2319
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002320int tracer_init(struct tracer *t, struct trace_array *tr)
2321{
2322 tracing_reset_online_cpus(tr);
2323 return t->init(tr);
2324}
2325
Steven Rostedt73c51622009-03-11 13:42:01 -04002326static int tracing_resize_ring_buffer(unsigned long size)
2327{
2328 int ret;
2329
2330 /*
2331 * If kernel or user changes the size of the ring buffer
2332 * it get completed.
2333 */
2334 ring_buffer_expanded = 1;
2335
2336 ret = ring_buffer_resize(global_trace.buffer, size);
2337 if (ret < 0)
2338 return ret;
2339
2340 ret = ring_buffer_resize(max_tr.buffer, size);
2341 if (ret < 0) {
2342 int r;
2343
2344 r = ring_buffer_resize(global_trace.buffer,
2345 global_trace.entries);
2346 if (r < 0) {
2347 /* AARGH! We are left with different
2348 * size max buffer!!!! */
2349 WARN_ON(1);
2350 tracing_disabled = 1;
2351 }
2352 return ret;
2353 }
2354
2355 global_trace.entries = size;
2356
2357 return ret;
2358}
2359
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002360/**
2361 * tracing_update_buffers - used by tracing facility to expand ring buffers
2362 *
2363 * To save on memory when the tracing is never used on a system with it
2364 * configured in. The ring buffers are set to a minimum size. But once
2365 * a user starts to use the tracing facility, then they need to grow
2366 * to their default size.
2367 *
2368 * This function is to be called when a tracer is about to be used.
2369 */
2370int tracing_update_buffers(void)
2371{
2372 int ret = 0;
2373
2374 if (!ring_buffer_expanded)
2375 ret = tracing_resize_ring_buffer(trace_buf_size);
2376
2377 return ret;
2378}
2379
Steven Rostedt577b7852009-02-26 23:43:05 -05002380struct trace_option_dentry;
2381
2382static struct trace_option_dentry *
2383create_trace_option_files(struct tracer *tracer);
2384
2385static void
2386destroy_trace_option_files(struct trace_option_dentry *topts);
2387
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002388static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002389{
Steven Rostedt577b7852009-02-26 23:43:05 -05002390 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002391 struct trace_array *tr = &global_trace;
2392 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002393 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002394
Steven Rostedt73c51622009-03-11 13:42:01 -04002395 if (!ring_buffer_expanded) {
2396 ret = tracing_resize_ring_buffer(trace_buf_size);
2397 if (ret < 0)
2398 return ret;
2399 ret = 0;
2400 }
2401
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002402 mutex_lock(&trace_types_lock);
2403 for (t = trace_types; t; t = t->next) {
2404 if (strcmp(t->name, buf) == 0)
2405 break;
2406 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002407 if (!t) {
2408 ret = -EINVAL;
2409 goto out;
2410 }
2411 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002412 goto out;
2413
Steven Rostedt9f029e82008-11-12 15:24:24 -05002414 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002415 if (current_trace && current_trace->reset)
2416 current_trace->reset(tr);
2417
Steven Rostedt577b7852009-02-26 23:43:05 -05002418 destroy_trace_option_files(topts);
2419
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002420 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002421
2422 topts = create_trace_option_files(current_trace);
2423
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002424 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002425 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002426 if (ret)
2427 goto out;
2428 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002429
Steven Rostedt9f029e82008-11-12 15:24:24 -05002430 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002431 out:
2432 mutex_unlock(&trace_types_lock);
2433
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002434 return ret;
2435}
2436
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002437static ssize_t
2438tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2439 size_t cnt, loff_t *ppos)
2440{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002441 char buf[max_tracer_type_len+1];
2442 int i;
2443 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002444 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002445
Steven Rostedt60063a62008-10-28 10:44:24 -04002446 ret = cnt;
2447
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002448 if (cnt > max_tracer_type_len)
2449 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002450
2451 if (copy_from_user(&buf, ubuf, cnt))
2452 return -EFAULT;
2453
2454 buf[cnt] = 0;
2455
2456 /* strip ending whitespace. */
2457 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2458 buf[i] = 0;
2459
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002460 err = tracing_set_tracer(buf);
2461 if (err)
2462 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002463
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002464 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002465
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002466 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002467}
2468
2469static ssize_t
2470tracing_max_lat_read(struct file *filp, char __user *ubuf,
2471 size_t cnt, loff_t *ppos)
2472{
2473 unsigned long *ptr = filp->private_data;
2474 char buf[64];
2475 int r;
2476
Steven Rostedtcffae432008-05-12 21:21:00 +02002477 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002478 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002479 if (r > sizeof(buf))
2480 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002481 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002482}
2483
2484static ssize_t
2485tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2486 size_t cnt, loff_t *ppos)
2487{
Hannes Eder5e398412009-02-10 19:44:34 +01002488 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002489 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002490 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002491 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002492
Steven Rostedtcffae432008-05-12 21:21:00 +02002493 if (cnt >= sizeof(buf))
2494 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002495
2496 if (copy_from_user(&buf, ubuf, cnt))
2497 return -EFAULT;
2498
2499 buf[cnt] = 0;
2500
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002501 ret = strict_strtoul(buf, 10, &val);
2502 if (ret < 0)
2503 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002504
2505 *ptr = val * 1000;
2506
2507 return cnt;
2508}
2509
Steven Rostedtb3806b42008-05-12 21:20:46 +02002510static int tracing_open_pipe(struct inode *inode, struct file *filp)
2511{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002512 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002513 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002514 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002515
2516 if (tracing_disabled)
2517 return -ENODEV;
2518
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002519 mutex_lock(&trace_types_lock);
2520
2521 /* We only allow one reader per cpu */
2522 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2523 if (!cpumask_empty(tracing_reader_cpumask)) {
2524 ret = -EBUSY;
2525 goto out;
2526 }
2527 cpumask_setall(tracing_reader_cpumask);
2528 } else {
2529 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2530 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2531 else {
2532 ret = -EBUSY;
2533 goto out;
2534 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002535 }
2536
2537 /* create a buffer to store the information to pass to userspace */
2538 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002539 if (!iter) {
2540 ret = -ENOMEM;
2541 goto out;
2542 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002543
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002544 /*
2545 * We make a copy of the current tracer to avoid concurrent
2546 * changes on it while we are reading.
2547 */
2548 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2549 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002550 ret = -ENOMEM;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002551 goto fail;
2552 }
2553 if (current_trace)
2554 *iter->trace = *current_trace;
2555
2556 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2557 ret = -ENOMEM;
2558 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302559 }
2560
Steven Rostedta3097202008-11-07 22:36:02 -05002561 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302562 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002563
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002564 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002565 iter->tr = &global_trace;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002566 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002567 filp->private_data = iter;
2568
Steven Rostedt107bad82008-05-12 21:21:01 +02002569 if (iter->trace->pipe_open)
2570 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002571
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002572out:
2573 mutex_unlock(&trace_types_lock);
2574 return ret;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002575
2576fail:
2577 kfree(iter->trace);
2578 kfree(iter);
2579 mutex_unlock(&trace_types_lock);
2580 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002581}
2582
2583static int tracing_release_pipe(struct inode *inode, struct file *file)
2584{
2585 struct trace_iterator *iter = file->private_data;
2586
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002587 mutex_lock(&trace_types_lock);
2588
2589 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2590 cpumask_clear(tracing_reader_cpumask);
2591 else
2592 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2593
2594 mutex_unlock(&trace_types_lock);
2595
Rusty Russell44623442009-01-01 10:12:23 +10302596 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002597 mutex_destroy(&iter->mutex);
2598 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002599 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002600
2601 return 0;
2602}
2603
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002604static unsigned int
2605tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2606{
2607 struct trace_iterator *iter = filp->private_data;
2608
2609 if (trace_flags & TRACE_ITER_BLOCK) {
2610 /*
2611 * Always select as readable when in blocking mode
2612 */
2613 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002614 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002615 if (!trace_empty(iter))
2616 return POLLIN | POLLRDNORM;
2617 poll_wait(filp, &trace_wait, poll_table);
2618 if (!trace_empty(iter))
2619 return POLLIN | POLLRDNORM;
2620
2621 return 0;
2622 }
2623}
2624
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002625
2626void default_wait_pipe(struct trace_iterator *iter)
2627{
2628 DEFINE_WAIT(wait);
2629
2630 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2631
2632 if (trace_empty(iter))
2633 schedule();
2634
2635 finish_wait(&trace_wait, &wait);
2636}
2637
2638/*
2639 * This is a make-shift waitqueue.
2640 * A tracer might use this callback on some rare cases:
2641 *
2642 * 1) the current tracer might hold the runqueue lock when it wakes up
2643 * a reader, hence a deadlock (sched, function, and function graph tracers)
2644 * 2) the function tracers, trace all functions, we don't want
2645 * the overhead of calling wake_up and friends
2646 * (and tracing them too)
2647 *
2648 * Anyway, this is really very primitive wakeup.
2649 */
2650void poll_wait_pipe(struct trace_iterator *iter)
2651{
2652 set_current_state(TASK_INTERRUPTIBLE);
2653 /* sleep for 100 msecs, and try again. */
2654 schedule_timeout(HZ / 10);
2655}
2656
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002657/* Must be called with trace_types_lock mutex held. */
2658static int tracing_wait_pipe(struct file *filp)
2659{
2660 struct trace_iterator *iter = filp->private_data;
2661
2662 while (trace_empty(iter)) {
2663
2664 if ((filp->f_flags & O_NONBLOCK)) {
2665 return -EAGAIN;
2666 }
2667
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002668 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002669
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002670 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002671
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002672 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002673
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002674 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002675 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002676
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002677 /*
2678 * We block until we read something and tracing is disabled.
2679 * We still block if tracing is disabled, but we have never
2680 * read anything. This allows a user to cat this file, and
2681 * then enable tracing. But after we have read something,
2682 * we give an EOF when tracing is again disabled.
2683 *
2684 * iter->pos will be 0 if we haven't read anything.
2685 */
2686 if (!tracer_enabled && iter->pos)
2687 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002688 }
2689
2690 return 1;
2691}
2692
Steven Rostedtb3806b42008-05-12 21:20:46 +02002693/*
2694 * Consumer reader.
2695 */
2696static ssize_t
2697tracing_read_pipe(struct file *filp, char __user *ubuf,
2698 size_t cnt, loff_t *ppos)
2699{
2700 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002701 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002702 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002703
2704 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002705 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2706 if (sret != -EBUSY)
2707 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002708
Steven Rostedtf9520752009-03-02 14:04:40 -05002709 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002710
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002711 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002712 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002713 if (unlikely(old_tracer != current_trace && current_trace)) {
2714 old_tracer = current_trace;
2715 *iter->trace = *current_trace;
2716 }
2717 mutex_unlock(&trace_types_lock);
2718
2719 /*
2720 * Avoid more than one consumer on a single file descriptor
2721 * This is just a matter of traces coherency, the ring buffer itself
2722 * is protected.
2723 */
2724 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002725 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002726 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2727 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002728 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002729 }
2730
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002731waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002732 sret = tracing_wait_pipe(filp);
2733 if (sret <= 0)
2734 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002735
2736 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002737 if (trace_empty(iter)) {
2738 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002739 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002740 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002741
2742 if (cnt >= PAGE_SIZE)
2743 cnt = PAGE_SIZE - 1;
2744
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002745 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002746 memset(&iter->seq, 0,
2747 sizeof(struct trace_iterator) -
2748 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002749 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002750
Steven Rostedt088b1e42008-05-12 21:20:48 +02002751 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002752 enum print_line_t ret;
Steven Rostedt088b1e42008-05-12 21:20:48 +02002753 int len = iter->seq.len;
2754
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002755 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002756 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e42008-05-12 21:20:48 +02002757 /* don't print partial lines */
2758 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002759 break;
Steven Rostedt088b1e42008-05-12 21:20:48 +02002760 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002761 if (ret != TRACE_TYPE_NO_CONSUME)
2762 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002763
2764 if (iter->seq.len >= cnt)
2765 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002766 }
2767
Steven Rostedtb3806b42008-05-12 21:20:46 +02002768 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002769 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2770 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002771 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002772
2773 /*
2774 * If there was nothing to send to user, inspite of consuming trace
2775 * entries, go back to wait for more entries.
2776 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002777 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002778 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002779
Steven Rostedt107bad82008-05-12 21:21:01 +02002780out:
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002781 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002782
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002783 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002784}
2785
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002786static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2787 struct pipe_buffer *buf)
2788{
2789 __free_page(buf->page);
2790}
2791
2792static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2793 unsigned int idx)
2794{
2795 __free_page(spd->pages[idx]);
2796}
2797
2798static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002799 .can_merge = 0,
2800 .map = generic_pipe_buf_map,
2801 .unmap = generic_pipe_buf_unmap,
2802 .confirm = generic_pipe_buf_confirm,
2803 .release = tracing_pipe_buf_release,
2804 .steal = generic_pipe_buf_steal,
2805 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002806};
2807
Steven Rostedt34cd4992009-02-09 12:06:29 -05002808static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002809tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002810{
2811 size_t count;
2812 int ret;
2813
2814 /* Seq buffer is page-sized, exactly what we need. */
2815 for (;;) {
2816 count = iter->seq.len;
2817 ret = print_trace_line(iter);
2818 count = iter->seq.len - count;
2819 if (rem < count) {
2820 rem = 0;
2821 iter->seq.len -= count;
2822 break;
2823 }
2824 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2825 iter->seq.len -= count;
2826 break;
2827 }
2828
2829 trace_consume(iter);
2830 rem -= count;
2831 if (!find_next_entry_inc(iter)) {
2832 rem = 0;
2833 iter->ent = NULL;
2834 break;
2835 }
2836 }
2837
2838 return rem;
2839}
2840
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002841static ssize_t tracing_splice_read_pipe(struct file *filp,
2842 loff_t *ppos,
2843 struct pipe_inode_info *pipe,
2844 size_t len,
2845 unsigned int flags)
2846{
2847 struct page *pages[PIPE_BUFFERS];
2848 struct partial_page partial[PIPE_BUFFERS];
2849 struct trace_iterator *iter = filp->private_data;
2850 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002851 .pages = pages,
2852 .partial = partial,
2853 .nr_pages = 0, /* This gets updated below. */
2854 .flags = flags,
2855 .ops = &tracing_pipe_buf_ops,
2856 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002857 };
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002858 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002859 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002860 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002861 unsigned int i;
2862
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002863 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002864 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002865 if (unlikely(old_tracer != current_trace && current_trace)) {
2866 old_tracer = current_trace;
2867 *iter->trace = *current_trace;
2868 }
2869 mutex_unlock(&trace_types_lock);
2870
2871 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002872
2873 if (iter->trace->splice_read) {
2874 ret = iter->trace->splice_read(iter, filp,
2875 ppos, pipe, len, flags);
2876 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002877 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002878 }
2879
2880 ret = tracing_wait_pipe(filp);
2881 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002882 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002883
2884 if (!iter->ent && !find_next_entry_inc(iter)) {
2885 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002886 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002887 }
2888
2889 /* Fill as many pages as possible. */
2890 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2891 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05002892 if (!pages[i])
2893 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002894
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002895 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002896
2897 /* Copy the data into the page, so we can start over. */
2898 ret = trace_seq_to_buffer(&iter->seq,
2899 page_address(pages[i]),
2900 iter->seq.len);
2901 if (ret < 0) {
2902 __free_page(pages[i]);
2903 break;
2904 }
2905 partial[i].offset = 0;
2906 partial[i].len = iter->seq.len;
2907
Steven Rostedtf9520752009-03-02 14:04:40 -05002908 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002909 }
2910
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002911 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002912
2913 spd.nr_pages = i;
2914
2915 return splice_to_pipe(pipe, &spd);
2916
Steven Rostedt34cd4992009-02-09 12:06:29 -05002917out_err:
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002918 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002919
2920 return ret;
2921}
2922
Steven Rostedta98a3c32008-05-12 21:20:59 +02002923static ssize_t
2924tracing_entries_read(struct file *filp, char __user *ubuf,
2925 size_t cnt, loff_t *ppos)
2926{
2927 struct trace_array *tr = filp->private_data;
2928 char buf[64];
2929 int r;
2930
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002931 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002932 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2933}
2934
2935static ssize_t
2936tracing_entries_write(struct file *filp, const char __user *ubuf,
2937 size_t cnt, loff_t *ppos)
2938{
2939 unsigned long val;
2940 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002941 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002942
Steven Rostedtcffae432008-05-12 21:21:00 +02002943 if (cnt >= sizeof(buf))
2944 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002945
2946 if (copy_from_user(&buf, ubuf, cnt))
2947 return -EFAULT;
2948
2949 buf[cnt] = 0;
2950
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002951 ret = strict_strtoul(buf, 10, &val);
2952 if (ret < 0)
2953 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002954
2955 /* must have at least 1 entry */
2956 if (!val)
2957 return -EINVAL;
2958
2959 mutex_lock(&trace_types_lock);
2960
Steven Rostedtc76f0692008-11-07 22:36:02 -05002961 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002962
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002963 /* disable all cpu buffers */
2964 for_each_tracing_cpu(cpu) {
2965 if (global_trace.data[cpu])
2966 atomic_inc(&global_trace.data[cpu]->disabled);
2967 if (max_tr.data[cpu])
2968 atomic_inc(&max_tr.data[cpu]->disabled);
2969 }
2970
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002971 /* value is in KB */
2972 val <<= 10;
2973
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002974 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04002975 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002976 if (ret < 0) {
2977 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002978 goto out;
2979 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02002980 }
2981
2982 filp->f_pos += cnt;
2983
Steven Rostedt19384c02008-05-22 00:22:16 -04002984 /* If check pages failed, return ENOMEM */
2985 if (tracing_disabled)
2986 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002987 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002988 for_each_tracing_cpu(cpu) {
2989 if (global_trace.data[cpu])
2990 atomic_dec(&global_trace.data[cpu]->disabled);
2991 if (max_tr.data[cpu])
2992 atomic_dec(&max_tr.data[cpu]->disabled);
2993 }
2994
Steven Rostedtc76f0692008-11-07 22:36:02 -05002995 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002996 max_tr.entries = global_trace.entries;
2997 mutex_unlock(&trace_types_lock);
2998
2999 return cnt;
3000}
3001
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003002static int mark_printk(const char *fmt, ...)
3003{
3004 int ret;
3005 va_list args;
3006 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003007 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003008 va_end(args);
3009 return ret;
3010}
3011
3012static ssize_t
3013tracing_mark_write(struct file *filp, const char __user *ubuf,
3014 size_t cnt, loff_t *fpos)
3015{
3016 char *buf;
3017 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003018
Steven Rostedtc76f0692008-11-07 22:36:02 -05003019 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003020 return -EINVAL;
3021
3022 if (cnt > TRACE_BUF_SIZE)
3023 cnt = TRACE_BUF_SIZE;
3024
3025 buf = kmalloc(cnt + 1, GFP_KERNEL);
3026 if (buf == NULL)
3027 return -ENOMEM;
3028
3029 if (copy_from_user(buf, ubuf, cnt)) {
3030 kfree(buf);
3031 return -EFAULT;
3032 }
3033
3034 /* Cut from the first nil or newline. */
3035 buf[cnt] = '\0';
3036 end = strchr(buf, '\n');
3037 if (end)
3038 *end = '\0';
3039
3040 cnt = mark_printk("%s\n", buf);
3041 kfree(buf);
3042 *fpos += cnt;
3043
3044 return cnt;
3045}
3046
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003047static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003048 .open = tracing_open_generic,
3049 .read = tracing_max_lat_read,
3050 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003051};
3052
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003053static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003054 .open = tracing_open_generic,
3055 .read = tracing_ctrl_read,
3056 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003057};
3058
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003059static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003060 .open = tracing_open_generic,
3061 .read = tracing_set_trace_read,
3062 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003063};
3064
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003065static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003066 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003067 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003068 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003069 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003070 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003071};
3072
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003073static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003074 .open = tracing_open_generic,
3075 .read = tracing_entries_read,
3076 .write = tracing_entries_write,
3077};
3078
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003079static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003080 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003081 .write = tracing_mark_write,
3082};
3083
Steven Rostedt2cadf912008-12-01 22:20:19 -05003084struct ftrace_buffer_info {
3085 struct trace_array *tr;
3086 void *spare;
3087 int cpu;
3088 unsigned int read;
3089};
3090
3091static int tracing_buffers_open(struct inode *inode, struct file *filp)
3092{
3093 int cpu = (int)(long)inode->i_private;
3094 struct ftrace_buffer_info *info;
3095
3096 if (tracing_disabled)
3097 return -ENODEV;
3098
3099 info = kzalloc(sizeof(*info), GFP_KERNEL);
3100 if (!info)
3101 return -ENOMEM;
3102
3103 info->tr = &global_trace;
3104 info->cpu = cpu;
3105 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3106 /* Force reading ring buffer for first read */
3107 info->read = (unsigned int)-1;
3108 if (!info->spare)
3109 goto out;
3110
3111 filp->private_data = info;
3112
3113 return 0;
3114
3115 out:
3116 kfree(info);
3117 return -ENOMEM;
3118}
3119
3120static ssize_t
3121tracing_buffers_read(struct file *filp, char __user *ubuf,
3122 size_t count, loff_t *ppos)
3123{
3124 struct ftrace_buffer_info *info = filp->private_data;
3125 unsigned int pos;
3126 ssize_t ret;
3127 size_t size;
3128
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003129 if (!count)
3130 return 0;
3131
Steven Rostedt2cadf912008-12-01 22:20:19 -05003132 /* Do we have previous read data to read? */
3133 if (info->read < PAGE_SIZE)
3134 goto read;
3135
3136 info->read = 0;
3137
3138 ret = ring_buffer_read_page(info->tr->buffer,
3139 &info->spare,
3140 count,
3141 info->cpu, 0);
3142 if (ret < 0)
3143 return 0;
3144
3145 pos = ring_buffer_page_len(info->spare);
3146
3147 if (pos < PAGE_SIZE)
3148 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3149
3150read:
3151 size = PAGE_SIZE - info->read;
3152 if (size > count)
3153 size = count;
3154
3155 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003156 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003157 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003158 size -= ret;
3159
Steven Rostedt2cadf912008-12-01 22:20:19 -05003160 *ppos += size;
3161 info->read += size;
3162
3163 return size;
3164}
3165
3166static int tracing_buffers_release(struct inode *inode, struct file *file)
3167{
3168 struct ftrace_buffer_info *info = file->private_data;
3169
3170 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3171 kfree(info);
3172
3173 return 0;
3174}
3175
3176struct buffer_ref {
3177 struct ring_buffer *buffer;
3178 void *page;
3179 int ref;
3180};
3181
3182static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3183 struct pipe_buffer *buf)
3184{
3185 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3186
3187 if (--ref->ref)
3188 return;
3189
3190 ring_buffer_free_read_page(ref->buffer, ref->page);
3191 kfree(ref);
3192 buf->private = 0;
3193}
3194
3195static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3196 struct pipe_buffer *buf)
3197{
3198 return 1;
3199}
3200
3201static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3202 struct pipe_buffer *buf)
3203{
3204 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3205
3206 ref->ref++;
3207}
3208
3209/* Pipe buffer operations for a buffer. */
3210static struct pipe_buf_operations buffer_pipe_buf_ops = {
3211 .can_merge = 0,
3212 .map = generic_pipe_buf_map,
3213 .unmap = generic_pipe_buf_unmap,
3214 .confirm = generic_pipe_buf_confirm,
3215 .release = buffer_pipe_buf_release,
3216 .steal = buffer_pipe_buf_steal,
3217 .get = buffer_pipe_buf_get,
3218};
3219
3220/*
3221 * Callback from splice_to_pipe(), if we need to release some pages
3222 * at the end of the spd in case we error'ed out in filling the pipe.
3223 */
3224static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3225{
3226 struct buffer_ref *ref =
3227 (struct buffer_ref *)spd->partial[i].private;
3228
3229 if (--ref->ref)
3230 return;
3231
3232 ring_buffer_free_read_page(ref->buffer, ref->page);
3233 kfree(ref);
3234 spd->partial[i].private = 0;
3235}
3236
3237static ssize_t
3238tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3239 struct pipe_inode_info *pipe, size_t len,
3240 unsigned int flags)
3241{
3242 struct ftrace_buffer_info *info = file->private_data;
3243 struct partial_page partial[PIPE_BUFFERS];
3244 struct page *pages[PIPE_BUFFERS];
3245 struct splice_pipe_desc spd = {
3246 .pages = pages,
3247 .partial = partial,
3248 .flags = flags,
3249 .ops = &buffer_pipe_buf_ops,
3250 .spd_release = buffer_spd_release,
3251 };
3252 struct buffer_ref *ref;
3253 int size, i;
3254 size_t ret;
3255
3256 /*
3257 * We can't seek on a buffer input
3258 */
3259 if (unlikely(*ppos))
3260 return -ESPIPE;
3261
3262
3263 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3264 struct page *page;
3265 int r;
3266
3267 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3268 if (!ref)
3269 break;
3270
3271 ref->buffer = info->tr->buffer;
3272 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3273 if (!ref->page) {
3274 kfree(ref);
3275 break;
3276 }
3277
3278 r = ring_buffer_read_page(ref->buffer, &ref->page,
3279 len, info->cpu, 0);
3280 if (r < 0) {
3281 ring_buffer_free_read_page(ref->buffer,
3282 ref->page);
3283 kfree(ref);
3284 break;
3285 }
3286
3287 /*
3288 * zero out any left over data, this is going to
3289 * user land.
3290 */
3291 size = ring_buffer_page_len(ref->page);
3292 if (size < PAGE_SIZE)
3293 memset(ref->page + size, 0, PAGE_SIZE - size);
3294
3295 page = virt_to_page(ref->page);
3296
3297 spd.pages[i] = page;
3298 spd.partial[i].len = PAGE_SIZE;
3299 spd.partial[i].offset = 0;
3300 spd.partial[i].private = (unsigned long)ref;
3301 spd.nr_pages++;
3302 }
3303
3304 spd.nr_pages = i;
3305
3306 /* did we read anything? */
3307 if (!spd.nr_pages) {
3308 if (flags & SPLICE_F_NONBLOCK)
3309 ret = -EAGAIN;
3310 else
3311 ret = 0;
3312 /* TODO: block */
3313 return ret;
3314 }
3315
3316 ret = splice_to_pipe(pipe, &spd);
3317
3318 return ret;
3319}
3320
3321static const struct file_operations tracing_buffers_fops = {
3322 .open = tracing_buffers_open,
3323 .read = tracing_buffers_read,
3324 .release = tracing_buffers_release,
3325 .splice_read = tracing_buffers_splice_read,
3326 .llseek = no_llseek,
3327};
3328
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003329#ifdef CONFIG_DYNAMIC_FTRACE
3330
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003331int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003332{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003333 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003334}
3335
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003336static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003337tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003338 size_t cnt, loff_t *ppos)
3339{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003340 static char ftrace_dyn_info_buffer[1024];
3341 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003342 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003343 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003344 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003345 int r;
3346
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003347 mutex_lock(&dyn_info_mutex);
3348 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003349
Steven Rostedta26a2a22008-10-31 00:03:22 -04003350 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003351 buf[r++] = '\n';
3352
3353 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3354
3355 mutex_unlock(&dyn_info_mutex);
3356
3357 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003358}
3359
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003360static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003361 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003362 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003363};
3364#endif
3365
3366static struct dentry *d_tracer;
3367
3368struct dentry *tracing_init_dentry(void)
3369{
3370 static int once;
3371
3372 if (d_tracer)
3373 return d_tracer;
3374
3375 d_tracer = debugfs_create_dir("tracing", NULL);
3376
3377 if (!d_tracer && !once) {
3378 once = 1;
3379 pr_warning("Could not create debugfs directory 'tracing'\n");
3380 return NULL;
3381 }
3382
3383 return d_tracer;
3384}
3385
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003386static struct dentry *d_percpu;
3387
3388struct dentry *tracing_dentry_percpu(void)
3389{
3390 static int once;
3391 struct dentry *d_tracer;
3392
3393 if (d_percpu)
3394 return d_percpu;
3395
3396 d_tracer = tracing_init_dentry();
3397
3398 if (!d_tracer)
3399 return NULL;
3400
3401 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3402
3403 if (!d_percpu && !once) {
3404 once = 1;
3405 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3406 return NULL;
3407 }
3408
3409 return d_percpu;
3410}
3411
3412static void tracing_init_debugfs_percpu(long cpu)
3413{
3414 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003415 struct dentry *entry, *d_cpu;
3416 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3417 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003418
3419 if (cpu > 999 || cpu < 0)
3420 return;
3421
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003422 sprintf(cpu_dir, "cpu%ld", cpu);
3423 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3424 if (!d_cpu) {
3425 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3426 return;
3427 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003428
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003429 /* per cpu trace_pipe */
3430 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003431 (void *) cpu, &tracing_pipe_fops);
3432 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003433 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003434
3435 /* per cpu trace */
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003436 entry = debugfs_create_file("trace", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003437 (void *) cpu, &tracing_fops);
3438 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003439 pr_warning("Could not create debugfs 'trace' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003440}
3441
Steven Rostedt60a11772008-05-12 21:20:44 +02003442#ifdef CONFIG_FTRACE_SELFTEST
3443/* Let selftest have access to static functions in this file */
3444#include "trace_selftest.c"
3445#endif
3446
Steven Rostedt577b7852009-02-26 23:43:05 -05003447struct trace_option_dentry {
3448 struct tracer_opt *opt;
3449 struct tracer_flags *flags;
3450 struct dentry *entry;
3451};
3452
3453static ssize_t
3454trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3455 loff_t *ppos)
3456{
3457 struct trace_option_dentry *topt = filp->private_data;
3458 char *buf;
3459
3460 if (topt->flags->val & topt->opt->bit)
3461 buf = "1\n";
3462 else
3463 buf = "0\n";
3464
3465 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3466}
3467
3468static ssize_t
3469trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3470 loff_t *ppos)
3471{
3472 struct trace_option_dentry *topt = filp->private_data;
3473 unsigned long val;
3474 char buf[64];
3475 int ret;
3476
3477 if (cnt >= sizeof(buf))
3478 return -EINVAL;
3479
3480 if (copy_from_user(&buf, ubuf, cnt))
3481 return -EFAULT;
3482
3483 buf[cnt] = 0;
3484
3485 ret = strict_strtoul(buf, 10, &val);
3486 if (ret < 0)
3487 return ret;
3488
3489 ret = 0;
3490 switch (val) {
3491 case 0:
3492 /* do nothing if already cleared */
3493 if (!(topt->flags->val & topt->opt->bit))
3494 break;
3495
3496 mutex_lock(&trace_types_lock);
3497 if (current_trace->set_flag)
3498 ret = current_trace->set_flag(topt->flags->val,
3499 topt->opt->bit, 0);
3500 mutex_unlock(&trace_types_lock);
3501 if (ret)
3502 return ret;
3503 topt->flags->val &= ~topt->opt->bit;
3504 break;
3505 case 1:
3506 /* do nothing if already set */
3507 if (topt->flags->val & topt->opt->bit)
3508 break;
3509
3510 mutex_lock(&trace_types_lock);
3511 if (current_trace->set_flag)
3512 ret = current_trace->set_flag(topt->flags->val,
3513 topt->opt->bit, 1);
3514 mutex_unlock(&trace_types_lock);
3515 if (ret)
3516 return ret;
3517 topt->flags->val |= topt->opt->bit;
3518 break;
3519
3520 default:
3521 return -EINVAL;
3522 }
3523
3524 *ppos += cnt;
3525
3526 return cnt;
3527}
3528
3529
3530static const struct file_operations trace_options_fops = {
3531 .open = tracing_open_generic,
3532 .read = trace_options_read,
3533 .write = trace_options_write,
3534};
3535
Steven Rostedta8259072009-02-26 22:19:12 -05003536static ssize_t
3537trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3538 loff_t *ppos)
3539{
3540 long index = (long)filp->private_data;
3541 char *buf;
3542
3543 if (trace_flags & (1 << index))
3544 buf = "1\n";
3545 else
3546 buf = "0\n";
3547
3548 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3549}
3550
3551static ssize_t
3552trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3553 loff_t *ppos)
3554{
3555 long index = (long)filp->private_data;
3556 char buf[64];
3557 unsigned long val;
3558 int ret;
3559
3560 if (cnt >= sizeof(buf))
3561 return -EINVAL;
3562
3563 if (copy_from_user(&buf, ubuf, cnt))
3564 return -EFAULT;
3565
3566 buf[cnt] = 0;
3567
3568 ret = strict_strtoul(buf, 10, &val);
3569 if (ret < 0)
3570 return ret;
3571
3572 switch (val) {
3573 case 0:
3574 trace_flags &= ~(1 << index);
3575 break;
3576 case 1:
3577 trace_flags |= 1 << index;
3578 break;
3579
3580 default:
3581 return -EINVAL;
3582 }
3583
3584 *ppos += cnt;
3585
3586 return cnt;
3587}
3588
Steven Rostedta8259072009-02-26 22:19:12 -05003589static const struct file_operations trace_options_core_fops = {
3590 .open = tracing_open_generic,
3591 .read = trace_options_core_read,
3592 .write = trace_options_core_write,
3593};
3594
3595static struct dentry *trace_options_init_dentry(void)
3596{
3597 struct dentry *d_tracer;
3598 static struct dentry *t_options;
3599
3600 if (t_options)
3601 return t_options;
3602
3603 d_tracer = tracing_init_dentry();
3604 if (!d_tracer)
3605 return NULL;
3606
3607 t_options = debugfs_create_dir("options", d_tracer);
3608 if (!t_options) {
3609 pr_warning("Could not create debugfs directory 'options'\n");
3610 return NULL;
3611 }
3612
3613 return t_options;
3614}
3615
Steven Rostedt577b7852009-02-26 23:43:05 -05003616static void
3617create_trace_option_file(struct trace_option_dentry *topt,
3618 struct tracer_flags *flags,
3619 struct tracer_opt *opt)
3620{
3621 struct dentry *t_options;
3622 struct dentry *entry;
3623
3624 t_options = trace_options_init_dentry();
3625 if (!t_options)
3626 return;
3627
3628 topt->flags = flags;
3629 topt->opt = opt;
3630
3631 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3632 &trace_options_fops);
3633
3634 topt->entry = entry;
3635
3636}
3637
3638static struct trace_option_dentry *
3639create_trace_option_files(struct tracer *tracer)
3640{
3641 struct trace_option_dentry *topts;
3642 struct tracer_flags *flags;
3643 struct tracer_opt *opts;
3644 int cnt;
3645
3646 if (!tracer)
3647 return NULL;
3648
3649 flags = tracer->flags;
3650
3651 if (!flags || !flags->opts)
3652 return NULL;
3653
3654 opts = flags->opts;
3655
3656 for (cnt = 0; opts[cnt].name; cnt++)
3657 ;
3658
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003659 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003660 if (!topts)
3661 return NULL;
3662
3663 for (cnt = 0; opts[cnt].name; cnt++)
3664 create_trace_option_file(&topts[cnt], flags,
3665 &opts[cnt]);
3666
3667 return topts;
3668}
3669
3670static void
3671destroy_trace_option_files(struct trace_option_dentry *topts)
3672{
3673 int cnt;
3674
3675 if (!topts)
3676 return;
3677
3678 for (cnt = 0; topts[cnt].opt; cnt++) {
3679 if (topts[cnt].entry)
3680 debugfs_remove(topts[cnt].entry);
3681 }
3682
3683 kfree(topts);
3684}
3685
Steven Rostedta8259072009-02-26 22:19:12 -05003686static struct dentry *
3687create_trace_option_core_file(const char *option, long index)
3688{
3689 struct dentry *t_options;
3690 struct dentry *entry;
3691
3692 t_options = trace_options_init_dentry();
3693 if (!t_options)
3694 return NULL;
3695
3696 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3697 &trace_options_core_fops);
3698
3699 return entry;
3700}
3701
3702static __init void create_trace_options_dir(void)
3703{
3704 struct dentry *t_options;
3705 struct dentry *entry;
3706 int i;
3707
3708 t_options = trace_options_init_dentry();
3709 if (!t_options)
3710 return;
3711
3712 for (i = 0; trace_options[i]; i++) {
3713 entry = create_trace_option_core_file(trace_options[i], i);
3714 if (!entry)
3715 pr_warning("Could not create debugfs %s entry\n",
3716 trace_options[i]);
3717 }
3718}
3719
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003720static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003721{
3722 struct dentry *d_tracer;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003723 struct dentry *buffers;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003724 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003725 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003726
3727 d_tracer = tracing_init_dentry();
3728
3729 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3730 &global_trace, &tracing_ctrl_fops);
3731 if (!entry)
3732 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3733
Steven Rostedtee6bce52008-11-12 17:52:37 -05003734 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003735 NULL, &tracing_iter_fops);
3736 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003737 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003738
Steven Rostedta8259072009-02-26 22:19:12 -05003739 create_trace_options_dir();
3740
Ingo Molnarc7078de2008-05-12 21:20:52 +02003741 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3742 NULL, &tracing_cpumask_fops);
3743 if (!entry)
3744 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3745
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003746 entry = debugfs_create_file("trace", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003747 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003748 if (!entry)
3749 pr_warning("Could not create debugfs 'trace' entry\n");
3750
3751 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3752 &global_trace, &show_traces_fops);
3753 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003754 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003755
3756 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3757 &global_trace, &set_tracer_fops);
3758 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003759 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003760
3761 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3762 &tracing_max_latency,
3763 &tracing_max_lat_fops);
3764 if (!entry)
3765 pr_warning("Could not create debugfs "
3766 "'tracing_max_latency' entry\n");
3767
3768 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3769 &tracing_thresh, &tracing_max_lat_fops);
3770 if (!entry)
3771 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003772 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003773 entry = debugfs_create_file("README", 0644, d_tracer,
3774 NULL, &tracing_readme_fops);
3775 if (!entry)
3776 pr_warning("Could not create debugfs 'README' entry\n");
3777
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003778 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3779 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003780 if (!entry)
3781 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003782 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003783
Steven Rostedta94c80e2008-11-12 17:52:36 -05003784 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003785 &global_trace, &tracing_entries_fops);
3786 if (!entry)
3787 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003788 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003789
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003790 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3791 NULL, &tracing_mark_fops);
3792 if (!entry)
3793 pr_warning("Could not create debugfs "
3794 "'trace_marker' entry\n");
3795
Steven Rostedt2cadf912008-12-01 22:20:19 -05003796 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3797
3798 if (!buffers)
3799 pr_warning("Could not create buffers directory\n");
3800 else {
3801 int cpu;
3802 char buf[64];
3803
3804 for_each_tracing_cpu(cpu) {
3805 sprintf(buf, "%d", cpu);
3806
3807 entry = debugfs_create_file(buf, 0444, buffers,
3808 (void *)(long)cpu,
3809 &tracing_buffers_fops);
3810 if (!entry)
3811 pr_warning("Could not create debugfs buffers "
3812 "'%s' entry\n", buf);
3813 }
3814 }
3815
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003816#ifdef CONFIG_DYNAMIC_FTRACE
3817 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3818 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003819 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003820 if (!entry)
3821 pr_warning("Could not create debugfs "
3822 "'dyn_ftrace_total_info' entry\n");
3823#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003824#ifdef CONFIG_SYSPROF_TRACER
3825 init_tracer_sysprof_debugfs(d_tracer);
3826#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003827
3828 for_each_tracing_cpu(cpu)
3829 tracing_init_debugfs_percpu(cpu);
3830
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003831 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003832}
3833
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003834static int trace_panic_handler(struct notifier_block *this,
3835 unsigned long event, void *unused)
3836{
Steven Rostedt944ac422008-10-23 19:26:08 -04003837 if (ftrace_dump_on_oops)
3838 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003839 return NOTIFY_OK;
3840}
3841
3842static struct notifier_block trace_panic_notifier = {
3843 .notifier_call = trace_panic_handler,
3844 .next = NULL,
3845 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3846};
3847
3848static int trace_die_handler(struct notifier_block *self,
3849 unsigned long val,
3850 void *data)
3851{
3852 switch (val) {
3853 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003854 if (ftrace_dump_on_oops)
3855 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003856 break;
3857 default:
3858 break;
3859 }
3860 return NOTIFY_OK;
3861}
3862
3863static struct notifier_block trace_die_notifier = {
3864 .notifier_call = trace_die_handler,
3865 .priority = 200
3866};
3867
3868/*
3869 * printk is set to max of 1024, we really don't need it that big.
3870 * Nothing should be printing 1000 characters anyway.
3871 */
3872#define TRACE_MAX_PRINT 1000
3873
3874/*
3875 * Define here KERN_TRACE so that we have one place to modify
3876 * it if we decide to change what log level the ftrace dump
3877 * should be at.
3878 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003879#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003880
3881static void
3882trace_printk_seq(struct trace_seq *s)
3883{
3884 /* Probably should print a warning here. */
3885 if (s->len >= 1000)
3886 s->len = 1000;
3887
3888 /* should be zero ended, but we are paranoid. */
3889 s->buffer[s->len] = 0;
3890
3891 printk(KERN_TRACE "%s", s->buffer);
3892
Steven Rostedtf9520752009-03-02 14:04:40 -05003893 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003894}
3895
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003896void ftrace_dump(void)
3897{
3898 static DEFINE_SPINLOCK(ftrace_dump_lock);
3899 /* use static because iter can be a bit big for the stack */
3900 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003901 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003902 unsigned long flags;
3903 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003904
3905 /* only one dump */
3906 spin_lock_irqsave(&ftrace_dump_lock, flags);
3907 if (dump_ran)
3908 goto out;
3909
3910 dump_ran = 1;
3911
3912 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05003913 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003914 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003915
Steven Rostedtd7690412008-10-01 00:29:53 -04003916 for_each_tracing_cpu(cpu) {
3917 atomic_inc(&global_trace.data[cpu]->disabled);
3918 }
3919
Török Edwinb54d3de2008-11-22 13:28:48 +02003920 /* don't look at user memory in panic mode */
3921 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3922
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003923 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3924
Steven Rostedte543ad72009-03-04 18:20:36 -05003925 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003926 iter.tr = &global_trace;
3927 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05003928 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003929
3930 /*
3931 * We need to stop all tracing on all CPUS to read the
3932 * the next buffer. This is a bit expensive, but is
3933 * not done often. We fill all what we can read,
3934 * and then release the locks again.
3935 */
3936
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003937 while (!trace_empty(&iter)) {
3938
3939 if (!cnt)
3940 printk(KERN_TRACE "---------------------------------\n");
3941
3942 cnt++;
3943
3944 /* reset all but tr, trace, and overruns */
3945 memset(&iter.seq, 0,
3946 sizeof(struct trace_iterator) -
3947 offsetof(struct trace_iterator, seq));
3948 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3949 iter.pos = -1;
3950
3951 if (find_next_entry_inc(&iter) != NULL) {
3952 print_trace_line(&iter);
3953 trace_consume(&iter);
3954 }
3955
3956 trace_printk_seq(&iter.seq);
3957 }
3958
3959 if (!cnt)
3960 printk(KERN_TRACE " (ftrace buffer empty)\n");
3961 else
3962 printk(KERN_TRACE "---------------------------------\n");
3963
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003964 out:
3965 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3966}
3967
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003968__init static int tracer_alloc_buffers(void)
3969{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003970 struct trace_array_cpu *data;
Steven Rostedt73c51622009-03-11 13:42:01 -04003971 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003972 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303973 int ret = -ENOMEM;
3974
3975 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3976 goto out;
3977
3978 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3979 goto out_free_buffer_mask;
3980
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003981 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3982 goto out_free_tracing_cpumask;
3983
Steven Rostedt73c51622009-03-11 13:42:01 -04003984 /* To save memory, keep the ring buffer size to its minimum */
3985 if (ring_buffer_expanded)
3986 ring_buf_size = trace_buf_size;
3987 else
3988 ring_buf_size = 1;
3989
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303990 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3991 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003992 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003993
Steven Rostedtab464282008-05-12 21:21:00 +02003994 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04003995 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003996 TRACE_BUFFER_FLAGS);
3997 if (!global_trace.buffer) {
3998 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3999 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304000 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004001 }
4002 global_trace.entries = ring_buffer_size(global_trace.buffer);
4003
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304004
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004005#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt73c51622009-03-11 13:42:01 -04004006 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004007 TRACE_BUFFER_FLAGS);
4008 if (!max_tr.buffer) {
4009 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4010 WARN_ON(1);
4011 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304012 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004013 }
4014 max_tr.entries = ring_buffer_size(max_tr.buffer);
4015 WARN_ON(max_tr.entries != global_trace.entries);
4016#endif
4017
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004018 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004019 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004020 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004021 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004022 }
4023
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004024 trace_init_cmdlines();
4025
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004026 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004027 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004028#ifdef CONFIG_BOOT_TRACER
4029 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004030#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004031 /* All seems OK, enable tracing */
4032 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004033
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004034 atomic_notifier_chain_register(&panic_notifier_list,
4035 &trace_panic_notifier);
4036
4037 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304038 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004039
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304040out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004041 free_cpumask_var(tracing_reader_cpumask);
4042out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304043 free_cpumask_var(tracing_cpumask);
4044out_free_buffer_mask:
4045 free_cpumask_var(tracing_buffer_mask);
4046out:
4047 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004048}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004049
4050__init static int clear_boot_tracer(void)
4051{
4052 /*
4053 * The default tracer at boot buffer is an init section.
4054 * This function is called in lateinit. If we did not
4055 * find the boot tracer, then clear it out, to prevent
4056 * later registration from accessing the buffer that is
4057 * about to be freed.
4058 */
4059 if (!default_bootup_tracer)
4060 return 0;
4061
4062 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4063 default_bootup_tracer);
4064 default_bootup_tracer = NULL;
4065
4066 return 0;
4067}
4068
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004069early_initcall(tracer_alloc_buffers);
4070fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004071late_initcall(clear_boot_tracer);