blob: ee6a7339cf0e63d84076dc271b9b79144316b693 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020037#include <linux/ctype.h>
38#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020039#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020040#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();
Rusty Russelldd17c8f2009-10-29 22:34:15 +090095 __this_cpu_inc(ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040096}
97
98static inline void ftrace_enable_cpu(void)
99{
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900100 __this_cpu_dec(ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -0400101 preempt_enable();
102}
103
Jason Wessel955b61e2010-08-05 09:22:23 -0500104cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200105
Steven Rostedt944ac422008-10-23 19:26:08 -0400106/*
107 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
108 *
109 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110 * is set, then ftrace_dump is called. This will output the contents
111 * of the ftrace buffers to the console. This is very useful for
112 * capturing traces that lead to crashes and outputing it to a
113 * serial console.
114 *
115 * It is default off, but you can enable it with either specifying
116 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200117 * /proc/sys/kernel/ftrace_dump_on_oops
118 * Set 1 if you want to dump buffers of all CPUs
119 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400120 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200121
122enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400123
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{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200142 if (*str++ != '=' || !*str) {
143 ftrace_dump_on_oops = DUMP_ALL;
144 return 1;
145 }
146
147 if (!strcmp("orig_cpu", str)) {
148 ftrace_dump_on_oops = DUMP_ORIG;
149 return 1;
150 }
151
152 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400153}
154__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200155
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800156unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200157{
158 nsec += 500;
159 do_div(nsec, 1000);
160 return nsec;
161}
162
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200163/*
164 * The global_trace is the descriptor that holds the tracing
165 * buffers for the live tracing. For each CPU, it contains
166 * a link list of pages that will store trace entries. The
167 * page descriptor of the pages in the memory is used to hold
168 * the link list by linking the lru item in the page descriptor
169 * to each of the pages in the buffer per CPU.
170 *
171 * For each active CPU there is a data field that holds the
172 * pages for the buffer for that CPU. Each CPU has the same number
173 * of pages allocated for its buffer.
174 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200175static struct trace_array global_trace;
176
177static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
178
Steven Rostedte77405a2009-09-02 14:17:06 -0400179int filter_current_check_discard(struct ring_buffer *buffer,
180 struct ftrace_event_call *call, void *rec,
Tom Zanussieb02ce02009-04-08 03:15:54 -0500181 struct ring_buffer_event *event)
182{
Steven Rostedte77405a2009-09-02 14:17:06 -0400183 return filter_check_discard(call, rec, buffer, event);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500184}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400185EXPORT_SYMBOL_GPL(filter_current_check_discard);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500186
Steven Rostedt37886f62009-03-17 17:22:06 -0400187cycle_t ftrace_now(int cpu)
188{
189 u64 ts;
190
191 /* Early boot up does not have a buffer yet */
192 if (!global_trace.buffer)
193 return trace_clock_local();
194
195 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
196 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
197
198 return ts;
199}
200
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200201/*
202 * The max_tr is used to snapshot the global_trace when a maximum
203 * latency is reached. Some tracers will use this to store a maximum
204 * trace while it continues examining live traces.
205 *
206 * The buffers for the max_tr are set up the same as the global_trace.
207 * When a snapshot is taken, the link list of the max_tr is swapped
208 * with the link list of the global_trace and the buffers are reset for
209 * the global_trace so the tracing can continue.
210 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200211static struct trace_array max_tr;
212
Tejun Heo9705f692009-10-29 22:34:13 +0900213static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200214
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200215/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200216static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200217
Steven Rostedt90369902008-11-05 16:05:44 -0500218/**
219 * tracing_is_enabled - return tracer_enabled status
220 *
221 * This function is used by other tracers to know the status
222 * of the tracer_enabled flag. Tracers may use this function
223 * to know if it should enable their features when starting
224 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
225 */
226int tracing_is_enabled(void)
227{
228 return tracer_enabled;
229}
230
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200231/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400232 * trace_buf_size is the size in bytes that is allocated
233 * for a buffer. Note, the number of bytes is always rounded
234 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400235 *
236 * This number is purposely set to a low number of 16384.
237 * If the dump on oops happens, it will be much appreciated
238 * to not have to wait for all that output. Anyway this can be
239 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200240 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400241#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400242
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400243static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200244
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200245/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200246static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200247
248/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200249static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200250
251/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200252 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200253 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200254static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200255
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800256/*
257 * serialize the access of the ring buffer
258 *
259 * ring buffer serializes readers, but it is low level protection.
260 * The validity of the events (which returns by ring_buffer_peek() ..etc)
261 * are not protected by ring buffer.
262 *
263 * The content of events may become garbage if we allow other process consumes
264 * these events concurrently:
265 * A) the page of the consumed events may become a normal page
266 * (not reader page) in ring buffer, and this page will be rewrited
267 * by events producer.
268 * B) The page of the consumed events may become a page for splice_read,
269 * and this page will be returned to system.
270 *
271 * These primitives allow multi process access to different cpu ring buffer
272 * concurrently.
273 *
274 * These primitives don't distinguish read-only and read-consume access.
275 * Multi read-only access are also serialized.
276 */
277
278#ifdef CONFIG_SMP
279static DECLARE_RWSEM(all_cpu_access_lock);
280static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
281
282static inline void trace_access_lock(int cpu)
283{
284 if (cpu == TRACE_PIPE_ALL_CPU) {
285 /* gain it for accessing the whole ring buffer. */
286 down_write(&all_cpu_access_lock);
287 } else {
288 /* gain it for accessing a cpu ring buffer. */
289
290 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
291 down_read(&all_cpu_access_lock);
292
293 /* Secondly block other access to this @cpu ring buffer. */
294 mutex_lock(&per_cpu(cpu_access_lock, cpu));
295 }
296}
297
298static inline void trace_access_unlock(int cpu)
299{
300 if (cpu == TRACE_PIPE_ALL_CPU) {
301 up_write(&all_cpu_access_lock);
302 } else {
303 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
304 up_read(&all_cpu_access_lock);
305 }
306}
307
308static inline void trace_access_lock_init(void)
309{
310 int cpu;
311
312 for_each_possible_cpu(cpu)
313 mutex_init(&per_cpu(cpu_access_lock, cpu));
314}
315
316#else
317
318static DEFINE_MUTEX(access_lock);
319
320static inline void trace_access_lock(int cpu)
321{
322 (void)cpu;
323 mutex_lock(&access_lock);
324}
325
326static inline void trace_access_unlock(int cpu)
327{
328 (void)cpu;
329 mutex_unlock(&access_lock);
330}
331
332static inline void trace_access_lock_init(void)
333{
334}
335
336#endif
337
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200338/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200339static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
340
Steven Rostedtee6bce52008-11-12 17:52:37 -0500341/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500342unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400343 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
Li Zefane870e9a2010-07-02 11:07:32 +0800344 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD;
Ingo Molnar4e655512008-05-12 21:20:52 +0200345
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400346static int trace_stop_count;
347static DEFINE_SPINLOCK(tracing_start_lock);
348
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200349/**
350 * trace_wake_up - wake up tasks waiting for trace input
351 *
352 * Simply wakes up any task that is blocked on the trace_wait
353 * queue. These is used with trace_poll for tasks polling the trace.
354 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200355void trace_wake_up(void)
356{
Andrew Morton89f19f02009-09-19 11:55:44 -0700357 int cpu;
358
359 if (trace_flags & TRACE_ITER_BLOCK)
360 return;
Ingo Molnar017730c2008-05-12 21:20:52 +0200361 /*
362 * The runqueue_is_locked() can fail, but this is the best we
363 * have for now:
364 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700365 cpu = get_cpu();
366 if (!runqueue_is_locked(cpu))
Ingo Molnar4e655512008-05-12 21:20:52 +0200367 wake_up(&trace_wait);
Andrew Morton89f19f02009-09-19 11:55:44 -0700368 put_cpu();
Ingo Molnar4e655512008-05-12 21:20:52 +0200369}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200370
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400371static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200372{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400373 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200374
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200375 if (!str)
376 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800377 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200378 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800379 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200380 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400381 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200382 return 1;
383}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400384__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200385
Tim Bird0e950172010-02-25 15:36:43 -0800386static int __init set_tracing_thresh(char *str)
387{
388 unsigned long threshhold;
389 int ret;
390
391 if (!str)
392 return 0;
393 ret = strict_strtoul(str, 0, &threshhold);
394 if (ret < 0)
395 return 0;
396 tracing_thresh = threshhold * 1000;
397 return 1;
398}
399__setup("tracing_thresh=", set_tracing_thresh);
400
Steven Rostedt57f50be2008-05-12 21:20:44 +0200401unsigned long nsecs_to_usecs(unsigned long nsecs)
402{
403 return nsecs / 1000;
404}
405
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200406/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200407static const char *trace_options[] = {
408 "print-parent",
409 "sym-offset",
410 "sym-addr",
411 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200412 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200413 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200414 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200415 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200416 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100417 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500418 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500419 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500420 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200421 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200422 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100423 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200424 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500425 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400426 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400427 "graph-time",
Li Zefane870e9a2010-07-02 11:07:32 +0800428 "record-cmd",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200429 NULL
430};
431
Zhaolei5079f322009-08-25 16:12:56 +0800432static struct {
433 u64 (*func)(void);
434 const char *name;
435} trace_clocks[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
438};
439
440int trace_clock_id;
441
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200442/*
443 * trace_parser_get_init - gets the buffer for trace parser
444 */
445int trace_parser_get_init(struct trace_parser *parser, int size)
446{
447 memset(parser, 0, sizeof(*parser));
448
449 parser->buffer = kmalloc(size, GFP_KERNEL);
450 if (!parser->buffer)
451 return 1;
452
453 parser->size = size;
454 return 0;
455}
456
457/*
458 * trace_parser_put - frees the buffer for trace parser
459 */
460void trace_parser_put(struct trace_parser *parser)
461{
462 kfree(parser->buffer);
463}
464
465/*
466 * trace_get_user - reads the user input string separated by space
467 * (matched by isspace(ch))
468 *
469 * For each string found the 'struct trace_parser' is updated,
470 * and the function returns.
471 *
472 * Returns number of bytes read.
473 *
474 * See kernel/trace/trace.h for 'struct trace_parser' details.
475 */
476int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
477 size_t cnt, loff_t *ppos)
478{
479 char ch;
480 size_t read = 0;
481 ssize_t ret;
482
483 if (!*ppos)
484 trace_parser_clear(parser);
485
486 ret = get_user(ch, ubuf++);
487 if (ret)
488 goto out;
489
490 read++;
491 cnt--;
492
493 /*
494 * The parser is not finished with the last write,
495 * continue reading the user input without skipping spaces.
496 */
497 if (!parser->cont) {
498 /* skip white space */
499 while (cnt && isspace(ch)) {
500 ret = get_user(ch, ubuf++);
501 if (ret)
502 goto out;
503 read++;
504 cnt--;
505 }
506
507 /* only spaces were written */
508 if (isspace(ch)) {
509 *ppos += read;
510 ret = read;
511 goto out;
512 }
513
514 parser->idx = 0;
515 }
516
517 /* read the non-space input */
518 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800519 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200520 parser->buffer[parser->idx++] = ch;
521 else {
522 ret = -EINVAL;
523 goto out;
524 }
525 ret = get_user(ch, ubuf++);
526 if (ret)
527 goto out;
528 read++;
529 cnt--;
530 }
531
532 /* We either got finished input or we have to wait for another call. */
533 if (isspace(ch)) {
534 parser->buffer[parser->idx] = 0;
535 parser->cont = false;
536 } else {
537 parser->cont = true;
538 parser->buffer[parser->idx++] = ch;
539 }
540
541 *ppos += read;
542 ret = read;
543
544out:
545 return ret;
546}
547
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200548ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
549{
550 int len;
551 int ret;
552
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500553 if (!cnt)
554 return 0;
555
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200556 if (s->len <= s->readpos)
557 return -EBUSY;
558
559 len = s->len - s->readpos;
560 if (cnt > len)
561 cnt = len;
562 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500563 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200564 return -EFAULT;
565
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500566 cnt -= ret;
567
Steven Rostedte74da522009-03-04 20:31:11 -0500568 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200569 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200570}
571
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200572static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200573{
574 int len;
575 void *ret;
576
577 if (s->len <= s->readpos)
578 return -EBUSY;
579
580 len = s->len - s->readpos;
581 if (cnt > len)
582 cnt = len;
583 ret = memcpy(buf, s->buffer + s->readpos, cnt);
584 if (!ret)
585 return -EFAULT;
586
Steven Rostedte74da522009-03-04 20:31:11 -0500587 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200588 return cnt;
589}
590
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400591/*
592 * ftrace_max_lock is used to protect the swapping of buffers
593 * when taking a max snapshot. The buffers themselves are
594 * protected by per_cpu spinlocks. But the action of the swap
595 * needs its own lock.
596 *
Thomas Gleixner445c8952009-12-02 19:49:50 +0100597 * This is defined as a arch_spinlock_t in order to help
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400598 * with performance when lockdep debugging is enabled.
599 *
600 * It is also used in other places outside the update_max_tr
601 * so it needs to be defined outside of the
602 * CONFIG_TRACER_MAX_TRACE.
603 */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100604static arch_spinlock_t ftrace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100605 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400606
Tim Bird0e950172010-02-25 15:36:43 -0800607unsigned long __read_mostly tracing_thresh;
608
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400609#ifdef CONFIG_TRACER_MAX_TRACE
610unsigned long __read_mostly tracing_max_latency;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400611
612/*
613 * Copy the new maximum trace into the separate maximum-trace
614 * structure. (this way the maximum trace is permanently saved,
615 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
616 */
617static void
618__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
619{
620 struct trace_array_cpu *data = tr->data[cpu];
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300621 struct trace_array_cpu *max_data;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400622
623 max_tr.cpu = cpu;
624 max_tr.time_start = data->preempt_timestamp;
625
Steven Rostedt8248ac02009-09-02 12:27:41 -0400626 max_data = max_tr.data[cpu];
627 max_data->saved_latency = tracing_max_latency;
628 max_data->critical_start = data->critical_start;
629 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400630
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300631 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -0400632 max_data->pid = tsk->pid;
633 max_data->uid = task_uid(tsk);
634 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
635 max_data->policy = tsk->policy;
636 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400637
638 /* record this tasks comm */
639 tracing_record_cmdline(tsk);
640}
641
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200642/**
643 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
644 * @tr: tracer
645 * @tsk: the task with the latency
646 * @cpu: The cpu that initiated the trace.
647 *
648 * Flip the buffers between the @tr and the max_tr and record information
649 * about which task was the cause of this latency.
650 */
Ingo Molnare309b412008-05-12 21:20:51 +0200651void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
653{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400654 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200655
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400656 if (trace_stop_count)
657 return;
658
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200659 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900660 if (!current_trace->use_max_tr) {
661 WARN_ON_ONCE(1);
662 return;
663 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100664 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400665
666 tr->buffer = max_tr.buffer;
667 max_tr.buffer = buf;
668
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200669 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100670 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200671}
672
673/**
674 * update_max_tr_single - only copy one trace over, and reset the rest
675 * @tr - tracer
676 * @tsk - task with the latency
677 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200678 *
679 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200680 */
Ingo Molnare309b412008-05-12 21:20:51 +0200681void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200682update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
683{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400684 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200685
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400686 if (trace_stop_count)
687 return;
688
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200689 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900690 if (!current_trace->use_max_tr) {
691 WARN_ON_ONCE(1);
692 return;
693 }
694
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100695 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200696
Steven Rostedtd7690412008-10-01 00:29:53 -0400697 ftrace_disable_cpu();
698
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400699 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
700
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400701 if (ret == -EBUSY) {
702 /*
703 * We failed to swap the buffer due to a commit taking
704 * place on this CPU. We fail to record, but we reset
705 * the max trace buffer (no one writes directly to it)
706 * and flag that it failed.
707 */
708 trace_array_printk(&max_tr, _THIS_IP_,
709 "Failed to swap buffers due to commit in progress\n");
710 }
711
Steven Rostedtd7690412008-10-01 00:29:53 -0400712 ftrace_enable_cpu();
713
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400714 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200715
716 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100717 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200718}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400719#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200720
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200721/**
722 * register_tracer - register a tracer with the ftrace system.
723 * @type - the plugin for the tracer
724 *
725 * Register a new plugin tracer.
726 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200727int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100728__releases(kernel_lock)
729__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730{
731 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200732 int ret = 0;
733
734 if (!type->name) {
735 pr_info("Tracer must have a name\n");
736 return -1;
737 }
738
Dan Carpenter24a461d2010-07-10 12:06:44 +0200739 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +0800740 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
741 return -1;
742 }
743
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200744 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100745
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100746 tracing_selftest_running = true;
747
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200748 for (t = trace_types; t; t = t->next) {
749 if (strcmp(type->name, t->name) == 0) {
750 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +0800751 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200752 type->name);
753 ret = -1;
754 goto out;
755 }
756 }
757
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100758 if (!type->set_flag)
759 type->set_flag = &dummy_set_flag;
760 if (!type->flags)
761 type->flags = &dummy_tracer_flags;
762 else
763 if (!type->flags->opts)
764 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100765 if (!type->wait_pipe)
766 type->wait_pipe = default_wait_pipe;
767
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100768
Steven Rostedt60a11772008-05-12 21:20:44 +0200769#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500770 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200771 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200772 struct trace_array *tr = &global_trace;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100773
Steven Rostedt60a11772008-05-12 21:20:44 +0200774 /*
775 * Run a selftest on this tracer.
776 * Here we reset the trace buffer, and set the current
777 * tracer to be this tracer. The tracer can then run some
778 * internal tracing to verify that everything is in order.
779 * If we fail, we do not register this tracer.
780 */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400781 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100782
Steven Rostedt60a11772008-05-12 21:20:44 +0200783 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200784 /* the test is responsible for initializing and enabling */
785 pr_info("Testing tracer %s: ", type->name);
786 ret = type->selftest(type, tr);
787 /* the test is responsible for resetting too */
788 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200789 if (ret) {
790 printk(KERN_CONT "FAILED!\n");
791 goto out;
792 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200793 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400794 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100795
Steven Rostedt60a11772008-05-12 21:20:44 +0200796 printk(KERN_CONT "PASSED\n");
797 }
798#endif
799
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200800 type->next = trace_types;
801 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200802
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200803 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100804 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200805 mutex_unlock(&trace_types_lock);
806
Steven Rostedtdac74942009-02-05 01:13:38 -0500807 if (ret || !default_bootup_tracer)
808 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500809
Li Zefanee6c2c12009-09-18 14:06:47 +0800810 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -0500811 goto out_unlock;
812
813 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
814 /* Do we want this tracer to start on bootup? */
815 tracing_set_tracer(type->name);
816 default_bootup_tracer = NULL;
817 /* disable other selftests, since this will break it. */
818 tracing_selftest_disabled = 1;
819#ifdef CONFIG_FTRACE_STARTUP_TEST
820 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
821 type->name);
822#endif
823
824 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200825 return ret;
826}
827
828void unregister_tracer(struct tracer *type)
829{
830 struct tracer **t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200831
832 mutex_lock(&trace_types_lock);
833 for (t = &trace_types; *t; t = &(*t)->next) {
834 if (*t == type)
835 goto found;
836 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800837 pr_info("Tracer %s not registered\n", type->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200838 goto out;
839
840 found:
841 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200842
843 if (type == current_trace && tracer_enabled) {
844 tracer_enabled = 0;
845 tracing_stop();
846 if (current_trace->stop)
847 current_trace->stop(&global_trace);
848 current_trace = &nop_trace;
849 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800850out:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200851 mutex_unlock(&trace_types_lock);
852}
853
Steven Rostedt283740c2010-03-12 19:48:41 -0500854static void __tracing_reset(struct ring_buffer *buffer, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200855{
Steven Rostedtd7690412008-10-01 00:29:53 -0400856 ftrace_disable_cpu();
Steven Rostedt283740c2010-03-12 19:48:41 -0500857 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400858 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200859}
860
Steven Rostedtf6339032009-09-04 12:35:16 -0400861void tracing_reset(struct trace_array *tr, int cpu)
862{
863 struct ring_buffer *buffer = tr->buffer;
864
865 ring_buffer_record_disable(buffer);
866
867 /* Make sure all commits have finished */
868 synchronize_sched();
Steven Rostedt283740c2010-03-12 19:48:41 -0500869 __tracing_reset(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -0400870
871 ring_buffer_record_enable(buffer);
872}
873
Pekka J Enberg213cc062008-12-19 12:08:39 +0200874void tracing_reset_online_cpus(struct trace_array *tr)
875{
Steven Rostedt621968c2009-09-04 12:02:35 -0400876 struct ring_buffer *buffer = tr->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +0200877 int cpu;
878
Steven Rostedt621968c2009-09-04 12:02:35 -0400879 ring_buffer_record_disable(buffer);
880
881 /* Make sure all commits have finished */
882 synchronize_sched();
883
Pekka J Enberg213cc062008-12-19 12:08:39 +0200884 tr->time_start = ftrace_now(tr->cpu);
885
886 for_each_online_cpu(cpu)
Steven Rostedt283740c2010-03-12 19:48:41 -0500887 __tracing_reset(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -0400888
889 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +0200890}
891
Steven Rostedt9456f0f2009-05-06 21:54:09 -0400892void tracing_reset_current(int cpu)
893{
894 tracing_reset(&global_trace, cpu);
895}
896
897void tracing_reset_current_online_cpus(void)
898{
899 tracing_reset_online_cpus(&global_trace);
900}
901
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200902#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100903#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200904static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
905static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
906static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
907static int cmdline_idx;
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100908static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200909
Steven Rostedt25b0b442008-05-12 21:21:00 +0200910/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100911static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200912
913static void trace_init_cmdlines(void)
914{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100915 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
916 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200917 cmdline_idx = 0;
918}
919
Carsten Emdeb5130b12009-09-13 01:43:07 +0200920int is_tracing_stopped(void)
921{
922 return trace_stop_count;
923}
924
Steven Rostedt0f048702008-11-05 16:05:44 -0500925/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500926 * ftrace_off_permanent - disable all ftrace code permanently
927 *
928 * This should only be called when a serious anomally has
929 * been detected. This will turn off the function tracing,
930 * ring buffers, and other tracing utilites. It takes no
931 * locks and can be called from any context.
932 */
933void ftrace_off_permanent(void)
934{
935 tracing_disabled = 1;
936 ftrace_stop();
937 tracing_off_permanent();
938}
939
940/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500941 * tracing_start - quick start of the tracer
942 *
943 * If tracing is enabled but was stopped by tracing_stop,
944 * this will start the tracer back up.
945 */
946void tracing_start(void)
947{
948 struct ring_buffer *buffer;
949 unsigned long flags;
950
951 if (tracing_disabled)
952 return;
953
954 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500955 if (--trace_stop_count) {
956 if (trace_stop_count < 0) {
957 /* Someone screwed up their debugging */
958 WARN_ON_ONCE(1);
959 trace_stop_count = 0;
960 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500961 goto out;
962 }
963
Steven Rostedta2f80712010-03-12 19:56:00 -0500964 /* Prevent the buffers from switching */
965 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -0500966
967 buffer = global_trace.buffer;
968 if (buffer)
969 ring_buffer_record_enable(buffer);
970
971 buffer = max_tr.buffer;
972 if (buffer)
973 ring_buffer_record_enable(buffer);
974
Steven Rostedta2f80712010-03-12 19:56:00 -0500975 arch_spin_unlock(&ftrace_max_lock);
976
Steven Rostedt0f048702008-11-05 16:05:44 -0500977 ftrace_start();
978 out:
979 spin_unlock_irqrestore(&tracing_start_lock, flags);
980}
981
982/**
983 * tracing_stop - quick stop of the tracer
984 *
985 * Light weight way to stop tracing. Use in conjunction with
986 * tracing_start.
987 */
988void tracing_stop(void)
989{
990 struct ring_buffer *buffer;
991 unsigned long flags;
992
993 ftrace_stop();
994 spin_lock_irqsave(&tracing_start_lock, flags);
995 if (trace_stop_count++)
996 goto out;
997
Steven Rostedta2f80712010-03-12 19:56:00 -0500998 /* Prevent the buffers from switching */
999 arch_spin_lock(&ftrace_max_lock);
1000
Steven Rostedt0f048702008-11-05 16:05:44 -05001001 buffer = global_trace.buffer;
1002 if (buffer)
1003 ring_buffer_record_disable(buffer);
1004
1005 buffer = max_tr.buffer;
1006 if (buffer)
1007 ring_buffer_record_disable(buffer);
1008
Steven Rostedta2f80712010-03-12 19:56:00 -05001009 arch_spin_unlock(&ftrace_max_lock);
1010
Steven Rostedt0f048702008-11-05 16:05:44 -05001011 out:
1012 spin_unlock_irqrestore(&tracing_start_lock, flags);
1013}
1014
Ingo Molnare309b412008-05-12 21:20:51 +02001015void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016
Ingo Molnare309b412008-05-12 21:20:51 +02001017static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001018{
Carsten Emdea635cf02009-03-18 09:00:41 +01001019 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001020
1021 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1022 return;
1023
1024 /*
1025 * It's not the end of the world if we don't get
1026 * the lock, but we also don't want to spin
1027 * nor do we want to disable interrupts,
1028 * so if we miss here, then better luck next time.
1029 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001030 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001031 return;
1032
1033 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001034 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001035 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1036
Carsten Emdea635cf02009-03-18 09:00:41 +01001037 /*
1038 * Check whether the cmdline buffer at idx has a pid
1039 * mapped. We are going to overwrite that entry so we
1040 * need to clear the map_pid_to_cmdline. Otherwise we
1041 * would read the new comm for the old pid.
1042 */
1043 pid = map_cmdline_to_pid[idx];
1044 if (pid != NO_CMDLINE_MAP)
1045 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001046
Carsten Emdea635cf02009-03-18 09:00:41 +01001047 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001048 map_pid_to_cmdline[tsk->pid] = idx;
1049
1050 cmdline_idx = idx;
1051 }
1052
1053 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1054
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001055 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056}
1057
Steven Rostedt4ca530852009-03-16 19:20:15 -04001058void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001059{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001060 unsigned map;
1061
Steven Rostedt4ca530852009-03-16 19:20:15 -04001062 if (!pid) {
1063 strcpy(comm, "<idle>");
1064 return;
1065 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001066
Steven Rostedt74bf4072010-01-25 15:11:53 -05001067 if (WARN_ON_ONCE(pid < 0)) {
1068 strcpy(comm, "<XXX>");
1069 return;
1070 }
1071
Steven Rostedt4ca530852009-03-16 19:20:15 -04001072 if (pid > PID_MAX_DEFAULT) {
1073 strcpy(comm, "<...>");
1074 return;
1075 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001076
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001077 preempt_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001078 arch_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001079 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001080 if (map != NO_CMDLINE_MAP)
1081 strcpy(comm, saved_cmdlines[map]);
1082 else
1083 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001084
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001085 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001086 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001087}
1088
Ingo Molnare309b412008-05-12 21:20:51 +02001089void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001090{
Thomas Gleixner18aecd32009-03-18 08:56:58 +01001091 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1092 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001093 return;
1094
1095 trace_save_cmdline(tsk);
1096}
1097
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001098void
Steven Rostedt38697052008-10-01 13:14:09 -04001099tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1100 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001101{
1102 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001103
Steven Rostedt777e2082008-09-29 23:02:42 -04001104 entry->preempt_count = pc & 0xff;
1105 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedt637e7e82009-09-11 13:55:35 -04001106 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -04001107 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001108#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001109 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001110#else
1111 TRACE_FLAG_IRQS_NOSUPPORT |
1112#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001113 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1114 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1115 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1116}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001117EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118
Steven Rostedte77405a2009-09-02 14:17:06 -04001119struct ring_buffer_event *
1120trace_buffer_lock_reserve(struct ring_buffer *buffer,
1121 int type,
1122 unsigned long len,
1123 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001124{
1125 struct ring_buffer_event *event;
1126
Steven Rostedte77405a2009-09-02 14:17:06 -04001127 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001128 if (event != NULL) {
1129 struct trace_entry *ent = ring_buffer_event_data(event);
1130
1131 tracing_generic_entry_update(ent, flags, pc);
1132 ent->type = type;
1133 }
1134
1135 return event;
1136}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001137
Steven Rostedte77405a2009-09-02 14:17:06 -04001138static inline void
1139__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1140 struct ring_buffer_event *event,
1141 unsigned long flags, int pc,
1142 int wake)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001143{
Steven Rostedte77405a2009-09-02 14:17:06 -04001144 ring_buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001145
Steven Rostedte77405a2009-09-02 14:17:06 -04001146 ftrace_trace_stack(buffer, flags, 6, pc);
1147 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001148
1149 if (wake)
1150 trace_wake_up();
1151}
1152
Steven Rostedte77405a2009-09-02 14:17:06 -04001153void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1154 struct ring_buffer_event *event,
1155 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001156{
Steven Rostedte77405a2009-09-02 14:17:06 -04001157 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001158}
1159
Steven Rostedtef5580d2009-02-27 19:38:04 -05001160struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001161trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1162 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001163 unsigned long flags, int pc)
1164{
Steven Rostedte77405a2009-09-02 14:17:06 -04001165 *current_rb = global_trace.buffer;
1166 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001167 type, len, flags, pc);
1168}
Steven Rostedt94487d62009-05-05 19:22:53 -04001169EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001170
Steven Rostedte77405a2009-09-02 14:17:06 -04001171void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1172 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001173 unsigned long flags, int pc)
1174{
Steven Rostedte77405a2009-09-02 14:17:06 -04001175 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001176}
Steven Rostedt94487d62009-05-05 19:22:53 -04001177EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001178
Steven Rostedte77405a2009-09-02 14:17:06 -04001179void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1180 struct ring_buffer_event *event,
1181 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001182{
Steven Rostedte77405a2009-09-02 14:17:06 -04001183 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001184}
Steven Rostedt94487d62009-05-05 19:22:53 -04001185EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
Steven Rostedt77d9f462009-04-02 01:16:59 -04001186
Steven Rostedte77405a2009-09-02 14:17:06 -04001187void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1188 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001189{
Steven Rostedte77405a2009-09-02 14:17:06 -04001190 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001191}
Steven Rostedt12acd472009-04-17 16:01:56 -04001192EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001193
Ingo Molnare309b412008-05-12 21:20:51 +02001194void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001195trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001196 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1197 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001198{
Tom Zanussie1112b42009-03-31 00:48:49 -05001199 struct ftrace_event_call *call = &event_function;
Steven Rostedte77405a2009-09-02 14:17:06 -04001200 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001201 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001202 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001203
Steven Rostedtd7690412008-10-01 00:29:53 -04001204 /* If we are reading the ring buffer, don't trace */
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001205 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04001206 return;
1207
Steven Rostedte77405a2009-09-02 14:17:06 -04001208 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001209 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001210 if (!event)
1211 return;
1212 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001213 entry->ip = ip;
1214 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001215
Steven Rostedte77405a2009-09-02 14:17:06 -04001216 if (!filter_check_discard(call, entry, buffer, event))
1217 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001218}
1219
Ingo Molnare309b412008-05-12 21:20:51 +02001220void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001221ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -04001222 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1223 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001224{
1225 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001226 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001227}
1228
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001229#ifdef CONFIG_STACKTRACE
Steven Rostedte77405a2009-09-02 14:17:06 -04001230static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001231 unsigned long flags,
1232 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +02001233{
Tom Zanussie1112b42009-03-31 00:48:49 -05001234 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001235 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001236 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001237 struct stack_trace trace;
1238
Steven Rostedte77405a2009-09-02 14:17:06 -04001239 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001240 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001241 if (!event)
1242 return;
1243 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001244 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +02001245
1246 trace.nr_entries = 0;
1247 trace.max_entries = FTRACE_STACK_ENTRIES;
1248 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -04001249 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +02001250
1251 save_stack_trace(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001252 if (!filter_check_discard(call, entry, buffer, event))
1253 ring_buffer_unlock_commit(buffer, event);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001254}
1255
Steven Rostedte77405a2009-09-02 14:17:06 -04001256void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1257 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001258{
1259 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1260 return;
1261
Steven Rostedte77405a2009-09-02 14:17:06 -04001262 __ftrace_trace_stack(buffer, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -05001263}
1264
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001265void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1266 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001267{
Steven Rostedte77405a2009-09-02 14:17:06 -04001268 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -04001269}
1270
Steven Rostedt03889382009-12-11 09:48:22 -05001271/**
1272 * trace_dump_stack - record a stack back trace in the trace buffer
1273 */
1274void trace_dump_stack(void)
1275{
1276 unsigned long flags;
1277
1278 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001279 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001280
1281 local_save_flags(flags);
1282
1283 /* skipping 3 traces, seems to get us at the caller of this function */
1284 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1285}
1286
Steven Rostedt91e86e52010-11-10 12:56:12 +01001287static DEFINE_PER_CPU(int, user_stack_count);
1288
Steven Rostedte77405a2009-09-02 14:17:06 -04001289void
1290ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001291{
Tom Zanussie1112b42009-03-31 00:48:49 -05001292 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001293 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001294 struct userstack_entry *entry;
1295 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001296
1297 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1298 return;
1299
Steven Rostedtb6345872010-03-12 20:03:30 -05001300 /*
1301 * NMIs can not handle page faults, even with fix ups.
1302 * The save user stack can (and often does) fault.
1303 */
1304 if (unlikely(in_nmi()))
1305 return;
1306
Steven Rostedt91e86e52010-11-10 12:56:12 +01001307 /*
1308 * prevent recursion, since the user stack tracing may
1309 * trigger other kernel events.
1310 */
1311 preempt_disable();
1312 if (__this_cpu_read(user_stack_count))
1313 goto out;
1314
1315 __this_cpu_inc(user_stack_count);
1316
1317
1318
Steven Rostedte77405a2009-09-02 14:17:06 -04001319 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001320 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001321 if (!event)
1322 return;
1323 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001324
Steven Rostedt48659d32009-09-11 11:36:23 -04001325 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001326 memset(&entry->caller, 0, sizeof(entry->caller));
1327
1328 trace.nr_entries = 0;
1329 trace.max_entries = FTRACE_STACK_ENTRIES;
1330 trace.skip = 0;
1331 trace.entries = entry->caller;
1332
1333 save_stack_trace_user(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001334 if (!filter_check_discard(call, entry, buffer, event))
1335 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001336
1337 __this_cpu_dec(user_stack_count);
1338
1339 out:
1340 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02001341}
1342
Hannes Eder4fd27352009-02-10 19:44:12 +01001343#ifdef UNUSED
1344static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001345{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001346 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001347}
Hannes Eder4fd27352009-02-10 19:44:12 +01001348#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001349
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001350#endif /* CONFIG_STACKTRACE */
1351
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001352/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001353 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001354 *
1355 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001356int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001357{
Thomas Gleixner445c8952009-12-02 19:49:50 +01001358 static arch_spinlock_t trace_buf_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001359 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001360 static u32 trace_buf[TRACE_BUF_SIZE];
1361
Tom Zanussie1112b42009-03-31 00:48:49 -05001362 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001363 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001364 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001365 struct trace_array *tr = &global_trace;
1366 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001367 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001368 unsigned long flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001369 int disable;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001370 int cpu, len = 0, size, pc;
1371
1372 if (unlikely(tracing_selftest_running || tracing_disabled))
1373 return 0;
1374
1375 /* Don't pollute graph traces with trace_vprintk internals */
1376 pause_graph_tracing();
1377
1378 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04001379 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001380 cpu = raw_smp_processor_id();
1381 data = tr->data[cpu];
1382
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001383 disable = atomic_inc_return(&data->disabled);
1384 if (unlikely(disable != 1))
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001385 goto out;
1386
Steven Rostedt80370cb2009-03-10 17:16:35 -04001387 /* Lockdep uses trace_printk for lock tracing */
1388 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001389 arch_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001390 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1391
1392 if (len > TRACE_BUF_SIZE || len < 0)
1393 goto out_unlock;
1394
1395 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedte77405a2009-09-02 14:17:06 -04001396 buffer = tr->buffer;
1397 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1398 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001399 if (!event)
1400 goto out_unlock;
1401 entry = ring_buffer_event_data(event);
1402 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001403 entry->fmt = fmt;
1404
1405 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
Steven Rostedtd9313692010-01-06 17:27:11 -05001406 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001407 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001408 ftrace_trace_stack(buffer, flags, 6, pc);
1409 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001410
1411out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001412 arch_spin_unlock(&trace_buf_lock);
Steven Rostedt80370cb2009-03-10 17:16:35 -04001413 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001414
1415out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001416 atomic_dec_return(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -04001417 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001418 unpause_graph_tracing();
1419
1420 return len;
1421}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001422EXPORT_SYMBOL_GPL(trace_vbprintk);
1423
Steven Rostedt659372d2009-09-03 19:11:07 -04001424int trace_array_printk(struct trace_array *tr,
1425 unsigned long ip, const char *fmt, ...)
1426{
1427 int ret;
1428 va_list ap;
1429
1430 if (!(trace_flags & TRACE_ITER_PRINTK))
1431 return 0;
1432
1433 va_start(ap, fmt);
1434 ret = trace_array_vprintk(tr, ip, fmt, ap);
1435 va_end(ap);
1436 return ret;
1437}
1438
1439int trace_array_vprintk(struct trace_array *tr,
1440 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001441{
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001442 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001443 static char trace_buf[TRACE_BUF_SIZE];
1444
Tom Zanussie1112b42009-03-31 00:48:49 -05001445 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001446 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001447 struct ring_buffer *buffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001448 struct trace_array_cpu *data;
1449 int cpu, len = 0, size, pc;
1450 struct print_entry *entry;
1451 unsigned long irq_flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001452 int disable;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001453
1454 if (tracing_disabled || tracing_selftest_running)
1455 return 0;
1456
1457 pc = preempt_count();
1458 preempt_disable_notrace();
1459 cpu = raw_smp_processor_id();
1460 data = tr->data[cpu];
1461
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001462 disable = atomic_inc_return(&data->disabled);
1463 if (unlikely(disable != 1))
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001464 goto out;
1465
1466 pause_graph_tracing();
1467 raw_local_irq_save(irq_flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001468 arch_spin_lock(&trace_buf_lock);
Carsten Emdef2942482009-12-06 14:02:44 +01001469 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001470
1471 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04001472 buffer = tr->buffer;
1473 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1474 irq_flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001475 if (!event)
1476 goto out_unlock;
1477 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001478 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001479
1480 memcpy(&entry->buf, trace_buf, len);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001481 entry->buf[len] = '\0';
Steven Rostedtd9313692010-01-06 17:27:11 -05001482 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001483 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001484 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1485 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001486
1487 out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001488 arch_spin_unlock(&trace_buf_lock);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001489 raw_local_irq_restore(irq_flags);
1490 unpause_graph_tracing();
1491 out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001492 atomic_dec_return(&data->disabled);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001493 preempt_enable_notrace();
1494
1495 return len;
1496}
Steven Rostedt659372d2009-09-03 19:11:07 -04001497
1498int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1499{
Steven Rostedta813a152009-10-09 01:41:35 -04001500 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04001501}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001502EXPORT_SYMBOL_GPL(trace_vprintk);
1503
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001504static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001505{
Steven Rostedtd7690412008-10-01 00:29:53 -04001506 /* Don't allow ftrace to trace into the ring buffers */
1507 ftrace_disable_cpu();
1508
Steven Rostedt5a90f572008-09-03 17:42:51 -04001509 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001510 if (iter->buffer_iter[iter->cpu])
1511 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1512
1513 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001514}
1515
Ingo Molnare309b412008-05-12 21:20:51 +02001516static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001517peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1518 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001519{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001520 struct ring_buffer_event *event;
1521 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001522
Steven Rostedtd7690412008-10-01 00:29:53 -04001523 /* Don't allow ftrace to trace into the ring buffers */
1524 ftrace_disable_cpu();
1525
1526 if (buf_iter)
1527 event = ring_buffer_iter_peek(buf_iter, ts);
1528 else
Steven Rostedtbc21b472010-03-31 19:49:26 -04001529 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1530 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001531
1532 ftrace_enable_cpu();
1533
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001534 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001535}
Steven Rostedtd7690412008-10-01 00:29:53 -04001536
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001537static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001538__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1539 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001540{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001541 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08001543 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001544 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001545 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001546 int next_cpu = -1;
1547 int cpu;
1548
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001549 /*
1550 * If we are in a per_cpu trace file, don't bother by iterating over
1551 * all cpu and peek directly.
1552 */
1553 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1554 if (ring_buffer_empty_cpu(buffer, cpu_file))
1555 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001556 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001557 if (ent_cpu)
1558 *ent_cpu = cpu_file;
1559
1560 return ent;
1561 }
1562
Steven Rostedtab464282008-05-12 21:21:00 +02001563 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001564
1565 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001566 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001567
Steven Rostedtbc21b472010-03-31 19:49:26 -04001568 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001569
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001570 /*
1571 * Pick the entry with the smallest timestamp:
1572 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001573 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001574 next = ent;
1575 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001576 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001577 next_lost = lost_events;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001578 }
1579 }
1580
1581 if (ent_cpu)
1582 *ent_cpu = next_cpu;
1583
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001584 if (ent_ts)
1585 *ent_ts = next_ts;
1586
Steven Rostedtbc21b472010-03-31 19:49:26 -04001587 if (missing_events)
1588 *missing_events = next_lost;
1589
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001590 return next;
1591}
1592
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001593/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001594struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1595 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001596{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001597 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001598}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001599
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001600/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05001601void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001602{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001603 iter->ent = __find_next_entry(iter, &iter->cpu,
1604 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001605
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001606 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001607 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001608
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001609 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001610}
1611
Ingo Molnare309b412008-05-12 21:20:51 +02001612static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001613{
Steven Rostedtd7690412008-10-01 00:29:53 -04001614 /* Don't allow ftrace to trace into the ring buffers */
1615 ftrace_disable_cpu();
Steven Rostedtbc21b472010-03-31 19:49:26 -04001616 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1617 &iter->lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001618 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001619}
1620
Ingo Molnare309b412008-05-12 21:20:51 +02001621static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001622{
1623 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001624 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001625 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001626
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001627 WARN_ON_ONCE(iter->leftover);
1628
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001629 (*pos)++;
1630
1631 /* can't go backwards */
1632 if (iter->idx > i)
1633 return NULL;
1634
1635 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05001636 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001637 else
1638 ent = iter;
1639
1640 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05001641 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001642
1643 iter->pos = *pos;
1644
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001645 return ent;
1646}
1647
Jason Wessel955b61e2010-08-05 09:22:23 -05001648void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001649{
1650 struct trace_array *tr = iter->tr;
1651 struct ring_buffer_event *event;
1652 struct ring_buffer_iter *buf_iter;
1653 unsigned long entries = 0;
1654 u64 ts;
1655
1656 tr->data[cpu]->skipped_entries = 0;
1657
1658 if (!iter->buffer_iter[cpu])
1659 return;
1660
1661 buf_iter = iter->buffer_iter[cpu];
1662 ring_buffer_iter_reset(buf_iter);
1663
1664 /*
1665 * We could have the case with the max latency tracers
1666 * that a reset never took place on a cpu. This is evident
1667 * by the timestamp being before the start of the buffer.
1668 */
1669 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1670 if (ts >= iter->tr->time_start)
1671 break;
1672 entries++;
1673 ring_buffer_read(buf_iter, NULL);
1674 }
1675
1676 tr->data[cpu]->skipped_entries = entries;
1677}
1678
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001679/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001680 * The current tracer is copied to avoid a global locking
1681 * all around.
1682 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001683static void *s_start(struct seq_file *m, loff_t *pos)
1684{
1685 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001686 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001687 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001688 void *p = NULL;
1689 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001690 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001691
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001692 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001693 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001694 if (unlikely(old_tracer != current_trace && current_trace)) {
1695 old_tracer = current_trace;
1696 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001697 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001698 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001699
1700 atomic_inc(&trace_record_cmdline_disabled);
1701
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001702 if (*pos != iter->pos) {
1703 iter->ent = NULL;
1704 iter->cpu = 0;
1705 iter->idx = -1;
1706
Steven Rostedtd7690412008-10-01 00:29:53 -04001707 ftrace_disable_cpu();
1708
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001709 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1710 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001711 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001712 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001713 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001714
Steven Rostedtd7690412008-10-01 00:29:53 -04001715 ftrace_enable_cpu();
1716
Lai Jiangshanac91d852010-03-02 17:54:50 +08001717 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001718 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1719 ;
1720
1721 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001722 /*
1723 * If we overflowed the seq_file before, then we want
1724 * to just reuse the trace_seq buffer again.
1725 */
1726 if (iter->leftover)
1727 p = iter;
1728 else {
1729 l = *pos - 1;
1730 p = s_next(m, p, &l);
1731 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732 }
1733
Lai Jiangshan4f535962009-05-18 19:35:34 +08001734 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001735 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001736 return p;
1737}
1738
1739static void s_stop(struct seq_file *m, void *p)
1740{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001741 struct trace_iterator *iter = m->private;
1742
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001743 atomic_dec(&trace_record_cmdline_disabled);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001744 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08001745 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001746}
1747
Ingo Molnare309b412008-05-12 21:20:51 +02001748static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001749{
Michael Ellermana6168352008-08-20 16:36:11 -07001750 seq_puts(m, "# _------=> CPU# \n");
1751 seq_puts(m, "# / _-----=> irqs-off \n");
1752 seq_puts(m, "# | / _----=> need-resched \n");
1753 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1754 seq_puts(m, "# ||| / _--=> preempt-depth \n");
Steven Rostedt637e7e82009-09-11 13:55:35 -04001755 seq_puts(m, "# |||| /_--=> lock-depth \n");
1756 seq_puts(m, "# |||||/ delay \n");
1757 seq_puts(m, "# cmd pid |||||| time | caller \n");
1758 seq_puts(m, "# \\ / |||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001759}
1760
Ingo Molnare309b412008-05-12 21:20:51 +02001761static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001762{
Michael Ellermana6168352008-08-20 16:36:11 -07001763 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1764 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765}
1766
1767
Jiri Olsa62b915f2010-04-02 19:01:22 +02001768void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001769print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1770{
1771 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1772 struct trace_array *tr = iter->tr;
1773 struct trace_array_cpu *data = tr->data[tr->cpu];
1774 struct tracer *type = current_trace;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001775 unsigned long entries = 0;
1776 unsigned long total = 0;
1777 unsigned long count;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001778 const char *name = "preemption";
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001779 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001780
1781 if (type)
1782 name = type->name;
1783
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001784
1785 for_each_tracing_cpu(cpu) {
1786 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1787 /*
1788 * If this buffer has skipped entries, then we hold all
1789 * entries for the trace and we need to ignore the
1790 * ones before the time stamp.
1791 */
1792 if (tr->data[cpu]->skipped_entries) {
1793 count -= tr->data[cpu]->skipped_entries;
1794 /* total is the same as the entries */
1795 total += count;
1796 } else
1797 total += count +
1798 ring_buffer_overrun_cpu(tr->buffer, cpu);
1799 entries += count;
1800 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001801
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001802 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001803 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001804 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001805 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001806 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001807 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001808 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001809 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001810 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001811 tr->cpu,
1812#if defined(CONFIG_PREEMPT_NONE)
1813 "server",
1814#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1815 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001816#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001817 "preempt",
1818#else
1819 "unknown",
1820#endif
1821 /* These are reserved for later use */
1822 0, 0, 0, 0);
1823#ifdef CONFIG_SMP
1824 seq_printf(m, " #P:%d)\n", num_online_cpus());
1825#else
1826 seq_puts(m, ")\n");
1827#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001828 seq_puts(m, "# -----------------\n");
1829 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001830 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1831 data->comm, data->pid, data->uid, data->nice,
1832 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001833 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001834
1835 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001836 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001837 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1838 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001839 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001840 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1841 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001842 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001843 }
1844
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001845 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001846}
1847
Steven Rostedta3097202008-11-07 22:36:02 -05001848static void test_cpu_buff_start(struct trace_iterator *iter)
1849{
1850 struct trace_seq *s = &iter->seq;
1851
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001852 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1853 return;
1854
1855 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1856 return;
1857
Rusty Russell44623442009-01-01 10:12:23 +10301858 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001859 return;
1860
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001861 if (iter->tr->data[iter->cpu]->skipped_entries)
1862 return;
1863
Rusty Russell44623442009-01-01 10:12:23 +10301864 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001865
1866 /* Don't print started cpu buffer for the first entry of the trace */
1867 if (iter->idx > 1)
1868 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1869 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05001870}
1871
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001872static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001873{
Steven Rostedt214023c2008-05-12 21:20:46 +02001874 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001875 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001876 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001877 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001878
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001879 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001880
Steven Rostedta3097202008-11-07 22:36:02 -05001881 test_cpu_buff_start(iter);
1882
Steven Rostedtf633cef2008-12-23 23:24:13 -05001883 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001884
1885 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001886 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1887 if (!trace_print_lat_context(iter))
1888 goto partial;
1889 } else {
1890 if (!trace_print_context(iter))
1891 goto partial;
1892 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001893 }
1894
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001895 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04001896 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001897
1898 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1899 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001900
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001901 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001902partial:
1903 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001904}
1905
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001906static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001907{
1908 struct trace_seq *s = &iter->seq;
1909 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001910 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001911
1912 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001913
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001914 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001915 if (!trace_seq_printf(s, "%d %d %llu ",
1916 entry->pid, iter->cpu, iter->ts))
1917 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001918 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001919
Steven Rostedtf633cef2008-12-23 23:24:13 -05001920 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001921 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04001922 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001923
1924 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1925 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001926
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001927 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001928partial:
1929 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001930}
1931
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001932static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001933{
1934 struct trace_seq *s = &iter->seq;
1935 unsigned char newline = '\n';
1936 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001937 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001938
1939 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001940
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001941 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1942 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1943 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1944 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1945 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001946
Steven Rostedtf633cef2008-12-23 23:24:13 -05001947 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001948 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04001949 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001950 if (ret != TRACE_TYPE_HANDLED)
1951 return ret;
1952 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001953
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001954 SEQ_PUT_FIELD_RET(s, newline);
1955
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001956 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001957}
1958
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001959static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001960{
1961 struct trace_seq *s = &iter->seq;
1962 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001963 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001964
1965 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001966
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001967 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1968 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001969 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001970 SEQ_PUT_FIELD_RET(s, iter->ts);
1971 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001972
Steven Rostedtf633cef2008-12-23 23:24:13 -05001973 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04001974 return event ? event->funcs->binary(iter, 0, event) :
1975 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001976}
1977
Jiri Olsa62b915f2010-04-02 19:01:22 +02001978int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001979{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001980 int cpu;
1981
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001982 /* If we are looking at one CPU buffer, only check that one */
1983 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1984 cpu = iter->cpu_file;
1985 if (iter->buffer_iter[cpu]) {
1986 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1987 return 0;
1988 } else {
1989 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1990 return 0;
1991 }
1992 return 1;
1993 }
1994
Steven Rostedtab464282008-05-12 21:21:00 +02001995 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001996 if (iter->buffer_iter[cpu]) {
1997 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1998 return 0;
1999 } else {
2000 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2001 return 0;
2002 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002003 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002004
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002005 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002006}
2007
Lai Jiangshan4f535962009-05-18 19:35:34 +08002008/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05002009enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002010{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002011 enum print_line_t ret;
2012
Steven Rostedtbc21b472010-03-31 19:49:26 -04002013 if (iter->lost_events)
2014 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2015 iter->cpu, iter->lost_events);
2016
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002017 if (iter->trace && iter->trace->print_line) {
2018 ret = iter->trace->print_line(iter);
2019 if (ret != TRACE_TYPE_UNHANDLED)
2020 return ret;
2021 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002022
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002023 if (iter->ent->type == TRACE_BPRINT &&
2024 trace_flags & TRACE_ITER_PRINTK &&
2025 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002026 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002027
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002028 if (iter->ent->type == TRACE_PRINT &&
2029 trace_flags & TRACE_ITER_PRINTK &&
2030 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002031 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002032
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002033 if (trace_flags & TRACE_ITER_BIN)
2034 return print_bin_fmt(iter);
2035
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002036 if (trace_flags & TRACE_ITER_HEX)
2037 return print_hex_fmt(iter);
2038
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002039 if (trace_flags & TRACE_ITER_RAW)
2040 return print_raw_fmt(iter);
2041
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002042 return print_trace_fmt(iter);
2043}
2044
Jiri Olsa62b915f2010-04-02 19:01:22 +02002045void trace_default_header(struct seq_file *m)
2046{
2047 struct trace_iterator *iter = m->private;
2048
2049 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2050 /* print nothing if the buffers are empty */
2051 if (trace_empty(iter))
2052 return;
2053 print_trace_header(m, iter);
2054 if (!(trace_flags & TRACE_ITER_VERBOSE))
2055 print_lat_help_header(m);
2056 } else {
2057 if (!(trace_flags & TRACE_ITER_VERBOSE))
2058 print_func_help_header(m);
2059 }
2060}
2061
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062static int s_show(struct seq_file *m, void *v)
2063{
2064 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002065 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002066
2067 if (iter->ent == NULL) {
2068 if (iter->tr) {
2069 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2070 seq_puts(m, "#\n");
2071 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002072 if (iter->trace && iter->trace->print_header)
2073 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02002074 else
2075 trace_default_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) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002162 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002163 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2164 }
2165 ring_buffer_read_prepare_sync();
2166 for_each_tracing_cpu(cpu) {
2167 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002168 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002169 }
2170 } else {
2171 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002172 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002173 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2174 ring_buffer_read_prepare_sync();
2175 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002176 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002177 }
2178
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002179 ret = seq_open(file, &tracer_seq_ops);
2180 if (ret < 0) {
2181 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002182 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002183 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002184
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002185 m = file->private_data;
2186 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002187
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188 mutex_unlock(&trace_types_lock);
2189
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002190 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002191
2192 fail_buffer:
2193 for_each_tracing_cpu(cpu) {
2194 if (iter->buffer_iter[cpu])
2195 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2196 }
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002197 free_cpumask_var(iter->started);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002198 tracing_start();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002199 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002200 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002201 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01002202 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002203
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002204 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002205}
2206
2207int tracing_open_generic(struct inode *inode, struct file *filp)
2208{
Steven Rostedt60a11772008-05-12 21:20:44 +02002209 if (tracing_disabled)
2210 return -ENODEV;
2211
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002212 filp->private_data = inode->i_private;
2213 return 0;
2214}
2215
Hannes Eder4fd27352009-02-10 19:44:12 +01002216static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002217{
matt mooney907f2782010-09-27 19:04:53 -07002218 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002219 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002220 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002221
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002222 if (!(file->f_mode & FMODE_READ))
2223 return 0;
2224
2225 iter = m->private;
2226
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002227 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002228 for_each_tracing_cpu(cpu) {
2229 if (iter->buffer_iter[cpu])
2230 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2231 }
2232
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002233 if (iter->trace && iter->trace->close)
2234 iter->trace->close(iter);
2235
2236 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05002237 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002238 mutex_unlock(&trace_types_lock);
2239
2240 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002241 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002242 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002243 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002244 kfree(iter);
2245 return 0;
2246}
2247
2248static int tracing_open(struct inode *inode, struct file *file)
2249{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002250 struct trace_iterator *iter;
2251 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002252
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002253 /* If this file was open for write, then erase contents */
2254 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002255 (file->f_flags & O_TRUNC)) {
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002256 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002257
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002258 if (cpu == TRACE_PIPE_ALL_CPU)
2259 tracing_reset_online_cpus(&global_trace);
2260 else
2261 tracing_reset(&global_trace, cpu);
2262 }
2263
2264 if (file->f_mode & FMODE_READ) {
2265 iter = __tracing_open(inode, file);
2266 if (IS_ERR(iter))
2267 ret = PTR_ERR(iter);
2268 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2269 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2270 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002271 return ret;
2272}
2273
Ingo Molnare309b412008-05-12 21:20:51 +02002274static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002275t_next(struct seq_file *m, void *v, loff_t *pos)
2276{
Li Zefanf129e962009-06-24 09:53:44 +08002277 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002278
2279 (*pos)++;
2280
2281 if (t)
2282 t = t->next;
2283
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002284 return t;
2285}
2286
2287static void *t_start(struct seq_file *m, loff_t *pos)
2288{
Li Zefanf129e962009-06-24 09:53:44 +08002289 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002290 loff_t l = 0;
2291
2292 mutex_lock(&trace_types_lock);
Li Zefanf129e962009-06-24 09:53:44 +08002293 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002294 ;
2295
2296 return t;
2297}
2298
2299static void t_stop(struct seq_file *m, void *p)
2300{
2301 mutex_unlock(&trace_types_lock);
2302}
2303
2304static int t_show(struct seq_file *m, void *v)
2305{
2306 struct tracer *t = v;
2307
2308 if (!t)
2309 return 0;
2310
2311 seq_printf(m, "%s", t->name);
2312 if (t->next)
2313 seq_putc(m, ' ');
2314 else
2315 seq_putc(m, '\n');
2316
2317 return 0;
2318}
2319
James Morris88e9d342009-09-22 16:43:43 -07002320static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002321 .start = t_start,
2322 .next = t_next,
2323 .stop = t_stop,
2324 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002325};
2326
2327static int show_traces_open(struct inode *inode, struct file *file)
2328{
Steven Rostedt60a11772008-05-12 21:20:44 +02002329 if (tracing_disabled)
2330 return -ENODEV;
2331
Li Zefanf129e962009-06-24 09:53:44 +08002332 return seq_open(file, &show_traces_seq_ops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002333}
2334
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002335static ssize_t
2336tracing_write_stub(struct file *filp, const char __user *ubuf,
2337 size_t count, loff_t *ppos)
2338{
2339 return count;
2340}
2341
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002342static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002343 .open = tracing_open,
2344 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002345 .write = tracing_write_stub,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002346 .llseek = seq_lseek,
2347 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002348};
2349
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002350static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002351 .open = show_traces_open,
2352 .read = seq_read,
2353 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002354 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02002355};
2356
Ingo Molnar36dfe922008-05-12 21:20:52 +02002357/*
2358 * Only trace on a CPU if the bitmask is set:
2359 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302360static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002361
2362/*
2363 * The tracer itself will not take this lock, but still we want
2364 * to provide a consistent cpumask to user-space:
2365 */
2366static DEFINE_MUTEX(tracing_cpumask_update_lock);
2367
2368/*
2369 * Temporary storage for the character representation of the
2370 * CPU bitmask (and one more byte for the newline):
2371 */
2372static char mask_str[NR_CPUS + 1];
2373
Ingo Molnarc7078de2008-05-12 21:20:52 +02002374static ssize_t
2375tracing_cpumask_read(struct file *filp, char __user *ubuf,
2376 size_t count, loff_t *ppos)
2377{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002378 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002379
2380 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002381
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302382 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002383 if (count - len < 2) {
2384 count = -EINVAL;
2385 goto out_err;
2386 }
2387 len += sprintf(mask_str + len, "\n");
2388 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2389
2390out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002391 mutex_unlock(&tracing_cpumask_update_lock);
2392
2393 return count;
2394}
2395
2396static ssize_t
2397tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2398 size_t count, loff_t *ppos)
2399{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002400 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302401 cpumask_var_t tracing_cpumask_new;
2402
2403 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2404 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002405
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302406 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002407 if (err)
2408 goto err_unlock;
2409
Li Zefan215368e2009-06-15 10:56:42 +08002410 mutex_lock(&tracing_cpumask_update_lock);
2411
Steven Rostedta5e25882008-12-02 15:34:05 -05002412 local_irq_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002413 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002414 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002415 /*
2416 * Increase/decrease the disabled counter if we are
2417 * about to flip a bit in the cpumask:
2418 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302419 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2420 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002421 atomic_inc(&global_trace.data[cpu]->disabled);
2422 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302423 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2424 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002425 atomic_dec(&global_trace.data[cpu]->disabled);
2426 }
2427 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002428 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002429 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002430
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302431 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002432
Ingo Molnarc7078de2008-05-12 21:20:52 +02002433 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302434 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002435
Ingo Molnarc7078de2008-05-12 21:20:52 +02002436 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002437
2438err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08002439 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002440
2441 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002442}
2443
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002444static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002445 .open = tracing_open_generic,
2446 .read = tracing_cpumask_read,
2447 .write = tracing_cpumask_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002448 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002449};
2450
Li Zefanfdb372e2009-12-08 11:15:59 +08002451static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002452{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002453 struct tracer_opt *trace_opts;
2454 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002455 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002456
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002457 mutex_lock(&trace_types_lock);
2458 tracer_flags = current_trace->flags->val;
2459 trace_opts = current_trace->flags->opts;
2460
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002461 for (i = 0; trace_options[i]; i++) {
2462 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08002463 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002464 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002465 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002466 }
2467
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002468 for (i = 0; trace_opts[i].name; i++) {
2469 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08002470 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002471 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002472 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002473 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002474 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002475
Li Zefanfdb372e2009-12-08 11:15:59 +08002476 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002477}
2478
Li Zefan8d18eaa2009-12-08 11:17:06 +08002479static int __set_tracer_option(struct tracer *trace,
2480 struct tracer_flags *tracer_flags,
2481 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002482{
Li Zefan8d18eaa2009-12-08 11:17:06 +08002483 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002484
Li Zefan8d18eaa2009-12-08 11:17:06 +08002485 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002486 if (ret)
2487 return ret;
2488
2489 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08002490 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002491 else
Zhaolei77708412009-08-07 18:53:21 +08002492 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002493 return 0;
2494}
2495
Li Zefan8d18eaa2009-12-08 11:17:06 +08002496/* Try to assign a tracer specific option */
2497static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2498{
2499 struct tracer_flags *tracer_flags = trace->flags;
2500 struct tracer_opt *opts = NULL;
2501 int i;
2502
2503 for (i = 0; tracer_flags->opts[i].name; i++) {
2504 opts = &tracer_flags->opts[i];
2505
2506 if (strcmp(cmp, opts->name) == 0)
2507 return __set_tracer_option(trace, trace->flags,
2508 opts, neg);
2509 }
2510
2511 return -EINVAL;
2512}
2513
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002514static void set_tracer_flags(unsigned int mask, int enabled)
2515{
2516 /* do nothing if flag is already set */
2517 if (!!(trace_flags & mask) == !!enabled)
2518 return;
2519
2520 if (enabled)
2521 trace_flags |= mask;
2522 else
2523 trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08002524
2525 if (mask == TRACE_ITER_RECORD_CMD)
2526 trace_event_enable_cmd_record(enabled);
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002527}
2528
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002529static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002530tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002531 size_t cnt, loff_t *ppos)
2532{
2533 char buf[64];
Li Zefan8d18eaa2009-12-08 11:17:06 +08002534 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002535 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002536 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002537 int i;
2538
Steven Rostedtcffae432008-05-12 21:21:00 +02002539 if (cnt >= sizeof(buf))
2540 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002541
2542 if (copy_from_user(&buf, ubuf, cnt))
2543 return -EFAULT;
2544
2545 buf[cnt] = 0;
Li Zefan8d18eaa2009-12-08 11:17:06 +08002546 cmp = strstrip(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002547
Li Zefan8d18eaa2009-12-08 11:17:06 +08002548 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002549 neg = 1;
2550 cmp += 2;
2551 }
2552
2553 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08002554 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002555 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002556 break;
2557 }
2558 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002559
2560 /* If no option could be set, test the specific tracer options */
2561 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002562 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002563 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002564 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002565 if (ret)
2566 return ret;
2567 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002568
Jiri Olsacf8517c2009-10-23 19:36:16 -04002569 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002570
2571 return cnt;
2572}
2573
Li Zefanfdb372e2009-12-08 11:15:59 +08002574static int tracing_trace_options_open(struct inode *inode, struct file *file)
2575{
2576 if (tracing_disabled)
2577 return -ENODEV;
2578 return single_open(file, tracing_trace_options_show, NULL);
2579}
2580
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002581static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08002582 .open = tracing_trace_options_open,
2583 .read = seq_read,
2584 .llseek = seq_lseek,
2585 .release = single_release,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002586 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002587};
2588
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002589static const char readme_msg[] =
2590 "tracing mini-HOWTO:\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002591 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2592 "# cat /sys/kernel/debug/tracing/available_tracers\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302593 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002594 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302595 "nop\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002596 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2597 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002598 "sched_switch\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002599 "# cat /sys/kernel/debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002600 "noprint-parent nosym-offset nosym-addr noverbose\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002601 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2602 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2603 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2604 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002605;
2606
2607static ssize_t
2608tracing_readme_read(struct file *filp, char __user *ubuf,
2609 size_t cnt, loff_t *ppos)
2610{
2611 return simple_read_from_buffer(ubuf, cnt, ppos,
2612 readme_msg, strlen(readme_msg));
2613}
2614
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002615static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002616 .open = tracing_open_generic,
2617 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002618 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002619};
2620
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002621static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04002622tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2623 size_t cnt, loff_t *ppos)
2624{
2625 char *buf_comm;
2626 char *file_buf;
2627 char *buf;
2628 int len = 0;
2629 int pid;
2630 int i;
2631
2632 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2633 if (!file_buf)
2634 return -ENOMEM;
2635
2636 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2637 if (!buf_comm) {
2638 kfree(file_buf);
2639 return -ENOMEM;
2640 }
2641
2642 buf = file_buf;
2643
2644 for (i = 0; i < SAVED_CMDLINES; i++) {
2645 int r;
2646
2647 pid = map_cmdline_to_pid[i];
2648 if (pid == -1 || pid == NO_CMDLINE_MAP)
2649 continue;
2650
2651 trace_find_cmdline(pid, buf_comm);
2652 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2653 buf += r;
2654 len += r;
2655 }
2656
2657 len = simple_read_from_buffer(ubuf, cnt, ppos,
2658 file_buf, len);
2659
2660 kfree(file_buf);
2661 kfree(buf_comm);
2662
2663 return len;
2664}
2665
2666static const struct file_operations tracing_saved_cmdlines_fops = {
2667 .open = tracing_open_generic,
2668 .read = tracing_saved_cmdlines_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002669 .llseek = generic_file_llseek,
Avadh Patel69abe6a2009-04-10 16:04:48 -04002670};
2671
2672static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002673tracing_ctrl_read(struct file *filp, char __user *ubuf,
2674 size_t cnt, loff_t *ppos)
2675{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002676 char buf[64];
2677 int r;
2678
Steven Rostedt90369902008-11-05 16:05:44 -05002679 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002680 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002681}
2682
2683static ssize_t
2684tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2685 size_t cnt, loff_t *ppos)
2686{
2687 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002688 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002689 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002690 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002691
Steven Rostedtcffae432008-05-12 21:21:00 +02002692 if (cnt >= sizeof(buf))
2693 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002694
2695 if (copy_from_user(&buf, ubuf, cnt))
2696 return -EFAULT;
2697
2698 buf[cnt] = 0;
2699
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002700 ret = strict_strtoul(buf, 10, &val);
2701 if (ret < 0)
2702 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002703
2704 val = !!val;
2705
2706 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002707 if (tracer_enabled ^ val) {
2708 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002709 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002710 if (current_trace->start)
2711 current_trace->start(tr);
2712 tracing_start();
2713 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002714 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002715 tracing_stop();
2716 if (current_trace->stop)
2717 current_trace->stop(tr);
2718 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002719 }
2720 mutex_unlock(&trace_types_lock);
2721
Jiri Olsacf8517c2009-10-23 19:36:16 -04002722 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002723
2724 return cnt;
2725}
2726
2727static ssize_t
2728tracing_set_trace_read(struct file *filp, char __user *ubuf,
2729 size_t cnt, loff_t *ppos)
2730{
Li Zefanee6c2c12009-09-18 14:06:47 +08002731 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002732 int r;
2733
2734 mutex_lock(&trace_types_lock);
2735 if (current_trace)
2736 r = sprintf(buf, "%s\n", current_trace->name);
2737 else
2738 r = sprintf(buf, "\n");
2739 mutex_unlock(&trace_types_lock);
2740
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002741 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002742}
2743
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002744int tracer_init(struct tracer *t, struct trace_array *tr)
2745{
2746 tracing_reset_online_cpus(tr);
2747 return t->init(tr);
2748}
2749
Steven Rostedt73c51622009-03-11 13:42:01 -04002750static int tracing_resize_ring_buffer(unsigned long size)
2751{
2752 int ret;
2753
2754 /*
2755 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002756 * we use the size that was given, and we can forget about
2757 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002758 */
2759 ring_buffer_expanded = 1;
2760
2761 ret = ring_buffer_resize(global_trace.buffer, size);
2762 if (ret < 0)
2763 return ret;
2764
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002765 if (!current_trace->use_max_tr)
2766 goto out;
2767
Steven Rostedt73c51622009-03-11 13:42:01 -04002768 ret = ring_buffer_resize(max_tr.buffer, size);
2769 if (ret < 0) {
2770 int r;
2771
2772 r = ring_buffer_resize(global_trace.buffer,
2773 global_trace.entries);
2774 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002775 /*
2776 * AARGH! We are left with different
2777 * size max buffer!!!!
2778 * The max buffer is our "snapshot" buffer.
2779 * When a tracer needs a snapshot (one of the
2780 * latency tracers), it swaps the max buffer
2781 * with the saved snap shot. We succeeded to
2782 * update the size of the main buffer, but failed to
2783 * update the size of the max buffer. But when we tried
2784 * to reset the main buffer to the original size, we
2785 * failed there too. This is very unlikely to
2786 * happen, but if it does, warn and kill all
2787 * tracing.
2788 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002789 WARN_ON(1);
2790 tracing_disabled = 1;
2791 }
2792 return ret;
2793 }
2794
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002795 max_tr.entries = size;
2796 out:
Steven Rostedt73c51622009-03-11 13:42:01 -04002797 global_trace.entries = size;
2798
2799 return ret;
2800}
2801
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002802
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002803/**
2804 * tracing_update_buffers - used by tracing facility to expand ring buffers
2805 *
2806 * To save on memory when the tracing is never used on a system with it
2807 * configured in. The ring buffers are set to a minimum size. But once
2808 * a user starts to use the tracing facility, then they need to grow
2809 * to their default size.
2810 *
2811 * This function is to be called when a tracer is about to be used.
2812 */
2813int tracing_update_buffers(void)
2814{
2815 int ret = 0;
2816
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002817 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002818 if (!ring_buffer_expanded)
2819 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002820 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002821
2822 return ret;
2823}
2824
Steven Rostedt577b7852009-02-26 23:43:05 -05002825struct trace_option_dentry;
2826
2827static struct trace_option_dentry *
2828create_trace_option_files(struct tracer *tracer);
2829
2830static void
2831destroy_trace_option_files(struct trace_option_dentry *topts);
2832
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002833static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002834{
Steven Rostedt577b7852009-02-26 23:43:05 -05002835 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002836 struct trace_array *tr = &global_trace;
2837 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002838 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002839
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002840 mutex_lock(&trace_types_lock);
2841
Steven Rostedt73c51622009-03-11 13:42:01 -04002842 if (!ring_buffer_expanded) {
2843 ret = tracing_resize_ring_buffer(trace_buf_size);
2844 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01002845 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04002846 ret = 0;
2847 }
2848
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002849 for (t = trace_types; t; t = t->next) {
2850 if (strcmp(t->name, buf) == 0)
2851 break;
2852 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002853 if (!t) {
2854 ret = -EINVAL;
2855 goto out;
2856 }
2857 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002858 goto out;
2859
Steven Rostedt9f029e82008-11-12 15:24:24 -05002860 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002861 if (current_trace && current_trace->reset)
2862 current_trace->reset(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002863 if (current_trace && current_trace->use_max_tr) {
2864 /*
2865 * We don't free the ring buffer. instead, resize it because
2866 * The max_tr ring buffer has some state (e.g. ring->clock) and
2867 * we want preserve it.
2868 */
2869 ring_buffer_resize(max_tr.buffer, 1);
2870 max_tr.entries = 1;
2871 }
Steven Rostedt577b7852009-02-26 23:43:05 -05002872 destroy_trace_option_files(topts);
2873
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002874 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002875
2876 topts = create_trace_option_files(current_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002877 if (current_trace->use_max_tr) {
2878 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2879 if (ret < 0)
2880 goto out;
2881 max_tr.entries = global_trace.entries;
2882 }
Steven Rostedt577b7852009-02-26 23:43:05 -05002883
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002884 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002885 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002886 if (ret)
2887 goto out;
2888 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002889
Steven Rostedt9f029e82008-11-12 15:24:24 -05002890 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002891 out:
2892 mutex_unlock(&trace_types_lock);
2893
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002894 return ret;
2895}
2896
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002897static ssize_t
2898tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2899 size_t cnt, loff_t *ppos)
2900{
Li Zefanee6c2c12009-09-18 14:06:47 +08002901 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002902 int i;
2903 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002904 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002905
Steven Rostedt60063a62008-10-28 10:44:24 -04002906 ret = cnt;
2907
Li Zefanee6c2c12009-09-18 14:06:47 +08002908 if (cnt > MAX_TRACER_SIZE)
2909 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002910
2911 if (copy_from_user(&buf, ubuf, cnt))
2912 return -EFAULT;
2913
2914 buf[cnt] = 0;
2915
2916 /* strip ending whitespace. */
2917 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2918 buf[i] = 0;
2919
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002920 err = tracing_set_tracer(buf);
2921 if (err)
2922 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002923
Jiri Olsacf8517c2009-10-23 19:36:16 -04002924 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002925
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002926 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002927}
2928
2929static ssize_t
2930tracing_max_lat_read(struct file *filp, char __user *ubuf,
2931 size_t cnt, loff_t *ppos)
2932{
2933 unsigned long *ptr = filp->private_data;
2934 char buf[64];
2935 int r;
2936
Steven Rostedtcffae432008-05-12 21:21:00 +02002937 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002938 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002939 if (r > sizeof(buf))
2940 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002941 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002942}
2943
2944static ssize_t
2945tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2946 size_t cnt, loff_t *ppos)
2947{
Hannes Eder5e398412009-02-10 19:44:34 +01002948 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002949 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002950 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002951 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002952
Steven Rostedtcffae432008-05-12 21:21:00 +02002953 if (cnt >= sizeof(buf))
2954 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002955
2956 if (copy_from_user(&buf, ubuf, cnt))
2957 return -EFAULT;
2958
2959 buf[cnt] = 0;
2960
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002961 ret = strict_strtoul(buf, 10, &val);
2962 if (ret < 0)
2963 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002964
2965 *ptr = val * 1000;
2966
2967 return cnt;
2968}
2969
Steven Rostedtb3806b42008-05-12 21:20:46 +02002970static int tracing_open_pipe(struct inode *inode, struct file *filp)
2971{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002972 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002973 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002974 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002975
2976 if (tracing_disabled)
2977 return -ENODEV;
2978
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002979 mutex_lock(&trace_types_lock);
2980
Steven Rostedtb3806b42008-05-12 21:20:46 +02002981 /* create a buffer to store the information to pass to userspace */
2982 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002983 if (!iter) {
2984 ret = -ENOMEM;
2985 goto out;
2986 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002987
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002988 /*
2989 * We make a copy of the current tracer to avoid concurrent
2990 * changes on it while we are reading.
2991 */
2992 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2993 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002994 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002995 goto fail;
2996 }
2997 if (current_trace)
2998 *iter->trace = *current_trace;
2999
3000 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3001 ret = -ENOMEM;
3002 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10303003 }
3004
Steven Rostedta3097202008-11-07 22:36:02 -05003005 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10303006 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05003007
Steven Rostedt112f38a72009-06-01 15:16:05 -04003008 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3009 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3010
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003011 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003012 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003013 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003014 filp->private_data = iter;
3015
Steven Rostedt107bad82008-05-12 21:21:01 +02003016 if (iter->trace->pipe_open)
3017 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02003018
Arnd Bergmannb4447862010-07-07 23:40:11 +02003019 nonseekable_open(inode, filp);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003020out:
3021 mutex_unlock(&trace_types_lock);
3022 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003023
3024fail:
3025 kfree(iter->trace);
3026 kfree(iter);
3027 mutex_unlock(&trace_types_lock);
3028 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003029}
3030
3031static int tracing_release_pipe(struct inode *inode, struct file *file)
3032{
3033 struct trace_iterator *iter = file->private_data;
3034
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003035 mutex_lock(&trace_types_lock);
3036
Steven Rostedt29bf4a52009-12-09 12:37:43 -05003037 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05003038 iter->trace->pipe_close(iter);
3039
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003040 mutex_unlock(&trace_types_lock);
3041
Rusty Russell44623442009-01-01 10:12:23 +10303042 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003043 mutex_destroy(&iter->mutex);
3044 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003045 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003046
3047 return 0;
3048}
3049
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003050static unsigned int
3051tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3052{
3053 struct trace_iterator *iter = filp->private_data;
3054
3055 if (trace_flags & TRACE_ITER_BLOCK) {
3056 /*
3057 * Always select as readable when in blocking mode
3058 */
3059 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02003060 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003061 if (!trace_empty(iter))
3062 return POLLIN | POLLRDNORM;
3063 poll_wait(filp, &trace_wait, poll_table);
3064 if (!trace_empty(iter))
3065 return POLLIN | POLLRDNORM;
3066
3067 return 0;
3068 }
3069}
3070
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003071
3072void default_wait_pipe(struct trace_iterator *iter)
3073{
3074 DEFINE_WAIT(wait);
3075
3076 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3077
3078 if (trace_empty(iter))
3079 schedule();
3080
3081 finish_wait(&trace_wait, &wait);
3082}
3083
3084/*
3085 * This is a make-shift waitqueue.
3086 * A tracer might use this callback on some rare cases:
3087 *
3088 * 1) the current tracer might hold the runqueue lock when it wakes up
3089 * a reader, hence a deadlock (sched, function, and function graph tracers)
3090 * 2) the function tracers, trace all functions, we don't want
3091 * the overhead of calling wake_up and friends
3092 * (and tracing them too)
3093 *
3094 * Anyway, this is really very primitive wakeup.
3095 */
3096void poll_wait_pipe(struct trace_iterator *iter)
3097{
3098 set_current_state(TASK_INTERRUPTIBLE);
3099 /* sleep for 100 msecs, and try again. */
3100 schedule_timeout(HZ / 10);
3101}
3102
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003103/* Must be called with trace_types_lock mutex held. */
3104static int tracing_wait_pipe(struct file *filp)
3105{
3106 struct trace_iterator *iter = filp->private_data;
3107
3108 while (trace_empty(iter)) {
3109
3110 if ((filp->f_flags & O_NONBLOCK)) {
3111 return -EAGAIN;
3112 }
3113
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003114 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003115
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003116 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003117
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003118 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003119
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003120 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003121 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003122
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003123 /*
3124 * We block until we read something and tracing is disabled.
3125 * We still block if tracing is disabled, but we have never
3126 * read anything. This allows a user to cat this file, and
3127 * then enable tracing. But after we have read something,
3128 * we give an EOF when tracing is again disabled.
3129 *
3130 * iter->pos will be 0 if we haven't read anything.
3131 */
3132 if (!tracer_enabled && iter->pos)
3133 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003134 }
3135
3136 return 1;
3137}
3138
Steven Rostedtb3806b42008-05-12 21:20:46 +02003139/*
3140 * Consumer reader.
3141 */
3142static ssize_t
3143tracing_read_pipe(struct file *filp, char __user *ubuf,
3144 size_t cnt, loff_t *ppos)
3145{
3146 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003147 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003148 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003149
3150 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003151 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3152 if (sret != -EBUSY)
3153 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003154
Steven Rostedtf9520752009-03-02 14:04:40 -05003155 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003156
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003157 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02003158 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003159 if (unlikely(old_tracer != current_trace && current_trace)) {
3160 old_tracer = current_trace;
3161 *iter->trace = *current_trace;
3162 }
3163 mutex_unlock(&trace_types_lock);
3164
3165 /*
3166 * Avoid more than one consumer on a single file descriptor
3167 * This is just a matter of traces coherency, the ring buffer itself
3168 * is protected.
3169 */
3170 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003171 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003172 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3173 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02003174 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02003175 }
3176
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003177waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003178 sret = tracing_wait_pipe(filp);
3179 if (sret <= 0)
3180 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003181
3182 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003183 if (trace_empty(iter)) {
3184 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003185 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003186 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003187
3188 if (cnt >= PAGE_SIZE)
3189 cnt = PAGE_SIZE - 1;
3190
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003191 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003192 memset(&iter->seq, 0,
3193 sizeof(struct trace_iterator) -
3194 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02003195 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003196
Lai Jiangshan4f535962009-05-18 19:35:34 +08003197 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003198 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05003199 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003200 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003201 int len = iter->seq.len;
3202
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003203 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003204 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02003205 /* don't print partial lines */
3206 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003207 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003208 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01003209 if (ret != TRACE_TYPE_NO_CONSUME)
3210 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003211
3212 if (iter->seq.len >= cnt)
3213 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003214 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003215 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003216 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02003217
Steven Rostedtb3806b42008-05-12 21:20:46 +02003218 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003219 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3220 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05003221 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003222
3223 /*
3224 * If there was nothing to send to user, inspite of consuming trace
3225 * entries, go back to wait for more entries.
3226 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003227 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003228 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003229
Steven Rostedt107bad82008-05-12 21:21:01 +02003230out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003231 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003232
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003233 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003234}
3235
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003236static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3237 struct pipe_buffer *buf)
3238{
3239 __free_page(buf->page);
3240}
3241
3242static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3243 unsigned int idx)
3244{
3245 __free_page(spd->pages[idx]);
3246}
3247
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003248static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003249 .can_merge = 0,
3250 .map = generic_pipe_buf_map,
3251 .unmap = generic_pipe_buf_unmap,
3252 .confirm = generic_pipe_buf_confirm,
3253 .release = tracing_pipe_buf_release,
3254 .steal = generic_pipe_buf_steal,
3255 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003256};
3257
Steven Rostedt34cd4992009-02-09 12:06:29 -05003258static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003259tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003260{
3261 size_t count;
3262 int ret;
3263
3264 /* Seq buffer is page-sized, exactly what we need. */
3265 for (;;) {
3266 count = iter->seq.len;
3267 ret = print_trace_line(iter);
3268 count = iter->seq.len - count;
3269 if (rem < count) {
3270 rem = 0;
3271 iter->seq.len -= count;
3272 break;
3273 }
3274 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3275 iter->seq.len -= count;
3276 break;
3277 }
3278
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08003279 if (ret != TRACE_TYPE_NO_CONSUME)
3280 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003281 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05003282 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003283 rem = 0;
3284 iter->ent = NULL;
3285 break;
3286 }
3287 }
3288
3289 return rem;
3290}
3291
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003292static ssize_t tracing_splice_read_pipe(struct file *filp,
3293 loff_t *ppos,
3294 struct pipe_inode_info *pipe,
3295 size_t len,
3296 unsigned int flags)
3297{
Jens Axboe35f3d142010-05-20 10:43:18 +02003298 struct page *pages_def[PIPE_DEF_BUFFERS];
3299 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003300 struct trace_iterator *iter = filp->private_data;
3301 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02003302 .pages = pages_def,
3303 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05003304 .nr_pages = 0, /* This gets updated below. */
3305 .flags = flags,
3306 .ops = &tracing_pipe_buf_ops,
3307 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003308 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003309 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003310 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003311 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003312 unsigned int i;
3313
Jens Axboe35f3d142010-05-20 10:43:18 +02003314 if (splice_grow_spd(pipe, &spd))
3315 return -ENOMEM;
3316
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003317 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003318 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003319 if (unlikely(old_tracer != current_trace && current_trace)) {
3320 old_tracer = current_trace;
3321 *iter->trace = *current_trace;
3322 }
3323 mutex_unlock(&trace_types_lock);
3324
3325 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003326
3327 if (iter->trace->splice_read) {
3328 ret = iter->trace->splice_read(iter, filp,
3329 ppos, pipe, len, flags);
3330 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003331 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003332 }
3333
3334 ret = tracing_wait_pipe(filp);
3335 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003336 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003337
Jason Wessel955b61e2010-08-05 09:22:23 -05003338 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003339 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003340 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003341 }
3342
Lai Jiangshan4f535962009-05-18 19:35:34 +08003343 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003344 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003345
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003346 /* Fill as many pages as possible. */
Jens Axboe35f3d142010-05-20 10:43:18 +02003347 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3348 spd.pages[i] = alloc_page(GFP_KERNEL);
3349 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05003350 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003351
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003352 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003353
3354 /* Copy the data into the page, so we can start over. */
3355 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02003356 page_address(spd.pages[i]),
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003357 iter->seq.len);
3358 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02003359 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003360 break;
3361 }
Jens Axboe35f3d142010-05-20 10:43:18 +02003362 spd.partial[i].offset = 0;
3363 spd.partial[i].len = iter->seq.len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003364
Steven Rostedtf9520752009-03-02 14:04:40 -05003365 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003366 }
3367
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003368 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003369 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003370 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003371
3372 spd.nr_pages = i;
3373
Jens Axboe35f3d142010-05-20 10:43:18 +02003374 ret = splice_to_pipe(pipe, &spd);
3375out:
3376 splice_shrink_spd(pipe, &spd);
3377 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003378
Steven Rostedt34cd4992009-02-09 12:06:29 -05003379out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003380 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02003381 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003382}
3383
Steven Rostedta98a3c32008-05-12 21:20:59 +02003384static ssize_t
3385tracing_entries_read(struct file *filp, char __user *ubuf,
3386 size_t cnt, loff_t *ppos)
3387{
3388 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003389 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003390 int r;
3391
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003392 mutex_lock(&trace_types_lock);
3393 if (!ring_buffer_expanded)
3394 r = sprintf(buf, "%lu (expanded: %lu)\n",
3395 tr->entries >> 10,
3396 trace_buf_size >> 10);
3397 else
3398 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3399 mutex_unlock(&trace_types_lock);
3400
Steven Rostedta98a3c32008-05-12 21:20:59 +02003401 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3402}
3403
3404static ssize_t
3405tracing_entries_write(struct file *filp, const char __user *ubuf,
3406 size_t cnt, loff_t *ppos)
3407{
3408 unsigned long val;
3409 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003410 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003411
Steven Rostedtcffae432008-05-12 21:21:00 +02003412 if (cnt >= sizeof(buf))
3413 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003414
3415 if (copy_from_user(&buf, ubuf, cnt))
3416 return -EFAULT;
3417
3418 buf[cnt] = 0;
3419
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003420 ret = strict_strtoul(buf, 10, &val);
3421 if (ret < 0)
3422 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003423
3424 /* must have at least 1 entry */
3425 if (!val)
3426 return -EINVAL;
3427
3428 mutex_lock(&trace_types_lock);
3429
Steven Rostedtc76f0692008-11-07 22:36:02 -05003430 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003431
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003432 /* disable all cpu buffers */
3433 for_each_tracing_cpu(cpu) {
3434 if (global_trace.data[cpu])
3435 atomic_inc(&global_trace.data[cpu]->disabled);
3436 if (max_tr.data[cpu])
3437 atomic_inc(&max_tr.data[cpu]->disabled);
3438 }
3439
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003440 /* value is in KB */
3441 val <<= 10;
3442
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003443 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003444 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003445 if (ret < 0) {
3446 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003447 goto out;
3448 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003449 }
3450
Jiri Olsacf8517c2009-10-23 19:36:16 -04003451 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003452
Steven Rostedt19384c02008-05-22 00:22:16 -04003453 /* If check pages failed, return ENOMEM */
3454 if (tracing_disabled)
3455 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003456 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003457 for_each_tracing_cpu(cpu) {
3458 if (global_trace.data[cpu])
3459 atomic_dec(&global_trace.data[cpu]->disabled);
3460 if (max_tr.data[cpu])
3461 atomic_dec(&max_tr.data[cpu]->disabled);
3462 }
3463
Steven Rostedtc76f0692008-11-07 22:36:02 -05003464 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003465 mutex_unlock(&trace_types_lock);
3466
3467 return cnt;
3468}
3469
Carsten Emdef2942482009-12-06 14:02:44 +01003470static int mark_printk(const char *fmt, ...)
3471{
3472 int ret;
3473 va_list args;
3474 va_start(args, fmt);
3475 ret = trace_vprintk(0, fmt, args);
3476 va_end(args);
3477 return ret;
3478}
3479
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003480static ssize_t
3481tracing_mark_write(struct file *filp, const char __user *ubuf,
3482 size_t cnt, loff_t *fpos)
3483{
3484 char *buf;
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003485 size_t written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003486
Steven Rostedtc76f0692008-11-07 22:36:02 -05003487 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003488 return -EINVAL;
3489
3490 if (cnt > TRACE_BUF_SIZE)
3491 cnt = TRACE_BUF_SIZE;
3492
Carsten Emdec13d2f72009-11-16 20:56:13 +01003493 buf = kmalloc(cnt + 2, GFP_KERNEL);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003494 if (buf == NULL)
3495 return -ENOMEM;
3496
3497 if (copy_from_user(buf, ubuf, cnt)) {
3498 kfree(buf);
3499 return -EFAULT;
3500 }
Carsten Emdec13d2f72009-11-16 20:56:13 +01003501 if (buf[cnt-1] != '\n') {
3502 buf[cnt] = '\n';
3503 buf[cnt+1] = '\0';
3504 } else
3505 buf[cnt] = '\0';
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003506
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003507 written = mark_printk("%s", buf);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003508 kfree(buf);
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003509 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003510
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003511 /* don't tell userspace we wrote more - it might confuse them */
3512 if (written > cnt)
3513 written = cnt;
3514
3515 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003516}
3517
Li Zefan13f16d22009-12-08 11:16:11 +08003518static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08003519{
Zhaolei5079f322009-08-25 16:12:56 +08003520 int i;
3521
3522 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08003523 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08003524 "%s%s%s%s", i ? " " : "",
3525 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3526 i == trace_clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08003527 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08003528
Li Zefan13f16d22009-12-08 11:16:11 +08003529 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08003530}
3531
3532static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3533 size_t cnt, loff_t *fpos)
3534{
3535 char buf[64];
3536 const char *clockstr;
3537 int i;
3538
3539 if (cnt >= sizeof(buf))
3540 return -EINVAL;
3541
3542 if (copy_from_user(&buf, ubuf, cnt))
3543 return -EFAULT;
3544
3545 buf[cnt] = 0;
3546
3547 clockstr = strstrip(buf);
3548
3549 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3550 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3551 break;
3552 }
3553 if (i == ARRAY_SIZE(trace_clocks))
3554 return -EINVAL;
3555
3556 trace_clock_id = i;
3557
3558 mutex_lock(&trace_types_lock);
3559
3560 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3561 if (max_tr.buffer)
3562 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3563
3564 mutex_unlock(&trace_types_lock);
3565
3566 *fpos += cnt;
3567
3568 return cnt;
3569}
3570
Li Zefan13f16d22009-12-08 11:16:11 +08003571static int tracing_clock_open(struct inode *inode, struct file *file)
3572{
3573 if (tracing_disabled)
3574 return -ENODEV;
3575 return single_open(file, tracing_clock_show, NULL);
3576}
3577
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003578static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003579 .open = tracing_open_generic,
3580 .read = tracing_max_lat_read,
3581 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003582 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003583};
3584
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003585static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003586 .open = tracing_open_generic,
3587 .read = tracing_ctrl_read,
3588 .write = tracing_ctrl_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003589 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003590};
3591
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003592static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003593 .open = tracing_open_generic,
3594 .read = tracing_set_trace_read,
3595 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003596 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003597};
3598
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003599static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003600 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003601 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003602 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003603 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003604 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003605 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003606};
3607
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003608static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003609 .open = tracing_open_generic,
3610 .read = tracing_entries_read,
3611 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003612 .llseek = generic_file_llseek,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003613};
3614
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003615static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003616 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003617 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003618 .llseek = generic_file_llseek,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003619};
3620
Zhaolei5079f322009-08-25 16:12:56 +08003621static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08003622 .open = tracing_clock_open,
3623 .read = seq_read,
3624 .llseek = seq_lseek,
3625 .release = single_release,
Zhaolei5079f322009-08-25 16:12:56 +08003626 .write = tracing_clock_write,
3627};
3628
Steven Rostedt2cadf912008-12-01 22:20:19 -05003629struct ftrace_buffer_info {
3630 struct trace_array *tr;
3631 void *spare;
3632 int cpu;
3633 unsigned int read;
3634};
3635
3636static int tracing_buffers_open(struct inode *inode, struct file *filp)
3637{
3638 int cpu = (int)(long)inode->i_private;
3639 struct ftrace_buffer_info *info;
3640
3641 if (tracing_disabled)
3642 return -ENODEV;
3643
3644 info = kzalloc(sizeof(*info), GFP_KERNEL);
3645 if (!info)
3646 return -ENOMEM;
3647
3648 info->tr = &global_trace;
3649 info->cpu = cpu;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003650 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003651 /* Force reading ring buffer for first read */
3652 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003653
3654 filp->private_data = info;
3655
Lai Jiangshand1e7e022009-04-02 15:16:56 +08003656 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003657}
3658
3659static ssize_t
3660tracing_buffers_read(struct file *filp, char __user *ubuf,
3661 size_t count, loff_t *ppos)
3662{
3663 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003664 ssize_t ret;
3665 size_t size;
3666
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003667 if (!count)
3668 return 0;
3669
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003670 if (!info->spare)
3671 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3672 if (!info->spare)
3673 return -ENOMEM;
3674
Steven Rostedt2cadf912008-12-01 22:20:19 -05003675 /* Do we have previous read data to read? */
3676 if (info->read < PAGE_SIZE)
3677 goto read;
3678
3679 info->read = 0;
3680
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003681 trace_access_lock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003682 ret = ring_buffer_read_page(info->tr->buffer,
3683 &info->spare,
3684 count,
3685 info->cpu, 0);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003686 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003687 if (ret < 0)
3688 return 0;
3689
Steven Rostedt2cadf912008-12-01 22:20:19 -05003690read:
3691 size = PAGE_SIZE - info->read;
3692 if (size > count)
3693 size = count;
3694
3695 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003696 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003697 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003698 size -= ret;
3699
Steven Rostedt2cadf912008-12-01 22:20:19 -05003700 *ppos += size;
3701 info->read += size;
3702
3703 return size;
3704}
3705
3706static int tracing_buffers_release(struct inode *inode, struct file *file)
3707{
3708 struct ftrace_buffer_info *info = file->private_data;
3709
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003710 if (info->spare)
3711 ring_buffer_free_read_page(info->tr->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003712 kfree(info);
3713
3714 return 0;
3715}
3716
3717struct buffer_ref {
3718 struct ring_buffer *buffer;
3719 void *page;
3720 int ref;
3721};
3722
3723static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3724 struct pipe_buffer *buf)
3725{
3726 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3727
3728 if (--ref->ref)
3729 return;
3730
3731 ring_buffer_free_read_page(ref->buffer, ref->page);
3732 kfree(ref);
3733 buf->private = 0;
3734}
3735
3736static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3737 struct pipe_buffer *buf)
3738{
3739 return 1;
3740}
3741
3742static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3743 struct pipe_buffer *buf)
3744{
3745 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3746
3747 ref->ref++;
3748}
3749
3750/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003751static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003752 .can_merge = 0,
3753 .map = generic_pipe_buf_map,
3754 .unmap = generic_pipe_buf_unmap,
3755 .confirm = generic_pipe_buf_confirm,
3756 .release = buffer_pipe_buf_release,
3757 .steal = buffer_pipe_buf_steal,
3758 .get = buffer_pipe_buf_get,
3759};
3760
3761/*
3762 * Callback from splice_to_pipe(), if we need to release some pages
3763 * at the end of the spd in case we error'ed out in filling the pipe.
3764 */
3765static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3766{
3767 struct buffer_ref *ref =
3768 (struct buffer_ref *)spd->partial[i].private;
3769
3770 if (--ref->ref)
3771 return;
3772
3773 ring_buffer_free_read_page(ref->buffer, ref->page);
3774 kfree(ref);
3775 spd->partial[i].private = 0;
3776}
3777
3778static ssize_t
3779tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3780 struct pipe_inode_info *pipe, size_t len,
3781 unsigned int flags)
3782{
3783 struct ftrace_buffer_info *info = file->private_data;
Jens Axboe35f3d142010-05-20 10:43:18 +02003784 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3785 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05003786 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02003787 .pages = pages_def,
3788 .partial = partial_def,
Steven Rostedt2cadf912008-12-01 22:20:19 -05003789 .flags = flags,
3790 .ops = &buffer_pipe_buf_ops,
3791 .spd_release = buffer_spd_release,
3792 };
3793 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04003794 int entries, size, i;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003795 size_t ret;
3796
Jens Axboe35f3d142010-05-20 10:43:18 +02003797 if (splice_grow_spd(pipe, &spd))
3798 return -ENOMEM;
3799
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003800 if (*ppos & (PAGE_SIZE - 1)) {
3801 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02003802 ret = -EINVAL;
3803 goto out;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003804 }
3805
3806 if (len & (PAGE_SIZE - 1)) {
3807 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02003808 if (len < PAGE_SIZE) {
3809 ret = -EINVAL;
3810 goto out;
3811 }
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003812 len &= PAGE_MASK;
3813 }
3814
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003815 trace_access_lock(info->cpu);
Steven Rostedt93459c62009-04-29 00:23:13 -04003816 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3817
Jens Axboe35f3d142010-05-20 10:43:18 +02003818 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003819 struct page *page;
3820 int r;
3821
3822 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3823 if (!ref)
3824 break;
3825
Steven Rostedt7267fa62009-04-29 00:16:21 -04003826 ref->ref = 1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003827 ref->buffer = info->tr->buffer;
3828 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3829 if (!ref->page) {
3830 kfree(ref);
3831 break;
3832 }
3833
3834 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtf2957f12009-04-29 00:26:30 -04003835 len, info->cpu, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003836 if (r < 0) {
3837 ring_buffer_free_read_page(ref->buffer,
3838 ref->page);
3839 kfree(ref);
3840 break;
3841 }
3842
3843 /*
3844 * zero out any left over data, this is going to
3845 * user land.
3846 */
3847 size = ring_buffer_page_len(ref->page);
3848 if (size < PAGE_SIZE)
3849 memset(ref->page + size, 0, PAGE_SIZE - size);
3850
3851 page = virt_to_page(ref->page);
3852
3853 spd.pages[i] = page;
3854 spd.partial[i].len = PAGE_SIZE;
3855 spd.partial[i].offset = 0;
3856 spd.partial[i].private = (unsigned long)ref;
3857 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003858 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04003859
3860 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003861 }
3862
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003863 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003864 spd.nr_pages = i;
3865
3866 /* did we read anything? */
3867 if (!spd.nr_pages) {
3868 if (flags & SPLICE_F_NONBLOCK)
3869 ret = -EAGAIN;
3870 else
3871 ret = 0;
3872 /* TODO: block */
Jens Axboe35f3d142010-05-20 10:43:18 +02003873 goto out;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003874 }
3875
3876 ret = splice_to_pipe(pipe, &spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02003877 splice_shrink_spd(pipe, &spd);
3878out:
Steven Rostedt2cadf912008-12-01 22:20:19 -05003879 return ret;
3880}
3881
3882static const struct file_operations tracing_buffers_fops = {
3883 .open = tracing_buffers_open,
3884 .read = tracing_buffers_read,
3885 .release = tracing_buffers_release,
3886 .splice_read = tracing_buffers_splice_read,
3887 .llseek = no_llseek,
3888};
3889
Steven Rostedtc8d77182009-04-29 18:03:45 -04003890static ssize_t
3891tracing_stats_read(struct file *filp, char __user *ubuf,
3892 size_t count, loff_t *ppos)
3893{
3894 unsigned long cpu = (unsigned long)filp->private_data;
3895 struct trace_array *tr = &global_trace;
3896 struct trace_seq *s;
3897 unsigned long cnt;
3898
Li Zefane4f2d102009-06-15 10:57:28 +08003899 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04003900 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01003901 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04003902
3903 trace_seq_init(s);
3904
3905 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3906 trace_seq_printf(s, "entries: %ld\n", cnt);
3907
3908 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3909 trace_seq_printf(s, "overrun: %ld\n", cnt);
3910
3911 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3912 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3913
Steven Rostedtc8d77182009-04-29 18:03:45 -04003914 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3915
3916 kfree(s);
3917
3918 return count;
3919}
3920
3921static const struct file_operations tracing_stats_fops = {
3922 .open = tracing_open_generic,
3923 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003924 .llseek = generic_file_llseek,
Steven Rostedtc8d77182009-04-29 18:03:45 -04003925};
3926
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003927#ifdef CONFIG_DYNAMIC_FTRACE
3928
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003929int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003930{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003931 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003932}
3933
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003934static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003935tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003936 size_t cnt, loff_t *ppos)
3937{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003938 static char ftrace_dyn_info_buffer[1024];
3939 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003940 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003941 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003942 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003943 int r;
3944
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003945 mutex_lock(&dyn_info_mutex);
3946 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003947
Steven Rostedta26a2a22008-10-31 00:03:22 -04003948 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003949 buf[r++] = '\n';
3950
3951 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3952
3953 mutex_unlock(&dyn_info_mutex);
3954
3955 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003956}
3957
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003958static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003959 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003960 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003961 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003962};
3963#endif
3964
3965static struct dentry *d_tracer;
3966
3967struct dentry *tracing_init_dentry(void)
3968{
3969 static int once;
3970
3971 if (d_tracer)
3972 return d_tracer;
3973
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01003974 if (!debugfs_initialized())
3975 return NULL;
3976
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003977 d_tracer = debugfs_create_dir("tracing", NULL);
3978
3979 if (!d_tracer && !once) {
3980 once = 1;
3981 pr_warning("Could not create debugfs directory 'tracing'\n");
3982 return NULL;
3983 }
3984
3985 return d_tracer;
3986}
3987
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003988static struct dentry *d_percpu;
3989
3990struct dentry *tracing_dentry_percpu(void)
3991{
3992 static int once;
3993 struct dentry *d_tracer;
3994
3995 if (d_percpu)
3996 return d_percpu;
3997
3998 d_tracer = tracing_init_dentry();
3999
4000 if (!d_tracer)
4001 return NULL;
4002
4003 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4004
4005 if (!d_percpu && !once) {
4006 once = 1;
4007 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4008 return NULL;
4009 }
4010
4011 return d_percpu;
4012}
4013
4014static void tracing_init_debugfs_percpu(long cpu)
4015{
4016 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004017 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04004018 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004019
Steven Rostedtdd49a382010-10-20 21:51:26 -04004020 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004021 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4022 if (!d_cpu) {
4023 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4024 return;
4025 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004026
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004027 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004028 trace_create_file("trace_pipe", 0444, d_cpu,
4029 (void *) cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004030
4031 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004032 trace_create_file("trace", 0644, d_cpu,
4033 (void *) cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04004034
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004035 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4036 (void *) cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004037
4038 trace_create_file("stats", 0444, d_cpu,
4039 (void *) cpu, &tracing_stats_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004040}
4041
Steven Rostedt60a11772008-05-12 21:20:44 +02004042#ifdef CONFIG_FTRACE_SELFTEST
4043/* Let selftest have access to static functions in this file */
4044#include "trace_selftest.c"
4045#endif
4046
Steven Rostedt577b7852009-02-26 23:43:05 -05004047struct trace_option_dentry {
4048 struct tracer_opt *opt;
4049 struct tracer_flags *flags;
4050 struct dentry *entry;
4051};
4052
4053static ssize_t
4054trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4055 loff_t *ppos)
4056{
4057 struct trace_option_dentry *topt = filp->private_data;
4058 char *buf;
4059
4060 if (topt->flags->val & topt->opt->bit)
4061 buf = "1\n";
4062 else
4063 buf = "0\n";
4064
4065 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4066}
4067
4068static ssize_t
4069trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4070 loff_t *ppos)
4071{
4072 struct trace_option_dentry *topt = filp->private_data;
4073 unsigned long val;
4074 char buf[64];
4075 int ret;
4076
4077 if (cnt >= sizeof(buf))
4078 return -EINVAL;
4079
4080 if (copy_from_user(&buf, ubuf, cnt))
4081 return -EFAULT;
4082
4083 buf[cnt] = 0;
4084
4085 ret = strict_strtoul(buf, 10, &val);
4086 if (ret < 0)
4087 return ret;
4088
Li Zefan8d18eaa2009-12-08 11:17:06 +08004089 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05004090 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004091
4092 if (!!(topt->flags->val & topt->opt->bit) != val) {
4093 mutex_lock(&trace_types_lock);
4094 ret = __set_tracer_option(current_trace, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05004095 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004096 mutex_unlock(&trace_types_lock);
4097 if (ret)
4098 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05004099 }
4100
4101 *ppos += cnt;
4102
4103 return cnt;
4104}
4105
4106
4107static const struct file_operations trace_options_fops = {
4108 .open = tracing_open_generic,
4109 .read = trace_options_read,
4110 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004111 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05004112};
4113
Steven Rostedta8259072009-02-26 22:19:12 -05004114static ssize_t
4115trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4116 loff_t *ppos)
4117{
4118 long index = (long)filp->private_data;
4119 char *buf;
4120
4121 if (trace_flags & (1 << index))
4122 buf = "1\n";
4123 else
4124 buf = "0\n";
4125
4126 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4127}
4128
4129static ssize_t
4130trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4131 loff_t *ppos)
4132{
4133 long index = (long)filp->private_data;
4134 char buf[64];
4135 unsigned long val;
4136 int ret;
4137
4138 if (cnt >= sizeof(buf))
4139 return -EINVAL;
4140
4141 if (copy_from_user(&buf, ubuf, cnt))
4142 return -EFAULT;
4143
4144 buf[cnt] = 0;
4145
4146 ret = strict_strtoul(buf, 10, &val);
4147 if (ret < 0)
4148 return ret;
4149
Zhaoleif2d84b62009-08-07 18:55:48 +08004150 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05004151 return -EINVAL;
Zhaoleif2d84b62009-08-07 18:55:48 +08004152 set_tracer_flags(1 << index, val);
Steven Rostedta8259072009-02-26 22:19:12 -05004153
4154 *ppos += cnt;
4155
4156 return cnt;
4157}
4158
Steven Rostedta8259072009-02-26 22:19:12 -05004159static const struct file_operations trace_options_core_fops = {
4160 .open = tracing_open_generic,
4161 .read = trace_options_core_read,
4162 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004163 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05004164};
4165
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004166struct dentry *trace_create_file(const char *name,
4167 mode_t mode,
4168 struct dentry *parent,
4169 void *data,
4170 const struct file_operations *fops)
4171{
4172 struct dentry *ret;
4173
4174 ret = debugfs_create_file(name, mode, parent, data, fops);
4175 if (!ret)
4176 pr_warning("Could not create debugfs '%s' entry\n", name);
4177
4178 return ret;
4179}
4180
4181
Steven Rostedta8259072009-02-26 22:19:12 -05004182static struct dentry *trace_options_init_dentry(void)
4183{
4184 struct dentry *d_tracer;
4185 static struct dentry *t_options;
4186
4187 if (t_options)
4188 return t_options;
4189
4190 d_tracer = tracing_init_dentry();
4191 if (!d_tracer)
4192 return NULL;
4193
4194 t_options = debugfs_create_dir("options", d_tracer);
4195 if (!t_options) {
4196 pr_warning("Could not create debugfs directory 'options'\n");
4197 return NULL;
4198 }
4199
4200 return t_options;
4201}
4202
Steven Rostedt577b7852009-02-26 23:43:05 -05004203static void
4204create_trace_option_file(struct trace_option_dentry *topt,
4205 struct tracer_flags *flags,
4206 struct tracer_opt *opt)
4207{
4208 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05004209
4210 t_options = trace_options_init_dentry();
4211 if (!t_options)
4212 return;
4213
4214 topt->flags = flags;
4215 topt->opt = opt;
4216
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004217 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05004218 &trace_options_fops);
4219
Steven Rostedt577b7852009-02-26 23:43:05 -05004220}
4221
4222static struct trace_option_dentry *
4223create_trace_option_files(struct tracer *tracer)
4224{
4225 struct trace_option_dentry *topts;
4226 struct tracer_flags *flags;
4227 struct tracer_opt *opts;
4228 int cnt;
4229
4230 if (!tracer)
4231 return NULL;
4232
4233 flags = tracer->flags;
4234
4235 if (!flags || !flags->opts)
4236 return NULL;
4237
4238 opts = flags->opts;
4239
4240 for (cnt = 0; opts[cnt].name; cnt++)
4241 ;
4242
Steven Rostedt0cfe8242009-02-27 10:51:10 -05004243 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05004244 if (!topts)
4245 return NULL;
4246
4247 for (cnt = 0; opts[cnt].name; cnt++)
4248 create_trace_option_file(&topts[cnt], flags,
4249 &opts[cnt]);
4250
4251 return topts;
4252}
4253
4254static void
4255destroy_trace_option_files(struct trace_option_dentry *topts)
4256{
4257 int cnt;
4258
4259 if (!topts)
4260 return;
4261
4262 for (cnt = 0; topts[cnt].opt; cnt++) {
4263 if (topts[cnt].entry)
4264 debugfs_remove(topts[cnt].entry);
4265 }
4266
4267 kfree(topts);
4268}
4269
Steven Rostedta8259072009-02-26 22:19:12 -05004270static struct dentry *
4271create_trace_option_core_file(const char *option, long index)
4272{
4273 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004274
4275 t_options = trace_options_init_dentry();
4276 if (!t_options)
4277 return NULL;
4278
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004279 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05004280 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004281}
4282
4283static __init void create_trace_options_dir(void)
4284{
4285 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004286 int i;
4287
4288 t_options = trace_options_init_dentry();
4289 if (!t_options)
4290 return;
4291
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004292 for (i = 0; trace_options[i]; i++)
4293 create_trace_option_core_file(trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05004294}
4295
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004296static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004297{
4298 struct dentry *d_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004299 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004300
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004301 trace_access_lock_init();
4302
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004303 d_tracer = tracing_init_dentry();
4304
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004305 trace_create_file("tracing_enabled", 0644, d_tracer,
4306 &global_trace, &tracing_ctrl_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004307
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004308 trace_create_file("trace_options", 0644, d_tracer,
4309 NULL, &tracing_iter_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004310
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004311 trace_create_file("tracing_cpumask", 0644, d_tracer,
4312 NULL, &tracing_cpumask_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004313
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004314 trace_create_file("trace", 0644, d_tracer,
4315 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004316
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004317 trace_create_file("available_tracers", 0444, d_tracer,
4318 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004319
Li Zefan339ae5d2009-04-17 10:34:30 +08004320 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004321 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004322
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04004323#ifdef CONFIG_TRACER_MAX_TRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004324 trace_create_file("tracing_max_latency", 0644, d_tracer,
4325 &tracing_max_latency, &tracing_max_lat_fops);
Tim Bird0e950172010-02-25 15:36:43 -08004326#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004327
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004328 trace_create_file("tracing_thresh", 0644, d_tracer,
4329 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004330
Li Zefan339ae5d2009-04-17 10:34:30 +08004331 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004332 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004333
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004334 trace_create_file("trace_pipe", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004335 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004336
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004337 trace_create_file("buffer_size_kb", 0644, d_tracer,
4338 &global_trace, &tracing_entries_fops);
Steven Rostedta98a3c32008-05-12 21:20:59 +02004339
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004340 trace_create_file("trace_marker", 0220, d_tracer,
4341 NULL, &tracing_mark_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004342
Avadh Patel69abe6a2009-04-10 16:04:48 -04004343 trace_create_file("saved_cmdlines", 0444, d_tracer,
4344 NULL, &tracing_saved_cmdlines_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004345
Zhaolei5079f322009-08-25 16:12:56 +08004346 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4347 &trace_clock_fops);
4348
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004349#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004350 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4351 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004352#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004353
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004354 create_trace_options_dir();
4355
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004356 for_each_tracing_cpu(cpu)
4357 tracing_init_debugfs_percpu(cpu);
4358
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004359 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004360}
4361
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004362static int trace_panic_handler(struct notifier_block *this,
4363 unsigned long event, void *unused)
4364{
Steven Rostedt944ac422008-10-23 19:26:08 -04004365 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004366 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004367 return NOTIFY_OK;
4368}
4369
4370static struct notifier_block trace_panic_notifier = {
4371 .notifier_call = trace_panic_handler,
4372 .next = NULL,
4373 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4374};
4375
4376static int trace_die_handler(struct notifier_block *self,
4377 unsigned long val,
4378 void *data)
4379{
4380 switch (val) {
4381 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04004382 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004383 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004384 break;
4385 default:
4386 break;
4387 }
4388 return NOTIFY_OK;
4389}
4390
4391static struct notifier_block trace_die_notifier = {
4392 .notifier_call = trace_die_handler,
4393 .priority = 200
4394};
4395
4396/*
4397 * printk is set to max of 1024, we really don't need it that big.
4398 * Nothing should be printing 1000 characters anyway.
4399 */
4400#define TRACE_MAX_PRINT 1000
4401
4402/*
4403 * Define here KERN_TRACE so that we have one place to modify
4404 * it if we decide to change what log level the ftrace dump
4405 * should be at.
4406 */
Steven Rostedt428aee12009-01-14 12:24:42 -05004407#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004408
Jason Wessel955b61e2010-08-05 09:22:23 -05004409void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004410trace_printk_seq(struct trace_seq *s)
4411{
4412 /* Probably should print a warning here. */
4413 if (s->len >= 1000)
4414 s->len = 1000;
4415
4416 /* should be zero ended, but we are paranoid. */
4417 s->buffer[s->len] = 0;
4418
4419 printk(KERN_TRACE "%s", s->buffer);
4420
Steven Rostedtf9520752009-03-02 14:04:40 -05004421 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004422}
4423
Jason Wessel955b61e2010-08-05 09:22:23 -05004424void trace_init_global_iter(struct trace_iterator *iter)
4425{
4426 iter->tr = &global_trace;
4427 iter->trace = current_trace;
4428 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4429}
4430
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004431static void
4432__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004433{
Thomas Gleixner445c8952009-12-02 19:49:50 +01004434 static arch_spinlock_t ftrace_dump_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01004435 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004436 /* use static because iter can be a bit big for the stack */
4437 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004438 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004439 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004440 unsigned long flags;
4441 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004442
4443 /* only one dump */
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004444 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004445 arch_spin_lock(&ftrace_dump_lock);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004446 if (dump_ran)
4447 goto out;
4448
4449 dump_ran = 1;
4450
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004451 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004452
4453 if (disable_tracing)
4454 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004455
Jason Wessel955b61e2010-08-05 09:22:23 -05004456 trace_init_global_iter(&iter);
4457
Steven Rostedtd7690412008-10-01 00:29:53 -04004458 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05004459 atomic_inc(&iter.tr->data[cpu]->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04004460 }
4461
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004462 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4463
Török Edwinb54d3de2008-11-22 13:28:48 +02004464 /* don't look at user memory in panic mode */
4465 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4466
Steven Rostedte543ad72009-03-04 18:20:36 -05004467 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004468 iter.tr = &global_trace;
4469 iter.trace = current_trace;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004470
4471 switch (oops_dump_mode) {
4472 case DUMP_ALL:
4473 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4474 break;
4475 case DUMP_ORIG:
4476 iter.cpu_file = raw_smp_processor_id();
4477 break;
4478 case DUMP_NONE:
4479 goto out_enable;
4480 default:
4481 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4482 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4483 }
4484
4485 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004486
4487 /*
4488 * We need to stop all tracing on all CPUS to read the
4489 * the next buffer. This is a bit expensive, but is
4490 * not done often. We fill all what we can read,
4491 * and then release the locks again.
4492 */
4493
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004494 while (!trace_empty(&iter)) {
4495
4496 if (!cnt)
4497 printk(KERN_TRACE "---------------------------------\n");
4498
4499 cnt++;
4500
4501 /* reset all but tr, trace, and overruns */
4502 memset(&iter.seq, 0,
4503 sizeof(struct trace_iterator) -
4504 offsetof(struct trace_iterator, seq));
4505 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4506 iter.pos = -1;
4507
Jason Wessel955b61e2010-08-05 09:22:23 -05004508 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08004509 int ret;
4510
4511 ret = print_trace_line(&iter);
4512 if (ret != TRACE_TYPE_NO_CONSUME)
4513 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004514 }
4515
4516 trace_printk_seq(&iter.seq);
4517 }
4518
4519 if (!cnt)
4520 printk(KERN_TRACE " (ftrace buffer empty)\n");
4521 else
4522 printk(KERN_TRACE "---------------------------------\n");
4523
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004524 out_enable:
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004525 /* Re-enable tracing if requested */
4526 if (!disable_tracing) {
4527 trace_flags |= old_userobj;
4528
4529 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05004530 atomic_dec(&iter.tr->data[cpu]->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004531 }
4532 tracing_on();
4533 }
4534
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004535 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004536 arch_spin_unlock(&ftrace_dump_lock);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004537 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004538}
4539
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004540/* By default: disable tracing after the dump */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004541void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004542{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004543 __ftrace_dump(true, oops_dump_mode);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004544}
4545
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004546__init static int tracer_alloc_buffers(void)
4547{
Steven Rostedt73c51622009-03-11 13:42:01 -04004548 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004549 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304550 int ret = -ENOMEM;
4551
4552 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4553 goto out;
4554
4555 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4556 goto out_free_buffer_mask;
4557
Steven Rostedt73c51622009-03-11 13:42:01 -04004558 /* To save memory, keep the ring buffer size to its minimum */
4559 if (ring_buffer_expanded)
4560 ring_buf_size = trace_buf_size;
4561 else
4562 ring_buf_size = 1;
4563
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304564 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4565 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004566
Steven Rostedtab464282008-05-12 21:21:00 +02004567 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04004568 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004569 TRACE_BUFFER_FLAGS);
4570 if (!global_trace.buffer) {
4571 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4572 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304573 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004574 }
4575 global_trace.entries = ring_buffer_size(global_trace.buffer);
4576
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304577
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004578#ifdef CONFIG_TRACER_MAX_TRACE
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004579 max_tr.buffer = ring_buffer_alloc(1, TRACE_BUFFER_FLAGS);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004580 if (!max_tr.buffer) {
4581 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4582 WARN_ON(1);
4583 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304584 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004585 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004586 max_tr.entries = 1;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004587#endif
4588
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004589 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004590 for_each_tracing_cpu(i) {
jolsa@redhat.com566b0aa2009-07-16 21:44:26 +02004591 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Tejun Heo9705f692009-10-29 22:34:13 +09004592 max_tr.data[i] = &per_cpu(max_tr_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004593 }
4594
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004595 trace_init_cmdlines();
4596
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004597 register_tracer(&nop_trace);
Steven Rostedt79fb0768f2009-02-02 21:38:33 -05004598 current_trace = &nop_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +02004599 /* All seems OK, enable tracing */
4600 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004601
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004602 atomic_notifier_chain_register(&panic_notifier_list,
4603 &trace_panic_notifier);
4604
4605 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01004606
4607 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004608
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304609out_free_cpumask:
4610 free_cpumask_var(tracing_cpumask);
4611out_free_buffer_mask:
4612 free_cpumask_var(tracing_buffer_mask);
4613out:
4614 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004615}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004616
4617__init static int clear_boot_tracer(void)
4618{
4619 /*
4620 * The default tracer at boot buffer is an init section.
4621 * This function is called in lateinit. If we did not
4622 * find the boot tracer, then clear it out, to prevent
4623 * later registration from accessing the buffer that is
4624 * about to be freed.
4625 */
4626 if (!default_bootup_tracer)
4627 return 0;
4628
4629 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4630 default_bootup_tracer);
4631 default_bootup_tracer = NULL;
4632
4633 return 0;
4634}
4635
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004636early_initcall(tracer_alloc_buffers);
4637fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004638late_initcall(clear_boot_tracer);