blob: f11a285ee5bb4f87d139ca1f938c9652490322e7 [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 Rostedtb892e5c2012-03-01 22:06:48 -050039#include <linux/nmi.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020040#include <linux/fs.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020041
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020042#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050043#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020044
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010045/*
Steven Rostedt73c51622009-03-11 13:42:01 -040046 * On boot up, the ring buffer is set to the minimum size, so that
47 * we do not waste memory on systems that are not using tracing.
48 */
Li Zefan020e5f82009-07-01 10:47:05 +080049int ring_buffer_expanded;
Steven Rostedt73c51622009-03-11 13:42:01 -040050
51/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010052 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010053 * A selftest will lurk into the ring-buffer to count the
54 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010055 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010056 * at the same time, giving false positive or negative results.
57 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010058static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010059
Steven Rostedtb2821ae2009-02-02 21:38:32 -050060/*
61 * If a tracer is running, we do not want to run SELFTEST.
62 */
Li Zefan020e5f82009-07-01 10:47:05 +080063bool __read_mostly tracing_selftest_disabled;
Steven Rostedtb2821ae2009-02-02 21:38:32 -050064
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010065/* For tracers that don't implement custom flags */
66static struct tracer_opt dummy_tracer_opt[] = {
67 { }
68};
69
70static struct tracer_flags dummy_tracer_flags = {
71 .val = 0,
72 .opts = dummy_tracer_opt
73};
74
75static int dummy_set_flag(u32 old_flags, u32 bit, int set)
76{
77 return 0;
78}
Steven Rostedt0f048702008-11-05 16:05:44 -050079
80/*
81 * Kill all tracing for good (never come back).
82 * It is initialized to 1 but will turn to zero if the initialization
83 * of the tracer is successful. But that is the only place that sets
84 * this back to zero.
85 */
Hannes Eder4fd27352009-02-10 19:44:12 +010086static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050087
Christoph Lameter9288f992009-10-07 19:17:45 -040088DEFINE_PER_CPU(int, ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040089
90static inline void ftrace_disable_cpu(void)
91{
92 preempt_disable();
Rusty Russelldd17c8f2009-10-29 22:34:15 +090093 __this_cpu_inc(ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040094}
95
96static inline void ftrace_enable_cpu(void)
97{
Rusty Russelldd17c8f2009-10-29 22:34:15 +090098 __this_cpu_dec(ftrace_cpu_disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -040099 preempt_enable();
100}
101
Jason Wessel955b61e2010-08-05 09:22:23 -0500102cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200103
Steven Rostedt944ac422008-10-23 19:26:08 -0400104/*
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 *
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
111 * serial console.
112 *
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200115 * /proc/sys/kernel/ftrace_dump_on_oops
116 * Set 1 if you want to dump buffers of all CPUs
117 * Set 2 if you want to dump the buffer of the CPU that triggered oops
Steven Rostedt944ac422008-10-23 19:26:08 -0400118 */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200119
120enum ftrace_dump_mode ftrace_dump_on_oops;
Steven Rostedt944ac422008-10-23 19:26:08 -0400121
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500122static int tracing_set_tracer(const char *buf);
123
Li Zefanee6c2c12009-09-18 14:06:47 +0800124#define MAX_TRACER_SIZE 100
125static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500126static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100127
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200128static int __init set_cmdline_ftrace(char *str)
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100129{
Li Zefanee6c2c12009-09-18 14:06:47 +0800130 strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500131 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400132 /* We are using ftrace early, expand it */
133 ring_buffer_expanded = 1;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100134 return 1;
135}
Frederic Weisbecker1beee962009-10-14 20:50:32 +0200136__setup("ftrace=", set_cmdline_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100137
Steven Rostedt944ac422008-10-23 19:26:08 -0400138static int __init set_ftrace_dump_on_oops(char *str)
139{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200140 if (*str++ != '=' || !*str) {
141 ftrace_dump_on_oops = DUMP_ALL;
142 return 1;
143 }
144
145 if (!strcmp("orig_cpu", str)) {
146 ftrace_dump_on_oops = DUMP_ORIG;
147 return 1;
148 }
149
150 return 0;
Steven Rostedt944ac422008-10-23 19:26:08 -0400151}
152__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200153
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800154unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200155{
156 nsec += 500;
157 do_div(nsec, 1000);
158 return nsec;
159}
160
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200161/*
162 * The global_trace is the descriptor that holds the tracing
163 * buffers for the live tracing. For each CPU, it contains
164 * a link list of pages that will store trace entries. The
165 * page descriptor of the pages in the memory is used to hold
166 * the link list by linking the lru item in the page descriptor
167 * to each of the pages in the buffer per CPU.
168 *
169 * For each active CPU there is a data field that holds the
170 * pages for the buffer for that CPU. Each CPU has the same number
171 * of pages allocated for its buffer.
172 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200173static struct trace_array global_trace;
174
175static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
176
Steven Rostedte77405a2009-09-02 14:17:06 -0400177int filter_current_check_discard(struct ring_buffer *buffer,
178 struct ftrace_event_call *call, void *rec,
Tom Zanussieb02ce02009-04-08 03:15:54 -0500179 struct ring_buffer_event *event)
180{
Steven Rostedte77405a2009-09-02 14:17:06 -0400181 return filter_check_discard(call, rec, buffer, event);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500182}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400183EXPORT_SYMBOL_GPL(filter_current_check_discard);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500184
Steven Rostedt37886f62009-03-17 17:22:06 -0400185cycle_t ftrace_now(int cpu)
186{
187 u64 ts;
188
189 /* Early boot up does not have a buffer yet */
190 if (!global_trace.buffer)
191 return trace_clock_local();
192
193 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
194 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
195
196 return ts;
197}
198
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200199/*
200 * The max_tr is used to snapshot the global_trace when a maximum
201 * latency is reached. Some tracers will use this to store a maximum
202 * trace while it continues examining live traces.
203 *
204 * The buffers for the max_tr are set up the same as the global_trace.
205 * When a snapshot is taken, the link list of the max_tr is swapped
206 * with the link list of the global_trace and the buffers are reset for
207 * the global_trace so the tracing can continue.
208 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200209static struct trace_array max_tr;
210
Tejun Heo9705f692009-10-29 22:34:13 +0900211static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200212
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200213/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200214static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200215
Steven Rostedt90369902008-11-05 16:05:44 -0500216/**
217 * tracing_is_enabled - return tracer_enabled status
218 *
219 * This function is used by other tracers to know the status
220 * of the tracer_enabled flag. Tracers may use this function
221 * to know if it should enable their features when starting
222 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
223 */
224int tracing_is_enabled(void)
225{
226 return tracer_enabled;
227}
228
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200229/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400230 * trace_buf_size is the size in bytes that is allocated
231 * for a buffer. Note, the number of bytes is always rounded
232 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400233 *
234 * This number is purposely set to a low number of 16384.
235 * If the dump on oops happens, it will be much appreciated
236 * to not have to wait for all that output. Anyway this can be
237 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200238 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400239#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400240
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400241static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200242
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200243/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200244static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200245
246/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200247static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200248
249/*
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200250 * trace_types_lock is used to protect the trace_types list.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200251 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200252static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200253
Lai Jiangshan7e53bd42010-01-06 20:08:50 +0800254/*
255 * serialize the access of the ring buffer
256 *
257 * ring buffer serializes readers, but it is low level protection.
258 * The validity of the events (which returns by ring_buffer_peek() ..etc)
259 * are not protected by ring buffer.
260 *
261 * The content of events may become garbage if we allow other process consumes
262 * these events concurrently:
263 * A) the page of the consumed events may become a normal page
264 * (not reader page) in ring buffer, and this page will be rewrited
265 * by events producer.
266 * B) The page of the consumed events may become a page for splice_read,
267 * and this page will be returned to system.
268 *
269 * These primitives allow multi process access to different cpu ring buffer
270 * concurrently.
271 *
272 * These primitives don't distinguish read-only and read-consume access.
273 * Multi read-only access are also serialized.
274 */
275
276#ifdef CONFIG_SMP
277static DECLARE_RWSEM(all_cpu_access_lock);
278static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
279
280static inline void trace_access_lock(int cpu)
281{
282 if (cpu == TRACE_PIPE_ALL_CPU) {
283 /* gain it for accessing the whole ring buffer. */
284 down_write(&all_cpu_access_lock);
285 } else {
286 /* gain it for accessing a cpu ring buffer. */
287
288 /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
289 down_read(&all_cpu_access_lock);
290
291 /* Secondly block other access to this @cpu ring buffer. */
292 mutex_lock(&per_cpu(cpu_access_lock, cpu));
293 }
294}
295
296static inline void trace_access_unlock(int cpu)
297{
298 if (cpu == TRACE_PIPE_ALL_CPU) {
299 up_write(&all_cpu_access_lock);
300 } else {
301 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
302 up_read(&all_cpu_access_lock);
303 }
304}
305
306static inline void trace_access_lock_init(void)
307{
308 int cpu;
309
310 for_each_possible_cpu(cpu)
311 mutex_init(&per_cpu(cpu_access_lock, cpu));
312}
313
314#else
315
316static DEFINE_MUTEX(access_lock);
317
318static inline void trace_access_lock(int cpu)
319{
320 (void)cpu;
321 mutex_lock(&access_lock);
322}
323
324static inline void trace_access_unlock(int cpu)
325{
326 (void)cpu;
327 mutex_unlock(&access_lock);
328}
329
330static inline void trace_access_lock_init(void)
331{
332}
333
334#endif
335
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200336/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200337static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
338
Steven Rostedtee6bce52008-11-12 17:52:37 -0500339/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500340unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400341 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
Steven Rostedt77271ce2011-11-17 09:34:33 -0500342 TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
343 TRACE_ITER_IRQ_INFO;
Ingo Molnar4e655512008-05-12 21:20:52 +0200344
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400345static int trace_stop_count;
Thomas Gleixner5389f6f2009-07-25 17:13:33 +0200346static DEFINE_RAW_SPINLOCK(tracing_start_lock);
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400347
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700348static void wakeup_work_handler(struct work_struct *work)
349{
350 wake_up(&trace_wait);
351}
352
353static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
354
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200355/**
Steven Rostedt499e5472012-02-22 15:50:28 -0500356 * tracing_on - enable tracing buffers
357 *
358 * This function enables tracing buffers that may have been
359 * disabled with tracing_off.
360 */
361void tracing_on(void)
362{
363 if (global_trace.buffer)
364 ring_buffer_record_on(global_trace.buffer);
365 /*
366 * This flag is only looked at when buffers haven't been
367 * allocated yet. We don't really care about the race
368 * between setting this flag and actually turning
369 * on the buffer.
370 */
371 global_trace.buffer_disabled = 0;
372}
373EXPORT_SYMBOL_GPL(tracing_on);
374
375/**
376 * tracing_off - turn off tracing buffers
377 *
378 * This function stops the tracing buffers from recording data.
379 * It does not disable any overhead the tracers themselves may
380 * be causing. This function simply causes all recording to
381 * the ring buffers to fail.
382 */
383void tracing_off(void)
384{
385 if (global_trace.buffer)
386 ring_buffer_record_on(global_trace.buffer);
387 /*
388 * This flag is only looked at when buffers haven't been
389 * allocated yet. We don't really care about the race
390 * between setting this flag and actually turning
391 * on the buffer.
392 */
393 global_trace.buffer_disabled = 1;
394}
395EXPORT_SYMBOL_GPL(tracing_off);
396
397/**
398 * tracing_is_on - show state of ring buffers enabled
399 */
400int tracing_is_on(void)
401{
402 if (global_trace.buffer)
403 return ring_buffer_record_is_on(global_trace.buffer);
404 return !global_trace.buffer_disabled;
405}
406EXPORT_SYMBOL_GPL(tracing_is_on);
407
408/**
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200409 * trace_wake_up - wake up tasks waiting for trace input
410 *
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700411 * Schedules a delayed work to wake up any task that is blocked on the
412 * trace_wait queue. These is used with trace_poll for tasks polling the
413 * trace.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200414 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200415void trace_wake_up(void)
416{
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700417 const unsigned long delay = msecs_to_jiffies(2);
Andrew Morton89f19f02009-09-19 11:55:44 -0700418
419 if (trace_flags & TRACE_ITER_BLOCK)
420 return;
Vaibhav Nagarnaike7e2ee82011-05-10 13:27:21 -0700421 schedule_delayed_work(&wakeup_work, delay);
Ingo Molnar4e655512008-05-12 21:20:52 +0200422}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200423
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400424static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200425{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400426 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200427
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200428 if (!str)
429 return 0;
Li Zefan9d612be2009-06-24 17:33:15 +0800430 buf_size = memparse(str, &str);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200431 /* nr_entries can not be zero */
Li Zefan9d612be2009-06-24 17:33:15 +0800432 if (buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200433 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400434 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200435 return 1;
436}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400437__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200438
Tim Bird0e950172010-02-25 15:36:43 -0800439static int __init set_tracing_thresh(char *str)
440{
441 unsigned long threshhold;
442 int ret;
443
444 if (!str)
445 return 0;
446 ret = strict_strtoul(str, 0, &threshhold);
447 if (ret < 0)
448 return 0;
449 tracing_thresh = threshhold * 1000;
450 return 1;
451}
452__setup("tracing_thresh=", set_tracing_thresh);
453
Steven Rostedt57f50be2008-05-12 21:20:44 +0200454unsigned long nsecs_to_usecs(unsigned long nsecs)
455{
456 return nsecs / 1000;
457}
458
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200459/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200460static const char *trace_options[] = {
461 "print-parent",
462 "sym-offset",
463 "sym-addr",
464 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200465 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200466 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200467 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200468 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200469 "stacktrace",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100470 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500471 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500472 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500473 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200474 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200475 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100476 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200477 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500478 "latency-format",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400479 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400480 "graph-time",
Li Zefane870e9a2010-07-02 11:07:32 +0800481 "record-cmd",
David Sharp750912f2010-12-08 13:46:47 -0800482 "overwrite",
Steven Rostedtcf30cf62011-06-14 22:44:07 -0400483 "disable_on_free",
Steven Rostedt77271ce2011-11-17 09:34:33 -0500484 "irq-info",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200485 NULL
486};
487
Zhaolei5079f322009-08-25 16:12:56 +0800488static struct {
489 u64 (*func)(void);
490 const char *name;
491} trace_clocks[] = {
492 { trace_clock_local, "local" },
493 { trace_clock_global, "global" },
Steven Rostedt62496872011-09-19 11:35:58 -0400494 { trace_clock_counter, "counter" },
Zhaolei5079f322009-08-25 16:12:56 +0800495};
496
497int trace_clock_id;
498
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200499/*
500 * trace_parser_get_init - gets the buffer for trace parser
501 */
502int trace_parser_get_init(struct trace_parser *parser, int size)
503{
504 memset(parser, 0, sizeof(*parser));
505
506 parser->buffer = kmalloc(size, GFP_KERNEL);
507 if (!parser->buffer)
508 return 1;
509
510 parser->size = size;
511 return 0;
512}
513
514/*
515 * trace_parser_put - frees the buffer for trace parser
516 */
517void trace_parser_put(struct trace_parser *parser)
518{
519 kfree(parser->buffer);
520}
521
522/*
523 * trace_get_user - reads the user input string separated by space
524 * (matched by isspace(ch))
525 *
526 * For each string found the 'struct trace_parser' is updated,
527 * and the function returns.
528 *
529 * Returns number of bytes read.
530 *
531 * See kernel/trace/trace.h for 'struct trace_parser' details.
532 */
533int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
534 size_t cnt, loff_t *ppos)
535{
536 char ch;
537 size_t read = 0;
538 ssize_t ret;
539
540 if (!*ppos)
541 trace_parser_clear(parser);
542
543 ret = get_user(ch, ubuf++);
544 if (ret)
545 goto out;
546
547 read++;
548 cnt--;
549
550 /*
551 * The parser is not finished with the last write,
552 * continue reading the user input without skipping spaces.
553 */
554 if (!parser->cont) {
555 /* skip white space */
556 while (cnt && isspace(ch)) {
557 ret = get_user(ch, ubuf++);
558 if (ret)
559 goto out;
560 read++;
561 cnt--;
562 }
563
564 /* only spaces were written */
565 if (isspace(ch)) {
566 *ppos += read;
567 ret = read;
568 goto out;
569 }
570
571 parser->idx = 0;
572 }
573
574 /* read the non-space input */
575 while (cnt && !isspace(ch)) {
Li Zefan3c235a32009-09-22 13:51:54 +0800576 if (parser->idx < parser->size - 1)
jolsa@redhat.comb63f39e2009-09-11 17:29:27 +0200577 parser->buffer[parser->idx++] = ch;
578 else {
579 ret = -EINVAL;
580 goto out;
581 }
582 ret = get_user(ch, ubuf++);
583 if (ret)
584 goto out;
585 read++;
586 cnt--;
587 }
588
589 /* We either got finished input or we have to wait for another call. */
590 if (isspace(ch)) {
591 parser->buffer[parser->idx] = 0;
592 parser->cont = false;
593 } else {
594 parser->cont = true;
595 parser->buffer[parser->idx++] = ch;
596 }
597
598 *ppos += read;
599 ret = read;
600
601out:
602 return ret;
603}
604
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200605ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
606{
607 int len;
608 int ret;
609
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500610 if (!cnt)
611 return 0;
612
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200613 if (s->len <= s->readpos)
614 return -EBUSY;
615
616 len = s->len - s->readpos;
617 if (cnt > len)
618 cnt = len;
619 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500620 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200621 return -EFAULT;
622
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500623 cnt -= ret;
624
Steven Rostedte74da522009-03-04 20:31:11 -0500625 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200626 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200627}
628
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200629static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200630{
631 int len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200632
633 if (s->len <= s->readpos)
634 return -EBUSY;
635
636 len = s->len - s->readpos;
637 if (cnt > len)
638 cnt = len;
Dan Carpenter5a26c8f2012-04-20 09:31:45 +0300639 memcpy(buf, s->buffer + s->readpos, cnt);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200640
Steven Rostedte74da522009-03-04 20:31:11 -0500641 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200642 return cnt;
643}
644
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400645/*
646 * ftrace_max_lock is used to protect the swapping of buffers
647 * when taking a max snapshot. The buffers themselves are
648 * protected by per_cpu spinlocks. But the action of the swap
649 * needs its own lock.
650 *
Thomas Gleixner445c8952009-12-02 19:49:50 +0100651 * This is defined as a arch_spinlock_t in order to help
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400652 * with performance when lockdep debugging is enabled.
653 *
654 * It is also used in other places outside the update_max_tr
655 * so it needs to be defined outside of the
656 * CONFIG_TRACER_MAX_TRACE.
657 */
Thomas Gleixner445c8952009-12-02 19:49:50 +0100658static arch_spinlock_t ftrace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100659 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400660
Tim Bird0e950172010-02-25 15:36:43 -0800661unsigned long __read_mostly tracing_thresh;
662
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400663#ifdef CONFIG_TRACER_MAX_TRACE
664unsigned long __read_mostly tracing_max_latency;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400665
666/*
667 * Copy the new maximum trace into the separate maximum-trace
668 * structure. (this way the maximum trace is permanently saved,
669 * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
670 */
671static void
672__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
673{
674 struct trace_array_cpu *data = tr->data[cpu];
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300675 struct trace_array_cpu *max_data;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400676
677 max_tr.cpu = cpu;
678 max_tr.time_start = data->preempt_timestamp;
679
Steven Rostedt8248ac02009-09-02 12:27:41 -0400680 max_data = max_tr.data[cpu];
681 max_data->saved_latency = tracing_max_latency;
682 max_data->critical_start = data->critical_start;
683 max_data->critical_end = data->critical_end;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400684
Arnaldo Carvalho de Melo1acaa1b2010-03-05 18:23:50 -0300685 memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
Steven Rostedt8248ac02009-09-02 12:27:41 -0400686 max_data->pid = tsk->pid;
687 max_data->uid = task_uid(tsk);
688 max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
689 max_data->policy = tsk->policy;
690 max_data->rt_priority = tsk->rt_priority;
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400691
692 /* record this tasks comm */
693 tracing_record_cmdline(tsk);
694}
695
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200696/**
697 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
698 * @tr: tracer
699 * @tsk: the task with the latency
700 * @cpu: The cpu that initiated the trace.
701 *
702 * Flip the buffers between the @tr and the max_tr and record information
703 * about which task was the cause of this latency.
704 */
Ingo Molnare309b412008-05-12 21:20:51 +0200705void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200706update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
707{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400708 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200709
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400710 if (trace_stop_count)
711 return;
712
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200713 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900714 if (!current_trace->use_max_tr) {
715 WARN_ON_ONCE(1);
716 return;
717 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100718 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400719
720 tr->buffer = max_tr.buffer;
721 max_tr.buffer = buf;
722
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200723 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100724 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200725}
726
727/**
728 * update_max_tr_single - only copy one trace over, and reset the rest
729 * @tr - tracer
730 * @tsk - task with the latency
731 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200732 *
733 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200734 */
Ingo Molnare309b412008-05-12 21:20:51 +0200735void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200736update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
737{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400738 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200739
Steven Rostedtb8de7bd12009-08-31 22:32:27 -0400740 if (trace_stop_count)
741 return;
742
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200743 WARN_ON_ONCE(!irqs_disabled());
KOSAKI Motohiroef710e12010-07-01 14:34:35 +0900744 if (!current_trace->use_max_tr) {
745 WARN_ON_ONCE(1);
746 return;
747 }
748
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100749 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200750
Steven Rostedtd7690412008-10-01 00:29:53 -0400751 ftrace_disable_cpu();
752
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400753 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
754
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400755 if (ret == -EBUSY) {
756 /*
757 * We failed to swap the buffer due to a commit taking
758 * place on this CPU. We fail to record, but we reset
759 * the max trace buffer (no one writes directly to it)
760 * and flag that it failed.
761 */
762 trace_array_printk(&max_tr, _THIS_IP_,
763 "Failed to swap buffers due to commit in progress\n");
764 }
765
Steven Rostedtd7690412008-10-01 00:29:53 -0400766 ftrace_enable_cpu();
767
Steven Rostedte8165dbb2009-09-03 19:13:05 -0400768 WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200769
770 __update_max_tr(tr, tsk, cpu);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100771 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200772}
Steven Rostedt5d4a9db2009-08-27 16:52:21 -0400773#endif /* CONFIG_TRACER_MAX_TRACE */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200774
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200775/**
776 * register_tracer - register a tracer with the ftrace system.
777 * @type - the plugin for the tracer
778 *
779 * Register a new plugin tracer.
780 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100782__releases(kernel_lock)
783__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200784{
785 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200786 int ret = 0;
787
788 if (!type->name) {
789 pr_info("Tracer must have a name\n");
790 return -1;
791 }
792
Dan Carpenter24a461d2010-07-10 12:06:44 +0200793 if (strlen(type->name) >= MAX_TRACER_SIZE) {
Li Zefanee6c2c12009-09-18 14:06:47 +0800794 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
795 return -1;
796 }
797
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200798 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100799
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100800 tracing_selftest_running = true;
801
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200802 for (t = trace_types; t; t = t->next) {
803 if (strcmp(type->name, t->name) == 0) {
804 /* already found */
Li Zefanee6c2c12009-09-18 14:06:47 +0800805 pr_info("Tracer %s already registered\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200806 type->name);
807 ret = -1;
808 goto out;
809 }
810 }
811
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100812 if (!type->set_flag)
813 type->set_flag = &dummy_set_flag;
814 if (!type->flags)
815 type->flags = &dummy_tracer_flags;
816 else
817 if (!type->flags->opts)
818 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100819 if (!type->wait_pipe)
820 type->wait_pipe = default_wait_pipe;
821
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100822
Steven Rostedt60a11772008-05-12 21:20:44 +0200823#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500824 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200825 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200826 struct trace_array *tr = &global_trace;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100827
Steven Rostedt60a11772008-05-12 21:20:44 +0200828 /*
829 * Run a selftest on this tracer.
830 * Here we reset the trace buffer, and set the current
831 * tracer to be this tracer. The tracer can then run some
832 * internal tracing to verify that everything is in order.
833 * If we fail, we do not register this tracer.
834 */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400835 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100836
Steven Rostedt60a11772008-05-12 21:20:44 +0200837 current_trace = type;
Steven Rostedt4a0b1662011-03-09 20:09:26 -0500838
839 /* If we expanded the buffers, make sure the max is expanded too */
840 if (ring_buffer_expanded && type->use_max_tr)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800841 ring_buffer_resize(max_tr.buffer, trace_buf_size,
842 RING_BUFFER_ALL_CPUS);
Steven Rostedt4a0b1662011-03-09 20:09:26 -0500843
Steven Rostedt60a11772008-05-12 21:20:44 +0200844 /* the test is responsible for initializing and enabling */
845 pr_info("Testing tracer %s: ", type->name);
846 ret = type->selftest(type, tr);
847 /* the test is responsible for resetting too */
848 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200849 if (ret) {
850 printk(KERN_CONT "FAILED!\n");
851 goto out;
852 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200853 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedt76f0d072009-09-04 12:12:39 -0400854 tracing_reset_online_cpus(tr);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100855
Steven Rostedt4a0b1662011-03-09 20:09:26 -0500856 /* Shrink the max buffer again */
857 if (ring_buffer_expanded && type->use_max_tr)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -0800858 ring_buffer_resize(max_tr.buffer, 1,
859 RING_BUFFER_ALL_CPUS);
Steven Rostedt4a0b1662011-03-09 20:09:26 -0500860
Steven Rostedt60a11772008-05-12 21:20:44 +0200861 printk(KERN_CONT "PASSED\n");
862 }
863#endif
864
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200865 type->next = trace_types;
866 trace_types = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200867
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200868 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100869 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200870 mutex_unlock(&trace_types_lock);
871
Steven Rostedtdac74942009-02-05 01:13:38 -0500872 if (ret || !default_bootup_tracer)
873 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500874
Li Zefanee6c2c12009-09-18 14:06:47 +0800875 if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
Steven Rostedtdac74942009-02-05 01:13:38 -0500876 goto out_unlock;
877
878 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
879 /* Do we want this tracer to start on bootup? */
880 tracing_set_tracer(type->name);
881 default_bootup_tracer = NULL;
882 /* disable other selftests, since this will break it. */
883 tracing_selftest_disabled = 1;
884#ifdef CONFIG_FTRACE_STARTUP_TEST
885 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
886 type->name);
887#endif
888
889 out_unlock:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200890 return ret;
891}
892
893void unregister_tracer(struct tracer *type)
894{
895 struct tracer **t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200896
897 mutex_lock(&trace_types_lock);
898 for (t = &trace_types; *t; t = &(*t)->next) {
899 if (*t == type)
900 goto found;
901 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800902 pr_info("Tracer %s not registered\n", type->name);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200903 goto out;
904
905 found:
906 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200907
908 if (type == current_trace && tracer_enabled) {
909 tracer_enabled = 0;
910 tracing_stop();
911 if (current_trace->stop)
912 current_trace->stop(&global_trace);
913 current_trace = &nop_trace;
914 }
Li Zefanee6c2c12009-09-18 14:06:47 +0800915out:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200916 mutex_unlock(&trace_types_lock);
917}
918
Steven Rostedt283740c2010-03-12 19:48:41 -0500919static void __tracing_reset(struct ring_buffer *buffer, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200920{
Steven Rostedtd7690412008-10-01 00:29:53 -0400921 ftrace_disable_cpu();
Steven Rostedt283740c2010-03-12 19:48:41 -0500922 ring_buffer_reset_cpu(buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400923 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200924}
925
Steven Rostedtf6339032009-09-04 12:35:16 -0400926void tracing_reset(struct trace_array *tr, int cpu)
927{
928 struct ring_buffer *buffer = tr->buffer;
929
930 ring_buffer_record_disable(buffer);
931
932 /* Make sure all commits have finished */
933 synchronize_sched();
Steven Rostedt283740c2010-03-12 19:48:41 -0500934 __tracing_reset(buffer, cpu);
Steven Rostedtf6339032009-09-04 12:35:16 -0400935
936 ring_buffer_record_enable(buffer);
937}
938
Pekka J Enberg213cc062008-12-19 12:08:39 +0200939void tracing_reset_online_cpus(struct trace_array *tr)
940{
Steven Rostedt621968c2009-09-04 12:02:35 -0400941 struct ring_buffer *buffer = tr->buffer;
Pekka J Enberg213cc062008-12-19 12:08:39 +0200942 int cpu;
943
Steven Rostedt621968c2009-09-04 12:02:35 -0400944 ring_buffer_record_disable(buffer);
945
946 /* Make sure all commits have finished */
947 synchronize_sched();
948
Pekka J Enberg213cc062008-12-19 12:08:39 +0200949 tr->time_start = ftrace_now(tr->cpu);
950
951 for_each_online_cpu(cpu)
Steven Rostedt283740c2010-03-12 19:48:41 -0500952 __tracing_reset(buffer, cpu);
Steven Rostedt621968c2009-09-04 12:02:35 -0400953
954 ring_buffer_record_enable(buffer);
Pekka J Enberg213cc062008-12-19 12:08:39 +0200955}
956
Steven Rostedt9456f0f2009-05-06 21:54:09 -0400957void tracing_reset_current(int cpu)
958{
959 tracing_reset(&global_trace, cpu);
960}
961
962void tracing_reset_current_online_cpus(void)
963{
964 tracing_reset_online_cpus(&global_trace);
965}
966
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200967#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100968#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200969static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
970static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
971static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
972static int cmdline_idx;
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100973static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200974
Steven Rostedt25b0b442008-05-12 21:21:00 +0200975/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100976static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200977
978static void trace_init_cmdlines(void)
979{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100980 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
981 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200982 cmdline_idx = 0;
983}
984
Carsten Emdeb5130b12009-09-13 01:43:07 +0200985int is_tracing_stopped(void)
986{
987 return trace_stop_count;
988}
989
Steven Rostedt0f048702008-11-05 16:05:44 -0500990/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500991 * ftrace_off_permanent - disable all ftrace code permanently
992 *
993 * This should only be called when a serious anomally has
994 * been detected. This will turn off the function tracing,
995 * ring buffers, and other tracing utilites. It takes no
996 * locks and can be called from any context.
997 */
998void ftrace_off_permanent(void)
999{
1000 tracing_disabled = 1;
1001 ftrace_stop();
1002 tracing_off_permanent();
1003}
1004
1005/**
Steven Rostedt0f048702008-11-05 16:05:44 -05001006 * tracing_start - quick start of the tracer
1007 *
1008 * If tracing is enabled but was stopped by tracing_stop,
1009 * this will start the tracer back up.
1010 */
1011void tracing_start(void)
1012{
1013 struct ring_buffer *buffer;
1014 unsigned long flags;
1015
1016 if (tracing_disabled)
1017 return;
1018
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001019 raw_spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -05001020 if (--trace_stop_count) {
1021 if (trace_stop_count < 0) {
1022 /* Someone screwed up their debugging */
1023 WARN_ON_ONCE(1);
1024 trace_stop_count = 0;
1025 }
Steven Rostedt0f048702008-11-05 16:05:44 -05001026 goto out;
1027 }
1028
Steven Rostedta2f80712010-03-12 19:56:00 -05001029 /* Prevent the buffers from switching */
1030 arch_spin_lock(&ftrace_max_lock);
Steven Rostedt0f048702008-11-05 16:05:44 -05001031
1032 buffer = global_trace.buffer;
1033 if (buffer)
1034 ring_buffer_record_enable(buffer);
1035
1036 buffer = max_tr.buffer;
1037 if (buffer)
1038 ring_buffer_record_enable(buffer);
1039
Steven Rostedta2f80712010-03-12 19:56:00 -05001040 arch_spin_unlock(&ftrace_max_lock);
1041
Steven Rostedt0f048702008-11-05 16:05:44 -05001042 ftrace_start();
1043 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001044 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001045}
1046
1047/**
1048 * tracing_stop - quick stop of the tracer
1049 *
1050 * Light weight way to stop tracing. Use in conjunction with
1051 * tracing_start.
1052 */
1053void tracing_stop(void)
1054{
1055 struct ring_buffer *buffer;
1056 unsigned long flags;
1057
1058 ftrace_stop();
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001059 raw_spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001060 if (trace_stop_count++)
1061 goto out;
1062
Steven Rostedta2f80712010-03-12 19:56:00 -05001063 /* Prevent the buffers from switching */
1064 arch_spin_lock(&ftrace_max_lock);
1065
Steven Rostedt0f048702008-11-05 16:05:44 -05001066 buffer = global_trace.buffer;
1067 if (buffer)
1068 ring_buffer_record_disable(buffer);
1069
1070 buffer = max_tr.buffer;
1071 if (buffer)
1072 ring_buffer_record_disable(buffer);
1073
Steven Rostedta2f80712010-03-12 19:56:00 -05001074 arch_spin_unlock(&ftrace_max_lock);
1075
Steven Rostedt0f048702008-11-05 16:05:44 -05001076 out:
Thomas Gleixner5389f6f2009-07-25 17:13:33 +02001077 raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
Steven Rostedt0f048702008-11-05 16:05:44 -05001078}
1079
Ingo Molnare309b412008-05-12 21:20:51 +02001080void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001081
Ingo Molnare309b412008-05-12 21:20:51 +02001082static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001083{
Carsten Emdea635cf02009-03-18 09:00:41 +01001084 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001085
1086 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
1087 return;
1088
1089 /*
1090 * It's not the end of the world if we don't get
1091 * the lock, but we also don't want to spin
1092 * nor do we want to disable interrupts,
1093 * so if we miss here, then better luck next time.
1094 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001095 if (!arch_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001096 return;
1097
1098 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +01001099 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001100 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
1101
Carsten Emdea635cf02009-03-18 09:00:41 +01001102 /*
1103 * Check whether the cmdline buffer at idx has a pid
1104 * mapped. We are going to overwrite that entry so we
1105 * need to clear the map_pid_to_cmdline. Otherwise we
1106 * would read the new comm for the old pid.
1107 */
1108 pid = map_cmdline_to_pid[idx];
1109 if (pid != NO_CMDLINE_MAP)
1110 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001111
Carsten Emdea635cf02009-03-18 09:00:41 +01001112 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001113 map_pid_to_cmdline[tsk->pid] = idx;
1114
1115 cmdline_idx = idx;
1116 }
1117
1118 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
1119
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001120 arch_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001121}
1122
Steven Rostedt4ca530852009-03-16 19:20:15 -04001123void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001124{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001125 unsigned map;
1126
Steven Rostedt4ca530852009-03-16 19:20:15 -04001127 if (!pid) {
1128 strcpy(comm, "<idle>");
1129 return;
1130 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001131
Steven Rostedt74bf4072010-01-25 15:11:53 -05001132 if (WARN_ON_ONCE(pid < 0)) {
1133 strcpy(comm, "<XXX>");
1134 return;
1135 }
1136
Steven Rostedt4ca530852009-03-16 19:20:15 -04001137 if (pid > PID_MAX_DEFAULT) {
1138 strcpy(comm, "<...>");
1139 return;
1140 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001141
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001142 preempt_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001143 arch_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001144 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +01001145 if (map != NO_CMDLINE_MAP)
1146 strcpy(comm, saved_cmdlines[map]);
1147 else
1148 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001149
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001150 arch_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +02001151 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001152}
1153
Ingo Molnare309b412008-05-12 21:20:51 +02001154void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001155{
Thomas Gleixner18aecd32009-03-18 08:56:58 +01001156 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
1157 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001158 return;
1159
1160 trace_save_cmdline(tsk);
1161}
1162
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +03001163void
Steven Rostedt38697052008-10-01 13:14:09 -04001164tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
1165 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001166{
1167 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001168
Steven Rostedt777e2082008-09-29 23:02:42 -04001169 entry->preempt_count = pc & 0xff;
1170 entry->pid = (tsk) ? tsk->pid : 0;
Arjan van de Vena3a4a5a2011-05-05 23:55:18 -04001171 entry->padding = 0;
Steven Rostedt777e2082008-09-29 23:02:42 -04001172 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -04001173#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001174 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -04001175#else
1176 TRACE_FLAG_IRQS_NOSUPPORT |
1177#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001178 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
1179 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
1180 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
1181}
Frederic Weisbeckerf413cdb2009-08-07 01:25:54 +02001182EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001183
Steven Rostedte77405a2009-09-02 14:17:06 -04001184struct ring_buffer_event *
1185trace_buffer_lock_reserve(struct ring_buffer *buffer,
1186 int type,
1187 unsigned long len,
1188 unsigned long flags, int pc)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001189{
1190 struct ring_buffer_event *event;
1191
Steven Rostedte77405a2009-09-02 14:17:06 -04001192 event = ring_buffer_lock_reserve(buffer, len);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001193 if (event != NULL) {
1194 struct trace_entry *ent = ring_buffer_event_data(event);
1195
1196 tracing_generic_entry_update(ent, flags, pc);
1197 ent->type = type;
1198 }
1199
1200 return event;
1201}
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001202
Steven Rostedte77405a2009-09-02 14:17:06 -04001203static inline void
1204__trace_buffer_unlock_commit(struct ring_buffer *buffer,
1205 struct ring_buffer_event *event,
1206 unsigned long flags, int pc,
1207 int wake)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001208{
Steven Rostedte77405a2009-09-02 14:17:06 -04001209 ring_buffer_unlock_commit(buffer, event);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001210
Steven Rostedte77405a2009-09-02 14:17:06 -04001211 ftrace_trace_stack(buffer, flags, 6, pc);
1212 ftrace_trace_userstack(buffer, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001213
1214 if (wake)
1215 trace_wake_up();
1216}
1217
Steven Rostedte77405a2009-09-02 14:17:06 -04001218void trace_buffer_unlock_commit(struct ring_buffer *buffer,
1219 struct ring_buffer_event *event,
1220 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001221{
Steven Rostedte77405a2009-09-02 14:17:06 -04001222 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001223}
1224
Steven Rostedtef5580d2009-02-27 19:38:04 -05001225struct ring_buffer_event *
Steven Rostedte77405a2009-09-02 14:17:06 -04001226trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
1227 int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001228 unsigned long flags, int pc)
1229{
Steven Rostedte77405a2009-09-02 14:17:06 -04001230 *current_rb = global_trace.buffer;
1231 return trace_buffer_lock_reserve(*current_rb,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001232 type, len, flags, pc);
1233}
Steven Rostedt94487d62009-05-05 19:22:53 -04001234EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001235
Steven Rostedte77405a2009-09-02 14:17:06 -04001236void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
1237 struct ring_buffer_event *event,
Steven Rostedtef5580d2009-02-27 19:38:04 -05001238 unsigned long flags, int pc)
1239{
Steven Rostedte77405a2009-09-02 14:17:06 -04001240 __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001241}
Steven Rostedt94487d62009-05-05 19:22:53 -04001242EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001243
Steven Rostedte77405a2009-09-02 14:17:06 -04001244void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
1245 struct ring_buffer_event *event,
1246 unsigned long flags, int pc)
Frederic Weisbecker07edf712009-03-22 23:10:46 +01001247{
Steven Rostedte77405a2009-09-02 14:17:06 -04001248 __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001249}
Steven Rostedt94487d62009-05-05 19:22:53 -04001250EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
Steven Rostedt77d9f462009-04-02 01:16:59 -04001251
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001252void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
1253 struct ring_buffer_event *event,
1254 unsigned long flags, int pc,
1255 struct pt_regs *regs)
1256{
1257 ring_buffer_unlock_commit(buffer, event);
1258
1259 ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
1260 ftrace_trace_userstack(buffer, flags, pc);
1261}
1262EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
1263
Steven Rostedte77405a2009-09-02 14:17:06 -04001264void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
1265 struct ring_buffer_event *event)
Steven Rostedt77d9f462009-04-02 01:16:59 -04001266{
Steven Rostedte77405a2009-09-02 14:17:06 -04001267 ring_buffer_discard_commit(buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001268}
Steven Rostedt12acd472009-04-17 16:01:56 -04001269EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -05001270
Ingo Molnare309b412008-05-12 21:20:51 +02001271void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001272trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001273 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1274 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001275{
Tom Zanussie1112b42009-03-31 00:48:49 -05001276 struct ftrace_event_call *call = &event_function;
Steven Rostedte77405a2009-09-02 14:17:06 -04001277 struct ring_buffer *buffer = tr->buffer;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001278 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001279 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001280
Steven Rostedtd7690412008-10-01 00:29:53 -04001281 /* If we are reading the ring buffer, don't trace */
Rusty Russelldd17c8f2009-10-29 22:34:15 +09001282 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
Steven Rostedtd7690412008-10-01 00:29:53 -04001283 return;
1284
Steven Rostedte77405a2009-09-02 14:17:06 -04001285 event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001286 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001287 if (!event)
1288 return;
1289 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001290 entry->ip = ip;
1291 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -05001292
Steven Rostedte77405a2009-09-02 14:17:06 -04001293 if (!filter_check_discard(call, entry, buffer, event))
1294 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001295}
1296
Ingo Molnare309b412008-05-12 21:20:51 +02001297void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001298ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -04001299 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1300 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001301{
1302 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001303 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001304}
1305
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001306#ifdef CONFIG_STACKTRACE
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001307
1308#define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
1309struct ftrace_stack {
1310 unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
1311};
1312
1313static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
1314static DEFINE_PER_CPU(int, ftrace_stack_reserve);
1315
Steven Rostedte77405a2009-09-02 14:17:06 -04001316static void __ftrace_trace_stack(struct ring_buffer *buffer,
Steven Rostedt53614992009-01-15 19:12:40 -05001317 unsigned long flags,
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001318 int skip, int pc, struct pt_regs *regs)
Ingo Molnar86387f72008-05-12 21:20:51 +02001319{
Tom Zanussie1112b42009-03-31 00:48:49 -05001320 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001321 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001322 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001323 struct stack_trace trace;
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001324 int use_stack;
1325 int size = FTRACE_STACK_ENTRIES;
Ingo Molnar86387f72008-05-12 21:20:51 +02001326
1327 trace.nr_entries = 0;
Ingo Molnar86387f72008-05-12 21:20:51 +02001328 trace.skip = skip;
Ingo Molnar86387f72008-05-12 21:20:51 +02001329
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001330 /*
1331 * Since events can happen in NMIs there's no safe way to
1332 * use the per cpu ftrace_stacks. We reserve it and if an interrupt
1333 * or NMI comes in, it will just have to use the default
1334 * FTRACE_STACK_SIZE.
1335 */
1336 preempt_disable_notrace();
1337
1338 use_stack = ++__get_cpu_var(ftrace_stack_reserve);
1339 /*
1340 * We don't need any atomic variables, just a barrier.
1341 * If an interrupt comes in, we don't care, because it would
1342 * have exited and put the counter back to what we want.
1343 * We just need a barrier to keep gcc from moving things
1344 * around.
1345 */
1346 barrier();
1347 if (use_stack == 1) {
1348 trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
1349 trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
1350
1351 if (regs)
1352 save_stack_trace_regs(regs, &trace);
1353 else
1354 save_stack_trace(&trace);
1355
1356 if (trace.nr_entries > size)
1357 size = trace.nr_entries;
1358 } else
1359 /* From now on, use_stack is a boolean */
1360 use_stack = 0;
1361
1362 size *= sizeof(unsigned long);
1363
1364 event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
1365 sizeof(*entry) + size, flags, pc);
1366 if (!event)
1367 goto out;
1368 entry = ring_buffer_event_data(event);
1369
1370 memset(&entry->caller, 0, size);
1371
1372 if (use_stack)
1373 memcpy(&entry->caller, trace.entries,
1374 trace.nr_entries * sizeof(unsigned long));
1375 else {
1376 trace.max_entries = FTRACE_STACK_ENTRIES;
1377 trace.entries = entry->caller;
1378 if (regs)
1379 save_stack_trace_regs(regs, &trace);
1380 else
1381 save_stack_trace(&trace);
1382 }
1383
1384 entry->size = trace.nr_entries;
1385
Steven Rostedte77405a2009-09-02 14:17:06 -04001386 if (!filter_check_discard(call, entry, buffer, event))
1387 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001388
1389 out:
1390 /* Again, don't let gcc optimize things here */
1391 barrier();
1392 __get_cpu_var(ftrace_stack_reserve)--;
1393 preempt_enable_notrace();
1394
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001395}
1396
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001397void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
1398 int skip, int pc, struct pt_regs *regs)
1399{
1400 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1401 return;
1402
1403 __ftrace_trace_stack(buffer, flags, skip, pc, regs);
1404}
1405
Steven Rostedte77405a2009-09-02 14:17:06 -04001406void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
1407 int skip, int pc)
Steven Rostedt53614992009-01-15 19:12:40 -05001408{
1409 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1410 return;
1411
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001412 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
Steven Rostedt53614992009-01-15 19:12:40 -05001413}
1414
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001415void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
1416 int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001417{
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001418 __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
Steven Rostedt38697052008-10-01 13:14:09 -04001419}
1420
Steven Rostedt03889382009-12-11 09:48:22 -05001421/**
1422 * trace_dump_stack - record a stack back trace in the trace buffer
1423 */
1424void trace_dump_stack(void)
1425{
1426 unsigned long flags;
1427
1428 if (tracing_disabled || tracing_selftest_running)
Steven Rostedte36c5452009-12-14 15:58:33 -05001429 return;
Steven Rostedt03889382009-12-11 09:48:22 -05001430
1431 local_save_flags(flags);
1432
1433 /* skipping 3 traces, seems to get us at the caller of this function */
Masami Hiramatsu1fd8df22011-06-08 16:09:34 +09001434 __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
Steven Rostedt03889382009-12-11 09:48:22 -05001435}
1436
Steven Rostedt91e86e52010-11-10 12:56:12 +01001437static DEFINE_PER_CPU(int, user_stack_count);
1438
Steven Rostedte77405a2009-09-02 14:17:06 -04001439void
1440ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001441{
Tom Zanussie1112b42009-03-31 00:48:49 -05001442 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001443 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001444 struct userstack_entry *entry;
1445 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001446
1447 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1448 return;
1449
Steven Rostedtb6345872010-03-12 20:03:30 -05001450 /*
1451 * NMIs can not handle page faults, even with fix ups.
1452 * The save user stack can (and often does) fault.
1453 */
1454 if (unlikely(in_nmi()))
1455 return;
1456
Steven Rostedt91e86e52010-11-10 12:56:12 +01001457 /*
1458 * prevent recursion, since the user stack tracing may
1459 * trigger other kernel events.
1460 */
1461 preempt_disable();
1462 if (__this_cpu_read(user_stack_count))
1463 goto out;
1464
1465 __this_cpu_inc(user_stack_count);
1466
Steven Rostedte77405a2009-09-02 14:17:06 -04001467 event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001468 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001469 if (!event)
Li Zefan1dbd1952010-12-09 15:47:56 +08001470 goto out_drop_count;
Török Edwin02b67512008-11-22 13:28:47 +02001471 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001472
Steven Rostedt48659d32009-09-11 11:36:23 -04001473 entry->tgid = current->tgid;
Török Edwin02b67512008-11-22 13:28:47 +02001474 memset(&entry->caller, 0, sizeof(entry->caller));
1475
1476 trace.nr_entries = 0;
1477 trace.max_entries = FTRACE_STACK_ENTRIES;
1478 trace.skip = 0;
1479 trace.entries = entry->caller;
1480
1481 save_stack_trace_user(&trace);
Steven Rostedte77405a2009-09-02 14:17:06 -04001482 if (!filter_check_discard(call, entry, buffer, event))
1483 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001484
Li Zefan1dbd1952010-12-09 15:47:56 +08001485 out_drop_count:
Steven Rostedt91e86e52010-11-10 12:56:12 +01001486 __this_cpu_dec(user_stack_count);
Steven Rostedt91e86e52010-11-10 12:56:12 +01001487 out:
1488 preempt_enable();
Török Edwin02b67512008-11-22 13:28:47 +02001489}
1490
Hannes Eder4fd27352009-02-10 19:44:12 +01001491#ifdef UNUSED
1492static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001493{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001494 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001495}
Hannes Eder4fd27352009-02-10 19:44:12 +01001496#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001497
Frederic Weisbeckerc0a0d0d2009-07-29 17:51:13 +02001498#endif /* CONFIG_STACKTRACE */
1499
Steven Rostedt07d777f2011-09-22 14:01:55 -04001500/* created for use with alloc_percpu */
1501struct trace_buffer_struct {
1502 char buffer[TRACE_BUF_SIZE];
1503};
1504
1505static struct trace_buffer_struct *trace_percpu_buffer;
1506static struct trace_buffer_struct *trace_percpu_sirq_buffer;
1507static struct trace_buffer_struct *trace_percpu_irq_buffer;
1508static struct trace_buffer_struct *trace_percpu_nmi_buffer;
1509
1510/*
1511 * The buffer used is dependent on the context. There is a per cpu
1512 * buffer for normal context, softirq contex, hard irq context and
1513 * for NMI context. Thise allows for lockless recording.
1514 *
1515 * Note, if the buffers failed to be allocated, then this returns NULL
1516 */
1517static char *get_trace_buf(void)
1518{
1519 struct trace_buffer_struct *percpu_buffer;
1520 struct trace_buffer_struct *buffer;
1521
1522 /*
1523 * If we have allocated per cpu buffers, then we do not
1524 * need to do any locking.
1525 */
1526 if (in_nmi())
1527 percpu_buffer = trace_percpu_nmi_buffer;
1528 else if (in_irq())
1529 percpu_buffer = trace_percpu_irq_buffer;
1530 else if (in_softirq())
1531 percpu_buffer = trace_percpu_sirq_buffer;
1532 else
1533 percpu_buffer = trace_percpu_buffer;
1534
1535 if (!percpu_buffer)
1536 return NULL;
1537
1538 buffer = per_cpu_ptr(percpu_buffer, smp_processor_id());
1539
1540 return buffer->buffer;
1541}
1542
1543static int alloc_percpu_trace_buffer(void)
1544{
1545 struct trace_buffer_struct *buffers;
1546 struct trace_buffer_struct *sirq_buffers;
1547 struct trace_buffer_struct *irq_buffers;
1548 struct trace_buffer_struct *nmi_buffers;
1549
1550 buffers = alloc_percpu(struct trace_buffer_struct);
1551 if (!buffers)
1552 goto err_warn;
1553
1554 sirq_buffers = alloc_percpu(struct trace_buffer_struct);
1555 if (!sirq_buffers)
1556 goto err_sirq;
1557
1558 irq_buffers = alloc_percpu(struct trace_buffer_struct);
1559 if (!irq_buffers)
1560 goto err_irq;
1561
1562 nmi_buffers = alloc_percpu(struct trace_buffer_struct);
1563 if (!nmi_buffers)
1564 goto err_nmi;
1565
1566 trace_percpu_buffer = buffers;
1567 trace_percpu_sirq_buffer = sirq_buffers;
1568 trace_percpu_irq_buffer = irq_buffers;
1569 trace_percpu_nmi_buffer = nmi_buffers;
1570
1571 return 0;
1572
1573 err_nmi:
1574 free_percpu(irq_buffers);
1575 err_irq:
1576 free_percpu(sirq_buffers);
1577 err_sirq:
1578 free_percpu(buffers);
1579 err_warn:
1580 WARN(1, "Could not allocate percpu trace_printk buffer");
1581 return -ENOMEM;
1582}
1583
1584void trace_printk_init_buffers(void)
1585{
1586 static int buffers_allocated;
1587
1588 if (buffers_allocated)
1589 return;
1590
1591 if (alloc_percpu_trace_buffer())
1592 return;
1593
1594 pr_info("ftrace: Allocated trace_printk buffers\n");
1595
1596 buffers_allocated = 1;
1597}
1598
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001599/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001600 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001601 *
1602 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001603int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001604{
Tom Zanussie1112b42009-03-31 00:48:49 -05001605 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001606 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001607 struct ring_buffer *buffer;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001608 struct trace_array *tr = &global_trace;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001609 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001610 unsigned long flags;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001611 char *tbuffer;
1612 int len = 0, size, pc;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001613
1614 if (unlikely(tracing_selftest_running || tracing_disabled))
1615 return 0;
1616
1617 /* Don't pollute graph traces with trace_vprintk internals */
1618 pause_graph_tracing();
1619
1620 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04001621 preempt_disable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001622
Steven Rostedt07d777f2011-09-22 14:01:55 -04001623 tbuffer = get_trace_buf();
1624 if (!tbuffer) {
1625 len = 0;
1626 goto out;
1627 }
1628
1629 len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
1630
1631 if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001632 goto out;
1633
Steven Rostedt07d777f2011-09-22 14:01:55 -04001634 local_save_flags(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001635 size = sizeof(*entry) + sizeof(u32) * len;
Steven Rostedte77405a2009-09-02 14:17:06 -04001636 buffer = tr->buffer;
1637 event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
1638 flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001639 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04001640 goto out;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001641 entry = ring_buffer_event_data(event);
1642 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001643 entry->fmt = fmt;
1644
Steven Rostedt07d777f2011-09-22 14:01:55 -04001645 memcpy(entry->buf, tbuffer, sizeof(u32) * len);
Steven Rostedtd9313692010-01-06 17:27:11 -05001646 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001647 ring_buffer_unlock_commit(buffer, event);
Steven Rostedtd9313692010-01-06 17:27:11 -05001648 ftrace_trace_stack(buffer, flags, 6, pc);
1649 }
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001650
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001651out:
Steven Rostedt5168ae52010-06-03 09:36:50 -04001652 preempt_enable_notrace();
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001653 unpause_graph_tracing();
1654
1655 return len;
1656}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001657EXPORT_SYMBOL_GPL(trace_vbprintk);
1658
Steven Rostedt659372d2009-09-03 19:11:07 -04001659int trace_array_printk(struct trace_array *tr,
1660 unsigned long ip, const char *fmt, ...)
1661{
1662 int ret;
1663 va_list ap;
1664
1665 if (!(trace_flags & TRACE_ITER_PRINTK))
1666 return 0;
1667
1668 va_start(ap, fmt);
1669 ret = trace_array_vprintk(tr, ip, fmt, ap);
1670 va_end(ap);
1671 return ret;
1672}
1673
1674int trace_array_vprintk(struct trace_array *tr,
1675 unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001676{
Tom Zanussie1112b42009-03-31 00:48:49 -05001677 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001678 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04001679 struct ring_buffer *buffer;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001680 int len = 0, size, pc;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001681 struct print_entry *entry;
Steven Rostedt07d777f2011-09-22 14:01:55 -04001682 unsigned long flags;
1683 char *tbuffer;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001684
1685 if (tracing_disabled || tracing_selftest_running)
1686 return 0;
1687
Steven Rostedt07d777f2011-09-22 14:01:55 -04001688 /* Don't pollute graph traces with trace_vprintk internals */
1689 pause_graph_tracing();
1690
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001691 pc = preempt_count();
1692 preempt_disable_notrace();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001693
Steven Rostedt07d777f2011-09-22 14:01:55 -04001694
1695 tbuffer = get_trace_buf();
1696 if (!tbuffer) {
1697 len = 0;
1698 goto out;
1699 }
1700
1701 len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
1702 if (len > TRACE_BUF_SIZE)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001703 goto out;
1704
Steven Rostedt07d777f2011-09-22 14:01:55 -04001705 local_save_flags(flags);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001706 size = sizeof(*entry) + len + 1;
Steven Rostedte77405a2009-09-02 14:17:06 -04001707 buffer = tr->buffer;
1708 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
Steven Rostedt07d777f2011-09-22 14:01:55 -04001709 flags, pc);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001710 if (!event)
Steven Rostedt07d777f2011-09-22 14:01:55 -04001711 goto out;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001712 entry = ring_buffer_event_data(event);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001713 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001714
Steven Rostedt07d777f2011-09-22 14:01:55 -04001715 memcpy(&entry->buf, tbuffer, len);
Carsten Emdec13d2f72009-11-16 20:56:13 +01001716 entry->buf[len] = '\0';
Steven Rostedtd9313692010-01-06 17:27:11 -05001717 if (!filter_check_discard(call, entry, buffer, event)) {
Steven Rostedte77405a2009-09-02 14:17:06 -04001718 ring_buffer_unlock_commit(buffer, event);
Steven Rostedt07d777f2011-09-22 14:01:55 -04001719 ftrace_trace_stack(buffer, flags, 6, pc);
Steven Rostedtd9313692010-01-06 17:27:11 -05001720 }
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001721 out:
1722 preempt_enable_notrace();
Steven Rostedt07d777f2011-09-22 14:01:55 -04001723 unpause_graph_tracing();
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001724
1725 return len;
1726}
Steven Rostedt659372d2009-09-03 19:11:07 -04001727
1728int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1729{
Steven Rostedta813a152009-10-09 01:41:35 -04001730 return trace_array_vprintk(&global_trace, ip, fmt, args);
Steven Rostedt659372d2009-09-03 19:11:07 -04001731}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001732EXPORT_SYMBOL_GPL(trace_vprintk);
1733
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001734static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001735{
Steven Rostedtd7690412008-10-01 00:29:53 -04001736 /* Don't allow ftrace to trace into the ring buffers */
1737 ftrace_disable_cpu();
1738
Steven Rostedt5a90f572008-09-03 17:42:51 -04001739 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001740 if (iter->buffer_iter[iter->cpu])
1741 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1742
1743 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001744}
1745
Ingo Molnare309b412008-05-12 21:20:51 +02001746static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001747peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
1748 unsigned long *lost_events)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001749{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001750 struct ring_buffer_event *event;
1751 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001752
Steven Rostedtd7690412008-10-01 00:29:53 -04001753 /* Don't allow ftrace to trace into the ring buffers */
1754 ftrace_disable_cpu();
1755
1756 if (buf_iter)
1757 event = ring_buffer_iter_peek(buf_iter, ts);
1758 else
Steven Rostedtbc21b472010-03-31 19:49:26 -04001759 event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
1760 lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001761
1762 ftrace_enable_cpu();
1763
Steven Rostedt4a9bd3f2011-07-14 16:36:53 -04001764 if (event) {
1765 iter->ent_size = ring_buffer_event_length(event);
1766 return ring_buffer_event_data(event);
1767 }
1768 iter->ent_size = 0;
1769 return NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001770}
Steven Rostedtd7690412008-10-01 00:29:53 -04001771
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001772static struct trace_entry *
Steven Rostedtbc21b472010-03-31 19:49:26 -04001773__find_next_entry(struct trace_iterator *iter, int *ent_cpu,
1774 unsigned long *missing_events, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001775{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001776 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001777 struct trace_entry *ent, *next = NULL;
Lai Jiangshanaa274972010-04-05 17:11:05 +08001778 unsigned long lost_events = 0, next_lost = 0;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001779 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001780 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001781 int next_cpu = -1;
Steven Rostedt12b5da32012-03-27 10:43:28 -04001782 int next_size = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001783 int cpu;
1784
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001785 /*
1786 * If we are in a per_cpu trace file, don't bother by iterating over
1787 * all cpu and peek directly.
1788 */
1789 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1790 if (ring_buffer_empty_cpu(buffer, cpu_file))
1791 return NULL;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001792 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001793 if (ent_cpu)
1794 *ent_cpu = cpu_file;
1795
1796 return ent;
1797 }
1798
Steven Rostedtab464282008-05-12 21:21:00 +02001799 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001800
1801 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001802 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001803
Steven Rostedtbc21b472010-03-31 19:49:26 -04001804 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001805
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001806 /*
1807 * Pick the entry with the smallest timestamp:
1808 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001809 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001810 next = ent;
1811 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001812 next_ts = ts;
Steven Rostedtbc21b472010-03-31 19:49:26 -04001813 next_lost = lost_events;
Steven Rostedt12b5da32012-03-27 10:43:28 -04001814 next_size = iter->ent_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001815 }
1816 }
1817
Steven Rostedt12b5da32012-03-27 10:43:28 -04001818 iter->ent_size = next_size;
1819
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001820 if (ent_cpu)
1821 *ent_cpu = next_cpu;
1822
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001823 if (ent_ts)
1824 *ent_ts = next_ts;
1825
Steven Rostedtbc21b472010-03-31 19:49:26 -04001826 if (missing_events)
1827 *missing_events = next_lost;
1828
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001829 return next;
1830}
1831
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001832/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001833struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1834 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001835{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001836 return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001837}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001838
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001839/* Find the next real entry, and increment the iterator to the next entry */
Jason Wessel955b61e2010-08-05 09:22:23 -05001840void *trace_find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001841{
Steven Rostedtbc21b472010-03-31 19:49:26 -04001842 iter->ent = __find_next_entry(iter, &iter->cpu,
1843 &iter->lost_events, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001844
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001845 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001846 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001847
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001848 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001849}
1850
Ingo Molnare309b412008-05-12 21:20:51 +02001851static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001852{
Steven Rostedtd7690412008-10-01 00:29:53 -04001853 /* Don't allow ftrace to trace into the ring buffers */
1854 ftrace_disable_cpu();
Steven Rostedtbc21b472010-03-31 19:49:26 -04001855 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
1856 &iter->lost_events);
Steven Rostedtd7690412008-10-01 00:29:53 -04001857 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001858}
1859
Ingo Molnare309b412008-05-12 21:20:51 +02001860static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001861{
1862 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001863 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001864 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001865
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001866 WARN_ON_ONCE(iter->leftover);
1867
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001868 (*pos)++;
1869
1870 /* can't go backwards */
1871 if (iter->idx > i)
1872 return NULL;
1873
1874 if (iter->idx < 0)
Jason Wessel955b61e2010-08-05 09:22:23 -05001875 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001876 else
1877 ent = iter;
1878
1879 while (ent && iter->idx < i)
Jason Wessel955b61e2010-08-05 09:22:23 -05001880 ent = trace_find_next_entry_inc(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001881
1882 iter->pos = *pos;
1883
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001884 return ent;
1885}
1886
Jason Wessel955b61e2010-08-05 09:22:23 -05001887void tracing_iter_reset(struct trace_iterator *iter, int cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001888{
1889 struct trace_array *tr = iter->tr;
1890 struct ring_buffer_event *event;
1891 struct ring_buffer_iter *buf_iter;
1892 unsigned long entries = 0;
1893 u64 ts;
1894
1895 tr->data[cpu]->skipped_entries = 0;
1896
1897 if (!iter->buffer_iter[cpu])
1898 return;
1899
1900 buf_iter = iter->buffer_iter[cpu];
1901 ring_buffer_iter_reset(buf_iter);
1902
1903 /*
1904 * We could have the case with the max latency tracers
1905 * that a reset never took place on a cpu. This is evident
1906 * by the timestamp being before the start of the buffer.
1907 */
1908 while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
1909 if (ts >= iter->tr->time_start)
1910 break;
1911 entries++;
1912 ring_buffer_read(buf_iter, NULL);
1913 }
1914
1915 tr->data[cpu]->skipped_entries = entries;
1916}
1917
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001918/*
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001919 * The current tracer is copied to avoid a global locking
1920 * all around.
1921 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001922static void *s_start(struct seq_file *m, loff_t *pos)
1923{
1924 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001925 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001926 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001927 void *p = NULL;
1928 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001929 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001930
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001931 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001932 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001933 if (unlikely(old_tracer != current_trace && current_trace)) {
1934 old_tracer = current_trace;
1935 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001936 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001937 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001938
1939 atomic_inc(&trace_record_cmdline_disabled);
1940
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001941 if (*pos != iter->pos) {
1942 iter->ent = NULL;
1943 iter->cpu = 0;
1944 iter->idx = -1;
1945
Steven Rostedtd7690412008-10-01 00:29:53 -04001946 ftrace_disable_cpu();
1947
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001948 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1949 for_each_tracing_cpu(cpu)
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001950 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001951 } else
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04001952 tracing_iter_reset(iter, cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001953
Steven Rostedtd7690412008-10-01 00:29:53 -04001954 ftrace_enable_cpu();
1955
Lai Jiangshanac91d852010-03-02 17:54:50 +08001956 iter->leftover = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001957 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1958 ;
1959
1960 } else {
Steven Rostedta63ce5b2009-12-07 09:11:39 -05001961 /*
1962 * If we overflowed the seq_file before, then we want
1963 * to just reuse the trace_seq buffer again.
1964 */
1965 if (iter->leftover)
1966 p = iter;
1967 else {
1968 l = *pos - 1;
1969 p = s_next(m, p, &l);
1970 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001971 }
1972
Lai Jiangshan4f535962009-05-18 19:35:34 +08001973 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001974 trace_access_lock(cpu_file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001975 return p;
1976}
1977
1978static void s_stop(struct seq_file *m, void *p)
1979{
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001980 struct trace_iterator *iter = m->private;
1981
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001982 atomic_dec(&trace_record_cmdline_disabled);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08001983 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08001984 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001985}
1986
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05001987static void
1988get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
1989{
1990 unsigned long count;
1991 int cpu;
1992
1993 *total = 0;
1994 *entries = 0;
1995
1996 for_each_tracing_cpu(cpu) {
1997 count = ring_buffer_entries_cpu(tr->buffer, cpu);
1998 /*
1999 * If this buffer has skipped entries, then we hold all
2000 * entries for the trace and we need to ignore the
2001 * ones before the time stamp.
2002 */
2003 if (tr->data[cpu]->skipped_entries) {
2004 count -= tr->data[cpu]->skipped_entries;
2005 /* total is the same as the entries */
2006 *total += count;
2007 } else
2008 *total += count +
2009 ring_buffer_overrun_cpu(tr->buffer, cpu);
2010 *entries += count;
2011 }
2012}
2013
Ingo Molnare309b412008-05-12 21:20:51 +02002014static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002015{
Michael Ellermana6168352008-08-20 16:36:11 -07002016 seq_puts(m, "# _------=> CPU# \n");
2017 seq_puts(m, "# / _-----=> irqs-off \n");
2018 seq_puts(m, "# | / _----=> need-resched \n");
2019 seq_puts(m, "# || / _---=> hardirq/softirq \n");
2020 seq_puts(m, "# ||| / _--=> preempt-depth \n");
Steven Rostedte6e1e252011-03-09 10:41:56 -05002021 seq_puts(m, "# |||| / delay \n");
2022 seq_puts(m, "# cmd pid ||||| time | caller \n");
2023 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002024}
2025
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002026static void print_event_info(struct trace_array *tr, struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002027{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002028 unsigned long total;
2029 unsigned long entries;
2030
2031 get_total_entries(tr, &total, &entries);
2032 seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
2033 entries, total, num_online_cpus());
2034 seq_puts(m, "#\n");
2035}
2036
2037static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
2038{
2039 print_event_info(tr, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002040 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
Michael Ellermana6168352008-08-20 16:36:11 -07002041 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002042}
2043
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002044static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
Steven Rostedt77271ce2011-11-17 09:34:33 -05002045{
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002046 print_event_info(tr, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002047 seq_puts(m, "# _-----=> irqs-off\n");
2048 seq_puts(m, "# / _----=> need-resched\n");
2049 seq_puts(m, "# | / _---=> hardirq/softirq\n");
2050 seq_puts(m, "# || / _--=> preempt-depth\n");
2051 seq_puts(m, "# ||| / delay\n");
2052 seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
2053 seq_puts(m, "# | | | |||| | |\n");
2054}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002055
Jiri Olsa62b915f2010-04-02 19:01:22 +02002056void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002057print_trace_header(struct seq_file *m, struct trace_iterator *iter)
2058{
2059 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2060 struct trace_array *tr = iter->tr;
2061 struct trace_array_cpu *data = tr->data[tr->cpu];
2062 struct tracer *type = current_trace;
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002063 unsigned long entries;
2064 unsigned long total;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002065 const char *name = "preemption";
2066
2067 if (type)
2068 name = type->name;
2069
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002070 get_total_entries(tr, &total, &entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002071
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002072 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002073 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002074 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002075 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002076 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002077 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02002078 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002079 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002080 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002081 tr->cpu,
2082#if defined(CONFIG_PREEMPT_NONE)
2083 "server",
2084#elif defined(CONFIG_PREEMPT_VOLUNTARY)
2085 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04002086#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002087 "preempt",
2088#else
2089 "unknown",
2090#endif
2091 /* These are reserved for later use */
2092 0, 0, 0, 0);
2093#ifdef CONFIG_SMP
2094 seq_printf(m, " #P:%d)\n", num_online_cpus());
2095#else
2096 seq_puts(m, ")\n");
2097#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002098 seq_puts(m, "# -----------------\n");
2099 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002100 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
2101 data->comm, data->pid, data->uid, data->nice,
2102 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002103 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002104
2105 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002106 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002107 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
2108 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002109 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02002110 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
2111 trace_print_seq(m, &iter->seq);
Steven Rostedt8248ac02009-09-02 12:27:41 -04002112 seq_puts(m, "\n#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002113 }
2114
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09002115 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002116}
2117
Steven Rostedta3097202008-11-07 22:36:02 -05002118static void test_cpu_buff_start(struct trace_iterator *iter)
2119{
2120 struct trace_seq *s = &iter->seq;
2121
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002122 if (!(trace_flags & TRACE_ITER_ANNOTATE))
2123 return;
2124
2125 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
2126 return;
2127
Rusty Russell44623442009-01-01 10:12:23 +10302128 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05002129 return;
2130
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002131 if (iter->tr->data[iter->cpu]->skipped_entries)
2132 return;
2133
Rusty Russell44623442009-01-01 10:12:23 +10302134 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002135
2136 /* Don't print started cpu buffer for the first entry of the trace */
2137 if (iter->idx > 1)
2138 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
2139 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05002140}
2141
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002142static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002143{
Steven Rostedt214023c2008-05-12 21:20:46 +02002144 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002145 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002146 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002147 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002148
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002149 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002150
Steven Rostedta3097202008-11-07 22:36:02 -05002151 test_cpu_buff_start(iter);
2152
Steven Rostedtf633cef2008-12-23 23:24:13 -05002153 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002154
2155 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05002156 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2157 if (!trace_print_lat_context(iter))
2158 goto partial;
2159 } else {
2160 if (!trace_print_context(iter))
2161 goto partial;
2162 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002163 }
2164
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002165 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002166 return event->funcs->trace(iter, sym_flags, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002167
2168 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
2169 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04002170
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002171 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002172partial:
2173 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002174}
2175
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002176static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002177{
2178 struct trace_seq *s = &iter->seq;
2179 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002180 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002181
2182 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002183
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002184 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002185 if (!trace_seq_printf(s, "%d %d %llu ",
2186 entry->pid, iter->cpu, iter->ts))
2187 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002188 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002189
Steven Rostedtf633cef2008-12-23 23:24:13 -05002190 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002191 if (event)
Steven Rostedta9a57762010-04-22 18:46:14 -04002192 return event->funcs->raw(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002193
2194 if (!trace_seq_printf(s, "%d ?\n", entry->type))
2195 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04002196
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002197 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002198partial:
2199 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002200}
2201
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002202static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002203{
2204 struct trace_seq *s = &iter->seq;
2205 unsigned char newline = '\n';
2206 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002207 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002208
2209 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002210
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002211 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2212 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
2213 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
2214 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
2215 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002216
Steven Rostedtf633cef2008-12-23 23:24:13 -05002217 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02002218 if (event) {
Steven Rostedta9a57762010-04-22 18:46:14 -04002219 enum print_line_t ret = event->funcs->hex(iter, 0, event);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02002220 if (ret != TRACE_TYPE_HANDLED)
2221 return ret;
2222 }
Steven Rostedt7104f302008-10-01 10:52:51 -04002223
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002224 SEQ_PUT_FIELD_RET(s, newline);
2225
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002226 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002227}
2228
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002229static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002230{
2231 struct trace_seq *s = &iter->seq;
2232 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05002233 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002234
2235 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04002236
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002237 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
2238 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05002239 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02002240 SEQ_PUT_FIELD_RET(s, iter->ts);
2241 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002242
Steven Rostedtf633cef2008-12-23 23:24:13 -05002243 event = ftrace_find_event(entry->type);
Steven Rostedta9a57762010-04-22 18:46:14 -04002244 return event ? event->funcs->binary(iter, 0, event) :
2245 TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002246}
2247
Jiri Olsa62b915f2010-04-02 19:01:22 +02002248int trace_empty(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002249{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002250 int cpu;
2251
Steven Rostedt9aba60f2009-03-11 19:52:30 -04002252 /* If we are looking at one CPU buffer, only check that one */
2253 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
2254 cpu = iter->cpu_file;
2255 if (iter->buffer_iter[cpu]) {
2256 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2257 return 0;
2258 } else {
2259 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2260 return 0;
2261 }
2262 return 1;
2263 }
2264
Steven Rostedtab464282008-05-12 21:21:00 +02002265 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04002266 if (iter->buffer_iter[cpu]) {
2267 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
2268 return 0;
2269 } else {
2270 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
2271 return 0;
2272 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273 }
Steven Rostedtd7690412008-10-01 00:29:53 -04002274
Frederic Weisbecker797d3712008-09-30 18:13:45 +02002275 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002276}
2277
Lai Jiangshan4f535962009-05-18 19:35:34 +08002278/* Called with trace_event_read_lock() held. */
Jason Wessel955b61e2010-08-05 09:22:23 -05002279enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002280{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002281 enum print_line_t ret;
2282
Jiri Olsaee5e51f2011-03-25 12:05:18 +01002283 if (iter->lost_events &&
2284 !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
2285 iter->cpu, iter->lost_events))
2286 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc21b472010-03-31 19:49:26 -04002287
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002288 if (iter->trace && iter->trace->print_line) {
2289 ret = iter->trace->print_line(iter);
2290 if (ret != TRACE_TYPE_UNHANDLED)
2291 return ret;
2292 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002293
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002294 if (iter->ent->type == TRACE_BPRINT &&
2295 trace_flags & TRACE_ITER_PRINTK &&
2296 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002297 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01002298
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002299 if (iter->ent->type == TRACE_PRINT &&
2300 trace_flags & TRACE_ITER_PRINTK &&
2301 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04002302 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01002303
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02002304 if (trace_flags & TRACE_ITER_BIN)
2305 return print_bin_fmt(iter);
2306
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02002307 if (trace_flags & TRACE_ITER_HEX)
2308 return print_hex_fmt(iter);
2309
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002310 if (trace_flags & TRACE_ITER_RAW)
2311 return print_raw_fmt(iter);
2312
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002313 return print_trace_fmt(iter);
2314}
2315
Jiri Olsa7e9a49e2011-11-07 16:08:49 +01002316void trace_latency_header(struct seq_file *m)
2317{
2318 struct trace_iterator *iter = m->private;
2319
2320 /* print nothing if the buffers are empty */
2321 if (trace_empty(iter))
2322 return;
2323
2324 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
2325 print_trace_header(m, iter);
2326
2327 if (!(trace_flags & TRACE_ITER_VERBOSE))
2328 print_lat_help_header(m);
2329}
2330
Jiri Olsa62b915f2010-04-02 19:01:22 +02002331void trace_default_header(struct seq_file *m)
2332{
2333 struct trace_iterator *iter = m->private;
2334
Jiri Olsaf56e7f82011-06-03 16:58:49 +02002335 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
2336 return;
2337
Jiri Olsa62b915f2010-04-02 19:01:22 +02002338 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
2339 /* print nothing if the buffers are empty */
2340 if (trace_empty(iter))
2341 return;
2342 print_trace_header(m, iter);
2343 if (!(trace_flags & TRACE_ITER_VERBOSE))
2344 print_lat_help_header(m);
2345 } else {
Steven Rostedt77271ce2011-11-17 09:34:33 -05002346 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
2347 if (trace_flags & TRACE_ITER_IRQ_INFO)
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002348 print_func_help_header_irq(iter->tr, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002349 else
Steven Rostedt39eaf7e2011-11-17 10:35:16 -05002350 print_func_help_header(iter->tr, m);
Steven Rostedt77271ce2011-11-17 09:34:33 -05002351 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02002352 }
2353}
2354
Steven Rostedte0a413f2011-09-29 21:26:16 -04002355static void test_ftrace_alive(struct seq_file *m)
2356{
2357 if (!ftrace_is_dead())
2358 return;
2359 seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
2360 seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
2361}
2362
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363static int s_show(struct seq_file *m, void *v)
2364{
2365 struct trace_iterator *iter = v;
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002366 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002367
2368 if (iter->ent == NULL) {
2369 if (iter->tr) {
2370 seq_printf(m, "# tracer: %s\n", iter->trace->name);
2371 seq_puts(m, "#\n");
Steven Rostedte0a413f2011-09-29 21:26:16 -04002372 test_ftrace_alive(m);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002373 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002374 if (iter->trace && iter->trace->print_header)
2375 iter->trace->print_header(m);
Jiri Olsa62b915f2010-04-02 19:01:22 +02002376 else
2377 trace_default_header(m);
2378
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002379 } else if (iter->leftover) {
2380 /*
2381 * If we filled the seq_file buffer earlier, we
2382 * want to just show it now.
2383 */
2384 ret = trace_print_seq(m, &iter->seq);
2385
2386 /* ret should this time be zero, but you never know */
2387 iter->leftover = ret;
2388
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002389 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002390 print_trace_line(iter);
Steven Rostedta63ce5b2009-12-07 09:11:39 -05002391 ret = trace_print_seq(m, &iter->seq);
2392 /*
2393 * If we overflow the seq_file buffer, then it will
2394 * ask us for this data again at start up.
2395 * Use that instead.
2396 * ret is 0 if seq_file write succeeded.
2397 * -1 otherwise.
2398 */
2399 iter->leftover = ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002400 }
2401
2402 return 0;
2403}
2404
James Morris88e9d342009-09-22 16:43:43 -07002405static const struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002406 .start = s_start,
2407 .next = s_next,
2408 .stop = s_stop,
2409 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002410};
2411
Ingo Molnare309b412008-05-12 21:20:51 +02002412static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002413__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002414{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002415 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002416 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002417 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002418 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002419 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002420
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002421 if (tracing_disabled)
2422 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02002423
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002424 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002425 if (!iter)
2426 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002427
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002428 /*
2429 * We make a copy of the current tracer to avoid concurrent
2430 * changes on it while we are reading.
2431 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002432 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002433 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002434 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002435 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002436
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002437 if (current_trace)
2438 *iter->trace = *current_trace;
2439
Li Zefan79f55992009-06-15 14:58:26 +08002440 if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002441 goto fail;
2442
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002443 if (current_trace && current_trace->print_max)
2444 iter->tr = &max_tr;
2445 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002446 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002447 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002448 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002449 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002450
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002451 /* Notify the tracer early; before we stop tracing. */
2452 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01002453 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01002454
Steven Rostedt12ef7d42008-11-12 17:52:38 -05002455 /* Annotate start of buffers if we had overruns */
2456 if (ring_buffer_overruns(iter->tr->buffer))
2457 iter->iter_flags |= TRACE_FILE_ANNOTATE;
2458
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002459 /* stop the trace while dumping */
2460 tracing_stop();
2461
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002462 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
2463 for_each_tracing_cpu(cpu) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002464 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002465 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2466 }
2467 ring_buffer_read_prepare_sync();
2468 for_each_tracing_cpu(cpu) {
2469 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002470 tracing_iter_reset(iter, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002471 }
2472 } else {
2473 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002474 iter->buffer_iter[cpu] =
David Miller72c9ddf2010-04-20 15:47:11 -07002475 ring_buffer_read_prepare(iter->tr->buffer, cpu);
2476 ring_buffer_read_prepare_sync();
2477 ring_buffer_read_start(iter->buffer_iter[cpu]);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002478 tracing_iter_reset(iter, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002479 }
2480
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002481 ret = seq_open(file, &tracer_seq_ops);
2482 if (ret < 0) {
2483 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002484 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002485 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002486
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002487 m = file->private_data;
2488 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002489
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002490 mutex_unlock(&trace_types_lock);
2491
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002492 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002493
2494 fail_buffer:
2495 for_each_tracing_cpu(cpu) {
2496 if (iter->buffer_iter[cpu])
2497 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2498 }
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002499 free_cpumask_var(iter->started);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -04002500 tracing_start();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002501 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002502 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002503 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01002504 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002505
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002506 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002507}
2508
2509int tracing_open_generic(struct inode *inode, struct file *filp)
2510{
Steven Rostedt60a11772008-05-12 21:20:44 +02002511 if (tracing_disabled)
2512 return -ENODEV;
2513
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002514 filp->private_data = inode->i_private;
2515 return 0;
2516}
2517
Hannes Eder4fd27352009-02-10 19:44:12 +01002518static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002519{
matt mooney907f2782010-09-27 19:04:53 -07002520 struct seq_file *m = file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002521 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002522 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002523
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002524 if (!(file->f_mode & FMODE_READ))
2525 return 0;
2526
2527 iter = m->private;
2528
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002529 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002530 for_each_tracing_cpu(cpu) {
2531 if (iter->buffer_iter[cpu])
2532 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2533 }
2534
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002535 if (iter->trace && iter->trace->close)
2536 iter->trace->close(iter);
2537
2538 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05002539 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002540 mutex_unlock(&trace_types_lock);
2541
2542 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002543 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002544 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002545 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002546 kfree(iter);
2547 return 0;
2548}
2549
2550static int tracing_open(struct inode *inode, struct file *file)
2551{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002552 struct trace_iterator *iter;
2553 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002554
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002555 /* If this file was open for write, then erase contents */
2556 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002557 (file->f_flags & O_TRUNC)) {
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002558 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002559
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002560 if (cpu == TRACE_PIPE_ALL_CPU)
2561 tracing_reset_online_cpus(&global_trace);
2562 else
2563 tracing_reset(&global_trace, cpu);
2564 }
2565
2566 if (file->f_mode & FMODE_READ) {
2567 iter = __tracing_open(inode, file);
2568 if (IS_ERR(iter))
2569 ret = PTR_ERR(iter);
2570 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2571 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2572 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002573 return ret;
2574}
2575
Ingo Molnare309b412008-05-12 21:20:51 +02002576static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002577t_next(struct seq_file *m, void *v, loff_t *pos)
2578{
Li Zefanf129e962009-06-24 09:53:44 +08002579 struct tracer *t = v;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002580
2581 (*pos)++;
2582
2583 if (t)
2584 t = t->next;
2585
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002586 return t;
2587}
2588
2589static void *t_start(struct seq_file *m, loff_t *pos)
2590{
Li Zefanf129e962009-06-24 09:53:44 +08002591 struct tracer *t;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002592 loff_t l = 0;
2593
2594 mutex_lock(&trace_types_lock);
Li Zefanf129e962009-06-24 09:53:44 +08002595 for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002596 ;
2597
2598 return t;
2599}
2600
2601static void t_stop(struct seq_file *m, void *p)
2602{
2603 mutex_unlock(&trace_types_lock);
2604}
2605
2606static int t_show(struct seq_file *m, void *v)
2607{
2608 struct tracer *t = v;
2609
2610 if (!t)
2611 return 0;
2612
2613 seq_printf(m, "%s", t->name);
2614 if (t->next)
2615 seq_putc(m, ' ');
2616 else
2617 seq_putc(m, '\n');
2618
2619 return 0;
2620}
2621
James Morris88e9d342009-09-22 16:43:43 -07002622static const struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002623 .start = t_start,
2624 .next = t_next,
2625 .stop = t_stop,
2626 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002627};
2628
2629static int show_traces_open(struct inode *inode, struct file *file)
2630{
Steven Rostedt60a11772008-05-12 21:20:44 +02002631 if (tracing_disabled)
2632 return -ENODEV;
2633
Li Zefanf129e962009-06-24 09:53:44 +08002634 return seq_open(file, &show_traces_seq_ops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002635}
2636
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002637static ssize_t
2638tracing_write_stub(struct file *filp, const char __user *ubuf,
2639 size_t count, loff_t *ppos)
2640{
2641 return count;
2642}
2643
Slava Pestov364829b2010-11-24 15:13:16 -08002644static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
2645{
2646 if (file->f_mode & FMODE_READ)
2647 return seq_lseek(file, offset, origin);
2648 else
2649 return 0;
2650}
2651
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002652static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002653 .open = tracing_open,
2654 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002655 .write = tracing_write_stub,
Slava Pestov364829b2010-11-24 15:13:16 -08002656 .llseek = tracing_seek,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002657 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002658};
2659
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002660static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002661 .open = show_traces_open,
2662 .read = seq_read,
2663 .release = seq_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002664 .llseek = seq_lseek,
Ingo Molnarc7078de2008-05-12 21:20:52 +02002665};
2666
Ingo Molnar36dfe922008-05-12 21:20:52 +02002667/*
2668 * Only trace on a CPU if the bitmask is set:
2669 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302670static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002671
2672/*
2673 * The tracer itself will not take this lock, but still we want
2674 * to provide a consistent cpumask to user-space:
2675 */
2676static DEFINE_MUTEX(tracing_cpumask_update_lock);
2677
2678/*
2679 * Temporary storage for the character representation of the
2680 * CPU bitmask (and one more byte for the newline):
2681 */
2682static char mask_str[NR_CPUS + 1];
2683
Ingo Molnarc7078de2008-05-12 21:20:52 +02002684static ssize_t
2685tracing_cpumask_read(struct file *filp, char __user *ubuf,
2686 size_t count, loff_t *ppos)
2687{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002688 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002689
2690 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002691
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302692 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002693 if (count - len < 2) {
2694 count = -EINVAL;
2695 goto out_err;
2696 }
2697 len += sprintf(mask_str + len, "\n");
2698 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2699
2700out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002701 mutex_unlock(&tracing_cpumask_update_lock);
2702
2703 return count;
2704}
2705
2706static ssize_t
2707tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2708 size_t count, loff_t *ppos)
2709{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002710 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302711 cpumask_var_t tracing_cpumask_new;
2712
2713 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2714 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002715
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302716 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002717 if (err)
2718 goto err_unlock;
2719
Li Zefan215368e2009-06-15 10:56:42 +08002720 mutex_lock(&tracing_cpumask_update_lock);
2721
Steven Rostedta5e25882008-12-02 15:34:05 -05002722 local_irq_disable();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002723 arch_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002724 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002725 /*
2726 * Increase/decrease the disabled counter if we are
2727 * about to flip a bit in the cpumask:
2728 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302729 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2730 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002731 atomic_inc(&global_trace.data[cpu]->disabled);
2732 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302733 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2734 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002735 atomic_dec(&global_trace.data[cpu]->disabled);
2736 }
2737 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01002738 arch_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002739 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002740
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302741 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002742
Ingo Molnarc7078de2008-05-12 21:20:52 +02002743 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302744 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002745
Ingo Molnarc7078de2008-05-12 21:20:52 +02002746 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002747
2748err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08002749 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002750
2751 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002752}
2753
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002754static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002755 .open = tracing_open_generic,
2756 .read = tracing_cpumask_read,
2757 .write = tracing_cpumask_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002758 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002759};
2760
Li Zefanfdb372e2009-12-08 11:15:59 +08002761static int tracing_trace_options_show(struct seq_file *m, void *v)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002762{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002763 struct tracer_opt *trace_opts;
2764 u32 tracer_flags;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002765 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002766
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002767 mutex_lock(&trace_types_lock);
2768 tracer_flags = current_trace->flags->val;
2769 trace_opts = current_trace->flags->opts;
2770
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002771 for (i = 0; trace_options[i]; i++) {
2772 if (trace_flags & (1 << i))
Li Zefanfdb372e2009-12-08 11:15:59 +08002773 seq_printf(m, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002774 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002775 seq_printf(m, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002776 }
2777
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002778 for (i = 0; trace_opts[i].name; i++) {
2779 if (tracer_flags & trace_opts[i].bit)
Li Zefanfdb372e2009-12-08 11:15:59 +08002780 seq_printf(m, "%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002781 else
Li Zefanfdb372e2009-12-08 11:15:59 +08002782 seq_printf(m, "no%s\n", trace_opts[i].name);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002783 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002784 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002785
Li Zefanfdb372e2009-12-08 11:15:59 +08002786 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002787}
2788
Li Zefan8d18eaa2009-12-08 11:17:06 +08002789static int __set_tracer_option(struct tracer *trace,
2790 struct tracer_flags *tracer_flags,
2791 struct tracer_opt *opts, int neg)
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002792{
Li Zefan8d18eaa2009-12-08 11:17:06 +08002793 int ret;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002794
Li Zefan8d18eaa2009-12-08 11:17:06 +08002795 ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002796 if (ret)
2797 return ret;
2798
2799 if (neg)
Zhaolei77708412009-08-07 18:53:21 +08002800 tracer_flags->val &= ~opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002801 else
Zhaolei77708412009-08-07 18:53:21 +08002802 tracer_flags->val |= opts->bit;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002803 return 0;
2804}
2805
Li Zefan8d18eaa2009-12-08 11:17:06 +08002806/* Try to assign a tracer specific option */
2807static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2808{
2809 struct tracer_flags *tracer_flags = trace->flags;
2810 struct tracer_opt *opts = NULL;
2811 int i;
2812
2813 for (i = 0; tracer_flags->opts[i].name; i++) {
2814 opts = &tracer_flags->opts[i];
2815
2816 if (strcmp(cmp, opts->name) == 0)
2817 return __set_tracer_option(trace, trace->flags,
2818 opts, neg);
2819 }
2820
2821 return -EINVAL;
2822}
2823
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002824static void set_tracer_flags(unsigned int mask, int enabled)
2825{
2826 /* do nothing if flag is already set */
2827 if (!!(trace_flags & mask) == !!enabled)
2828 return;
2829
2830 if (enabled)
2831 trace_flags |= mask;
2832 else
2833 trace_flags &= ~mask;
Li Zefane870e9a2010-07-02 11:07:32 +08002834
2835 if (mask == TRACE_ITER_RECORD_CMD)
2836 trace_event_enable_cmd_record(enabled);
David Sharp750912f2010-12-08 13:46:47 -08002837
2838 if (mask == TRACE_ITER_OVERWRITE)
2839 ring_buffer_change_overwrite(global_trace.buffer, enabled);
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002840}
2841
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002842static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002843tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002844 size_t cnt, loff_t *ppos)
2845{
2846 char buf[64];
Li Zefan8d18eaa2009-12-08 11:17:06 +08002847 char *cmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002848 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002849 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002850 int i;
2851
Steven Rostedtcffae432008-05-12 21:21:00 +02002852 if (cnt >= sizeof(buf))
2853 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002854
2855 if (copy_from_user(&buf, ubuf, cnt))
2856 return -EFAULT;
2857
2858 buf[cnt] = 0;
Li Zefan8d18eaa2009-12-08 11:17:06 +08002859 cmp = strstrip(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002860
Li Zefan8d18eaa2009-12-08 11:17:06 +08002861 if (strncmp(cmp, "no", 2) == 0) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002862 neg = 1;
2863 cmp += 2;
2864 }
2865
2866 for (i = 0; trace_options[i]; i++) {
Li Zefan8d18eaa2009-12-08 11:17:06 +08002867 if (strcmp(cmp, trace_options[i]) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002868 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002869 break;
2870 }
2871 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002872
2873 /* If no option could be set, test the specific tracer options */
2874 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002875 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002876 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002877 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002878 if (ret)
2879 return ret;
2880 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002881
Jiri Olsacf8517c2009-10-23 19:36:16 -04002882 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002883
2884 return cnt;
2885}
2886
Li Zefanfdb372e2009-12-08 11:15:59 +08002887static int tracing_trace_options_open(struct inode *inode, struct file *file)
2888{
2889 if (tracing_disabled)
2890 return -ENODEV;
2891 return single_open(file, tracing_trace_options_show, NULL);
2892}
2893
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002894static const struct file_operations tracing_iter_fops = {
Li Zefanfdb372e2009-12-08 11:15:59 +08002895 .open = tracing_trace_options_open,
2896 .read = seq_read,
2897 .llseek = seq_lseek,
2898 .release = single_release,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002899 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002900};
2901
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002902static const char readme_msg[] =
2903 "tracing mini-HOWTO:\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002904 "# mount -t debugfs nodev /sys/kernel/debug\n\n"
2905 "# cat /sys/kernel/debug/tracing/available_tracers\n"
Geunsik Lim1e42e832012-02-08 19:05:36 +09002906 "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002907 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302908 "nop\n"
Geunsik Lim1e42e832012-02-08 19:05:36 +09002909 "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002910 "# cat /sys/kernel/debug/tracing/current_tracer\n"
Geunsik Lim1e42e832012-02-08 19:05:36 +09002911 "wakeup\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002912 "# cat /sys/kernel/debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002913 "noprint-parent nosym-offset nosym-addr noverbose\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002914 "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
Geunsik Lim9b5f8b32011-08-12 14:30:22 +09002915 "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
GeunSik Lim156f5a72009-06-02 15:01:37 +09002916 "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
Geunsik Lim9b5f8b32011-08-12 14:30:22 +09002917 "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002918;
2919
2920static ssize_t
2921tracing_readme_read(struct file *filp, char __user *ubuf,
2922 size_t cnt, loff_t *ppos)
2923{
2924 return simple_read_from_buffer(ubuf, cnt, ppos,
2925 readme_msg, strlen(readme_msg));
2926}
2927
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002928static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002929 .open = tracing_open_generic,
2930 .read = tracing_readme_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002931 .llseek = generic_file_llseek,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002932};
2933
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002934static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04002935tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2936 size_t cnt, loff_t *ppos)
2937{
2938 char *buf_comm;
2939 char *file_buf;
2940 char *buf;
2941 int len = 0;
2942 int pid;
2943 int i;
2944
2945 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2946 if (!file_buf)
2947 return -ENOMEM;
2948
2949 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2950 if (!buf_comm) {
2951 kfree(file_buf);
2952 return -ENOMEM;
2953 }
2954
2955 buf = file_buf;
2956
2957 for (i = 0; i < SAVED_CMDLINES; i++) {
2958 int r;
2959
2960 pid = map_cmdline_to_pid[i];
2961 if (pid == -1 || pid == NO_CMDLINE_MAP)
2962 continue;
2963
2964 trace_find_cmdline(pid, buf_comm);
2965 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2966 buf += r;
2967 len += r;
2968 }
2969
2970 len = simple_read_from_buffer(ubuf, cnt, ppos,
2971 file_buf, len);
2972
2973 kfree(file_buf);
2974 kfree(buf_comm);
2975
2976 return len;
2977}
2978
2979static const struct file_operations tracing_saved_cmdlines_fops = {
2980 .open = tracing_open_generic,
2981 .read = tracing_saved_cmdlines_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02002982 .llseek = generic_file_llseek,
Avadh Patel69abe6a2009-04-10 16:04:48 -04002983};
2984
2985static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002986tracing_ctrl_read(struct file *filp, char __user *ubuf,
2987 size_t cnt, loff_t *ppos)
2988{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002989 char buf[64];
2990 int r;
2991
Steven Rostedt90369902008-11-05 16:05:44 -05002992 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002993 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002994}
2995
2996static ssize_t
2997tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2998 size_t cnt, loff_t *ppos)
2999{
3000 struct trace_array *tr = filp->private_data;
Hannes Eder5e398412009-02-10 19:44:34 +01003001 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003002 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003003
Peter Huewe22fe9b52011-06-07 21:58:27 +02003004 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3005 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003006 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003007
3008 val = !!val;
3009
3010 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05003011 if (tracer_enabled ^ val) {
Steven Rostedt6752ab42011-02-08 13:54:06 -05003012
3013 /* Only need to warn if this is used to change the state */
3014 WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
3015
Steven Rostedt90369902008-11-05 16:05:44 -05003016 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003017 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05003018 if (current_trace->start)
3019 current_trace->start(tr);
3020 tracing_start();
3021 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003022 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05003023 tracing_stop();
3024 if (current_trace->stop)
3025 current_trace->stop(tr);
3026 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003027 }
3028 mutex_unlock(&trace_types_lock);
3029
Jiri Olsacf8517c2009-10-23 19:36:16 -04003030 *ppos += cnt;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003031
3032 return cnt;
3033}
3034
3035static ssize_t
3036tracing_set_trace_read(struct file *filp, char __user *ubuf,
3037 size_t cnt, loff_t *ppos)
3038{
Li Zefanee6c2c12009-09-18 14:06:47 +08003039 char buf[MAX_TRACER_SIZE+2];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003040 int r;
3041
3042 mutex_lock(&trace_types_lock);
3043 if (current_trace)
3044 r = sprintf(buf, "%s\n", current_trace->name);
3045 else
3046 r = sprintf(buf, "\n");
3047 mutex_unlock(&trace_types_lock);
3048
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003049 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003050}
3051
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003052int tracer_init(struct tracer *t, struct trace_array *tr)
3053{
3054 tracing_reset_online_cpus(tr);
3055 return t->init(tr);
3056}
3057
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003058static void set_buffer_entries(struct trace_array *tr, unsigned long val)
3059{
3060 int cpu;
3061 for_each_tracing_cpu(cpu)
3062 tr->data[cpu]->entries = val;
3063}
3064
3065static int __tracing_resize_ring_buffer(unsigned long size, int cpu)
Steven Rostedt73c51622009-03-11 13:42:01 -04003066{
3067 int ret;
3068
3069 /*
3070 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04003071 * we use the size that was given, and we can forget about
3072 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04003073 */
3074 ring_buffer_expanded = 1;
3075
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003076 ret = ring_buffer_resize(global_trace.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003077 if (ret < 0)
3078 return ret;
3079
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003080 if (!current_trace->use_max_tr)
3081 goto out;
3082
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003083 ret = ring_buffer_resize(max_tr.buffer, size, cpu);
Steven Rostedt73c51622009-03-11 13:42:01 -04003084 if (ret < 0) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003085 int r = 0;
Steven Rostedt73c51622009-03-11 13:42:01 -04003086
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003087 if (cpu == RING_BUFFER_ALL_CPUS) {
3088 int i;
3089 for_each_tracing_cpu(i) {
3090 r = ring_buffer_resize(global_trace.buffer,
3091 global_trace.data[i]->entries,
3092 i);
3093 if (r < 0)
3094 break;
3095 }
3096 } else {
3097 r = ring_buffer_resize(global_trace.buffer,
3098 global_trace.data[cpu]->entries,
3099 cpu);
3100 }
3101
Steven Rostedt73c51622009-03-11 13:42:01 -04003102 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04003103 /*
3104 * AARGH! We are left with different
3105 * size max buffer!!!!
3106 * The max buffer is our "snapshot" buffer.
3107 * When a tracer needs a snapshot (one of the
3108 * latency tracers), it swaps the max buffer
3109 * with the saved snap shot. We succeeded to
3110 * update the size of the main buffer, but failed to
3111 * update the size of the max buffer. But when we tried
3112 * to reset the main buffer to the original size, we
3113 * failed there too. This is very unlikely to
3114 * happen, but if it does, warn and kill all
3115 * tracing.
3116 */
Steven Rostedt73c51622009-03-11 13:42:01 -04003117 WARN_ON(1);
3118 tracing_disabled = 1;
3119 }
3120 return ret;
3121 }
3122
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003123 if (cpu == RING_BUFFER_ALL_CPUS)
3124 set_buffer_entries(&max_tr, size);
3125 else
3126 max_tr.data[cpu]->entries = size;
3127
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003128 out:
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003129 if (cpu == RING_BUFFER_ALL_CPUS)
3130 set_buffer_entries(&global_trace, size);
3131 else
3132 global_trace.data[cpu]->entries = size;
Steven Rostedt73c51622009-03-11 13:42:01 -04003133
3134 return ret;
3135}
3136
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003137static ssize_t tracing_resize_ring_buffer(unsigned long size, int cpu_id)
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003138{
3139 int cpu, ret = size;
3140
3141 mutex_lock(&trace_types_lock);
3142
3143 tracing_stop();
3144
3145 /* disable all cpu buffers */
3146 for_each_tracing_cpu(cpu) {
3147 if (global_trace.data[cpu])
3148 atomic_inc(&global_trace.data[cpu]->disabled);
3149 if (max_tr.data[cpu])
3150 atomic_inc(&max_tr.data[cpu]->disabled);
3151 }
3152
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003153 if (cpu_id != RING_BUFFER_ALL_CPUS) {
3154 /* make sure, this cpu is enabled in the mask */
3155 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
3156 ret = -EINVAL;
3157 goto out;
3158 }
3159 }
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003160
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003161 ret = __tracing_resize_ring_buffer(size, cpu_id);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003162 if (ret < 0)
3163 ret = -ENOMEM;
3164
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003165out:
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003166 for_each_tracing_cpu(cpu) {
3167 if (global_trace.data[cpu])
3168 atomic_dec(&global_trace.data[cpu]->disabled);
3169 if (max_tr.data[cpu])
3170 atomic_dec(&max_tr.data[cpu]->disabled);
3171 }
3172
3173 tracing_start();
3174 mutex_unlock(&trace_types_lock);
3175
3176 return ret;
3177}
3178
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003179
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003180/**
3181 * tracing_update_buffers - used by tracing facility to expand ring buffers
3182 *
3183 * To save on memory when the tracing is never used on a system with it
3184 * configured in. The ring buffers are set to a minimum size. But once
3185 * a user starts to use the tracing facility, then they need to grow
3186 * to their default size.
3187 *
3188 * This function is to be called when a tracer is about to be used.
3189 */
3190int tracing_update_buffers(void)
3191{
3192 int ret = 0;
3193
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003194 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003195 if (!ring_buffer_expanded)
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003196 ret = __tracing_resize_ring_buffer(trace_buf_size,
3197 RING_BUFFER_ALL_CPUS);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003198 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04003199
3200 return ret;
3201}
3202
Steven Rostedt577b7852009-02-26 23:43:05 -05003203struct trace_option_dentry;
3204
3205static struct trace_option_dentry *
3206create_trace_option_files(struct tracer *tracer);
3207
3208static void
3209destroy_trace_option_files(struct trace_option_dentry *topts);
3210
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003211static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003212{
Steven Rostedt577b7852009-02-26 23:43:05 -05003213 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003214 struct trace_array *tr = &global_trace;
3215 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01003216 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003217
Steven Rostedt1027fcb2009-03-12 11:33:20 -04003218 mutex_lock(&trace_types_lock);
3219
Steven Rostedt73c51622009-03-11 13:42:01 -04003220 if (!ring_buffer_expanded) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003221 ret = __tracing_resize_ring_buffer(trace_buf_size,
3222 RING_BUFFER_ALL_CPUS);
Steven Rostedt73c51622009-03-11 13:42:01 -04003223 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01003224 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04003225 ret = 0;
3226 }
3227
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003228 for (t = trace_types; t; t = t->next) {
3229 if (strcmp(t->name, buf) == 0)
3230 break;
3231 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02003232 if (!t) {
3233 ret = -EINVAL;
3234 goto out;
3235 }
3236 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003237 goto out;
3238
Steven Rostedt9f029e82008-11-12 15:24:24 -05003239 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003240 if (current_trace && current_trace->reset)
3241 current_trace->reset(tr);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003242 if (current_trace && current_trace->use_max_tr) {
3243 /*
3244 * We don't free the ring buffer. instead, resize it because
3245 * The max_tr ring buffer has some state (e.g. ring->clock) and
3246 * we want preserve it.
3247 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003248 ring_buffer_resize(max_tr.buffer, 1, RING_BUFFER_ALL_CPUS);
3249 set_buffer_entries(&max_tr, 1);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003250 }
Steven Rostedt577b7852009-02-26 23:43:05 -05003251 destroy_trace_option_files(topts);
3252
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003253 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05003254
3255 topts = create_trace_option_files(current_trace);
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003256 if (current_trace->use_max_tr) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003257 int cpu;
3258 /* we need to make per cpu buffer sizes equivalent */
3259 for_each_tracing_cpu(cpu) {
3260 ret = ring_buffer_resize(max_tr.buffer,
3261 global_trace.data[cpu]->entries,
3262 cpu);
3263 if (ret < 0)
3264 goto out;
3265 max_tr.data[cpu]->entries =
3266 global_trace.data[cpu]->entries;
3267 }
KOSAKI Motohiroef710e12010-07-01 14:34:35 +09003268 }
Steven Rostedt577b7852009-02-26 23:43:05 -05003269
Frederic Weisbecker1c800252008-11-16 05:57:26 +01003270 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02003271 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01003272 if (ret)
3273 goto out;
3274 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003275
Steven Rostedt9f029e82008-11-12 15:24:24 -05003276 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003277 out:
3278 mutex_unlock(&trace_types_lock);
3279
Peter Zijlstrad9e54072008-11-01 19:57:37 +01003280 return ret;
3281}
3282
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003283static ssize_t
3284tracing_set_trace_write(struct file *filp, const char __user *ubuf,
3285 size_t cnt, loff_t *ppos)
3286{
Li Zefanee6c2c12009-09-18 14:06:47 +08003287 char buf[MAX_TRACER_SIZE+1];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003288 int i;
3289 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01003290 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003291
Steven Rostedt60063a62008-10-28 10:44:24 -04003292 ret = cnt;
3293
Li Zefanee6c2c12009-09-18 14:06:47 +08003294 if (cnt > MAX_TRACER_SIZE)
3295 cnt = MAX_TRACER_SIZE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003296
3297 if (copy_from_user(&buf, ubuf, cnt))
3298 return -EFAULT;
3299
3300 buf[cnt] = 0;
3301
3302 /* strip ending whitespace. */
3303 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
3304 buf[i] = 0;
3305
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01003306 err = tracing_set_tracer(buf);
3307 if (err)
3308 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003309
Jiri Olsacf8517c2009-10-23 19:36:16 -04003310 *ppos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003311
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02003312 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003313}
3314
3315static ssize_t
3316tracing_max_lat_read(struct file *filp, char __user *ubuf,
3317 size_t cnt, loff_t *ppos)
3318{
3319 unsigned long *ptr = filp->private_data;
3320 char buf[64];
3321 int r;
3322
Steven Rostedtcffae432008-05-12 21:21:00 +02003323 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003324 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02003325 if (r > sizeof(buf))
3326 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003327 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003328}
3329
3330static ssize_t
3331tracing_max_lat_write(struct file *filp, const char __user *ubuf,
3332 size_t cnt, loff_t *ppos)
3333{
Hannes Eder5e398412009-02-10 19:44:34 +01003334 unsigned long *ptr = filp->private_data;
Hannes Eder5e398412009-02-10 19:44:34 +01003335 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003336 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003337
Peter Huewe22fe9b52011-06-07 21:58:27 +02003338 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3339 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003340 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003341
3342 *ptr = val * 1000;
3343
3344 return cnt;
3345}
3346
Steven Rostedtb3806b42008-05-12 21:20:46 +02003347static int tracing_open_pipe(struct inode *inode, struct file *filp)
3348{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003349 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003350 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003351 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003352
3353 if (tracing_disabled)
3354 return -ENODEV;
3355
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003356 mutex_lock(&trace_types_lock);
3357
Steven Rostedtb3806b42008-05-12 21:20:46 +02003358 /* create a buffer to store the information to pass to userspace */
3359 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003360 if (!iter) {
3361 ret = -ENOMEM;
3362 goto out;
3363 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003364
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003365 /*
3366 * We make a copy of the current tracer to avoid concurrent
3367 * changes on it while we are reading.
3368 */
3369 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
3370 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003371 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003372 goto fail;
3373 }
3374 if (current_trace)
3375 *iter->trace = *current_trace;
3376
3377 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
3378 ret = -ENOMEM;
3379 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10303380 }
3381
Steven Rostedta3097202008-11-07 22:36:02 -05003382 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10303383 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05003384
Steven Rostedt112f38a72009-06-01 15:16:05 -04003385 if (trace_flags & TRACE_ITER_LATENCY_FMT)
3386 iter->iter_flags |= TRACE_FILE_LAT_FMT;
3387
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003388 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003389 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003390 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003391 filp->private_data = iter;
3392
Steven Rostedt107bad82008-05-12 21:21:01 +02003393 if (iter->trace->pipe_open)
3394 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02003395
Arnd Bergmannb4447862010-07-07 23:40:11 +02003396 nonseekable_open(inode, filp);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003397out:
3398 mutex_unlock(&trace_types_lock);
3399 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003400
3401fail:
3402 kfree(iter->trace);
3403 kfree(iter);
3404 mutex_unlock(&trace_types_lock);
3405 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003406}
3407
3408static int tracing_release_pipe(struct inode *inode, struct file *file)
3409{
3410 struct trace_iterator *iter = file->private_data;
3411
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003412 mutex_lock(&trace_types_lock);
3413
Steven Rostedt29bf4a52009-12-09 12:37:43 -05003414 if (iter->trace->pipe_close)
Steven Rostedtc521efd2009-12-07 09:06:24 -05003415 iter->trace->pipe_close(iter);
3416
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003417 mutex_unlock(&trace_types_lock);
3418
Rusty Russell44623442009-01-01 10:12:23 +10303419 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003420 mutex_destroy(&iter->mutex);
3421 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003422 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003423
3424 return 0;
3425}
3426
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003427static unsigned int
3428tracing_poll_pipe(struct file *filp, poll_table *poll_table)
3429{
3430 struct trace_iterator *iter = filp->private_data;
3431
3432 if (trace_flags & TRACE_ITER_BLOCK) {
3433 /*
3434 * Always select as readable when in blocking mode
3435 */
3436 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02003437 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003438 if (!trace_empty(iter))
3439 return POLLIN | POLLRDNORM;
3440 poll_wait(filp, &trace_wait, poll_table);
3441 if (!trace_empty(iter))
3442 return POLLIN | POLLRDNORM;
3443
3444 return 0;
3445 }
3446}
3447
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003448
3449void default_wait_pipe(struct trace_iterator *iter)
3450{
3451 DEFINE_WAIT(wait);
3452
3453 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
3454
3455 if (trace_empty(iter))
3456 schedule();
3457
3458 finish_wait(&trace_wait, &wait);
3459}
3460
3461/*
3462 * This is a make-shift waitqueue.
3463 * A tracer might use this callback on some rare cases:
3464 *
3465 * 1) the current tracer might hold the runqueue lock when it wakes up
3466 * a reader, hence a deadlock (sched, function, and function graph tracers)
3467 * 2) the function tracers, trace all functions, we don't want
3468 * the overhead of calling wake_up and friends
3469 * (and tracing them too)
3470 *
3471 * Anyway, this is really very primitive wakeup.
3472 */
3473void poll_wait_pipe(struct trace_iterator *iter)
3474{
3475 set_current_state(TASK_INTERRUPTIBLE);
3476 /* sleep for 100 msecs, and try again. */
3477 schedule_timeout(HZ / 10);
3478}
3479
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003480/* Must be called with trace_types_lock mutex held. */
3481static int tracing_wait_pipe(struct file *filp)
3482{
3483 struct trace_iterator *iter = filp->private_data;
3484
3485 while (trace_empty(iter)) {
3486
3487 if ((filp->f_flags & O_NONBLOCK)) {
3488 return -EAGAIN;
3489 }
3490
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003491 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003492
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003493 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003494
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003495 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003496
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01003497 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003498 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003499
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003500 /*
3501 * We block until we read something and tracing is disabled.
3502 * We still block if tracing is disabled, but we have never
3503 * read anything. This allows a user to cat this file, and
3504 * then enable tracing. But after we have read something,
3505 * we give an EOF when tracing is again disabled.
3506 *
3507 * iter->pos will be 0 if we haven't read anything.
3508 */
3509 if (!tracer_enabled && iter->pos)
3510 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003511 }
3512
3513 return 1;
3514}
3515
Steven Rostedtb3806b42008-05-12 21:20:46 +02003516/*
3517 * Consumer reader.
3518 */
3519static ssize_t
3520tracing_read_pipe(struct file *filp, char __user *ubuf,
3521 size_t cnt, loff_t *ppos)
3522{
3523 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003524 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003525 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003526
3527 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003528 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3529 if (sret != -EBUSY)
3530 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003531
Steven Rostedtf9520752009-03-02 14:04:40 -05003532 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003533
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003534 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02003535 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003536 if (unlikely(old_tracer != current_trace && current_trace)) {
3537 old_tracer = current_trace;
3538 *iter->trace = *current_trace;
3539 }
3540 mutex_unlock(&trace_types_lock);
3541
3542 /*
3543 * Avoid more than one consumer on a single file descriptor
3544 * This is just a matter of traces coherency, the ring buffer itself
3545 * is protected.
3546 */
3547 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003548 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003549 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
3550 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02003551 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02003552 }
3553
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003554waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003555 sret = tracing_wait_pipe(filp);
3556 if (sret <= 0)
3557 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003558
3559 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003560 if (trace_empty(iter)) {
3561 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003562 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003563 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003564
3565 if (cnt >= PAGE_SIZE)
3566 cnt = PAGE_SIZE - 1;
3567
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003568 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003569 memset(&iter->seq, 0,
3570 sizeof(struct trace_iterator) -
3571 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02003572 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003573
Lai Jiangshan4f535962009-05-18 19:35:34 +08003574 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003575 trace_access_lock(iter->cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -05003576 while (trace_find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003577 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003578 int len = iter->seq.len;
3579
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003580 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003581 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02003582 /* don't print partial lines */
3583 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003584 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003585 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01003586 if (ret != TRACE_TYPE_NO_CONSUME)
3587 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003588
3589 if (iter->seq.len >= cnt)
3590 break;
Jiri Olsaee5e51f2011-03-25 12:05:18 +01003591
3592 /*
3593 * Setting the full flag means we reached the trace_seq buffer
3594 * size and we should leave by partial output condition above.
3595 * One of the trace_seq_* functions is not used properly.
3596 */
3597 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
3598 iter->ent->type);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003599 }
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003600 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003601 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02003602
Steven Rostedtb3806b42008-05-12 21:20:46 +02003603 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003604 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3605 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05003606 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003607
3608 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003609 * If there was nothing to send to user, in spite of consuming trace
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003610 * entries, go back to wait for more entries.
3611 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003612 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003613 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003614
Steven Rostedt107bad82008-05-12 21:21:01 +02003615out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003616 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003617
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003618 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003619}
3620
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003621static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3622 struct pipe_buffer *buf)
3623{
3624 __free_page(buf->page);
3625}
3626
3627static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3628 unsigned int idx)
3629{
3630 __free_page(spd->pages[idx]);
3631}
3632
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08003633static const struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003634 .can_merge = 0,
3635 .map = generic_pipe_buf_map,
3636 .unmap = generic_pipe_buf_unmap,
3637 .confirm = generic_pipe_buf_confirm,
3638 .release = tracing_pipe_buf_release,
3639 .steal = generic_pipe_buf_steal,
3640 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003641};
3642
Steven Rostedt34cd4992009-02-09 12:06:29 -05003643static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003644tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003645{
3646 size_t count;
3647 int ret;
3648
3649 /* Seq buffer is page-sized, exactly what we need. */
3650 for (;;) {
3651 count = iter->seq.len;
3652 ret = print_trace_line(iter);
3653 count = iter->seq.len - count;
3654 if (rem < count) {
3655 rem = 0;
3656 iter->seq.len -= count;
3657 break;
3658 }
3659 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3660 iter->seq.len -= count;
3661 break;
3662 }
3663
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08003664 if (ret != TRACE_TYPE_NO_CONSUME)
3665 trace_consume(iter);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003666 rem -= count;
Jason Wessel955b61e2010-08-05 09:22:23 -05003667 if (!trace_find_next_entry_inc(iter)) {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003668 rem = 0;
3669 iter->ent = NULL;
3670 break;
3671 }
3672 }
3673
3674 return rem;
3675}
3676
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003677static ssize_t tracing_splice_read_pipe(struct file *filp,
3678 loff_t *ppos,
3679 struct pipe_inode_info *pipe,
3680 size_t len,
3681 unsigned int flags)
3682{
Jens Axboe35f3d142010-05-20 10:43:18 +02003683 struct page *pages_def[PIPE_DEF_BUFFERS];
3684 struct partial_page partial_def[PIPE_DEF_BUFFERS];
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003685 struct trace_iterator *iter = filp->private_data;
3686 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02003687 .pages = pages_def,
3688 .partial = partial_def,
Steven Rostedt34cd4992009-02-09 12:06:29 -05003689 .nr_pages = 0, /* This gets updated below. */
3690 .flags = flags,
3691 .ops = &tracing_pipe_buf_ops,
3692 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003693 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003694 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003695 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003696 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003697 unsigned int i;
3698
Jens Axboe35f3d142010-05-20 10:43:18 +02003699 if (splice_grow_spd(pipe, &spd))
3700 return -ENOMEM;
3701
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003702 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003703 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003704 if (unlikely(old_tracer != current_trace && current_trace)) {
3705 old_tracer = current_trace;
3706 *iter->trace = *current_trace;
3707 }
3708 mutex_unlock(&trace_types_lock);
3709
3710 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003711
3712 if (iter->trace->splice_read) {
3713 ret = iter->trace->splice_read(iter, filp,
3714 ppos, pipe, len, flags);
3715 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003716 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003717 }
3718
3719 ret = tracing_wait_pipe(filp);
3720 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003721 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003722
Jason Wessel955b61e2010-08-05 09:22:23 -05003723 if (!iter->ent && !trace_find_next_entry_inc(iter)) {
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003724 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003725 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003726 }
3727
Lai Jiangshan4f535962009-05-18 19:35:34 +08003728 trace_event_read_lock();
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003729 trace_access_lock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003730
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003731 /* Fill as many pages as possible. */
Jens Axboe35f3d142010-05-20 10:43:18 +02003732 for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
3733 spd.pages[i] = alloc_page(GFP_KERNEL);
3734 if (!spd.pages[i])
Steven Rostedt34cd4992009-02-09 12:06:29 -05003735 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003736
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003737 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003738
3739 /* Copy the data into the page, so we can start over. */
3740 ret = trace_seq_to_buffer(&iter->seq,
Jens Axboe35f3d142010-05-20 10:43:18 +02003741 page_address(spd.pages[i]),
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003742 iter->seq.len);
3743 if (ret < 0) {
Jens Axboe35f3d142010-05-20 10:43:18 +02003744 __free_page(spd.pages[i]);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003745 break;
3746 }
Jens Axboe35f3d142010-05-20 10:43:18 +02003747 spd.partial[i].offset = 0;
3748 spd.partial[i].len = iter->seq.len;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003749
Steven Rostedtf9520752009-03-02 14:04:40 -05003750 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003751 }
3752
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08003753 trace_access_unlock(iter->cpu_file);
Lai Jiangshan4f535962009-05-18 19:35:34 +08003754 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003755 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003756
3757 spd.nr_pages = i;
3758
Jens Axboe35f3d142010-05-20 10:43:18 +02003759 ret = splice_to_pipe(pipe, &spd);
3760out:
3761 splice_shrink_spd(pipe, &spd);
3762 return ret;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003763
Steven Rostedt34cd4992009-02-09 12:06:29 -05003764out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003765 mutex_unlock(&iter->mutex);
Jens Axboe35f3d142010-05-20 10:43:18 +02003766 goto out;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003767}
3768
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003769struct ftrace_entries_info {
3770 struct trace_array *tr;
3771 int cpu;
3772};
3773
3774static int tracing_entries_open(struct inode *inode, struct file *filp)
3775{
3776 struct ftrace_entries_info *info;
3777
3778 if (tracing_disabled)
3779 return -ENODEV;
3780
3781 info = kzalloc(sizeof(*info), GFP_KERNEL);
3782 if (!info)
3783 return -ENOMEM;
3784
3785 info->tr = &global_trace;
3786 info->cpu = (unsigned long)inode->i_private;
3787
3788 filp->private_data = info;
3789
3790 return 0;
3791}
3792
Steven Rostedta98a3c32008-05-12 21:20:59 +02003793static ssize_t
3794tracing_entries_read(struct file *filp, char __user *ubuf,
3795 size_t cnt, loff_t *ppos)
3796{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003797 struct ftrace_entries_info *info = filp->private_data;
3798 struct trace_array *tr = info->tr;
3799 char buf[64];
3800 int r = 0;
3801 ssize_t ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003802
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003803 mutex_lock(&trace_types_lock);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003804
3805 if (info->cpu == RING_BUFFER_ALL_CPUS) {
3806 int cpu, buf_size_same;
3807 unsigned long size;
3808
3809 size = 0;
3810 buf_size_same = 1;
3811 /* check if all cpu sizes are same */
3812 for_each_tracing_cpu(cpu) {
3813 /* fill in the size from first enabled cpu */
3814 if (size == 0)
3815 size = tr->data[cpu]->entries;
3816 if (size != tr->data[cpu]->entries) {
3817 buf_size_same = 0;
3818 break;
3819 }
3820 }
3821
3822 if (buf_size_same) {
3823 if (!ring_buffer_expanded)
3824 r = sprintf(buf, "%lu (expanded: %lu)\n",
3825 size >> 10,
3826 trace_buf_size >> 10);
3827 else
3828 r = sprintf(buf, "%lu\n", size >> 10);
3829 } else
3830 r = sprintf(buf, "X\n");
3831 } else
3832 r = sprintf(buf, "%lu\n", tr->data[info->cpu]->entries >> 10);
3833
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003834 mutex_unlock(&trace_types_lock);
3835
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003836 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3837 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003838}
3839
3840static ssize_t
3841tracing_entries_write(struct file *filp, const char __user *ubuf,
3842 size_t cnt, loff_t *ppos)
3843{
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003844 struct ftrace_entries_info *info = filp->private_data;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003845 unsigned long val;
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003846 int ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003847
Peter Huewe22fe9b52011-06-07 21:58:27 +02003848 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
3849 if (ret)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003850 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003851
3852 /* must have at least 1 entry */
3853 if (!val)
3854 return -EINVAL;
3855
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003856 /* value is in KB */
3857 val <<= 10;
3858
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003859 ret = tracing_resize_ring_buffer(val, info->cpu);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003860 if (ret < 0)
3861 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003862
Jiri Olsacf8517c2009-10-23 19:36:16 -04003863 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003864
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003865 return cnt;
3866}
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003867
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003868static int
3869tracing_entries_release(struct inode *inode, struct file *filp)
3870{
3871 struct ftrace_entries_info *info = filp->private_data;
3872
3873 kfree(info);
3874
3875 return 0;
3876}
3877
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003878static ssize_t
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07003879tracing_total_entries_read(struct file *filp, char __user *ubuf,
3880 size_t cnt, loff_t *ppos)
3881{
3882 struct trace_array *tr = filp->private_data;
3883 char buf[64];
3884 int r, cpu;
3885 unsigned long size = 0, expanded_size = 0;
3886
3887 mutex_lock(&trace_types_lock);
3888 for_each_tracing_cpu(cpu) {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003889 size += tr->data[cpu]->entries >> 10;
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07003890 if (!ring_buffer_expanded)
3891 expanded_size += trace_buf_size >> 10;
3892 }
3893 if (ring_buffer_expanded)
3894 r = sprintf(buf, "%lu\n", size);
3895 else
3896 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
3897 mutex_unlock(&trace_types_lock);
3898
3899 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3900}
3901
3902static ssize_t
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003903tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
3904 size_t cnt, loff_t *ppos)
3905{
3906 /*
3907 * There is no need to read what the user has written, this function
3908 * is just to make sure that there is no error when "echo" is used
3909 */
3910
3911 *ppos += cnt;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003912
3913 return cnt;
3914}
3915
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003916static int
3917tracing_free_buffer_release(struct inode *inode, struct file *filp)
3918{
Steven Rostedtcf30cf62011-06-14 22:44:07 -04003919 /* disable tracing ? */
3920 if (trace_flags & TRACE_ITER_STOP_ON_FREE)
3921 tracing_off();
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003922 /* resize the ring buffer to 0 */
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08003923 tracing_resize_ring_buffer(0, RING_BUFFER_ALL_CPUS);
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07003924
3925 return 0;
3926}
3927
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003928static ssize_t
3929tracing_mark_write(struct file *filp, const char __user *ubuf,
3930 size_t cnt, loff_t *fpos)
3931{
Steven Rostedtd696b582011-09-22 11:50:27 -04003932 unsigned long addr = (unsigned long)ubuf;
3933 struct ring_buffer_event *event;
3934 struct ring_buffer *buffer;
3935 struct print_entry *entry;
3936 unsigned long irq_flags;
3937 struct page *pages[2];
3938 int nr_pages = 1;
3939 ssize_t written;
3940 void *page1;
3941 void *page2;
3942 int offset;
3943 int size;
3944 int len;
3945 int ret;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003946
Steven Rostedtc76f0692008-11-07 22:36:02 -05003947 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003948 return -EINVAL;
3949
3950 if (cnt > TRACE_BUF_SIZE)
3951 cnt = TRACE_BUF_SIZE;
3952
Steven Rostedtd696b582011-09-22 11:50:27 -04003953 /*
3954 * Userspace is injecting traces into the kernel trace buffer.
3955 * We want to be as non intrusive as possible.
3956 * To do so, we do not want to allocate any special buffers
3957 * or take any locks, but instead write the userspace data
3958 * straight into the ring buffer.
3959 *
3960 * First we need to pin the userspace buffer into memory,
3961 * which, most likely it is, because it just referenced it.
3962 * But there's no guarantee that it is. By using get_user_pages_fast()
3963 * and kmap_atomic/kunmap_atomic() we can get access to the
3964 * pages directly. We then write the data directly into the
3965 * ring buffer.
3966 */
3967 BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003968
Steven Rostedtd696b582011-09-22 11:50:27 -04003969 /* check if we cross pages */
3970 if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
3971 nr_pages = 2;
3972
3973 offset = addr & (PAGE_SIZE - 1);
3974 addr &= PAGE_MASK;
3975
3976 ret = get_user_pages_fast(addr, nr_pages, 0, pages);
3977 if (ret < nr_pages) {
3978 while (--ret >= 0)
3979 put_page(pages[ret]);
3980 written = -EFAULT;
3981 goto out;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003982 }
3983
Steven Rostedtd696b582011-09-22 11:50:27 -04003984 page1 = kmap_atomic(pages[0]);
3985 if (nr_pages == 2)
3986 page2 = kmap_atomic(pages[1]);
3987
3988 local_save_flags(irq_flags);
3989 size = sizeof(*entry) + cnt + 2; /* possible \n added */
3990 buffer = global_trace.buffer;
3991 event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3992 irq_flags, preempt_count());
3993 if (!event) {
3994 /* Ring buffer disabled, return as if not open for write */
3995 written = -EBADF;
3996 goto out_unlock;
3997 }
3998
3999 entry = ring_buffer_event_data(event);
4000 entry->ip = _THIS_IP_;
4001
4002 if (nr_pages == 2) {
4003 len = PAGE_SIZE - offset;
4004 memcpy(&entry->buf, page1 + offset, len);
4005 memcpy(&entry->buf[len], page2, cnt - len);
4006 } else
4007 memcpy(&entry->buf, page1 + offset, cnt);
4008
4009 if (entry->buf[cnt - 1] != '\n') {
4010 entry->buf[cnt] = '\n';
4011 entry->buf[cnt + 1] = '\0';
4012 } else
4013 entry->buf[cnt] = '\0';
4014
4015 ring_buffer_unlock_commit(buffer, event);
4016
4017 written = cnt;
4018
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02004019 *fpos += written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004020
Steven Rostedtd696b582011-09-22 11:50:27 -04004021 out_unlock:
4022 if (nr_pages == 2)
4023 kunmap_atomic(page2);
4024 kunmap_atomic(page1);
4025 while (nr_pages > 0)
4026 put_page(pages[--nr_pages]);
4027 out:
Marcin Slusarz1aa54bc2010-07-28 01:18:01 +02004028 return written;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004029}
4030
Li Zefan13f16d22009-12-08 11:16:11 +08004031static int tracing_clock_show(struct seq_file *m, void *v)
Zhaolei5079f322009-08-25 16:12:56 +08004032{
Zhaolei5079f322009-08-25 16:12:56 +08004033 int i;
4034
4035 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
Li Zefan13f16d22009-12-08 11:16:11 +08004036 seq_printf(m,
Zhaolei5079f322009-08-25 16:12:56 +08004037 "%s%s%s%s", i ? " " : "",
4038 i == trace_clock_id ? "[" : "", trace_clocks[i].name,
4039 i == trace_clock_id ? "]" : "");
Li Zefan13f16d22009-12-08 11:16:11 +08004040 seq_putc(m, '\n');
Zhaolei5079f322009-08-25 16:12:56 +08004041
Li Zefan13f16d22009-12-08 11:16:11 +08004042 return 0;
Zhaolei5079f322009-08-25 16:12:56 +08004043}
4044
4045static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
4046 size_t cnt, loff_t *fpos)
4047{
4048 char buf[64];
4049 const char *clockstr;
4050 int i;
4051
4052 if (cnt >= sizeof(buf))
4053 return -EINVAL;
4054
4055 if (copy_from_user(&buf, ubuf, cnt))
4056 return -EFAULT;
4057
4058 buf[cnt] = 0;
4059
4060 clockstr = strstrip(buf);
4061
4062 for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
4063 if (strcmp(trace_clocks[i].name, clockstr) == 0)
4064 break;
4065 }
4066 if (i == ARRAY_SIZE(trace_clocks))
4067 return -EINVAL;
4068
4069 trace_clock_id = i;
4070
4071 mutex_lock(&trace_types_lock);
4072
4073 ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
4074 if (max_tr.buffer)
4075 ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
4076
4077 mutex_unlock(&trace_types_lock);
4078
4079 *fpos += cnt;
4080
4081 return cnt;
4082}
4083
Li Zefan13f16d22009-12-08 11:16:11 +08004084static int tracing_clock_open(struct inode *inode, struct file *file)
4085{
4086 if (tracing_disabled)
4087 return -ENODEV;
4088 return single_open(file, tracing_clock_show, NULL);
4089}
4090
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004091static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004092 .open = tracing_open_generic,
4093 .read = tracing_max_lat_read,
4094 .write = tracing_max_lat_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004095 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004096};
4097
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004098static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004099 .open = tracing_open_generic,
4100 .read = tracing_ctrl_read,
4101 .write = tracing_ctrl_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004102 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004103};
4104
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004105static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004106 .open = tracing_open_generic,
4107 .read = tracing_set_trace_read,
4108 .write = tracing_set_trace_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004109 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004110};
4111
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004112static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004113 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02004114 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004115 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02004116 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004117 .release = tracing_release_pipe,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004118 .llseek = no_llseek,
Steven Rostedtb3806b42008-05-12 21:20:46 +02004119};
4120
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004121static const struct file_operations tracing_entries_fops = {
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004122 .open = tracing_entries_open,
Steven Rostedta98a3c32008-05-12 21:20:59 +02004123 .read = tracing_entries_read,
4124 .write = tracing_entries_write,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004125 .release = tracing_entries_release,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004126 .llseek = generic_file_llseek,
Steven Rostedta98a3c32008-05-12 21:20:59 +02004127};
4128
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004129static const struct file_operations tracing_total_entries_fops = {
4130 .open = tracing_open_generic,
4131 .read = tracing_total_entries_read,
4132 .llseek = generic_file_llseek,
4133};
4134
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004135static const struct file_operations tracing_free_buffer_fops = {
4136 .write = tracing_free_buffer_write,
4137 .release = tracing_free_buffer_release,
4138};
4139
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004140static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004141 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004142 .write = tracing_mark_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004143 .llseek = generic_file_llseek,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004144};
4145
Zhaolei5079f322009-08-25 16:12:56 +08004146static const struct file_operations trace_clock_fops = {
Li Zefan13f16d22009-12-08 11:16:11 +08004147 .open = tracing_clock_open,
4148 .read = seq_read,
4149 .llseek = seq_lseek,
4150 .release = single_release,
Zhaolei5079f322009-08-25 16:12:56 +08004151 .write = tracing_clock_write,
4152};
4153
Steven Rostedt2cadf912008-12-01 22:20:19 -05004154struct ftrace_buffer_info {
4155 struct trace_array *tr;
4156 void *spare;
4157 int cpu;
4158 unsigned int read;
4159};
4160
4161static int tracing_buffers_open(struct inode *inode, struct file *filp)
4162{
4163 int cpu = (int)(long)inode->i_private;
4164 struct ftrace_buffer_info *info;
4165
4166 if (tracing_disabled)
4167 return -ENODEV;
4168
4169 info = kzalloc(sizeof(*info), GFP_KERNEL);
4170 if (!info)
4171 return -ENOMEM;
4172
4173 info->tr = &global_trace;
4174 info->cpu = cpu;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004175 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004176 /* Force reading ring buffer for first read */
4177 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004178
4179 filp->private_data = info;
4180
Lai Jiangshand1e7e022009-04-02 15:16:56 +08004181 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004182}
4183
4184static ssize_t
4185tracing_buffers_read(struct file *filp, char __user *ubuf,
4186 size_t count, loff_t *ppos)
4187{
4188 struct ftrace_buffer_info *info = filp->private_data;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004189 ssize_t ret;
4190 size_t size;
4191
Steven Rostedt2dc5d122009-03-04 19:10:05 -05004192 if (!count)
4193 return 0;
4194
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004195 if (!info->spare)
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004196 info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004197 if (!info->spare)
4198 return -ENOMEM;
4199
Steven Rostedt2cadf912008-12-01 22:20:19 -05004200 /* Do we have previous read data to read? */
4201 if (info->read < PAGE_SIZE)
4202 goto read;
4203
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004204 trace_access_lock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004205 ret = ring_buffer_read_page(info->tr->buffer,
4206 &info->spare,
4207 count,
4208 info->cpu, 0);
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004209 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004210 if (ret < 0)
4211 return 0;
4212
Steven Rostedt436fc282011-10-14 10:44:25 -04004213 info->read = 0;
4214
Steven Rostedt2cadf912008-12-01 22:20:19 -05004215read:
4216 size = PAGE_SIZE - info->read;
4217 if (size > count)
4218 size = count;
4219
4220 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05004221 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05004222 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05004223 size -= ret;
4224
Steven Rostedt2cadf912008-12-01 22:20:19 -05004225 *ppos += size;
4226 info->read += size;
4227
4228 return size;
4229}
4230
4231static int tracing_buffers_release(struct inode *inode, struct file *file)
4232{
4233 struct ftrace_buffer_info *info = file->private_data;
4234
Lai Jiangshanddd538f2009-04-02 15:16:59 +08004235 if (info->spare)
4236 ring_buffer_free_read_page(info->tr->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004237 kfree(info);
4238
4239 return 0;
4240}
4241
4242struct buffer_ref {
4243 struct ring_buffer *buffer;
4244 void *page;
4245 int ref;
4246};
4247
4248static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
4249 struct pipe_buffer *buf)
4250{
4251 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4252
4253 if (--ref->ref)
4254 return;
4255
4256 ring_buffer_free_read_page(ref->buffer, ref->page);
4257 kfree(ref);
4258 buf->private = 0;
4259}
4260
4261static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
4262 struct pipe_buffer *buf)
4263{
4264 return 1;
4265}
4266
4267static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
4268 struct pipe_buffer *buf)
4269{
4270 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
4271
4272 ref->ref++;
4273}
4274
4275/* Pipe buffer operations for a buffer. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -08004276static const struct pipe_buf_operations buffer_pipe_buf_ops = {
Steven Rostedt2cadf912008-12-01 22:20:19 -05004277 .can_merge = 0,
4278 .map = generic_pipe_buf_map,
4279 .unmap = generic_pipe_buf_unmap,
4280 .confirm = generic_pipe_buf_confirm,
4281 .release = buffer_pipe_buf_release,
4282 .steal = buffer_pipe_buf_steal,
4283 .get = buffer_pipe_buf_get,
4284};
4285
4286/*
4287 * Callback from splice_to_pipe(), if we need to release some pages
4288 * at the end of the spd in case we error'ed out in filling the pipe.
4289 */
4290static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
4291{
4292 struct buffer_ref *ref =
4293 (struct buffer_ref *)spd->partial[i].private;
4294
4295 if (--ref->ref)
4296 return;
4297
4298 ring_buffer_free_read_page(ref->buffer, ref->page);
4299 kfree(ref);
4300 spd->partial[i].private = 0;
4301}
4302
4303static ssize_t
4304tracing_buffers_splice_read(struct file *file, loff_t *ppos,
4305 struct pipe_inode_info *pipe, size_t len,
4306 unsigned int flags)
4307{
4308 struct ftrace_buffer_info *info = file->private_data;
Jens Axboe35f3d142010-05-20 10:43:18 +02004309 struct partial_page partial_def[PIPE_DEF_BUFFERS];
4310 struct page *pages_def[PIPE_DEF_BUFFERS];
Steven Rostedt2cadf912008-12-01 22:20:19 -05004311 struct splice_pipe_desc spd = {
Jens Axboe35f3d142010-05-20 10:43:18 +02004312 .pages = pages_def,
4313 .partial = partial_def,
Steven Rostedt2cadf912008-12-01 22:20:19 -05004314 .flags = flags,
4315 .ops = &buffer_pipe_buf_ops,
4316 .spd_release = buffer_spd_release,
4317 };
4318 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04004319 int entries, size, i;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004320 size_t ret;
4321
Jens Axboe35f3d142010-05-20 10:43:18 +02004322 if (splice_grow_spd(pipe, &spd))
4323 return -ENOMEM;
4324
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004325 if (*ppos & (PAGE_SIZE - 1)) {
4326 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02004327 ret = -EINVAL;
4328 goto out;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004329 }
4330
4331 if (len & (PAGE_SIZE - 1)) {
4332 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
Jens Axboe35f3d142010-05-20 10:43:18 +02004333 if (len < PAGE_SIZE) {
4334 ret = -EINVAL;
4335 goto out;
4336 }
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004337 len &= PAGE_MASK;
4338 }
4339
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004340 trace_access_lock(info->cpu);
Steven Rostedt93459c62009-04-29 00:23:13 -04004341 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
4342
Jens Axboe35f3d142010-05-20 10:43:18 +02004343 for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05004344 struct page *page;
4345 int r;
4346
4347 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
4348 if (!ref)
4349 break;
4350
Steven Rostedt7267fa62009-04-29 00:16:21 -04004351 ref->ref = 1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004352 ref->buffer = info->tr->buffer;
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004353 ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004354 if (!ref->page) {
4355 kfree(ref);
4356 break;
4357 }
4358
4359 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtf2957f12009-04-29 00:26:30 -04004360 len, info->cpu, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004361 if (r < 0) {
Vaibhav Nagarnaik7ea59062011-05-03 17:56:42 -07004362 ring_buffer_free_read_page(ref->buffer, ref->page);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004363 kfree(ref);
4364 break;
4365 }
4366
4367 /*
4368 * zero out any left over data, this is going to
4369 * user land.
4370 */
4371 size = ring_buffer_page_len(ref->page);
4372 if (size < PAGE_SIZE)
4373 memset(ref->page + size, 0, PAGE_SIZE - size);
4374
4375 page = virt_to_page(ref->page);
4376
4377 spd.pages[i] = page;
4378 spd.partial[i].len = PAGE_SIZE;
4379 spd.partial[i].offset = 0;
4380 spd.partial[i].private = (unsigned long)ref;
4381 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08004382 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04004383
4384 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004385 }
4386
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004387 trace_access_unlock(info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05004388 spd.nr_pages = i;
4389
4390 /* did we read anything? */
4391 if (!spd.nr_pages) {
4392 if (flags & SPLICE_F_NONBLOCK)
4393 ret = -EAGAIN;
4394 else
4395 ret = 0;
4396 /* TODO: block */
Jens Axboe35f3d142010-05-20 10:43:18 +02004397 goto out;
Steven Rostedt2cadf912008-12-01 22:20:19 -05004398 }
4399
4400 ret = splice_to_pipe(pipe, &spd);
Jens Axboe35f3d142010-05-20 10:43:18 +02004401 splice_shrink_spd(pipe, &spd);
4402out:
Steven Rostedt2cadf912008-12-01 22:20:19 -05004403 return ret;
4404}
4405
4406static const struct file_operations tracing_buffers_fops = {
4407 .open = tracing_buffers_open,
4408 .read = tracing_buffers_read,
4409 .release = tracing_buffers_release,
4410 .splice_read = tracing_buffers_splice_read,
4411 .llseek = no_llseek,
4412};
4413
Steven Rostedtc8d77182009-04-29 18:03:45 -04004414static ssize_t
4415tracing_stats_read(struct file *filp, char __user *ubuf,
4416 size_t count, loff_t *ppos)
4417{
4418 unsigned long cpu = (unsigned long)filp->private_data;
4419 struct trace_array *tr = &global_trace;
4420 struct trace_seq *s;
4421 unsigned long cnt;
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004422 unsigned long long t;
4423 unsigned long usec_rem;
Steven Rostedtc8d77182009-04-29 18:03:45 -04004424
Li Zefane4f2d102009-06-15 10:57:28 +08004425 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004426 if (!s)
Roel Kluina6463652009-11-11 22:26:35 +01004427 return -ENOMEM;
Steven Rostedtc8d77182009-04-29 18:03:45 -04004428
4429 trace_seq_init(s);
4430
4431 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
4432 trace_seq_printf(s, "entries: %ld\n", cnt);
4433
4434 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
4435 trace_seq_printf(s, "overrun: %ld\n", cnt);
4436
4437 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
4438 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
4439
Vaibhav Nagarnaikc64e1482011-08-16 14:46:16 -07004440 cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
4441 trace_seq_printf(s, "bytes: %ld\n", cnt);
4442
4443 t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
4444 usec_rem = do_div(t, USEC_PER_SEC);
4445 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
4446
4447 t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
4448 usec_rem = do_div(t, USEC_PER_SEC);
4449 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
4450
Steven Rostedtc8d77182009-04-29 18:03:45 -04004451 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
4452
4453 kfree(s);
4454
4455 return count;
4456}
4457
4458static const struct file_operations tracing_stats_fops = {
4459 .open = tracing_open_generic,
4460 .read = tracing_stats_read,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004461 .llseek = generic_file_llseek,
Steven Rostedtc8d77182009-04-29 18:03:45 -04004462};
4463
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004464#ifdef CONFIG_DYNAMIC_FTRACE
4465
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004466int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004467{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004468 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004469}
4470
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004471static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004472tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004473 size_t cnt, loff_t *ppos)
4474{
Steven Rostedta26a2a22008-10-31 00:03:22 -04004475 static char ftrace_dyn_info_buffer[1024];
4476 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004477 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004478 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04004479 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004480 int r;
4481
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004482 mutex_lock(&dyn_info_mutex);
4483 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004484
Steven Rostedta26a2a22008-10-31 00:03:22 -04004485 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004486 buf[r++] = '\n';
4487
4488 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4489
4490 mutex_unlock(&dyn_info_mutex);
4491
4492 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004493}
4494
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004495static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02004496 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04004497 .read = tracing_read_dyn_info,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004498 .llseek = generic_file_llseek,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004499};
4500#endif
4501
4502static struct dentry *d_tracer;
4503
4504struct dentry *tracing_init_dentry(void)
4505{
4506 static int once;
4507
4508 if (d_tracer)
4509 return d_tracer;
4510
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01004511 if (!debugfs_initialized())
4512 return NULL;
4513
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004514 d_tracer = debugfs_create_dir("tracing", NULL);
4515
4516 if (!d_tracer && !once) {
4517 once = 1;
4518 pr_warning("Could not create debugfs directory 'tracing'\n");
4519 return NULL;
4520 }
4521
4522 return d_tracer;
4523}
4524
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004525static struct dentry *d_percpu;
4526
4527struct dentry *tracing_dentry_percpu(void)
4528{
4529 static int once;
4530 struct dentry *d_tracer;
4531
4532 if (d_percpu)
4533 return d_percpu;
4534
4535 d_tracer = tracing_init_dentry();
4536
4537 if (!d_tracer)
4538 return NULL;
4539
4540 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
4541
4542 if (!d_percpu && !once) {
4543 once = 1;
4544 pr_warning("Could not create debugfs directory 'per_cpu'\n");
4545 return NULL;
4546 }
4547
4548 return d_percpu;
4549}
4550
4551static void tracing_init_debugfs_percpu(long cpu)
4552{
4553 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004554 struct dentry *d_cpu;
Steven Rostedtdd49a382010-10-20 21:51:26 -04004555 char cpu_dir[30]; /* 30 characters should be more than enough */
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004556
Steven Rostedtdd49a382010-10-20 21:51:26 -04004557 snprintf(cpu_dir, 30, "cpu%ld", cpu);
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004558 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
4559 if (!d_cpu) {
4560 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
4561 return;
4562 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004563
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01004564 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004565 trace_create_file("trace_pipe", 0444, d_cpu,
4566 (void *) cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004567
4568 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004569 trace_create_file("trace", 0644, d_cpu,
4570 (void *) cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04004571
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004572 trace_create_file("trace_pipe_raw", 0444, d_cpu,
4573 (void *) cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04004574
4575 trace_create_file("stats", 0444, d_cpu,
4576 (void *) cpu, &tracing_stats_fops);
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004577
4578 trace_create_file("buffer_size_kb", 0444, d_cpu,
4579 (void *) cpu, &tracing_entries_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004580}
4581
Steven Rostedt60a11772008-05-12 21:20:44 +02004582#ifdef CONFIG_FTRACE_SELFTEST
4583/* Let selftest have access to static functions in this file */
4584#include "trace_selftest.c"
4585#endif
4586
Steven Rostedt577b7852009-02-26 23:43:05 -05004587struct trace_option_dentry {
4588 struct tracer_opt *opt;
4589 struct tracer_flags *flags;
4590 struct dentry *entry;
4591};
4592
4593static ssize_t
4594trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
4595 loff_t *ppos)
4596{
4597 struct trace_option_dentry *topt = filp->private_data;
4598 char *buf;
4599
4600 if (topt->flags->val & topt->opt->bit)
4601 buf = "1\n";
4602 else
4603 buf = "0\n";
4604
4605 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4606}
4607
4608static ssize_t
4609trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
4610 loff_t *ppos)
4611{
4612 struct trace_option_dentry *topt = filp->private_data;
4613 unsigned long val;
Steven Rostedt577b7852009-02-26 23:43:05 -05004614 int ret;
4615
Peter Huewe22fe9b52011-06-07 21:58:27 +02004616 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4617 if (ret)
Steven Rostedt577b7852009-02-26 23:43:05 -05004618 return ret;
4619
Li Zefan8d18eaa2009-12-08 11:17:06 +08004620 if (val != 0 && val != 1)
Steven Rostedt577b7852009-02-26 23:43:05 -05004621 return -EINVAL;
Li Zefan8d18eaa2009-12-08 11:17:06 +08004622
4623 if (!!(topt->flags->val & topt->opt->bit) != val) {
4624 mutex_lock(&trace_types_lock);
4625 ret = __set_tracer_option(current_trace, topt->flags,
Steven Rostedtc757bea2009-12-21 22:35:16 -05004626 topt->opt, !val);
Li Zefan8d18eaa2009-12-08 11:17:06 +08004627 mutex_unlock(&trace_types_lock);
4628 if (ret)
4629 return ret;
Steven Rostedt577b7852009-02-26 23:43:05 -05004630 }
4631
4632 *ppos += cnt;
4633
4634 return cnt;
4635}
4636
4637
4638static const struct file_operations trace_options_fops = {
4639 .open = tracing_open_generic,
4640 .read = trace_options_read,
4641 .write = trace_options_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004642 .llseek = generic_file_llseek,
Steven Rostedt577b7852009-02-26 23:43:05 -05004643};
4644
Steven Rostedta8259072009-02-26 22:19:12 -05004645static ssize_t
4646trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
4647 loff_t *ppos)
4648{
4649 long index = (long)filp->private_data;
4650 char *buf;
4651
4652 if (trace_flags & (1 << index))
4653 buf = "1\n";
4654 else
4655 buf = "0\n";
4656
4657 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
4658}
4659
4660static ssize_t
4661trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
4662 loff_t *ppos)
4663{
4664 long index = (long)filp->private_data;
Steven Rostedta8259072009-02-26 22:19:12 -05004665 unsigned long val;
4666 int ret;
4667
Peter Huewe22fe9b52011-06-07 21:58:27 +02004668 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4669 if (ret)
Steven Rostedta8259072009-02-26 22:19:12 -05004670 return ret;
4671
Zhaoleif2d84b62009-08-07 18:55:48 +08004672 if (val != 0 && val != 1)
Steven Rostedta8259072009-02-26 22:19:12 -05004673 return -EINVAL;
Zhaoleif2d84b62009-08-07 18:55:48 +08004674 set_tracer_flags(1 << index, val);
Steven Rostedta8259072009-02-26 22:19:12 -05004675
4676 *ppos += cnt;
4677
4678 return cnt;
4679}
4680
Steven Rostedta8259072009-02-26 22:19:12 -05004681static const struct file_operations trace_options_core_fops = {
4682 .open = tracing_open_generic,
4683 .read = trace_options_core_read,
4684 .write = trace_options_core_write,
Arnd Bergmannb4447862010-07-07 23:40:11 +02004685 .llseek = generic_file_llseek,
Steven Rostedta8259072009-02-26 22:19:12 -05004686};
4687
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004688struct dentry *trace_create_file(const char *name,
Al Virof4ae40a62011-07-24 04:33:43 -04004689 umode_t mode,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004690 struct dentry *parent,
4691 void *data,
4692 const struct file_operations *fops)
4693{
4694 struct dentry *ret;
4695
4696 ret = debugfs_create_file(name, mode, parent, data, fops);
4697 if (!ret)
4698 pr_warning("Could not create debugfs '%s' entry\n", name);
4699
4700 return ret;
4701}
4702
4703
Steven Rostedta8259072009-02-26 22:19:12 -05004704static struct dentry *trace_options_init_dentry(void)
4705{
4706 struct dentry *d_tracer;
4707 static struct dentry *t_options;
4708
4709 if (t_options)
4710 return t_options;
4711
4712 d_tracer = tracing_init_dentry();
4713 if (!d_tracer)
4714 return NULL;
4715
4716 t_options = debugfs_create_dir("options", d_tracer);
4717 if (!t_options) {
4718 pr_warning("Could not create debugfs directory 'options'\n");
4719 return NULL;
4720 }
4721
4722 return t_options;
4723}
4724
Steven Rostedt577b7852009-02-26 23:43:05 -05004725static void
4726create_trace_option_file(struct trace_option_dentry *topt,
4727 struct tracer_flags *flags,
4728 struct tracer_opt *opt)
4729{
4730 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05004731
4732 t_options = trace_options_init_dentry();
4733 if (!t_options)
4734 return;
4735
4736 topt->flags = flags;
4737 topt->opt = opt;
4738
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004739 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05004740 &trace_options_fops);
4741
Steven Rostedt577b7852009-02-26 23:43:05 -05004742}
4743
4744static struct trace_option_dentry *
4745create_trace_option_files(struct tracer *tracer)
4746{
4747 struct trace_option_dentry *topts;
4748 struct tracer_flags *flags;
4749 struct tracer_opt *opts;
4750 int cnt;
4751
4752 if (!tracer)
4753 return NULL;
4754
4755 flags = tracer->flags;
4756
4757 if (!flags || !flags->opts)
4758 return NULL;
4759
4760 opts = flags->opts;
4761
4762 for (cnt = 0; opts[cnt].name; cnt++)
4763 ;
4764
Steven Rostedt0cfe8242009-02-27 10:51:10 -05004765 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05004766 if (!topts)
4767 return NULL;
4768
4769 for (cnt = 0; opts[cnt].name; cnt++)
4770 create_trace_option_file(&topts[cnt], flags,
4771 &opts[cnt]);
4772
4773 return topts;
4774}
4775
4776static void
4777destroy_trace_option_files(struct trace_option_dentry *topts)
4778{
4779 int cnt;
4780
4781 if (!topts)
4782 return;
4783
4784 for (cnt = 0; topts[cnt].opt; cnt++) {
4785 if (topts[cnt].entry)
4786 debugfs_remove(topts[cnt].entry);
4787 }
4788
4789 kfree(topts);
4790}
4791
Steven Rostedta8259072009-02-26 22:19:12 -05004792static struct dentry *
4793create_trace_option_core_file(const char *option, long index)
4794{
4795 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004796
4797 t_options = trace_options_init_dentry();
4798 if (!t_options)
4799 return NULL;
4800
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004801 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05004802 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004803}
4804
4805static __init void create_trace_options_dir(void)
4806{
4807 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004808 int i;
4809
4810 t_options = trace_options_init_dentry();
4811 if (!t_options)
4812 return;
4813
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004814 for (i = 0; trace_options[i]; i++)
4815 create_trace_option_core_file(trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05004816}
4817
Steven Rostedt499e5472012-02-22 15:50:28 -05004818static ssize_t
4819rb_simple_read(struct file *filp, char __user *ubuf,
4820 size_t cnt, loff_t *ppos)
4821{
4822 struct ring_buffer *buffer = filp->private_data;
4823 char buf[64];
4824 int r;
4825
4826 if (buffer)
4827 r = ring_buffer_record_is_on(buffer);
4828 else
4829 r = 0;
4830
4831 r = sprintf(buf, "%d\n", r);
4832
4833 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
4834}
4835
4836static ssize_t
4837rb_simple_write(struct file *filp, const char __user *ubuf,
4838 size_t cnt, loff_t *ppos)
4839{
4840 struct ring_buffer *buffer = filp->private_data;
4841 unsigned long val;
4842 int ret;
4843
4844 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
4845 if (ret)
4846 return ret;
4847
4848 if (buffer) {
4849 if (val)
4850 ring_buffer_record_on(buffer);
4851 else
4852 ring_buffer_record_off(buffer);
4853 }
4854
4855 (*ppos)++;
4856
4857 return cnt;
4858}
4859
4860static const struct file_operations rb_simple_fops = {
4861 .open = tracing_open_generic,
4862 .read = rb_simple_read,
4863 .write = rb_simple_write,
4864 .llseek = default_llseek,
4865};
4866
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004867static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004868{
4869 struct dentry *d_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004870 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004871
Lai Jiangshan7e53bd42010-01-06 20:08:50 +08004872 trace_access_lock_init();
4873
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004874 d_tracer = tracing_init_dentry();
4875
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004876 trace_create_file("tracing_enabled", 0644, d_tracer,
4877 &global_trace, &tracing_ctrl_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004878
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004879 trace_create_file("trace_options", 0644, d_tracer,
4880 NULL, &tracing_iter_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004881
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004882 trace_create_file("tracing_cpumask", 0644, d_tracer,
4883 NULL, &tracing_cpumask_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004884
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004885 trace_create_file("trace", 0644, d_tracer,
4886 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004887
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004888 trace_create_file("available_tracers", 0444, d_tracer,
4889 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004890
Li Zefan339ae5d2009-04-17 10:34:30 +08004891 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004892 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004893
Steven Rostedt5d4a9db2009-08-27 16:52:21 -04004894#ifdef CONFIG_TRACER_MAX_TRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004895 trace_create_file("tracing_max_latency", 0644, d_tracer,
4896 &tracing_max_latency, &tracing_max_lat_fops);
Tim Bird0e950172010-02-25 15:36:43 -08004897#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004898
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004899 trace_create_file("tracing_thresh", 0644, d_tracer,
4900 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004901
Li Zefan339ae5d2009-04-17 10:34:30 +08004902 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004903 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004904
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004905 trace_create_file("trace_pipe", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004906 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004907
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004908 trace_create_file("buffer_size_kb", 0644, d_tracer,
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08004909 (void *) RING_BUFFER_ALL_CPUS, &tracing_entries_fops);
Steven Rostedta98a3c32008-05-12 21:20:59 +02004910
Vaibhav Nagarnaikf81ab072011-08-16 14:46:15 -07004911 trace_create_file("buffer_total_size_kb", 0444, d_tracer,
4912 &global_trace, &tracing_total_entries_fops);
4913
Vaibhav Nagarnaik4f271a22011-06-13 17:51:57 -07004914 trace_create_file("free_buffer", 0644, d_tracer,
4915 &global_trace, &tracing_free_buffer_fops);
4916
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004917 trace_create_file("trace_marker", 0220, d_tracer,
4918 NULL, &tracing_mark_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004919
Avadh Patel69abe6a2009-04-10 16:04:48 -04004920 trace_create_file("saved_cmdlines", 0444, d_tracer,
4921 NULL, &tracing_saved_cmdlines_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004922
Zhaolei5079f322009-08-25 16:12:56 +08004923 trace_create_file("trace_clock", 0644, d_tracer, NULL,
4924 &trace_clock_fops);
4925
Steven Rostedt499e5472012-02-22 15:50:28 -05004926 trace_create_file("tracing_on", 0644, d_tracer,
4927 global_trace.buffer, &rb_simple_fops);
4928
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004929#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004930 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4931 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004932#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004933
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004934 create_trace_options_dir();
4935
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004936 for_each_tracing_cpu(cpu)
4937 tracing_init_debugfs_percpu(cpu);
4938
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004939 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004940}
4941
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004942static int trace_panic_handler(struct notifier_block *this,
4943 unsigned long event, void *unused)
4944{
Steven Rostedt944ac422008-10-23 19:26:08 -04004945 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004946 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004947 return NOTIFY_OK;
4948}
4949
4950static struct notifier_block trace_panic_notifier = {
4951 .notifier_call = trace_panic_handler,
4952 .next = NULL,
4953 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4954};
4955
4956static int trace_die_handler(struct notifier_block *self,
4957 unsigned long val,
4958 void *data)
4959{
4960 switch (val) {
4961 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04004962 if (ftrace_dump_on_oops)
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02004963 ftrace_dump(ftrace_dump_on_oops);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004964 break;
4965 default:
4966 break;
4967 }
4968 return NOTIFY_OK;
4969}
4970
4971static struct notifier_block trace_die_notifier = {
4972 .notifier_call = trace_die_handler,
4973 .priority = 200
4974};
4975
4976/*
4977 * printk is set to max of 1024, we really don't need it that big.
4978 * Nothing should be printing 1000 characters anyway.
4979 */
4980#define TRACE_MAX_PRINT 1000
4981
4982/*
4983 * Define here KERN_TRACE so that we have one place to modify
4984 * it if we decide to change what log level the ftrace dump
4985 * should be at.
4986 */
Steven Rostedt428aee12009-01-14 12:24:42 -05004987#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004988
Jason Wessel955b61e2010-08-05 09:22:23 -05004989void
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004990trace_printk_seq(struct trace_seq *s)
4991{
4992 /* Probably should print a warning here. */
4993 if (s->len >= 1000)
4994 s->len = 1000;
4995
4996 /* should be zero ended, but we are paranoid. */
4997 s->buffer[s->len] = 0;
4998
4999 printk(KERN_TRACE "%s", s->buffer);
5000
Steven Rostedtf9520752009-03-02 14:04:40 -05005001 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005002}
5003
Jason Wessel955b61e2010-08-05 09:22:23 -05005004void trace_init_global_iter(struct trace_iterator *iter)
5005{
5006 iter->tr = &global_trace;
5007 iter->trace = current_trace;
5008 iter->cpu_file = TRACE_PIPE_ALL_CPU;
5009}
5010
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005011static void
5012__ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005013{
Thomas Gleixner445c8952009-12-02 19:49:50 +01005014 static arch_spinlock_t ftrace_dump_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +01005015 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005016 /* use static because iter can be a bit big for the stack */
5017 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005018 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005019 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04005020 unsigned long flags;
5021 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005022
5023 /* only one dump */
Steven Rostedtcd891ae2009-04-28 11:39:34 -04005024 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005025 arch_spin_lock(&ftrace_dump_lock);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005026 if (dump_ran)
5027 goto out;
5028
5029 dump_ran = 1;
5030
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05005031 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005032
Steven Rostedte0a413f2011-09-29 21:26:16 -04005033 /* Did function tracer already get disabled? */
5034 if (ftrace_is_dead()) {
5035 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
5036 printk("# MAY BE MISSING FUNCTION EVENTS\n");
5037 }
5038
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005039 if (disable_tracing)
5040 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005041
Jason Wessel955b61e2010-08-05 09:22:23 -05005042 trace_init_global_iter(&iter);
5043
Steven Rostedtd7690412008-10-01 00:29:53 -04005044 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05005045 atomic_inc(&iter.tr->data[cpu]->disabled);
Steven Rostedtd7690412008-10-01 00:29:53 -04005046 }
5047
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005048 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
5049
Török Edwinb54d3de2008-11-22 13:28:48 +02005050 /* don't look at user memory in panic mode */
5051 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
5052
Steven Rostedte543ad72009-03-04 18:20:36 -05005053 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005054 iter.tr = &global_trace;
5055 iter.trace = current_trace;
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005056
5057 switch (oops_dump_mode) {
5058 case DUMP_ALL:
5059 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5060 break;
5061 case DUMP_ORIG:
5062 iter.cpu_file = raw_smp_processor_id();
5063 break;
5064 case DUMP_NONE:
5065 goto out_enable;
5066 default:
5067 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
5068 iter.cpu_file = TRACE_PIPE_ALL_CPU;
5069 }
5070
5071 printk(KERN_TRACE "Dumping ftrace buffer:\n");
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005072
5073 /*
5074 * We need to stop all tracing on all CPUS to read the
5075 * the next buffer. This is a bit expensive, but is
5076 * not done often. We fill all what we can read,
5077 * and then release the locks again.
5078 */
5079
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005080 while (!trace_empty(&iter)) {
5081
5082 if (!cnt)
5083 printk(KERN_TRACE "---------------------------------\n");
5084
5085 cnt++;
5086
5087 /* reset all but tr, trace, and overruns */
5088 memset(&iter.seq, 0,
5089 sizeof(struct trace_iterator) -
5090 offsetof(struct trace_iterator, seq));
5091 iter.iter_flags |= TRACE_FILE_LAT_FMT;
5092 iter.pos = -1;
5093
Jason Wessel955b61e2010-08-05 09:22:23 -05005094 if (trace_find_next_entry_inc(&iter) != NULL) {
Lai Jiangshan74e7ff82009-07-28 20:17:22 +08005095 int ret;
5096
5097 ret = print_trace_line(&iter);
5098 if (ret != TRACE_TYPE_NO_CONSUME)
5099 trace_consume(&iter);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005100 }
Steven Rostedtb892e5c2012-03-01 22:06:48 -05005101 touch_nmi_watchdog();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005102
5103 trace_printk_seq(&iter.seq);
5104 }
5105
5106 if (!cnt)
5107 printk(KERN_TRACE " (ftrace buffer empty)\n");
5108 else
5109 printk(KERN_TRACE "---------------------------------\n");
5110
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005111 out_enable:
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005112 /* Re-enable tracing if requested */
5113 if (!disable_tracing) {
5114 trace_flags |= old_userobj;
5115
5116 for_each_tracing_cpu(cpu) {
Jason Wessel955b61e2010-08-05 09:22:23 -05005117 atomic_dec(&iter.tr->data[cpu]->disabled);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005118 }
5119 tracing_on();
5120 }
5121
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005122 out:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01005123 arch_spin_unlock(&ftrace_dump_lock);
Steven Rostedtcd891ae2009-04-28 11:39:34 -04005124 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005125}
5126
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005127/* By default: disable tracing after the dump */
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005128void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005129{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +02005130 __ftrace_dump(true, oops_dump_mode);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005131}
Paul E. McKenneya8eecf22011-10-02 11:01:15 -07005132EXPORT_SYMBOL_GPL(ftrace_dump);
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01005133
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005134__init static int tracer_alloc_buffers(void)
5135{
Steven Rostedt73c51622009-03-11 13:42:01 -04005136 int ring_buf_size;
David Sharp750912f2010-12-08 13:46:47 -08005137 enum ring_buffer_flags rb_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005138 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305139 int ret = -ENOMEM;
5140
David Sharp750912f2010-12-08 13:46:47 -08005141
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305142 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
5143 goto out;
5144
5145 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
5146 goto out_free_buffer_mask;
5147
Steven Rostedt07d777f2011-09-22 14:01:55 -04005148 /* Only allocate trace_printk buffers if a trace_printk exists */
5149 if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
5150 trace_printk_init_buffers();
5151
Steven Rostedt73c51622009-03-11 13:42:01 -04005152 /* To save memory, keep the ring buffer size to its minimum */
5153 if (ring_buffer_expanded)
5154 ring_buf_size = trace_buf_size;
5155 else
5156 ring_buf_size = 1;
5157
David Sharp750912f2010-12-08 13:46:47 -08005158 rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
5159
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305160 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
5161 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005162
Steven Rostedtab464282008-05-12 21:21:00 +02005163 /* TODO: make the number of buffers hot pluggable with CPUS */
David Sharp750912f2010-12-08 13:46:47 -08005164 global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005165 if (!global_trace.buffer) {
5166 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
5167 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305168 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005169 }
Steven Rostedt499e5472012-02-22 15:50:28 -05005170 if (global_trace.buffer_disabled)
5171 tracing_off();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005172
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305173
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005174#ifdef CONFIG_TRACER_MAX_TRACE
David Sharp750912f2010-12-08 13:46:47 -08005175 max_tr.buffer = ring_buffer_alloc(1, rb_flags);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005176 if (!max_tr.buffer) {
5177 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
5178 WARN_ON(1);
5179 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305180 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005181 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005182#endif
5183
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02005184 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02005185 for_each_tracing_cpu(i) {
jolsa@redhat.com566b0aa2009-07-16 21:44:26 +02005186 global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Tejun Heo9705f692009-10-29 22:34:13 +09005187 max_tr.data[i] = &per_cpu(max_tr_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005188 }
5189
Vaibhav Nagarnaik438ced12012-02-02 12:00:41 -08005190 set_buffer_entries(&global_trace, ring_buf_size);
5191#ifdef CONFIG_TRACER_MAX_TRACE
5192 set_buffer_entries(&max_tr, 1);
5193#endif
5194
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005195 trace_init_cmdlines();
5196
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02005197 register_tracer(&nop_trace);
Steven Rostedt79fb0768f2009-02-02 21:38:33 -05005198 current_trace = &nop_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +02005199 /* All seems OK, enable tracing */
5200 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04005201
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005202 atomic_notifier_chain_register(&panic_notifier_list,
5203 &trace_panic_notifier);
5204
5205 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01005206
5207 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04005208
Rusty Russell9e01c1b2009-01-01 10:12:22 +10305209out_free_cpumask:
5210 free_cpumask_var(tracing_cpumask);
5211out_free_buffer_mask:
5212 free_cpumask_var(tracing_buffer_mask);
5213out:
5214 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02005215}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05005216
5217__init static int clear_boot_tracer(void)
5218{
5219 /*
5220 * The default tracer at boot buffer is an init section.
5221 * This function is called in lateinit. If we did not
5222 * find the boot tracer, then clear it out, to prevent
5223 * later registration from accessing the buffer that is
5224 * about to be freed.
5225 */
5226 if (!default_bootup_tracer)
5227 return 0;
5228
5229 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
5230 default_bootup_tracer);
5231 default_bootup_tracer = NULL;
5232
5233 return 0;
5234}
5235
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01005236early_initcall(tracer_alloc_buffers);
5237fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05005238late_initcall(clear_boot_tracer);