blob: 85e3ee1e474e1a1336507dd592c1ab9f8619344f [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>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040020#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050021#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020022#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020023#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020024#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050027#include <linux/kprobes.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050031#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040032#include <linux/kdebug.h>
Frederic Weisbecker5f0c6c02009-03-27 14:22:10 +010033#include <linux/string.h>
Lai Jiangshan7e53bd42010-01-06 20:08:50 +080034#include <linux/rwsem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020036#include <linux/ctype.h>
37#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020038#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020039#include <linux/fs.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020040
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020041#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050042#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020043
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010044/*
Steven Rostedt73c51622009-03-11 13:42:01 -040045 * On boot up, the ring buffer is set to the minimum size, so that
46 * we do not waste memory on systems that are not using tracing.
47 */
Li Zefan020e5f82009-07-01 10:47:05 +080048int ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040049
50/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010051 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010052 * A selftest will lurk into the ring-buffer to count the
53 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010054 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010055 * at the same time, giving false positive or negative results.
56 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010057static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010058
Steven Rostedtb2821ae2009-02-02 21:38:32 -050059/*
60 * If a tracer is running, we do not want to run SELFTEST.
61 */
Li Zefan020e5f82009-07-01 10:47:05 +080062bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050063
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010064/* For tracers that don't implement custom flags */
65static struct tracer_opt dummy_tracer_opt[] = {
66 { }
67};
68
69static struct tracer_flags dummy_tracer_flags = {
70 .val = 0,
71 .opts = dummy_tracer_opt
72};
73
74static int dummy_set_flag(u32 old_flags, u32 bit, int set)
75{
76 return 0;
77}
Steven Rostedt0f048702008-11-05 16:05:44 -050078
79/*
80 * Kill all tracing for good (never come back).
81 * It is initialized to 1 but will turn to zero if the initialization
82 * of the tracer is successful. But that is the only place that sets
83 * this back to zero.
84 */
Hannes Eder4fd27352009-02-10 19:44:12 +010085static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050086
Christoph Lameter9288f992009-10-07 19:17:45 -040087DEFINE_PER_CPU(int, ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040088
89static inline void ftrace_disable_cpu(void)
90{
91 preempt_disable();
Rusty Russelldd17c8f2009-10-29 22:34:15 +090092 __this_cpu_inc(ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040093}
94
95static inline void ftrace_enable_cpu(void)
96{
Rusty Russelldd17c8f2009-10-29 22:34:15 +090097 __this_cpu_dec(ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040098 preempt_enable();
99}
100
Jason Wessel955b61e2010-08-05 09:22:23 -0500101cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200102
Steven Rostedt944ac422008-10-23 19:26:08 -0400103/*
104 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
105 *
106 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
107 * is set, then ftrace_dump is called. This will output the contents
108 * of the ftrace buffers to the console. This is very useful for
109 * capturing traces that lead to crashes and outputing it to a
110 * serial console.
111 *
112 * It is default off, but you can enable it with either specifying
113 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200114 * /proc/sys/kernel/ftrace_dump_on_oops
115 * Set 1 if you want to dump buffers of all CPUs
116 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400117 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200118
119enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400120
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500121static int tracing_set_tracer(const char *buf);
122
Li Zefanee6c2c12009-09-18 14:06:47 +0800123#define MAX_TRACER_SIZE 100
124static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500125static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100126
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200127static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100128{
Li Zefanee6c2c12009-09-18 14:06:47 +0800129 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500130 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400131 /* We are using ftrace early, expand it */
132 ring_buffer_expanded = 1;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100133 return 1;
134}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200135__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100136
Steven Rostedt944ac422008-10-23 19:26:08 -0400137static int __init set_ftrace_dump_on_oops(char *str)
138{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200139 if (*str++ != '=' || !*str) {
140 ftrace_dump_on_oops = DUMP_ALL;
141 return 1;
142 }
143
144 if (!strcmp("orig_cpu", str)) {
145 ftrace_dump_on_oops = DUMP_ORIG;
146 return 1;
147 }
148
149 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400150}
151__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200152
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800153unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200154{
155 nsec += 500;
156 do_div(nsec, 1000);
157 return nsec;
158}
159
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200160/*
161 * The global_trace is the descriptor that holds the tracing
162 * buffers for the live tracing. For each CPU, it contains
163 * a link list of pages that will store trace entries. The
164 * page descriptor of the pages in the memory is used to hold
165 * the link list by linking the lru item in the page descriptor
166 * to each of the pages in the buffer per CPU.
167 *
168 * For each active CPU there is a data field that holds the
169 * pages for the buffer for that CPU. Each CPU has the same number
170 * of pages allocated for its buffer.
171 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200172static struct trace_array global_trace;
173
174static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
175
Steven Rostedte77405a2009-09-02 14:17:06 -0400176int filter_current_check_discard(struct ring_buffer *buffer,
177 struct ftrace_event_call *call, void *rec,
Tom Zanussieb02ce02009-04-08 03:15:54 -0500178 struct ring_buffer_event *event)
179{
Steven Rostedte77405a2009-09-02 14:17:06 -0400180 return filter_check_discard(call, rec, buffer, event);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500181}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400182EXPORT_SYMBOL_GPL(filter_current_check_discard);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500183
Steven Rostedt37886f62009-03-17 17:22:06 -0400184cycle_t ftrace_now(int cpu)
185{
186 u64 ts;
187
188 /* Early boot up does not have a buffer yet */
189 if (!global_trace.buffer)
190 return trace_clock_local();
191
192 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
193 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
194
195 return ts;
196}
197
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200198/*
199 * The max_tr is used to snapshot the global_trace when a maximum
200 * latency is reached. Some tracers will use this to store a maximum
201 * trace while it continues examining live traces.
202 *
203 * The buffers for the max_tr are set up the same as the global_trace.
204 * When a snapshot is taken, the link list of the max_tr is swapped
205 * with the link list of the global_trace and the buffers are reset for
206 * the global_trace so the tracing can continue.
207 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200208static struct trace_array max_tr;
209
Tejun Heo9705f692009-10-29 22:34:13 +0900210static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200211
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200212/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200213static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200214
Steven Rostedt90369902008-11-05 16:05:44 -0500215/**
216 * tracing_is_enabled - return tracer_enabled status
217 *
218 * This function is used by other tracers to know the status
219 * of the tracer_enabled flag. Tracers may use this function
220 * to know if it should enable their features when starting
221 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
222 */
223int tracing_is_enabled(void)
224{
225 return tracer_enabled;
226}
227
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200228/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400229 * trace_buf_size is the size in bytes that is allocated
230 * for a buffer. Note, the number of bytes is always rounded
231 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400232 *
233 * This number is purposely set to a low number of 16384.
234 * If the dump on oops happens, it will be much appreciated
235 * to not have to wait for all that output. Anyway this can be
236 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200237 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400238#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400239
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400240static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200241
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200242/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200243static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200244
245/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200246static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200247
248/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200249 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200250 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200251static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200252
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800253/*
254 * serialize the access of the ring buffer
255 *
256 * ring buffer serializes readers, but it is low level protection.
257 * The validity of the events (which returns by ring_buffer_peek() ..etc)
258 * are not protected by ring buffer.
259 *
260 * The content of events may become garbage if we allow other process consumes
261 * these events concurrently:
262 * A) the page of the consumed events may become a normal page
263 * (not reader page) in ring buffer, and this page will be rewrited
264 * by events producer.
265 * B) The page of the consumed events may become a page for splice_read,
266 * and this page will be returned to system.
267 *
268 * These primitives allow multi process access to different cpu ring buffer
269 * concurrently.
270 *
271 * These primitives don't distinguish read-only and read-consume access.
272 * Multi read-only access are also serialized.
273 */
274
275#ifdef CONFIG_SMP
276static DECLARE_RWSEM(all_cpu_access_lock);
277static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
278
279static inline void trace_access_lock(int cpu)
280{
281 if (cpu == TRACE_PIPE_ALL_CPU) {
282 /* gain it for accessing the whole ring buffer. */
283 down_write(&all_cpu_access_lock);
284 } else {
285 /* gain it for accessing a cpu ring buffer. */
286
287 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
288 down_read(&all_cpu_access_lock);
289
290 /* Secondly block other access to this @cpu ring buffer. */
291 mutex_lock(&per_cpu(cpu_access_lock, cpu));
292 }
293}
294
295static inline void trace_access_unlock(int cpu)
296{
297 if (cpu == TRACE_PIPE_ALL_CPU) {
298 up_write(&all_cpu_access_lock);
299 } else {
300 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
301 up_read(&all_cpu_access_lock);
302 }
303}
304
305static inline void trace_access_lock_init(void)
306{
307 int cpu;
308
309 for_each_possible_cpu(cpu)
310 mutex_init(&per_cpu(cpu_access_lock, cpu));
311}
312
313#else
314
315static DEFINE_MUTEX(access_lock);
316
317static inline void trace_access_lock(int cpu)
318{
319 (void)cpu;
320 mutex_lock(&access_lock);
321}
322
323static inline void trace_access_unlock(int cpu)
324{
325 (void)cpu;
326 mutex_unlock(&access_lock);
327}
328
329static inline void trace_access_lock_init(void)
330{
331}
332
333#endif
334
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200335/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200336static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
337
Steven Rostedtee6bce52008-11-12 17:52:37 -0500338/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500339unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400340 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
David Sharp750912f2010-12-08 13:46:47 -0800341 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE;
Ingo Molnar4e655512008-05-12 21:20:52 +0200342
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400343static int trace_stop_count;
344static DEFINE_SPINLOCK(tracing_start_lock);
345
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200346/**
347 * trace_wake_up - wake up tasks waiting for trace input
348 *
349 * Simply wakes up any task that is blocked on the trace_wait
350 * queue. These is used with trace_poll for tasks polling the trace.
351 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200352void trace_wake_up(void)
353{
Andrew Morton89f19f02009-09-19 11:55:44 -0700354 int cpu;
355
356 if (trace_flags & TRACE_ITER_BLOCK)
357 return;
Ingo Molnar017730c2008-05-12 21:20:52 +0200358 /*
359 * The runqueue_is_locked() can fail, but this is the best we
360 * have for now:
361 */
Andrew Morton89f19f02009-09-19 11:55:44 -0700362 cpu = get_cpu();
363 if (!runqueue_is_locked(cpu))
Ingo Molnar4e655512008-05-12 21:20:52 +0200364 wake_up(&trace_wait);
Andrew Morton89f19f02009-09-19 11:55:44 -0700365 put_cpu();
Ingo Molnar4e655512008-05-12 21:20:52 +0200366}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200367
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400368static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200369{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400370 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200371
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200372 if (!str)
373 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800374 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200375 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800376 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200377 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400378 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200379 return 1;
380}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400381__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200382
Tim Bird0e950172010-02-25 15:36:43 -0800383static int __init set_tracing_thresh(char *str)
384{
385 unsigned long threshhold;
386 int ret;
387
388 if (!str)
389 return 0;
390 ret = strict_strtoul(str, 0, &threshhold);
391 if (ret < 0)
392 return 0;
393 tracing_thresh = threshhold * 1000;
394 return 1;
395}
396__setup("tracing_thresh=", set_tracing_thresh);
397
Steven Rostedt57f50be2008-05-12 21:20:44 +0200398unsigned long nsecs_to_usecs(unsigned long nsecs)
399{
400 return nsecs / 1000;
401}
402
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200403/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200404static const char *trace_options[] = {
405 "print-parent",
406 "sym-offset",
407 "sym-addr",
408 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200409 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200410 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200411 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200412 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200413 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100414 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500415 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500416 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500417 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200418 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200419 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100420 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200421 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500422 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400423 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400424 "graph-time",
Li Zefane870e9a2010-07-02 11:07:32 +0800425 "record-cmd",
David Sharp750912f2010-12-08 13:46:47 -0800426 "overwrite",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200427 NULL
428};
429
Zhaolei5079f322009-08-25 16:12:56 +0800430static struct {
431 u64 (*func)(void);
432 const char *name;
433} trace_clocks[] = {
434 { trace_clock_local, "local" },
435 { trace_clock_global, "global" },
436};
437
438int trace_clock_id;
439
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200440/*
441 * trace_parser_get_init - gets the buffer for trace parser
442 */
443int trace_parser_get_init(struct trace_parser *parser, int size)
444{
445 memset(parser, 0, sizeof(*parser));
446
447 parser->buffer = kmalloc(size, GFP_KERNEL);
448 if (!parser->buffer)
449 return 1;
450
451 parser->size = size;
452 return 0;
453}
454
455/*
456 * trace_parser_put - frees the buffer for trace parser
457 */
458void trace_parser_put(struct trace_parser *parser)
459{
460 kfree(parser->buffer);
461}
462
463/*
464 * trace_get_user - reads the user input string separated by space
465 * (matched by isspace(ch))
466 *
467 * For each string found the 'struct trace_parser' is updated,
468 * and the function returns.
469 *
470 * Returns number of bytes read.
471 *
472 * See kernel/trace/trace.h for 'struct trace_parser' details.
473 */
474int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
475 size_t cnt, loff_t *ppos)
476{
477 char ch;
478 size_t read = 0;
479 ssize_t ret;
480
481 if (!*ppos)
482 trace_parser_clear(parser);
483
484 ret = get_user(ch, ubuf++);
485 if (ret)
486 goto out;
487
488 read++;
489 cnt--;
490
491 /*
492 * The parser is not finished with the last write,
493 * continue reading the user input without skipping spaces.
494 */
495 if (!parser->cont) {
496 /* skip white space */
497 while (cnt && isspace(ch)) {
498 ret = get_user(ch, ubuf++);
499 if (ret)
500 goto out;
501 read++;
502 cnt--;
503 }
504
505 /* only spaces were written */
506 if (isspace(ch)) {
507 *ppos += read;
508 ret = read;
509 goto out;
510 }
511
512 parser->idx = 0;
513 }
514
515 /* read the non-space input */
516 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800517 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200518 parser->buffer[parser->idx++] = ch;
519 else {
520 ret = -EINVAL;
521 goto out;
522 }
523 ret = get_user(ch, ubuf++);
524 if (ret)
525 goto out;
526 read++;
527 cnt--;
528 }
529
530 /* We either got finished input or we have to wait for another call. */
531 if (isspace(ch)) {
532 parser->buffer[parser->idx] = 0;
533 parser->cont = false;
534 } else {
535 parser->cont = true;
536 parser->buffer[parser->idx++] = ch;
537 }
538
539 *ppos += read;
540 ret = read;
541
542out:
543 return ret;
544}
545
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200546ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
547{
548 int len;
549 int ret;
550
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500551 if (!cnt)
552 return 0;
553
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200554 if (s->len <= s->readpos)
555 return -EBUSY;
556
557 len = s->len - s->readpos;
558 if (cnt > len)
559 cnt = len;
560 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500561 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200562 return -EFAULT;
563
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500564 cnt -= ret;
565
Steven Rostedte74da522009-03-04 20:31:11 -0500566 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200567 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200568}
569
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200570static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200571{
572 int len;
573 void *ret;
574
575 if (s->len <= s->readpos)
576 return -EBUSY;
577
578 len = s->len - s->readpos;
579 if (cnt > len)
580 cnt = len;
581 ret = memcpy(buf, s->buffer + s->readpos, cnt);
582 if (!ret)
583 return -EFAULT;
584
Steven Rostedte74da522009-03-04 20:31:11 -0500585 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200586 return cnt;
587}
588
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400589/*
590 * ftrace_max_lock is used to protect the swapping of buffers
591 * when taking a max snapshot. The buffers themselves are
592 * protected by per_cpu spinlocks. But the action of the swap
593 * needs its own lock.
594 *
Thomas Gleixner445c8952009-12-02 19:49:50 +0100595 * This is defined as a arch_spinlock_t in order to help
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400596 * with performance when lockdep debugging is enabled.
597 *
598 * It is also used in other places outside the update_max_tr
599 * so it needs to be defined outside of the
600 * CONFIG_TRACER_MAX_TRACE.
601 */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100602static arch_spinlock_t ftrace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100603 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400604
Tim Bird0e950172010-02-25 15:36:43 -0800605unsigned long __read_mostly tracing_thresh;
606
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400607#ifdef CONFIG_TRACER_MAX_TRACE
608unsigned long __read_mostly tracing_max_latency;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400609
610/*
611 * Copy the new maximum trace into the separate maximum-trace
612 * structure. (this way the maximum trace is permanently saved,
613 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
614 */
615static void
616__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
617{
618 struct trace_array_cpu *data = tr->data[cpu];
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300619 struct trace_array_cpu *max_data;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400620
621 max_tr.cpu = cpu;
622 max_tr.time_start = data->preempt_timestamp;
623
Steven Rostedt8248ac02009-09-02 12:27:41 -0400624 max_data = max_tr.data[cpu];
625 max_data->saved_latency = tracing_max_latency;
626 max_data->critical_start = data->critical_start;
627 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400628
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300629 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -0400630 max_data->pid = tsk->pid;
631 max_data->uid = task_uid(tsk);
632 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
633 max_data->policy = tsk->policy;
634 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400635
636 /* record this tasks comm */
637 tracing_record_cmdline(tsk);
638}
639
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200640/**
641 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
642 * @tr: tracer
643 * @tsk: the task with the latency
644 * @cpu: The cpu that initiated the trace.
645 *
646 * Flip the buffers between the @tr and the max_tr and record information
647 * about which task was the cause of this latency.
648 */
Ingo Molnare309b412008-05-12 21:20:51 +0200649void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200650update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
651{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400652 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400654 if (trace_stop_count)
655 return;
656
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200657 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900658 if (!current_trace->use_max_tr) {
659 WARN_ON_ONCE(1);
660 return;
661 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100662 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400663
664 tr->buffer = max_tr.buffer;
665 max_tr.buffer = buf;
666
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200667 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100668 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200669}
670
671/**
672 * update_max_tr_single - only copy one trace over, and reset the rest
673 * @tr - tracer
674 * @tsk - task with the latency
675 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200676 *
677 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200678 */
Ingo Molnare309b412008-05-12 21:20:51 +0200679void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200680update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
681{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400682 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200683
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400684 if (trace_stop_count)
685 return;
686
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200687 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900688 if (!current_trace->use_max_tr) {
689 WARN_ON_ONCE(1);
690 return;
691 }
692
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100693 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200694
Steven Rostedtd7690412008-10-01 00:29:53 -0400695 ftrace_disable_cpu();
696
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400697 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
698
Steven Rostedte8165db2009-09-03 19:13:05 -0400699 if (ret == -EBUSY) {
700 /*
701 * We failed to swap the buffer due to a commit taking
702 * place on this CPU. We fail to record, but we reset
703 * the max trace buffer (no one writes directly to it)
704 * and flag that it failed.
705 */
706 trace_array_printk(&max_tr, _THIS_IP_,
707 "Failed to swap buffers due to commit in progress\n");
708 }
709
Steven Rostedtd7690412008-10-01 00:29:53 -0400710 ftrace_enable_cpu();
711
Steven Rostedte8165db2009-09-03 19:13:05 -0400712 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200713
714 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100715 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200716}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400717#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200718
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200719/**
720 * register_tracer - register a tracer with the ftrace system.
721 * @type - the plugin for the tracer
722 *
723 * Register a new plugin tracer.
724 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200725int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100726__releases(kernel_lock)
727__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200728{
729 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730 int ret = 0;
731
732 if (!type->name) {
733 pr_info("Tracer must have a name\n");
734 return -1;
735 }
736
Dan Carpenter24a461d2010-07-10 12:06:44 +0200737 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +0800738 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
739 return -1;
740 }
741
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200742 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100743
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100744 tracing_selftest_running = true;
745
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200746 for (t = trace_types; t; t = t->next) {
747 if (strcmp(type->name, t->name) == 0) {
748 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +0800749 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200750 type->name);
751 ret = -1;
752 goto out;
753 }
754 }
755
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100756 if (!type->set_flag)
757 type->set_flag = &dummy_set_flag;
758 if (!type->flags)
759 type->flags = &dummy_tracer_flags;
760 else
761 if (!type->flags->opts)
762 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100763 if (!type->wait_pipe)
764 type->wait_pipe = default_wait_pipe;
765
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100766
Steven Rostedt60a11772008-05-12 21:20:44 +0200767#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500768 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200769 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200770 struct trace_array *tr = &global_trace;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100771
Steven Rostedt60a11772008-05-12 21:20:44 +0200772 /*
773 * Run a selftest on this tracer.
774 * Here we reset the trace buffer, and set the current
775 * tracer to be this tracer. The tracer can then run some
776 * internal tracing to verify that everything is in order.
777 * If we fail, we do not register this tracer.
778 */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400779 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100780
Steven Rostedt60a11772008-05-12 21:20:44 +0200781 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200782 /* the test is responsible for initializing and enabling */
783 pr_info("Testing tracer %s: ", type->name);
784 ret = type->selftest(type, tr);
785 /* the test is responsible for resetting too */
786 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200787 if (ret) {
788 printk(KERN_CONT "FAILED!\n");
789 goto out;
790 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200791 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400792 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100793
Steven Rostedt60a11772008-05-12 21:20:44 +0200794 printk(KERN_CONT "PASSED\n");
795 }
796#endif
797
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200798 type->next = trace_types;
799 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200800
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200801 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100802 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200803 mutex_unlock(&trace_types_lock);
804
Steven Rostedtdac74942009-02-05 01:13:38 -0500805 if (ret || !default_bootup_tracer)
806 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500807
Li Zefanee6c2c12009-09-18 14:06:47 +0800808 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -0500809 goto out_unlock;
810
811 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
812 /* Do we want this tracer to start on bootup? */
813 tracing_set_tracer(type->name);
814 default_bootup_tracer = NULL;
815 /* disable other selftests, since this will break it. */
816 tracing_selftest_disabled = 1;
817#ifdef CONFIG_FTRACE_STARTUP_TEST
818 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
819 type->name);
820#endif
821
822 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200823 return ret;
824}
825
826void unregister_tracer(struct tracer *type)
827{
828 struct tracer **t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200829
830 mutex_lock(&trace_types_lock);
831 for (t = &trace_types; *t; t = &(*t)->next) {
832 if (*t == type)
833 goto found;
834 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800835 pr_info("Tracer %s not registered\n", type->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200836 goto out;
837
838 found:
839 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200840
841 if (type == current_trace && tracer_enabled) {
842 tracer_enabled = 0;
843 tracing_stop();
844 if (current_trace->stop)
845 current_trace->stop(&global_trace);
846 current_trace = &nop_trace;
847 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800848out:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200849 mutex_unlock(&trace_types_lock);
850}
851
Steven Rostedt283740c2010-03-12 19:48:41 -0500852static void __tracing_reset(struct ring_buffer *buffer, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200853{
Steven Rostedtd7690412008-10-01 00:29:53 -0400854 ftrace_disable_cpu();
Steven Rostedt283740c2010-03-12 19:48:41 -0500855 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400856 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200857}
858
Steven Rostedtf6339032009-09-04 12:35:16 -0400859void tracing_reset(struct trace_array *tr, int cpu)
860{
861 struct ring_buffer *buffer = tr->buffer;
862
863 ring_buffer_record_disable(buffer);
864
865 /* Make sure all commits have finished */
866 synchronize_sched();
Steven Rostedt283740c2010-03-12 19:48:41 -0500867 __tracing_reset(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -0400868
869 ring_buffer_record_enable(buffer);
870}
871
Pekka J Enberg213cc062008-12-19 12:08:39 +0200872void tracing_reset_online_cpus(struct trace_array *tr)
873{
Steven Rostedt621968c2009-09-04 12:02:35 -0400874 struct ring_buffer *buffer = tr->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +0200875 int cpu;
876
Steven Rostedt621968c2009-09-04 12:02:35 -0400877 ring_buffer_record_disable(buffer);
878
879 /* Make sure all commits have finished */
880 synchronize_sched();
881
Pekka J Enberg213cc062008-12-19 12:08:39 +0200882 tr->time_start = ftrace_now(tr->cpu);
883
884 for_each_online_cpu(cpu)
Steven Rostedt283740c2010-03-12 19:48:41 -0500885 __tracing_reset(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -0400886
887 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +0200888}
889
Steven Rostedt9456f0f2009-05-06 21:54:09 -0400890void tracing_reset_current(int cpu)
891{
892 tracing_reset(&global_trace, cpu);
893}
894
895void tracing_reset_current_online_cpus(void)
896{
897 tracing_reset_online_cpus(&global_trace);
898}
899
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200900#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100901#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200902static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
903static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
904static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
905static int cmdline_idx;
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100906static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200907
Steven Rostedt25b0b442008-05-12 21:21:00 +0200908/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100909static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200910
911static void trace_init_cmdlines(void)
912{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100913 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
914 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200915 cmdline_idx = 0;
916}
917
Carsten Emdeb5130b12009-09-13 01:43:07 +0200918int is_tracing_stopped(void)
919{
920 return trace_stop_count;
921}
922
Steven Rostedt0f048702008-11-05 16:05:44 -0500923/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500924 * ftrace_off_permanent - disable all ftrace code permanently
925 *
926 * This should only be called when a serious anomally has
927 * been detected. This will turn off the function tracing,
928 * ring buffers, and other tracing utilites. It takes no
929 * locks and can be called from any context.
930 */
931void ftrace_off_permanent(void)
932{
933 tracing_disabled = 1;
934 ftrace_stop();
935 tracing_off_permanent();
936}
937
938/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500939 * tracing_start - quick start of the tracer
940 *
941 * If tracing is enabled but was stopped by tracing_stop,
942 * this will start the tracer back up.
943 */
944void tracing_start(void)
945{
946 struct ring_buffer *buffer;
947 unsigned long flags;
948
949 if (tracing_disabled)
950 return;
951
952 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500953 if (--trace_stop_count) {
954 if (trace_stop_count < 0) {
955 /* Someone screwed up their debugging */
956 WARN_ON_ONCE(1);
957 trace_stop_count = 0;
958 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500959 goto out;
960 }
961
Steven Rostedta2f80712010-03-12 19:56:00 -0500962 /* Prevent the buffers from switching */
963 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -0500964
965 buffer = global_trace.buffer;
966 if (buffer)
967 ring_buffer_record_enable(buffer);
968
969 buffer = max_tr.buffer;
970 if (buffer)
971 ring_buffer_record_enable(buffer);
972
Steven Rostedta2f80712010-03-12 19:56:00 -0500973 arch_spin_unlock(&ftrace_max_lock);
974
Steven Rostedt0f048702008-11-05 16:05:44 -0500975 ftrace_start();
976 out:
977 spin_unlock_irqrestore(&tracing_start_lock, flags);
978}
979
980/**
981 * tracing_stop - quick stop of the tracer
982 *
983 * Light weight way to stop tracing. Use in conjunction with
984 * tracing_start.
985 */
986void tracing_stop(void)
987{
988 struct ring_buffer *buffer;
989 unsigned long flags;
990
991 ftrace_stop();
992 spin_lock_irqsave(&tracing_start_lock, flags);
993 if (trace_stop_count++)
994 goto out;
995
Steven Rostedta2f80712010-03-12 19:56:00 -0500996 /* Prevent the buffers from switching */
997 arch_spin_lock(&ftrace_max_lock);
998
Steven Rostedt0f048702008-11-05 16:05:44 -0500999 buffer = global_trace.buffer;
1000 if (buffer)
1001 ring_buffer_record_disable(buffer);
1002
1003 buffer = max_tr.buffer;
1004 if (buffer)
1005 ring_buffer_record_disable(buffer);
1006
Steven Rostedta2f80712010-03-12 19:56:00 -05001007 arch_spin_unlock(&ftrace_max_lock);
1008
Steven Rostedt0f048702008-11-05 16:05:44 -05001009 out:
1010 spin_unlock_irqrestore(&tracing_start_lock, flags);
1011}
1012
Ingo Molnare309b412008-05-12 21:20:51 +02001013void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001014
Ingo Molnare309b412008-05-12 21:20:51 +02001015static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016{
Carsten Emdea635cf02009-03-18 09:00:41 +01001017 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001018
1019 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1020 return;
1021
1022 /*
1023 * It's not the end of the world if we don't get
1024 * the lock, but we also don't want to spin
1025 * nor do we want to disable interrupts,
1026 * so if we miss here, then better luck next time.
1027 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001028 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001029 return;
1030
1031 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001032 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001033 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1034
Carsten Emdea635cf02009-03-18 09:00:41 +01001035 /*
1036 * Check whether the cmdline buffer at idx has a pid
1037 * mapped. We are going to overwrite that entry so we
1038 * need to clear the map_pid_to_cmdline. Otherwise we
1039 * would read the new comm for the old pid.
1040 */
1041 pid = map_cmdline_to_pid[idx];
1042 if (pid != NO_CMDLINE_MAP)
1043 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044
Carsten Emdea635cf02009-03-18 09:00:41 +01001045 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001046 map_pid_to_cmdline[tsk->pid] = idx;
1047
1048 cmdline_idx = idx;
1049 }
1050
1051 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1052
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001053 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001054}
1055
Steven Rostedt4ca53082009-03-16 19:20:15 -04001056void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001057{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001058 unsigned map;
1059
Steven Rostedt4ca53082009-03-16 19:20:15 -04001060 if (!pid) {
1061 strcpy(comm, "<idle>");
1062 return;
1063 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001064
Steven Rostedt74bf4072010-01-25 15:11:53 -05001065 if (WARN_ON_ONCE(pid < 0)) {
1066 strcpy(comm, "<XXX>");
1067 return;
1068 }
1069
Steven Rostedt4ca53082009-03-16 19:20:15 -04001070 if (pid > PID_MAX_DEFAULT) {
1071 strcpy(comm, "<...>");
1072 return;
1073 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001074
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001075 preempt_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001076 arch_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001077 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001078 if (map != NO_CMDLINE_MAP)
1079 strcpy(comm, saved_cmdlines[map]);
1080 else
1081 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001082
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001083 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001084 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001085}
1086
Ingo Molnare309b412008-05-12 21:20:51 +02001087void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001088{
Thomas Gleixner18aecd32009-03-18 08:56:58 +01001089 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1090 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001091 return;
1092
1093 trace_save_cmdline(tsk);
1094}
1095
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001096void
Steven Rostedt38697052008-10-01 13:14:09 -04001097tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1098 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099{
1100 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001101
Steven Rostedt777e2082008-09-29 23:02:42 -04001102 entry->preempt_count = pc & 0xff;
1103 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedt637e7e82009-09-11 13:55:35 -04001104 entry->lock_depth = (tsk) ? tsk->lock_depth : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -04001105 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001106#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001107 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001108#else
1109 TRACE_FLAG_IRQS_NOSUPPORT |
1110#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001111 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1112 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1113 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1114}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001115EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001116
Steven Rostedte77405a2009-09-02 14:17:06 -04001117struct ring_buffer_event *
1118trace_buffer_lock_reserve(struct ring_buffer *buffer,
1119 int type,
1120 unsigned long len,
1121 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001122{
1123 struct ring_buffer_event *event;
1124
Steven Rostedte77405a2009-09-02 14:17:06 -04001125 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001126 if (event != NULL) {
1127 struct trace_entry *ent = ring_buffer_event_data(event);
1128
1129 tracing_generic_entry_update(ent, flags, pc);
1130 ent->type = type;
1131 }
1132
1133 return event;
1134}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001135
Steven Rostedte77405a2009-09-02 14:17:06 -04001136static inline void
1137__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1138 struct ring_buffer_event *event,
1139 unsigned long flags, int pc,
1140 int wake)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001141{
Steven Rostedte77405a2009-09-02 14:17:06 -04001142 ring_buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001143
Steven Rostedte77405a2009-09-02 14:17:06 -04001144 ftrace_trace_stack(buffer, flags, 6, pc);
1145 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001146
1147 if (wake)
1148 trace_wake_up();
1149}
1150
Steven Rostedte77405a2009-09-02 14:17:06 -04001151void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1152 struct ring_buffer_event *event,
1153 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001154{
Steven Rostedte77405a2009-09-02 14:17:06 -04001155 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001156}
1157
Steven Rostedtef5580d2009-02-27 19:38:04 -05001158struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001159trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1160 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001161 unsigned long flags, int pc)
1162{
Steven Rostedte77405a2009-09-02 14:17:06 -04001163 *current_rb = global_trace.buffer;
1164 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001165 type, len, flags, pc);
1166}
Steven Rostedt94487d62009-05-05 19:22:53 -04001167EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001168
Steven Rostedte77405a2009-09-02 14:17:06 -04001169void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1170 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001171 unsigned long flags, int pc)
1172{
Steven Rostedte77405a2009-09-02 14:17:06 -04001173 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001174}
Steven Rostedt94487d62009-05-05 19:22:53 -04001175EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001176
Steven Rostedte77405a2009-09-02 14:17:06 -04001177void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1178 struct ring_buffer_event *event,
1179 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001180{
Steven Rostedte77405a2009-09-02 14:17:06 -04001181 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001182}
Steven Rostedt94487d62009-05-05 19:22:53 -04001183EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
Steven Rostedt77d9f462009-04-02 01:16:59 -04001184
Steven Rostedte77405a2009-09-02 14:17:06 -04001185void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1186 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001187{
Steven Rostedte77405a2009-09-02 14:17:06 -04001188 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001189}
Steven Rostedt12acd472009-04-17 16:01:56 -04001190EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001191
Ingo Molnare309b412008-05-12 21:20:51 +02001192void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001193trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001194 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1195 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001196{
Tom Zanussie1112b42009-03-31 00:48:49 -05001197 struct ftrace_event_call *call = &event_function;
Steven Rostedte77405a2009-09-02 14:17:06 -04001198 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001199 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001200 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001201
Steven Rostedtd7690412008-10-01 00:29:53 -04001202 /* If we are reading the ring buffer, don't trace */
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001203 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04001204 return;
1205
Steven Rostedte77405a2009-09-02 14:17:06 -04001206 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001207 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001208 if (!event)
1209 return;
1210 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001211 entry->ip = ip;
1212 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001213
Steven Rostedte77405a2009-09-02 14:17:06 -04001214 if (!filter_check_discard(call, entry, buffer, event))
1215 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001216}
1217
Ingo Molnare309b412008-05-12 21:20:51 +02001218void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001219ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -04001220 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1221 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001222{
1223 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001224 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001225}
1226
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001227#ifdef CONFIG_STACKTRACE
Steven Rostedte77405a2009-09-02 14:17:06 -04001228static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001229 unsigned long flags,
1230 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +02001231{
Tom Zanussie1112b42009-03-31 00:48:49 -05001232 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001233 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001234 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001235 struct stack_trace trace;
1236
Steven Rostedte77405a2009-09-02 14:17:06 -04001237 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001238 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001239 if (!event)
1240 return;
1241 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001242 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +02001243
1244 trace.nr_entries = 0;
1245 trace.max_entries = FTRACE_STACK_ENTRIES;
1246 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -04001247 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +02001248
1249 save_stack_trace(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001250 if (!filter_check_discard(call, entry, buffer, event))
1251 ring_buffer_unlock_commit(buffer, event);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001252}
1253
Steven Rostedte77405a2009-09-02 14:17:06 -04001254void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1255 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001256{
1257 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1258 return;
1259
Steven Rostedte77405a2009-09-02 14:17:06 -04001260 __ftrace_trace_stack(buffer, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -05001261}
1262
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001263void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1264 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001265{
Steven Rostedte77405a2009-09-02 14:17:06 -04001266 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -04001267}
1268
Steven Rostedt03889382009-12-11 09:48:22 -05001269/**
1270 * trace_dump_stack - record a stack back trace in the trace buffer
1271 */
1272void trace_dump_stack(void)
1273{
1274 unsigned long flags;
1275
1276 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001277 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001278
1279 local_save_flags(flags);
1280
1281 /* skipping 3 traces, seems to get us at the caller of this function */
1282 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1283}
1284
Steven Rostedt91e86e52010-11-10 12:56:12 +01001285static DEFINE_PER_CPU(int, user_stack_count);
1286
Steven Rostedte77405a2009-09-02 14:17:06 -04001287void
1288ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001289{
Tom Zanussie1112b42009-03-31 00:48:49 -05001290 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001291 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001292 struct userstack_entry *entry;
1293 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001294
1295 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1296 return;
1297
Steven Rostedtb6345872010-03-12 20:03:30 -05001298 /*
1299 * NMIs can not handle page faults, even with fix ups.
1300 * The save user stack can (and often does) fault.
1301 */
1302 if (unlikely(in_nmi()))
1303 return;
1304
Steven Rostedt91e86e52010-11-10 12:56:12 +01001305 /*
1306 * prevent recursion, since the user stack tracing may
1307 * trigger other kernel events.
1308 */
1309 preempt_disable();
1310 if (__this_cpu_read(user_stack_count))
1311 goto out;
1312
1313 __this_cpu_inc(user_stack_count);
1314
Steven Rostedte77405a2009-09-02 14:17:06 -04001315 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001316 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001317 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08001318 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02001319 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001320
Steven Rostedt48659d32009-09-11 11:36:23 -04001321 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001322 memset(&entry->caller, 0, sizeof(entry->caller));
1323
1324 trace.nr_entries = 0;
1325 trace.max_entries = FTRACE_STACK_ENTRIES;
1326 trace.skip = 0;
1327 trace.entries = entry->caller;
1328
1329 save_stack_trace_user(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001330 if (!filter_check_discard(call, entry, buffer, event))
1331 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001332
Li Zefan1dbd1952010-12-09 15:47:56 +08001333 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01001334 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001335 out:
1336 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02001337}
1338
Hannes Eder4fd27352009-02-10 19:44:12 +01001339#ifdef UNUSED
1340static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001341{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001342 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001343}
Hannes Eder4fd27352009-02-10 19:44:12 +01001344#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001345
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001346#endif /* CONFIG_STACKTRACE */
1347
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001348/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001349 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001350 *
1351 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001352int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001353{
Thomas Gleixner445c8952009-12-02 19:49:50 +01001354 static arch_spinlock_t trace_buf_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001355 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001356 static u32 trace_buf[TRACE_BUF_SIZE];
1357
Tom Zanussie1112b42009-03-31 00:48:49 -05001358 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001359 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001360 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001361 struct trace_array *tr = &global_trace;
1362 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001363 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001364 unsigned long flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001365 int disable;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001366 int cpu, len = 0, size, pc;
1367
1368 if (unlikely(tracing_selftest_running || tracing_disabled))
1369 return 0;
1370
1371 /* Don't pollute graph traces with trace_vprintk internals */
1372 pause_graph_tracing();
1373
1374 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04001375 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001376 cpu = raw_smp_processor_id();
1377 data = tr->data[cpu];
1378
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001379 disable = atomic_inc_return(&data->disabled);
1380 if (unlikely(disable != 1))
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001381 goto out;
1382
Steven Rostedt80370cb2009-03-10 17:16:35 -04001383 /* Lockdep uses trace_printk for lock tracing */
1384 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001385 arch_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001386 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1387
1388 if (len > TRACE_BUF_SIZE || len < 0)
1389 goto out_unlock;
1390
1391 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedte77405a2009-09-02 14:17:06 -04001392 buffer = tr->buffer;
1393 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1394 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001395 if (!event)
1396 goto out_unlock;
1397 entry = ring_buffer_event_data(event);
1398 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001399 entry->fmt = fmt;
1400
1401 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
Steven Rostedtd9313692010-01-06 17:27:11 -05001402 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001403 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001404 ftrace_trace_stack(buffer, flags, 6, pc);
1405 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001406
1407out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001408 arch_spin_unlock(&trace_buf_lock);
Steven Rostedt80370cb2009-03-10 17:16:35 -04001409 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001410
1411out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001412 atomic_dec_return(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -04001413 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001414 unpause_graph_tracing();
1415
1416 return len;
1417}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001418EXPORT_SYMBOL_GPL(trace_vbprintk);
1419
Steven Rostedt659372d2009-09-03 19:11:07 -04001420int trace_array_printk(struct trace_array *tr,
1421 unsigned long ip, const char *fmt, ...)
1422{
1423 int ret;
1424 va_list ap;
1425
1426 if (!(trace_flags & TRACE_ITER_PRINTK))
1427 return 0;
1428
1429 va_start(ap, fmt);
1430 ret = trace_array_vprintk(tr, ip, fmt, ap);
1431 va_end(ap);
1432 return ret;
1433}
1434
1435int trace_array_vprintk(struct trace_array *tr,
1436 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001437{
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001438 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001439 static char trace_buf[TRACE_BUF_SIZE];
1440
Tom Zanussie1112b42009-03-31 00:48:49 -05001441 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001442 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001443 struct ring_buffer *buffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001444 struct trace_array_cpu *data;
1445 int cpu, len = 0, size, pc;
1446 struct print_entry *entry;
1447 unsigned long irq_flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001448 int disable;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001449
1450 if (tracing_disabled || tracing_selftest_running)
1451 return 0;
1452
1453 pc = preempt_count();
1454 preempt_disable_notrace();
1455 cpu = raw_smp_processor_id();
1456 data = tr->data[cpu];
1457
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001458 disable = atomic_inc_return(&data->disabled);
1459 if (unlikely(disable != 1))
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001460 goto out;
1461
1462 pause_graph_tracing();
1463 raw_local_irq_save(irq_flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001464 arch_spin_lock(&trace_buf_lock);
Carsten Emdef2942482009-12-06 14:02:44 +01001465 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001466
1467 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04001468 buffer = tr->buffer;
1469 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1470 irq_flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001471 if (!event)
1472 goto out_unlock;
1473 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001474 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001475
1476 memcpy(&entry->buf, trace_buf, len);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001477 entry->buf[len] = '\0';
Steven Rostedtd9313692010-01-06 17:27:11 -05001478 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001479 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001480 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1481 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001482
1483 out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001484 arch_spin_unlock(&trace_buf_lock);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001485 raw_local_irq_restore(irq_flags);
1486 unpause_graph_tracing();
1487 out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001488 atomic_dec_return(&data->disabled);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001489 preempt_enable_notrace();
1490
1491 return len;
1492}
Steven Rostedt659372d2009-09-03 19:11:07 -04001493
1494int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1495{
Steven Rostedta813a152009-10-09 01:41:35 -04001496 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04001497}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001498EXPORT_SYMBOL_GPL(trace_vprintk);
1499
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001500static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001501{
Steven Rostedtd7690412008-10-01 00:29:53 -04001502 /* Don't allow ftrace to trace into the ring buffers */
1503 ftrace_disable_cpu();
1504
Steven Rostedt5a90f572008-09-03 17:42:51 -04001505 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001506 if (iter->buffer_iter[iter->cpu])
1507 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1508
1509 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001510}
1511
Ingo Molnare309b412008-05-12 21:20:51 +02001512static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001513peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1514 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001515{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001516 struct ring_buffer_event *event;
1517 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001518
Steven Rostedtd7690412008-10-01 00:29:53 -04001519 /* Don't allow ftrace to trace into the ring buffers */
1520 ftrace_disable_cpu();
1521
1522 if (buf_iter)
1523 event = ring_buffer_iter_peek(buf_iter, ts);
1524 else
Steven Rostedtbc21b472010-03-31 19:49:26 -04001525 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1526 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001527
1528 ftrace_enable_cpu();
1529
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001530 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001531}
Steven Rostedtd7690412008-10-01 00:29:53 -04001532
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001533static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001534__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1535 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001536{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001537 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001538 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08001539 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001540 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001541 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542 int next_cpu = -1;
1543 int cpu;
1544
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001545 /*
1546 * If we are in a per_cpu trace file, don't bother by iterating over
1547 * all cpu and peek directly.
1548 */
1549 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1550 if (ring_buffer_empty_cpu(buffer, cpu_file))
1551 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001552 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001553 if (ent_cpu)
1554 *ent_cpu = cpu_file;
1555
1556 return ent;
1557 }
1558
Steven Rostedtab464282008-05-12 21:21:00 +02001559 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001560
1561 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001562 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001563
Steven Rostedtbc21b472010-03-31 19:49:26 -04001564 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001565
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001566 /*
1567 * Pick the entry with the smallest timestamp:
1568 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001569 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001570 next = ent;
1571 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001572 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001573 next_lost = lost_events;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001574 }
1575 }
1576
1577 if (ent_cpu)
1578 *ent_cpu = next_cpu;
1579
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001580 if (ent_ts)
1581 *ent_ts = next_ts;
1582
Steven Rostedtbc21b472010-03-31 19:49:26 -04001583 if (missing_events)
1584 *missing_events = next_lost;
1585
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001586 return next;
1587}
1588
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001589/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001590struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1591 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001592{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001593 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001594}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001595
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001596/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05001597void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001598{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001599 iter->ent = __find_next_entry(iter, &iter->cpu,
1600 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001601
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001602 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001603 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001604
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001605 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001606}
1607
Ingo Molnare309b412008-05-12 21:20:51 +02001608static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001609{
Steven Rostedtd7690412008-10-01 00:29:53 -04001610 /* Don't allow ftrace to trace into the ring buffers */
1611 ftrace_disable_cpu();
Steven Rostedtbc21b472010-03-31 19:49:26 -04001612 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1613 &iter->lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001614 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001615}
1616
Ingo Molnare309b412008-05-12 21:20:51 +02001617static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001618{
1619 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001620 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001621 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001622
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001623 WARN_ON_ONCE(iter->leftover);
1624
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001625 (*pos)++;
1626
1627 /* can't go backwards */
1628 if (iter->idx > i)
1629 return NULL;
1630
1631 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05001632 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001633 else
1634 ent = iter;
1635
1636 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05001637 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001638
1639 iter->pos = *pos;
1640
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001641 return ent;
1642}
1643
Jason Wessel955b61e2010-08-05 09:22:23 -05001644void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001645{
1646 struct trace_array *tr = iter->tr;
1647 struct ring_buffer_event *event;
1648 struct ring_buffer_iter *buf_iter;
1649 unsigned long entries = 0;
1650 u64 ts;
1651
1652 tr->data[cpu]->skipped_entries = 0;
1653
1654 if (!iter->buffer_iter[cpu])
1655 return;
1656
1657 buf_iter = iter->buffer_iter[cpu];
1658 ring_buffer_iter_reset(buf_iter);
1659
1660 /*
1661 * We could have the case with the max latency tracers
1662 * that a reset never took place on a cpu. This is evident
1663 * by the timestamp being before the start of the buffer.
1664 */
1665 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1666 if (ts >= iter->tr->time_start)
1667 break;
1668 entries++;
1669 ring_buffer_read(buf_iter, NULL);
1670 }
1671
1672 tr->data[cpu]->skipped_entries = entries;
1673}
1674
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001675/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001676 * The current tracer is copied to avoid a global locking
1677 * all around.
1678 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001679static void *s_start(struct seq_file *m, loff_t *pos)
1680{
1681 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001682 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001683 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001684 void *p = NULL;
1685 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001686 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001687
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001688 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001689 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001690 if (unlikely(old_tracer != current_trace && current_trace)) {
1691 old_tracer = current_trace;
1692 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001693 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001694 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001695
1696 atomic_inc(&trace_record_cmdline_disabled);
1697
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001698 if (*pos != iter->pos) {
1699 iter->ent = NULL;
1700 iter->cpu = 0;
1701 iter->idx = -1;
1702
Steven Rostedtd7690412008-10-01 00:29:53 -04001703 ftrace_disable_cpu();
1704
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001705 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1706 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001707 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001708 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001709 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001710
Steven Rostedtd7690412008-10-01 00:29:53 -04001711 ftrace_enable_cpu();
1712
Lai Jiangshanac91d852010-03-02 17:54:50 +08001713 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001714 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1715 ;
1716
1717 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001718 /*
1719 * If we overflowed the seq_file before, then we want
1720 * to just reuse the trace_seq buffer again.
1721 */
1722 if (iter->leftover)
1723 p = iter;
1724 else {
1725 l = *pos - 1;
1726 p = s_next(m, p, &l);
1727 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001728 }
1729
Lai Jiangshan4f535962009-05-18 19:35:34 +08001730 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001731 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732 return p;
1733}
1734
1735static void s_stop(struct seq_file *m, void *p)
1736{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001737 struct trace_iterator *iter = m->private;
1738
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001739 atomic_dec(&trace_record_cmdline_disabled);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001740 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08001741 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001742}
1743
Ingo Molnare309b412008-05-12 21:20:51 +02001744static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001745{
Michael Ellermana6168352008-08-20 16:36:11 -07001746 seq_puts(m, "# _------=> CPU# \n");
1747 seq_puts(m, "# / _-----=> irqs-off \n");
1748 seq_puts(m, "# | / _----=> need-resched \n");
1749 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1750 seq_puts(m, "# ||| / _--=> preempt-depth \n");
Steven Rostedt637e7e82009-09-11 13:55:35 -04001751 seq_puts(m, "# |||| /_--=> lock-depth \n");
1752 seq_puts(m, "# |||||/ delay \n");
1753 seq_puts(m, "# cmd pid |||||| time | caller \n");
1754 seq_puts(m, "# \\ / |||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001755}
1756
Ingo Molnare309b412008-05-12 21:20:51 +02001757static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001758{
Michael Ellermana6168352008-08-20 16:36:11 -07001759 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1760 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001761}
1762
1763
Jiri Olsa62b915f2010-04-02 19:01:22 +02001764void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1766{
1767 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1768 struct trace_array *tr = iter->tr;
1769 struct trace_array_cpu *data = tr->data[tr->cpu];
1770 struct tracer *type = current_trace;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001771 unsigned long entries = 0;
1772 unsigned long total = 0;
1773 unsigned long count;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001774 const char *name = "preemption";
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001775 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001776
1777 if (type)
1778 name = type->name;
1779
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001780
1781 for_each_tracing_cpu(cpu) {
1782 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1783 /*
1784 * If this buffer has skipped entries, then we hold all
1785 * entries for the trace and we need to ignore the
1786 * ones before the time stamp.
1787 */
1788 if (tr->data[cpu]->skipped_entries) {
1789 count -= tr->data[cpu]->skipped_entries;
1790 /* total is the same as the entries */
1791 total += count;
1792 } else
1793 total += count +
1794 ring_buffer_overrun_cpu(tr->buffer, cpu);
1795 entries += count;
1796 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001797
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001798 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001799 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001800 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001801 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001802 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001803 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001804 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001805 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001806 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001807 tr->cpu,
1808#if defined(CONFIG_PREEMPT_NONE)
1809 "server",
1810#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1811 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001812#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001813 "preempt",
1814#else
1815 "unknown",
1816#endif
1817 /* These are reserved for later use */
1818 0, 0, 0, 0);
1819#ifdef CONFIG_SMP
1820 seq_printf(m, " #P:%d)\n", num_online_cpus());
1821#else
1822 seq_puts(m, ")\n");
1823#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001824 seq_puts(m, "# -----------------\n");
1825 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001826 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1827 data->comm, data->pid, data->uid, data->nice,
1828 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001829 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001830
1831 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001832 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001833 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1834 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001835 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001836 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1837 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001838 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001839 }
1840
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001841 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001842}
1843
Steven Rostedta3097202008-11-07 22:36:02 -05001844static void test_cpu_buff_start(struct trace_iterator *iter)
1845{
1846 struct trace_seq *s = &iter->seq;
1847
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001848 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1849 return;
1850
1851 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1852 return;
1853
Rusty Russell44623442009-01-01 10:12:23 +10301854 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001855 return;
1856
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001857 if (iter->tr->data[iter->cpu]->skipped_entries)
1858 return;
1859
Rusty Russell44623442009-01-01 10:12:23 +10301860 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001861
1862 /* Don't print started cpu buffer for the first entry of the trace */
1863 if (iter->idx > 1)
1864 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1865 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05001866}
1867
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001868static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001869{
Steven Rostedt214023c2008-05-12 21:20:46 +02001870 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001871 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001872 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001873 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001874
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001875 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001876
Steven Rostedta3097202008-11-07 22:36:02 -05001877 test_cpu_buff_start(iter);
1878
Steven Rostedtf633cef2008-12-23 23:24:13 -05001879 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001880
1881 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001882 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1883 if (!trace_print_lat_context(iter))
1884 goto partial;
1885 } else {
1886 if (!trace_print_context(iter))
1887 goto partial;
1888 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001889 }
1890
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001891 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04001892 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001893
1894 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1895 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001896
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001897 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001898partial:
1899 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001900}
1901
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001902static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001903{
1904 struct trace_seq *s = &iter->seq;
1905 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001906 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001907
1908 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001909
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001910 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001911 if (!trace_seq_printf(s, "%d %d %llu ",
1912 entry->pid, iter->cpu, iter->ts))
1913 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001914 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001915
Steven Rostedtf633cef2008-12-23 23:24:13 -05001916 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001917 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04001918 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001919
1920 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1921 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001922
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001923 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001924partial:
1925 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001926}
1927
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001928static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001929{
1930 struct trace_seq *s = &iter->seq;
1931 unsigned char newline = '\n';
1932 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001933 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001934
1935 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001936
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001937 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1938 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1939 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1940 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1941 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001942
Steven Rostedtf633cef2008-12-23 23:24:13 -05001943 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001944 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04001945 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001946 if (ret != TRACE_TYPE_HANDLED)
1947 return ret;
1948 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001949
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001950 SEQ_PUT_FIELD_RET(s, newline);
1951
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001952 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001953}
1954
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001955static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001956{
1957 struct trace_seq *s = &iter->seq;
1958 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001959 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001960
1961 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001962
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001963 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1964 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001965 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001966 SEQ_PUT_FIELD_RET(s, iter->ts);
1967 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001968
Steven Rostedtf633cef2008-12-23 23:24:13 -05001969 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04001970 return event ? event->funcs->binary(iter, 0, event) :
1971 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001972}
1973
Jiri Olsa62b915f2010-04-02 19:01:22 +02001974int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001975{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001976 int cpu;
1977
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001978 /* If we are looking at one CPU buffer, only check that one */
1979 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1980 cpu = iter->cpu_file;
1981 if (iter->buffer_iter[cpu]) {
1982 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1983 return 0;
1984 } else {
1985 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1986 return 0;
1987 }
1988 return 1;
1989 }
1990
Steven Rostedtab464282008-05-12 21:21:00 +02001991 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001992 if (iter->buffer_iter[cpu]) {
1993 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1994 return 0;
1995 } else {
1996 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1997 return 0;
1998 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001999 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002000
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002001 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002002}
2003
Lai Jiangshan4f535962009-05-18 19:35:34 +08002004/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05002005enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002006{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002007 enum print_line_t ret;
2008
Steven Rostedtbc21b472010-03-31 19:49:26 -04002009 if (iter->lost_events)
2010 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2011 iter->cpu, iter->lost_events);
2012
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002013 if (iter->trace && iter->trace->print_line) {
2014 ret = iter->trace->print_line(iter);
2015 if (ret != TRACE_TYPE_UNHANDLED)
2016 return ret;
2017 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002018
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002019 if (iter->ent->type == TRACE_BPRINT &&
2020 trace_flags & TRACE_ITER_PRINTK &&
2021 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002022 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002023
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002024 if (iter->ent->type == TRACE_PRINT &&
2025 trace_flags & TRACE_ITER_PRINTK &&
2026 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002027 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002028
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002029 if (trace_flags & TRACE_ITER_BIN)
2030 return print_bin_fmt(iter);
2031
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002032 if (trace_flags & TRACE_ITER_HEX)
2033 return print_hex_fmt(iter);
2034
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002035 if (trace_flags & TRACE_ITER_RAW)
2036 return print_raw_fmt(iter);
2037
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002038 return print_trace_fmt(iter);
2039}
2040
Jiri Olsa62b915f2010-04-02 19:01:22 +02002041void trace_default_header(struct seq_file *m)
2042{
2043 struct trace_iterator *iter = m->private;
2044
2045 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2046 /* print nothing if the buffers are empty */
2047 if (trace_empty(iter))
2048 return;
2049 print_trace_header(m, iter);
2050 if (!(trace_flags & TRACE_ITER_VERBOSE))
2051 print_lat_help_header(m);
2052 } else {
2053 if (!(trace_flags & TRACE_ITER_VERBOSE))
2054 print_func_help_header(m);
2055 }
2056}
2057
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002058static int s_show(struct seq_file *m, void *v)
2059{
2060 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002061 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062
2063 if (iter->ent == NULL) {
2064 if (iter->tr) {
2065 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2066 seq_puts(m, "#\n");
2067 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002068 if (iter->trace && iter->trace->print_header)
2069 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02002070 else
2071 trace_default_header(m);
2072
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002073 } else if (iter->leftover) {
2074 /*
2075 * If we filled the seq_file buffer earlier, we
2076 * want to just show it now.
2077 */
2078 ret = trace_print_seq(m, &iter->seq);
2079
2080 /* ret should this time be zero, but you never know */
2081 iter->leftover = ret;
2082
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002083 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002084 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002085 ret = trace_print_seq(m, &iter->seq);
2086 /*
2087 * If we overflow the seq_file buffer, then it will
2088 * ask us for this data again at start up.
2089 * Use that instead.
2090 * ret is 0 if seq_file write succeeded.
2091 * -1 otherwise.
2092 */
2093 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002094 }
2095
2096 return 0;
2097}
2098
James Morris88e9d342009-09-22 16:43:43 -07002099static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002100 .start = s_start,
2101 .next = s_next,
2102 .stop = s_stop,
2103 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002104};
2105
Ingo Molnare309b412008-05-12 21:20:51 +02002106static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002107__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002108{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002109 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002110 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002111 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002112 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002113 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002114
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002115 if (tracing_disabled)
2116 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02002117
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002118 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002119 if (!iter)
2120 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002121
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002122 /*
2123 * We make a copy of the current tracer to avoid concurrent
2124 * changes on it while we are reading.
2125 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002126 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002127 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002128 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002129 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002130
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002131 if (current_trace)
2132 *iter->trace = *current_trace;
2133
Li Zefan79f55992009-06-15 14:58:26 +08002134 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002135 goto fail;
2136
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002137 if (current_trace && current_trace->print_max)
2138 iter->tr = &max_tr;
2139 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002140 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002141 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002142 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002143 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002144
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002145 /* Notify the tracer early; before we stop tracing. */
2146 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01002147 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002148
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002149 /* Annotate start of buffers if we had overruns */
2150 if (ring_buffer_overruns(iter->tr->buffer))
2151 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2152
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002153 /* stop the trace while dumping */
2154 tracing_stop();
2155
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002156 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2157 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002158 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002159 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2160 }
2161 ring_buffer_read_prepare_sync();
2162 for_each_tracing_cpu(cpu) {
2163 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002164 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002165 }
2166 } else {
2167 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002168 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002169 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2170 ring_buffer_read_prepare_sync();
2171 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002172 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002173 }
2174
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002175 ret = seq_open(file, &tracer_seq_ops);
2176 if (ret < 0) {
2177 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002178 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002179 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002180
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002181 m = file->private_data;
2182 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002183
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002184 mutex_unlock(&trace_types_lock);
2185
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002186 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002187
2188 fail_buffer:
2189 for_each_tracing_cpu(cpu) {
2190 if (iter->buffer_iter[cpu])
2191 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2192 }
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002193 free_cpumask_var(iter->started);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002194 tracing_start();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002195 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002196 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002197 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01002198 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002199
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002200 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002201}
2202
2203int tracing_open_generic(struct inode *inode, struct file *filp)
2204{
Steven Rostedt60a11772008-05-12 21:20:44 +02002205 if (tracing_disabled)
2206 return -ENODEV;
2207
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002208 filp->private_data = inode->i_private;
2209 return 0;
2210}
2211
Hannes Eder4fd27352009-02-10 19:44:12 +01002212static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002213{
matt mooney907f2782010-09-27 19:04:53 -07002214 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002215 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002216 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002217
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002218 if (!(file->f_mode & FMODE_READ))
2219 return 0;
2220
2221 iter = m->private;
2222
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002223 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002224 for_each_tracing_cpu(cpu) {
2225 if (iter->buffer_iter[cpu])
2226 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2227 }
2228
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002229 if (iter->trace && iter->trace->close)
2230 iter->trace->close(iter);
2231
2232 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05002233 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002234 mutex_unlock(&trace_types_lock);
2235
2236 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002237 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002238 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002239 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002240 kfree(iter);
2241 return 0;
2242}
2243
2244static int tracing_open(struct inode *inode, struct file *file)
2245{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002246 struct trace_iterator *iter;
2247 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002248
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002249 /* If this file was open for write, then erase contents */
2250 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002251 (file->f_flags & O_TRUNC)) {
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002252 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002253
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002254 if (cpu == TRACE_PIPE_ALL_CPU)
2255 tracing_reset_online_cpus(&global_trace);
2256 else
2257 tracing_reset(&global_trace, cpu);
2258 }
2259
2260 if (file->f_mode & FMODE_READ) {
2261 iter = __tracing_open(inode, file);
2262 if (IS_ERR(iter))
2263 ret = PTR_ERR(iter);
2264 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2265 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2266 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002267 return ret;
2268}
2269
Ingo Molnare309b412008-05-12 21:20:51 +02002270static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002271t_next(struct seq_file *m, void *v, loff_t *pos)
2272{
Li Zefanf129e962009-06-24 09:53:44 +08002273 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002274
2275 (*pos)++;
2276
2277 if (t)
2278 t = t->next;
2279
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002280 return t;
2281}
2282
2283static void *t_start(struct seq_file *m, loff_t *pos)
2284{
Li Zefanf129e962009-06-24 09:53:44 +08002285 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002286 loff_t l = 0;
2287
2288 mutex_lock(&trace_types_lock);
Li Zefanf129e962009-06-24 09:53:44 +08002289 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002290 ;
2291
2292 return t;
2293}
2294
2295static void t_stop(struct seq_file *m, void *p)
2296{
2297 mutex_unlock(&trace_types_lock);
2298}
2299
2300static int t_show(struct seq_file *m, void *v)
2301{
2302 struct tracer *t = v;
2303
2304 if (!t)
2305 return 0;
2306
2307 seq_printf(m, "%s", t->name);
2308 if (t->next)
2309 seq_putc(m, ' ');
2310 else
2311 seq_putc(m, '\n');
2312
2313 return 0;
2314}
2315
James Morris88e9d342009-09-22 16:43:43 -07002316static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002317 .start = t_start,
2318 .next = t_next,
2319 .stop = t_stop,
2320 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002321};
2322
2323static int show_traces_open(struct inode *inode, struct file *file)
2324{
Steven Rostedt60a11772008-05-12 21:20:44 +02002325 if (tracing_disabled)
2326 return -ENODEV;
2327
Li Zefanf129e962009-06-24 09:53:44 +08002328 return seq_open(file, &show_traces_seq_ops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002329}
2330
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002331static ssize_t
2332tracing_write_stub(struct file *filp, const char __user *ubuf,
2333 size_t count, loff_t *ppos)
2334{
2335 return count;
2336}
2337
Slava Pestov364829b2010-11-24 15:13:16 -08002338static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2339{
2340 if (file->f_mode & FMODE_READ)
2341 return seq_lseek(file, offset, origin);
2342 else
2343 return 0;
2344}
2345
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002346static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002347 .open = tracing_open,
2348 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002349 .write = tracing_write_stub,
Slava Pestov364829b2010-11-24 15:13:16 -08002350 .llseek = tracing_seek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002351 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002352};
2353
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002354static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002355 .open = show_traces_open,
2356 .read = seq_read,
2357 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002358 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02002359};
2360
Ingo Molnar36dfe922008-05-12 21:20:52 +02002361/*
2362 * Only trace on a CPU if the bitmask is set:
2363 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302364static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002365
2366/*
2367 * The tracer itself will not take this lock, but still we want
2368 * to provide a consistent cpumask to user-space:
2369 */
2370static DEFINE_MUTEX(tracing_cpumask_update_lock);
2371
2372/*
2373 * Temporary storage for the character representation of the
2374 * CPU bitmask (and one more byte for the newline):
2375 */
2376static char mask_str[NR_CPUS + 1];
2377
Ingo Molnarc7078de2008-05-12 21:20:52 +02002378static ssize_t
2379tracing_cpumask_read(struct file *filp, char __user *ubuf,
2380 size_t count, loff_t *ppos)
2381{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002382 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002383
2384 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002385
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302386 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002387 if (count - len < 2) {
2388 count = -EINVAL;
2389 goto out_err;
2390 }
2391 len += sprintf(mask_str + len, "\n");
2392 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2393
2394out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002395 mutex_unlock(&tracing_cpumask_update_lock);
2396
2397 return count;
2398}
2399
2400static ssize_t
2401tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2402 size_t count, loff_t *ppos)
2403{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002404 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302405 cpumask_var_t tracing_cpumask_new;
2406
2407 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2408 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002409
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302410 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002411 if (err)
2412 goto err_unlock;
2413
Li Zefan215368e2009-06-15 10:56:42 +08002414 mutex_lock(&tracing_cpumask_update_lock);
2415
Steven Rostedta5e25882008-12-02 15:34:05 -05002416 local_irq_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002417 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002418 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002419 /*
2420 * Increase/decrease the disabled counter if we are
2421 * about to flip a bit in the cpumask:
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_inc(&global_trace.data[cpu]->disabled);
2426 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302427 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2428 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002429 atomic_dec(&global_trace.data[cpu]->disabled);
2430 }
2431 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002432 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002433 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002434
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302435 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002436
Ingo Molnarc7078de2008-05-12 21:20:52 +02002437 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302438 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002439
Ingo Molnarc7078de2008-05-12 21:20:52 +02002440 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002441
2442err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08002443 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002444
2445 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002446}
2447
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002448static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002449 .open = tracing_open_generic,
2450 .read = tracing_cpumask_read,
2451 .write = tracing_cpumask_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002452 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002453};
2454
Li Zefanfdb372e2009-12-08 11:15:59 +08002455static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002456{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002457 struct tracer_opt *trace_opts;
2458 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002459 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002460
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002461 mutex_lock(&trace_types_lock);
2462 tracer_flags = current_trace->flags->val;
2463 trace_opts = current_trace->flags->opts;
2464
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002465 for (i = 0; trace_options[i]; i++) {
2466 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08002467 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002468 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002469 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002470 }
2471
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002472 for (i = 0; trace_opts[i].name; i++) {
2473 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08002474 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002475 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002476 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002477 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002478 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002479
Li Zefanfdb372e2009-12-08 11:15:59 +08002480 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002481}
2482
Li Zefan8d18eaa2009-12-08 11:17:06 +08002483static int __set_tracer_option(struct tracer *trace,
2484 struct tracer_flags *tracer_flags,
2485 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002486{
Li Zefan8d18eaa2009-12-08 11:17:06 +08002487 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002488
Li Zefan8d18eaa2009-12-08 11:17:06 +08002489 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002490 if (ret)
2491 return ret;
2492
2493 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08002494 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002495 else
Zhaolei77708412009-08-07 18:53:21 +08002496 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002497 return 0;
2498}
2499
Li Zefan8d18eaa2009-12-08 11:17:06 +08002500/* Try to assign a tracer specific option */
2501static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2502{
2503 struct tracer_flags *tracer_flags = trace->flags;
2504 struct tracer_opt *opts = NULL;
2505 int i;
2506
2507 for (i = 0; tracer_flags->opts[i].name; i++) {
2508 opts = &tracer_flags->opts[i];
2509
2510 if (strcmp(cmp, opts->name) == 0)
2511 return __set_tracer_option(trace, trace->flags,
2512 opts, neg);
2513 }
2514
2515 return -EINVAL;
2516}
2517
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002518static void set_tracer_flags(unsigned int mask, int enabled)
2519{
2520 /* do nothing if flag is already set */
2521 if (!!(trace_flags & mask) == !!enabled)
2522 return;
2523
2524 if (enabled)
2525 trace_flags |= mask;
2526 else
2527 trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08002528
2529 if (mask == TRACE_ITER_RECORD_CMD)
2530 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08002531
2532 if (mask == TRACE_ITER_OVERWRITE)
2533 ring_buffer_change_overwrite(global_trace.buffer, enabled);
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002534}
2535
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002536static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002537tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002538 size_t cnt, loff_t *ppos)
2539{
2540 char buf[64];
Li Zefan8d18eaa2009-12-08 11:17:06 +08002541 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002542 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002543 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002544 int i;
2545
Steven Rostedtcffae432008-05-12 21:21:00 +02002546 if (cnt >= sizeof(buf))
2547 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002548
2549 if (copy_from_user(&buf, ubuf, cnt))
2550 return -EFAULT;
2551
2552 buf[cnt] = 0;
Li Zefan8d18eaa2009-12-08 11:17:06 +08002553 cmp = strstrip(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002554
Li Zefan8d18eaa2009-12-08 11:17:06 +08002555 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002556 neg = 1;
2557 cmp += 2;
2558 }
2559
2560 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08002561 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002562 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002563 break;
2564 }
2565 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002566
2567 /* If no option could be set, test the specific tracer options */
2568 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002569 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002570 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002571 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002572 if (ret)
2573 return ret;
2574 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002575
Jiri Olsacf8517c2009-10-23 19:36:16 -04002576 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002577
2578 return cnt;
2579}
2580
Li Zefanfdb372e2009-12-08 11:15:59 +08002581static int tracing_trace_options_open(struct inode *inode, struct file *file)
2582{
2583 if (tracing_disabled)
2584 return -ENODEV;
2585 return single_open(file, tracing_trace_options_show, NULL);
2586}
2587
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002588static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08002589 .open = tracing_trace_options_open,
2590 .read = seq_read,
2591 .llseek = seq_lseek,
2592 .release = single_release,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002593 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002594};
2595
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002596static const char readme_msg[] =
2597 "tracing mini-HOWTO:\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002598 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2599 "# cat /sys/kernel/debug/tracing/available_tracers\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302600 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002601 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302602 "nop\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002603 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2604 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002605 "sched_switch\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002606 "# cat /sys/kernel/debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002607 "noprint-parent nosym-offset nosym-addr noverbose\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002608 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2609 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2610 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2611 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002612;
2613
2614static ssize_t
2615tracing_readme_read(struct file *filp, char __user *ubuf,
2616 size_t cnt, loff_t *ppos)
2617{
2618 return simple_read_from_buffer(ubuf, cnt, ppos,
2619 readme_msg, strlen(readme_msg));
2620}
2621
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002622static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002623 .open = tracing_open_generic,
2624 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002625 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002626};
2627
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002628static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04002629tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2630 size_t cnt, loff_t *ppos)
2631{
2632 char *buf_comm;
2633 char *file_buf;
2634 char *buf;
2635 int len = 0;
2636 int pid;
2637 int i;
2638
2639 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2640 if (!file_buf)
2641 return -ENOMEM;
2642
2643 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2644 if (!buf_comm) {
2645 kfree(file_buf);
2646 return -ENOMEM;
2647 }
2648
2649 buf = file_buf;
2650
2651 for (i = 0; i < SAVED_CMDLINES; i++) {
2652 int r;
2653
2654 pid = map_cmdline_to_pid[i];
2655 if (pid == -1 || pid == NO_CMDLINE_MAP)
2656 continue;
2657
2658 trace_find_cmdline(pid, buf_comm);
2659 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2660 buf += r;
2661 len += r;
2662 }
2663
2664 len = simple_read_from_buffer(ubuf, cnt, ppos,
2665 file_buf, len);
2666
2667 kfree(file_buf);
2668 kfree(buf_comm);
2669
2670 return len;
2671}
2672
2673static const struct file_operations tracing_saved_cmdlines_fops = {
2674 .open = tracing_open_generic,
2675 .read = tracing_saved_cmdlines_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002676 .llseek = generic_file_llseek,
Avadh Patel69abe6a2009-04-10 16:04:48 -04002677};
2678
2679static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002680tracing_ctrl_read(struct file *filp, char __user *ubuf,
2681 size_t cnt, loff_t *ppos)
2682{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002683 char buf[64];
2684 int r;
2685
Steven Rostedt90369902008-11-05 16:05:44 -05002686 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002687 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002688}
2689
2690static ssize_t
2691tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2692 size_t cnt, loff_t *ppos)
2693{
2694 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002695 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002696 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002697 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002698
Steven Rostedtcffae432008-05-12 21:21:00 +02002699 if (cnt >= sizeof(buf))
2700 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002701
2702 if (copy_from_user(&buf, ubuf, cnt))
2703 return -EFAULT;
2704
2705 buf[cnt] = 0;
2706
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002707 ret = strict_strtoul(buf, 10, &val);
2708 if (ret < 0)
2709 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002710
2711 val = !!val;
2712
2713 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002714 if (tracer_enabled ^ val) {
Steven Rostedt6752ab42011-02-08 13:54:06 -05002715
2716 /* Only need to warn if this is used to change the state */
2717 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2718
Steven Rostedt90369902008-11-05 16:05:44 -05002719 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002720 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002721 if (current_trace->start)
2722 current_trace->start(tr);
2723 tracing_start();
2724 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002725 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002726 tracing_stop();
2727 if (current_trace->stop)
2728 current_trace->stop(tr);
2729 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002730 }
2731 mutex_unlock(&trace_types_lock);
2732
Jiri Olsacf8517c2009-10-23 19:36:16 -04002733 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002734
2735 return cnt;
2736}
2737
2738static ssize_t
2739tracing_set_trace_read(struct file *filp, char __user *ubuf,
2740 size_t cnt, loff_t *ppos)
2741{
Li Zefanee6c2c12009-09-18 14:06:47 +08002742 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002743 int r;
2744
2745 mutex_lock(&trace_types_lock);
2746 if (current_trace)
2747 r = sprintf(buf, "%s\n", current_trace->name);
2748 else
2749 r = sprintf(buf, "\n");
2750 mutex_unlock(&trace_types_lock);
2751
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002752 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002753}
2754
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002755int tracer_init(struct tracer *t, struct trace_array *tr)
2756{
2757 tracing_reset_online_cpus(tr);
2758 return t->init(tr);
2759}
2760
Steven Rostedt73c51622009-03-11 13:42:01 -04002761static int tracing_resize_ring_buffer(unsigned long size)
2762{
2763 int ret;
2764
2765 /*
2766 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002767 * we use the size that was given, and we can forget about
2768 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002769 */
2770 ring_buffer_expanded = 1;
2771
2772 ret = ring_buffer_resize(global_trace.buffer, size);
2773 if (ret < 0)
2774 return ret;
2775
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002776 if (!current_trace->use_max_tr)
2777 goto out;
2778
Steven Rostedt73c51622009-03-11 13:42:01 -04002779 ret = ring_buffer_resize(max_tr.buffer, size);
2780 if (ret < 0) {
2781 int r;
2782
2783 r = ring_buffer_resize(global_trace.buffer,
2784 global_trace.entries);
2785 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002786 /*
2787 * AARGH! We are left with different
2788 * size max buffer!!!!
2789 * The max buffer is our "snapshot" buffer.
2790 * When a tracer needs a snapshot (one of the
2791 * latency tracers), it swaps the max buffer
2792 * with the saved snap shot. We succeeded to
2793 * update the size of the main buffer, but failed to
2794 * update the size of the max buffer. But when we tried
2795 * to reset the main buffer to the original size, we
2796 * failed there too. This is very unlikely to
2797 * happen, but if it does, warn and kill all
2798 * tracing.
2799 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002800 WARN_ON(1);
2801 tracing_disabled = 1;
2802 }
2803 return ret;
2804 }
2805
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002806 max_tr.entries = size;
2807 out:
Steven Rostedt73c51622009-03-11 13:42:01 -04002808 global_trace.entries = size;
2809
2810 return ret;
2811}
2812
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002813
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002814/**
2815 * tracing_update_buffers - used by tracing facility to expand ring buffers
2816 *
2817 * To save on memory when the tracing is never used on a system with it
2818 * configured in. The ring buffers are set to a minimum size. But once
2819 * a user starts to use the tracing facility, then they need to grow
2820 * to their default size.
2821 *
2822 * This function is to be called when a tracer is about to be used.
2823 */
2824int tracing_update_buffers(void)
2825{
2826 int ret = 0;
2827
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002828 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002829 if (!ring_buffer_expanded)
2830 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002831 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002832
2833 return ret;
2834}
2835
Steven Rostedt577b7852009-02-26 23:43:05 -05002836struct trace_option_dentry;
2837
2838static struct trace_option_dentry *
2839create_trace_option_files(struct tracer *tracer);
2840
2841static void
2842destroy_trace_option_files(struct trace_option_dentry *topts);
2843
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002844static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002845{
Steven Rostedt577b7852009-02-26 23:43:05 -05002846 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002847 struct trace_array *tr = &global_trace;
2848 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002849 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002850
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002851 mutex_lock(&trace_types_lock);
2852
Steven Rostedt73c51622009-03-11 13:42:01 -04002853 if (!ring_buffer_expanded) {
2854 ret = tracing_resize_ring_buffer(trace_buf_size);
2855 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01002856 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04002857 ret = 0;
2858 }
2859
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002860 for (t = trace_types; t; t = t->next) {
2861 if (strcmp(t->name, buf) == 0)
2862 break;
2863 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002864 if (!t) {
2865 ret = -EINVAL;
2866 goto out;
2867 }
2868 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002869 goto out;
2870
Steven Rostedt9f029e82008-11-12 15:24:24 -05002871 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002872 if (current_trace && current_trace->reset)
2873 current_trace->reset(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002874 if (current_trace && current_trace->use_max_tr) {
2875 /*
2876 * We don't free the ring buffer. instead, resize it because
2877 * The max_tr ring buffer has some state (e.g. ring->clock) and
2878 * we want preserve it.
2879 */
2880 ring_buffer_resize(max_tr.buffer, 1);
2881 max_tr.entries = 1;
2882 }
Steven Rostedt577b7852009-02-26 23:43:05 -05002883 destroy_trace_option_files(topts);
2884
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002885 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002886
2887 topts = create_trace_option_files(current_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002888 if (current_trace->use_max_tr) {
2889 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2890 if (ret < 0)
2891 goto out;
2892 max_tr.entries = global_trace.entries;
2893 }
Steven Rostedt577b7852009-02-26 23:43:05 -05002894
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002895 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002896 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002897 if (ret)
2898 goto out;
2899 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002900
Steven Rostedt9f029e82008-11-12 15:24:24 -05002901 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002902 out:
2903 mutex_unlock(&trace_types_lock);
2904
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002905 return ret;
2906}
2907
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002908static ssize_t
2909tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2910 size_t cnt, loff_t *ppos)
2911{
Li Zefanee6c2c12009-09-18 14:06:47 +08002912 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002913 int i;
2914 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002915 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002916
Steven Rostedt60063a62008-10-28 10:44:24 -04002917 ret = cnt;
2918
Li Zefanee6c2c12009-09-18 14:06:47 +08002919 if (cnt > MAX_TRACER_SIZE)
2920 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002921
2922 if (copy_from_user(&buf, ubuf, cnt))
2923 return -EFAULT;
2924
2925 buf[cnt] = 0;
2926
2927 /* strip ending whitespace. */
2928 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2929 buf[i] = 0;
2930
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002931 err = tracing_set_tracer(buf);
2932 if (err)
2933 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002934
Jiri Olsacf8517c2009-10-23 19:36:16 -04002935 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002936
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002937 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002938}
2939
2940static ssize_t
2941tracing_max_lat_read(struct file *filp, char __user *ubuf,
2942 size_t cnt, loff_t *ppos)
2943{
2944 unsigned long *ptr = filp->private_data;
2945 char buf[64];
2946 int r;
2947
Steven Rostedtcffae432008-05-12 21:21:00 +02002948 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002949 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002950 if (r > sizeof(buf))
2951 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002952 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002953}
2954
2955static ssize_t
2956tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2957 size_t cnt, loff_t *ppos)
2958{
Hannes Eder5e398412009-02-10 19:44:34 +01002959 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002960 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002961 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002962 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002963
Steven Rostedtcffae432008-05-12 21:21:00 +02002964 if (cnt >= sizeof(buf))
2965 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002966
2967 if (copy_from_user(&buf, ubuf, cnt))
2968 return -EFAULT;
2969
2970 buf[cnt] = 0;
2971
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002972 ret = strict_strtoul(buf, 10, &val);
2973 if (ret < 0)
2974 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002975
2976 *ptr = val * 1000;
2977
2978 return cnt;
2979}
2980
Steven Rostedtb3806b42008-05-12 21:20:46 +02002981static int tracing_open_pipe(struct inode *inode, struct file *filp)
2982{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002983 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002984 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002985 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002986
2987 if (tracing_disabled)
2988 return -ENODEV;
2989
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002990 mutex_lock(&trace_types_lock);
2991
Steven Rostedtb3806b42008-05-12 21:20:46 +02002992 /* create a buffer to store the information to pass to userspace */
2993 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002994 if (!iter) {
2995 ret = -ENOMEM;
2996 goto out;
2997 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002998
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002999 /*
3000 * We make a copy of the current tracer to avoid concurrent
3001 * changes on it while we are reading.
3002 */
3003 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3004 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003005 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003006 goto fail;
3007 }
3008 if (current_trace)
3009 *iter->trace = *current_trace;
3010
3011 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3012 ret = -ENOMEM;
3013 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10303014 }
3015
Steven Rostedta3097202008-11-07 22:36:02 -05003016 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10303017 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05003018
Steven Rostedt112f38a72009-06-01 15:16:05 -04003019 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3020 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3021
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003022 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003023 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003024 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003025 filp->private_data = iter;
3026
Steven Rostedt107bad82008-05-12 21:21:01 +02003027 if (iter->trace->pipe_open)
3028 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02003029
Arnd Bergmannb4447862010-07-07 23:40:11 +02003030 nonseekable_open(inode, filp);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003031out:
3032 mutex_unlock(&trace_types_lock);
3033 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003034
3035fail:
3036 kfree(iter->trace);
3037 kfree(iter);
3038 mutex_unlock(&trace_types_lock);
3039 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003040}
3041
3042static int tracing_release_pipe(struct inode *inode, struct file *file)
3043{
3044 struct trace_iterator *iter = file->private_data;
3045
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003046 mutex_lock(&trace_types_lock);
3047
Steven Rostedt29bf4a52009-12-09 12:37:43 -05003048 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05003049 iter->trace->pipe_close(iter);
3050
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003051 mutex_unlock(&trace_types_lock);
3052
Rusty Russell44623442009-01-01 10:12:23 +10303053 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003054 mutex_destroy(&iter->mutex);
3055 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003056 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003057
3058 return 0;
3059}
3060
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003061static unsigned int
3062tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3063{
3064 struct trace_iterator *iter = filp->private_data;
3065
3066 if (trace_flags & TRACE_ITER_BLOCK) {
3067 /*
3068 * Always select as readable when in blocking mode
3069 */
3070 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02003071 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003072 if (!trace_empty(iter))
3073 return POLLIN | POLLRDNORM;
3074 poll_wait(filp, &trace_wait, poll_table);
3075 if (!trace_empty(iter))
3076 return POLLIN | POLLRDNORM;
3077
3078 return 0;
3079 }
3080}
3081
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003082
3083void default_wait_pipe(struct trace_iterator *iter)
3084{
3085 DEFINE_WAIT(wait);
3086
3087 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3088
3089 if (trace_empty(iter))
3090 schedule();
3091
3092 finish_wait(&trace_wait, &wait);
3093}
3094
3095/*
3096 * This is a make-shift waitqueue.
3097 * A tracer might use this callback on some rare cases:
3098 *
3099 * 1) the current tracer might hold the runqueue lock when it wakes up
3100 * a reader, hence a deadlock (sched, function, and function graph tracers)
3101 * 2) the function tracers, trace all functions, we don't want
3102 * the overhead of calling wake_up and friends
3103 * (and tracing them too)
3104 *
3105 * Anyway, this is really very primitive wakeup.
3106 */
3107void poll_wait_pipe(struct trace_iterator *iter)
3108{
3109 set_current_state(TASK_INTERRUPTIBLE);
3110 /* sleep for 100 msecs, and try again. */
3111 schedule_timeout(HZ / 10);
3112}
3113
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003114/* Must be called with trace_types_lock mutex held. */
3115static int tracing_wait_pipe(struct file *filp)
3116{
3117 struct trace_iterator *iter = filp->private_data;
3118
3119 while (trace_empty(iter)) {
3120
3121 if ((filp->f_flags & O_NONBLOCK)) {
3122 return -EAGAIN;
3123 }
3124
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003125 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003126
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003127 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003128
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003129 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003130
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003131 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003132 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003133
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003134 /*
3135 * We block until we read something and tracing is disabled.
3136 * We still block if tracing is disabled, but we have never
3137 * read anything. This allows a user to cat this file, and
3138 * then enable tracing. But after we have read something,
3139 * we give an EOF when tracing is again disabled.
3140 *
3141 * iter->pos will be 0 if we haven't read anything.
3142 */
3143 if (!tracer_enabled && iter->pos)
3144 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003145 }
3146
3147 return 1;
3148}
3149
Steven Rostedtb3806b42008-05-12 21:20:46 +02003150/*
3151 * Consumer reader.
3152 */
3153static ssize_t
3154tracing_read_pipe(struct file *filp, char __user *ubuf,
3155 size_t cnt, loff_t *ppos)
3156{
3157 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003158 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003159 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003160
3161 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003162 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3163 if (sret != -EBUSY)
3164 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003165
Steven Rostedtf9520752009-03-02 14:04:40 -05003166 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003167
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003168 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02003169 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003170 if (unlikely(old_tracer != current_trace && current_trace)) {
3171 old_tracer = current_trace;
3172 *iter->trace = *current_trace;
3173 }
3174 mutex_unlock(&trace_types_lock);
3175
3176 /*
3177 * Avoid more than one consumer on a single file descriptor
3178 * This is just a matter of traces coherency, the ring buffer itself
3179 * is protected.
3180 */
3181 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003182 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003183 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3184 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02003185 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02003186 }
3187
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003188waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003189 sret = tracing_wait_pipe(filp);
3190 if (sret <= 0)
3191 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003192
3193 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003194 if (trace_empty(iter)) {
3195 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003196 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003197 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003198
3199 if (cnt >= PAGE_SIZE)
3200 cnt = PAGE_SIZE - 1;
3201
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003202 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003203 memset(&iter->seq, 0,
3204 sizeof(struct trace_iterator) -
3205 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02003206 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003207
Lai Jiangshan4f535962009-05-18 19:35:34 +08003208 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003209 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05003210 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003211 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003212 int len = iter->seq.len;
3213
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003214 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003215 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02003216 /* don't print partial lines */
3217 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003218 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003219 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01003220 if (ret != TRACE_TYPE_NO_CONSUME)
3221 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003222
3223 if (iter->seq.len >= cnt)
3224 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003225 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003226 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003227 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02003228
Steven Rostedtb3806b42008-05-12 21:20:46 +02003229 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003230 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3231 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05003232 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003233
3234 /*
3235 * If there was nothing to send to user, inspite of consuming trace
3236 * entries, go back to wait for more entries.
3237 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003238 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003239 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003240
Steven Rostedt107bad82008-05-12 21:21:01 +02003241out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003242 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003243
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003244 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003245}
3246
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003247static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3248 struct pipe_buffer *buf)
3249{
3250 __free_page(buf->page);
3251}
3252
3253static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3254 unsigned int idx)
3255{
3256 __free_page(spd->pages[idx]);
3257}
3258
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003259static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003260 .can_merge = 0,
3261 .map = generic_pipe_buf_map,
3262 .unmap = generic_pipe_buf_unmap,
3263 .confirm = generic_pipe_buf_confirm,
3264 .release = tracing_pipe_buf_release,
3265 .steal = generic_pipe_buf_steal,
3266 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003267};
3268
Steven Rostedt34cd4992009-02-09 12:06:29 -05003269static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003270tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003271{
3272 size_t count;
3273 int ret;
3274
3275 /* Seq buffer is page-sized, exactly what we need. */
3276 for (;;) {
3277 count = iter->seq.len;
3278 ret = print_trace_line(iter);
3279 count = iter->seq.len - count;
3280 if (rem < count) {
3281 rem = 0;
3282 iter->seq.len -= count;
3283 break;
3284 }
3285 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3286 iter->seq.len -= count;
3287 break;
3288 }
3289
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08003290 if (ret != TRACE_TYPE_NO_CONSUME)
3291 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003292 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05003293 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003294 rem = 0;
3295 iter->ent = NULL;
3296 break;
3297 }
3298 }
3299
3300 return rem;
3301}
3302
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003303static ssize_t tracing_splice_read_pipe(struct file *filp,
3304 loff_t *ppos,
3305 struct pipe_inode_info *pipe,
3306 size_t len,
3307 unsigned int flags)
3308{
Jens Axboe35f3d142010-05-20 10:43:18 +02003309 struct page *pages_def[PIPE_DEF_BUFFERS];
3310 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003311 struct trace_iterator *iter = filp->private_data;
3312 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02003313 .pages = pages_def,
3314 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05003315 .nr_pages = 0, /* This gets updated below. */
3316 .flags = flags,
3317 .ops = &tracing_pipe_buf_ops,
3318 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003319 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003320 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003321 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003322 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003323 unsigned int i;
3324
Jens Axboe35f3d142010-05-20 10:43:18 +02003325 if (splice_grow_spd(pipe, &spd))
3326 return -ENOMEM;
3327
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003328 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003329 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003330 if (unlikely(old_tracer != current_trace && current_trace)) {
3331 old_tracer = current_trace;
3332 *iter->trace = *current_trace;
3333 }
3334 mutex_unlock(&trace_types_lock);
3335
3336 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003337
3338 if (iter->trace->splice_read) {
3339 ret = iter->trace->splice_read(iter, filp,
3340 ppos, pipe, len, flags);
3341 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003342 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003343 }
3344
3345 ret = tracing_wait_pipe(filp);
3346 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003347 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003348
Jason Wessel955b61e2010-08-05 09:22:23 -05003349 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003350 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003351 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003352 }
3353
Lai Jiangshan4f535962009-05-18 19:35:34 +08003354 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003355 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003356
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003357 /* Fill as many pages as possible. */
Jens Axboe35f3d142010-05-20 10:43:18 +02003358 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3359 spd.pages[i] = alloc_page(GFP_KERNEL);
3360 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05003361 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003362
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003363 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003364
3365 /* Copy the data into the page, so we can start over. */
3366 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02003367 page_address(spd.pages[i]),
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003368 iter->seq.len);
3369 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02003370 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003371 break;
3372 }
Jens Axboe35f3d142010-05-20 10:43:18 +02003373 spd.partial[i].offset = 0;
3374 spd.partial[i].len = iter->seq.len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003375
Steven Rostedtf9520752009-03-02 14:04:40 -05003376 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003377 }
3378
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003379 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003380 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003381 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003382
3383 spd.nr_pages = i;
3384
Jens Axboe35f3d142010-05-20 10:43:18 +02003385 ret = splice_to_pipe(pipe, &spd);
3386out:
3387 splice_shrink_spd(pipe, &spd);
3388 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003389
Steven Rostedt34cd4992009-02-09 12:06:29 -05003390out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003391 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02003392 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003393}
3394
Steven Rostedta98a3c32008-05-12 21:20:59 +02003395static ssize_t
3396tracing_entries_read(struct file *filp, char __user *ubuf,
3397 size_t cnt, loff_t *ppos)
3398{
3399 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003400 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003401 int r;
3402
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003403 mutex_lock(&trace_types_lock);
3404 if (!ring_buffer_expanded)
3405 r = sprintf(buf, "%lu (expanded: %lu)\n",
3406 tr->entries >> 10,
3407 trace_buf_size >> 10);
3408 else
3409 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3410 mutex_unlock(&trace_types_lock);
3411
Steven Rostedta98a3c32008-05-12 21:20:59 +02003412 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3413}
3414
3415static ssize_t
3416tracing_entries_write(struct file *filp, const char __user *ubuf,
3417 size_t cnt, loff_t *ppos)
3418{
3419 unsigned long val;
3420 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003421 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003422
Steven Rostedtcffae432008-05-12 21:21:00 +02003423 if (cnt >= sizeof(buf))
3424 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003425
3426 if (copy_from_user(&buf, ubuf, cnt))
3427 return -EFAULT;
3428
3429 buf[cnt] = 0;
3430
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003431 ret = strict_strtoul(buf, 10, &val);
3432 if (ret < 0)
3433 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003434
3435 /* must have at least 1 entry */
3436 if (!val)
3437 return -EINVAL;
3438
3439 mutex_lock(&trace_types_lock);
3440
Steven Rostedtc76f0692008-11-07 22:36:02 -05003441 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003442
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003443 /* disable all cpu buffers */
3444 for_each_tracing_cpu(cpu) {
3445 if (global_trace.data[cpu])
3446 atomic_inc(&global_trace.data[cpu]->disabled);
3447 if (max_tr.data[cpu])
3448 atomic_inc(&max_tr.data[cpu]->disabled);
3449 }
3450
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003451 /* value is in KB */
3452 val <<= 10;
3453
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003454 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003455 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003456 if (ret < 0) {
3457 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003458 goto out;
3459 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003460 }
3461
Jiri Olsacf8517c2009-10-23 19:36:16 -04003462 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003463
Steven Rostedt19384c02008-05-22 00:22:16 -04003464 /* If check pages failed, return ENOMEM */
3465 if (tracing_disabled)
3466 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003467 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003468 for_each_tracing_cpu(cpu) {
3469 if (global_trace.data[cpu])
3470 atomic_dec(&global_trace.data[cpu]->disabled);
3471 if (max_tr.data[cpu])
3472 atomic_dec(&max_tr.data[cpu]->disabled);
3473 }
3474
Steven Rostedtc76f0692008-11-07 22:36:02 -05003475 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003476 mutex_unlock(&trace_types_lock);
3477
3478 return cnt;
3479}
3480
Carsten Emdef2942482009-12-06 14:02:44 +01003481static int mark_printk(const char *fmt, ...)
3482{
3483 int ret;
3484 va_list args;
3485 va_start(args, fmt);
3486 ret = trace_vprintk(0, fmt, args);
3487 va_end(args);
3488 return ret;
3489}
3490
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003491static ssize_t
3492tracing_mark_write(struct file *filp, const char __user *ubuf,
3493 size_t cnt, loff_t *fpos)
3494{
3495 char *buf;
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003496 size_t written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003497
Steven Rostedtc76f0692008-11-07 22:36:02 -05003498 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003499 return -EINVAL;
3500
3501 if (cnt > TRACE_BUF_SIZE)
3502 cnt = TRACE_BUF_SIZE;
3503
Carsten Emdec13d2f72009-11-16 20:56:13 +01003504 buf = kmalloc(cnt + 2, GFP_KERNEL);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003505 if (buf == NULL)
3506 return -ENOMEM;
3507
3508 if (copy_from_user(buf, ubuf, cnt)) {
3509 kfree(buf);
3510 return -EFAULT;
3511 }
Carsten Emdec13d2f72009-11-16 20:56:13 +01003512 if (buf[cnt-1] != '\n') {
3513 buf[cnt] = '\n';
3514 buf[cnt+1] = '\0';
3515 } else
3516 buf[cnt] = '\0';
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003517
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003518 written = mark_printk("%s", buf);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003519 kfree(buf);
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003520 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003521
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003522 /* don't tell userspace we wrote more - it might confuse them */
3523 if (written > cnt)
3524 written = cnt;
3525
3526 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003527}
3528
Li Zefan13f16d22009-12-08 11:16:11 +08003529static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08003530{
Zhaolei5079f322009-08-25 16:12:56 +08003531 int i;
3532
3533 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08003534 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08003535 "%s%s%s%s", i ? " " : "",
3536 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3537 i == trace_clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08003538 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08003539
Li Zefan13f16d22009-12-08 11:16:11 +08003540 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08003541}
3542
3543static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3544 size_t cnt, loff_t *fpos)
3545{
3546 char buf[64];
3547 const char *clockstr;
3548 int i;
3549
3550 if (cnt >= sizeof(buf))
3551 return -EINVAL;
3552
3553 if (copy_from_user(&buf, ubuf, cnt))
3554 return -EFAULT;
3555
3556 buf[cnt] = 0;
3557
3558 clockstr = strstrip(buf);
3559
3560 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3561 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3562 break;
3563 }
3564 if (i == ARRAY_SIZE(trace_clocks))
3565 return -EINVAL;
3566
3567 trace_clock_id = i;
3568
3569 mutex_lock(&trace_types_lock);
3570
3571 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3572 if (max_tr.buffer)
3573 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3574
3575 mutex_unlock(&trace_types_lock);
3576
3577 *fpos += cnt;
3578
3579 return cnt;
3580}
3581
Li Zefan13f16d22009-12-08 11:16:11 +08003582static int tracing_clock_open(struct inode *inode, struct file *file)
3583{
3584 if (tracing_disabled)
3585 return -ENODEV;
3586 return single_open(file, tracing_clock_show, NULL);
3587}
3588
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003589static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003590 .open = tracing_open_generic,
3591 .read = tracing_max_lat_read,
3592 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003593 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003594};
3595
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003596static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003597 .open = tracing_open_generic,
3598 .read = tracing_ctrl_read,
3599 .write = tracing_ctrl_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003600 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003601};
3602
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003603static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003604 .open = tracing_open_generic,
3605 .read = tracing_set_trace_read,
3606 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003607 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003608};
3609
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003610static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003611 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003612 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003613 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003614 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003615 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003616 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003617};
3618
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003619static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003620 .open = tracing_open_generic,
3621 .read = tracing_entries_read,
3622 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003623 .llseek = generic_file_llseek,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003624};
3625
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003626static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003627 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003628 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003629 .llseek = generic_file_llseek,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003630};
3631
Zhaolei5079f322009-08-25 16:12:56 +08003632static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08003633 .open = tracing_clock_open,
3634 .read = seq_read,
3635 .llseek = seq_lseek,
3636 .release = single_release,
Zhaolei5079f322009-08-25 16:12:56 +08003637 .write = tracing_clock_write,
3638};
3639
Steven Rostedt2cadf912008-12-01 22:20:19 -05003640struct ftrace_buffer_info {
3641 struct trace_array *tr;
3642 void *spare;
3643 int cpu;
3644 unsigned int read;
3645};
3646
3647static int tracing_buffers_open(struct inode *inode, struct file *filp)
3648{
3649 int cpu = (int)(long)inode->i_private;
3650 struct ftrace_buffer_info *info;
3651
3652 if (tracing_disabled)
3653 return -ENODEV;
3654
3655 info = kzalloc(sizeof(*info), GFP_KERNEL);
3656 if (!info)
3657 return -ENOMEM;
3658
3659 info->tr = &global_trace;
3660 info->cpu = cpu;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003661 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003662 /* Force reading ring buffer for first read */
3663 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003664
3665 filp->private_data = info;
3666
Lai Jiangshand1e7e022009-04-02 15:16:56 +08003667 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003668}
3669
3670static ssize_t
3671tracing_buffers_read(struct file *filp, char __user *ubuf,
3672 size_t count, loff_t *ppos)
3673{
3674 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003675 ssize_t ret;
3676 size_t size;
3677
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003678 if (!count)
3679 return 0;
3680
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003681 if (!info->spare)
3682 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3683 if (!info->spare)
3684 return -ENOMEM;
3685
Steven Rostedt2cadf912008-12-01 22:20:19 -05003686 /* Do we have previous read data to read? */
3687 if (info->read < PAGE_SIZE)
3688 goto read;
3689
3690 info->read = 0;
3691
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003692 trace_access_lock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003693 ret = ring_buffer_read_page(info->tr->buffer,
3694 &info->spare,
3695 count,
3696 info->cpu, 0);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003697 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003698 if (ret < 0)
3699 return 0;
3700
Steven Rostedt2cadf912008-12-01 22:20:19 -05003701read:
3702 size = PAGE_SIZE - info->read;
3703 if (size > count)
3704 size = count;
3705
3706 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003707 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003708 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003709 size -= ret;
3710
Steven Rostedt2cadf912008-12-01 22:20:19 -05003711 *ppos += size;
3712 info->read += size;
3713
3714 return size;
3715}
3716
3717static int tracing_buffers_release(struct inode *inode, struct file *file)
3718{
3719 struct ftrace_buffer_info *info = file->private_data;
3720
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003721 if (info->spare)
3722 ring_buffer_free_read_page(info->tr->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003723 kfree(info);
3724
3725 return 0;
3726}
3727
3728struct buffer_ref {
3729 struct ring_buffer *buffer;
3730 void *page;
3731 int ref;
3732};
3733
3734static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3735 struct pipe_buffer *buf)
3736{
3737 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3738
3739 if (--ref->ref)
3740 return;
3741
3742 ring_buffer_free_read_page(ref->buffer, ref->page);
3743 kfree(ref);
3744 buf->private = 0;
3745}
3746
3747static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3748 struct pipe_buffer *buf)
3749{
3750 return 1;
3751}
3752
3753static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3754 struct pipe_buffer *buf)
3755{
3756 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3757
3758 ref->ref++;
3759}
3760
3761/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003762static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003763 .can_merge = 0,
3764 .map = generic_pipe_buf_map,
3765 .unmap = generic_pipe_buf_unmap,
3766 .confirm = generic_pipe_buf_confirm,
3767 .release = buffer_pipe_buf_release,
3768 .steal = buffer_pipe_buf_steal,
3769 .get = buffer_pipe_buf_get,
3770};
3771
3772/*
3773 * Callback from splice_to_pipe(), if we need to release some pages
3774 * at the end of the spd in case we error'ed out in filling the pipe.
3775 */
3776static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3777{
3778 struct buffer_ref *ref =
3779 (struct buffer_ref *)spd->partial[i].private;
3780
3781 if (--ref->ref)
3782 return;
3783
3784 ring_buffer_free_read_page(ref->buffer, ref->page);
3785 kfree(ref);
3786 spd->partial[i].private = 0;
3787}
3788
3789static ssize_t
3790tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3791 struct pipe_inode_info *pipe, size_t len,
3792 unsigned int flags)
3793{
3794 struct ftrace_buffer_info *info = file->private_data;
Jens Axboe35f3d142010-05-20 10:43:18 +02003795 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3796 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05003797 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02003798 .pages = pages_def,
3799 .partial = partial_def,
Steven Rostedt2cadf912008-12-01 22:20:19 -05003800 .flags = flags,
3801 .ops = &buffer_pipe_buf_ops,
3802 .spd_release = buffer_spd_release,
3803 };
3804 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04003805 int entries, size, i;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003806 size_t ret;
3807
Jens Axboe35f3d142010-05-20 10:43:18 +02003808 if (splice_grow_spd(pipe, &spd))
3809 return -ENOMEM;
3810
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003811 if (*ppos & (PAGE_SIZE - 1)) {
3812 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02003813 ret = -EINVAL;
3814 goto out;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003815 }
3816
3817 if (len & (PAGE_SIZE - 1)) {
3818 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02003819 if (len < PAGE_SIZE) {
3820 ret = -EINVAL;
3821 goto out;
3822 }
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003823 len &= PAGE_MASK;
3824 }
3825
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003826 trace_access_lock(info->cpu);
Steven Rostedt93459c62009-04-29 00:23:13 -04003827 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3828
Jens Axboe35f3d142010-05-20 10:43:18 +02003829 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003830 struct page *page;
3831 int r;
3832
3833 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3834 if (!ref)
3835 break;
3836
Steven Rostedt7267fa62009-04-29 00:16:21 -04003837 ref->ref = 1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003838 ref->buffer = info->tr->buffer;
3839 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3840 if (!ref->page) {
3841 kfree(ref);
3842 break;
3843 }
3844
3845 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtf2957f12009-04-29 00:26:30 -04003846 len, info->cpu, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003847 if (r < 0) {
3848 ring_buffer_free_read_page(ref->buffer,
3849 ref->page);
3850 kfree(ref);
3851 break;
3852 }
3853
3854 /*
3855 * zero out any left over data, this is going to
3856 * user land.
3857 */
3858 size = ring_buffer_page_len(ref->page);
3859 if (size < PAGE_SIZE)
3860 memset(ref->page + size, 0, PAGE_SIZE - size);
3861
3862 page = virt_to_page(ref->page);
3863
3864 spd.pages[i] = page;
3865 spd.partial[i].len = PAGE_SIZE;
3866 spd.partial[i].offset = 0;
3867 spd.partial[i].private = (unsigned long)ref;
3868 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003869 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04003870
3871 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003872 }
3873
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003874 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003875 spd.nr_pages = i;
3876
3877 /* did we read anything? */
3878 if (!spd.nr_pages) {
3879 if (flags & SPLICE_F_NONBLOCK)
3880 ret = -EAGAIN;
3881 else
3882 ret = 0;
3883 /* TODO: block */
Jens Axboe35f3d142010-05-20 10:43:18 +02003884 goto out;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003885 }
3886
3887 ret = splice_to_pipe(pipe, &spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02003888 splice_shrink_spd(pipe, &spd);
3889out:
Steven Rostedt2cadf912008-12-01 22:20:19 -05003890 return ret;
3891}
3892
3893static const struct file_operations tracing_buffers_fops = {
3894 .open = tracing_buffers_open,
3895 .read = tracing_buffers_read,
3896 .release = tracing_buffers_release,
3897 .splice_read = tracing_buffers_splice_read,
3898 .llseek = no_llseek,
3899};
3900
Steven Rostedtc8d77182009-04-29 18:03:45 -04003901static ssize_t
3902tracing_stats_read(struct file *filp, char __user *ubuf,
3903 size_t count, loff_t *ppos)
3904{
3905 unsigned long cpu = (unsigned long)filp->private_data;
3906 struct trace_array *tr = &global_trace;
3907 struct trace_seq *s;
3908 unsigned long cnt;
3909
Li Zefane4f2d102009-06-15 10:57:28 +08003910 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04003911 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01003912 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04003913
3914 trace_seq_init(s);
3915
3916 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3917 trace_seq_printf(s, "entries: %ld\n", cnt);
3918
3919 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3920 trace_seq_printf(s, "overrun: %ld\n", cnt);
3921
3922 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3923 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3924
Steven Rostedtc8d77182009-04-29 18:03:45 -04003925 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3926
3927 kfree(s);
3928
3929 return count;
3930}
3931
3932static const struct file_operations tracing_stats_fops = {
3933 .open = tracing_open_generic,
3934 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003935 .llseek = generic_file_llseek,
Steven Rostedtc8d77182009-04-29 18:03:45 -04003936};
3937
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003938#ifdef CONFIG_DYNAMIC_FTRACE
3939
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003940int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003941{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003942 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003943}
3944
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003945static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003946tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003947 size_t cnt, loff_t *ppos)
3948{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003949 static char ftrace_dyn_info_buffer[1024];
3950 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003951 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003952 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003953 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003954 int r;
3955
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003956 mutex_lock(&dyn_info_mutex);
3957 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003958
Steven Rostedta26a2a22008-10-31 00:03:22 -04003959 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003960 buf[r++] = '\n';
3961
3962 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3963
3964 mutex_unlock(&dyn_info_mutex);
3965
3966 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003967}
3968
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003969static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003970 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003971 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003972 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003973};
3974#endif
3975
3976static struct dentry *d_tracer;
3977
3978struct dentry *tracing_init_dentry(void)
3979{
3980 static int once;
3981
3982 if (d_tracer)
3983 return d_tracer;
3984
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01003985 if (!debugfs_initialized())
3986 return NULL;
3987
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003988 d_tracer = debugfs_create_dir("tracing", NULL);
3989
3990 if (!d_tracer && !once) {
3991 once = 1;
3992 pr_warning("Could not create debugfs directory 'tracing'\n");
3993 return NULL;
3994 }
3995
3996 return d_tracer;
3997}
3998
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003999static struct dentry *d_percpu;
4000
4001struct dentry *tracing_dentry_percpu(void)
4002{
4003 static int once;
4004 struct dentry *d_tracer;
4005
4006 if (d_percpu)
4007 return d_percpu;
4008
4009 d_tracer = tracing_init_dentry();
4010
4011 if (!d_tracer)
4012 return NULL;
4013
4014 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4015
4016 if (!d_percpu && !once) {
4017 once = 1;
4018 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4019 return NULL;
4020 }
4021
4022 return d_percpu;
4023}
4024
4025static void tracing_init_debugfs_percpu(long cpu)
4026{
4027 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004028 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04004029 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004030
Steven Rostedtdd49a382010-10-20 21:51:26 -04004031 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004032 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4033 if (!d_cpu) {
4034 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4035 return;
4036 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004037
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004038 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004039 trace_create_file("trace_pipe", 0444, d_cpu,
4040 (void *) cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004041
4042 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004043 trace_create_file("trace", 0644, d_cpu,
4044 (void *) cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04004045
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004046 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4047 (void *) cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004048
4049 trace_create_file("stats", 0444, d_cpu,
4050 (void *) cpu, &tracing_stats_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004051}
4052
Steven Rostedt60a11772008-05-12 21:20:44 +02004053#ifdef CONFIG_FTRACE_SELFTEST
4054/* Let selftest have access to static functions in this file */
4055#include "trace_selftest.c"
4056#endif
4057
Steven Rostedt577b7852009-02-26 23:43:05 -05004058struct trace_option_dentry {
4059 struct tracer_opt *opt;
4060 struct tracer_flags *flags;
4061 struct dentry *entry;
4062};
4063
4064static ssize_t
4065trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4066 loff_t *ppos)
4067{
4068 struct trace_option_dentry *topt = filp->private_data;
4069 char *buf;
4070
4071 if (topt->flags->val & topt->opt->bit)
4072 buf = "1\n";
4073 else
4074 buf = "0\n";
4075
4076 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4077}
4078
4079static ssize_t
4080trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4081 loff_t *ppos)
4082{
4083 struct trace_option_dentry *topt = filp->private_data;
4084 unsigned long val;
4085 char buf[64];
4086 int ret;
4087
4088 if (cnt >= sizeof(buf))
4089 return -EINVAL;
4090
4091 if (copy_from_user(&buf, ubuf, cnt))
4092 return -EFAULT;
4093
4094 buf[cnt] = 0;
4095
4096 ret = strict_strtoul(buf, 10, &val);
4097 if (ret < 0)
4098 return ret;
4099
Li Zefan8d18eaa2009-12-08 11:17:06 +08004100 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05004101 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004102
4103 if (!!(topt->flags->val & topt->opt->bit) != val) {
4104 mutex_lock(&trace_types_lock);
4105 ret = __set_tracer_option(current_trace, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05004106 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004107 mutex_unlock(&trace_types_lock);
4108 if (ret)
4109 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05004110 }
4111
4112 *ppos += cnt;
4113
4114 return cnt;
4115}
4116
4117
4118static const struct file_operations trace_options_fops = {
4119 .open = tracing_open_generic,
4120 .read = trace_options_read,
4121 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004122 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05004123};
4124
Steven Rostedta8259072009-02-26 22:19:12 -05004125static ssize_t
4126trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4127 loff_t *ppos)
4128{
4129 long index = (long)filp->private_data;
4130 char *buf;
4131
4132 if (trace_flags & (1 << index))
4133 buf = "1\n";
4134 else
4135 buf = "0\n";
4136
4137 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4138}
4139
4140static ssize_t
4141trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4142 loff_t *ppos)
4143{
4144 long index = (long)filp->private_data;
4145 char buf[64];
4146 unsigned long val;
4147 int ret;
4148
4149 if (cnt >= sizeof(buf))
4150 return -EINVAL;
4151
4152 if (copy_from_user(&buf, ubuf, cnt))
4153 return -EFAULT;
4154
4155 buf[cnt] = 0;
4156
4157 ret = strict_strtoul(buf, 10, &val);
4158 if (ret < 0)
4159 return ret;
4160
Zhaoleif2d84b62009-08-07 18:55:48 +08004161 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05004162 return -EINVAL;
Zhaoleif2d84b62009-08-07 18:55:48 +08004163 set_tracer_flags(1 << index, val);
Steven Rostedta8259072009-02-26 22:19:12 -05004164
4165 *ppos += cnt;
4166
4167 return cnt;
4168}
4169
Steven Rostedta8259072009-02-26 22:19:12 -05004170static const struct file_operations trace_options_core_fops = {
4171 .open = tracing_open_generic,
4172 .read = trace_options_core_read,
4173 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004174 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05004175};
4176
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004177struct dentry *trace_create_file(const char *name,
4178 mode_t mode,
4179 struct dentry *parent,
4180 void *data,
4181 const struct file_operations *fops)
4182{
4183 struct dentry *ret;
4184
4185 ret = debugfs_create_file(name, mode, parent, data, fops);
4186 if (!ret)
4187 pr_warning("Could not create debugfs '%s' entry\n", name);
4188
4189 return ret;
4190}
4191
4192
Steven Rostedta8259072009-02-26 22:19:12 -05004193static struct dentry *trace_options_init_dentry(void)
4194{
4195 struct dentry *d_tracer;
4196 static struct dentry *t_options;
4197
4198 if (t_options)
4199 return t_options;
4200
4201 d_tracer = tracing_init_dentry();
4202 if (!d_tracer)
4203 return NULL;
4204
4205 t_options = debugfs_create_dir("options", d_tracer);
4206 if (!t_options) {
4207 pr_warning("Could not create debugfs directory 'options'\n");
4208 return NULL;
4209 }
4210
4211 return t_options;
4212}
4213
Steven Rostedt577b7852009-02-26 23:43:05 -05004214static void
4215create_trace_option_file(struct trace_option_dentry *topt,
4216 struct tracer_flags *flags,
4217 struct tracer_opt *opt)
4218{
4219 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05004220
4221 t_options = trace_options_init_dentry();
4222 if (!t_options)
4223 return;
4224
4225 topt->flags = flags;
4226 topt->opt = opt;
4227
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004228 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05004229 &trace_options_fops);
4230
Steven Rostedt577b7852009-02-26 23:43:05 -05004231}
4232
4233static struct trace_option_dentry *
4234create_trace_option_files(struct tracer *tracer)
4235{
4236 struct trace_option_dentry *topts;
4237 struct tracer_flags *flags;
4238 struct tracer_opt *opts;
4239 int cnt;
4240
4241 if (!tracer)
4242 return NULL;
4243
4244 flags = tracer->flags;
4245
4246 if (!flags || !flags->opts)
4247 return NULL;
4248
4249 opts = flags->opts;
4250
4251 for (cnt = 0; opts[cnt].name; cnt++)
4252 ;
4253
Steven Rostedt0cfe8242009-02-27 10:51:10 -05004254 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05004255 if (!topts)
4256 return NULL;
4257
4258 for (cnt = 0; opts[cnt].name; cnt++)
4259 create_trace_option_file(&topts[cnt], flags,
4260 &opts[cnt]);
4261
4262 return topts;
4263}
4264
4265static void
4266destroy_trace_option_files(struct trace_option_dentry *topts)
4267{
4268 int cnt;
4269
4270 if (!topts)
4271 return;
4272
4273 for (cnt = 0; topts[cnt].opt; cnt++) {
4274 if (topts[cnt].entry)
4275 debugfs_remove(topts[cnt].entry);
4276 }
4277
4278 kfree(topts);
4279}
4280
Steven Rostedta8259072009-02-26 22:19:12 -05004281static struct dentry *
4282create_trace_option_core_file(const char *option, long index)
4283{
4284 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004285
4286 t_options = trace_options_init_dentry();
4287 if (!t_options)
4288 return NULL;
4289
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004290 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05004291 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004292}
4293
4294static __init void create_trace_options_dir(void)
4295{
4296 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004297 int i;
4298
4299 t_options = trace_options_init_dentry();
4300 if (!t_options)
4301 return;
4302
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004303 for (i = 0; trace_options[i]; i++)
4304 create_trace_option_core_file(trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05004305}
4306
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004307static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004308{
4309 struct dentry *d_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004310 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004311
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004312 trace_access_lock_init();
4313
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004314 d_tracer = tracing_init_dentry();
4315
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004316 trace_create_file("tracing_enabled", 0644, d_tracer,
4317 &global_trace, &tracing_ctrl_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004318
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004319 trace_create_file("trace_options", 0644, d_tracer,
4320 NULL, &tracing_iter_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004321
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004322 trace_create_file("tracing_cpumask", 0644, d_tracer,
4323 NULL, &tracing_cpumask_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004324
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004325 trace_create_file("trace", 0644, d_tracer,
4326 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004327
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004328 trace_create_file("available_tracers", 0444, d_tracer,
4329 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004330
Li Zefan339ae5d2009-04-17 10:34:30 +08004331 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004332 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004333
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04004334#ifdef CONFIG_TRACER_MAX_TRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004335 trace_create_file("tracing_max_latency", 0644, d_tracer,
4336 &tracing_max_latency, &tracing_max_lat_fops);
Tim Bird0e950172010-02-25 15:36:43 -08004337#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004338
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004339 trace_create_file("tracing_thresh", 0644, d_tracer,
4340 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004341
Li Zefan339ae5d2009-04-17 10:34:30 +08004342 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004343 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004344
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004345 trace_create_file("trace_pipe", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004346 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004347
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004348 trace_create_file("buffer_size_kb", 0644, d_tracer,
4349 &global_trace, &tracing_entries_fops);
Steven Rostedta98a3c32008-05-12 21:20:59 +02004350
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004351 trace_create_file("trace_marker", 0220, d_tracer,
4352 NULL, &tracing_mark_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004353
Avadh Patel69abe6a2009-04-10 16:04:48 -04004354 trace_create_file("saved_cmdlines", 0444, d_tracer,
4355 NULL, &tracing_saved_cmdlines_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004356
Zhaolei5079f322009-08-25 16:12:56 +08004357 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4358 &trace_clock_fops);
4359
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004360#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004361 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4362 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004363#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004364
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004365 create_trace_options_dir();
4366
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004367 for_each_tracing_cpu(cpu)
4368 tracing_init_debugfs_percpu(cpu);
4369
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004370 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004371}
4372
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004373static int trace_panic_handler(struct notifier_block *this,
4374 unsigned long event, void *unused)
4375{
Steven Rostedt944ac422008-10-23 19:26:08 -04004376 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004377 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004378 return NOTIFY_OK;
4379}
4380
4381static struct notifier_block trace_panic_notifier = {
4382 .notifier_call = trace_panic_handler,
4383 .next = NULL,
4384 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4385};
4386
4387static int trace_die_handler(struct notifier_block *self,
4388 unsigned long val,
4389 void *data)
4390{
4391 switch (val) {
4392 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04004393 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004394 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004395 break;
4396 default:
4397 break;
4398 }
4399 return NOTIFY_OK;
4400}
4401
4402static struct notifier_block trace_die_notifier = {
4403 .notifier_call = trace_die_handler,
4404 .priority = 200
4405};
4406
4407/*
4408 * printk is set to max of 1024, we really don't need it that big.
4409 * Nothing should be printing 1000 characters anyway.
4410 */
4411#define TRACE_MAX_PRINT 1000
4412
4413/*
4414 * Define here KERN_TRACE so that we have one place to modify
4415 * it if we decide to change what log level the ftrace dump
4416 * should be at.
4417 */
Steven Rostedt428aee12009-01-14 12:24:42 -05004418#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004419
Jason Wessel955b61e2010-08-05 09:22:23 -05004420void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004421trace_printk_seq(struct trace_seq *s)
4422{
4423 /* Probably should print a warning here. */
4424 if (s->len >= 1000)
4425 s->len = 1000;
4426
4427 /* should be zero ended, but we are paranoid. */
4428 s->buffer[s->len] = 0;
4429
4430 printk(KERN_TRACE "%s", s->buffer);
4431
Steven Rostedtf9520752009-03-02 14:04:40 -05004432 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004433}
4434
Jason Wessel955b61e2010-08-05 09:22:23 -05004435void trace_init_global_iter(struct trace_iterator *iter)
4436{
4437 iter->tr = &global_trace;
4438 iter->trace = current_trace;
4439 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4440}
4441
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004442static void
4443__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004444{
Thomas Gleixner445c8952009-12-02 19:49:50 +01004445 static arch_spinlock_t ftrace_dump_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01004446 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004447 /* use static because iter can be a bit big for the stack */
4448 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004449 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004450 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004451 unsigned long flags;
4452 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004453
4454 /* only one dump */
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004455 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004456 arch_spin_lock(&ftrace_dump_lock);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004457 if (dump_ran)
4458 goto out;
4459
4460 dump_ran = 1;
4461
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004462 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004463
4464 if (disable_tracing)
4465 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004466
Jason Wessel955b61e2010-08-05 09:22:23 -05004467 trace_init_global_iter(&iter);
4468
Steven Rostedtd7690412008-10-01 00:29:53 -04004469 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05004470 atomic_inc(&iter.tr->data[cpu]->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04004471 }
4472
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004473 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4474
Török Edwinb54d3de2008-11-22 13:28:48 +02004475 /* don't look at user memory in panic mode */
4476 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4477
Steven Rostedte543ad72009-03-04 18:20:36 -05004478 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004479 iter.tr = &global_trace;
4480 iter.trace = current_trace;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004481
4482 switch (oops_dump_mode) {
4483 case DUMP_ALL:
4484 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4485 break;
4486 case DUMP_ORIG:
4487 iter.cpu_file = raw_smp_processor_id();
4488 break;
4489 case DUMP_NONE:
4490 goto out_enable;
4491 default:
4492 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4493 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4494 }
4495
4496 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004497
4498 /*
4499 * We need to stop all tracing on all CPUS to read the
4500 * the next buffer. This is a bit expensive, but is
4501 * not done often. We fill all what we can read,
4502 * and then release the locks again.
4503 */
4504
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004505 while (!trace_empty(&iter)) {
4506
4507 if (!cnt)
4508 printk(KERN_TRACE "---------------------------------\n");
4509
4510 cnt++;
4511
4512 /* reset all but tr, trace, and overruns */
4513 memset(&iter.seq, 0,
4514 sizeof(struct trace_iterator) -
4515 offsetof(struct trace_iterator, seq));
4516 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4517 iter.pos = -1;
4518
Jason Wessel955b61e2010-08-05 09:22:23 -05004519 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08004520 int ret;
4521
4522 ret = print_trace_line(&iter);
4523 if (ret != TRACE_TYPE_NO_CONSUME)
4524 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004525 }
4526
4527 trace_printk_seq(&iter.seq);
4528 }
4529
4530 if (!cnt)
4531 printk(KERN_TRACE " (ftrace buffer empty)\n");
4532 else
4533 printk(KERN_TRACE "---------------------------------\n");
4534
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004535 out_enable:
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004536 /* Re-enable tracing if requested */
4537 if (!disable_tracing) {
4538 trace_flags |= old_userobj;
4539
4540 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05004541 atomic_dec(&iter.tr->data[cpu]->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004542 }
4543 tracing_on();
4544 }
4545
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004546 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004547 arch_spin_unlock(&ftrace_dump_lock);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004548 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004549}
4550
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004551/* By default: disable tracing after the dump */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004552void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004553{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004554 __ftrace_dump(true, oops_dump_mode);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004555}
4556
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004557__init static int tracer_alloc_buffers(void)
4558{
Steven Rostedt73c51622009-03-11 13:42:01 -04004559 int ring_buf_size;
David Sharp750912f2010-12-08 13:46:47 -08004560 enum ring_buffer_flags rb_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004561 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304562 int ret = -ENOMEM;
4563
David Sharp750912f2010-12-08 13:46:47 -08004564
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304565 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4566 goto out;
4567
4568 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4569 goto out_free_buffer_mask;
4570
Steven Rostedt73c51622009-03-11 13:42:01 -04004571 /* To save memory, keep the ring buffer size to its minimum */
4572 if (ring_buffer_expanded)
4573 ring_buf_size = trace_buf_size;
4574 else
4575 ring_buf_size = 1;
4576
David Sharp750912f2010-12-08 13:46:47 -08004577 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4578
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304579 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4580 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004581
Steven Rostedtab464282008-05-12 21:21:00 +02004582 /* TODO: make the number of buffers hot pluggable with CPUS */
David Sharp750912f2010-12-08 13:46:47 -08004583 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004584 if (!global_trace.buffer) {
4585 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4586 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304587 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004588 }
4589 global_trace.entries = ring_buffer_size(global_trace.buffer);
4590
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304591
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004592#ifdef CONFIG_TRACER_MAX_TRACE
David Sharp750912f2010-12-08 13:46:47 -08004593 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004594 if (!max_tr.buffer) {
4595 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4596 WARN_ON(1);
4597 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304598 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004599 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004600 max_tr.entries = 1;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004601#endif
4602
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004603 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004604 for_each_tracing_cpu(i) {
jolsa@redhat.com566b0aa2009-07-16 21:44:26 +02004605 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Tejun Heo9705f692009-10-29 22:34:13 +09004606 max_tr.data[i] = &per_cpu(max_tr_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004607 }
4608
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004609 trace_init_cmdlines();
4610
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004611 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004612 current_trace = &nop_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +02004613 /* All seems OK, enable tracing */
4614 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004615
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004616 atomic_notifier_chain_register(&panic_notifier_list,
4617 &trace_panic_notifier);
4618
4619 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01004620
4621 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004622
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304623out_free_cpumask:
4624 free_cpumask_var(tracing_cpumask);
4625out_free_buffer_mask:
4626 free_cpumask_var(tracing_buffer_mask);
4627out:
4628 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004629}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004630
4631__init static int clear_boot_tracer(void)
4632{
4633 /*
4634 * The default tracer at boot buffer is an init section.
4635 * This function is called in lateinit. If we did not
4636 * find the boot tracer, then clear it out, to prevent
4637 * later registration from accessing the buffer that is
4638 * about to be freed.
4639 */
4640 if (!default_bootup_tracer)
4641 return 0;
4642
4643 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4644 default_bootup_tracer);
4645 default_bootup_tracer = NULL;
4646
4647 return 0;
4648}
4649
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004650early_initcall(tracer_alloc_buffers);
4651fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004652late_initcall(clear_boot_tracer);