blob: 42fdf3adff319b4cb5e2ec2dc9e8dddb792d8cbf [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
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700346static void wakeup_work_handler(struct work_struct *work)
347{
348 wake_up(&trace_wait);
349}
350
351static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
352
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200353/**
354 * trace_wake_up - wake up tasks waiting for trace input
355 *
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700356 * Schedules a delayed work to wake up any task that is blocked on the
357 * trace_wait queue. These is used with trace_poll for tasks polling the
358 * trace.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200359 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200360void trace_wake_up(void)
361{
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700362 const unsigned long delay = msecs_to_jiffies(2);
Andrew Morton89f19f02009-09-19 11:55:44 -0700363
364 if (trace_flags & TRACE_ITER_BLOCK)
365 return;
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700366 schedule_delayed_work(&wakeup_work, delay);
Ingo Molnar4e655512008-05-12 21:20:52 +0200367}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200368
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400369static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200370{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400371 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200372
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200373 if (!str)
374 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800375 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200376 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800377 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200378 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400379 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200380 return 1;
381}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400382__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200383
Tim Bird0e950172010-02-25 15:36:43 -0800384static int __init set_tracing_thresh(char *str)
385{
386 unsigned long threshhold;
387 int ret;
388
389 if (!str)
390 return 0;
391 ret = strict_strtoul(str, 0, &threshhold);
392 if (ret < 0)
393 return 0;
394 tracing_thresh = threshhold * 1000;
395 return 1;
396}
397__setup("tracing_thresh=", set_tracing_thresh);
398
Steven Rostedt57f50be2008-05-12 21:20:44 +0200399unsigned long nsecs_to_usecs(unsigned long nsecs)
400{
401 return nsecs / 1000;
402}
403
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200404/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200405static const char *trace_options[] = {
406 "print-parent",
407 "sym-offset",
408 "sym-addr",
409 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200410 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200411 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200412 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200413 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200414 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100415 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500416 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500417 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500418 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200419 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200420 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100421 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200422 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500423 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400424 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400425 "graph-time",
Li Zefane870e9a2010-07-02 11:07:32 +0800426 "record-cmd",
David Sharp750912f2010-12-08 13:46:47 -0800427 "overwrite",
Steven Rostedtcf30cf62011-06-14 22:44:07 -0400428 "disable_on_free",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200429 NULL
430};
431
Zhaolei5079f322009-08-25 16:12:56 +0800432static struct {
433 u64 (*func)(void);
434 const char *name;
435} trace_clocks[] = {
436 { trace_clock_local, "local" },
437 { trace_clock_global, "global" },
438};
439
440int trace_clock_id;
441
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200442/*
443 * trace_parser_get_init - gets the buffer for trace parser
444 */
445int trace_parser_get_init(struct trace_parser *parser, int size)
446{
447 memset(parser, 0, sizeof(*parser));
448
449 parser->buffer = kmalloc(size, GFP_KERNEL);
450 if (!parser->buffer)
451 return 1;
452
453 parser->size = size;
454 return 0;
455}
456
457/*
458 * trace_parser_put - frees the buffer for trace parser
459 */
460void trace_parser_put(struct trace_parser *parser)
461{
462 kfree(parser->buffer);
463}
464
465/*
466 * trace_get_user - reads the user input string separated by space
467 * (matched by isspace(ch))
468 *
469 * For each string found the 'struct trace_parser' is updated,
470 * and the function returns.
471 *
472 * Returns number of bytes read.
473 *
474 * See kernel/trace/trace.h for 'struct trace_parser' details.
475 */
476int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
477 size_t cnt, loff_t *ppos)
478{
479 char ch;
480 size_t read = 0;
481 ssize_t ret;
482
483 if (!*ppos)
484 trace_parser_clear(parser);
485
486 ret = get_user(ch, ubuf++);
487 if (ret)
488 goto out;
489
490 read++;
491 cnt--;
492
493 /*
494 * The parser is not finished with the last write,
495 * continue reading the user input without skipping spaces.
496 */
497 if (!parser->cont) {
498 /* skip white space */
499 while (cnt && isspace(ch)) {
500 ret = get_user(ch, ubuf++);
501 if (ret)
502 goto out;
503 read++;
504 cnt--;
505 }
506
507 /* only spaces were written */
508 if (isspace(ch)) {
509 *ppos += read;
510 ret = read;
511 goto out;
512 }
513
514 parser->idx = 0;
515 }
516
517 /* read the non-space input */
518 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800519 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200520 parser->buffer[parser->idx++] = ch;
521 else {
522 ret = -EINVAL;
523 goto out;
524 }
525 ret = get_user(ch, ubuf++);
526 if (ret)
527 goto out;
528 read++;
529 cnt--;
530 }
531
532 /* We either got finished input or we have to wait for another call. */
533 if (isspace(ch)) {
534 parser->buffer[parser->idx] = 0;
535 parser->cont = false;
536 } else {
537 parser->cont = true;
538 parser->buffer[parser->idx++] = ch;
539 }
540
541 *ppos += read;
542 ret = read;
543
544out:
545 return ret;
546}
547
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200548ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
549{
550 int len;
551 int ret;
552
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500553 if (!cnt)
554 return 0;
555
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200556 if (s->len <= s->readpos)
557 return -EBUSY;
558
559 len = s->len - s->readpos;
560 if (cnt > len)
561 cnt = len;
562 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500563 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200564 return -EFAULT;
565
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500566 cnt -= ret;
567
Steven Rostedte74da522009-03-04 20:31:11 -0500568 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200569 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200570}
571
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200572static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200573{
574 int len;
575 void *ret;
576
577 if (s->len <= s->readpos)
578 return -EBUSY;
579
580 len = s->len - s->readpos;
581 if (cnt > len)
582 cnt = len;
583 ret = memcpy(buf, s->buffer + s->readpos, cnt);
584 if (!ret)
585 return -EFAULT;
586
Steven Rostedte74da522009-03-04 20:31:11 -0500587 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200588 return cnt;
589}
590
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400591/*
592 * ftrace_max_lock is used to protect the swapping of buffers
593 * when taking a max snapshot. The buffers themselves are
594 * protected by per_cpu spinlocks. But the action of the swap
595 * needs its own lock.
596 *
Thomas Gleixner445c8952009-12-02 19:49:50 +0100597 * This is defined as a arch_spinlock_t in order to help
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400598 * with performance when lockdep debugging is enabled.
599 *
600 * It is also used in other places outside the update_max_tr
601 * so it needs to be defined outside of the
602 * CONFIG_TRACER_MAX_TRACE.
603 */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100604static arch_spinlock_t ftrace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100605 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400606
Tim Bird0e950172010-02-25 15:36:43 -0800607unsigned long __read_mostly tracing_thresh;
608
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400609#ifdef CONFIG_TRACER_MAX_TRACE
610unsigned long __read_mostly tracing_max_latency;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400611
612/*
613 * Copy the new maximum trace into the separate maximum-trace
614 * structure. (this way the maximum trace is permanently saved,
615 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
616 */
617static void
618__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
619{
620 struct trace_array_cpu *data = tr->data[cpu];
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300621 struct trace_array_cpu *max_data;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400622
623 max_tr.cpu = cpu;
624 max_tr.time_start = data->preempt_timestamp;
625
Steven Rostedt8248ac02009-09-02 12:27:41 -0400626 max_data = max_tr.data[cpu];
627 max_data->saved_latency = tracing_max_latency;
628 max_data->critical_start = data->critical_start;
629 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400630
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300631 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -0400632 max_data->pid = tsk->pid;
633 max_data->uid = task_uid(tsk);
634 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
635 max_data->policy = tsk->policy;
636 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400637
638 /* record this tasks comm */
639 tracing_record_cmdline(tsk);
640}
641
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200642/**
643 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
644 * @tr: tracer
645 * @tsk: the task with the latency
646 * @cpu: The cpu that initiated the trace.
647 *
648 * Flip the buffers between the @tr and the max_tr and record information
649 * about which task was the cause of this latency.
650 */
Ingo Molnare309b412008-05-12 21:20:51 +0200651void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
653{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400654 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200655
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400656 if (trace_stop_count)
657 return;
658
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200659 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900660 if (!current_trace->use_max_tr) {
661 WARN_ON_ONCE(1);
662 return;
663 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100664 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400665
666 tr->buffer = max_tr.buffer;
667 max_tr.buffer = buf;
668
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200669 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100670 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200671}
672
673/**
674 * update_max_tr_single - only copy one trace over, and reset the rest
675 * @tr - tracer
676 * @tsk - task with the latency
677 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200678 *
679 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200680 */
Ingo Molnare309b412008-05-12 21:20:51 +0200681void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200682update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
683{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400684 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200685
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400686 if (trace_stop_count)
687 return;
688
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200689 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900690 if (!current_trace->use_max_tr) {
691 WARN_ON_ONCE(1);
692 return;
693 }
694
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100695 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200696
Steven Rostedtd7690412008-10-01 00:29:53 -0400697 ftrace_disable_cpu();
698
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400699 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
700
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400701 if (ret == -EBUSY) {
702 /*
703 * We failed to swap the buffer due to a commit taking
704 * place on this CPU. We fail to record, but we reset
705 * the max trace buffer (no one writes directly to it)
706 * and flag that it failed.
707 */
708 trace_array_printk(&max_tr, _THIS_IP_,
709 "Failed to swap buffers due to commit in progress\n");
710 }
711
Steven Rostedtd7690412008-10-01 00:29:53 -0400712 ftrace_enable_cpu();
713
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400714 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200715
716 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100717 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200718}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400719#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200720
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200721/**
722 * register_tracer - register a tracer with the ftrace system.
723 * @type - the plugin for the tracer
724 *
725 * Register a new plugin tracer.
726 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200727int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100728__releases(kernel_lock)
729__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730{
731 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200732 int ret = 0;
733
734 if (!type->name) {
735 pr_info("Tracer must have a name\n");
736 return -1;
737 }
738
Dan Carpenter24a461d2010-07-10 12:06:44 +0200739 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +0800740 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
741 return -1;
742 }
743
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200744 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100745
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100746 tracing_selftest_running = true;
747
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200748 for (t = trace_types; t; t = t->next) {
749 if (strcmp(type->name, t->name) == 0) {
750 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +0800751 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200752 type->name);
753 ret = -1;
754 goto out;
755 }
756 }
757
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100758 if (!type->set_flag)
759 type->set_flag = &dummy_set_flag;
760 if (!type->flags)
761 type->flags = &dummy_tracer_flags;
762 else
763 if (!type->flags->opts)
764 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100765 if (!type->wait_pipe)
766 type->wait_pipe = default_wait_pipe;
767
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100768
Steven Rostedt60a11772008-05-12 21:20:44 +0200769#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500770 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200771 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200772 struct trace_array *tr = &global_trace;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100773
Steven Rostedt60a11772008-05-12 21:20:44 +0200774 /*
775 * Run a selftest on this tracer.
776 * Here we reset the trace buffer, and set the current
777 * tracer to be this tracer. The tracer can then run some
778 * internal tracing to verify that everything is in order.
779 * If we fail, we do not register this tracer.
780 */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400781 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100782
Steven Rostedt60a11772008-05-12 21:20:44 +0200783 current_trace = type;
Steven Rostedt4a0b1662011-03-09 20:09:26 -0500784
785 /* If we expanded the buffers, make sure the max is expanded too */
786 if (ring_buffer_expanded && type->use_max_tr)
787 ring_buffer_resize(max_tr.buffer, trace_buf_size);
788
Steven Rostedt60a11772008-05-12 21:20:44 +0200789 /* the test is responsible for initializing and enabling */
790 pr_info("Testing tracer %s: ", type->name);
791 ret = type->selftest(type, tr);
792 /* the test is responsible for resetting too */
793 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200794 if (ret) {
795 printk(KERN_CONT "FAILED!\n");
796 goto out;
797 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200798 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400799 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100800
Steven Rostedt4a0b1662011-03-09 20:09:26 -0500801 /* Shrink the max buffer again */
802 if (ring_buffer_expanded && type->use_max_tr)
803 ring_buffer_resize(max_tr.buffer, 1);
804
Steven Rostedt60a11772008-05-12 21:20:44 +0200805 printk(KERN_CONT "PASSED\n");
806 }
807#endif
808
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200809 type->next = trace_types;
810 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200811
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200812 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100813 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200814 mutex_unlock(&trace_types_lock);
815
Steven Rostedtdac74942009-02-05 01:13:38 -0500816 if (ret || !default_bootup_tracer)
817 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500818
Li Zefanee6c2c12009-09-18 14:06:47 +0800819 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -0500820 goto out_unlock;
821
822 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
823 /* Do we want this tracer to start on bootup? */
824 tracing_set_tracer(type->name);
825 default_bootup_tracer = NULL;
826 /* disable other selftests, since this will break it. */
827 tracing_selftest_disabled = 1;
828#ifdef CONFIG_FTRACE_STARTUP_TEST
829 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
830 type->name);
831#endif
832
833 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200834 return ret;
835}
836
837void unregister_tracer(struct tracer *type)
838{
839 struct tracer **t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200840
841 mutex_lock(&trace_types_lock);
842 for (t = &trace_types; *t; t = &(*t)->next) {
843 if (*t == type)
844 goto found;
845 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800846 pr_info("Tracer %s not registered\n", type->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200847 goto out;
848
849 found:
850 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200851
852 if (type == current_trace && tracer_enabled) {
853 tracer_enabled = 0;
854 tracing_stop();
855 if (current_trace->stop)
856 current_trace->stop(&global_trace);
857 current_trace = &nop_trace;
858 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800859out:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200860 mutex_unlock(&trace_types_lock);
861}
862
Steven Rostedt283740c2010-03-12 19:48:41 -0500863static void __tracing_reset(struct ring_buffer *buffer, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200864{
Steven Rostedtd7690412008-10-01 00:29:53 -0400865 ftrace_disable_cpu();
Steven Rostedt283740c2010-03-12 19:48:41 -0500866 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400867 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200868}
869
Steven Rostedtf6339032009-09-04 12:35:16 -0400870void tracing_reset(struct trace_array *tr, int cpu)
871{
872 struct ring_buffer *buffer = tr->buffer;
873
874 ring_buffer_record_disable(buffer);
875
876 /* Make sure all commits have finished */
877 synchronize_sched();
Steven Rostedt283740c2010-03-12 19:48:41 -0500878 __tracing_reset(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -0400879
880 ring_buffer_record_enable(buffer);
881}
882
Pekka J Enberg213cc062008-12-19 12:08:39 +0200883void tracing_reset_online_cpus(struct trace_array *tr)
884{
Steven Rostedt621968c2009-09-04 12:02:35 -0400885 struct ring_buffer *buffer = tr->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +0200886 int cpu;
887
Steven Rostedt621968c2009-09-04 12:02:35 -0400888 ring_buffer_record_disable(buffer);
889
890 /* Make sure all commits have finished */
891 synchronize_sched();
892
Pekka J Enberg213cc062008-12-19 12:08:39 +0200893 tr->time_start = ftrace_now(tr->cpu);
894
895 for_each_online_cpu(cpu)
Steven Rostedt283740c2010-03-12 19:48:41 -0500896 __tracing_reset(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -0400897
898 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +0200899}
900
Steven Rostedt9456f0f2009-05-06 21:54:09 -0400901void tracing_reset_current(int cpu)
902{
903 tracing_reset(&global_trace, cpu);
904}
905
906void tracing_reset_current_online_cpus(void)
907{
908 tracing_reset_online_cpus(&global_trace);
909}
910
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200911#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100912#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200913static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
914static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
915static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
916static int cmdline_idx;
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100917static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200918
Steven Rostedt25b0b442008-05-12 21:21:00 +0200919/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100920static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200921
922static void trace_init_cmdlines(void)
923{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100924 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
925 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200926 cmdline_idx = 0;
927}
928
Carsten Emdeb5130b12009-09-13 01:43:07 +0200929int is_tracing_stopped(void)
930{
931 return trace_stop_count;
932}
933
Steven Rostedt0f048702008-11-05 16:05:44 -0500934/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500935 * ftrace_off_permanent - disable all ftrace code permanently
936 *
937 * This should only be called when a serious anomally has
938 * been detected. This will turn off the function tracing,
939 * ring buffers, and other tracing utilites. It takes no
940 * locks and can be called from any context.
941 */
942void ftrace_off_permanent(void)
943{
944 tracing_disabled = 1;
945 ftrace_stop();
946 tracing_off_permanent();
947}
948
949/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500950 * tracing_start - quick start of the tracer
951 *
952 * If tracing is enabled but was stopped by tracing_stop,
953 * this will start the tracer back up.
954 */
955void tracing_start(void)
956{
957 struct ring_buffer *buffer;
958 unsigned long flags;
959
960 if (tracing_disabled)
961 return;
962
963 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500964 if (--trace_stop_count) {
965 if (trace_stop_count < 0) {
966 /* Someone screwed up their debugging */
967 WARN_ON_ONCE(1);
968 trace_stop_count = 0;
969 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500970 goto out;
971 }
972
Steven Rostedta2f80712010-03-12 19:56:00 -0500973 /* Prevent the buffers from switching */
974 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -0500975
976 buffer = global_trace.buffer;
977 if (buffer)
978 ring_buffer_record_enable(buffer);
979
980 buffer = max_tr.buffer;
981 if (buffer)
982 ring_buffer_record_enable(buffer);
983
Steven Rostedta2f80712010-03-12 19:56:00 -0500984 arch_spin_unlock(&ftrace_max_lock);
985
Steven Rostedt0f048702008-11-05 16:05:44 -0500986 ftrace_start();
987 out:
988 spin_unlock_irqrestore(&tracing_start_lock, flags);
989}
990
991/**
992 * tracing_stop - quick stop of the tracer
993 *
994 * Light weight way to stop tracing. Use in conjunction with
995 * tracing_start.
996 */
997void tracing_stop(void)
998{
999 struct ring_buffer *buffer;
1000 unsigned long flags;
1001
1002 ftrace_stop();
1003 spin_lock_irqsave(&tracing_start_lock, flags);
1004 if (trace_stop_count++)
1005 goto out;
1006
Steven Rostedta2f80712010-03-12 19:56:00 -05001007 /* Prevent the buffers from switching */
1008 arch_spin_lock(&ftrace_max_lock);
1009
Steven Rostedt0f048702008-11-05 16:05:44 -05001010 buffer = global_trace.buffer;
1011 if (buffer)
1012 ring_buffer_record_disable(buffer);
1013
1014 buffer = max_tr.buffer;
1015 if (buffer)
1016 ring_buffer_record_disable(buffer);
1017
Steven Rostedta2f80712010-03-12 19:56:00 -05001018 arch_spin_unlock(&ftrace_max_lock);
1019
Steven Rostedt0f048702008-11-05 16:05:44 -05001020 out:
1021 spin_unlock_irqrestore(&tracing_start_lock, flags);
1022}
1023
Ingo Molnare309b412008-05-12 21:20:51 +02001024void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001025
Ingo Molnare309b412008-05-12 21:20:51 +02001026static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001027{
Carsten Emdea635cf02009-03-18 09:00:41 +01001028 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001029
1030 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1031 return;
1032
1033 /*
1034 * It's not the end of the world if we don't get
1035 * the lock, but we also don't want to spin
1036 * nor do we want to disable interrupts,
1037 * so if we miss here, then better luck next time.
1038 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001039 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001040 return;
1041
1042 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001043 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1045
Carsten Emdea635cf02009-03-18 09:00:41 +01001046 /*
1047 * Check whether the cmdline buffer at idx has a pid
1048 * mapped. We are going to overwrite that entry so we
1049 * need to clear the map_pid_to_cmdline. Otherwise we
1050 * would read the new comm for the old pid.
1051 */
1052 pid = map_cmdline_to_pid[idx];
1053 if (pid != NO_CMDLINE_MAP)
1054 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001055
Carsten Emdea635cf02009-03-18 09:00:41 +01001056 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001057 map_pid_to_cmdline[tsk->pid] = idx;
1058
1059 cmdline_idx = idx;
1060 }
1061
1062 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1063
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001064 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001065}
1066
Steven Rostedt4ca530852009-03-16 19:20:15 -04001067void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001069 unsigned map;
1070
Steven Rostedt4ca530852009-03-16 19:20:15 -04001071 if (!pid) {
1072 strcpy(comm, "<idle>");
1073 return;
1074 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001075
Steven Rostedt74bf4072010-01-25 15:11:53 -05001076 if (WARN_ON_ONCE(pid < 0)) {
1077 strcpy(comm, "<XXX>");
1078 return;
1079 }
1080
Steven Rostedt4ca530852009-03-16 19:20:15 -04001081 if (pid > PID_MAX_DEFAULT) {
1082 strcpy(comm, "<...>");
1083 return;
1084 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001085
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001086 preempt_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001087 arch_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001088 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001089 if (map != NO_CMDLINE_MAP)
1090 strcpy(comm, saved_cmdlines[map]);
1091 else
1092 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001093
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001094 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001095 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001096}
1097
Ingo Molnare309b412008-05-12 21:20:51 +02001098void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099{
Thomas Gleixner18aecd32009-03-18 08:56:58 +01001100 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1101 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001102 return;
1103
1104 trace_save_cmdline(tsk);
1105}
1106
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001107void
Steven Rostedt38697052008-10-01 13:14:09 -04001108tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1109 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001110{
1111 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001112
Steven Rostedt777e2082008-09-29 23:02:42 -04001113 entry->preempt_count = pc & 0xff;
1114 entry->pid = (tsk) ? tsk->pid : 0;
Arjan van de Vena3a4a5a2011-05-05 23:55:18 -04001115 entry->padding = 0;
Steven Rostedt777e2082008-09-29 23:02:42 -04001116 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001117#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001118 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001119#else
1120 TRACE_FLAG_IRQS_NOSUPPORT |
1121#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001122 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1123 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1124 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1125}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001126EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001127
Steven Rostedte77405a2009-09-02 14:17:06 -04001128struct ring_buffer_event *
1129trace_buffer_lock_reserve(struct ring_buffer *buffer,
1130 int type,
1131 unsigned long len,
1132 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001133{
1134 struct ring_buffer_event *event;
1135
Steven Rostedte77405a2009-09-02 14:17:06 -04001136 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001137 if (event != NULL) {
1138 struct trace_entry *ent = ring_buffer_event_data(event);
1139
1140 tracing_generic_entry_update(ent, flags, pc);
1141 ent->type = type;
1142 }
1143
1144 return event;
1145}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001146
Steven Rostedte77405a2009-09-02 14:17:06 -04001147static inline void
1148__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1149 struct ring_buffer_event *event,
1150 unsigned long flags, int pc,
1151 int wake)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001152{
Steven Rostedte77405a2009-09-02 14:17:06 -04001153 ring_buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001154
Steven Rostedte77405a2009-09-02 14:17:06 -04001155 ftrace_trace_stack(buffer, flags, 6, pc);
1156 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001157
1158 if (wake)
1159 trace_wake_up();
1160}
1161
Steven Rostedte77405a2009-09-02 14:17:06 -04001162void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1163 struct ring_buffer_event *event,
1164 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001165{
Steven Rostedte77405a2009-09-02 14:17:06 -04001166 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001167}
1168
Steven Rostedtef5580d2009-02-27 19:38:04 -05001169struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001170trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1171 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001172 unsigned long flags, int pc)
1173{
Steven Rostedte77405a2009-09-02 14:17:06 -04001174 *current_rb = global_trace.buffer;
1175 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001176 type, len, flags, pc);
1177}
Steven Rostedt94487d62009-05-05 19:22:53 -04001178EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001179
Steven Rostedte77405a2009-09-02 14:17:06 -04001180void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1181 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001182 unsigned long flags, int pc)
1183{
Steven Rostedte77405a2009-09-02 14:17:06 -04001184 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001185}
Steven Rostedt94487d62009-05-05 19:22:53 -04001186EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001187
Steven Rostedte77405a2009-09-02 14:17:06 -04001188void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1189 struct ring_buffer_event *event,
1190 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001191{
Steven Rostedte77405a2009-09-02 14:17:06 -04001192 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001193}
Steven Rostedt94487d62009-05-05 19:22:53 -04001194EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
Steven Rostedt77d9f462009-04-02 01:16:59 -04001195
Steven Rostedte77405a2009-09-02 14:17:06 -04001196void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1197 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001198{
Steven Rostedte77405a2009-09-02 14:17:06 -04001199 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001200}
Steven Rostedt12acd472009-04-17 16:01:56 -04001201EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001202
Ingo Molnare309b412008-05-12 21:20:51 +02001203void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001204trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001205 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1206 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001207{
Tom Zanussie1112b42009-03-31 00:48:49 -05001208 struct ftrace_event_call *call = &event_function;
Steven Rostedte77405a2009-09-02 14:17:06 -04001209 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001210 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001211 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001212
Steven Rostedtd7690412008-10-01 00:29:53 -04001213 /* If we are reading the ring buffer, don't trace */
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001214 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04001215 return;
1216
Steven Rostedte77405a2009-09-02 14:17:06 -04001217 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001218 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001219 if (!event)
1220 return;
1221 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001222 entry->ip = ip;
1223 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001224
Steven Rostedte77405a2009-09-02 14:17:06 -04001225 if (!filter_check_discard(call, entry, buffer, event))
1226 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001227}
1228
Ingo Molnare309b412008-05-12 21:20:51 +02001229void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001230ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -04001231 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1232 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001233{
1234 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001235 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001236}
1237
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001238#ifdef CONFIG_STACKTRACE
Steven Rostedte77405a2009-09-02 14:17:06 -04001239static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001240 unsigned long flags,
1241 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +02001242{
Tom Zanussie1112b42009-03-31 00:48:49 -05001243 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001244 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001245 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001246 struct stack_trace trace;
1247
Steven Rostedte77405a2009-09-02 14:17:06 -04001248 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001249 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001250 if (!event)
1251 return;
1252 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001253 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +02001254
1255 trace.nr_entries = 0;
1256 trace.max_entries = FTRACE_STACK_ENTRIES;
1257 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -04001258 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +02001259
1260 save_stack_trace(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001261 if (!filter_check_discard(call, entry, buffer, event))
1262 ring_buffer_unlock_commit(buffer, event);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001263}
1264
Steven Rostedte77405a2009-09-02 14:17:06 -04001265void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1266 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001267{
1268 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1269 return;
1270
Steven Rostedte77405a2009-09-02 14:17:06 -04001271 __ftrace_trace_stack(buffer, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -05001272}
1273
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001274void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1275 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001276{
Steven Rostedte77405a2009-09-02 14:17:06 -04001277 __ftrace_trace_stack(tr->buffer, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -04001278}
1279
Steven Rostedt03889382009-12-11 09:48:22 -05001280/**
1281 * trace_dump_stack - record a stack back trace in the trace buffer
1282 */
1283void trace_dump_stack(void)
1284{
1285 unsigned long flags;
1286
1287 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001288 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001289
1290 local_save_flags(flags);
1291
1292 /* skipping 3 traces, seems to get us at the caller of this function */
1293 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count());
1294}
1295
Steven Rostedt91e86e52010-11-10 12:56:12 +01001296static DEFINE_PER_CPU(int, user_stack_count);
1297
Steven Rostedte77405a2009-09-02 14:17:06 -04001298void
1299ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001300{
Tom Zanussie1112b42009-03-31 00:48:49 -05001301 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001302 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001303 struct userstack_entry *entry;
1304 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001305
1306 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1307 return;
1308
Steven Rostedtb6345872010-03-12 20:03:30 -05001309 /*
1310 * NMIs can not handle page faults, even with fix ups.
1311 * The save user stack can (and often does) fault.
1312 */
1313 if (unlikely(in_nmi()))
1314 return;
1315
Steven Rostedt91e86e52010-11-10 12:56:12 +01001316 /*
1317 * prevent recursion, since the user stack tracing may
1318 * trigger other kernel events.
1319 */
1320 preempt_disable();
1321 if (__this_cpu_read(user_stack_count))
1322 goto out;
1323
1324 __this_cpu_inc(user_stack_count);
1325
Steven Rostedte77405a2009-09-02 14:17:06 -04001326 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001327 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001328 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08001329 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02001330 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001331
Steven Rostedt48659d32009-09-11 11:36:23 -04001332 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001333 memset(&entry->caller, 0, sizeof(entry->caller));
1334
1335 trace.nr_entries = 0;
1336 trace.max_entries = FTRACE_STACK_ENTRIES;
1337 trace.skip = 0;
1338 trace.entries = entry->caller;
1339
1340 save_stack_trace_user(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001341 if (!filter_check_discard(call, entry, buffer, event))
1342 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001343
Li Zefan1dbd1952010-12-09 15:47:56 +08001344 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01001345 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001346 out:
1347 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02001348}
1349
Hannes Eder4fd27352009-02-10 19:44:12 +01001350#ifdef UNUSED
1351static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001352{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001353 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001354}
Hannes Eder4fd27352009-02-10 19:44:12 +01001355#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001356
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001357#endif /* CONFIG_STACKTRACE */
1358
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001359/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001360 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001361 *
1362 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001363int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001364{
Thomas Gleixner445c8952009-12-02 19:49:50 +01001365 static arch_spinlock_t trace_buf_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001366 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001367 static u32 trace_buf[TRACE_BUF_SIZE];
1368
Tom Zanussie1112b42009-03-31 00:48:49 -05001369 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001370 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001371 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001372 struct trace_array *tr = &global_trace;
1373 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001374 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001375 unsigned long flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001376 int disable;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001377 int cpu, len = 0, size, pc;
1378
1379 if (unlikely(tracing_selftest_running || tracing_disabled))
1380 return 0;
1381
1382 /* Don't pollute graph traces with trace_vprintk internals */
1383 pause_graph_tracing();
1384
1385 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04001386 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001387 cpu = raw_smp_processor_id();
1388 data = tr->data[cpu];
1389
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001390 disable = atomic_inc_return(&data->disabled);
1391 if (unlikely(disable != 1))
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001392 goto out;
1393
Steven Rostedt80370cb2009-03-10 17:16:35 -04001394 /* Lockdep uses trace_printk for lock tracing */
1395 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001396 arch_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001397 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1398
1399 if (len > TRACE_BUF_SIZE || len < 0)
1400 goto out_unlock;
1401
1402 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedte77405a2009-09-02 14:17:06 -04001403 buffer = tr->buffer;
1404 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1405 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001406 if (!event)
1407 goto out_unlock;
1408 entry = ring_buffer_event_data(event);
1409 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001410 entry->fmt = fmt;
1411
1412 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
Steven Rostedtd9313692010-01-06 17:27:11 -05001413 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001414 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001415 ftrace_trace_stack(buffer, flags, 6, pc);
1416 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001417
1418out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001419 arch_spin_unlock(&trace_buf_lock);
Steven Rostedt80370cb2009-03-10 17:16:35 -04001420 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001421
1422out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001423 atomic_dec_return(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -04001424 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001425 unpause_graph_tracing();
1426
1427 return len;
1428}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001429EXPORT_SYMBOL_GPL(trace_vbprintk);
1430
Steven Rostedt659372d2009-09-03 19:11:07 -04001431int trace_array_printk(struct trace_array *tr,
1432 unsigned long ip, const char *fmt, ...)
1433{
1434 int ret;
1435 va_list ap;
1436
1437 if (!(trace_flags & TRACE_ITER_PRINTK))
1438 return 0;
1439
1440 va_start(ap, fmt);
1441 ret = trace_array_vprintk(tr, ip, fmt, ap);
1442 va_end(ap);
1443 return ret;
1444}
1445
1446int trace_array_vprintk(struct trace_array *tr,
1447 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001448{
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01001449 static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001450 static char trace_buf[TRACE_BUF_SIZE];
1451
Tom Zanussie1112b42009-03-31 00:48:49 -05001452 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001453 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001454 struct ring_buffer *buffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001455 struct trace_array_cpu *data;
1456 int cpu, len = 0, size, pc;
1457 struct print_entry *entry;
1458 unsigned long irq_flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001459 int disable;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001460
1461 if (tracing_disabled || tracing_selftest_running)
1462 return 0;
1463
1464 pc = preempt_count();
1465 preempt_disable_notrace();
1466 cpu = raw_smp_processor_id();
1467 data = tr->data[cpu];
1468
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001469 disable = atomic_inc_return(&data->disabled);
1470 if (unlikely(disable != 1))
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001471 goto out;
1472
1473 pause_graph_tracing();
1474 raw_local_irq_save(irq_flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001475 arch_spin_lock(&trace_buf_lock);
Carsten Emdef2942482009-12-06 14:02:44 +01001476 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001477
1478 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04001479 buffer = tr->buffer;
1480 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
1481 irq_flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001482 if (!event)
1483 goto out_unlock;
1484 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001485 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001486
1487 memcpy(&entry->buf, trace_buf, len);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001488 entry->buf[len] = '\0';
Steven Rostedtd9313692010-01-06 17:27:11 -05001489 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001490 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001491 ftrace_trace_stack(buffer, irq_flags, 6, pc);
1492 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001493
1494 out_unlock:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001495 arch_spin_unlock(&trace_buf_lock);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001496 raw_local_irq_restore(irq_flags);
1497 unpause_graph_tracing();
1498 out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001499 atomic_dec_return(&data->disabled);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001500 preempt_enable_notrace();
1501
1502 return len;
1503}
Steven Rostedt659372d2009-09-03 19:11:07 -04001504
1505int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1506{
Steven Rostedta813a152009-10-09 01:41:35 -04001507 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04001508}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001509EXPORT_SYMBOL_GPL(trace_vprintk);
1510
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001511static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001512{
Steven Rostedtd7690412008-10-01 00:29:53 -04001513 /* Don't allow ftrace to trace into the ring buffers */
1514 ftrace_disable_cpu();
1515
Steven Rostedt5a90f572008-09-03 17:42:51 -04001516 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001517 if (iter->buffer_iter[iter->cpu])
1518 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1519
1520 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001521}
1522
Ingo Molnare309b412008-05-12 21:20:51 +02001523static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001524peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1525 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001526{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001527 struct ring_buffer_event *event;
1528 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001529
Steven Rostedtd7690412008-10-01 00:29:53 -04001530 /* Don't allow ftrace to trace into the ring buffers */
1531 ftrace_disable_cpu();
1532
1533 if (buf_iter)
1534 event = ring_buffer_iter_peek(buf_iter, ts);
1535 else
Steven Rostedtbc21b472010-03-31 19:49:26 -04001536 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1537 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001538
1539 ftrace_enable_cpu();
1540
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001541 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001542}
Steven Rostedtd7690412008-10-01 00:29:53 -04001543
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001544static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001545__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1546 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001548 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001549 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08001550 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001551 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001552 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001553 int next_cpu = -1;
1554 int cpu;
1555
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001556 /*
1557 * If we are in a per_cpu trace file, don't bother by iterating over
1558 * all cpu and peek directly.
1559 */
1560 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1561 if (ring_buffer_empty_cpu(buffer, cpu_file))
1562 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001563 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001564 if (ent_cpu)
1565 *ent_cpu = cpu_file;
1566
1567 return ent;
1568 }
1569
Steven Rostedtab464282008-05-12 21:21:00 +02001570 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001571
1572 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001573 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001574
Steven Rostedtbc21b472010-03-31 19:49:26 -04001575 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001576
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001577 /*
1578 * Pick the entry with the smallest timestamp:
1579 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001580 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001581 next = ent;
1582 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001583 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001584 next_lost = lost_events;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001585 }
1586 }
1587
1588 if (ent_cpu)
1589 *ent_cpu = next_cpu;
1590
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001591 if (ent_ts)
1592 *ent_ts = next_ts;
1593
Steven Rostedtbc21b472010-03-31 19:49:26 -04001594 if (missing_events)
1595 *missing_events = next_lost;
1596
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001597 return next;
1598}
1599
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001600/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001601struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1602 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001603{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001604 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001605}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001606
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001607/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05001608void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001609{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001610 iter->ent = __find_next_entry(iter, &iter->cpu,
1611 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001612
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001613 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001614 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001615
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001616 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001617}
1618
Ingo Molnare309b412008-05-12 21:20:51 +02001619static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001620{
Steven Rostedtd7690412008-10-01 00:29:53 -04001621 /* Don't allow ftrace to trace into the ring buffers */
1622 ftrace_disable_cpu();
Steven Rostedtbc21b472010-03-31 19:49:26 -04001623 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1624 &iter->lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001625 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001626}
1627
Ingo Molnare309b412008-05-12 21:20:51 +02001628static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001629{
1630 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001631 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001632 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001633
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001634 WARN_ON_ONCE(iter->leftover);
1635
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001636 (*pos)++;
1637
1638 /* can't go backwards */
1639 if (iter->idx > i)
1640 return NULL;
1641
1642 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05001643 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001644 else
1645 ent = iter;
1646
1647 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05001648 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001649
1650 iter->pos = *pos;
1651
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001652 return ent;
1653}
1654
Jason Wessel955b61e2010-08-05 09:22:23 -05001655void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001656{
1657 struct trace_array *tr = iter->tr;
1658 struct ring_buffer_event *event;
1659 struct ring_buffer_iter *buf_iter;
1660 unsigned long entries = 0;
1661 u64 ts;
1662
1663 tr->data[cpu]->skipped_entries = 0;
1664
1665 if (!iter->buffer_iter[cpu])
1666 return;
1667
1668 buf_iter = iter->buffer_iter[cpu];
1669 ring_buffer_iter_reset(buf_iter);
1670
1671 /*
1672 * We could have the case with the max latency tracers
1673 * that a reset never took place on a cpu. This is evident
1674 * by the timestamp being before the start of the buffer.
1675 */
1676 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1677 if (ts >= iter->tr->time_start)
1678 break;
1679 entries++;
1680 ring_buffer_read(buf_iter, NULL);
1681 }
1682
1683 tr->data[cpu]->skipped_entries = entries;
1684}
1685
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001686/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001687 * The current tracer is copied to avoid a global locking
1688 * all around.
1689 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001690static void *s_start(struct seq_file *m, loff_t *pos)
1691{
1692 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001693 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001694 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001695 void *p = NULL;
1696 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001697 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001698
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001699 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001700 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001701 if (unlikely(old_tracer != current_trace && current_trace)) {
1702 old_tracer = current_trace;
1703 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001704 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001705 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001706
1707 atomic_inc(&trace_record_cmdline_disabled);
1708
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001709 if (*pos != iter->pos) {
1710 iter->ent = NULL;
1711 iter->cpu = 0;
1712 iter->idx = -1;
1713
Steven Rostedtd7690412008-10-01 00:29:53 -04001714 ftrace_disable_cpu();
1715
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001716 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1717 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001718 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001719 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001720 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001721
Steven Rostedtd7690412008-10-01 00:29:53 -04001722 ftrace_enable_cpu();
1723
Lai Jiangshanac91d852010-03-02 17:54:50 +08001724 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001725 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1726 ;
1727
1728 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001729 /*
1730 * If we overflowed the seq_file before, then we want
1731 * to just reuse the trace_seq buffer again.
1732 */
1733 if (iter->leftover)
1734 p = iter;
1735 else {
1736 l = *pos - 1;
1737 p = s_next(m, p, &l);
1738 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001739 }
1740
Lai Jiangshan4f535962009-05-18 19:35:34 +08001741 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001742 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001743 return p;
1744}
1745
1746static void s_stop(struct seq_file *m, void *p)
1747{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001748 struct trace_iterator *iter = m->private;
1749
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001750 atomic_dec(&trace_record_cmdline_disabled);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001751 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08001752 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001753}
1754
Ingo Molnare309b412008-05-12 21:20:51 +02001755static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001756{
Michael Ellermana6168352008-08-20 16:36:11 -07001757 seq_puts(m, "# _------=> CPU# \n");
1758 seq_puts(m, "# / _-----=> irqs-off \n");
1759 seq_puts(m, "# | / _----=> need-resched \n");
1760 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1761 seq_puts(m, "# ||| / _--=> preempt-depth \n");
Steven Rostedte6e1e252011-03-09 10:41:56 -05001762 seq_puts(m, "# |||| / delay \n");
1763 seq_puts(m, "# cmd pid ||||| time | caller \n");
1764 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765}
1766
Ingo Molnare309b412008-05-12 21:20:51 +02001767static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001768{
Michael Ellermana6168352008-08-20 16:36:11 -07001769 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1770 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001771}
1772
1773
Jiri Olsa62b915f2010-04-02 19:01:22 +02001774void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001775print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1776{
1777 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1778 struct trace_array *tr = iter->tr;
1779 struct trace_array_cpu *data = tr->data[tr->cpu];
1780 struct tracer *type = current_trace;
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001781 unsigned long entries = 0;
1782 unsigned long total = 0;
1783 unsigned long count;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001784 const char *name = "preemption";
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001785 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001786
1787 if (type)
1788 name = type->name;
1789
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001790
1791 for_each_tracing_cpu(cpu) {
1792 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1793 /*
1794 * If this buffer has skipped entries, then we hold all
1795 * entries for the trace and we need to ignore the
1796 * ones before the time stamp.
1797 */
1798 if (tr->data[cpu]->skipped_entries) {
1799 count -= tr->data[cpu]->skipped_entries;
1800 /* total is the same as the entries */
1801 total += count;
1802 } else
1803 total += count +
1804 ring_buffer_overrun_cpu(tr->buffer, cpu);
1805 entries += count;
1806 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001807
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001808 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001809 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001810 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001811 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001812 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001813 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001814 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001815 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001816 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001817 tr->cpu,
1818#if defined(CONFIG_PREEMPT_NONE)
1819 "server",
1820#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1821 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001822#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001823 "preempt",
1824#else
1825 "unknown",
1826#endif
1827 /* These are reserved for later use */
1828 0, 0, 0, 0);
1829#ifdef CONFIG_SMP
1830 seq_printf(m, " #P:%d)\n", num_online_cpus());
1831#else
1832 seq_puts(m, ")\n");
1833#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001834 seq_puts(m, "# -----------------\n");
1835 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001836 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1837 data->comm, data->pid, data->uid, data->nice,
1838 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001839 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001840
1841 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001842 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001843 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1844 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001845 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001846 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1847 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04001848 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001849 }
1850
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001851 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001852}
1853
Steven Rostedta3097202008-11-07 22:36:02 -05001854static void test_cpu_buff_start(struct trace_iterator *iter)
1855{
1856 struct trace_seq *s = &iter->seq;
1857
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001858 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1859 return;
1860
1861 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1862 return;
1863
Rusty Russell44623442009-01-01 10:12:23 +10301864 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001865 return;
1866
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001867 if (iter->tr->data[iter->cpu]->skipped_entries)
1868 return;
1869
Rusty Russell44623442009-01-01 10:12:23 +10301870 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001871
1872 /* Don't print started cpu buffer for the first entry of the trace */
1873 if (iter->idx > 1)
1874 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1875 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05001876}
1877
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001878static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001879{
Steven Rostedt214023c2008-05-12 21:20:46 +02001880 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001881 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001882 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001883 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001884
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001885 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001886
Steven Rostedta3097202008-11-07 22:36:02 -05001887 test_cpu_buff_start(iter);
1888
Steven Rostedtf633cef2008-12-23 23:24:13 -05001889 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001890
1891 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001892 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1893 if (!trace_print_lat_context(iter))
1894 goto partial;
1895 } else {
1896 if (!trace_print_context(iter))
1897 goto partial;
1898 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001899 }
1900
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001901 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04001902 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001903
1904 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1905 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001906
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001907 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001908partial:
1909 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001910}
1911
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001912static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001913{
1914 struct trace_seq *s = &iter->seq;
1915 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001916 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001917
1918 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001919
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001920 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001921 if (!trace_seq_printf(s, "%d %d %llu ",
1922 entry->pid, iter->cpu, iter->ts))
1923 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001924 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001925
Steven Rostedtf633cef2008-12-23 23:24:13 -05001926 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001927 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04001928 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001929
1930 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1931 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001932
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001933 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001934partial:
1935 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001936}
1937
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001938static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001939{
1940 struct trace_seq *s = &iter->seq;
1941 unsigned char newline = '\n';
1942 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001943 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001944
1945 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001946
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001947 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1948 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1949 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1950 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1951 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001952
Steven Rostedtf633cef2008-12-23 23:24:13 -05001953 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001954 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04001955 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001956 if (ret != TRACE_TYPE_HANDLED)
1957 return ret;
1958 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001959
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001960 SEQ_PUT_FIELD_RET(s, newline);
1961
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001962 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001963}
1964
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001965static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001966{
1967 struct trace_seq *s = &iter->seq;
1968 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001969 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001970
1971 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001972
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001973 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1974 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001975 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001976 SEQ_PUT_FIELD_RET(s, iter->ts);
1977 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001978
Steven Rostedtf633cef2008-12-23 23:24:13 -05001979 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04001980 return event ? event->funcs->binary(iter, 0, event) :
1981 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001982}
1983
Jiri Olsa62b915f2010-04-02 19:01:22 +02001984int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001985{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001986 int cpu;
1987
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001988 /* If we are looking at one CPU buffer, only check that one */
1989 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1990 cpu = iter->cpu_file;
1991 if (iter->buffer_iter[cpu]) {
1992 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1993 return 0;
1994 } else {
1995 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1996 return 0;
1997 }
1998 return 1;
1999 }
2000
Steven Rostedtab464282008-05-12 21:21:00 +02002001 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04002002 if (iter->buffer_iter[cpu]) {
2003 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2004 return 0;
2005 } else {
2006 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2007 return 0;
2008 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002009 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002010
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002011 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002012}
2013
Lai Jiangshan4f535962009-05-18 19:35:34 +08002014/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05002015enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002016{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002017 enum print_line_t ret;
2018
Jiri Olsaee5e51f2011-03-25 12:05:18 +01002019 if (iter->lost_events &&
2020 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2021 iter->cpu, iter->lost_events))
2022 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002023
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002024 if (iter->trace && iter->trace->print_line) {
2025 ret = iter->trace->print_line(iter);
2026 if (ret != TRACE_TYPE_UNHANDLED)
2027 return ret;
2028 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002029
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002030 if (iter->ent->type == TRACE_BPRINT &&
2031 trace_flags & TRACE_ITER_PRINTK &&
2032 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002033 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002034
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002035 if (iter->ent->type == TRACE_PRINT &&
2036 trace_flags & TRACE_ITER_PRINTK &&
2037 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002038 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002039
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002040 if (trace_flags & TRACE_ITER_BIN)
2041 return print_bin_fmt(iter);
2042
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002043 if (trace_flags & TRACE_ITER_HEX)
2044 return print_hex_fmt(iter);
2045
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002046 if (trace_flags & TRACE_ITER_RAW)
2047 return print_raw_fmt(iter);
2048
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002049 return print_trace_fmt(iter);
2050}
2051
Jiri Olsa62b915f2010-04-02 19:01:22 +02002052void trace_default_header(struct seq_file *m)
2053{
2054 struct trace_iterator *iter = m->private;
2055
2056 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2057 /* print nothing if the buffers are empty */
2058 if (trace_empty(iter))
2059 return;
2060 print_trace_header(m, iter);
2061 if (!(trace_flags & TRACE_ITER_VERBOSE))
2062 print_lat_help_header(m);
2063 } else {
2064 if (!(trace_flags & TRACE_ITER_VERBOSE))
2065 print_func_help_header(m);
2066 }
2067}
2068
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002069static int s_show(struct seq_file *m, void *v)
2070{
2071 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002072 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002073
2074 if (iter->ent == NULL) {
2075 if (iter->tr) {
2076 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2077 seq_puts(m, "#\n");
2078 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002079 if (iter->trace && iter->trace->print_header)
2080 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02002081 else
2082 trace_default_header(m);
2083
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002084 } else if (iter->leftover) {
2085 /*
2086 * If we filled the seq_file buffer earlier, we
2087 * want to just show it now.
2088 */
2089 ret = trace_print_seq(m, &iter->seq);
2090
2091 /* ret should this time be zero, but you never know */
2092 iter->leftover = ret;
2093
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002094 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002095 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002096 ret = trace_print_seq(m, &iter->seq);
2097 /*
2098 * If we overflow the seq_file buffer, then it will
2099 * ask us for this data again at start up.
2100 * Use that instead.
2101 * ret is 0 if seq_file write succeeded.
2102 * -1 otherwise.
2103 */
2104 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002105 }
2106
2107 return 0;
2108}
2109
James Morris88e9d342009-09-22 16:43:43 -07002110static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002111 .start = s_start,
2112 .next = s_next,
2113 .stop = s_stop,
2114 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002115};
2116
Ingo Molnare309b412008-05-12 21:20:51 +02002117static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002118__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002119{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002120 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002121 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002122 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002123 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002124 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002125
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002126 if (tracing_disabled)
2127 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02002128
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002129 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002130 if (!iter)
2131 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002132
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002133 /*
2134 * We make a copy of the current tracer to avoid concurrent
2135 * changes on it while we are reading.
2136 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002137 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002138 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002139 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002140 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002141
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002142 if (current_trace)
2143 *iter->trace = *current_trace;
2144
Li Zefan79f55992009-06-15 14:58:26 +08002145 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002146 goto fail;
2147
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002148 if (current_trace && current_trace->print_max)
2149 iter->tr = &max_tr;
2150 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002151 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002152 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002153 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002154 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002155
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002156 /* Notify the tracer early; before we stop tracing. */
2157 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01002158 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002159
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002160 /* Annotate start of buffers if we had overruns */
2161 if (ring_buffer_overruns(iter->tr->buffer))
2162 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2163
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002164 /* stop the trace while dumping */
2165 tracing_stop();
2166
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002167 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2168 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002169 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002170 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2171 }
2172 ring_buffer_read_prepare_sync();
2173 for_each_tracing_cpu(cpu) {
2174 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002175 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002176 }
2177 } else {
2178 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002179 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002180 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2181 ring_buffer_read_prepare_sync();
2182 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002183 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002184 }
2185
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002186 ret = seq_open(file, &tracer_seq_ops);
2187 if (ret < 0) {
2188 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002189 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002190 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002191
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002192 m = file->private_data;
2193 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002194
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002195 mutex_unlock(&trace_types_lock);
2196
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002197 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002198
2199 fail_buffer:
2200 for_each_tracing_cpu(cpu) {
2201 if (iter->buffer_iter[cpu])
2202 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2203 }
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002204 free_cpumask_var(iter->started);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002205 tracing_start();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002206 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002207 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002208 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01002209 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002210
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002211 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002212}
2213
2214int tracing_open_generic(struct inode *inode, struct file *filp)
2215{
Steven Rostedt60a11772008-05-12 21:20:44 +02002216 if (tracing_disabled)
2217 return -ENODEV;
2218
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002219 filp->private_data = inode->i_private;
2220 return 0;
2221}
2222
Hannes Eder4fd27352009-02-10 19:44:12 +01002223static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002224{
matt mooney907f2782010-09-27 19:04:53 -07002225 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002226 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002227 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002228
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002229 if (!(file->f_mode & FMODE_READ))
2230 return 0;
2231
2232 iter = m->private;
2233
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002234 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002235 for_each_tracing_cpu(cpu) {
2236 if (iter->buffer_iter[cpu])
2237 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2238 }
2239
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002240 if (iter->trace && iter->trace->close)
2241 iter->trace->close(iter);
2242
2243 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05002244 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002245 mutex_unlock(&trace_types_lock);
2246
2247 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002248 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002249 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002250 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002251 kfree(iter);
2252 return 0;
2253}
2254
2255static int tracing_open(struct inode *inode, struct file *file)
2256{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002257 struct trace_iterator *iter;
2258 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002259
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002260 /* If this file was open for write, then erase contents */
2261 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002262 (file->f_flags & O_TRUNC)) {
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002263 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002264
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002265 if (cpu == TRACE_PIPE_ALL_CPU)
2266 tracing_reset_online_cpus(&global_trace);
2267 else
2268 tracing_reset(&global_trace, cpu);
2269 }
2270
2271 if (file->f_mode & FMODE_READ) {
2272 iter = __tracing_open(inode, file);
2273 if (IS_ERR(iter))
2274 ret = PTR_ERR(iter);
2275 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2276 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2277 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002278 return ret;
2279}
2280
Ingo Molnare309b412008-05-12 21:20:51 +02002281static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002282t_next(struct seq_file *m, void *v, loff_t *pos)
2283{
Li Zefanf129e962009-06-24 09:53:44 +08002284 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002285
2286 (*pos)++;
2287
2288 if (t)
2289 t = t->next;
2290
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002291 return t;
2292}
2293
2294static void *t_start(struct seq_file *m, loff_t *pos)
2295{
Li Zefanf129e962009-06-24 09:53:44 +08002296 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002297 loff_t l = 0;
2298
2299 mutex_lock(&trace_types_lock);
Li Zefanf129e962009-06-24 09:53:44 +08002300 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002301 ;
2302
2303 return t;
2304}
2305
2306static void t_stop(struct seq_file *m, void *p)
2307{
2308 mutex_unlock(&trace_types_lock);
2309}
2310
2311static int t_show(struct seq_file *m, void *v)
2312{
2313 struct tracer *t = v;
2314
2315 if (!t)
2316 return 0;
2317
2318 seq_printf(m, "%s", t->name);
2319 if (t->next)
2320 seq_putc(m, ' ');
2321 else
2322 seq_putc(m, '\n');
2323
2324 return 0;
2325}
2326
James Morris88e9d342009-09-22 16:43:43 -07002327static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002328 .start = t_start,
2329 .next = t_next,
2330 .stop = t_stop,
2331 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002332};
2333
2334static int show_traces_open(struct inode *inode, struct file *file)
2335{
Steven Rostedt60a11772008-05-12 21:20:44 +02002336 if (tracing_disabled)
2337 return -ENODEV;
2338
Li Zefanf129e962009-06-24 09:53:44 +08002339 return seq_open(file, &show_traces_seq_ops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002340}
2341
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002342static ssize_t
2343tracing_write_stub(struct file *filp, const char __user *ubuf,
2344 size_t count, loff_t *ppos)
2345{
2346 return count;
2347}
2348
Slava Pestov364829b2010-11-24 15:13:16 -08002349static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2350{
2351 if (file->f_mode & FMODE_READ)
2352 return seq_lseek(file, offset, origin);
2353 else
2354 return 0;
2355}
2356
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002357static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002358 .open = tracing_open,
2359 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002360 .write = tracing_write_stub,
Slava Pestov364829b2010-11-24 15:13:16 -08002361 .llseek = tracing_seek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002362 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363};
2364
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002365static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002366 .open = show_traces_open,
2367 .read = seq_read,
2368 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002369 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02002370};
2371
Ingo Molnar36dfe922008-05-12 21:20:52 +02002372/*
2373 * Only trace on a CPU if the bitmask is set:
2374 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302375static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002376
2377/*
2378 * The tracer itself will not take this lock, but still we want
2379 * to provide a consistent cpumask to user-space:
2380 */
2381static DEFINE_MUTEX(tracing_cpumask_update_lock);
2382
2383/*
2384 * Temporary storage for the character representation of the
2385 * CPU bitmask (and one more byte for the newline):
2386 */
2387static char mask_str[NR_CPUS + 1];
2388
Ingo Molnarc7078de2008-05-12 21:20:52 +02002389static ssize_t
2390tracing_cpumask_read(struct file *filp, char __user *ubuf,
2391 size_t count, loff_t *ppos)
2392{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002393 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002394
2395 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002396
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302397 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002398 if (count - len < 2) {
2399 count = -EINVAL;
2400 goto out_err;
2401 }
2402 len += sprintf(mask_str + len, "\n");
2403 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2404
2405out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002406 mutex_unlock(&tracing_cpumask_update_lock);
2407
2408 return count;
2409}
2410
2411static ssize_t
2412tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2413 size_t count, loff_t *ppos)
2414{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002415 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302416 cpumask_var_t tracing_cpumask_new;
2417
2418 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2419 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002420
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302421 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002422 if (err)
2423 goto err_unlock;
2424
Li Zefan215368e2009-06-15 10:56:42 +08002425 mutex_lock(&tracing_cpumask_update_lock);
2426
Steven Rostedta5e25882008-12-02 15:34:05 -05002427 local_irq_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002428 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002429 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002430 /*
2431 * Increase/decrease the disabled counter if we are
2432 * about to flip a bit in the cpumask:
2433 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302434 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2435 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002436 atomic_inc(&global_trace.data[cpu]->disabled);
2437 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302438 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2439 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002440 atomic_dec(&global_trace.data[cpu]->disabled);
2441 }
2442 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002443 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002444 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002445
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302446 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002447
Ingo Molnarc7078de2008-05-12 21:20:52 +02002448 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302449 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002450
Ingo Molnarc7078de2008-05-12 21:20:52 +02002451 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002452
2453err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08002454 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002455
2456 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002457}
2458
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002459static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002460 .open = tracing_open_generic,
2461 .read = tracing_cpumask_read,
2462 .write = tracing_cpumask_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002463 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002464};
2465
Li Zefanfdb372e2009-12-08 11:15:59 +08002466static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002467{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002468 struct tracer_opt *trace_opts;
2469 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002470 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002471
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002472 mutex_lock(&trace_types_lock);
2473 tracer_flags = current_trace->flags->val;
2474 trace_opts = current_trace->flags->opts;
2475
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002476 for (i = 0; trace_options[i]; i++) {
2477 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08002478 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002479 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002480 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002481 }
2482
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002483 for (i = 0; trace_opts[i].name; i++) {
2484 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08002485 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002486 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002487 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002488 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002489 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002490
Li Zefanfdb372e2009-12-08 11:15:59 +08002491 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002492}
2493
Li Zefan8d18eaa2009-12-08 11:17:06 +08002494static int __set_tracer_option(struct tracer *trace,
2495 struct tracer_flags *tracer_flags,
2496 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002497{
Li Zefan8d18eaa2009-12-08 11:17:06 +08002498 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002499
Li Zefan8d18eaa2009-12-08 11:17:06 +08002500 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002501 if (ret)
2502 return ret;
2503
2504 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08002505 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002506 else
Zhaolei77708412009-08-07 18:53:21 +08002507 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002508 return 0;
2509}
2510
Li Zefan8d18eaa2009-12-08 11:17:06 +08002511/* Try to assign a tracer specific option */
2512static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2513{
2514 struct tracer_flags *tracer_flags = trace->flags;
2515 struct tracer_opt *opts = NULL;
2516 int i;
2517
2518 for (i = 0; tracer_flags->opts[i].name; i++) {
2519 opts = &tracer_flags->opts[i];
2520
2521 if (strcmp(cmp, opts->name) == 0)
2522 return __set_tracer_option(trace, trace->flags,
2523 opts, neg);
2524 }
2525
2526 return -EINVAL;
2527}
2528
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002529static void set_tracer_flags(unsigned int mask, int enabled)
2530{
2531 /* do nothing if flag is already set */
2532 if (!!(trace_flags & mask) == !!enabled)
2533 return;
2534
2535 if (enabled)
2536 trace_flags |= mask;
2537 else
2538 trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08002539
2540 if (mask == TRACE_ITER_RECORD_CMD)
2541 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08002542
2543 if (mask == TRACE_ITER_OVERWRITE)
2544 ring_buffer_change_overwrite(global_trace.buffer, enabled);
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002545}
2546
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002547static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002548tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002549 size_t cnt, loff_t *ppos)
2550{
2551 char buf[64];
Li Zefan8d18eaa2009-12-08 11:17:06 +08002552 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002553 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002554 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002555 int i;
2556
Steven Rostedtcffae432008-05-12 21:21:00 +02002557 if (cnt >= sizeof(buf))
2558 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002559
2560 if (copy_from_user(&buf, ubuf, cnt))
2561 return -EFAULT;
2562
2563 buf[cnt] = 0;
Li Zefan8d18eaa2009-12-08 11:17:06 +08002564 cmp = strstrip(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002565
Li Zefan8d18eaa2009-12-08 11:17:06 +08002566 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002567 neg = 1;
2568 cmp += 2;
2569 }
2570
2571 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08002572 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002573 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002574 break;
2575 }
2576 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002577
2578 /* If no option could be set, test the specific tracer options */
2579 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002580 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002581 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002582 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002583 if (ret)
2584 return ret;
2585 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002586
Jiri Olsacf8517c2009-10-23 19:36:16 -04002587 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002588
2589 return cnt;
2590}
2591
Li Zefanfdb372e2009-12-08 11:15:59 +08002592static int tracing_trace_options_open(struct inode *inode, struct file *file)
2593{
2594 if (tracing_disabled)
2595 return -ENODEV;
2596 return single_open(file, tracing_trace_options_show, NULL);
2597}
2598
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002599static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08002600 .open = tracing_trace_options_open,
2601 .read = seq_read,
2602 .llseek = seq_lseek,
2603 .release = single_release,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002604 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002605};
2606
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002607static const char readme_msg[] =
2608 "tracing mini-HOWTO:\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002609 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2610 "# cat /sys/kernel/debug/tracing/available_tracers\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302611 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002612 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302613 "nop\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002614 "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2615 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002616 "sched_switch\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002617 "# cat /sys/kernel/debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002618 "noprint-parent nosym-offset nosym-addr noverbose\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002619 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2620 "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2621 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2622 "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002623;
2624
2625static ssize_t
2626tracing_readme_read(struct file *filp, char __user *ubuf,
2627 size_t cnt, loff_t *ppos)
2628{
2629 return simple_read_from_buffer(ubuf, cnt, ppos,
2630 readme_msg, strlen(readme_msg));
2631}
2632
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002633static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002634 .open = tracing_open_generic,
2635 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002636 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002637};
2638
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002639static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04002640tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2641 size_t cnt, loff_t *ppos)
2642{
2643 char *buf_comm;
2644 char *file_buf;
2645 char *buf;
2646 int len = 0;
2647 int pid;
2648 int i;
2649
2650 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2651 if (!file_buf)
2652 return -ENOMEM;
2653
2654 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2655 if (!buf_comm) {
2656 kfree(file_buf);
2657 return -ENOMEM;
2658 }
2659
2660 buf = file_buf;
2661
2662 for (i = 0; i < SAVED_CMDLINES; i++) {
2663 int r;
2664
2665 pid = map_cmdline_to_pid[i];
2666 if (pid == -1 || pid == NO_CMDLINE_MAP)
2667 continue;
2668
2669 trace_find_cmdline(pid, buf_comm);
2670 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2671 buf += r;
2672 len += r;
2673 }
2674
2675 len = simple_read_from_buffer(ubuf, cnt, ppos,
2676 file_buf, len);
2677
2678 kfree(file_buf);
2679 kfree(buf_comm);
2680
2681 return len;
2682}
2683
2684static const struct file_operations tracing_saved_cmdlines_fops = {
2685 .open = tracing_open_generic,
2686 .read = tracing_saved_cmdlines_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002687 .llseek = generic_file_llseek,
Avadh Patel69abe6a2009-04-10 16:04:48 -04002688};
2689
2690static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002691tracing_ctrl_read(struct file *filp, char __user *ubuf,
2692 size_t cnt, loff_t *ppos)
2693{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002694 char buf[64];
2695 int r;
2696
Steven Rostedt90369902008-11-05 16:05:44 -05002697 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002698 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002699}
2700
2701static ssize_t
2702tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2703 size_t cnt, loff_t *ppos)
2704{
2705 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002706 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002707 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002708 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002709
Steven Rostedtcffae432008-05-12 21:21:00 +02002710 if (cnt >= sizeof(buf))
2711 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002712
2713 if (copy_from_user(&buf, ubuf, cnt))
2714 return -EFAULT;
2715
2716 buf[cnt] = 0;
2717
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002718 ret = strict_strtoul(buf, 10, &val);
2719 if (ret < 0)
2720 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002721
2722 val = !!val;
2723
2724 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002725 if (tracer_enabled ^ val) {
Steven Rostedt6752ab42011-02-08 13:54:06 -05002726
2727 /* Only need to warn if this is used to change the state */
2728 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
2729
Steven Rostedt90369902008-11-05 16:05:44 -05002730 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002731 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002732 if (current_trace->start)
2733 current_trace->start(tr);
2734 tracing_start();
2735 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002736 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002737 tracing_stop();
2738 if (current_trace->stop)
2739 current_trace->stop(tr);
2740 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002741 }
2742 mutex_unlock(&trace_types_lock);
2743
Jiri Olsacf8517c2009-10-23 19:36:16 -04002744 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002745
2746 return cnt;
2747}
2748
2749static ssize_t
2750tracing_set_trace_read(struct file *filp, char __user *ubuf,
2751 size_t cnt, loff_t *ppos)
2752{
Li Zefanee6c2c12009-09-18 14:06:47 +08002753 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002754 int r;
2755
2756 mutex_lock(&trace_types_lock);
2757 if (current_trace)
2758 r = sprintf(buf, "%s\n", current_trace->name);
2759 else
2760 r = sprintf(buf, "\n");
2761 mutex_unlock(&trace_types_lock);
2762
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002763 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002764}
2765
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002766int tracer_init(struct tracer *t, struct trace_array *tr)
2767{
2768 tracing_reset_online_cpus(tr);
2769 return t->init(tr);
2770}
2771
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07002772static int __tracing_resize_ring_buffer(unsigned long size)
Steven Rostedt73c51622009-03-11 13:42:01 -04002773{
2774 int ret;
2775
2776 /*
2777 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002778 * we use the size that was given, and we can forget about
2779 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002780 */
2781 ring_buffer_expanded = 1;
2782
2783 ret = ring_buffer_resize(global_trace.buffer, size);
2784 if (ret < 0)
2785 return ret;
2786
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002787 if (!current_trace->use_max_tr)
2788 goto out;
2789
Steven Rostedt73c51622009-03-11 13:42:01 -04002790 ret = ring_buffer_resize(max_tr.buffer, size);
2791 if (ret < 0) {
2792 int r;
2793
2794 r = ring_buffer_resize(global_trace.buffer,
2795 global_trace.entries);
2796 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002797 /*
2798 * AARGH! We are left with different
2799 * size max buffer!!!!
2800 * The max buffer is our "snapshot" buffer.
2801 * When a tracer needs a snapshot (one of the
2802 * latency tracers), it swaps the max buffer
2803 * with the saved snap shot. We succeeded to
2804 * update the size of the main buffer, but failed to
2805 * update the size of the max buffer. But when we tried
2806 * to reset the main buffer to the original size, we
2807 * failed there too. This is very unlikely to
2808 * happen, but if it does, warn and kill all
2809 * tracing.
2810 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002811 WARN_ON(1);
2812 tracing_disabled = 1;
2813 }
2814 return ret;
2815 }
2816
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002817 max_tr.entries = size;
2818 out:
Steven Rostedt73c51622009-03-11 13:42:01 -04002819 global_trace.entries = size;
2820
2821 return ret;
2822}
2823
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07002824static ssize_t tracing_resize_ring_buffer(unsigned long size)
2825{
2826 int cpu, ret = size;
2827
2828 mutex_lock(&trace_types_lock);
2829
2830 tracing_stop();
2831
2832 /* disable all cpu buffers */
2833 for_each_tracing_cpu(cpu) {
2834 if (global_trace.data[cpu])
2835 atomic_inc(&global_trace.data[cpu]->disabled);
2836 if (max_tr.data[cpu])
2837 atomic_inc(&max_tr.data[cpu]->disabled);
2838 }
2839
2840 if (size != global_trace.entries)
2841 ret = __tracing_resize_ring_buffer(size);
2842
2843 if (ret < 0)
2844 ret = -ENOMEM;
2845
2846 for_each_tracing_cpu(cpu) {
2847 if (global_trace.data[cpu])
2848 atomic_dec(&global_trace.data[cpu]->disabled);
2849 if (max_tr.data[cpu])
2850 atomic_dec(&max_tr.data[cpu]->disabled);
2851 }
2852
2853 tracing_start();
2854 mutex_unlock(&trace_types_lock);
2855
2856 return ret;
2857}
2858
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002859
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002860/**
2861 * tracing_update_buffers - used by tracing facility to expand ring buffers
2862 *
2863 * To save on memory when the tracing is never used on a system with it
2864 * configured in. The ring buffers are set to a minimum size. But once
2865 * a user starts to use the tracing facility, then they need to grow
2866 * to their default size.
2867 *
2868 * This function is to be called when a tracer is about to be used.
2869 */
2870int tracing_update_buffers(void)
2871{
2872 int ret = 0;
2873
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002874 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002875 if (!ring_buffer_expanded)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07002876 ret = __tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002877 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002878
2879 return ret;
2880}
2881
Steven Rostedt577b7852009-02-26 23:43:05 -05002882struct trace_option_dentry;
2883
2884static struct trace_option_dentry *
2885create_trace_option_files(struct tracer *tracer);
2886
2887static void
2888destroy_trace_option_files(struct trace_option_dentry *topts);
2889
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002890static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002891{
Steven Rostedt577b7852009-02-26 23:43:05 -05002892 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002893 struct trace_array *tr = &global_trace;
2894 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002895 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002896
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002897 mutex_lock(&trace_types_lock);
2898
Steven Rostedt73c51622009-03-11 13:42:01 -04002899 if (!ring_buffer_expanded) {
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07002900 ret = __tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt73c51622009-03-11 13:42:01 -04002901 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01002902 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04002903 ret = 0;
2904 }
2905
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002906 for (t = trace_types; t; t = t->next) {
2907 if (strcmp(t->name, buf) == 0)
2908 break;
2909 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002910 if (!t) {
2911 ret = -EINVAL;
2912 goto out;
2913 }
2914 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002915 goto out;
2916
Steven Rostedt9f029e82008-11-12 15:24:24 -05002917 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002918 if (current_trace && current_trace->reset)
2919 current_trace->reset(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002920 if (current_trace && current_trace->use_max_tr) {
2921 /*
2922 * We don't free the ring buffer. instead, resize it because
2923 * The max_tr ring buffer has some state (e.g. ring->clock) and
2924 * we want preserve it.
2925 */
2926 ring_buffer_resize(max_tr.buffer, 1);
2927 max_tr.entries = 1;
2928 }
Steven Rostedt577b7852009-02-26 23:43:05 -05002929 destroy_trace_option_files(topts);
2930
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002931 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002932
2933 topts = create_trace_option_files(current_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09002934 if (current_trace->use_max_tr) {
2935 ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
2936 if (ret < 0)
2937 goto out;
2938 max_tr.entries = global_trace.entries;
2939 }
Steven Rostedt577b7852009-02-26 23:43:05 -05002940
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002941 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002942 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002943 if (ret)
2944 goto out;
2945 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002946
Steven Rostedt9f029e82008-11-12 15:24:24 -05002947 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002948 out:
2949 mutex_unlock(&trace_types_lock);
2950
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002951 return ret;
2952}
2953
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002954static ssize_t
2955tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2956 size_t cnt, loff_t *ppos)
2957{
Li Zefanee6c2c12009-09-18 14:06:47 +08002958 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002959 int i;
2960 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002961 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002962
Steven Rostedt60063a62008-10-28 10:44:24 -04002963 ret = cnt;
2964
Li Zefanee6c2c12009-09-18 14:06:47 +08002965 if (cnt > MAX_TRACER_SIZE)
2966 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002967
2968 if (copy_from_user(&buf, ubuf, cnt))
2969 return -EFAULT;
2970
2971 buf[cnt] = 0;
2972
2973 /* strip ending whitespace. */
2974 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2975 buf[i] = 0;
2976
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002977 err = tracing_set_tracer(buf);
2978 if (err)
2979 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002980
Jiri Olsacf8517c2009-10-23 19:36:16 -04002981 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002982
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002983 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002984}
2985
2986static ssize_t
2987tracing_max_lat_read(struct file *filp, char __user *ubuf,
2988 size_t cnt, loff_t *ppos)
2989{
2990 unsigned long *ptr = filp->private_data;
2991 char buf[64];
2992 int r;
2993
Steven Rostedtcffae432008-05-12 21:21:00 +02002994 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002995 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002996 if (r > sizeof(buf))
2997 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002998 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002999}
3000
3001static ssize_t
3002tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3003 size_t cnt, loff_t *ppos)
3004{
Hannes Eder5e398412009-02-10 19:44:34 +01003005 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003006 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01003007 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003008 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003009
Steven Rostedtcffae432008-05-12 21:21:00 +02003010 if (cnt >= sizeof(buf))
3011 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003012
3013 if (copy_from_user(&buf, ubuf, cnt))
3014 return -EFAULT;
3015
3016 buf[cnt] = 0;
3017
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003018 ret = strict_strtoul(buf, 10, &val);
3019 if (ret < 0)
3020 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003021
3022 *ptr = val * 1000;
3023
3024 return cnt;
3025}
3026
Steven Rostedtb3806b42008-05-12 21:20:46 +02003027static int tracing_open_pipe(struct inode *inode, struct file *filp)
3028{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003029 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003030 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003031 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003032
3033 if (tracing_disabled)
3034 return -ENODEV;
3035
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003036 mutex_lock(&trace_types_lock);
3037
Steven Rostedtb3806b42008-05-12 21:20:46 +02003038 /* create a buffer to store the information to pass to userspace */
3039 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003040 if (!iter) {
3041 ret = -ENOMEM;
3042 goto out;
3043 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003044
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003045 /*
3046 * We make a copy of the current tracer to avoid concurrent
3047 * changes on it while we are reading.
3048 */
3049 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3050 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003051 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003052 goto fail;
3053 }
3054 if (current_trace)
3055 *iter->trace = *current_trace;
3056
3057 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3058 ret = -ENOMEM;
3059 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10303060 }
3061
Steven Rostedta3097202008-11-07 22:36:02 -05003062 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10303063 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05003064
Steven Rostedt112f38a72009-06-01 15:16:05 -04003065 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3066 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3067
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003068 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003069 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003070 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003071 filp->private_data = iter;
3072
Steven Rostedt107bad82008-05-12 21:21:01 +02003073 if (iter->trace->pipe_open)
3074 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02003075
Arnd Bergmannb4447862010-07-07 23:40:11 +02003076 nonseekable_open(inode, filp);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003077out:
3078 mutex_unlock(&trace_types_lock);
3079 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003080
3081fail:
3082 kfree(iter->trace);
3083 kfree(iter);
3084 mutex_unlock(&trace_types_lock);
3085 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003086}
3087
3088static int tracing_release_pipe(struct inode *inode, struct file *file)
3089{
3090 struct trace_iterator *iter = file->private_data;
3091
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003092 mutex_lock(&trace_types_lock);
3093
Steven Rostedt29bf4a52009-12-09 12:37:43 -05003094 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05003095 iter->trace->pipe_close(iter);
3096
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003097 mutex_unlock(&trace_types_lock);
3098
Rusty Russell44623442009-01-01 10:12:23 +10303099 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003100 mutex_destroy(&iter->mutex);
3101 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003102 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003103
3104 return 0;
3105}
3106
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003107static unsigned int
3108tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3109{
3110 struct trace_iterator *iter = filp->private_data;
3111
3112 if (trace_flags & TRACE_ITER_BLOCK) {
3113 /*
3114 * Always select as readable when in blocking mode
3115 */
3116 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02003117 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003118 if (!trace_empty(iter))
3119 return POLLIN | POLLRDNORM;
3120 poll_wait(filp, &trace_wait, poll_table);
3121 if (!trace_empty(iter))
3122 return POLLIN | POLLRDNORM;
3123
3124 return 0;
3125 }
3126}
3127
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003128
3129void default_wait_pipe(struct trace_iterator *iter)
3130{
3131 DEFINE_WAIT(wait);
3132
3133 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3134
3135 if (trace_empty(iter))
3136 schedule();
3137
3138 finish_wait(&trace_wait, &wait);
3139}
3140
3141/*
3142 * This is a make-shift waitqueue.
3143 * A tracer might use this callback on some rare cases:
3144 *
3145 * 1) the current tracer might hold the runqueue lock when it wakes up
3146 * a reader, hence a deadlock (sched, function, and function graph tracers)
3147 * 2) the function tracers, trace all functions, we don't want
3148 * the overhead of calling wake_up and friends
3149 * (and tracing them too)
3150 *
3151 * Anyway, this is really very primitive wakeup.
3152 */
3153void poll_wait_pipe(struct trace_iterator *iter)
3154{
3155 set_current_state(TASK_INTERRUPTIBLE);
3156 /* sleep for 100 msecs, and try again. */
3157 schedule_timeout(HZ / 10);
3158}
3159
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003160/* Must be called with trace_types_lock mutex held. */
3161static int tracing_wait_pipe(struct file *filp)
3162{
3163 struct trace_iterator *iter = filp->private_data;
3164
3165 while (trace_empty(iter)) {
3166
3167 if ((filp->f_flags & O_NONBLOCK)) {
3168 return -EAGAIN;
3169 }
3170
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003171 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003172
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003173 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003174
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003175 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003176
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003177 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003178 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003179
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003180 /*
3181 * We block until we read something and tracing is disabled.
3182 * We still block if tracing is disabled, but we have never
3183 * read anything. This allows a user to cat this file, and
3184 * then enable tracing. But after we have read something,
3185 * we give an EOF when tracing is again disabled.
3186 *
3187 * iter->pos will be 0 if we haven't read anything.
3188 */
3189 if (!tracer_enabled && iter->pos)
3190 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003191 }
3192
3193 return 1;
3194}
3195
Steven Rostedtb3806b42008-05-12 21:20:46 +02003196/*
3197 * Consumer reader.
3198 */
3199static ssize_t
3200tracing_read_pipe(struct file *filp, char __user *ubuf,
3201 size_t cnt, loff_t *ppos)
3202{
3203 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003204 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003205 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003206
3207 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003208 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3209 if (sret != -EBUSY)
3210 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003211
Steven Rostedtf9520752009-03-02 14:04:40 -05003212 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003213
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003214 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02003215 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003216 if (unlikely(old_tracer != current_trace && current_trace)) {
3217 old_tracer = current_trace;
3218 *iter->trace = *current_trace;
3219 }
3220 mutex_unlock(&trace_types_lock);
3221
3222 /*
3223 * Avoid more than one consumer on a single file descriptor
3224 * This is just a matter of traces coherency, the ring buffer itself
3225 * is protected.
3226 */
3227 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003228 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003229 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3230 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02003231 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02003232 }
3233
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003234waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003235 sret = tracing_wait_pipe(filp);
3236 if (sret <= 0)
3237 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003238
3239 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003240 if (trace_empty(iter)) {
3241 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003242 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003243 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003244
3245 if (cnt >= PAGE_SIZE)
3246 cnt = PAGE_SIZE - 1;
3247
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003248 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003249 memset(&iter->seq, 0,
3250 sizeof(struct trace_iterator) -
3251 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02003252 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003253
Lai Jiangshan4f535962009-05-18 19:35:34 +08003254 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003255 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05003256 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003257 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003258 int len = iter->seq.len;
3259
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003260 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003261 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02003262 /* don't print partial lines */
3263 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003264 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003265 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01003266 if (ret != TRACE_TYPE_NO_CONSUME)
3267 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003268
3269 if (iter->seq.len >= cnt)
3270 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01003271
3272 /*
3273 * Setting the full flag means we reached the trace_seq buffer
3274 * size and we should leave by partial output condition above.
3275 * One of the trace_seq_* functions is not used properly.
3276 */
3277 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3278 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003279 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003280 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003281 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02003282
Steven Rostedtb3806b42008-05-12 21:20:46 +02003283 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003284 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3285 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05003286 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003287
3288 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003289 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003290 * entries, go back to wait for more entries.
3291 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003292 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003293 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003294
Steven Rostedt107bad82008-05-12 21:21:01 +02003295out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003296 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003297
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003298 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003299}
3300
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003301static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3302 struct pipe_buffer *buf)
3303{
3304 __free_page(buf->page);
3305}
3306
3307static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3308 unsigned int idx)
3309{
3310 __free_page(spd->pages[idx]);
3311}
3312
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003313static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003314 .can_merge = 0,
3315 .map = generic_pipe_buf_map,
3316 .unmap = generic_pipe_buf_unmap,
3317 .confirm = generic_pipe_buf_confirm,
3318 .release = tracing_pipe_buf_release,
3319 .steal = generic_pipe_buf_steal,
3320 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003321};
3322
Steven Rostedt34cd4992009-02-09 12:06:29 -05003323static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003324tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003325{
3326 size_t count;
3327 int ret;
3328
3329 /* Seq buffer is page-sized, exactly what we need. */
3330 for (;;) {
3331 count = iter->seq.len;
3332 ret = print_trace_line(iter);
3333 count = iter->seq.len - count;
3334 if (rem < count) {
3335 rem = 0;
3336 iter->seq.len -= count;
3337 break;
3338 }
3339 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3340 iter->seq.len -= count;
3341 break;
3342 }
3343
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08003344 if (ret != TRACE_TYPE_NO_CONSUME)
3345 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003346 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05003347 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003348 rem = 0;
3349 iter->ent = NULL;
3350 break;
3351 }
3352 }
3353
3354 return rem;
3355}
3356
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003357static ssize_t tracing_splice_read_pipe(struct file *filp,
3358 loff_t *ppos,
3359 struct pipe_inode_info *pipe,
3360 size_t len,
3361 unsigned int flags)
3362{
Jens Axboe35f3d142010-05-20 10:43:18 +02003363 struct page *pages_def[PIPE_DEF_BUFFERS];
3364 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003365 struct trace_iterator *iter = filp->private_data;
3366 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02003367 .pages = pages_def,
3368 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05003369 .nr_pages = 0, /* This gets updated below. */
3370 .flags = flags,
3371 .ops = &tracing_pipe_buf_ops,
3372 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003373 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003374 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003375 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003376 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003377 unsigned int i;
3378
Jens Axboe35f3d142010-05-20 10:43:18 +02003379 if (splice_grow_spd(pipe, &spd))
3380 return -ENOMEM;
3381
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003382 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003383 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003384 if (unlikely(old_tracer != current_trace && current_trace)) {
3385 old_tracer = current_trace;
3386 *iter->trace = *current_trace;
3387 }
3388 mutex_unlock(&trace_types_lock);
3389
3390 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003391
3392 if (iter->trace->splice_read) {
3393 ret = iter->trace->splice_read(iter, filp,
3394 ppos, pipe, len, flags);
3395 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003396 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003397 }
3398
3399 ret = tracing_wait_pipe(filp);
3400 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003401 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003402
Jason Wessel955b61e2010-08-05 09:22:23 -05003403 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003404 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003405 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003406 }
3407
Lai Jiangshan4f535962009-05-18 19:35:34 +08003408 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003409 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003410
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003411 /* Fill as many pages as possible. */
Jens Axboe35f3d142010-05-20 10:43:18 +02003412 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3413 spd.pages[i] = alloc_page(GFP_KERNEL);
3414 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05003415 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003416
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003417 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003418
3419 /* Copy the data into the page, so we can start over. */
3420 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02003421 page_address(spd.pages[i]),
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003422 iter->seq.len);
3423 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02003424 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003425 break;
3426 }
Jens Axboe35f3d142010-05-20 10:43:18 +02003427 spd.partial[i].offset = 0;
3428 spd.partial[i].len = iter->seq.len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003429
Steven Rostedtf9520752009-03-02 14:04:40 -05003430 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003431 }
3432
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003433 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003434 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003435 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003436
3437 spd.nr_pages = i;
3438
Jens Axboe35f3d142010-05-20 10:43:18 +02003439 ret = splice_to_pipe(pipe, &spd);
3440out:
3441 splice_shrink_spd(pipe, &spd);
3442 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003443
Steven Rostedt34cd4992009-02-09 12:06:29 -05003444out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003445 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02003446 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003447}
3448
Steven Rostedta98a3c32008-05-12 21:20:59 +02003449static ssize_t
3450tracing_entries_read(struct file *filp, char __user *ubuf,
3451 size_t cnt, loff_t *ppos)
3452{
3453 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003454 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003455 int r;
3456
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003457 mutex_lock(&trace_types_lock);
3458 if (!ring_buffer_expanded)
3459 r = sprintf(buf, "%lu (expanded: %lu)\n",
3460 tr->entries >> 10,
3461 trace_buf_size >> 10);
3462 else
3463 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3464 mutex_unlock(&trace_types_lock);
3465
Steven Rostedta98a3c32008-05-12 21:20:59 +02003466 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3467}
3468
3469static ssize_t
3470tracing_entries_write(struct file *filp, const char __user *ubuf,
3471 size_t cnt, loff_t *ppos)
3472{
3473 unsigned long val;
3474 char buf[64];
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003475 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003476
Steven Rostedtcffae432008-05-12 21:21:00 +02003477 if (cnt >= sizeof(buf))
3478 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003479
3480 if (copy_from_user(&buf, ubuf, cnt))
3481 return -EFAULT;
3482
3483 buf[cnt] = 0;
3484
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003485 ret = strict_strtoul(buf, 10, &val);
3486 if (ret < 0)
3487 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003488
3489 /* must have at least 1 entry */
3490 if (!val)
3491 return -EINVAL;
3492
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003493 /* value is in KB */
3494 val <<= 10;
3495
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003496 ret = tracing_resize_ring_buffer(val);
3497 if (ret < 0)
3498 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003499
Jiri Olsacf8517c2009-10-23 19:36:16 -04003500 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003501
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003502 return cnt;
3503}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003504
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003505static ssize_t
3506tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3507 size_t cnt, loff_t *ppos)
3508{
3509 /*
3510 * There is no need to read what the user has written, this function
3511 * is just to make sure that there is no error when "echo" is used
3512 */
3513
3514 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003515
3516 return cnt;
3517}
3518
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003519static int
3520tracing_free_buffer_release(struct inode *inode, struct file *filp)
3521{
Steven Rostedtcf30cf62011-06-14 22:44:07 -04003522 /* disable tracing ? */
3523 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3524 tracing_off();
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003525 /* resize the ring buffer to 0 */
3526 tracing_resize_ring_buffer(0);
3527
3528 return 0;
3529}
3530
Carsten Emdef2942482009-12-06 14:02:44 +01003531static int mark_printk(const char *fmt, ...)
3532{
3533 int ret;
3534 va_list args;
3535 va_start(args, fmt);
3536 ret = trace_vprintk(0, fmt, args);
3537 va_end(args);
3538 return ret;
3539}
3540
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003541static ssize_t
3542tracing_mark_write(struct file *filp, const char __user *ubuf,
3543 size_t cnt, loff_t *fpos)
3544{
3545 char *buf;
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003546 size_t written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003547
Steven Rostedtc76f0692008-11-07 22:36:02 -05003548 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003549 return -EINVAL;
3550
3551 if (cnt > TRACE_BUF_SIZE)
3552 cnt = TRACE_BUF_SIZE;
3553
Carsten Emdec13d2f72009-11-16 20:56:13 +01003554 buf = kmalloc(cnt + 2, GFP_KERNEL);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003555 if (buf == NULL)
3556 return -ENOMEM;
3557
3558 if (copy_from_user(buf, ubuf, cnt)) {
3559 kfree(buf);
3560 return -EFAULT;
3561 }
Carsten Emdec13d2f72009-11-16 20:56:13 +01003562 if (buf[cnt-1] != '\n') {
3563 buf[cnt] = '\n';
3564 buf[cnt+1] = '\0';
3565 } else
3566 buf[cnt] = '\0';
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003567
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003568 written = mark_printk("%s", buf);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003569 kfree(buf);
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003570 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003571
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02003572 /* don't tell userspace we wrote more - it might confuse them */
3573 if (written > cnt)
3574 written = cnt;
3575
3576 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003577}
3578
Li Zefan13f16d22009-12-08 11:16:11 +08003579static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08003580{
Zhaolei5079f322009-08-25 16:12:56 +08003581 int i;
3582
3583 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08003584 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08003585 "%s%s%s%s", i ? " " : "",
3586 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
3587 i == trace_clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08003588 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08003589
Li Zefan13f16d22009-12-08 11:16:11 +08003590 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08003591}
3592
3593static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
3594 size_t cnt, loff_t *fpos)
3595{
3596 char buf[64];
3597 const char *clockstr;
3598 int i;
3599
3600 if (cnt >= sizeof(buf))
3601 return -EINVAL;
3602
3603 if (copy_from_user(&buf, ubuf, cnt))
3604 return -EFAULT;
3605
3606 buf[cnt] = 0;
3607
3608 clockstr = strstrip(buf);
3609
3610 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
3611 if (strcmp(trace_clocks[i].name, clockstr) == 0)
3612 break;
3613 }
3614 if (i == ARRAY_SIZE(trace_clocks))
3615 return -EINVAL;
3616
3617 trace_clock_id = i;
3618
3619 mutex_lock(&trace_types_lock);
3620
3621 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
3622 if (max_tr.buffer)
3623 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
3624
3625 mutex_unlock(&trace_types_lock);
3626
3627 *fpos += cnt;
3628
3629 return cnt;
3630}
3631
Li Zefan13f16d22009-12-08 11:16:11 +08003632static int tracing_clock_open(struct inode *inode, struct file *file)
3633{
3634 if (tracing_disabled)
3635 return -ENODEV;
3636 return single_open(file, tracing_clock_show, NULL);
3637}
3638
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003639static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003640 .open = tracing_open_generic,
3641 .read = tracing_max_lat_read,
3642 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003643 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003644};
3645
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003646static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003647 .open = tracing_open_generic,
3648 .read = tracing_ctrl_read,
3649 .write = tracing_ctrl_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003650 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003651};
3652
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003653static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003654 .open = tracing_open_generic,
3655 .read = tracing_set_trace_read,
3656 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003657 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003658};
3659
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003660static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003661 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003662 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003663 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003664 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003665 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003666 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003667};
3668
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003669static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003670 .open = tracing_open_generic,
3671 .read = tracing_entries_read,
3672 .write = tracing_entries_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003673 .llseek = generic_file_llseek,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003674};
3675
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003676static const struct file_operations tracing_free_buffer_fops = {
3677 .write = tracing_free_buffer_write,
3678 .release = tracing_free_buffer_release,
3679};
3680
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003681static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003682 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003683 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003684 .llseek = generic_file_llseek,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003685};
3686
Zhaolei5079f322009-08-25 16:12:56 +08003687static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08003688 .open = tracing_clock_open,
3689 .read = seq_read,
3690 .llseek = seq_lseek,
3691 .release = single_release,
Zhaolei5079f322009-08-25 16:12:56 +08003692 .write = tracing_clock_write,
3693};
3694
Steven Rostedt2cadf912008-12-01 22:20:19 -05003695struct ftrace_buffer_info {
3696 struct trace_array *tr;
3697 void *spare;
3698 int cpu;
3699 unsigned int read;
3700};
3701
3702static int tracing_buffers_open(struct inode *inode, struct file *filp)
3703{
3704 int cpu = (int)(long)inode->i_private;
3705 struct ftrace_buffer_info *info;
3706
3707 if (tracing_disabled)
3708 return -ENODEV;
3709
3710 info = kzalloc(sizeof(*info), GFP_KERNEL);
3711 if (!info)
3712 return -ENOMEM;
3713
3714 info->tr = &global_trace;
3715 info->cpu = cpu;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003716 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003717 /* Force reading ring buffer for first read */
3718 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003719
3720 filp->private_data = info;
3721
Lai Jiangshand1e7e022009-04-02 15:16:56 +08003722 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003723}
3724
3725static ssize_t
3726tracing_buffers_read(struct file *filp, char __user *ubuf,
3727 size_t count, loff_t *ppos)
3728{
3729 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003730 ssize_t ret;
3731 size_t size;
3732
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003733 if (!count)
3734 return 0;
3735
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003736 if (!info->spare)
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003737 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003738 if (!info->spare)
3739 return -ENOMEM;
3740
Steven Rostedt2cadf912008-12-01 22:20:19 -05003741 /* Do we have previous read data to read? */
3742 if (info->read < PAGE_SIZE)
3743 goto read;
3744
3745 info->read = 0;
3746
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003747 trace_access_lock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003748 ret = ring_buffer_read_page(info->tr->buffer,
3749 &info->spare,
3750 count,
3751 info->cpu, 0);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003752 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003753 if (ret < 0)
3754 return 0;
3755
Steven Rostedt2cadf912008-12-01 22:20:19 -05003756read:
3757 size = PAGE_SIZE - info->read;
3758 if (size > count)
3759 size = count;
3760
3761 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003762 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003763 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003764 size -= ret;
3765
Steven Rostedt2cadf912008-12-01 22:20:19 -05003766 *ppos += size;
3767 info->read += size;
3768
3769 return size;
3770}
3771
3772static int tracing_buffers_release(struct inode *inode, struct file *file)
3773{
3774 struct ftrace_buffer_info *info = file->private_data;
3775
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003776 if (info->spare)
3777 ring_buffer_free_read_page(info->tr->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003778 kfree(info);
3779
3780 return 0;
3781}
3782
3783struct buffer_ref {
3784 struct ring_buffer *buffer;
3785 void *page;
3786 int ref;
3787};
3788
3789static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3790 struct pipe_buffer *buf)
3791{
3792 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3793
3794 if (--ref->ref)
3795 return;
3796
3797 ring_buffer_free_read_page(ref->buffer, ref->page);
3798 kfree(ref);
3799 buf->private = 0;
3800}
3801
3802static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3803 struct pipe_buffer *buf)
3804{
3805 return 1;
3806}
3807
3808static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3809 struct pipe_buffer *buf)
3810{
3811 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3812
3813 ref->ref++;
3814}
3815
3816/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003817static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003818 .can_merge = 0,
3819 .map = generic_pipe_buf_map,
3820 .unmap = generic_pipe_buf_unmap,
3821 .confirm = generic_pipe_buf_confirm,
3822 .release = buffer_pipe_buf_release,
3823 .steal = buffer_pipe_buf_steal,
3824 .get = buffer_pipe_buf_get,
3825};
3826
3827/*
3828 * Callback from splice_to_pipe(), if we need to release some pages
3829 * at the end of the spd in case we error'ed out in filling the pipe.
3830 */
3831static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3832{
3833 struct buffer_ref *ref =
3834 (struct buffer_ref *)spd->partial[i].private;
3835
3836 if (--ref->ref)
3837 return;
3838
3839 ring_buffer_free_read_page(ref->buffer, ref->page);
3840 kfree(ref);
3841 spd->partial[i].private = 0;
3842}
3843
3844static ssize_t
3845tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3846 struct pipe_inode_info *pipe, size_t len,
3847 unsigned int flags)
3848{
3849 struct ftrace_buffer_info *info = file->private_data;
Jens Axboe35f3d142010-05-20 10:43:18 +02003850 struct partial_page partial_def[PIPE_DEF_BUFFERS];
3851 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05003852 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02003853 .pages = pages_def,
3854 .partial = partial_def,
Steven Rostedt2cadf912008-12-01 22:20:19 -05003855 .flags = flags,
3856 .ops = &buffer_pipe_buf_ops,
3857 .spd_release = buffer_spd_release,
3858 };
3859 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04003860 int entries, size, i;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003861 size_t ret;
3862
Jens Axboe35f3d142010-05-20 10:43:18 +02003863 if (splice_grow_spd(pipe, &spd))
3864 return -ENOMEM;
3865
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003866 if (*ppos & (PAGE_SIZE - 1)) {
3867 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02003868 ret = -EINVAL;
3869 goto out;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003870 }
3871
3872 if (len & (PAGE_SIZE - 1)) {
3873 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02003874 if (len < PAGE_SIZE) {
3875 ret = -EINVAL;
3876 goto out;
3877 }
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003878 len &= PAGE_MASK;
3879 }
3880
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003881 trace_access_lock(info->cpu);
Steven Rostedt93459c62009-04-29 00:23:13 -04003882 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3883
Jens Axboe35f3d142010-05-20 10:43:18 +02003884 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003885 struct page *page;
3886 int r;
3887
3888 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3889 if (!ref)
3890 break;
3891
Steven Rostedt7267fa62009-04-29 00:16:21 -04003892 ref->ref = 1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003893 ref->buffer = info->tr->buffer;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003894 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003895 if (!ref->page) {
3896 kfree(ref);
3897 break;
3898 }
3899
3900 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtf2957f12009-04-29 00:26:30 -04003901 len, info->cpu, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003902 if (r < 0) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07003903 ring_buffer_free_read_page(ref->buffer, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003904 kfree(ref);
3905 break;
3906 }
3907
3908 /*
3909 * zero out any left over data, this is going to
3910 * user land.
3911 */
3912 size = ring_buffer_page_len(ref->page);
3913 if (size < PAGE_SIZE)
3914 memset(ref->page + size, 0, PAGE_SIZE - size);
3915
3916 page = virt_to_page(ref->page);
3917
3918 spd.pages[i] = page;
3919 spd.partial[i].len = PAGE_SIZE;
3920 spd.partial[i].offset = 0;
3921 spd.partial[i].private = (unsigned long)ref;
3922 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003923 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04003924
3925 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003926 }
3927
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003928 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003929 spd.nr_pages = i;
3930
3931 /* did we read anything? */
3932 if (!spd.nr_pages) {
3933 if (flags & SPLICE_F_NONBLOCK)
3934 ret = -EAGAIN;
3935 else
3936 ret = 0;
3937 /* TODO: block */
Jens Axboe35f3d142010-05-20 10:43:18 +02003938 goto out;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003939 }
3940
3941 ret = splice_to_pipe(pipe, &spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02003942 splice_shrink_spd(pipe, &spd);
3943out:
Steven Rostedt2cadf912008-12-01 22:20:19 -05003944 return ret;
3945}
3946
3947static const struct file_operations tracing_buffers_fops = {
3948 .open = tracing_buffers_open,
3949 .read = tracing_buffers_read,
3950 .release = tracing_buffers_release,
3951 .splice_read = tracing_buffers_splice_read,
3952 .llseek = no_llseek,
3953};
3954
Steven Rostedtc8d77182009-04-29 18:03:45 -04003955static ssize_t
3956tracing_stats_read(struct file *filp, char __user *ubuf,
3957 size_t count, loff_t *ppos)
3958{
3959 unsigned long cpu = (unsigned long)filp->private_data;
3960 struct trace_array *tr = &global_trace;
3961 struct trace_seq *s;
3962 unsigned long cnt;
3963
Li Zefane4f2d102009-06-15 10:57:28 +08003964 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04003965 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01003966 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04003967
3968 trace_seq_init(s);
3969
3970 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3971 trace_seq_printf(s, "entries: %ld\n", cnt);
3972
3973 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3974 trace_seq_printf(s, "overrun: %ld\n", cnt);
3975
3976 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3977 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3978
Steven Rostedtc8d77182009-04-29 18:03:45 -04003979 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3980
3981 kfree(s);
3982
3983 return count;
3984}
3985
3986static const struct file_operations tracing_stats_fops = {
3987 .open = tracing_open_generic,
3988 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02003989 .llseek = generic_file_llseek,
Steven Rostedtc8d77182009-04-29 18:03:45 -04003990};
3991
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003992#ifdef CONFIG_DYNAMIC_FTRACE
3993
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003994int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003995{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003996 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003997}
3998
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003999static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004000tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004001 size_t cnt, loff_t *ppos)
4002{
Steven Rostedta26a2a22008-10-31 00:03:22 -04004003 static char ftrace_dyn_info_buffer[1024];
4004 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004005 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004006 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04004007 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004008 int r;
4009
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004010 mutex_lock(&dyn_info_mutex);
4011 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004012
Steven Rostedta26a2a22008-10-31 00:03:22 -04004013 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004014 buf[r++] = '\n';
4015
4016 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4017
4018 mutex_unlock(&dyn_info_mutex);
4019
4020 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004021}
4022
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004023static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004024 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004025 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004026 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004027};
4028#endif
4029
4030static struct dentry *d_tracer;
4031
4032struct dentry *tracing_init_dentry(void)
4033{
4034 static int once;
4035
4036 if (d_tracer)
4037 return d_tracer;
4038
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01004039 if (!debugfs_initialized())
4040 return NULL;
4041
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004042 d_tracer = debugfs_create_dir("tracing", NULL);
4043
4044 if (!d_tracer && !once) {
4045 once = 1;
4046 pr_warning("Could not create debugfs directory 'tracing'\n");
4047 return NULL;
4048 }
4049
4050 return d_tracer;
4051}
4052
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004053static struct dentry *d_percpu;
4054
4055struct dentry *tracing_dentry_percpu(void)
4056{
4057 static int once;
4058 struct dentry *d_tracer;
4059
4060 if (d_percpu)
4061 return d_percpu;
4062
4063 d_tracer = tracing_init_dentry();
4064
4065 if (!d_tracer)
4066 return NULL;
4067
4068 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4069
4070 if (!d_percpu && !once) {
4071 once = 1;
4072 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4073 return NULL;
4074 }
4075
4076 return d_percpu;
4077}
4078
4079static void tracing_init_debugfs_percpu(long cpu)
4080{
4081 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004082 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04004083 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004084
Steven Rostedtdd49a382010-10-20 21:51:26 -04004085 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004086 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4087 if (!d_cpu) {
4088 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4089 return;
4090 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004091
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004092 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004093 trace_create_file("trace_pipe", 0444, d_cpu,
4094 (void *) cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004095
4096 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004097 trace_create_file("trace", 0644, d_cpu,
4098 (void *) cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04004099
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004100 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4101 (void *) cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004102
4103 trace_create_file("stats", 0444, d_cpu,
4104 (void *) cpu, &tracing_stats_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004105}
4106
Steven Rostedt60a11772008-05-12 21:20:44 +02004107#ifdef CONFIG_FTRACE_SELFTEST
4108/* Let selftest have access to static functions in this file */
4109#include "trace_selftest.c"
4110#endif
4111
Steven Rostedt577b7852009-02-26 23:43:05 -05004112struct trace_option_dentry {
4113 struct tracer_opt *opt;
4114 struct tracer_flags *flags;
4115 struct dentry *entry;
4116};
4117
4118static ssize_t
4119trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4120 loff_t *ppos)
4121{
4122 struct trace_option_dentry *topt = filp->private_data;
4123 char *buf;
4124
4125 if (topt->flags->val & topt->opt->bit)
4126 buf = "1\n";
4127 else
4128 buf = "0\n";
4129
4130 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4131}
4132
4133static ssize_t
4134trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4135 loff_t *ppos)
4136{
4137 struct trace_option_dentry *topt = filp->private_data;
4138 unsigned long val;
4139 char buf[64];
4140 int ret;
4141
4142 if (cnt >= sizeof(buf))
4143 return -EINVAL;
4144
4145 if (copy_from_user(&buf, ubuf, cnt))
4146 return -EFAULT;
4147
4148 buf[cnt] = 0;
4149
4150 ret = strict_strtoul(buf, 10, &val);
4151 if (ret < 0)
4152 return ret;
4153
Li Zefan8d18eaa2009-12-08 11:17:06 +08004154 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05004155 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004156
4157 if (!!(topt->flags->val & topt->opt->bit) != val) {
4158 mutex_lock(&trace_types_lock);
4159 ret = __set_tracer_option(current_trace, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05004160 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004161 mutex_unlock(&trace_types_lock);
4162 if (ret)
4163 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05004164 }
4165
4166 *ppos += cnt;
4167
4168 return cnt;
4169}
4170
4171
4172static const struct file_operations trace_options_fops = {
4173 .open = tracing_open_generic,
4174 .read = trace_options_read,
4175 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004176 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05004177};
4178
Steven Rostedta8259072009-02-26 22:19:12 -05004179static ssize_t
4180trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4181 loff_t *ppos)
4182{
4183 long index = (long)filp->private_data;
4184 char *buf;
4185
4186 if (trace_flags & (1 << index))
4187 buf = "1\n";
4188 else
4189 buf = "0\n";
4190
4191 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4192}
4193
4194static ssize_t
4195trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4196 loff_t *ppos)
4197{
4198 long index = (long)filp->private_data;
4199 char buf[64];
4200 unsigned long val;
4201 int ret;
4202
4203 if (cnt >= sizeof(buf))
4204 return -EINVAL;
4205
4206 if (copy_from_user(&buf, ubuf, cnt))
4207 return -EFAULT;
4208
4209 buf[cnt] = 0;
4210
4211 ret = strict_strtoul(buf, 10, &val);
4212 if (ret < 0)
4213 return ret;
4214
Zhaoleif2d84b62009-08-07 18:55:48 +08004215 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05004216 return -EINVAL;
Zhaoleif2d84b62009-08-07 18:55:48 +08004217 set_tracer_flags(1 << index, val);
Steven Rostedta8259072009-02-26 22:19:12 -05004218
4219 *ppos += cnt;
4220
4221 return cnt;
4222}
4223
Steven Rostedta8259072009-02-26 22:19:12 -05004224static const struct file_operations trace_options_core_fops = {
4225 .open = tracing_open_generic,
4226 .read = trace_options_core_read,
4227 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004228 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05004229};
4230
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004231struct dentry *trace_create_file(const char *name,
4232 mode_t mode,
4233 struct dentry *parent,
4234 void *data,
4235 const struct file_operations *fops)
4236{
4237 struct dentry *ret;
4238
4239 ret = debugfs_create_file(name, mode, parent, data, fops);
4240 if (!ret)
4241 pr_warning("Could not create debugfs '%s' entry\n", name);
4242
4243 return ret;
4244}
4245
4246
Steven Rostedta8259072009-02-26 22:19:12 -05004247static struct dentry *trace_options_init_dentry(void)
4248{
4249 struct dentry *d_tracer;
4250 static struct dentry *t_options;
4251
4252 if (t_options)
4253 return t_options;
4254
4255 d_tracer = tracing_init_dentry();
4256 if (!d_tracer)
4257 return NULL;
4258
4259 t_options = debugfs_create_dir("options", d_tracer);
4260 if (!t_options) {
4261 pr_warning("Could not create debugfs directory 'options'\n");
4262 return NULL;
4263 }
4264
4265 return t_options;
4266}
4267
Steven Rostedt577b7852009-02-26 23:43:05 -05004268static void
4269create_trace_option_file(struct trace_option_dentry *topt,
4270 struct tracer_flags *flags,
4271 struct tracer_opt *opt)
4272{
4273 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05004274
4275 t_options = trace_options_init_dentry();
4276 if (!t_options)
4277 return;
4278
4279 topt->flags = flags;
4280 topt->opt = opt;
4281
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004282 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05004283 &trace_options_fops);
4284
Steven Rostedt577b7852009-02-26 23:43:05 -05004285}
4286
4287static struct trace_option_dentry *
4288create_trace_option_files(struct tracer *tracer)
4289{
4290 struct trace_option_dentry *topts;
4291 struct tracer_flags *flags;
4292 struct tracer_opt *opts;
4293 int cnt;
4294
4295 if (!tracer)
4296 return NULL;
4297
4298 flags = tracer->flags;
4299
4300 if (!flags || !flags->opts)
4301 return NULL;
4302
4303 opts = flags->opts;
4304
4305 for (cnt = 0; opts[cnt].name; cnt++)
4306 ;
4307
Steven Rostedt0cfe8242009-02-27 10:51:10 -05004308 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05004309 if (!topts)
4310 return NULL;
4311
4312 for (cnt = 0; opts[cnt].name; cnt++)
4313 create_trace_option_file(&topts[cnt], flags,
4314 &opts[cnt]);
4315
4316 return topts;
4317}
4318
4319static void
4320destroy_trace_option_files(struct trace_option_dentry *topts)
4321{
4322 int cnt;
4323
4324 if (!topts)
4325 return;
4326
4327 for (cnt = 0; topts[cnt].opt; cnt++) {
4328 if (topts[cnt].entry)
4329 debugfs_remove(topts[cnt].entry);
4330 }
4331
4332 kfree(topts);
4333}
4334
Steven Rostedta8259072009-02-26 22:19:12 -05004335static struct dentry *
4336create_trace_option_core_file(const char *option, long index)
4337{
4338 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004339
4340 t_options = trace_options_init_dentry();
4341 if (!t_options)
4342 return NULL;
4343
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004344 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05004345 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004346}
4347
4348static __init void create_trace_options_dir(void)
4349{
4350 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004351 int i;
4352
4353 t_options = trace_options_init_dentry();
4354 if (!t_options)
4355 return;
4356
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004357 for (i = 0; trace_options[i]; i++)
4358 create_trace_option_core_file(trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05004359}
4360
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004361static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004362{
4363 struct dentry *d_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004364 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004365
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004366 trace_access_lock_init();
4367
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004368 d_tracer = tracing_init_dentry();
4369
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004370 trace_create_file("tracing_enabled", 0644, d_tracer,
4371 &global_trace, &tracing_ctrl_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004372
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004373 trace_create_file("trace_options", 0644, d_tracer,
4374 NULL, &tracing_iter_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004375
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004376 trace_create_file("tracing_cpumask", 0644, d_tracer,
4377 NULL, &tracing_cpumask_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004378
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004379 trace_create_file("trace", 0644, d_tracer,
4380 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004381
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004382 trace_create_file("available_tracers", 0444, d_tracer,
4383 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004384
Li Zefan339ae5d2009-04-17 10:34:30 +08004385 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004386 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004387
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04004388#ifdef CONFIG_TRACER_MAX_TRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004389 trace_create_file("tracing_max_latency", 0644, d_tracer,
4390 &tracing_max_latency, &tracing_max_lat_fops);
Tim Bird0e950172010-02-25 15:36:43 -08004391#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004392
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004393 trace_create_file("tracing_thresh", 0644, d_tracer,
4394 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004395
Li Zefan339ae5d2009-04-17 10:34:30 +08004396 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004397 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004398
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004399 trace_create_file("trace_pipe", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004400 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004401
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004402 trace_create_file("buffer_size_kb", 0644, d_tracer,
4403 &global_trace, &tracing_entries_fops);
Steven Rostedta98a3c32008-05-12 21:20:59 +02004404
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004405 trace_create_file("free_buffer", 0644, d_tracer,
4406 &global_trace, &tracing_free_buffer_fops);
4407
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004408 trace_create_file("trace_marker", 0220, d_tracer,
4409 NULL, &tracing_mark_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004410
Avadh Patel69abe6a2009-04-10 16:04:48 -04004411 trace_create_file("saved_cmdlines", 0444, d_tracer,
4412 NULL, &tracing_saved_cmdlines_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004413
Zhaolei5079f322009-08-25 16:12:56 +08004414 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4415 &trace_clock_fops);
4416
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004417#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004418 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4419 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004420#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004421
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004422 create_trace_options_dir();
4423
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004424 for_each_tracing_cpu(cpu)
4425 tracing_init_debugfs_percpu(cpu);
4426
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004427 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004428}
4429
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004430static int trace_panic_handler(struct notifier_block *this,
4431 unsigned long event, void *unused)
4432{
Steven Rostedt944ac422008-10-23 19:26:08 -04004433 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004434 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004435 return NOTIFY_OK;
4436}
4437
4438static struct notifier_block trace_panic_notifier = {
4439 .notifier_call = trace_panic_handler,
4440 .next = NULL,
4441 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4442};
4443
4444static int trace_die_handler(struct notifier_block *self,
4445 unsigned long val,
4446 void *data)
4447{
4448 switch (val) {
4449 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04004450 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004451 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004452 break;
4453 default:
4454 break;
4455 }
4456 return NOTIFY_OK;
4457}
4458
4459static struct notifier_block trace_die_notifier = {
4460 .notifier_call = trace_die_handler,
4461 .priority = 200
4462};
4463
4464/*
4465 * printk is set to max of 1024, we really don't need it that big.
4466 * Nothing should be printing 1000 characters anyway.
4467 */
4468#define TRACE_MAX_PRINT 1000
4469
4470/*
4471 * Define here KERN_TRACE so that we have one place to modify
4472 * it if we decide to change what log level the ftrace dump
4473 * should be at.
4474 */
Steven Rostedt428aee12009-01-14 12:24:42 -05004475#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004476
Jason Wessel955b61e2010-08-05 09:22:23 -05004477void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004478trace_printk_seq(struct trace_seq *s)
4479{
4480 /* Probably should print a warning here. */
4481 if (s->len >= 1000)
4482 s->len = 1000;
4483
4484 /* should be zero ended, but we are paranoid. */
4485 s->buffer[s->len] = 0;
4486
4487 printk(KERN_TRACE "%s", s->buffer);
4488
Steven Rostedtf9520752009-03-02 14:04:40 -05004489 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004490}
4491
Jason Wessel955b61e2010-08-05 09:22:23 -05004492void trace_init_global_iter(struct trace_iterator *iter)
4493{
4494 iter->tr = &global_trace;
4495 iter->trace = current_trace;
4496 iter->cpu_file = TRACE_PIPE_ALL_CPU;
4497}
4498
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004499static void
4500__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004501{
Thomas Gleixner445c8952009-12-02 19:49:50 +01004502 static arch_spinlock_t ftrace_dump_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01004503 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004504 /* use static because iter can be a bit big for the stack */
4505 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004506 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004507 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004508 unsigned long flags;
4509 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004510
4511 /* only one dump */
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004512 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004513 arch_spin_lock(&ftrace_dump_lock);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004514 if (dump_ran)
4515 goto out;
4516
4517 dump_ran = 1;
4518
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004519 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004520
4521 if (disable_tracing)
4522 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004523
Jason Wessel955b61e2010-08-05 09:22:23 -05004524 trace_init_global_iter(&iter);
4525
Steven Rostedtd7690412008-10-01 00:29:53 -04004526 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05004527 atomic_inc(&iter.tr->data[cpu]->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04004528 }
4529
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004530 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4531
Török Edwinb54d3de2008-11-22 13:28:48 +02004532 /* don't look at user memory in panic mode */
4533 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4534
Steven Rostedte543ad72009-03-04 18:20:36 -05004535 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004536 iter.tr = &global_trace;
4537 iter.trace = current_trace;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004538
4539 switch (oops_dump_mode) {
4540 case DUMP_ALL:
4541 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4542 break;
4543 case DUMP_ORIG:
4544 iter.cpu_file = raw_smp_processor_id();
4545 break;
4546 case DUMP_NONE:
4547 goto out_enable;
4548 default:
4549 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
4550 iter.cpu_file = TRACE_PIPE_ALL_CPU;
4551 }
4552
4553 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004554
4555 /*
4556 * We need to stop all tracing on all CPUS to read the
4557 * the next buffer. This is a bit expensive, but is
4558 * not done often. We fill all what we can read,
4559 * and then release the locks again.
4560 */
4561
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004562 while (!trace_empty(&iter)) {
4563
4564 if (!cnt)
4565 printk(KERN_TRACE "---------------------------------\n");
4566
4567 cnt++;
4568
4569 /* reset all but tr, trace, and overruns */
4570 memset(&iter.seq, 0,
4571 sizeof(struct trace_iterator) -
4572 offsetof(struct trace_iterator, seq));
4573 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4574 iter.pos = -1;
4575
Jason Wessel955b61e2010-08-05 09:22:23 -05004576 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08004577 int ret;
4578
4579 ret = print_trace_line(&iter);
4580 if (ret != TRACE_TYPE_NO_CONSUME)
4581 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004582 }
4583
4584 trace_printk_seq(&iter.seq);
4585 }
4586
4587 if (!cnt)
4588 printk(KERN_TRACE " (ftrace buffer empty)\n");
4589 else
4590 printk(KERN_TRACE "---------------------------------\n");
4591
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004592 out_enable:
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004593 /* Re-enable tracing if requested */
4594 if (!disable_tracing) {
4595 trace_flags |= old_userobj;
4596
4597 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05004598 atomic_dec(&iter.tr->data[cpu]->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004599 }
4600 tracing_on();
4601 }
4602
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004603 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01004604 arch_spin_unlock(&ftrace_dump_lock);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004605 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004606}
4607
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004608/* By default: disable tracing after the dump */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004609void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004610{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004611 __ftrace_dump(true, oops_dump_mode);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004612}
4613
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004614__init static int tracer_alloc_buffers(void)
4615{
Steven Rostedt73c51622009-03-11 13:42:01 -04004616 int ring_buf_size;
David Sharp750912f2010-12-08 13:46:47 -08004617 enum ring_buffer_flags rb_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004618 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304619 int ret = -ENOMEM;
4620
David Sharp750912f2010-12-08 13:46:47 -08004621
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304622 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4623 goto out;
4624
4625 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4626 goto out_free_buffer_mask;
4627
Steven Rostedt73c51622009-03-11 13:42:01 -04004628 /* To save memory, keep the ring buffer size to its minimum */
4629 if (ring_buffer_expanded)
4630 ring_buf_size = trace_buf_size;
4631 else
4632 ring_buf_size = 1;
4633
David Sharp750912f2010-12-08 13:46:47 -08004634 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
4635
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304636 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4637 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004638
Steven Rostedtab464282008-05-12 21:21:00 +02004639 /* TODO: make the number of buffers hot pluggable with CPUS */
David Sharp750912f2010-12-08 13:46:47 -08004640 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004641 if (!global_trace.buffer) {
4642 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4643 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304644 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004645 }
4646 global_trace.entries = ring_buffer_size(global_trace.buffer);
4647
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304648
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004649#ifdef CONFIG_TRACER_MAX_TRACE
David Sharp750912f2010-12-08 13:46:47 -08004650 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004651 if (!max_tr.buffer) {
4652 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4653 WARN_ON(1);
4654 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304655 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004656 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09004657 max_tr.entries = 1;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004658#endif
4659
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004660 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004661 for_each_tracing_cpu(i) {
jolsa@redhat.com566b0aa2009-07-16 21:44:26 +02004662 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Tejun Heo9705f692009-10-29 22:34:13 +09004663 max_tr.data[i] = &per_cpu(max_tr_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004664 }
4665
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004666 trace_init_cmdlines();
4667
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004668 register_tracer(&nop_trace);
Steven Rostedt79fb0768f2009-02-02 21:38:33 -05004669 current_trace = &nop_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +02004670 /* All seems OK, enable tracing */
4671 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004672
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004673 atomic_notifier_chain_register(&panic_notifier_list,
4674 &trace_panic_notifier);
4675
4676 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01004677
4678 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004679
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304680out_free_cpumask:
4681 free_cpumask_var(tracing_cpumask);
4682out_free_buffer_mask:
4683 free_cpumask_var(tracing_buffer_mask);
4684out:
4685 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004686}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004687
4688__init static int clear_boot_tracer(void)
4689{
4690 /*
4691 * The default tracer at boot buffer is an init section.
4692 * This function is called in lateinit. If we did not
4693 * find the boot tracer, then clear it out, to prevent
4694 * later registration from accessing the buffer that is
4695 * about to be freed.
4696 */
4697 if (!default_bootup_tracer)
4698 return 0;
4699
4700 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4701 default_bootup_tracer);
4702 default_bootup_tracer = NULL;
4703
4704 return 0;
4705}
4706
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004707early_initcall(tracer_alloc_buffers);
4708fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004709late_initcall(clear_boot_tracer);