blob: 484337d339590c817314a4ac86cb64654fca7928 [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>
Sam Ravnborg273b2812009-10-18 00:52:28 +020015#include <generated/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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040020#include <linux/smp_lock.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040021#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050022#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020023#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020024#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020025#include <linux/hardirq.h>
26#include <linux/linkage.h>
27#include <linux/uaccess.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050028#include <linux/kprobes.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020029#include <linux/ftrace.h>
30#include <linux/module.h>
31#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050032#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040033#include <linux/kdebug.h>
Frederic Weisbecker5f0c6c02009-03-27 14:22:10 +010034#include <linux/string.h>
Lai Jiangshan7e53bd42010-01-06 20:08:50 +080035#include <linux/rwsem.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020036#include <linux/ctype.h>
37#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020038#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020039#include <linux/gfp.h>
40#include <linux/fs.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020041
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020042#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050043#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020044
Steven Rostedt3928a8a2008-09-29 23:02:41 -040045#define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
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 */
Li Zefan020e5f82009-07-01 10:47:05 +080051int ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040052
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 */
Li Zefan020e5f82009-07-01 10:47:05 +080065bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050066
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
Christoph Lameter9288f992009-10-07 19:17:45 -040090DEFINE_PER_CPU(int, ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040091
92static inline void ftrace_disable_cpu(void)
93{
94 preempt_disable();
Christoph Lameter9288f992009-10-07 19:17:45 -040095 __this_cpu_inc(per_cpu_var(ftrace_cpu_disabled));
Steven Rostedtd7690412008-10-01 00:29:53 -040096}
97
98static inline void ftrace_enable_cpu(void)
99{
Christoph Lameter9288f992009-10-07 19:17:45 -0400100 __this_cpu_dec(per_cpu_var(ftrace_cpu_disabled));
Steven Rostedtd7690412008-10-01 00:29:53 -0400101 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
106#define for_each_tracing_cpu(cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030107 for_each_cpu(cpu, tracing_buffer_mask)
Steven Rostedtab464282008-05-12 21:21:00 +0200108
Steven Rostedt944ac422008-10-23 19:26:08 -0400109/*
110 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
111 *
112 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
113 * is set, then ftrace_dump is called. This will output the contents
114 * of the ftrace buffers to the console. This is very useful for
115 * capturing traces that lead to crashes and outputing it to a
116 * serial console.
117 *
118 * It is default off, but you can enable it with either specifying
119 * "ftrace_dump_on_oops" in the kernel command line, or setting
120 * /proc/sys/kernel/ftrace_dump_on_oops to true.
121 */
122int ftrace_dump_on_oops;
123
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500124static int tracing_set_tracer(const char *buf);
125
Li Zefanee6c2c12009-09-18 14:06:47 +0800126#define MAX_TRACER_SIZE 100
127static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500128static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100129
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200130static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100131{
Li Zefanee6c2c12009-09-18 14:06:47 +0800132 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500133 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400134 /* We are using ftrace early, expand it */
135 ring_buffer_expanded = 1;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100136 return 1;
137}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200138__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100139
Steven Rostedt944ac422008-10-23 19:26:08 -0400140static int __init set_ftrace_dump_on_oops(char *str)
141{
142 ftrace_dump_on_oops = 1;
143 return 1;
144}
145__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200146
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800147unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200148{
149 nsec += 500;
150 do_div(nsec, 1000);
151 return nsec;
152}
153
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200154/*
155 * The global_trace is the descriptor that holds the tracing
156 * buffers for the live tracing. For each CPU, it contains
157 * a link list of pages that will store trace entries. The
158 * page descriptor of the pages in the memory is used to hold
159 * the link list by linking the lru item in the page descriptor
160 * to each of the pages in the buffer per CPU.
161 *
162 * For each active CPU there is a data field that holds the
163 * pages for the buffer for that CPU. Each CPU has the same number
164 * of pages allocated for its buffer.
165 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200166static struct trace_array global_trace;
167
168static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
169
Steven Rostedte77405a2009-09-02 14:17:06 -0400170int filter_current_check_discard(struct ring_buffer *buffer,
171 struct ftrace_event_call *call, void *rec,
Tom Zanussieb02ce02009-04-08 03:15:54 -0500172 struct ring_buffer_event *event)
173{
Steven Rostedte77405a2009-09-02 14:17:06 -0400174 return filter_check_discard(call, rec, buffer, event);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500175}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400176EXPORT_SYMBOL_GPL(filter_current_check_discard);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500177
Steven Rostedt37886f62009-03-17 17:22:06 -0400178cycle_t ftrace_now(int cpu)
179{
180 u64 ts;
181
182 /* Early boot up does not have a buffer yet */
183 if (!global_trace.buffer)
184 return trace_clock_local();
185
186 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
187 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
188
189 return ts;
190}
191
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200192/*
193 * The max_tr is used to snapshot the global_trace when a maximum
194 * latency is reached. Some tracers will use this to store a maximum
195 * trace while it continues examining live traces.
196 *
197 * The buffers for the max_tr are set up the same as the global_trace.
198 * When a snapshot is taken, the link list of the max_tr is swapped
199 * with the link list of the global_trace and the buffers are reset for
200 * the global_trace so the tracing can continue.
201 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200202static struct trace_array max_tr;
203
Tejun Heo9705f692009-10-29 22:34:13 +0900204static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200205
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200206/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200207static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200208
Steven Rostedt90369902008-11-05 16:05:44 -0500209/**
210 * tracing_is_enabled - return tracer_enabled status
211 *
212 * This function is used by other tracers to know the status
213 * of the tracer_enabled flag. Tracers may use this function
214 * to know if it should enable their features when starting
215 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
216 */
217int tracing_is_enabled(void)
218{
219 return tracer_enabled;
220}
221
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200222/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400223 * trace_buf_size is the size in bytes that is allocated
224 * for a buffer. Note, the number of bytes is always rounded
225 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400226 *
227 * This number is purposely set to a low number of 16384.
228 * If the dump on oops happens, it will be much appreciated
229 * to not have to wait for all that output. Anyway this can be
230 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200231 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400232#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400233
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400234static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200235
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200236/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200237static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200238
239/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200240static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200241
242/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200243 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200244 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200245static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200246
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800247/*
248 * serialize the access of the ring buffer
249 *
250 * ring buffer serializes readers, but it is low level protection.
251 * The validity of the events (which returns by ring_buffer_peek() ..etc)
252 * are not protected by ring buffer.
253 *
254 * The content of events may become garbage if we allow other process consumes
255 * these events concurrently:
256 * A) the page of the consumed events may become a normal page
257 * (not reader page) in ring buffer, and this page will be rewrited
258 * by events producer.
259 * B) The page of the consumed events may become a page for splice_read,
260 * and this page will be returned to system.
261 *
262 * These primitives allow multi process access to different cpu ring buffer
263 * concurrently.
264 *
265 * These primitives don't distinguish read-only and read-consume access.
266 * Multi read-only access are also serialized.
267 */
268
269#ifdef CONFIG_SMP
270static DECLARE_RWSEM(all_cpu_access_lock);
271static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
272
273static inline void trace_access_lock(int cpu)
274{
275 if (cpu == TRACE_PIPE_ALL_CPU) {
276 /* gain it for accessing the whole ring buffer. */
277 down_write(&all_cpu_access_lock);
278 } else {
279 /* gain it for accessing a cpu ring buffer. */
280
281 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
282 down_read(&all_cpu_access_lock);
283
284 /* Secondly block other access to this @cpu ring buffer. */
285 mutex_lock(&per_cpu(cpu_access_lock, cpu));
286 }
287}
288
289static inline void trace_access_unlock(int cpu)
290{
291 if (cpu == TRACE_PIPE_ALL_CPU) {
292 up_write(&all_cpu_access_lock);
293 } else {
294 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
295 up_read(&all_cpu_access_lock);
296 }
297}
298
299static inline void trace_access_lock_init(void)
300{
301 int cpu;
302
303 for_each_possible_cpu(cpu)
304 mutex_init(&per_cpu(cpu_access_lock, cpu));
305}
306
307#else
308
309static DEFINE_MUTEX(access_lock);
310
311static inline void trace_access_lock(int cpu)
312{
313 (void)cpu;
314 mutex_lock(&access_lock);
315}
316
317static inline void trace_access_unlock(int cpu)
318{
319 (void)cpu;
320 mutex_unlock(&access_lock);
321}
322
323static inline void trace_access_lock_init(void)
324{
325}
326
327#endif
328
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200329/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200330static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
331
Steven Rostedtee6bce52008-11-12 17:52:37 -0500332/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500333unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400334 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
335 TRACE_ITER_GRAPH_TIME;
Ingo Molnar4e655512008-05-12 21:20:52 +0200336
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400337static int trace_stop_count;
338static DEFINE_SPINLOCK(tracing_start_lock);
339
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200340/**
341 * trace_wake_up - wake up tasks waiting for trace input
342 *
343 * Simply wakes up any task that is blocked on the trace_wait
344 * queue. These is used with trace_poll for tasks polling the trace.
345 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200346void trace_wake_up(void)
347{
Andrew Morton89f19f02009-09-19 11:55:44 -0700348 int cpu;
349
350 if (trace_flags & TRACE_ITER_BLOCK)
351 return;
Ingo Molnar017730c2008-05-12 21:20:52 +0200352 /*
353 * The runqueue_is_locked() can fail, but this is the best we
354 * have for now:
355 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700356 cpu = get_cpu();
357 if (!runqueue_is_locked(cpu))
Ingo Molnar4e655512008-05-12 21:20:52 +0200358 wake_up(&trace_wait);
Andrew Morton89f19f02009-09-19 11:55:44 -0700359 put_cpu();
Ingo Molnar4e655512008-05-12 21:20:52 +0200360}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200361
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400362static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200363{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400364 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200365
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200366 if (!str)
367 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800368 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200369 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800370 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200371 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400372 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200373 return 1;
374}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400375__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200376
Tim Bird0e950172010-02-25 15:36:43 -0800377static int __init set_tracing_thresh(char *str)
378{
379 unsigned long threshhold;
380 int ret;
381
382 if (!str)
383 return 0;
384 ret = strict_strtoul(str, 0, &threshhold);
385 if (ret < 0)
386 return 0;
387 tracing_thresh = threshhold * 1000;
388 return 1;
389}
390__setup("tracing_thresh=", set_tracing_thresh);
391
Steven Rostedt57f50be2008-05-12 21:20:44 +0200392unsigned long nsecs_to_usecs(unsigned long nsecs)
393{
394 return nsecs / 1000;
395}
396
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200397/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200398static const char *trace_options[] = {
399 "print-parent",
400 "sym-offset",
401 "sym-addr",
402 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200403 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200404 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200405 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200406 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200407 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100408 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500409 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500410 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500411 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200412 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200413 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100414 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200415 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500416 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400417 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400418 "graph-time",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200419 NULL
420};
421
Zhaolei5079f322009-08-25 16:12:56 +0800422static struct {
423 u64 (*func)(void);
424 const char *name;
425} trace_clocks[] = {
426 { trace_clock_local, "local" },
427 { trace_clock_global, "global" },
428};
429
430int trace_clock_id;
431
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200432/*
433 * trace_parser_get_init - gets the buffer for trace parser
434 */
435int trace_parser_get_init(struct trace_parser *parser, int size)
436{
437 memset(parser, 0, sizeof(*parser));
438
439 parser->buffer = kmalloc(size, GFP_KERNEL);
440 if (!parser->buffer)
441 return 1;
442
443 parser->size = size;
444 return 0;
445}
446
447/*
448 * trace_parser_put - frees the buffer for trace parser
449 */
450void trace_parser_put(struct trace_parser *parser)
451{
452 kfree(parser->buffer);
453}
454
455/*
456 * trace_get_user - reads the user input string separated by space
457 * (matched by isspace(ch))
458 *
459 * For each string found the 'struct trace_parser' is updated,
460 * and the function returns.
461 *
462 * Returns number of bytes read.
463 *
464 * See kernel/trace/trace.h for 'struct trace_parser' details.
465 */
466int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
467 size_t cnt, loff_t *ppos)
468{
469 char ch;
470 size_t read = 0;
471 ssize_t ret;
472
473 if (!*ppos)
474 trace_parser_clear(parser);
475
476 ret = get_user(ch, ubuf++);
477 if (ret)
478 goto out;
479
480 read++;
481 cnt--;
482
483 /*
484 * The parser is not finished with the last write,
485 * continue reading the user input without skipping spaces.
486 */
487 if (!parser->cont) {
488 /* skip white space */
489 while (cnt && isspace(ch)) {
490 ret = get_user(ch, ubuf++);
491 if (ret)
492 goto out;
493 read++;
494 cnt--;
495 }
496
497 /* only spaces were written */
498 if (isspace(ch)) {
499 *ppos += read;
500 ret = read;
501 goto out;
502 }
503
504 parser->idx = 0;
505 }
506
507 /* read the non-space input */
508 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800509 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200510 parser->buffer[parser->idx++] = ch;
511 else {
512 ret = -EINVAL;
513 goto out;
514 }
515 ret = get_user(ch, ubuf++);
516 if (ret)
517 goto out;
518 read++;
519 cnt--;
520 }
521
522 /* We either got finished input or we have to wait for another call. */
523 if (isspace(ch)) {
524 parser->buffer[parser->idx] = 0;
525 parser->cont = false;
526 } else {
527 parser->cont = true;
528 parser->buffer[parser->idx++] = ch;
529 }
530
531 *ppos += read;
532 ret = read;
533
534out:
535 return ret;
536}
537
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200538ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
539{
540 int len;
541 int ret;
542
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500543 if (!cnt)
544 return 0;
545
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200546 if (s->len <= s->readpos)
547 return -EBUSY;
548
549 len = s->len - s->readpos;
550 if (cnt > len)
551 cnt = len;
552 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500553 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200554 return -EFAULT;
555
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500556 cnt -= ret;
557
Steven Rostedte74da522009-03-04 20:31:11 -0500558 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200559 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200560}
561
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200562static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200563{
564 int len;
565 void *ret;
566
567 if (s->len <= s->readpos)
568 return -EBUSY;
569
570 len = s->len - s->readpos;
571 if (cnt > len)
572 cnt = len;
573 ret = memcpy(buf, s->buffer + s->readpos, cnt);
574 if (!ret)
575 return -EFAULT;
576
Steven Rostedte74da522009-03-04 20:31:11 -0500577 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200578 return cnt;
579}
580
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400581/*
582 * ftrace_max_lock is used to protect the swapping of buffers
583 * when taking a max snapshot. The buffers themselves are
584 * protected by per_cpu spinlocks. But the action of the swap
585 * needs its own lock.
586 *
Thomas Gleixner445c8952009-12-02 19:49:50 +0100587 * This is defined as a arch_spinlock_t in order to help
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400588 * with performance when lockdep debugging is enabled.
589 *
590 * It is also used in other places outside the update_max_tr
591 * so it needs to be defined outside of the
592 * CONFIG_TRACER_MAX_TRACE.
593 */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100594static arch_spinlock_t ftrace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100595 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400596
Tim Bird0e950172010-02-25 15:36:43 -0800597unsigned long __read_mostly tracing_thresh;
598
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400599#ifdef CONFIG_TRACER_MAX_TRACE
600unsigned long __read_mostly tracing_max_latency;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400601
602/*
603 * Copy the new maximum trace into the separate maximum-trace
604 * structure. (this way the maximum trace is permanently saved,
605 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
606 */
607static void
608__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
609{
610 struct trace_array_cpu *data = tr->data[cpu];
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300611 struct trace_array_cpu *max_data;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400612
613 max_tr.cpu = cpu;
614 max_tr.time_start = data->preempt_timestamp;
615
Steven Rostedt8248ac02009-09-02 12:27:41 -0400616 max_data = max_tr.data[cpu];
617 max_data->saved_latency = tracing_max_latency;
618 max_data->critical_start = data->critical_start;
619 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400620
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300621 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -0400622 max_data->pid = tsk->pid;
623 max_data->uid = task_uid(tsk);
624 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
625 max_data->policy = tsk->policy;
626 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400627
628 /* record this tasks comm */
629 tracing_record_cmdline(tsk);
630}
631
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200632/**
633 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
634 * @tr: tracer
635 * @tsk: the task with the latency
636 * @cpu: The cpu that initiated the trace.
637 *
638 * Flip the buffers between the @tr and the max_tr and record information
639 * about which task was the cause of this latency.
640 */
Ingo Molnare309b412008-05-12 21:20:51 +0200641void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200642update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
643{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400644 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200645
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400646 if (trace_stop_count)
647 return;
648
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200649 WARN_ON_ONCE(!irqs_disabled());
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100650 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400651
652 tr->buffer = max_tr.buffer;
653 max_tr.buffer = buf;
654
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200655 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100656 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200657}
658
659/**
660 * update_max_tr_single - only copy one trace over, and reset the rest
661 * @tr - tracer
662 * @tsk - task with the latency
663 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200664 *
665 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200666 */
Ingo Molnare309b412008-05-12 21:20:51 +0200667void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200668update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
669{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400670 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200671
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400672 if (trace_stop_count)
673 return;
674
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200675 WARN_ON_ONCE(!irqs_disabled());
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100676 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200677
Steven Rostedtd7690412008-10-01 00:29:53 -0400678 ftrace_disable_cpu();
679
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400680 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
681
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400682 if (ret == -EBUSY) {
683 /*
684 * We failed to swap the buffer due to a commit taking
685 * place on this CPU. We fail to record, but we reset
686 * the max trace buffer (no one writes directly to it)
687 * and flag that it failed.
688 */
689 trace_array_printk(&max_tr, _THIS_IP_,
690 "Failed to swap buffers due to commit in progress\n");
691 }
692
Steven Rostedtd7690412008-10-01 00:29:53 -0400693 ftrace_enable_cpu();
694
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400695 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200696
697 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100698 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200699}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400700#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200701
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200702/**
703 * register_tracer - register a tracer with the ftrace system.
704 * @type - the plugin for the tracer
705 *
706 * Register a new plugin tracer.
707 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200708int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100709__releases(kernel_lock)
710__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200711{
712 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200713 int ret = 0;
714
715 if (!type->name) {
716 pr_info("Tracer must have a name\n");
717 return -1;
718 }
719
Li Zefanee6c2c12009-09-18 14:06:47 +0800720 if (strlen(type->name) > MAX_TRACER_SIZE) {
721 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
722 return -1;
723 }
724
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100725 /*
726 * When this gets called we hold the BKL which means that
727 * preemption is disabled. Various trace selftests however
728 * need to disable and enable preemption for successful tests.
729 * So we drop the BKL here and grab it after the tests again.
730 */
731 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200732 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100733
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100734 tracing_selftest_running = true;
735
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200736 for (t = trace_types; t; t = t->next) {
737 if (strcmp(type->name, t->name) == 0) {
738 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +0800739 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200740 type->name);
741 ret = -1;
742 goto out;
743 }
744 }
745
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100746 if (!type->set_flag)
747 type->set_flag = &dummy_set_flag;
748 if (!type->flags)
749 type->flags = &dummy_tracer_flags;
750 else
751 if (!type->flags->opts)
752 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100753 if (!type->wait_pipe)
754 type->wait_pipe = default_wait_pipe;
755
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100756
Steven Rostedt60a11772008-05-12 21:20:44 +0200757#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500758 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200759 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200760 struct trace_array *tr = &global_trace;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100761
Steven Rostedt60a11772008-05-12 21:20:44 +0200762 /*
763 * Run a selftest on this tracer.
764 * Here we reset the trace buffer, and set the current
765 * tracer to be this tracer. The tracer can then run some
766 * internal tracing to verify that everything is in order.
767 * If we fail, we do not register this tracer.
768 */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400769 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100770
Steven Rostedt60a11772008-05-12 21:20:44 +0200771 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200772 /* the test is responsible for initializing and enabling */
773 pr_info("Testing tracer %s: ", type->name);
774 ret = type->selftest(type, tr);
775 /* the test is responsible for resetting too */
776 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200777 if (ret) {
778 printk(KERN_CONT "FAILED!\n");
779 goto out;
780 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200781 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400782 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100783
Steven Rostedt60a11772008-05-12 21:20:44 +0200784 printk(KERN_CONT "PASSED\n");
785 }
786#endif
787
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200788 type->next = trace_types;
789 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200790
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200791 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100792 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793 mutex_unlock(&trace_types_lock);
794
Steven Rostedtdac74942009-02-05 01:13:38 -0500795 if (ret || !default_bootup_tracer)
796 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500797
Li Zefanee6c2c12009-09-18 14:06:47 +0800798 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -0500799 goto out_unlock;
800
801 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
802 /* Do we want this tracer to start on bootup? */
803 tracing_set_tracer(type->name);
804 default_bootup_tracer = NULL;
805 /* disable other selftests, since this will break it. */
806 tracing_selftest_disabled = 1;
807#ifdef CONFIG_FTRACE_STARTUP_TEST
808 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
809 type->name);
810#endif
811
812 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500813 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200814 return ret;
815}
816
817void unregister_tracer(struct tracer *type)
818{
819 struct tracer **t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200820
821 mutex_lock(&trace_types_lock);
822 for (t = &trace_types; *t; t = &(*t)->next) {
823 if (*t == type)
824 goto found;
825 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800826 pr_info("Tracer %s not registered\n", type->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200827 goto out;
828
829 found:
830 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200831
832 if (type == current_trace && tracer_enabled) {
833 tracer_enabled = 0;
834 tracing_stop();
835 if (current_trace->stop)
836 current_trace->stop(&global_trace);
837 current_trace = &nop_trace;
838 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800839out:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200840 mutex_unlock(&trace_types_lock);
841}
842
Steven Rostedt283740c2010-03-12 19:48:41 -0500843static void __tracing_reset(struct ring_buffer *buffer, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200844{
Steven Rostedtd7690412008-10-01 00:29:53 -0400845 ftrace_disable_cpu();
Steven Rostedt283740c2010-03-12 19:48:41 -0500846 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400847 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200848}
849
Steven Rostedtf6339032009-09-04 12:35:16 -0400850void tracing_reset(struct trace_array *tr, int cpu)
851{
852 struct ring_buffer *buffer = tr->buffer;
853
854 ring_buffer_record_disable(buffer);
855
856 /* Make sure all commits have finished */
857 synchronize_sched();
Steven Rostedt283740c2010-03-12 19:48:41 -0500858 __tracing_reset(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -0400859
860 ring_buffer_record_enable(buffer);
861}
862
Pekka J Enberg213cc062008-12-19 12:08:39 +0200863void tracing_reset_online_cpus(struct trace_array *tr)
864{
Steven Rostedt621968c2009-09-04 12:02:35 -0400865 struct ring_buffer *buffer = tr->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +0200866 int cpu;
867
Steven Rostedt621968c2009-09-04 12:02:35 -0400868 ring_buffer_record_disable(buffer);
869
870 /* Make sure all commits have finished */
871 synchronize_sched();
872
Pekka J Enberg213cc062008-12-19 12:08:39 +0200873 tr->time_start = ftrace_now(tr->cpu);
874
875 for_each_online_cpu(cpu)
Steven Rostedt283740c2010-03-12 19:48:41 -0500876 __tracing_reset(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -0400877
878 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +0200879}
880
Steven Rostedt9456f0f2009-05-06 21:54:09 -0400881void tracing_reset_current(int cpu)
882{
883 tracing_reset(&global_trace, cpu);
884}
885
886void tracing_reset_current_online_cpus(void)
887{
888 tracing_reset_online_cpus(&global_trace);
889}
890
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200891#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100892#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200893static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
894static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
895static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
896static int cmdline_idx;
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100897static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200898
Steven Rostedt25b0b442008-05-12 21:21:00 +0200899/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100900static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200901
902static void trace_init_cmdlines(void)
903{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100904 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
905 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200906 cmdline_idx = 0;
907}
908
Carsten Emdeb5130b12009-09-13 01:43:07 +0200909int is_tracing_stopped(void)
910{
911 return trace_stop_count;
912}
913
Steven Rostedt0f048702008-11-05 16:05:44 -0500914/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500915 * ftrace_off_permanent - disable all ftrace code permanently
916 *
917 * This should only be called when a serious anomally has
918 * been detected. This will turn off the function tracing,
919 * ring buffers, and other tracing utilites. It takes no
920 * locks and can be called from any context.
921 */
922void ftrace_off_permanent(void)
923{
924 tracing_disabled = 1;
925 ftrace_stop();
926 tracing_off_permanent();
927}
928
929/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500930 * tracing_start - quick start of the tracer
931 *
932 * If tracing is enabled but was stopped by tracing_stop,
933 * this will start the tracer back up.
934 */
935void tracing_start(void)
936{
937 struct ring_buffer *buffer;
938 unsigned long flags;
939
940 if (tracing_disabled)
941 return;
942
943 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500944 if (--trace_stop_count) {
945 if (trace_stop_count < 0) {
946 /* Someone screwed up their debugging */
947 WARN_ON_ONCE(1);
948 trace_stop_count = 0;
949 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500950 goto out;
951 }
952
Steven Rostedta2f80712010-03-12 19:56:00 -0500953 /* Prevent the buffers from switching */
954 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -0500955
956 buffer = global_trace.buffer;
957 if (buffer)
958 ring_buffer_record_enable(buffer);
959
960 buffer = max_tr.buffer;
961 if (buffer)
962 ring_buffer_record_enable(buffer);
963
Steven Rostedta2f80712010-03-12 19:56:00 -0500964 arch_spin_unlock(&ftrace_max_lock);
965
Steven Rostedt0f048702008-11-05 16:05:44 -0500966 ftrace_start();
967 out:
968 spin_unlock_irqrestore(&tracing_start_lock, flags);
969}
970
971/**
972 * tracing_stop - quick stop of the tracer
973 *
974 * Light weight way to stop tracing. Use in conjunction with
975 * tracing_start.
976 */
977void tracing_stop(void)
978{
979 struct ring_buffer *buffer;
980 unsigned long flags;
981
982 ftrace_stop();
983 spin_lock_irqsave(&tracing_start_lock, flags);
984 if (trace_stop_count++)
985 goto out;
986
Steven Rostedta2f80712010-03-12 19:56:00 -0500987 /* Prevent the buffers from switching */
988 arch_spin_lock(&ftrace_max_lock);
989
Steven Rostedt0f048702008-11-05 16:05:44 -0500990 buffer = global_trace.buffer;
991 if (buffer)
992 ring_buffer_record_disable(buffer);
993
994 buffer = max_tr.buffer;
995 if (buffer)
996 ring_buffer_record_disable(buffer);
997
Steven Rostedta2f80712010-03-12 19:56:00 -0500998 arch_spin_unlock(&ftrace_max_lock);
999
Steven Rostedt0f048702008-11-05 16:05:44 -05001000 out:
1001 spin_unlock_irqrestore(&tracing_start_lock, flags);
1002}
1003
Ingo Molnare309b412008-05-12 21:20:51 +02001004void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001005
Ingo Molnare309b412008-05-12 21:20:51 +02001006static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001007{
Carsten Emdea635cf02009-03-18 09:00:41 +01001008 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001009
1010 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1011 return;
1012
1013 /*
1014 * It's not the end of the world if we don't get
1015 * the lock, but we also don't want to spin
1016 * nor do we want to disable interrupts,
1017 * so if we miss here, then better luck next time.
1018 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001019 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001020 return;
1021
1022 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001023 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001024 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1025
Carsten Emdea635cf02009-03-18 09:00:41 +01001026 /*
1027 * Check whether the cmdline buffer at idx has a pid
1028 * mapped. We are going to overwrite that entry so we
1029 * need to clear the map_pid_to_cmdline. Otherwise we
1030 * would read the new comm for the old pid.
1031 */
1032 pid = map_cmdline_to_pid[idx];
1033 if (pid != NO_CMDLINE_MAP)
1034 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001035
Carsten Emdea635cf02009-03-18 09:00:41 +01001036 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001037 map_pid_to_cmdline[tsk->pid] = idx;
1038
1039 cmdline_idx = idx;
1040 }
1041
1042 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1043
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001044 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001045}
1046
Steven Rostedt4ca530852009-03-16 19:20:15 -04001047void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001048{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001049 unsigned map;
1050
Steven Rostedt4ca530852009-03-16 19:20:15 -04001051 if (!pid) {
1052 strcpy(comm, "<idle>");
1053 return;
1054 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001055
Steven Rostedt74bf4072010-01-25 15:11:53 -05001056 if (WARN_ON_ONCE(pid < 0)) {
1057 strcpy(comm, "<XXX>");
1058 return;
1059 }
1060
Steven Rostedt4ca530852009-03-16 19:20:15 -04001061 if (pid > PID_MAX_DEFAULT) {
1062 strcpy(comm, "<...>");
1063 return;
1064 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001065
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001066 preempt_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001067 arch_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001069 if (map != NO_CMDLINE_MAP)
1070 strcpy(comm, saved_cmdlines[map]);
1071 else
1072 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001073
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001074 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001075 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001076}
1077
Ingo Molnare309b412008-05-12 21:20:51 +02001078void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001079{
Thomas Gleixner18aecd32009-03-18 08:56:58 +01001080 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1081 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001082 return;
1083
1084 trace_save_cmdline(tsk);
1085}
1086
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001087void
Steven Rostedt38697052008-10-01 13:14:09 -04001088tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1089 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001090{
1091 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001092
Steven Rostedt777e2082008-09-29 23:02:42 -04001093 entry->preempt_count = pc & 0xff;
1094 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedt637e7e82009-09-11 13:55:35 -04001095 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -04001096 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001097#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001098 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001099#else
1100 TRACE_FLAG_IRQS_NOSUPPORT |
1101#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001102 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1103 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1104 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1105}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001106EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001107
Steven Rostedte77405a2009-09-02 14:17:06 -04001108struct ring_buffer_event *
1109trace_buffer_lock_reserve(struct ring_buffer *buffer,
1110 int type,
1111 unsigned long len,
1112 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001113{
1114 struct ring_buffer_event *event;
1115
Steven Rostedte77405a2009-09-02 14:17:06 -04001116 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001117 if (event != NULL) {
1118 struct trace_entry *ent = ring_buffer_event_data(event);
1119
1120 tracing_generic_entry_update(ent, flags, pc);
1121 ent->type = type;
1122 }
1123
1124 return event;
1125}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001126
Steven Rostedte77405a2009-09-02 14:17:06 -04001127static inline void
1128__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1129 struct ring_buffer_event *event,
1130 unsigned long flags, int pc,
1131 int wake)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001132{
Steven Rostedte77405a2009-09-02 14:17:06 -04001133 ring_buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001134
Steven Rostedte77405a2009-09-02 14:17:06 -04001135 ftrace_trace_stack(buffer, flags, 6, pc);
1136 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001137
1138 if (wake)
1139 trace_wake_up();
1140}
1141
Steven Rostedte77405a2009-09-02 14:17:06 -04001142void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1143 struct ring_buffer_event *event,
1144 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001145{
Steven Rostedte77405a2009-09-02 14:17:06 -04001146 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001147}
1148
Steven Rostedtef5580d2009-02-27 19:38:04 -05001149struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001150trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1151 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001152 unsigned long flags, int pc)
1153{
Steven Rostedte77405a2009-09-02 14:17:06 -04001154 *current_rb = global_trace.buffer;
1155 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001156 type, len, flags, pc);
1157}
Steven Rostedt94487d62009-05-05 19:22:53 -04001158EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001159
Steven Rostedte77405a2009-09-02 14:17:06 -04001160void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1161 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001162 unsigned long flags, int pc)
1163{
Steven Rostedte77405a2009-09-02 14:17:06 -04001164 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001165}
Steven Rostedt94487d62009-05-05 19:22:53 -04001166EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001167
Steven Rostedte77405a2009-09-02 14:17:06 -04001168void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1169 struct ring_buffer_event *event,
1170 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001171{
Steven Rostedte77405a2009-09-02 14:17:06 -04001172 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001173}
Steven Rostedt94487d62009-05-05 19:22:53 -04001174EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
Steven Rostedt77d9f462009-04-02 01:16:59 -04001175
Steven Rostedte77405a2009-09-02 14:17:06 -04001176void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1177 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001178{
Steven Rostedte77405a2009-09-02 14:17:06 -04001179 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001180}
Steven Rostedt12acd472009-04-17 16:01:56 -04001181EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001182
Ingo Molnare309b412008-05-12 21:20:51 +02001183void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001184trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001185 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1186 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001187{
Tom Zanussie1112b42009-03-31 00:48:49 -05001188 struct ftrace_event_call *call = &event_function;
Steven Rostedte77405a2009-09-02 14:17:06 -04001189 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001190 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001191 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001192
Steven Rostedtd7690412008-10-01 00:29:53 -04001193 /* If we are reading the ring buffer, don't trace */
Christoph Lameter9288f992009-10-07 19:17:45 -04001194 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
Steven Rostedtd7690412008-10-01 00:29:53 -04001195 return;
1196
Steven Rostedte77405a2009-09-02 14:17:06 -04001197 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001198 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001199 if (!event)
1200 return;
1201 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001202 entry->ip = ip;
1203 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001204
Steven Rostedte77405a2009-09-02 14:17:06 -04001205 if (!filter_check_discard(call, entry, buffer, event))
1206 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001207}
1208
Ingo Molnare309b412008-05-12 21:20:51 +02001209void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001210ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -04001211 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1212 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001213{
1214 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001215 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001216}
1217
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001218#ifdef CONFIG_STACKTRACE
Steven Rostedte77405a2009-09-02 14:17:06 -04001219static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001220 unsigned long flags,
1221 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +02001222{
Tom Zanussie1112b42009-03-31 00:48:49 -05001223 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001224 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001225 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001226 struct stack_trace trace;
1227
Steven Rostedte77405a2009-09-02 14:17:06 -04001228 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001229 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001230 if (!event)
1231 return;
1232 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001233 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +02001234
1235 trace.nr_entries = 0;
1236 trace.max_entries = FTRACE_STACK_ENTRIES;
1237 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -04001238 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +02001239
1240 save_stack_trace(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001241 if (!filter_check_discard(call, entry, buffer, event))
1242 ring_buffer_unlock_commit(buffer, event);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001243}
1244
Steven Rostedte77405a2009-09-02 14:17:06 -04001245void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1246 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001247{
1248 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1249 return;
1250
Steven Rostedte77405a2009-09-02 14:17:06 -04001251 __ftrace_trace_stack(buffer, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -05001252}
1253
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001254void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1255 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001256{
Steven Rostedte77405a2009-09-02 14:17:06 -04001257 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -04001258}
1259
Steven Rostedt03889382009-12-11 09:48:22 -05001260/**
1261 * trace_dump_stack - record a stack back trace in the trace buffer
1262 */
1263void trace_dump_stack(void)
1264{
1265 unsigned long flags;
1266
1267 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001268 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001269
1270 local_save_flags(flags);
1271
1272 /* skipping 3 traces, seems to get us at the caller of this function */
1273 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1274}
1275
Steven Rostedte77405a2009-09-02 14:17:06 -04001276void
1277ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001278{
Tom Zanussie1112b42009-03-31 00:48:49 -05001279 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001280 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001281 struct userstack_entry *entry;
1282 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001283
1284 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1285 return;
1286
Steven Rostedte77405a2009-09-02 14:17:06 -04001287 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001288 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001289 if (!event)
1290 return;
1291 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001292
Steven Rostedt48659d32009-09-11 11:36:23 -04001293 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001294 memset(&entry->caller, 0, sizeof(entry->caller));
1295
1296 trace.nr_entries = 0;
1297 trace.max_entries = FTRACE_STACK_ENTRIES;
1298 trace.skip = 0;
1299 trace.entries = entry->caller;
1300
1301 save_stack_trace_user(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001302 if (!filter_check_discard(call, entry, buffer, event))
1303 ring_buffer_unlock_commit(buffer, event);
Török Edwin02b67512008-11-22 13:28:47 +02001304}
1305
Hannes Eder4fd27352009-02-10 19:44:12 +01001306#ifdef UNUSED
1307static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001308{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001309 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001310}
Hannes Eder4fd27352009-02-10 19:44:12 +01001311#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001312
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001313#endif /* CONFIG_STACKTRACE */
1314
Steven Rostedt38697052008-10-01 13:14:09 -04001315static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001316ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001317 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1318 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +02001319{
Steven Rostedt60ba7702009-09-12 23:34:04 -04001320 struct ftrace_event_call *call = &event_special;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001321 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001322 struct trace_array *tr = __tr;
Steven Rostedte77405a2009-09-02 14:17:06 -04001323 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt777e2082008-09-29 23:02:42 -04001324 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001325
Steven Rostedte77405a2009-09-02 14:17:06 -04001326 event = trace_buffer_lock_reserve(buffer, TRACE_SPECIAL,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001327 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001328 if (!event)
1329 return;
1330 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001331 entry->arg1 = arg1;
1332 entry->arg2 = arg2;
1333 entry->arg3 = arg3;
Steven Rostedt60ba7702009-09-12 23:34:04 -04001334
1335 if (!filter_check_discard(call, entry, buffer, event))
1336 trace_buffer_unlock_commit(buffer, event, 0, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +02001337}
1338
1339void
Steven Rostedt38697052008-10-01 13:14:09 -04001340__trace_special(void *__tr, void *__data,
1341 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1342{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001343 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001344}
1345
1346void
Steven Rostedt4902f882008-05-22 00:22:18 -04001347ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1348{
1349 struct trace_array *tr = &global_trace;
1350 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001351 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001352 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001353 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001354
Steven Rostedtc76f0692008-11-07 22:36:02 -05001355 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001356 return;
1357
Steven Rostedt38697052008-10-01 13:14:09 -04001358 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001359 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001360 cpu = raw_smp_processor_id();
1361 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001362
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001363 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001364 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001365
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001366 atomic_dec(&data->disabled);
1367 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001368}
1369
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001370/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001371 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001372 *
1373 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001374int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001375{
Thomas Gleixner445c8952009-12-02 19:49:50 +01001376 static arch_spinlock_t trace_buf_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001377 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001378 static u32 trace_buf[TRACE_BUF_SIZE];
1379
Tom Zanussie1112b42009-03-31 00:48:49 -05001380 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001381 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001382 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001383 struct trace_array *tr = &global_trace;
1384 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001385 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001386 unsigned long flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001387 int disable;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001388 int resched;
1389 int cpu, len = 0, size, pc;
1390
1391 if (unlikely(tracing_selftest_running || tracing_disabled))
1392 return 0;
1393
1394 /* Don't pollute graph traces with trace_vprintk internals */
1395 pause_graph_tracing();
1396
1397 pc = preempt_count();
1398 resched = ftrace_preempt_disable();
1399 cpu = raw_smp_processor_id();
1400 data = tr->data[cpu];
1401
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001402 disable = atomic_inc_return(&data->disabled);
1403 if (unlikely(disable != 1))
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001404 goto out;
1405
Steven Rostedt80370cb2009-03-10 17:16:35 -04001406 /* Lockdep uses trace_printk for lock tracing */
1407 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001408 arch_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001409 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1410
1411 if (len > TRACE_BUF_SIZE || len < 0)
1412 goto out_unlock;
1413
1414 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedte77405a2009-09-02 14:17:06 -04001415 buffer = tr->buffer;
1416 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1417 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001418 if (!event)
1419 goto out_unlock;
1420 entry = ring_buffer_event_data(event);
1421 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001422 entry->fmt = fmt;
1423
1424 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
Steven Rostedtd9313692010-01-06 17:27:11 -05001425 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001426 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001427 ftrace_trace_stack(buffer, flags, 6, pc);
1428 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001429
1430out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001431 arch_spin_unlock(&trace_buf_lock);
Steven Rostedt80370cb2009-03-10 17:16:35 -04001432 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001433
1434out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001435 atomic_dec_return(&data->disabled);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001436 ftrace_preempt_enable(resched);
1437 unpause_graph_tracing();
1438
1439 return len;
1440}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001441EXPORT_SYMBOL_GPL(trace_vbprintk);
1442
Steven Rostedt659372d2009-09-03 19:11:07 -04001443int trace_array_printk(struct trace_array *tr,
1444 unsigned long ip, const char *fmt, ...)
1445{
1446 int ret;
1447 va_list ap;
1448
1449 if (!(trace_flags & TRACE_ITER_PRINTK))
1450 return 0;
1451
1452 va_start(ap, fmt);
1453 ret = trace_array_vprintk(tr, ip, fmt, ap);
1454 va_end(ap);
1455 return ret;
1456}
1457
1458int trace_array_vprintk(struct trace_array *tr,
1459 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001460{
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001461 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001462 static char trace_buf[TRACE_BUF_SIZE];
1463
Tom Zanussie1112b42009-03-31 00:48:49 -05001464 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001465 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001466 struct ring_buffer *buffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001467 struct trace_array_cpu *data;
1468 int cpu, len = 0, size, pc;
1469 struct print_entry *entry;
1470 unsigned long irq_flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001471 int disable;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001472
1473 if (tracing_disabled || tracing_selftest_running)
1474 return 0;
1475
1476 pc = preempt_count();
1477 preempt_disable_notrace();
1478 cpu = raw_smp_processor_id();
1479 data = tr->data[cpu];
1480
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001481 disable = atomic_inc_return(&data->disabled);
1482 if (unlikely(disable != 1))
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001483 goto out;
1484
1485 pause_graph_tracing();
1486 raw_local_irq_save(irq_flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001487 arch_spin_lock(&trace_buf_lock);
Carsten Emdef2942482009-12-06 14:02:44 +01001488 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001489
1490 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04001491 buffer = tr->buffer;
1492 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1493 irq_flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001494 if (!event)
1495 goto out_unlock;
1496 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001497 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001498
1499 memcpy(&entry->buf, trace_buf, len);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001500 entry->buf[len] = '\0';
Steven Rostedtd9313692010-01-06 17:27:11 -05001501 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001502 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001503 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1504 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001505
1506 out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001507 arch_spin_unlock(&trace_buf_lock);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001508 raw_local_irq_restore(irq_flags);
1509 unpause_graph_tracing();
1510 out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001511 atomic_dec_return(&data->disabled);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001512 preempt_enable_notrace();
1513
1514 return len;
1515}
Steven Rostedt659372d2009-09-03 19:11:07 -04001516
1517int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1518{
Steven Rostedta813a152009-10-09 01:41:35 -04001519 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04001520}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001521EXPORT_SYMBOL_GPL(trace_vprintk);
1522
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001523enum trace_file_type {
1524 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001525 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001526};
1527
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001528static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001529{
Steven Rostedtd7690412008-10-01 00:29:53 -04001530 /* Don't allow ftrace to trace into the ring buffers */
1531 ftrace_disable_cpu();
1532
Steven Rostedt5a90f572008-09-03 17:42:51 -04001533 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001534 if (iter->buffer_iter[iter->cpu])
1535 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1536
1537 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001538}
1539
Ingo Molnare309b412008-05-12 21:20:51 +02001540static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001541peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001542{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001543 struct ring_buffer_event *event;
1544 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001545
Steven Rostedtd7690412008-10-01 00:29:53 -04001546 /* Don't allow ftrace to trace into the ring buffers */
1547 ftrace_disable_cpu();
1548
1549 if (buf_iter)
1550 event = ring_buffer_iter_peek(buf_iter, ts);
1551 else
1552 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1553
1554 ftrace_enable_cpu();
1555
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001556 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001557}
Steven Rostedtd7690412008-10-01 00:29:53 -04001558
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001559static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001560__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001561{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001562 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001563 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001564 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001565 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001566 int next_cpu = -1;
1567 int cpu;
1568
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001569 /*
1570 * If we are in a per_cpu trace file, don't bother by iterating over
1571 * all cpu and peek directly.
1572 */
1573 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1574 if (ring_buffer_empty_cpu(buffer, cpu_file))
1575 return NULL;
1576 ent = peek_next_entry(iter, cpu_file, ent_ts);
1577 if (ent_cpu)
1578 *ent_cpu = cpu_file;
1579
1580 return ent;
1581 }
1582
Steven Rostedtab464282008-05-12 21:21:00 +02001583 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001584
1585 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001586 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001587
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001588 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001589
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001590 /*
1591 * Pick the entry with the smallest timestamp:
1592 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001593 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001594 next = ent;
1595 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001596 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001597 }
1598 }
1599
1600 if (ent_cpu)
1601 *ent_cpu = next_cpu;
1602
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001603 if (ent_ts)
1604 *ent_ts = next_ts;
1605
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001606 return next;
1607}
1608
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001609/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001610struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1611 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001612{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001613 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001614}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001615
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001616/* Find the next real entry, and increment the iterator to the next entry */
1617static void *find_next_entry_inc(struct trace_iterator *iter)
1618{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001619 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001620
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001621 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001622 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001623
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001624 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001625}
1626
Ingo Molnare309b412008-05-12 21:20:51 +02001627static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001628{
Steven Rostedtd7690412008-10-01 00:29:53 -04001629 /* Don't allow ftrace to trace into the ring buffers */
1630 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001631 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001632 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001633}
1634
Ingo Molnare309b412008-05-12 21:20:51 +02001635static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001636{
1637 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001638 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001639 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001640
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001641 WARN_ON_ONCE(iter->leftover);
1642
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001643 (*pos)++;
1644
1645 /* can't go backwards */
1646 if (iter->idx > i)
1647 return NULL;
1648
1649 if (iter->idx < 0)
1650 ent = find_next_entry_inc(iter);
1651 else
1652 ent = iter;
1653
1654 while (ent && iter->idx < i)
1655 ent = find_next_entry_inc(iter);
1656
1657 iter->pos = *pos;
1658
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001659 return ent;
1660}
1661
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001662static void tracing_iter_reset(struct trace_iterator *iter, int cpu)
1663{
1664 struct trace_array *tr = iter->tr;
1665 struct ring_buffer_event *event;
1666 struct ring_buffer_iter *buf_iter;
1667 unsigned long entries = 0;
1668 u64 ts;
1669
1670 tr->data[cpu]->skipped_entries = 0;
1671
1672 if (!iter->buffer_iter[cpu])
1673 return;
1674
1675 buf_iter = iter->buffer_iter[cpu];
1676 ring_buffer_iter_reset(buf_iter);
1677
1678 /*
1679 * We could have the case with the max latency tracers
1680 * that a reset never took place on a cpu. This is evident
1681 * by the timestamp being before the start of the buffer.
1682 */
1683 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1684 if (ts >= iter->tr->time_start)
1685 break;
1686 entries++;
1687 ring_buffer_read(buf_iter, NULL);
1688 }
1689
1690 tr->data[cpu]->skipped_entries = entries;
1691}
1692
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001693/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001694 * The current tracer is copied to avoid a global locking
1695 * all around.
1696 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001697static void *s_start(struct seq_file *m, loff_t *pos)
1698{
1699 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001700 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001701 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001702 void *p = NULL;
1703 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001704 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001705
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001706 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001707 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001708 if (unlikely(old_tracer != current_trace && current_trace)) {
1709 old_tracer = current_trace;
1710 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001711 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001712 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001713
1714 atomic_inc(&trace_record_cmdline_disabled);
1715
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001716 if (*pos != iter->pos) {
1717 iter->ent = NULL;
1718 iter->cpu = 0;
1719 iter->idx = -1;
1720
Steven Rostedtd7690412008-10-01 00:29:53 -04001721 ftrace_disable_cpu();
1722
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001723 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1724 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001725 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001726 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001727 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001728
Steven Rostedtd7690412008-10-01 00:29:53 -04001729 ftrace_enable_cpu();
1730
Lai Jiangshanac91d852010-03-02 17:54:50 +08001731 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1733 ;
1734
1735 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001736 /*
1737 * If we overflowed the seq_file before, then we want
1738 * to just reuse the trace_seq buffer again.
1739 */
1740 if (iter->leftover)
1741 p = iter;
1742 else {
1743 l = *pos - 1;
1744 p = s_next(m, p, &l);
1745 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001746 }
1747
Lai Jiangshan4f535962009-05-18 19:35:34 +08001748 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001749 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001750 return p;
1751}
1752
1753static void s_stop(struct seq_file *m, void *p)
1754{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001755 struct trace_iterator *iter = m->private;
1756
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001757 atomic_dec(&trace_record_cmdline_disabled);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001758 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08001759 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001760}
1761
Ingo Molnare309b412008-05-12 21:20:51 +02001762static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001763{
Michael Ellermana6168352008-08-20 16:36:11 -07001764 seq_puts(m, "# _------=> CPU# \n");
1765 seq_puts(m, "# / _-----=> irqs-off \n");
1766 seq_puts(m, "# | / _----=> need-resched \n");
1767 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1768 seq_puts(m, "# ||| / _--=> preempt-depth \n");
Steven Rostedt637e7e82009-09-11 13:55:35 -04001769 seq_puts(m, "# |||| /_--=> lock-depth \n");
1770 seq_puts(m, "# |||||/ delay \n");
1771 seq_puts(m, "# cmd pid |||||| time | caller \n");
1772 seq_puts(m, "# \\ / |||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001773}
1774
Ingo Molnare309b412008-05-12 21:20:51 +02001775static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001776{
Michael Ellermana6168352008-08-20 16:36:11 -07001777 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1778 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001779}
1780
1781
Ingo Molnare309b412008-05-12 21:20:51 +02001782static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001783print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1784{
1785 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1786 struct trace_array *tr = iter->tr;
1787 struct trace_array_cpu *data = tr->data[tr->cpu];
1788 struct tracer *type = current_trace;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001789 unsigned long entries = 0;
1790 unsigned long total = 0;
1791 unsigned long count;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001792 const char *name = "preemption";
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001793 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001794
1795 if (type)
1796 name = type->name;
1797
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001798
1799 for_each_tracing_cpu(cpu) {
1800 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1801 /*
1802 * If this buffer has skipped entries, then we hold all
1803 * entries for the trace and we need to ignore the
1804 * ones before the time stamp.
1805 */
1806 if (tr->data[cpu]->skipped_entries) {
1807 count -= tr->data[cpu]->skipped_entries;
1808 /* total is the same as the entries */
1809 total += count;
1810 } else
1811 total += count +
1812 ring_buffer_overrun_cpu(tr->buffer, cpu);
1813 entries += count;
1814 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001815
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001816 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001817 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001818 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001819 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001820 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001821 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001822 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001823 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001824 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001825 tr->cpu,
1826#if defined(CONFIG_PREEMPT_NONE)
1827 "server",
1828#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1829 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001830#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001831 "preempt",
1832#else
1833 "unknown",
1834#endif
1835 /* These are reserved for later use */
1836 0, 0, 0, 0);
1837#ifdef CONFIG_SMP
1838 seq_printf(m, " #P:%d)\n", num_online_cpus());
1839#else
1840 seq_puts(m, ")\n");
1841#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001842 seq_puts(m, "# -----------------\n");
1843 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001844 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1845 data->comm, data->pid, data->uid, data->nice,
1846 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001847 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001848
1849 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001850 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001851 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1852 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001853 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001854 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1855 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001856 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001857 }
1858
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001859 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001860}
1861
Steven Rostedta3097202008-11-07 22:36:02 -05001862static void test_cpu_buff_start(struct trace_iterator *iter)
1863{
1864 struct trace_seq *s = &iter->seq;
1865
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001866 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1867 return;
1868
1869 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1870 return;
1871
Rusty Russell44623442009-01-01 10:12:23 +10301872 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001873 return;
1874
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001875 if (iter->tr->data[iter->cpu]->skipped_entries)
1876 return;
1877
Rusty Russell44623442009-01-01 10:12:23 +10301878 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001879
1880 /* Don't print started cpu buffer for the first entry of the trace */
1881 if (iter->idx > 1)
1882 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1883 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05001884}
1885
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001886static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001887{
Steven Rostedt214023c2008-05-12 21:20:46 +02001888 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001889 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001890 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001891 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001892
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001893 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001894
Steven Rostedta3097202008-11-07 22:36:02 -05001895 test_cpu_buff_start(iter);
1896
Steven Rostedtf633cef2008-12-23 23:24:13 -05001897 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001898
1899 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001900 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1901 if (!trace_print_lat_context(iter))
1902 goto partial;
1903 } else {
1904 if (!trace_print_context(iter))
1905 goto partial;
1906 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001907 }
1908
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001909 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001910 return event->trace(iter, sym_flags);
1911
1912 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1913 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001914
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001915 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001916partial:
1917 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001918}
1919
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001920static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001921{
1922 struct trace_seq *s = &iter->seq;
1923 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001924 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001925
1926 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001927
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001928 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001929 if (!trace_seq_printf(s, "%d %d %llu ",
1930 entry->pid, iter->cpu, iter->ts))
1931 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001932 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001933
Steven Rostedtf633cef2008-12-23 23:24:13 -05001934 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001935 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001936 return event->raw(iter, 0);
1937
1938 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1939 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001940
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001941 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001942partial:
1943 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001944}
1945
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001946static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001947{
1948 struct trace_seq *s = &iter->seq;
1949 unsigned char newline = '\n';
1950 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001951 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001952
1953 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001954
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001955 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1956 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1957 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1958 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1959 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001960
Steven Rostedtf633cef2008-12-23 23:24:13 -05001961 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001962 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001963 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001964 if (ret != TRACE_TYPE_HANDLED)
1965 return ret;
1966 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001967
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001968 SEQ_PUT_FIELD_RET(s, newline);
1969
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001970 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001971}
1972
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001973static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001974{
1975 struct trace_seq *s = &iter->seq;
1976 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001977 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001978
1979 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001980
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001981 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1982 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001983 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001984 SEQ_PUT_FIELD_RET(s, iter->ts);
1985 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001986
Steven Rostedtf633cef2008-12-23 23:24:13 -05001987 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001988 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001989}
1990
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001991static int trace_empty(struct trace_iterator *iter)
1992{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001993 int cpu;
1994
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001995 /* If we are looking at one CPU buffer, only check that one */
1996 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1997 cpu = iter->cpu_file;
1998 if (iter->buffer_iter[cpu]) {
1999 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2000 return 0;
2001 } else {
2002 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2003 return 0;
2004 }
2005 return 1;
2006 }
2007
Steven Rostedtab464282008-05-12 21:21:00 +02002008 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04002009 if (iter->buffer_iter[cpu]) {
2010 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2011 return 0;
2012 } else {
2013 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2014 return 0;
2015 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002016 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002017
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002018 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002019}
2020
Lai Jiangshan4f535962009-05-18 19:35:34 +08002021/* Called with trace_event_read_lock() held. */
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002022static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002023{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002024 enum print_line_t ret;
2025
2026 if (iter->trace && iter->trace->print_line) {
2027 ret = iter->trace->print_line(iter);
2028 if (ret != TRACE_TYPE_UNHANDLED)
2029 return ret;
2030 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002031
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002032 if (iter->ent->type == TRACE_BPRINT &&
2033 trace_flags & TRACE_ITER_PRINTK &&
2034 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002035 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002036
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002037 if (iter->ent->type == TRACE_PRINT &&
2038 trace_flags & TRACE_ITER_PRINTK &&
2039 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002040 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002041
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002042 if (trace_flags & TRACE_ITER_BIN)
2043 return print_bin_fmt(iter);
2044
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002045 if (trace_flags & TRACE_ITER_HEX)
2046 return print_hex_fmt(iter);
2047
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002048 if (trace_flags & TRACE_ITER_RAW)
2049 return print_raw_fmt(iter);
2050
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002051 return print_trace_fmt(iter);
2052}
2053
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002054static int s_show(struct seq_file *m, void *v)
2055{
2056 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002057 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002058
2059 if (iter->ent == NULL) {
2060 if (iter->tr) {
2061 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2062 seq_puts(m, "#\n");
2063 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002064 if (iter->trace && iter->trace->print_header)
2065 iter->trace->print_header(m);
2066 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002067 /* print nothing if the buffers are empty */
2068 if (trace_empty(iter))
2069 return 0;
2070 print_trace_header(m, iter);
2071 if (!(trace_flags & TRACE_ITER_VERBOSE))
2072 print_lat_help_header(m);
2073 } else {
2074 if (!(trace_flags & TRACE_ITER_VERBOSE))
2075 print_func_help_header(m);
2076 }
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002077 } else if (iter->leftover) {
2078 /*
2079 * If we filled the seq_file buffer earlier, we
2080 * want to just show it now.
2081 */
2082 ret = trace_print_seq(m, &iter->seq);
2083
2084 /* ret should this time be zero, but you never know */
2085 iter->leftover = ret;
2086
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002087 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002088 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002089 ret = trace_print_seq(m, &iter->seq);
2090 /*
2091 * If we overflow the seq_file buffer, then it will
2092 * ask us for this data again at start up.
2093 * Use that instead.
2094 * ret is 0 if seq_file write succeeded.
2095 * -1 otherwise.
2096 */
2097 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002098 }
2099
2100 return 0;
2101}
2102
James Morris88e9d342009-09-22 16:43:43 -07002103static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002104 .start = s_start,
2105 .next = s_next,
2106 .stop = s_stop,
2107 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002108};
2109
Ingo Molnare309b412008-05-12 21:20:51 +02002110static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002111__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002112{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002113 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002114 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002115 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002116 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002117 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002118
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002119 if (tracing_disabled)
2120 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02002121
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002122 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002123 if (!iter)
2124 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002125
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002126 /*
2127 * We make a copy of the current tracer to avoid concurrent
2128 * changes on it while we are reading.
2129 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002130 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002131 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002132 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002133 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002134
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002135 if (current_trace)
2136 *iter->trace = *current_trace;
2137
Li Zefan79f55992009-06-15 14:58:26 +08002138 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002139 goto fail;
2140
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002141 if (current_trace && current_trace->print_max)
2142 iter->tr = &max_tr;
2143 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002144 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002145 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002146 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002147 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002148
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002149 /* Notify the tracer early; before we stop tracing. */
2150 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01002151 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002152
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002153 /* Annotate start of buffers if we had overruns */
2154 if (ring_buffer_overruns(iter->tr->buffer))
2155 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2156
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002157 /* stop the trace while dumping */
2158 tracing_stop();
2159
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002160 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2161 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002162
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002163 iter->buffer_iter[cpu] =
2164 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002165 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002166 }
2167 } else {
2168 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002169 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002170 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002171 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002172 }
2173
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002174 ret = seq_open(file, &tracer_seq_ops);
2175 if (ret < 0) {
2176 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002177 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002178 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002179
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002180 m = file->private_data;
2181 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002182
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002183 mutex_unlock(&trace_types_lock);
2184
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002185 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002186
2187 fail_buffer:
2188 for_each_tracing_cpu(cpu) {
2189 if (iter->buffer_iter[cpu])
2190 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2191 }
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002192 free_cpumask_var(iter->started);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002193 tracing_start();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002194 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002195 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002196 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01002197 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002198
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002199 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002200}
2201
2202int tracing_open_generic(struct inode *inode, struct file *filp)
2203{
Steven Rostedt60a11772008-05-12 21:20:44 +02002204 if (tracing_disabled)
2205 return -ENODEV;
2206
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002207 filp->private_data = inode->i_private;
2208 return 0;
2209}
2210
Hannes Eder4fd27352009-02-10 19:44:12 +01002211static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002212{
2213 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002214 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002215 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002216
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002217 if (!(file->f_mode & FMODE_READ))
2218 return 0;
2219
2220 iter = m->private;
2221
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002222 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002223 for_each_tracing_cpu(cpu) {
2224 if (iter->buffer_iter[cpu])
2225 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2226 }
2227
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002228 if (iter->trace && iter->trace->close)
2229 iter->trace->close(iter);
2230
2231 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05002232 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002233 mutex_unlock(&trace_types_lock);
2234
2235 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002236 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002237 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002238 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002239 kfree(iter);
2240 return 0;
2241}
2242
2243static int tracing_open(struct inode *inode, struct file *file)
2244{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002245 struct trace_iterator *iter;
2246 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002247
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002248 /* If this file was open for write, then erase contents */
2249 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002250 (file->f_flags & O_TRUNC)) {
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002251 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002252
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002253 if (cpu == TRACE_PIPE_ALL_CPU)
2254 tracing_reset_online_cpus(&global_trace);
2255 else
2256 tracing_reset(&global_trace, cpu);
2257 }
2258
2259 if (file->f_mode & FMODE_READ) {
2260 iter = __tracing_open(inode, file);
2261 if (IS_ERR(iter))
2262 ret = PTR_ERR(iter);
2263 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2264 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2265 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002266 return ret;
2267}
2268
Ingo Molnare309b412008-05-12 21:20:51 +02002269static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270t_next(struct seq_file *m, void *v, loff_t *pos)
2271{
Li Zefanf129e962009-06-24 09:53:44 +08002272 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273
2274 (*pos)++;
2275
2276 if (t)
2277 t = t->next;
2278
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002279 return t;
2280}
2281
2282static void *t_start(struct seq_file *m, loff_t *pos)
2283{
Li Zefanf129e962009-06-24 09:53:44 +08002284 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002285 loff_t l = 0;
2286
2287 mutex_lock(&trace_types_lock);
Li Zefanf129e962009-06-24 09:53:44 +08002288 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002289 ;
2290
2291 return t;
2292}
2293
2294static void t_stop(struct seq_file *m, void *p)
2295{
2296 mutex_unlock(&trace_types_lock);
2297}
2298
2299static int t_show(struct seq_file *m, void *v)
2300{
2301 struct tracer *t = v;
2302
2303 if (!t)
2304 return 0;
2305
2306 seq_printf(m, "%s", t->name);
2307 if (t->next)
2308 seq_putc(m, ' ');
2309 else
2310 seq_putc(m, '\n');
2311
2312 return 0;
2313}
2314
James Morris88e9d342009-09-22 16:43:43 -07002315static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002316 .start = t_start,
2317 .next = t_next,
2318 .stop = t_stop,
2319 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002320};
2321
2322static int show_traces_open(struct inode *inode, struct file *file)
2323{
Steven Rostedt60a11772008-05-12 21:20:44 +02002324 if (tracing_disabled)
2325 return -ENODEV;
2326
Li Zefanf129e962009-06-24 09:53:44 +08002327 return seq_open(file, &show_traces_seq_ops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002328}
2329
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002330static ssize_t
2331tracing_write_stub(struct file *filp, const char __user *ubuf,
2332 size_t count, loff_t *ppos)
2333{
2334 return count;
2335}
2336
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002337static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002338 .open = tracing_open,
2339 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002340 .write = tracing_write_stub,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002341 .llseek = seq_lseek,
2342 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002343};
2344
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002345static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002346 .open = show_traces_open,
2347 .read = seq_read,
2348 .release = seq_release,
2349};
2350
Ingo Molnar36dfe922008-05-12 21:20:52 +02002351/*
2352 * Only trace on a CPU if the bitmask is set:
2353 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302354static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002355
2356/*
2357 * The tracer itself will not take this lock, but still we want
2358 * to provide a consistent cpumask to user-space:
2359 */
2360static DEFINE_MUTEX(tracing_cpumask_update_lock);
2361
2362/*
2363 * Temporary storage for the character representation of the
2364 * CPU bitmask (and one more byte for the newline):
2365 */
2366static char mask_str[NR_CPUS + 1];
2367
Ingo Molnarc7078de2008-05-12 21:20:52 +02002368static ssize_t
2369tracing_cpumask_read(struct file *filp, char __user *ubuf,
2370 size_t count, loff_t *ppos)
2371{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002372 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002373
2374 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002375
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302376 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002377 if (count - len < 2) {
2378 count = -EINVAL;
2379 goto out_err;
2380 }
2381 len += sprintf(mask_str + len, "\n");
2382 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2383
2384out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002385 mutex_unlock(&tracing_cpumask_update_lock);
2386
2387 return count;
2388}
2389
2390static ssize_t
2391tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2392 size_t count, loff_t *ppos)
2393{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002394 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302395 cpumask_var_t tracing_cpumask_new;
2396
2397 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2398 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002399
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302400 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002401 if (err)
2402 goto err_unlock;
2403
Li Zefan215368e2009-06-15 10:56:42 +08002404 mutex_lock(&tracing_cpumask_update_lock);
2405
Steven Rostedta5e25882008-12-02 15:34:05 -05002406 local_irq_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002407 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002408 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002409 /*
2410 * Increase/decrease the disabled counter if we are
2411 * about to flip a bit in the cpumask:
2412 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302413 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2414 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002415 atomic_inc(&global_trace.data[cpu]->disabled);
2416 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302417 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2418 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002419 atomic_dec(&global_trace.data[cpu]->disabled);
2420 }
2421 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002422 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002423 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002424
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302425 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002426
Ingo Molnarc7078de2008-05-12 21:20:52 +02002427 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302428 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002429
Ingo Molnarc7078de2008-05-12 21:20:52 +02002430 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002431
2432err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08002433 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002434
2435 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002436}
2437
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002438static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002439 .open = tracing_open_generic,
2440 .read = tracing_cpumask_read,
2441 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002442};
2443
Li Zefanfdb372e2009-12-08 11:15:59 +08002444static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002445{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002446 struct tracer_opt *trace_opts;
2447 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002448 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002449
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002450 mutex_lock(&trace_types_lock);
2451 tracer_flags = current_trace->flags->val;
2452 trace_opts = current_trace->flags->opts;
2453
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002454 for (i = 0; trace_options[i]; i++) {
2455 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08002456 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002457 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002458 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002459 }
2460
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002461 for (i = 0; trace_opts[i].name; i++) {
2462 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08002463 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002464 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002465 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002466 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002467 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002468
Li Zefanfdb372e2009-12-08 11:15:59 +08002469 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002470}
2471
Li Zefan8d18eaa2009-12-08 11:17:06 +08002472static int __set_tracer_option(struct tracer *trace,
2473 struct tracer_flags *tracer_flags,
2474 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002475{
Li Zefan8d18eaa2009-12-08 11:17:06 +08002476 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002477
Li Zefan8d18eaa2009-12-08 11:17:06 +08002478 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002479 if (ret)
2480 return ret;
2481
2482 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08002483 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002484 else
Zhaolei77708412009-08-07 18:53:21 +08002485 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002486 return 0;
2487}
2488
Li Zefan8d18eaa2009-12-08 11:17:06 +08002489/* Try to assign a tracer specific option */
2490static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2491{
2492 struct tracer_flags *tracer_flags = trace->flags;
2493 struct tracer_opt *opts = NULL;
2494 int i;
2495
2496 for (i = 0; tracer_flags->opts[i].name; i++) {
2497 opts = &tracer_flags->opts[i];
2498
2499 if (strcmp(cmp, opts->name) == 0)
2500 return __set_tracer_option(trace, trace->flags,
2501 opts, neg);
2502 }
2503
2504 return -EINVAL;
2505}
2506
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002507static void set_tracer_flags(unsigned int mask, int enabled)
2508{
2509 /* do nothing if flag is already set */
2510 if (!!(trace_flags & mask) == !!enabled)
2511 return;
2512
2513 if (enabled)
2514 trace_flags |= mask;
2515 else
2516 trace_flags &= ~mask;
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002517}
2518
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002519static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002520tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002521 size_t cnt, loff_t *ppos)
2522{
2523 char buf[64];
Li Zefan8d18eaa2009-12-08 11:17:06 +08002524 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002525 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002526 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002527 int i;
2528
Steven Rostedtcffae432008-05-12 21:21:00 +02002529 if (cnt >= sizeof(buf))
2530 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002531
2532 if (copy_from_user(&buf, ubuf, cnt))
2533 return -EFAULT;
2534
2535 buf[cnt] = 0;
Li Zefan8d18eaa2009-12-08 11:17:06 +08002536 cmp = strstrip(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002537
Li Zefan8d18eaa2009-12-08 11:17:06 +08002538 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002539 neg = 1;
2540 cmp += 2;
2541 }
2542
2543 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08002544 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002545 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002546 break;
2547 }
2548 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002549
2550 /* If no option could be set, test the specific tracer options */
2551 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002552 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002553 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002554 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002555 if (ret)
2556 return ret;
2557 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002558
Jiri Olsacf8517c2009-10-23 19:36:16 -04002559 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002560
2561 return cnt;
2562}
2563
Li Zefanfdb372e2009-12-08 11:15:59 +08002564static int tracing_trace_options_open(struct inode *inode, struct file *file)
2565{
2566 if (tracing_disabled)
2567 return -ENODEV;
2568 return single_open(file, tracing_trace_options_show, NULL);
2569}
2570
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002571static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08002572 .open = tracing_trace_options_open,
2573 .read = seq_read,
2574 .llseek = seq_lseek,
2575 .release = single_release,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002576 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002577};
2578
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002579static const char readme_msg[] =
2580 "tracing mini-HOWTO:\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002581 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2582 "# cat /sys/kernel/debug/tracing/available_tracers\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302583 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002584 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302585 "nop\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002586 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2587 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002588 "sched_switch\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002589 "# cat /sys/kernel/debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002590 "noprint-parent nosym-offset nosym-addr noverbose\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002591 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2592 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2593 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2594 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002595;
2596
2597static ssize_t
2598tracing_readme_read(struct file *filp, char __user *ubuf,
2599 size_t cnt, loff_t *ppos)
2600{
2601 return simple_read_from_buffer(ubuf, cnt, ppos,
2602 readme_msg, strlen(readme_msg));
2603}
2604
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002605static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002606 .open = tracing_open_generic,
2607 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002608};
2609
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002610static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04002611tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2612 size_t cnt, loff_t *ppos)
2613{
2614 char *buf_comm;
2615 char *file_buf;
2616 char *buf;
2617 int len = 0;
2618 int pid;
2619 int i;
2620
2621 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2622 if (!file_buf)
2623 return -ENOMEM;
2624
2625 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2626 if (!buf_comm) {
2627 kfree(file_buf);
2628 return -ENOMEM;
2629 }
2630
2631 buf = file_buf;
2632
2633 for (i = 0; i < SAVED_CMDLINES; i++) {
2634 int r;
2635
2636 pid = map_cmdline_to_pid[i];
2637 if (pid == -1 || pid == NO_CMDLINE_MAP)
2638 continue;
2639
2640 trace_find_cmdline(pid, buf_comm);
2641 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2642 buf += r;
2643 len += r;
2644 }
2645
2646 len = simple_read_from_buffer(ubuf, cnt, ppos,
2647 file_buf, len);
2648
2649 kfree(file_buf);
2650 kfree(buf_comm);
2651
2652 return len;
2653}
2654
2655static const struct file_operations tracing_saved_cmdlines_fops = {
2656 .open = tracing_open_generic,
2657 .read = tracing_saved_cmdlines_read,
2658};
2659
2660static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002661tracing_ctrl_read(struct file *filp, char __user *ubuf,
2662 size_t cnt, loff_t *ppos)
2663{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002664 char buf[64];
2665 int r;
2666
Steven Rostedt90369902008-11-05 16:05:44 -05002667 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002668 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002669}
2670
2671static ssize_t
2672tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2673 size_t cnt, loff_t *ppos)
2674{
2675 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002676 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002677 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002678 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002679
Steven Rostedtcffae432008-05-12 21:21:00 +02002680 if (cnt >= sizeof(buf))
2681 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002682
2683 if (copy_from_user(&buf, ubuf, cnt))
2684 return -EFAULT;
2685
2686 buf[cnt] = 0;
2687
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002688 ret = strict_strtoul(buf, 10, &val);
2689 if (ret < 0)
2690 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002691
2692 val = !!val;
2693
2694 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002695 if (tracer_enabled ^ val) {
2696 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002697 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002698 if (current_trace->start)
2699 current_trace->start(tr);
2700 tracing_start();
2701 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002702 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002703 tracing_stop();
2704 if (current_trace->stop)
2705 current_trace->stop(tr);
2706 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002707 }
2708 mutex_unlock(&trace_types_lock);
2709
Jiri Olsacf8517c2009-10-23 19:36:16 -04002710 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002711
2712 return cnt;
2713}
2714
2715static ssize_t
2716tracing_set_trace_read(struct file *filp, char __user *ubuf,
2717 size_t cnt, loff_t *ppos)
2718{
Li Zefanee6c2c12009-09-18 14:06:47 +08002719 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002720 int r;
2721
2722 mutex_lock(&trace_types_lock);
2723 if (current_trace)
2724 r = sprintf(buf, "%s\n", current_trace->name);
2725 else
2726 r = sprintf(buf, "\n");
2727 mutex_unlock(&trace_types_lock);
2728
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002729 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002730}
2731
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002732int tracer_init(struct tracer *t, struct trace_array *tr)
2733{
2734 tracing_reset_online_cpus(tr);
2735 return t->init(tr);
2736}
2737
Steven Rostedt73c51622009-03-11 13:42:01 -04002738static int tracing_resize_ring_buffer(unsigned long size)
2739{
2740 int ret;
2741
2742 /*
2743 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002744 * we use the size that was given, and we can forget about
2745 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002746 */
2747 ring_buffer_expanded = 1;
2748
2749 ret = ring_buffer_resize(global_trace.buffer, size);
2750 if (ret < 0)
2751 return ret;
2752
2753 ret = ring_buffer_resize(max_tr.buffer, size);
2754 if (ret < 0) {
2755 int r;
2756
2757 r = ring_buffer_resize(global_trace.buffer,
2758 global_trace.entries);
2759 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002760 /*
2761 * AARGH! We are left with different
2762 * size max buffer!!!!
2763 * The max buffer is our "snapshot" buffer.
2764 * When a tracer needs a snapshot (one of the
2765 * latency tracers), it swaps the max buffer
2766 * with the saved snap shot. We succeeded to
2767 * update the size of the main buffer, but failed to
2768 * update the size of the max buffer. But when we tried
2769 * to reset the main buffer to the original size, we
2770 * failed there too. This is very unlikely to
2771 * happen, but if it does, warn and kill all
2772 * tracing.
2773 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002774 WARN_ON(1);
2775 tracing_disabled = 1;
2776 }
2777 return ret;
2778 }
2779
2780 global_trace.entries = size;
2781
2782 return ret;
2783}
2784
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002785/**
2786 * tracing_update_buffers - used by tracing facility to expand ring buffers
2787 *
2788 * To save on memory when the tracing is never used on a system with it
2789 * configured in. The ring buffers are set to a minimum size. But once
2790 * a user starts to use the tracing facility, then they need to grow
2791 * to their default size.
2792 *
2793 * This function is to be called when a tracer is about to be used.
2794 */
2795int tracing_update_buffers(void)
2796{
2797 int ret = 0;
2798
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002799 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002800 if (!ring_buffer_expanded)
2801 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002802 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002803
2804 return ret;
2805}
2806
Steven Rostedt577b7852009-02-26 23:43:05 -05002807struct trace_option_dentry;
2808
2809static struct trace_option_dentry *
2810create_trace_option_files(struct tracer *tracer);
2811
2812static void
2813destroy_trace_option_files(struct trace_option_dentry *topts);
2814
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002815static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002816{
Steven Rostedt577b7852009-02-26 23:43:05 -05002817 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002818 struct trace_array *tr = &global_trace;
2819 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002820 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002821
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002822 mutex_lock(&trace_types_lock);
2823
Steven Rostedt73c51622009-03-11 13:42:01 -04002824 if (!ring_buffer_expanded) {
2825 ret = tracing_resize_ring_buffer(trace_buf_size);
2826 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01002827 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04002828 ret = 0;
2829 }
2830
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002831 for (t = trace_types; t; t = t->next) {
2832 if (strcmp(t->name, buf) == 0)
2833 break;
2834 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002835 if (!t) {
2836 ret = -EINVAL;
2837 goto out;
2838 }
2839 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002840 goto out;
2841
Steven Rostedt9f029e82008-11-12 15:24:24 -05002842 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002843 if (current_trace && current_trace->reset)
2844 current_trace->reset(tr);
2845
Steven Rostedt577b7852009-02-26 23:43:05 -05002846 destroy_trace_option_files(topts);
2847
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002848 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002849
2850 topts = create_trace_option_files(current_trace);
2851
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002852 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002853 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002854 if (ret)
2855 goto out;
2856 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002857
Steven Rostedt9f029e82008-11-12 15:24:24 -05002858 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002859 out:
2860 mutex_unlock(&trace_types_lock);
2861
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002862 return ret;
2863}
2864
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002865static ssize_t
2866tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2867 size_t cnt, loff_t *ppos)
2868{
Li Zefanee6c2c12009-09-18 14:06:47 +08002869 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002870 int i;
2871 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002872 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002873
Steven Rostedt60063a62008-10-28 10:44:24 -04002874 ret = cnt;
2875
Li Zefanee6c2c12009-09-18 14:06:47 +08002876 if (cnt > MAX_TRACER_SIZE)
2877 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002878
2879 if (copy_from_user(&buf, ubuf, cnt))
2880 return -EFAULT;
2881
2882 buf[cnt] = 0;
2883
2884 /* strip ending whitespace. */
2885 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2886 buf[i] = 0;
2887
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002888 err = tracing_set_tracer(buf);
2889 if (err)
2890 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002891
Jiri Olsacf8517c2009-10-23 19:36:16 -04002892 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002893
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002894 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002895}
2896
2897static ssize_t
2898tracing_max_lat_read(struct file *filp, char __user *ubuf,
2899 size_t cnt, loff_t *ppos)
2900{
2901 unsigned long *ptr = filp->private_data;
2902 char buf[64];
2903 int r;
2904
Steven Rostedtcffae432008-05-12 21:21:00 +02002905 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002906 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002907 if (r > sizeof(buf))
2908 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002909 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002910}
2911
2912static ssize_t
2913tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2914 size_t cnt, loff_t *ppos)
2915{
Hannes Eder5e398412009-02-10 19:44:34 +01002916 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002917 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002918 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002919 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002920
Steven Rostedtcffae432008-05-12 21:21:00 +02002921 if (cnt >= sizeof(buf))
2922 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002923
2924 if (copy_from_user(&buf, ubuf, cnt))
2925 return -EFAULT;
2926
2927 buf[cnt] = 0;
2928
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002929 ret = strict_strtoul(buf, 10, &val);
2930 if (ret < 0)
2931 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002932
2933 *ptr = val * 1000;
2934
2935 return cnt;
2936}
2937
Steven Rostedtb3806b42008-05-12 21:20:46 +02002938static int tracing_open_pipe(struct inode *inode, struct file *filp)
2939{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002940 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002941 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002942 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002943
2944 if (tracing_disabled)
2945 return -ENODEV;
2946
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002947 mutex_lock(&trace_types_lock);
2948
Steven Rostedtb3806b42008-05-12 21:20:46 +02002949 /* create a buffer to store the information to pass to userspace */
2950 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002951 if (!iter) {
2952 ret = -ENOMEM;
2953 goto out;
2954 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002955
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002956 /*
2957 * We make a copy of the current tracer to avoid concurrent
2958 * changes on it while we are reading.
2959 */
2960 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2961 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002962 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002963 goto fail;
2964 }
2965 if (current_trace)
2966 *iter->trace = *current_trace;
2967
2968 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2969 ret = -ENOMEM;
2970 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302971 }
2972
Steven Rostedta3097202008-11-07 22:36:02 -05002973 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302974 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002975
Steven Rostedt112f38a72009-06-01 15:16:05 -04002976 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2977 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2978
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002979 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002980 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002981 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002982 filp->private_data = iter;
2983
Steven Rostedt107bad82008-05-12 21:21:01 +02002984 if (iter->trace->pipe_open)
2985 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002986
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002987out:
2988 mutex_unlock(&trace_types_lock);
2989 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002990
2991fail:
2992 kfree(iter->trace);
2993 kfree(iter);
2994 mutex_unlock(&trace_types_lock);
2995 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002996}
2997
2998static int tracing_release_pipe(struct inode *inode, struct file *file)
2999{
3000 struct trace_iterator *iter = file->private_data;
3001
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003002 mutex_lock(&trace_types_lock);
3003
Steven Rostedt29bf4a52009-12-09 12:37:43 -05003004 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05003005 iter->trace->pipe_close(iter);
3006
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003007 mutex_unlock(&trace_types_lock);
3008
Rusty Russell44623442009-01-01 10:12:23 +10303009 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003010 mutex_destroy(&iter->mutex);
3011 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003012 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003013
3014 return 0;
3015}
3016
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003017static unsigned int
3018tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3019{
3020 struct trace_iterator *iter = filp->private_data;
3021
3022 if (trace_flags & TRACE_ITER_BLOCK) {
3023 /*
3024 * Always select as readable when in blocking mode
3025 */
3026 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02003027 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003028 if (!trace_empty(iter))
3029 return POLLIN | POLLRDNORM;
3030 poll_wait(filp, &trace_wait, poll_table);
3031 if (!trace_empty(iter))
3032 return POLLIN | POLLRDNORM;
3033
3034 return 0;
3035 }
3036}
3037
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003038
3039void default_wait_pipe(struct trace_iterator *iter)
3040{
3041 DEFINE_WAIT(wait);
3042
3043 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3044
3045 if (trace_empty(iter))
3046 schedule();
3047
3048 finish_wait(&trace_wait, &wait);
3049}
3050
3051/*
3052 * This is a make-shift waitqueue.
3053 * A tracer might use this callback on some rare cases:
3054 *
3055 * 1) the current tracer might hold the runqueue lock when it wakes up
3056 * a reader, hence a deadlock (sched, function, and function graph tracers)
3057 * 2) the function tracers, trace all functions, we don't want
3058 * the overhead of calling wake_up and friends
3059 * (and tracing them too)
3060 *
3061 * Anyway, this is really very primitive wakeup.
3062 */
3063void poll_wait_pipe(struct trace_iterator *iter)
3064{
3065 set_current_state(TASK_INTERRUPTIBLE);
3066 /* sleep for 100 msecs, and try again. */
3067 schedule_timeout(HZ / 10);
3068}
3069
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003070/* Must be called with trace_types_lock mutex held. */
3071static int tracing_wait_pipe(struct file *filp)
3072{
3073 struct trace_iterator *iter = filp->private_data;
3074
3075 while (trace_empty(iter)) {
3076
3077 if ((filp->f_flags & O_NONBLOCK)) {
3078 return -EAGAIN;
3079 }
3080
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003081 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003082
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003083 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003084
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003085 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003086
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003087 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003088 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003089
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003090 /*
3091 * We block until we read something and tracing is disabled.
3092 * We still block if tracing is disabled, but we have never
3093 * read anything. This allows a user to cat this file, and
3094 * then enable tracing. But after we have read something,
3095 * we give an EOF when tracing is again disabled.
3096 *
3097 * iter->pos will be 0 if we haven't read anything.
3098 */
3099 if (!tracer_enabled && iter->pos)
3100 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003101 }
3102
3103 return 1;
3104}
3105
Steven Rostedtb3806b42008-05-12 21:20:46 +02003106/*
3107 * Consumer reader.
3108 */
3109static ssize_t
3110tracing_read_pipe(struct file *filp, char __user *ubuf,
3111 size_t cnt, loff_t *ppos)
3112{
3113 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003114 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003115 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003116
3117 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003118 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3119 if (sret != -EBUSY)
3120 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003121
Steven Rostedtf9520752009-03-02 14:04:40 -05003122 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003123
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003124 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02003125 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003126 if (unlikely(old_tracer != current_trace && current_trace)) {
3127 old_tracer = current_trace;
3128 *iter->trace = *current_trace;
3129 }
3130 mutex_unlock(&trace_types_lock);
3131
3132 /*
3133 * Avoid more than one consumer on a single file descriptor
3134 * This is just a matter of traces coherency, the ring buffer itself
3135 * is protected.
3136 */
3137 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003138 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003139 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3140 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02003141 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02003142 }
3143
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003144waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003145 sret = tracing_wait_pipe(filp);
3146 if (sret <= 0)
3147 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003148
3149 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003150 if (trace_empty(iter)) {
3151 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003152 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003153 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003154
3155 if (cnt >= PAGE_SIZE)
3156 cnt = PAGE_SIZE - 1;
3157
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003158 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003159 memset(&iter->seq, 0,
3160 sizeof(struct trace_iterator) -
3161 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02003162 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003163
Lai Jiangshan4f535962009-05-18 19:35:34 +08003164 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003165 trace_access_lock(iter->cpu_file);
Steven Rostedt088b1e422008-05-12 21:20:48 +02003166 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003167 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003168 int len = iter->seq.len;
3169
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003170 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003171 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02003172 /* don't print partial lines */
3173 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003174 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003175 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01003176 if (ret != TRACE_TYPE_NO_CONSUME)
3177 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003178
3179 if (iter->seq.len >= cnt)
3180 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003181 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003182 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003183 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02003184
Steven Rostedtb3806b42008-05-12 21:20:46 +02003185 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003186 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3187 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05003188 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003189
3190 /*
3191 * If there was nothing to send to user, inspite of consuming trace
3192 * entries, go back to wait for more entries.
3193 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003194 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003195 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003196
Steven Rostedt107bad82008-05-12 21:21:01 +02003197out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003198 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003199
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003200 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003201}
3202
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003203static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3204 struct pipe_buffer *buf)
3205{
3206 __free_page(buf->page);
3207}
3208
3209static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3210 unsigned int idx)
3211{
3212 __free_page(spd->pages[idx]);
3213}
3214
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003215static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003216 .can_merge = 0,
3217 .map = generic_pipe_buf_map,
3218 .unmap = generic_pipe_buf_unmap,
3219 .confirm = generic_pipe_buf_confirm,
3220 .release = tracing_pipe_buf_release,
3221 .steal = generic_pipe_buf_steal,
3222 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003223};
3224
Steven Rostedt34cd4992009-02-09 12:06:29 -05003225static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003226tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003227{
3228 size_t count;
3229 int ret;
3230
3231 /* Seq buffer is page-sized, exactly what we need. */
3232 for (;;) {
3233 count = iter->seq.len;
3234 ret = print_trace_line(iter);
3235 count = iter->seq.len - count;
3236 if (rem < count) {
3237 rem = 0;
3238 iter->seq.len -= count;
3239 break;
3240 }
3241 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3242 iter->seq.len -= count;
3243 break;
3244 }
3245
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08003246 if (ret != TRACE_TYPE_NO_CONSUME)
3247 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003248 rem -= count;
3249 if (!find_next_entry_inc(iter)) {
3250 rem = 0;
3251 iter->ent = NULL;
3252 break;
3253 }
3254 }
3255
3256 return rem;
3257}
3258
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003259static ssize_t tracing_splice_read_pipe(struct file *filp,
3260 loff_t *ppos,
3261 struct pipe_inode_info *pipe,
3262 size_t len,
3263 unsigned int flags)
3264{
3265 struct page *pages[PIPE_BUFFERS];
3266 struct partial_page partial[PIPE_BUFFERS];
3267 struct trace_iterator *iter = filp->private_data;
3268 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003269 .pages = pages,
3270 .partial = partial,
3271 .nr_pages = 0, /* This gets updated below. */
3272 .flags = flags,
3273 .ops = &tracing_pipe_buf_ops,
3274 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003275 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003276 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003277 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003278 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003279 unsigned int i;
3280
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003281 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003282 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003283 if (unlikely(old_tracer != current_trace && current_trace)) {
3284 old_tracer = current_trace;
3285 *iter->trace = *current_trace;
3286 }
3287 mutex_unlock(&trace_types_lock);
3288
3289 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003290
3291 if (iter->trace->splice_read) {
3292 ret = iter->trace->splice_read(iter, filp,
3293 ppos, pipe, len, flags);
3294 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003295 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003296 }
3297
3298 ret = tracing_wait_pipe(filp);
3299 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003300 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003301
3302 if (!iter->ent && !find_next_entry_inc(iter)) {
3303 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003304 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003305 }
3306
Lai Jiangshan4f535962009-05-18 19:35:34 +08003307 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003308 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003309
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003310 /* Fill as many pages as possible. */
3311 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3312 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003313 if (!pages[i])
3314 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003315
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003316 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003317
3318 /* Copy the data into the page, so we can start over. */
3319 ret = trace_seq_to_buffer(&iter->seq,
3320 page_address(pages[i]),
3321 iter->seq.len);
3322 if (ret < 0) {
3323 __free_page(pages[i]);
3324 break;
3325 }
3326 partial[i].offset = 0;
3327 partial[i].len = iter->seq.len;
3328
Steven Rostedtf9520752009-03-02 14:04:40 -05003329 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003330 }
3331
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003332 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003333 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003334 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003335
3336 spd.nr_pages = i;
3337
3338 return splice_to_pipe(pipe, &spd);
3339
Steven Rostedt34cd4992009-02-09 12:06:29 -05003340out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003341 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003342
3343 return ret;
3344}
3345
Steven Rostedta98a3c32008-05-12 21:20:59 +02003346static ssize_t
3347tracing_entries_read(struct file *filp, char __user *ubuf,
3348 size_t cnt, loff_t *ppos)
3349{
3350 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003351 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003352 int r;
3353
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003354 mutex_lock(&trace_types_lock);
3355 if (!ring_buffer_expanded)
3356 r = sprintf(buf, "%lu (expanded: %lu)\n",
3357 tr->entries >> 10,
3358 trace_buf_size >> 10);
3359 else
3360 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3361 mutex_unlock(&trace_types_lock);
3362
Steven Rostedta98a3c32008-05-12 21:20:59 +02003363 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3364}
3365
3366static ssize_t
3367tracing_entries_write(struct file *filp, const char __user *ubuf,
3368 size_t cnt, loff_t *ppos)
3369{
3370 unsigned long val;
3371 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003372 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003373
Steven Rostedtcffae432008-05-12 21:21:00 +02003374 if (cnt >= sizeof(buf))
3375 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003376
3377 if (copy_from_user(&buf, ubuf, cnt))
3378 return -EFAULT;
3379
3380 buf[cnt] = 0;
3381
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003382 ret = strict_strtoul(buf, 10, &val);
3383 if (ret < 0)
3384 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003385
3386 /* must have at least 1 entry */
3387 if (!val)
3388 return -EINVAL;
3389
3390 mutex_lock(&trace_types_lock);
3391
Steven Rostedtc76f0692008-11-07 22:36:02 -05003392 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003393
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003394 /* disable all cpu buffers */
3395 for_each_tracing_cpu(cpu) {
3396 if (global_trace.data[cpu])
3397 atomic_inc(&global_trace.data[cpu]->disabled);
3398 if (max_tr.data[cpu])
3399 atomic_inc(&max_tr.data[cpu]->disabled);
3400 }
3401
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003402 /* value is in KB */
3403 val <<= 10;
3404
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003405 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003406 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003407 if (ret < 0) {
3408 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003409 goto out;
3410 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003411 }
3412
Jiri Olsacf8517c2009-10-23 19:36:16 -04003413 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003414
Steven Rostedt19384c02008-05-22 00:22:16 -04003415 /* If check pages failed, return ENOMEM */
3416 if (tracing_disabled)
3417 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003418 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003419 for_each_tracing_cpu(cpu) {
3420 if (global_trace.data[cpu])
3421 atomic_dec(&global_trace.data[cpu]->disabled);
3422 if (max_tr.data[cpu])
3423 atomic_dec(&max_tr.data[cpu]->disabled);
3424 }
3425
Steven Rostedtc76f0692008-11-07 22:36:02 -05003426 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003427 max_tr.entries = global_trace.entries;
3428 mutex_unlock(&trace_types_lock);
3429
3430 return cnt;
3431}
3432
Carsten Emdef2942482009-12-06 14:02:44 +01003433static int mark_printk(const char *fmt, ...)
3434{
3435 int ret;
3436 va_list args;
3437 va_start(args, fmt);
3438 ret = trace_vprintk(0, fmt, args);
3439 va_end(args);
3440 return ret;
3441}
3442
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003443static ssize_t
3444tracing_mark_write(struct file *filp, const char __user *ubuf,
3445 size_t cnt, loff_t *fpos)
3446{
3447 char *buf;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003448
Steven Rostedtc76f0692008-11-07 22:36:02 -05003449 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003450 return -EINVAL;
3451
3452 if (cnt > TRACE_BUF_SIZE)
3453 cnt = TRACE_BUF_SIZE;
3454
Carsten Emdec13d2f72009-11-16 20:56:13 +01003455 buf = kmalloc(cnt + 2, GFP_KERNEL);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003456 if (buf == NULL)
3457 return -ENOMEM;
3458
3459 if (copy_from_user(buf, ubuf, cnt)) {
3460 kfree(buf);
3461 return -EFAULT;
3462 }
Carsten Emdec13d2f72009-11-16 20:56:13 +01003463 if (buf[cnt-1] != '\n') {
3464 buf[cnt] = '\n';
3465 buf[cnt+1] = '\0';
3466 } else
3467 buf[cnt] = '\0';
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003468
Carsten Emdef2942482009-12-06 14:02:44 +01003469 cnt = mark_printk("%s", buf);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003470 kfree(buf);
3471 *fpos += cnt;
3472
3473 return cnt;
3474}
3475
Li Zefan13f16d22009-12-08 11:16:11 +08003476static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08003477{
Zhaolei5079f322009-08-25 16:12:56 +08003478 int i;
3479
3480 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08003481 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08003482 "%s%s%s%s", i ? " " : "",
3483 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3484 i == trace_clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08003485 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08003486
Li Zefan13f16d22009-12-08 11:16:11 +08003487 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08003488}
3489
3490static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3491 size_t cnt, loff_t *fpos)
3492{
3493 char buf[64];
3494 const char *clockstr;
3495 int i;
3496
3497 if (cnt >= sizeof(buf))
3498 return -EINVAL;
3499
3500 if (copy_from_user(&buf, ubuf, cnt))
3501 return -EFAULT;
3502
3503 buf[cnt] = 0;
3504
3505 clockstr = strstrip(buf);
3506
3507 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3508 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3509 break;
3510 }
3511 if (i == ARRAY_SIZE(trace_clocks))
3512 return -EINVAL;
3513
3514 trace_clock_id = i;
3515
3516 mutex_lock(&trace_types_lock);
3517
3518 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3519 if (max_tr.buffer)
3520 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3521
3522 mutex_unlock(&trace_types_lock);
3523
3524 *fpos += cnt;
3525
3526 return cnt;
3527}
3528
Li Zefan13f16d22009-12-08 11:16:11 +08003529static int tracing_clock_open(struct inode *inode, struct file *file)
3530{
3531 if (tracing_disabled)
3532 return -ENODEV;
3533 return single_open(file, tracing_clock_show, NULL);
3534}
3535
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003536static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003537 .open = tracing_open_generic,
3538 .read = tracing_max_lat_read,
3539 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003540};
3541
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003542static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003543 .open = tracing_open_generic,
3544 .read = tracing_ctrl_read,
3545 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003546};
3547
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003548static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003549 .open = tracing_open_generic,
3550 .read = tracing_set_trace_read,
3551 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003552};
3553
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003554static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003555 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003556 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003557 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003558 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003559 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003560};
3561
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003562static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003563 .open = tracing_open_generic,
3564 .read = tracing_entries_read,
3565 .write = tracing_entries_write,
3566};
3567
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003568static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003569 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003570 .write = tracing_mark_write,
3571};
3572
Zhaolei5079f322009-08-25 16:12:56 +08003573static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08003574 .open = tracing_clock_open,
3575 .read = seq_read,
3576 .llseek = seq_lseek,
3577 .release = single_release,
Zhaolei5079f322009-08-25 16:12:56 +08003578 .write = tracing_clock_write,
3579};
3580
Steven Rostedt2cadf912008-12-01 22:20:19 -05003581struct ftrace_buffer_info {
3582 struct trace_array *tr;
3583 void *spare;
3584 int cpu;
3585 unsigned int read;
3586};
3587
3588static int tracing_buffers_open(struct inode *inode, struct file *filp)
3589{
3590 int cpu = (int)(long)inode->i_private;
3591 struct ftrace_buffer_info *info;
3592
3593 if (tracing_disabled)
3594 return -ENODEV;
3595
3596 info = kzalloc(sizeof(*info), GFP_KERNEL);
3597 if (!info)
3598 return -ENOMEM;
3599
3600 info->tr = &global_trace;
3601 info->cpu = cpu;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003602 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003603 /* Force reading ring buffer for first read */
3604 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003605
3606 filp->private_data = info;
3607
Lai Jiangshand1e7e022009-04-02 15:16:56 +08003608 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003609}
3610
3611static ssize_t
3612tracing_buffers_read(struct file *filp, char __user *ubuf,
3613 size_t count, loff_t *ppos)
3614{
3615 struct ftrace_buffer_info *info = filp->private_data;
3616 unsigned int pos;
3617 ssize_t ret;
3618 size_t size;
3619
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003620 if (!count)
3621 return 0;
3622
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003623 if (!info->spare)
3624 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3625 if (!info->spare)
3626 return -ENOMEM;
3627
Steven Rostedt2cadf912008-12-01 22:20:19 -05003628 /* Do we have previous read data to read? */
3629 if (info->read < PAGE_SIZE)
3630 goto read;
3631
3632 info->read = 0;
3633
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003634 trace_access_lock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003635 ret = ring_buffer_read_page(info->tr->buffer,
3636 &info->spare,
3637 count,
3638 info->cpu, 0);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003639 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003640 if (ret < 0)
3641 return 0;
3642
3643 pos = ring_buffer_page_len(info->spare);
3644
3645 if (pos < PAGE_SIZE)
3646 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3647
3648read:
3649 size = PAGE_SIZE - info->read;
3650 if (size > count)
3651 size = count;
3652
3653 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003654 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003655 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003656 size -= ret;
3657
Steven Rostedt2cadf912008-12-01 22:20:19 -05003658 *ppos += size;
3659 info->read += size;
3660
3661 return size;
3662}
3663
3664static int tracing_buffers_release(struct inode *inode, struct file *file)
3665{
3666 struct ftrace_buffer_info *info = file->private_data;
3667
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003668 if (info->spare)
3669 ring_buffer_free_read_page(info->tr->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003670 kfree(info);
3671
3672 return 0;
3673}
3674
3675struct buffer_ref {
3676 struct ring_buffer *buffer;
3677 void *page;
3678 int ref;
3679};
3680
3681static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3682 struct pipe_buffer *buf)
3683{
3684 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3685
3686 if (--ref->ref)
3687 return;
3688
3689 ring_buffer_free_read_page(ref->buffer, ref->page);
3690 kfree(ref);
3691 buf->private = 0;
3692}
3693
3694static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3695 struct pipe_buffer *buf)
3696{
3697 return 1;
3698}
3699
3700static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3701 struct pipe_buffer *buf)
3702{
3703 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3704
3705 ref->ref++;
3706}
3707
3708/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003709static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003710 .can_merge = 0,
3711 .map = generic_pipe_buf_map,
3712 .unmap = generic_pipe_buf_unmap,
3713 .confirm = generic_pipe_buf_confirm,
3714 .release = buffer_pipe_buf_release,
3715 .steal = buffer_pipe_buf_steal,
3716 .get = buffer_pipe_buf_get,
3717};
3718
3719/*
3720 * Callback from splice_to_pipe(), if we need to release some pages
3721 * at the end of the spd in case we error'ed out in filling the pipe.
3722 */
3723static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3724{
3725 struct buffer_ref *ref =
3726 (struct buffer_ref *)spd->partial[i].private;
3727
3728 if (--ref->ref)
3729 return;
3730
3731 ring_buffer_free_read_page(ref->buffer, ref->page);
3732 kfree(ref);
3733 spd->partial[i].private = 0;
3734}
3735
3736static ssize_t
3737tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3738 struct pipe_inode_info *pipe, size_t len,
3739 unsigned int flags)
3740{
3741 struct ftrace_buffer_info *info = file->private_data;
3742 struct partial_page partial[PIPE_BUFFERS];
3743 struct page *pages[PIPE_BUFFERS];
3744 struct splice_pipe_desc spd = {
3745 .pages = pages,
3746 .partial = partial,
3747 .flags = flags,
3748 .ops = &buffer_pipe_buf_ops,
3749 .spd_release = buffer_spd_release,
3750 };
3751 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04003752 int entries, size, i;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003753 size_t ret;
3754
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003755 if (*ppos & (PAGE_SIZE - 1)) {
3756 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3757 return -EINVAL;
3758 }
3759
3760 if (len & (PAGE_SIZE - 1)) {
3761 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3762 if (len < PAGE_SIZE)
3763 return -EINVAL;
3764 len &= PAGE_MASK;
3765 }
3766
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003767 trace_access_lock(info->cpu);
Steven Rostedt93459c62009-04-29 00:23:13 -04003768 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3769
3770 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003771 struct page *page;
3772 int r;
3773
3774 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3775 if (!ref)
3776 break;
3777
Steven Rostedt7267fa62009-04-29 00:16:21 -04003778 ref->ref = 1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003779 ref->buffer = info->tr->buffer;
3780 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3781 if (!ref->page) {
3782 kfree(ref);
3783 break;
3784 }
3785
3786 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtf2957f12009-04-29 00:26:30 -04003787 len, info->cpu, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003788 if (r < 0) {
3789 ring_buffer_free_read_page(ref->buffer,
3790 ref->page);
3791 kfree(ref);
3792 break;
3793 }
3794
3795 /*
3796 * zero out any left over data, this is going to
3797 * user land.
3798 */
3799 size = ring_buffer_page_len(ref->page);
3800 if (size < PAGE_SIZE)
3801 memset(ref->page + size, 0, PAGE_SIZE - size);
3802
3803 page = virt_to_page(ref->page);
3804
3805 spd.pages[i] = page;
3806 spd.partial[i].len = PAGE_SIZE;
3807 spd.partial[i].offset = 0;
3808 spd.partial[i].private = (unsigned long)ref;
3809 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003810 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04003811
3812 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003813 }
3814
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003815 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003816 spd.nr_pages = i;
3817
3818 /* did we read anything? */
3819 if (!spd.nr_pages) {
3820 if (flags & SPLICE_F_NONBLOCK)
3821 ret = -EAGAIN;
3822 else
3823 ret = 0;
3824 /* TODO: block */
3825 return ret;
3826 }
3827
3828 ret = splice_to_pipe(pipe, &spd);
3829
3830 return ret;
3831}
3832
3833static const struct file_operations tracing_buffers_fops = {
3834 .open = tracing_buffers_open,
3835 .read = tracing_buffers_read,
3836 .release = tracing_buffers_release,
3837 .splice_read = tracing_buffers_splice_read,
3838 .llseek = no_llseek,
3839};
3840
Steven Rostedtc8d77182009-04-29 18:03:45 -04003841static ssize_t
3842tracing_stats_read(struct file *filp, char __user *ubuf,
3843 size_t count, loff_t *ppos)
3844{
3845 unsigned long cpu = (unsigned long)filp->private_data;
3846 struct trace_array *tr = &global_trace;
3847 struct trace_seq *s;
3848 unsigned long cnt;
3849
Li Zefane4f2d102009-06-15 10:57:28 +08003850 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04003851 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01003852 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04003853
3854 trace_seq_init(s);
3855
3856 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3857 trace_seq_printf(s, "entries: %ld\n", cnt);
3858
3859 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3860 trace_seq_printf(s, "overrun: %ld\n", cnt);
3861
3862 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3863 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3864
Steven Rostedtc8d77182009-04-29 18:03:45 -04003865 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3866
3867 kfree(s);
3868
3869 return count;
3870}
3871
3872static const struct file_operations tracing_stats_fops = {
3873 .open = tracing_open_generic,
3874 .read = tracing_stats_read,
3875};
3876
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003877#ifdef CONFIG_DYNAMIC_FTRACE
3878
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003879int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003880{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003881 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003882}
3883
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003884static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003885tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003886 size_t cnt, loff_t *ppos)
3887{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003888 static char ftrace_dyn_info_buffer[1024];
3889 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003890 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003891 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003892 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003893 int r;
3894
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003895 mutex_lock(&dyn_info_mutex);
3896 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003897
Steven Rostedta26a2a22008-10-31 00:03:22 -04003898 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003899 buf[r++] = '\n';
3900
3901 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3902
3903 mutex_unlock(&dyn_info_mutex);
3904
3905 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003906}
3907
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003908static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003909 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003910 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003911};
3912#endif
3913
3914static struct dentry *d_tracer;
3915
3916struct dentry *tracing_init_dentry(void)
3917{
3918 static int once;
3919
3920 if (d_tracer)
3921 return d_tracer;
3922
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01003923 if (!debugfs_initialized())
3924 return NULL;
3925
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003926 d_tracer = debugfs_create_dir("tracing", NULL);
3927
3928 if (!d_tracer && !once) {
3929 once = 1;
3930 pr_warning("Could not create debugfs directory 'tracing'\n");
3931 return NULL;
3932 }
3933
3934 return d_tracer;
3935}
3936
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003937static struct dentry *d_percpu;
3938
3939struct dentry *tracing_dentry_percpu(void)
3940{
3941 static int once;
3942 struct dentry *d_tracer;
3943
3944 if (d_percpu)
3945 return d_percpu;
3946
3947 d_tracer = tracing_init_dentry();
3948
3949 if (!d_tracer)
3950 return NULL;
3951
3952 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3953
3954 if (!d_percpu && !once) {
3955 once = 1;
3956 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3957 return NULL;
3958 }
3959
3960 return d_percpu;
3961}
3962
3963static void tracing_init_debugfs_percpu(long cpu)
3964{
3965 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003966 struct dentry *d_cpu;
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003967 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3968 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003969
3970 if (cpu > 999 || cpu < 0)
3971 return;
3972
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003973 sprintf(cpu_dir, "cpu%ld", cpu);
3974 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3975 if (!d_cpu) {
3976 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3977 return;
3978 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003979
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003980 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003981 trace_create_file("trace_pipe", 0444, d_cpu,
3982 (void *) cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003983
3984 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003985 trace_create_file("trace", 0644, d_cpu,
3986 (void *) cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04003987
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003988 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3989 (void *) cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04003990
3991 trace_create_file("stats", 0444, d_cpu,
3992 (void *) cpu, &tracing_stats_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003993}
3994
Steven Rostedt60a11772008-05-12 21:20:44 +02003995#ifdef CONFIG_FTRACE_SELFTEST
3996/* Let selftest have access to static functions in this file */
3997#include "trace_selftest.c"
3998#endif
3999
Steven Rostedt577b7852009-02-26 23:43:05 -05004000struct trace_option_dentry {
4001 struct tracer_opt *opt;
4002 struct tracer_flags *flags;
4003 struct dentry *entry;
4004};
4005
4006static ssize_t
4007trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4008 loff_t *ppos)
4009{
4010 struct trace_option_dentry *topt = filp->private_data;
4011 char *buf;
4012
4013 if (topt->flags->val & topt->opt->bit)
4014 buf = "1\n";
4015 else
4016 buf = "0\n";
4017
4018 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4019}
4020
4021static ssize_t
4022trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4023 loff_t *ppos)
4024{
4025 struct trace_option_dentry *topt = filp->private_data;
4026 unsigned long val;
4027 char buf[64];
4028 int ret;
4029
4030 if (cnt >= sizeof(buf))
4031 return -EINVAL;
4032
4033 if (copy_from_user(&buf, ubuf, cnt))
4034 return -EFAULT;
4035
4036 buf[cnt] = 0;
4037
4038 ret = strict_strtoul(buf, 10, &val);
4039 if (ret < 0)
4040 return ret;
4041
Li Zefan8d18eaa2009-12-08 11:17:06 +08004042 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05004043 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004044
4045 if (!!(topt->flags->val & topt->opt->bit) != val) {
4046 mutex_lock(&trace_types_lock);
4047 ret = __set_tracer_option(current_trace, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05004048 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004049 mutex_unlock(&trace_types_lock);
4050 if (ret)
4051 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05004052 }
4053
4054 *ppos += cnt;
4055
4056 return cnt;
4057}
4058
4059
4060static const struct file_operations trace_options_fops = {
4061 .open = tracing_open_generic,
4062 .read = trace_options_read,
4063 .write = trace_options_write,
4064};
4065
Steven Rostedta8259072009-02-26 22:19:12 -05004066static ssize_t
4067trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4068 loff_t *ppos)
4069{
4070 long index = (long)filp->private_data;
4071 char *buf;
4072
4073 if (trace_flags & (1 << index))
4074 buf = "1\n";
4075 else
4076 buf = "0\n";
4077
4078 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4079}
4080
4081static ssize_t
4082trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4083 loff_t *ppos)
4084{
4085 long index = (long)filp->private_data;
4086 char buf[64];
4087 unsigned long val;
4088 int ret;
4089
4090 if (cnt >= sizeof(buf))
4091 return -EINVAL;
4092
4093 if (copy_from_user(&buf, ubuf, cnt))
4094 return -EFAULT;
4095
4096 buf[cnt] = 0;
4097
4098 ret = strict_strtoul(buf, 10, &val);
4099 if (ret < 0)
4100 return ret;
4101
Zhaoleif2d84b62009-08-07 18:55:48 +08004102 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05004103 return -EINVAL;
Zhaoleif2d84b62009-08-07 18:55:48 +08004104 set_tracer_flags(1 << index, val);
Steven Rostedta8259072009-02-26 22:19:12 -05004105
4106 *ppos += cnt;
4107
4108 return cnt;
4109}
4110
Steven Rostedta8259072009-02-26 22:19:12 -05004111static const struct file_operations trace_options_core_fops = {
4112 .open = tracing_open_generic,
4113 .read = trace_options_core_read,
4114 .write = trace_options_core_write,
4115};
4116
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004117struct dentry *trace_create_file(const char *name,
4118 mode_t mode,
4119 struct dentry *parent,
4120 void *data,
4121 const struct file_operations *fops)
4122{
4123 struct dentry *ret;
4124
4125 ret = debugfs_create_file(name, mode, parent, data, fops);
4126 if (!ret)
4127 pr_warning("Could not create debugfs '%s' entry\n", name);
4128
4129 return ret;
4130}
4131
4132
Steven Rostedta8259072009-02-26 22:19:12 -05004133static struct dentry *trace_options_init_dentry(void)
4134{
4135 struct dentry *d_tracer;
4136 static struct dentry *t_options;
4137
4138 if (t_options)
4139 return t_options;
4140
4141 d_tracer = tracing_init_dentry();
4142 if (!d_tracer)
4143 return NULL;
4144
4145 t_options = debugfs_create_dir("options", d_tracer);
4146 if (!t_options) {
4147 pr_warning("Could not create debugfs directory 'options'\n");
4148 return NULL;
4149 }
4150
4151 return t_options;
4152}
4153
Steven Rostedt577b7852009-02-26 23:43:05 -05004154static void
4155create_trace_option_file(struct trace_option_dentry *topt,
4156 struct tracer_flags *flags,
4157 struct tracer_opt *opt)
4158{
4159 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05004160
4161 t_options = trace_options_init_dentry();
4162 if (!t_options)
4163 return;
4164
4165 topt->flags = flags;
4166 topt->opt = opt;
4167
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004168 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05004169 &trace_options_fops);
4170
Steven Rostedt577b7852009-02-26 23:43:05 -05004171}
4172
4173static struct trace_option_dentry *
4174create_trace_option_files(struct tracer *tracer)
4175{
4176 struct trace_option_dentry *topts;
4177 struct tracer_flags *flags;
4178 struct tracer_opt *opts;
4179 int cnt;
4180
4181 if (!tracer)
4182 return NULL;
4183
4184 flags = tracer->flags;
4185
4186 if (!flags || !flags->opts)
4187 return NULL;
4188
4189 opts = flags->opts;
4190
4191 for (cnt = 0; opts[cnt].name; cnt++)
4192 ;
4193
Steven Rostedt0cfe8242009-02-27 10:51:10 -05004194 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05004195 if (!topts)
4196 return NULL;
4197
4198 for (cnt = 0; opts[cnt].name; cnt++)
4199 create_trace_option_file(&topts[cnt], flags,
4200 &opts[cnt]);
4201
4202 return topts;
4203}
4204
4205static void
4206destroy_trace_option_files(struct trace_option_dentry *topts)
4207{
4208 int cnt;
4209
4210 if (!topts)
4211 return;
4212
4213 for (cnt = 0; topts[cnt].opt; cnt++) {
4214 if (topts[cnt].entry)
4215 debugfs_remove(topts[cnt].entry);
4216 }
4217
4218 kfree(topts);
4219}
4220
Steven Rostedta8259072009-02-26 22:19:12 -05004221static struct dentry *
4222create_trace_option_core_file(const char *option, long index)
4223{
4224 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004225
4226 t_options = trace_options_init_dentry();
4227 if (!t_options)
4228 return NULL;
4229
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004230 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05004231 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004232}
4233
4234static __init void create_trace_options_dir(void)
4235{
4236 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004237 int i;
4238
4239 t_options = trace_options_init_dentry();
4240 if (!t_options)
4241 return;
4242
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004243 for (i = 0; trace_options[i]; i++)
4244 create_trace_option_core_file(trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05004245}
4246
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004247static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004248{
4249 struct dentry *d_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004250 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004251
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004252 trace_access_lock_init();
4253
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004254 d_tracer = tracing_init_dentry();
4255
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004256 trace_create_file("tracing_enabled", 0644, d_tracer,
4257 &global_trace, &tracing_ctrl_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004258
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004259 trace_create_file("trace_options", 0644, d_tracer,
4260 NULL, &tracing_iter_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004261
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004262 trace_create_file("tracing_cpumask", 0644, d_tracer,
4263 NULL, &tracing_cpumask_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004264
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004265 trace_create_file("trace", 0644, d_tracer,
4266 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004267
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004268 trace_create_file("available_tracers", 0444, d_tracer,
4269 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004270
Li Zefan339ae5d2009-04-17 10:34:30 +08004271 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004272 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004273
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04004274#ifdef CONFIG_TRACER_MAX_TRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004275 trace_create_file("tracing_max_latency", 0644, d_tracer,
4276 &tracing_max_latency, &tracing_max_lat_fops);
Tim Bird0e950172010-02-25 15:36:43 -08004277#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004278
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004279 trace_create_file("tracing_thresh", 0644, d_tracer,
4280 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004281
Li Zefan339ae5d2009-04-17 10:34:30 +08004282 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004283 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004284
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004285 trace_create_file("trace_pipe", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004286 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004287
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004288 trace_create_file("buffer_size_kb", 0644, d_tracer,
4289 &global_trace, &tracing_entries_fops);
Steven Rostedta98a3c32008-05-12 21:20:59 +02004290
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004291 trace_create_file("trace_marker", 0220, d_tracer,
4292 NULL, &tracing_mark_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004293
Avadh Patel69abe6a2009-04-10 16:04:48 -04004294 trace_create_file("saved_cmdlines", 0444, d_tracer,
4295 NULL, &tracing_saved_cmdlines_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004296
Zhaolei5079f322009-08-25 16:12:56 +08004297 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4298 &trace_clock_fops);
4299
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004300#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004301 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4302 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004303#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02004304#ifdef CONFIG_SYSPROF_TRACER
4305 init_tracer_sysprof_debugfs(d_tracer);
4306#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004307
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004308 create_trace_options_dir();
4309
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004310 for_each_tracing_cpu(cpu)
4311 tracing_init_debugfs_percpu(cpu);
4312
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004313 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004314}
4315
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004316static int trace_panic_handler(struct notifier_block *this,
4317 unsigned long event, void *unused)
4318{
Steven Rostedt944ac422008-10-23 19:26:08 -04004319 if (ftrace_dump_on_oops)
4320 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004321 return NOTIFY_OK;
4322}
4323
4324static struct notifier_block trace_panic_notifier = {
4325 .notifier_call = trace_panic_handler,
4326 .next = NULL,
4327 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4328};
4329
4330static int trace_die_handler(struct notifier_block *self,
4331 unsigned long val,
4332 void *data)
4333{
4334 switch (val) {
4335 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04004336 if (ftrace_dump_on_oops)
4337 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004338 break;
4339 default:
4340 break;
4341 }
4342 return NOTIFY_OK;
4343}
4344
4345static struct notifier_block trace_die_notifier = {
4346 .notifier_call = trace_die_handler,
4347 .priority = 200
4348};
4349
4350/*
4351 * printk is set to max of 1024, we really don't need it that big.
4352 * Nothing should be printing 1000 characters anyway.
4353 */
4354#define TRACE_MAX_PRINT 1000
4355
4356/*
4357 * Define here KERN_TRACE so that we have one place to modify
4358 * it if we decide to change what log level the ftrace dump
4359 * should be at.
4360 */
Steven Rostedt428aee12009-01-14 12:24:42 -05004361#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004362
4363static void
4364trace_printk_seq(struct trace_seq *s)
4365{
4366 /* Probably should print a warning here. */
4367 if (s->len >= 1000)
4368 s->len = 1000;
4369
4370 /* should be zero ended, but we are paranoid. */
4371 s->buffer[s->len] = 0;
4372
4373 printk(KERN_TRACE "%s", s->buffer);
4374
Steven Rostedtf9520752009-03-02 14:04:40 -05004375 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004376}
4377
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004378static void __ftrace_dump(bool disable_tracing)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004379{
Thomas Gleixner445c8952009-12-02 19:49:50 +01004380 static arch_spinlock_t ftrace_dump_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01004381 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004382 /* use static because iter can be a bit big for the stack */
4383 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004384 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004385 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004386 unsigned long flags;
4387 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004388
4389 /* only one dump */
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004390 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004391 arch_spin_lock(&ftrace_dump_lock);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004392 if (dump_ran)
4393 goto out;
4394
4395 dump_ran = 1;
4396
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004397 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004398
4399 if (disable_tracing)
4400 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004401
Steven Rostedtd7690412008-10-01 00:29:53 -04004402 for_each_tracing_cpu(cpu) {
4403 atomic_inc(&global_trace.data[cpu]->disabled);
4404 }
4405
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004406 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4407
Török Edwinb54d3de2008-11-22 13:28:48 +02004408 /* don't look at user memory in panic mode */
4409 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4410
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004411 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4412
Steven Rostedte543ad72009-03-04 18:20:36 -05004413 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004414 iter.tr = &global_trace;
4415 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05004416 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004417
4418 /*
4419 * We need to stop all tracing on all CPUS to read the
4420 * the next buffer. This is a bit expensive, but is
4421 * not done often. We fill all what we can read,
4422 * and then release the locks again.
4423 */
4424
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004425 while (!trace_empty(&iter)) {
4426
4427 if (!cnt)
4428 printk(KERN_TRACE "---------------------------------\n");
4429
4430 cnt++;
4431
4432 /* reset all but tr, trace, and overruns */
4433 memset(&iter.seq, 0,
4434 sizeof(struct trace_iterator) -
4435 offsetof(struct trace_iterator, seq));
4436 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4437 iter.pos = -1;
4438
4439 if (find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08004440 int ret;
4441
4442 ret = print_trace_line(&iter);
4443 if (ret != TRACE_TYPE_NO_CONSUME)
4444 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004445 }
4446
4447 trace_printk_seq(&iter.seq);
4448 }
4449
4450 if (!cnt)
4451 printk(KERN_TRACE " (ftrace buffer empty)\n");
4452 else
4453 printk(KERN_TRACE "---------------------------------\n");
4454
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004455 /* Re-enable tracing if requested */
4456 if (!disable_tracing) {
4457 trace_flags |= old_userobj;
4458
4459 for_each_tracing_cpu(cpu) {
4460 atomic_dec(&global_trace.data[cpu]->disabled);
4461 }
4462 tracing_on();
4463 }
4464
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004465 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004466 arch_spin_unlock(&ftrace_dump_lock);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004467 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004468}
4469
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004470/* By default: disable tracing after the dump */
4471void ftrace_dump(void)
4472{
4473 __ftrace_dump(true);
4474}
4475
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004476__init static int tracer_alloc_buffers(void)
4477{
Steven Rostedt73c51622009-03-11 13:42:01 -04004478 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004479 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304480 int ret = -ENOMEM;
4481
4482 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4483 goto out;
4484
4485 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4486 goto out_free_buffer_mask;
4487
Steven Rostedt73c51622009-03-11 13:42:01 -04004488 /* To save memory, keep the ring buffer size to its minimum */
4489 if (ring_buffer_expanded)
4490 ring_buf_size = trace_buf_size;
4491 else
4492 ring_buf_size = 1;
4493
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304494 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4495 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004496
Steven Rostedtab464282008-05-12 21:21:00 +02004497 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04004498 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004499 TRACE_BUFFER_FLAGS);
4500 if (!global_trace.buffer) {
4501 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4502 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304503 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004504 }
4505 global_trace.entries = ring_buffer_size(global_trace.buffer);
4506
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304507
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004508#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt73c51622009-03-11 13:42:01 -04004509 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004510 TRACE_BUFFER_FLAGS);
4511 if (!max_tr.buffer) {
4512 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4513 WARN_ON(1);
4514 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304515 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004516 }
4517 max_tr.entries = ring_buffer_size(max_tr.buffer);
4518 WARN_ON(max_tr.entries != global_trace.entries);
4519#endif
4520
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004521 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004522 for_each_tracing_cpu(i) {
jolsa@redhat.com566b0aa2009-07-16 21:44:26 +02004523 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Tejun Heo9705f692009-10-29 22:34:13 +09004524 max_tr.data[i] = &per_cpu(max_tr_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004525 }
4526
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004527 trace_init_cmdlines();
4528
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004529 register_tracer(&nop_trace);
Steven Rostedt79fb0768f2009-02-02 21:38:33 -05004530 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004531#ifdef CONFIG_BOOT_TRACER
4532 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004533#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004534 /* All seems OK, enable tracing */
4535 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004536
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004537 atomic_notifier_chain_register(&panic_notifier_list,
4538 &trace_panic_notifier);
4539
4540 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01004541
4542 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004543
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304544out_free_cpumask:
4545 free_cpumask_var(tracing_cpumask);
4546out_free_buffer_mask:
4547 free_cpumask_var(tracing_buffer_mask);
4548out:
4549 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004550}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004551
4552__init static int clear_boot_tracer(void)
4553{
4554 /*
4555 * The default tracer at boot buffer is an init section.
4556 * This function is called in lateinit. If we did not
4557 * find the boot tracer, then clear it out, to prevent
4558 * later registration from accessing the buffer that is
4559 * about to be freed.
4560 */
4561 if (!default_bootup_tracer)
4562 return 0;
4563
4564 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4565 default_bootup_tracer);
4566 default_bootup_tracer = NULL;
4567
4568 return 0;
4569}
4570
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004571early_initcall(tracer_alloc_buffers);
4572fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004573late_initcall(clear_boot_tracer);