blob: 0f57f1b443b69c2a96f3c50ea79f6d330d4e41f2 [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>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020015#include <linux/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>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020034#include <linux/ctype.h>
35#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020036#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020037#include <linux/gfp.h>
38#include <linux/fs.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020039
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020040#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050041#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020042
Steven Rostedt3928a8a2008-09-29 23:02:41 -040043#define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
44
Steven Rostedt745b1622009-01-15 23:40:11 -050045unsigned long __read_mostly tracing_max_latency;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020046unsigned long __read_mostly tracing_thresh;
47
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010048/*
Steven Rostedt73c51622009-03-11 13:42:01 -040049 * On boot up, the ring buffer is set to the minimum size, so that
50 * we do not waste memory on systems that are not using tracing.
51 */
52static int ring_buffer_expanded;
53
54/*
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010055 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010056 * A selftest will lurk into the ring-buffer to count the
57 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010058 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010059 * at the same time, giving false positive or negative results.
60 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010061static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010062
Steven Rostedtb2821ae2009-02-02 21:38:32 -050063/*
64 * If a tracer is running, we do not want to run SELFTEST.
65 */
66static bool __read_mostly tracing_selftest_disabled;
67
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010068/* For tracers that don't implement custom flags */
69static struct tracer_opt dummy_tracer_opt[] = {
70 { }
71};
72
73static struct tracer_flags dummy_tracer_flags = {
74 .val = 0,
75 .opts = dummy_tracer_opt
76};
77
78static int dummy_set_flag(u32 old_flags, u32 bit, int set)
79{
80 return 0;
81}
Steven Rostedt0f048702008-11-05 16:05:44 -050082
83/*
84 * Kill all tracing for good (never come back).
85 * It is initialized to 1 but will turn to zero if the initialization
86 * of the tracer is successful. But that is the only place that sets
87 * this back to zero.
88 */
Hannes Eder4fd27352009-02-10 19:44:12 +010089static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050090
Steven Rostedtd7690412008-10-01 00:29:53 -040091static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
92
93static inline void ftrace_disable_cpu(void)
94{
95 preempt_disable();
96 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
97}
98
99static inline void ftrace_enable_cpu(void)
100{
101 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
102 preempt_enable();
103}
104
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030105static cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200106
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +0100107/* Define which cpu buffers are currently read in trace_pipe */
108static cpumask_var_t tracing_reader_cpumask;
109
Steven Rostedtab464282008-05-12 21:21:00 +0200110#define for_each_tracing_cpu(cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030111 for_each_cpu(cpu, tracing_buffer_mask)
Steven Rostedtab464282008-05-12 21:21:00 +0200112
Steven Rostedt944ac422008-10-23 19:26:08 -0400113/*
114 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115 *
116 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117 * is set, then ftrace_dump is called. This will output the contents
118 * of the ftrace buffers to the console. This is very useful for
119 * capturing traces that lead to crashes and outputing it to a
120 * serial console.
121 *
122 * It is default off, but you can enable it with either specifying
123 * "ftrace_dump_on_oops" in the kernel command line, or setting
124 * /proc/sys/kernel/ftrace_dump_on_oops to true.
125 */
126int ftrace_dump_on_oops;
127
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500128static int tracing_set_tracer(const char *buf);
129
130#define BOOTUP_TRACER_SIZE 100
131static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
132static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100133
134static int __init set_ftrace(char *str)
135{
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500136 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
137 default_bootup_tracer = bootup_tracer_buf;
Steven Rostedt73c51622009-03-11 13:42:01 -0400138 /* We are using ftrace early, expand it */
139 ring_buffer_expanded = 1;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100140 return 1;
141}
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500142__setup("ftrace=", set_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100143
Steven Rostedt944ac422008-10-23 19:26:08 -0400144static int __init set_ftrace_dump_on_oops(char *str)
145{
146 ftrace_dump_on_oops = 1;
147 return 1;
148}
149__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200150
Lai Jiangshancf8e3472009-03-30 13:48:00 +0800151unsigned long long ns2usecs(cycle_t nsec)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200152{
153 nsec += 500;
154 do_div(nsec, 1000);
155 return nsec;
156}
157
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200158/*
159 * The global_trace is the descriptor that holds the tracing
160 * buffers for the live tracing. For each CPU, it contains
161 * a link list of pages that will store trace entries. The
162 * page descriptor of the pages in the memory is used to hold
163 * the link list by linking the lru item in the page descriptor
164 * to each of the pages in the buffer per CPU.
165 *
166 * For each active CPU there is a data field that holds the
167 * pages for the buffer for that CPU. Each CPU has the same number
168 * of pages allocated for its buffer.
169 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200170static struct trace_array global_trace;
171
172static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
173
Tom Zanussieb02ce02009-04-08 03:15:54 -0500174int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
175 struct ring_buffer_event *event)
176{
177 return filter_check_discard(call, rec, global_trace.buffer, event);
178}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400179EXPORT_SYMBOL_GPL(filter_current_check_discard);
Tom Zanussieb02ce02009-04-08 03:15:54 -0500180
Steven Rostedt37886f62009-03-17 17:22:06 -0400181cycle_t ftrace_now(int cpu)
182{
183 u64 ts;
184
185 /* Early boot up does not have a buffer yet */
186 if (!global_trace.buffer)
187 return trace_clock_local();
188
189 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
190 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
191
192 return ts;
193}
194
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200195/*
196 * The max_tr is used to snapshot the global_trace when a maximum
197 * latency is reached. Some tracers will use this to store a maximum
198 * trace while it continues examining live traces.
199 *
200 * The buffers for the max_tr are set up the same as the global_trace.
201 * When a snapshot is taken, the link list of the max_tr is swapped
202 * with the link list of the global_trace and the buffers are reset for
203 * the global_trace so the tracing can continue.
204 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200205static struct trace_array max_tr;
206
207static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
208
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200209/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200210static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200211
Steven Rostedt90369902008-11-05 16:05:44 -0500212/**
213 * tracing_is_enabled - return tracer_enabled status
214 *
215 * This function is used by other tracers to know the status
216 * of the tracer_enabled flag. Tracers may use this function
217 * to know if it should enable their features when starting
218 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219 */
220int tracing_is_enabled(void)
221{
222 return tracer_enabled;
223}
224
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200225/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400226 * trace_buf_size is the size in bytes that is allocated
227 * for a buffer. Note, the number of bytes is always rounded
228 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400229 *
230 * This number is purposely set to a low number of 16384.
231 * If the dump on oops happens, it will be much appreciated
232 * to not have to wait for all that output. Anyway this can be
233 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200234 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400235#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400236
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400237static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200238
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200239/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200240static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200241
242/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200243static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200244
245/*
246 * max_tracer_type_len is used to simplify the allocating of
247 * buffers to read userspace tracer names. We keep track of
248 * the longest tracer name registered.
249 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200250static int max_tracer_type_len;
251
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200252/*
253 * trace_types_lock is used to protect the trace_types list.
254 * This lock is also used to keep user access serialized.
255 * Accesses from userspace will grab this lock while userspace
256 * activities happen inside the kernel.
257 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200258static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200259
260/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200261static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
262
Steven Rostedtee6bce52008-11-12 17:52:37 -0500263/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500264unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400265 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
266 TRACE_ITER_GRAPH_TIME;
Ingo Molnar4e655512008-05-12 21:20:52 +0200267
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200268/**
269 * trace_wake_up - wake up tasks waiting for trace input
270 *
271 * Simply wakes up any task that is blocked on the trace_wait
272 * queue. These is used with trace_poll for tasks polling the trace.
273 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200274void trace_wake_up(void)
275{
Ingo Molnar017730c2008-05-12 21:20:52 +0200276 /*
277 * The runqueue_is_locked() can fail, but this is the best we
278 * have for now:
279 */
280 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200281 wake_up(&trace_wait);
282}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200283
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400284static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200285{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400286 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200287 int ret;
288
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200289 if (!str)
290 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400291 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200292 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400293 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200294 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400295 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200296 return 1;
297}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400298__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200299
Steven Rostedt57f50be2008-05-12 21:20:44 +0200300unsigned long nsecs_to_usecs(unsigned long nsecs)
301{
302 return nsecs / 1000;
303}
304
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200305/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200306static const char *trace_options[] = {
307 "print-parent",
308 "sym-offset",
309 "sym-addr",
310 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200311 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200312 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200313 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200314 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200315 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200316 "sched-tree",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100317 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500318 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500319 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500320 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200321 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200322 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100323 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200324 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500325 "latency-format",
Steven Rostedtaf4617b2009-03-17 18:09:55 -0400326 "global-clock",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400327 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400328 "graph-time",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200329 NULL
330};
331
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200332/*
333 * ftrace_max_lock is used to protect the swapping of buffers
334 * when taking a max snapshot. The buffers themselves are
335 * protected by per_cpu spinlocks. But the action of the swap
336 * needs its own lock.
337 *
338 * This is defined as a raw_spinlock_t in order to help
339 * with performance when lockdep debugging is enabled.
340 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200341static raw_spinlock_t ftrace_max_lock =
342 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200343
344/*
345 * Copy the new maximum trace into the separate maximum-trace
346 * structure. (this way the maximum trace is permanently saved,
347 * for later retrieval via /debugfs/tracing/latency_trace)
348 */
Ingo Molnare309b412008-05-12 21:20:51 +0200349static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200350__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
351{
352 struct trace_array_cpu *data = tr->data[cpu];
353
354 max_tr.cpu = cpu;
355 max_tr.time_start = data->preempt_timestamp;
356
357 data = max_tr.data[cpu];
358 data->saved_latency = tracing_max_latency;
359
360 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
361 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100362 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200363 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
364 data->policy = tsk->policy;
365 data->rt_priority = tsk->rt_priority;
366
367 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500368 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200369}
370
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200371ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
372{
373 int len;
374 int ret;
375
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500376 if (!cnt)
377 return 0;
378
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200379 if (s->len <= s->readpos)
380 return -EBUSY;
381
382 len = s->len - s->readpos;
383 if (cnt > len)
384 cnt = len;
385 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500386 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200387 return -EFAULT;
388
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500389 cnt -= ret;
390
Steven Rostedte74da522009-03-04 20:31:11 -0500391 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200392 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200393}
394
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200395static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200396{
397 int len;
398 void *ret;
399
400 if (s->len <= s->readpos)
401 return -EBUSY;
402
403 len = s->len - s->readpos;
404 if (cnt > len)
405 cnt = len;
406 ret = memcpy(buf, s->buffer + s->readpos, cnt);
407 if (!ret)
408 return -EFAULT;
409
Steven Rostedte74da522009-03-04 20:31:11 -0500410 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200411 return cnt;
412}
413
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200414/**
415 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
416 * @tr: tracer
417 * @tsk: the task with the latency
418 * @cpu: The cpu that initiated the trace.
419 *
420 * Flip the buffers between the @tr and the max_tr and record information
421 * about which task was the cause of this latency.
422 */
Ingo Molnare309b412008-05-12 21:20:51 +0200423void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200424update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
425{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400426 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200427
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200428 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200429 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400430
431 tr->buffer = max_tr.buffer;
432 max_tr.buffer = buf;
433
Steven Rostedtd7690412008-10-01 00:29:53 -0400434 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400435 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400436 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200437
438 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200439 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200440}
441
442/**
443 * update_max_tr_single - only copy one trace over, and reset the rest
444 * @tr - tracer
445 * @tsk - task with the latency
446 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200447 *
448 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200449 */
Ingo Molnare309b412008-05-12 21:20:51 +0200450void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200451update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
452{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400453 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200454
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200455 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200456 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200457
Steven Rostedtd7690412008-10-01 00:29:53 -0400458 ftrace_disable_cpu();
459
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400460 ring_buffer_reset(max_tr.buffer);
461 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
462
Steven Rostedtd7690412008-10-01 00:29:53 -0400463 ftrace_enable_cpu();
464
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500465 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200466
467 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200468 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200469}
470
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200471/**
472 * register_tracer - register a tracer with the ftrace system.
473 * @type - the plugin for the tracer
474 *
475 * Register a new plugin tracer.
476 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200477int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100478__releases(kernel_lock)
479__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200480{
481 struct tracer *t;
482 int len;
483 int ret = 0;
484
485 if (!type->name) {
486 pr_info("Tracer must have a name\n");
487 return -1;
488 }
489
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100490 /*
491 * When this gets called we hold the BKL which means that
492 * preemption is disabled. Various trace selftests however
493 * need to disable and enable preemption for successful tests.
494 * So we drop the BKL here and grab it after the tests again.
495 */
496 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200497 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100498
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100499 tracing_selftest_running = true;
500
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501 for (t = trace_types; t; t = t->next) {
502 if (strcmp(type->name, t->name) == 0) {
503 /* already found */
504 pr_info("Trace %s already registered\n",
505 type->name);
506 ret = -1;
507 goto out;
508 }
509 }
510
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100511 if (!type->set_flag)
512 type->set_flag = &dummy_set_flag;
513 if (!type->flags)
514 type->flags = &dummy_tracer_flags;
515 else
516 if (!type->flags->opts)
517 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100518 if (!type->wait_pipe)
519 type->wait_pipe = default_wait_pipe;
520
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100521
Steven Rostedt60a11772008-05-12 21:20:44 +0200522#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500523 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200524 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200525 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200526 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100527
Steven Rostedt60a11772008-05-12 21:20:44 +0200528 /*
529 * Run a selftest on this tracer.
530 * Here we reset the trace buffer, and set the current
531 * tracer to be this tracer. The tracer can then run some
532 * internal tracing to verify that everything is in order.
533 * If we fail, we do not register this tracer.
534 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100535 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400536 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100537
Steven Rostedt60a11772008-05-12 21:20:44 +0200538 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200539 /* the test is responsible for initializing and enabling */
540 pr_info("Testing tracer %s: ", type->name);
541 ret = type->selftest(type, tr);
542 /* the test is responsible for resetting too */
543 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200544 if (ret) {
545 printk(KERN_CONT "FAILED!\n");
546 goto out;
547 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200548 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100549 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400550 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100551
Steven Rostedt60a11772008-05-12 21:20:44 +0200552 printk(KERN_CONT "PASSED\n");
553 }
554#endif
555
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200556 type->next = trace_types;
557 trace_types = type;
558 len = strlen(type->name);
559 if (len > max_tracer_type_len)
560 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200561
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200562 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100563 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200564 mutex_unlock(&trace_types_lock);
565
Steven Rostedtdac74942009-02-05 01:13:38 -0500566 if (ret || !default_bootup_tracer)
567 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500568
Steven Rostedtdac74942009-02-05 01:13:38 -0500569 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
570 goto out_unlock;
571
572 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
573 /* Do we want this tracer to start on bootup? */
574 tracing_set_tracer(type->name);
575 default_bootup_tracer = NULL;
576 /* disable other selftests, since this will break it. */
577 tracing_selftest_disabled = 1;
578#ifdef CONFIG_FTRACE_STARTUP_TEST
579 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
580 type->name);
581#endif
582
583 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500584 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200585 return ret;
586}
587
588void unregister_tracer(struct tracer *type)
589{
590 struct tracer **t;
591 int len;
592
593 mutex_lock(&trace_types_lock);
594 for (t = &trace_types; *t; t = &(*t)->next) {
595 if (*t == type)
596 goto found;
597 }
598 pr_info("Trace %s not registered\n", type->name);
599 goto out;
600
601 found:
602 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200603
604 if (type == current_trace && tracer_enabled) {
605 tracer_enabled = 0;
606 tracing_stop();
607 if (current_trace->stop)
608 current_trace->stop(&global_trace);
609 current_trace = &nop_trace;
610 }
611
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200612 if (strlen(type->name) != max_tracer_type_len)
613 goto out;
614
615 max_tracer_type_len = 0;
616 for (t = &trace_types; *t; t = &(*t)->next) {
617 len = strlen((*t)->name);
618 if (len > max_tracer_type_len)
619 max_tracer_type_len = len;
620 }
621 out:
622 mutex_unlock(&trace_types_lock);
623}
624
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400625void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200626{
Steven Rostedtd7690412008-10-01 00:29:53 -0400627 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400628 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400629 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200630}
631
Pekka J Enberg213cc062008-12-19 12:08:39 +0200632void tracing_reset_online_cpus(struct trace_array *tr)
633{
634 int cpu;
635
636 tr->time_start = ftrace_now(tr->cpu);
637
638 for_each_online_cpu(cpu)
639 tracing_reset(tr, cpu);
640}
641
Steven Rostedt9456f0f2009-05-06 21:54:09 -0400642void tracing_reset_current(int cpu)
643{
644 tracing_reset(&global_trace, cpu);
645}
646
647void tracing_reset_current_online_cpus(void)
648{
649 tracing_reset_online_cpus(&global_trace);
650}
651
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100653#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200654static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
655static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
656static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
657static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100658static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200659
Steven Rostedt25b0b442008-05-12 21:21:00 +0200660/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100661static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200662
663static void trace_init_cmdlines(void)
664{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100665 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
666 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200667 cmdline_idx = 0;
668}
669
Steven Rostedt0f048702008-11-05 16:05:44 -0500670static int trace_stop_count;
671static DEFINE_SPINLOCK(tracing_start_lock);
672
673/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500674 * ftrace_off_permanent - disable all ftrace code permanently
675 *
676 * This should only be called when a serious anomally has
677 * been detected. This will turn off the function tracing,
678 * ring buffers, and other tracing utilites. It takes no
679 * locks and can be called from any context.
680 */
681void ftrace_off_permanent(void)
682{
683 tracing_disabled = 1;
684 ftrace_stop();
685 tracing_off_permanent();
686}
687
688/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500689 * tracing_start - quick start of the tracer
690 *
691 * If tracing is enabled but was stopped by tracing_stop,
692 * this will start the tracer back up.
693 */
694void tracing_start(void)
695{
696 struct ring_buffer *buffer;
697 unsigned long flags;
698
699 if (tracing_disabled)
700 return;
701
702 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500703 if (--trace_stop_count) {
704 if (trace_stop_count < 0) {
705 /* Someone screwed up their debugging */
706 WARN_ON_ONCE(1);
707 trace_stop_count = 0;
708 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500709 goto out;
710 }
711
712
713 buffer = global_trace.buffer;
714 if (buffer)
715 ring_buffer_record_enable(buffer);
716
717 buffer = max_tr.buffer;
718 if (buffer)
719 ring_buffer_record_enable(buffer);
720
721 ftrace_start();
722 out:
723 spin_unlock_irqrestore(&tracing_start_lock, flags);
724}
725
726/**
727 * tracing_stop - quick stop of the tracer
728 *
729 * Light weight way to stop tracing. Use in conjunction with
730 * tracing_start.
731 */
732void tracing_stop(void)
733{
734 struct ring_buffer *buffer;
735 unsigned long flags;
736
737 ftrace_stop();
738 spin_lock_irqsave(&tracing_start_lock, flags);
739 if (trace_stop_count++)
740 goto out;
741
742 buffer = global_trace.buffer;
743 if (buffer)
744 ring_buffer_record_disable(buffer);
745
746 buffer = max_tr.buffer;
747 if (buffer)
748 ring_buffer_record_disable(buffer);
749
750 out:
751 spin_unlock_irqrestore(&tracing_start_lock, flags);
752}
753
Ingo Molnare309b412008-05-12 21:20:51 +0200754void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200755
Ingo Molnare309b412008-05-12 21:20:51 +0200756static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200757{
Carsten Emdea635cf02009-03-18 09:00:41 +0100758 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200759
760 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
761 return;
762
763 /*
764 * It's not the end of the world if we don't get
765 * the lock, but we also don't want to spin
766 * nor do we want to disable interrupts,
767 * so if we miss here, then better luck next time.
768 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100769 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200770 return;
771
772 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100773 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200774 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
775
Carsten Emdea635cf02009-03-18 09:00:41 +0100776 /*
777 * Check whether the cmdline buffer at idx has a pid
778 * mapped. We are going to overwrite that entry so we
779 * need to clear the map_pid_to_cmdline. Otherwise we
780 * would read the new comm for the old pid.
781 */
782 pid = map_cmdline_to_pid[idx];
783 if (pid != NO_CMDLINE_MAP)
784 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200785
Carsten Emdea635cf02009-03-18 09:00:41 +0100786 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200787 map_pid_to_cmdline[tsk->pid] = idx;
788
789 cmdline_idx = idx;
790 }
791
792 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
793
Peter Zijlstraefed7922009-03-04 12:32:55 +0100794 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200795}
796
Steven Rostedt4ca530852009-03-16 19:20:15 -0400797void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200798{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200799 unsigned map;
800
Steven Rostedt4ca530852009-03-16 19:20:15 -0400801 if (!pid) {
802 strcpy(comm, "<idle>");
803 return;
804 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200805
Steven Rostedt4ca530852009-03-16 19:20:15 -0400806 if (pid > PID_MAX_DEFAULT) {
807 strcpy(comm, "<...>");
808 return;
809 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200810
Heiko Carstens5b6045a2009-05-26 17:28:02 +0200811 preempt_disable();
Steven Rostedt4ca530852009-03-16 19:20:15 -0400812 __raw_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200813 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +0100814 if (map != NO_CMDLINE_MAP)
815 strcpy(comm, saved_cmdlines[map]);
816 else
817 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818
Steven Rostedt4ca530852009-03-16 19:20:15 -0400819 __raw_spin_unlock(&trace_cmdline_lock);
Heiko Carstens5b6045a2009-05-26 17:28:02 +0200820 preempt_enable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200821}
822
Ingo Molnare309b412008-05-12 21:20:51 +0200823void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200824{
Thomas Gleixner18aecd32009-03-18 08:56:58 +0100825 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
826 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200827 return;
828
829 trace_save_cmdline(tsk);
830}
831
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300832void
Steven Rostedt38697052008-10-01 13:14:09 -0400833tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
834 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200835{
836 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200837
Steven Rostedt777e2082008-09-29 23:02:42 -0400838 entry->preempt_count = pc & 0xff;
839 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedtef180122009-03-10 14:10:56 -0400840 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400841 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400842#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400843 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400844#else
845 TRACE_FLAG_IRQS_NOSUPPORT |
846#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200847 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
848 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
849 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
850}
851
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200852struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
Li Zefan7a4f4532009-04-22 16:53:34 +0800853 int type,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200854 unsigned long len,
855 unsigned long flags, int pc)
856{
857 struct ring_buffer_event *event;
858
859 event = ring_buffer_lock_reserve(tr->buffer, len);
860 if (event != NULL) {
861 struct trace_entry *ent = ring_buffer_event_data(event);
862
863 tracing_generic_entry_update(ent, flags, pc);
864 ent->type = type;
865 }
866
867 return event;
868}
869static void ftrace_trace_stack(struct trace_array *tr,
870 unsigned long flags, int skip, int pc);
871static void ftrace_trace_userstack(struct trace_array *tr,
872 unsigned long flags, int pc);
873
Frederic Weisbecker07edf712009-03-22 23:10:46 +0100874static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
875 struct ring_buffer_event *event,
876 unsigned long flags, int pc,
877 int wake)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200878{
879 ring_buffer_unlock_commit(tr->buffer, event);
880
881 ftrace_trace_stack(tr, flags, 6, pc);
882 ftrace_trace_userstack(tr, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +0100883
884 if (wake)
885 trace_wake_up();
886}
887
888void trace_buffer_unlock_commit(struct trace_array *tr,
889 struct ring_buffer_event *event,
890 unsigned long flags, int pc)
891{
892 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200893}
894
Steven Rostedtef5580d2009-02-27 19:38:04 -0500895struct ring_buffer_event *
Li Zefan7a4f4532009-04-22 16:53:34 +0800896trace_current_buffer_lock_reserve(int type, unsigned long len,
Steven Rostedtef5580d2009-02-27 19:38:04 -0500897 unsigned long flags, int pc)
898{
899 return trace_buffer_lock_reserve(&global_trace,
900 type, len, flags, pc);
901}
Steven Rostedt94487d62009-05-05 19:22:53 -0400902EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
Steven Rostedtef5580d2009-02-27 19:38:04 -0500903
904void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
905 unsigned long flags, int pc)
906{
Steven Rostedt77d9f462009-04-02 01:16:59 -0400907 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
Frederic Weisbecker07edf712009-03-22 23:10:46 +0100908}
Steven Rostedt94487d62009-05-05 19:22:53 -0400909EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
Frederic Weisbecker07edf712009-03-22 23:10:46 +0100910
911void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
912 unsigned long flags, int pc)
913{
Steven Rostedt77d9f462009-04-02 01:16:59 -0400914 __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
Steven Rostedtef5580d2009-02-27 19:38:04 -0500915}
Steven Rostedt94487d62009-05-05 19:22:53 -0400916EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
Steven Rostedt77d9f462009-04-02 01:16:59 -0400917
918void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
919{
920 ring_buffer_discard_commit(global_trace.buffer, event);
Steven Rostedtef5580d2009-02-27 19:38:04 -0500921}
Steven Rostedt12acd472009-04-17 16:01:56 -0400922EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
Steven Rostedtef5580d2009-02-27 19:38:04 -0500923
Ingo Molnare309b412008-05-12 21:20:51 +0200924void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500925trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400926 unsigned long ip, unsigned long parent_ip, unsigned long flags,
927 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200928{
Tom Zanussie1112b42009-03-31 00:48:49 -0500929 struct ftrace_event_call *call = &event_function;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400930 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400931 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200932
Steven Rostedtd7690412008-10-01 00:29:53 -0400933 /* If we are reading the ring buffer, don't trace */
934 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
935 return;
936
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200937 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
938 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400939 if (!event)
940 return;
941 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400942 entry->ip = ip;
943 entry->parent_ip = parent_ip;
Tom Zanussie1112b42009-03-31 00:48:49 -0500944
Tom Zanussieb02ce02009-04-08 03:15:54 -0500945 if (!filter_check_discard(call, entry, tr->buffer, event))
946 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200947}
948
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100949#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker16185362009-03-23 22:17:01 +0100950static int __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100951 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100952 unsigned long flags,
953 int pc)
954{
Tom Zanussie1112b42009-03-31 00:48:49 -0500955 struct ftrace_event_call *call = &event_funcgraph_entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100956 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100957 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100958
959 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
Frederic Weisbecker16185362009-03-23 22:17:01 +0100960 return 0;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100961
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200962 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
963 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100964 if (!event)
Frederic Weisbecker16185362009-03-23 22:17:01 +0100965 return 0;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100966 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100967 entry->graph_ent = *trace;
Tom Zanussieb02ce02009-04-08 03:15:54 -0500968 if (!filter_current_check_discard(call, entry, event))
969 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker16185362009-03-23 22:17:01 +0100970
971 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100972}
973
974static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100975 struct ftrace_graph_ret *trace,
976 unsigned long flags,
977 int pc)
978{
Tom Zanussie1112b42009-03-31 00:48:49 -0500979 struct ftrace_event_call *call = &event_funcgraph_exit;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100980 struct ring_buffer_event *event;
981 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100982
983 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
984 return;
985
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200986 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
987 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100988 if (!event)
989 return;
990 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100991 entry->ret = *trace;
Tom Zanussieb02ce02009-04-08 03:15:54 -0500992 if (!filter_current_check_discard(call, entry, event))
993 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100994}
995#endif
996
Ingo Molnare309b412008-05-12 21:20:51 +0200997void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200998ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400999 unsigned long ip, unsigned long parent_ip, unsigned long flags,
1000 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001001{
1002 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001003 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001004}
1005
Steven Rostedt53614992009-01-15 19:12:40 -05001006static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -05001007 unsigned long flags,
1008 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +02001009{
Al Viroc2c80522008-10-31 19:50:41 +00001010#ifdef CONFIG_STACKTRACE
Tom Zanussie1112b42009-03-31 00:48:49 -05001011 struct ftrace_event_call *call = &event_kernel_stack;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001012 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001013 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +02001014 struct stack_trace trace;
1015
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001016 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
1017 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001018 if (!event)
1019 return;
1020 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001021 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +02001022
1023 trace.nr_entries = 0;
1024 trace.max_entries = FTRACE_STACK_ENTRIES;
1025 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -04001026 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +02001027
1028 save_stack_trace(&trace);
Tom Zanussieb02ce02009-04-08 03:15:54 -05001029 if (!filter_check_discard(call, entry, tr->buffer, event))
1030 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +00001031#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001032}
1033
Steven Rostedt53614992009-01-15 19:12:40 -05001034static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -05001035 unsigned long flags,
1036 int skip, int pc)
1037{
1038 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1039 return;
1040
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001041 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -05001042}
1043
Steven Rostedt38697052008-10-01 13:14:09 -04001044void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001045 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -05001046 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001047{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001048 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -04001049}
1050
Török Edwin02b67512008-11-22 13:28:47 +02001051static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001052 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001053{
Török Edwinc7425ac2008-11-28 11:17:56 +02001054#ifdef CONFIG_STACKTRACE
Tom Zanussie1112b42009-03-31 00:48:49 -05001055 struct ftrace_event_call *call = &event_user_stack;
Török Edwin8d7c6a92008-11-23 12:39:06 +02001056 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001057 struct userstack_entry *entry;
1058 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001059
1060 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1061 return;
1062
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001063 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1064 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001065 if (!event)
1066 return;
1067 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001068
1069 memset(&entry->caller, 0, sizeof(entry->caller));
1070
1071 trace.nr_entries = 0;
1072 trace.max_entries = FTRACE_STACK_ENTRIES;
1073 trace.skip = 0;
1074 trace.entries = entry->caller;
1075
1076 save_stack_trace_user(&trace);
Tom Zanussieb02ce02009-04-08 03:15:54 -05001077 if (!filter_check_discard(call, entry, tr->buffer, event))
1078 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001079#endif
Török Edwin02b67512008-11-22 13:28:47 +02001080}
1081
Hannes Eder4fd27352009-02-10 19:44:12 +01001082#ifdef UNUSED
1083static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001084{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001085 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001086}
Hannes Eder4fd27352009-02-10 19:44:12 +01001087#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001088
Steven Rostedt38697052008-10-01 13:14:09 -04001089static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001090ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001091 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1092 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +02001093{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001094 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001095 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001096 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001097
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001098 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1099 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001100 if (!event)
1101 return;
1102 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001103 entry->arg1 = arg1;
1104 entry->arg2 = arg2;
1105 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001106 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +02001107}
1108
1109void
Steven Rostedt38697052008-10-01 13:14:09 -04001110__trace_special(void *__tr, void *__data,
1111 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1112{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001113 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001114}
1115
1116void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001117tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001118 struct task_struct *prev,
1119 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001120 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001121{
Tom Zanussie1112b42009-03-31 00:48:49 -05001122 struct ftrace_event_call *call = &event_context_switch;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001123 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001124 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001125
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001126 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1127 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001128 if (!event)
1129 return;
1130 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001131 entry->prev_pid = prev->pid;
1132 entry->prev_prio = prev->prio;
1133 entry->prev_state = prev->state;
1134 entry->next_pid = next->pid;
1135 entry->next_prio = next->prio;
1136 entry->next_state = next->state;
1137 entry->next_cpu = task_cpu(next);
Tom Zanussie1112b42009-03-31 00:48:49 -05001138
Tom Zanussieb02ce02009-04-08 03:15:54 -05001139 if (!filter_check_discard(call, entry, tr->buffer, event))
1140 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001141}
1142
Ingo Molnar57422792008-05-12 21:20:51 +02001143void
1144tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001145 struct task_struct *wakee,
1146 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001147 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001148{
Tom Zanussie1112b42009-03-31 00:48:49 -05001149 struct ftrace_event_call *call = &event_wakeup;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001150 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001151 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001152
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001153 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1154 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001155 if (!event)
1156 return;
1157 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001158 entry->prev_pid = curr->pid;
1159 entry->prev_prio = curr->prio;
1160 entry->prev_state = curr->state;
1161 entry->next_pid = wakee->pid;
1162 entry->next_prio = wakee->prio;
1163 entry->next_state = wakee->state;
1164 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001165
Tom Zanussieb02ce02009-04-08 03:15:54 -05001166 if (!filter_check_discard(call, entry, tr->buffer, event))
1167 ring_buffer_unlock_commit(tr->buffer, event);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001168 ftrace_trace_stack(tr, flags, 6, pc);
1169 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001170}
1171
Steven Rostedt4902f882008-05-22 00:22:18 -04001172void
1173ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1174{
1175 struct trace_array *tr = &global_trace;
1176 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001177 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001178 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001179 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001180
Steven Rostedtc76f0692008-11-07 22:36:02 -05001181 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001182 return;
1183
Steven Rostedt38697052008-10-01 13:14:09 -04001184 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001185 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001186 cpu = raw_smp_processor_id();
1187 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001188
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001189 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001190 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001191
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001192 atomic_dec(&data->disabled);
1193 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001194}
1195
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001196#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001197int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001198{
1199 struct trace_array *tr = &global_trace;
1200 struct trace_array_cpu *data;
1201 unsigned long flags;
1202 long disabled;
Frederic Weisbecker16185362009-03-23 22:17:01 +01001203 int ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001204 int cpu;
1205 int pc;
1206
Steven Rostedt804a6852008-12-03 15:36:59 -05001207 if (!ftrace_trace_task(current))
1208 return 0;
1209
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001210 if (!ftrace_graph_addr(trace->func))
1211 return 0;
1212
Steven Rostedta5e25882008-12-02 15:34:05 -05001213 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001214 cpu = raw_smp_processor_id();
1215 data = tr->data[cpu];
1216 disabled = atomic_inc_return(&data->disabled);
1217 if (likely(disabled == 1)) {
1218 pc = preempt_count();
Frederic Weisbecker16185362009-03-23 22:17:01 +01001219 ret = __trace_graph_entry(tr, trace, flags, pc);
1220 } else {
1221 ret = 0;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001222 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001223 /* Only do the atomic if it is not already set */
1224 if (!test_tsk_trace_graph(current))
1225 set_tsk_trace_graph(current);
Frederic Weisbecker16185362009-03-23 22:17:01 +01001226
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001227 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001228 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001229
Frederic Weisbecker16185362009-03-23 22:17:01 +01001230 return ret;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001231}
1232
1233void trace_graph_return(struct ftrace_graph_ret *trace)
1234{
1235 struct trace_array *tr = &global_trace;
1236 struct trace_array_cpu *data;
1237 unsigned long flags;
1238 long disabled;
1239 int cpu;
1240 int pc;
1241
Steven Rostedta5e25882008-12-02 15:34:05 -05001242 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001243 cpu = raw_smp_processor_id();
1244 data = tr->data[cpu];
1245 disabled = atomic_inc_return(&data->disabled);
1246 if (likely(disabled == 1)) {
1247 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001248 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001249 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001250 if (!trace->depth)
1251 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001252 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001253 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001254}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001255#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001256
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001257
1258/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001259 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001260 *
1261 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001262int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001263{
Steven Rostedt80370cb2009-03-10 17:16:35 -04001264 static raw_spinlock_t trace_buf_lock =
1265 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001266 static u32 trace_buf[TRACE_BUF_SIZE];
1267
Tom Zanussie1112b42009-03-31 00:48:49 -05001268 struct ftrace_event_call *call = &event_bprint;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001269 struct ring_buffer_event *event;
1270 struct trace_array *tr = &global_trace;
1271 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001272 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001273 unsigned long flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001274 int disable;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001275 int resched;
1276 int cpu, len = 0, size, pc;
1277
1278 if (unlikely(tracing_selftest_running || tracing_disabled))
1279 return 0;
1280
1281 /* Don't pollute graph traces with trace_vprintk internals */
1282 pause_graph_tracing();
1283
1284 pc = preempt_count();
1285 resched = ftrace_preempt_disable();
1286 cpu = raw_smp_processor_id();
1287 data = tr->data[cpu];
1288
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001289 disable = atomic_inc_return(&data->disabled);
1290 if (unlikely(disable != 1))
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001291 goto out;
1292
Steven Rostedt80370cb2009-03-10 17:16:35 -04001293 /* Lockdep uses trace_printk for lock tracing */
1294 local_irq_save(flags);
1295 __raw_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001296 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1297
1298 if (len > TRACE_BUF_SIZE || len < 0)
1299 goto out_unlock;
1300
1301 size = sizeof(*entry) + sizeof(u32) * len;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001302 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001303 if (!event)
1304 goto out_unlock;
1305 entry = ring_buffer_event_data(event);
1306 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001307 entry->fmt = fmt;
1308
1309 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
Tom Zanussieb02ce02009-04-08 03:15:54 -05001310 if (!filter_check_discard(call, entry, tr->buffer, event))
1311 ring_buffer_unlock_commit(tr->buffer, event);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001312
1313out_unlock:
Steven Rostedt80370cb2009-03-10 17:16:35 -04001314 __raw_spin_unlock(&trace_buf_lock);
1315 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001316
1317out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001318 atomic_dec_return(&data->disabled);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001319 ftrace_preempt_enable(resched);
1320 unpause_graph_tracing();
1321
1322 return len;
1323}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001324EXPORT_SYMBOL_GPL(trace_vbprintk);
1325
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001326int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001327{
1328 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1329 static char trace_buf[TRACE_BUF_SIZE];
1330
Tom Zanussie1112b42009-03-31 00:48:49 -05001331 struct ftrace_event_call *call = &event_print;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001332 struct ring_buffer_event *event;
1333 struct trace_array *tr = &global_trace;
1334 struct trace_array_cpu *data;
1335 int cpu, len = 0, size, pc;
1336 struct print_entry *entry;
1337 unsigned long irq_flags;
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001338 int disable;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001339
1340 if (tracing_disabled || tracing_selftest_running)
1341 return 0;
1342
1343 pc = preempt_count();
1344 preempt_disable_notrace();
1345 cpu = raw_smp_processor_id();
1346 data = tr->data[cpu];
1347
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001348 disable = atomic_inc_return(&data->disabled);
1349 if (unlikely(disable != 1))
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001350 goto out;
1351
1352 pause_graph_tracing();
1353 raw_local_irq_save(irq_flags);
1354 __raw_spin_lock(&trace_buf_lock);
1355 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1356
1357 len = min(len, TRACE_BUF_SIZE-1);
1358 trace_buf[len] = 0;
1359
1360 size = sizeof(*entry) + len + 1;
1361 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1362 if (!event)
1363 goto out_unlock;
1364 entry = ring_buffer_event_data(event);
1365 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001366
1367 memcpy(&entry->buf, trace_buf, len);
1368 entry->buf[len] = 0;
Tom Zanussieb02ce02009-04-08 03:15:54 -05001369 if (!filter_check_discard(call, entry, tr->buffer, event))
1370 ring_buffer_unlock_commit(tr->buffer, event);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001371
1372 out_unlock:
1373 __raw_spin_unlock(&trace_buf_lock);
1374 raw_local_irq_restore(irq_flags);
1375 unpause_graph_tracing();
1376 out:
Steven Rostedt3189cdb2009-04-17 16:13:55 -04001377 atomic_dec_return(&data->disabled);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001378 preempt_enable_notrace();
1379
1380 return len;
1381}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001382EXPORT_SYMBOL_GPL(trace_vprintk);
1383
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384enum trace_file_type {
1385 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001386 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387};
1388
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001389static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001390{
Steven Rostedtd7690412008-10-01 00:29:53 -04001391 /* Don't allow ftrace to trace into the ring buffers */
1392 ftrace_disable_cpu();
1393
Steven Rostedt5a90f572008-09-03 17:42:51 -04001394 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001395 if (iter->buffer_iter[iter->cpu])
1396 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1397
1398 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001399}
1400
Ingo Molnare309b412008-05-12 21:20:51 +02001401static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001402peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001403{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001404 struct ring_buffer_event *event;
1405 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001406
Steven Rostedtd7690412008-10-01 00:29:53 -04001407 /* Don't allow ftrace to trace into the ring buffers */
1408 ftrace_disable_cpu();
1409
1410 if (buf_iter)
1411 event = ring_buffer_iter_peek(buf_iter, ts);
1412 else
1413 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1414
1415 ftrace_enable_cpu();
1416
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001417 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001418}
Steven Rostedtd7690412008-10-01 00:29:53 -04001419
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001420static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001421__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001422{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001423 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001424 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001425 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001426 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427 int next_cpu = -1;
1428 int cpu;
1429
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001430 /*
1431 * If we are in a per_cpu trace file, don't bother by iterating over
1432 * all cpu and peek directly.
1433 */
1434 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1435 if (ring_buffer_empty_cpu(buffer, cpu_file))
1436 return NULL;
1437 ent = peek_next_entry(iter, cpu_file, ent_ts);
1438 if (ent_cpu)
1439 *ent_cpu = cpu_file;
1440
1441 return ent;
1442 }
1443
Steven Rostedtab464282008-05-12 21:21:00 +02001444 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001445
1446 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001447 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001448
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001449 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001450
Ingo Molnarcdd31cd22008-05-12 21:20:46 +02001451 /*
1452 * Pick the entry with the smallest timestamp:
1453 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001454 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 next = ent;
1456 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001457 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001458 }
1459 }
1460
1461 if (ent_cpu)
1462 *ent_cpu = next_cpu;
1463
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001464 if (ent_ts)
1465 *ent_ts = next_ts;
1466
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001467 return next;
1468}
1469
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001470/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001471struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1472 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001473{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001474 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001475}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001476
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001477/* Find the next real entry, and increment the iterator to the next entry */
1478static void *find_next_entry_inc(struct trace_iterator *iter)
1479{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001480 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001481
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001482 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001483 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001484
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001485 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001486}
1487
Ingo Molnare309b412008-05-12 21:20:51 +02001488static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001489{
Steven Rostedtd7690412008-10-01 00:29:53 -04001490 /* Don't allow ftrace to trace into the ring buffers */
1491 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001492 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001493 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001494}
1495
Ingo Molnare309b412008-05-12 21:20:51 +02001496static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001497{
1498 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001499 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001500 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001501
1502 (*pos)++;
1503
1504 /* can't go backwards */
1505 if (iter->idx > i)
1506 return NULL;
1507
1508 if (iter->idx < 0)
1509 ent = find_next_entry_inc(iter);
1510 else
1511 ent = iter;
1512
1513 while (ent && iter->idx < i)
1514 ent = find_next_entry_inc(iter);
1515
1516 iter->pos = *pos;
1517
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001518 return ent;
1519}
1520
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001521/*
1522 * No necessary locking here. The worst thing which can
1523 * happen is loosing events consumed at the same time
1524 * by a trace_pipe reader.
1525 * Other than that, we don't risk to crash the ring buffer
1526 * because it serializes the readers.
1527 *
1528 * The current tracer is copied to avoid a global locking
1529 * all around.
1530 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001531static void *s_start(struct seq_file *m, loff_t *pos)
1532{
1533 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001534 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001535 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001536 void *p = NULL;
1537 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001538 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001539
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001540 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001541 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001542 if (unlikely(old_tracer != current_trace && current_trace)) {
1543 old_tracer = current_trace;
1544 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001545 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001546 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547
1548 atomic_inc(&trace_record_cmdline_disabled);
1549
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001550 if (*pos != iter->pos) {
1551 iter->ent = NULL;
1552 iter->cpu = 0;
1553 iter->idx = -1;
1554
Steven Rostedtd7690412008-10-01 00:29:53 -04001555 ftrace_disable_cpu();
1556
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001557 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1558 for_each_tracing_cpu(cpu)
1559 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1560 } else
1561 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1562
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001563
Steven Rostedtd7690412008-10-01 00:29:53 -04001564 ftrace_enable_cpu();
1565
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001566 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1567 ;
1568
1569 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001570 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001571 p = s_next(m, p, &l);
1572 }
1573
Lai Jiangshan4f535962009-05-18 19:35:34 +08001574 trace_event_read_lock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001575 return p;
1576}
1577
1578static void s_stop(struct seq_file *m, void *p)
1579{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580 atomic_dec(&trace_record_cmdline_disabled);
Lai Jiangshan4f535962009-05-18 19:35:34 +08001581 trace_event_read_unlock();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001582}
1583
Ingo Molnare309b412008-05-12 21:20:51 +02001584static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001585{
Michael Ellermana6168352008-08-20 16:36:11 -07001586 seq_puts(m, "# _------=> CPU# \n");
1587 seq_puts(m, "# / _-----=> irqs-off \n");
1588 seq_puts(m, "# | / _----=> need-resched \n");
1589 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1590 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1591 seq_puts(m, "# |||| / \n");
1592 seq_puts(m, "# ||||| delay \n");
1593 seq_puts(m, "# cmd pid ||||| time | caller \n");
1594 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001595}
1596
Ingo Molnare309b412008-05-12 21:20:51 +02001597static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001598{
Michael Ellermana6168352008-08-20 16:36:11 -07001599 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1600 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001601}
1602
1603
Ingo Molnare309b412008-05-12 21:20:51 +02001604static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001605print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1606{
1607 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1608 struct trace_array *tr = iter->tr;
1609 struct trace_array_cpu *data = tr->data[tr->cpu];
1610 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001611 unsigned long total;
1612 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001613 const char *name = "preemption";
1614
1615 if (type)
1616 name = type->name;
1617
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001618 entries = ring_buffer_entries(iter->tr->buffer);
1619 total = entries +
1620 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001621
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001622 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001623 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001624 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001625 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001626 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001627 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001628 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001629 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001630 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001631 tr->cpu,
1632#if defined(CONFIG_PREEMPT_NONE)
1633 "server",
1634#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1635 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001636#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001637 "preempt",
1638#else
1639 "unknown",
1640#endif
1641 /* These are reserved for later use */
1642 0, 0, 0, 0);
1643#ifdef CONFIG_SMP
1644 seq_printf(m, " #P:%d)\n", num_online_cpus());
1645#else
1646 seq_puts(m, ")\n");
1647#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001648 seq_puts(m, "# -----------------\n");
1649 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001650 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1651 data->comm, data->pid, data->uid, data->nice,
1652 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001653 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001654
1655 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001656 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001657 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1658 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001659 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001660 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1661 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001662 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001663 }
1664
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001665 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001666}
1667
Steven Rostedta3097202008-11-07 22:36:02 -05001668static void test_cpu_buff_start(struct trace_iterator *iter)
1669{
1670 struct trace_seq *s = &iter->seq;
1671
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001672 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1673 return;
1674
1675 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1676 return;
1677
Rusty Russell44623442009-01-01 10:12:23 +10301678 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001679 return;
1680
Rusty Russell44623442009-01-01 10:12:23 +10301681 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001682
1683 /* Don't print started cpu buffer for the first entry of the trace */
1684 if (iter->idx > 1)
1685 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1686 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05001687}
1688
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001689static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001690{
Steven Rostedt214023c2008-05-12 21:20:46 +02001691 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001692 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001693 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001694 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001695
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001696 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001697
Steven Rostedta3097202008-11-07 22:36:02 -05001698 test_cpu_buff_start(iter);
1699
Steven Rostedtf633cef2008-12-23 23:24:13 -05001700 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001701
1702 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001703 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1704 if (!trace_print_lat_context(iter))
1705 goto partial;
1706 } else {
1707 if (!trace_print_context(iter))
1708 goto partial;
1709 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001710 }
1711
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001712 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001713 return event->trace(iter, sym_flags);
1714
1715 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1716 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001717
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001718 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001719partial:
1720 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001721}
1722
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001723static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001724{
1725 struct trace_seq *s = &iter->seq;
1726 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001727 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001728
1729 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001730
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001731 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001732 if (!trace_seq_printf(s, "%d %d %llu ",
1733 entry->pid, iter->cpu, iter->ts))
1734 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001735 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001736
Steven Rostedtf633cef2008-12-23 23:24:13 -05001737 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001738 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001739 return event->raw(iter, 0);
1740
1741 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1742 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001743
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001744 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001745partial:
1746 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001747}
1748
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001749static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001750{
1751 struct trace_seq *s = &iter->seq;
1752 unsigned char newline = '\n';
1753 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001754 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001755
1756 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001757
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001758 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1759 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1760 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1761 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1762 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001763
Steven Rostedtf633cef2008-12-23 23:24:13 -05001764 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001765 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001766 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001767 if (ret != TRACE_TYPE_HANDLED)
1768 return ret;
1769 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001770
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001771 SEQ_PUT_FIELD_RET(s, newline);
1772
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001773 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001774}
1775
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001776static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001777{
1778 struct trace_seq *s = &iter->seq;
1779 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001780 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001781
1782 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001783
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001784 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1785 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001786 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001787 SEQ_PUT_FIELD_RET(s, iter->ts);
1788 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001789
Steven Rostedtf633cef2008-12-23 23:24:13 -05001790 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001791 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001792}
1793
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001794static int trace_empty(struct trace_iterator *iter)
1795{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001796 int cpu;
1797
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001798 /* If we are looking at one CPU buffer, only check that one */
1799 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1800 cpu = iter->cpu_file;
1801 if (iter->buffer_iter[cpu]) {
1802 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1803 return 0;
1804 } else {
1805 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1806 return 0;
1807 }
1808 return 1;
1809 }
1810
Steven Rostedtab464282008-05-12 21:21:00 +02001811 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001812 if (iter->buffer_iter[cpu]) {
1813 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1814 return 0;
1815 } else {
1816 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1817 return 0;
1818 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001819 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001820
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001821 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001822}
1823
Lai Jiangshan4f535962009-05-18 19:35:34 +08001824/* Called with trace_event_read_lock() held. */
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001825static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001826{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001827 enum print_line_t ret;
1828
1829 if (iter->trace && iter->trace->print_line) {
1830 ret = iter->trace->print_line(iter);
1831 if (ret != TRACE_TYPE_UNHANDLED)
1832 return ret;
1833 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001834
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001835 if (iter->ent->type == TRACE_BPRINT &&
1836 trace_flags & TRACE_ITER_PRINTK &&
1837 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04001838 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001839
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001840 if (iter->ent->type == TRACE_PRINT &&
1841 trace_flags & TRACE_ITER_PRINTK &&
1842 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04001843 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001844
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001845 if (trace_flags & TRACE_ITER_BIN)
1846 return print_bin_fmt(iter);
1847
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001848 if (trace_flags & TRACE_ITER_HEX)
1849 return print_hex_fmt(iter);
1850
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001851 if (trace_flags & TRACE_ITER_RAW)
1852 return print_raw_fmt(iter);
1853
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001854 return print_trace_fmt(iter);
1855}
1856
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001857static int s_show(struct seq_file *m, void *v)
1858{
1859 struct trace_iterator *iter = v;
1860
1861 if (iter->ent == NULL) {
1862 if (iter->tr) {
1863 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1864 seq_puts(m, "#\n");
1865 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001866 if (iter->trace && iter->trace->print_header)
1867 iter->trace->print_header(m);
1868 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001869 /* print nothing if the buffers are empty */
1870 if (trace_empty(iter))
1871 return 0;
1872 print_trace_header(m, iter);
1873 if (!(trace_flags & TRACE_ITER_VERBOSE))
1874 print_lat_help_header(m);
1875 } else {
1876 if (!(trace_flags & TRACE_ITER_VERBOSE))
1877 print_func_help_header(m);
1878 }
1879 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001880 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001881 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001882 }
1883
1884 return 0;
1885}
1886
1887static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001888 .start = s_start,
1889 .next = s_next,
1890 .stop = s_stop,
1891 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001892};
1893
Ingo Molnare309b412008-05-12 21:20:51 +02001894static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001895__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001896{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001897 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001898 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001899 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001900 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001901 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001902
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001903 if (tracing_disabled)
1904 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001905
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001906 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001907 if (!iter)
1908 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001909
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001910 /*
1911 * We make a copy of the current tracer to avoid concurrent
1912 * changes on it while we are reading.
1913 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001914 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001915 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001916 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001917 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001918
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001919 if (current_trace)
1920 *iter->trace = *current_trace;
1921
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001922 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1923 goto fail;
1924
1925 cpumask_clear(iter->started);
1926
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001927 if (current_trace && current_trace->print_max)
1928 iter->tr = &max_tr;
1929 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001930 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001931 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001932 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001933 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001934
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001935 /* Notify the tracer early; before we stop tracing. */
1936 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001937 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001938
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001939 /* Annotate start of buffers if we had overruns */
1940 if (ring_buffer_overruns(iter->tr->buffer))
1941 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1942
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001943 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1944 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001945
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001946 iter->buffer_iter[cpu] =
1947 ring_buffer_read_start(iter->tr->buffer, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001948 }
1949 } else {
1950 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001951 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001952 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001953 }
1954
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001955 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001956 ret = seq_open(file, &tracer_seq_ops);
1957 if (ret < 0) {
1958 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001959 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001960 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001961
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001962 m = file->private_data;
1963 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001964
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001965 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001966 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001967
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001968 mutex_unlock(&trace_types_lock);
1969
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001970 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001971
1972 fail_buffer:
1973 for_each_tracing_cpu(cpu) {
1974 if (iter->buffer_iter[cpu])
1975 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1976 }
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001977 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001978 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001979 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001980 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001981 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001982
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001983 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001984}
1985
1986int tracing_open_generic(struct inode *inode, struct file *filp)
1987{
Steven Rostedt60a11772008-05-12 21:20:44 +02001988 if (tracing_disabled)
1989 return -ENODEV;
1990
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001991 filp->private_data = inode->i_private;
1992 return 0;
1993}
1994
Hannes Eder4fd27352009-02-10 19:44:12 +01001995static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001996{
1997 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001998 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001999 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002000
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002001 if (!(file->f_mode & FMODE_READ))
2002 return 0;
2003
2004 iter = m->private;
2005
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002006 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002007 for_each_tracing_cpu(cpu) {
2008 if (iter->buffer_iter[cpu])
2009 ring_buffer_read_finish(iter->buffer_iter[cpu]);
2010 }
2011
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002012 if (iter->trace && iter->trace->close)
2013 iter->trace->close(iter);
2014
2015 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05002016 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002017 mutex_unlock(&trace_types_lock);
2018
2019 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002020 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02002021 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002022 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002023 kfree(iter);
2024 return 0;
2025}
2026
2027static int tracing_open(struct inode *inode, struct file *file)
2028{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05002029 struct trace_iterator *iter;
2030 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002031
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002032 /* If this file was open for write, then erase contents */
2033 if ((file->f_mode & FMODE_WRITE) &&
2034 !(file->f_flags & O_APPEND)) {
2035 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002036
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002037 if (cpu == TRACE_PIPE_ALL_CPU)
2038 tracing_reset_online_cpus(&global_trace);
2039 else
2040 tracing_reset(&global_trace, cpu);
2041 }
2042
2043 if (file->f_mode & FMODE_READ) {
2044 iter = __tracing_open(inode, file);
2045 if (IS_ERR(iter))
2046 ret = PTR_ERR(iter);
2047 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2048 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2049 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002050 return ret;
2051}
2052
Ingo Molnare309b412008-05-12 21:20:51 +02002053static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002054t_next(struct seq_file *m, void *v, loff_t *pos)
2055{
2056 struct tracer *t = m->private;
2057
2058 (*pos)++;
2059
2060 if (t)
2061 t = t->next;
2062
2063 m->private = t;
2064
2065 return t;
2066}
2067
2068static void *t_start(struct seq_file *m, loff_t *pos)
2069{
2070 struct tracer *t = m->private;
2071 loff_t l = 0;
2072
2073 mutex_lock(&trace_types_lock);
2074 for (; t && l < *pos; t = t_next(m, t, &l))
2075 ;
2076
2077 return t;
2078}
2079
2080static void t_stop(struct seq_file *m, void *p)
2081{
2082 mutex_unlock(&trace_types_lock);
2083}
2084
2085static int t_show(struct seq_file *m, void *v)
2086{
2087 struct tracer *t = v;
2088
2089 if (!t)
2090 return 0;
2091
2092 seq_printf(m, "%s", t->name);
2093 if (t->next)
2094 seq_putc(m, ' ');
2095 else
2096 seq_putc(m, '\n');
2097
2098 return 0;
2099}
2100
2101static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002102 .start = t_start,
2103 .next = t_next,
2104 .stop = t_stop,
2105 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002106};
2107
2108static int show_traces_open(struct inode *inode, struct file *file)
2109{
2110 int ret;
2111
Steven Rostedt60a11772008-05-12 21:20:44 +02002112 if (tracing_disabled)
2113 return -ENODEV;
2114
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002115 ret = seq_open(file, &show_traces_seq_ops);
2116 if (!ret) {
2117 struct seq_file *m = file->private_data;
2118 m->private = trace_types;
2119 }
2120
2121 return ret;
2122}
2123
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002124static ssize_t
2125tracing_write_stub(struct file *filp, const char __user *ubuf,
2126 size_t count, loff_t *ppos)
2127{
2128 return count;
2129}
2130
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002131static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002132 .open = tracing_open,
2133 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002134 .write = tracing_write_stub,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002135 .llseek = seq_lseek,
2136 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002137};
2138
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002139static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002140 .open = show_traces_open,
2141 .read = seq_read,
2142 .release = seq_release,
2143};
2144
Ingo Molnar36dfe922008-05-12 21:20:52 +02002145/*
2146 * Only trace on a CPU if the bitmask is set:
2147 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302148static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002149
2150/*
2151 * The tracer itself will not take this lock, but still we want
2152 * to provide a consistent cpumask to user-space:
2153 */
2154static DEFINE_MUTEX(tracing_cpumask_update_lock);
2155
2156/*
2157 * Temporary storage for the character representation of the
2158 * CPU bitmask (and one more byte for the newline):
2159 */
2160static char mask_str[NR_CPUS + 1];
2161
Ingo Molnarc7078de2008-05-12 21:20:52 +02002162static ssize_t
2163tracing_cpumask_read(struct file *filp, char __user *ubuf,
2164 size_t count, loff_t *ppos)
2165{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002166 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002167
2168 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002169
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302170 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002171 if (count - len < 2) {
2172 count = -EINVAL;
2173 goto out_err;
2174 }
2175 len += sprintf(mask_str + len, "\n");
2176 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2177
2178out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002179 mutex_unlock(&tracing_cpumask_update_lock);
2180
2181 return count;
2182}
2183
2184static ssize_t
2185tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2186 size_t count, loff_t *ppos)
2187{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002188 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302189 cpumask_var_t tracing_cpumask_new;
2190
2191 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2192 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002193
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302194 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002195 if (err)
2196 goto err_unlock;
2197
Li Zefan215368e2009-06-15 10:56:42 +08002198 mutex_lock(&tracing_cpumask_update_lock);
2199
Steven Rostedta5e25882008-12-02 15:34:05 -05002200 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02002201 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002202 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002203 /*
2204 * Increase/decrease the disabled counter if we are
2205 * about to flip a bit in the cpumask:
2206 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302207 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2208 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002209 atomic_inc(&global_trace.data[cpu]->disabled);
2210 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302211 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2212 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002213 atomic_dec(&global_trace.data[cpu]->disabled);
2214 }
2215 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002216 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002217 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002218
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302219 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002220
Ingo Molnarc7078de2008-05-12 21:20:52 +02002221 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302222 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002223
Ingo Molnarc7078de2008-05-12 21:20:52 +02002224 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002225
2226err_unlock:
Li Zefan215368e2009-06-15 10:56:42 +08002227 free_cpumask_var(tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002228
2229 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002230}
2231
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002232static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002233 .open = tracing_open_generic,
2234 .read = tracing_cpumask_read,
2235 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002236};
2237
2238static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002239tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002240 size_t cnt, loff_t *ppos)
2241{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002242 struct tracer_opt *trace_opts;
2243 u32 tracer_flags;
2244 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002245 char *buf;
2246 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002247 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002248
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002249
Wenji Huangc3706f02009-02-10 01:03:18 -05002250 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002251 for (i = 0; trace_options[i]; i++) {
2252 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002253 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002254 }
2255
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002256 mutex_lock(&trace_types_lock);
2257 tracer_flags = current_trace->flags->val;
2258 trace_opts = current_trace->flags->opts;
2259
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002260 /*
2261 * Increase the size with names of options specific
2262 * of the current tracer.
2263 */
2264 for (i = 0; trace_opts[i].name; i++) {
2265 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002266 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002267 }
2268
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002269 /* +2 for \n and \0 */
2270 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002271 if (!buf) {
2272 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002273 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002274 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002275
2276 for (i = 0; trace_options[i]; i++) {
2277 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002278 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002279 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002280 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002281 }
2282
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002283 for (i = 0; trace_opts[i].name; i++) {
2284 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002285 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002286 trace_opts[i].name);
2287 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002288 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002289 trace_opts[i].name);
2290 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002291 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002292
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002293 WARN_ON(r >= len + 2);
2294
Ingo Molnar36dfe922008-05-12 21:20:52 +02002295 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002296
2297 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002298 return r;
2299}
2300
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002301/* Try to assign a tracer specific option */
2302static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2303{
2304 struct tracer_flags *trace_flags = trace->flags;
2305 struct tracer_opt *opts = NULL;
2306 int ret = 0, i = 0;
2307 int len;
2308
2309 for (i = 0; trace_flags->opts[i].name; i++) {
2310 opts = &trace_flags->opts[i];
2311 len = strlen(opts->name);
2312
2313 if (strncmp(cmp, opts->name, len) == 0) {
2314 ret = trace->set_flag(trace_flags->val,
2315 opts->bit, !neg);
2316 break;
2317 }
2318 }
2319 /* Not found */
2320 if (!trace_flags->opts[i].name)
2321 return -EINVAL;
2322
2323 /* Refused to handle */
2324 if (ret)
2325 return ret;
2326
2327 if (neg)
2328 trace_flags->val &= ~opts->bit;
2329 else
2330 trace_flags->val |= opts->bit;
2331
2332 return 0;
2333}
2334
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002335static void set_tracer_flags(unsigned int mask, int enabled)
2336{
2337 /* do nothing if flag is already set */
2338 if (!!(trace_flags & mask) == !!enabled)
2339 return;
2340
2341 if (enabled)
2342 trace_flags |= mask;
2343 else
2344 trace_flags &= ~mask;
2345
2346 if (mask == TRACE_ITER_GLOBAL_CLK) {
2347 u64 (*func)(void);
2348
2349 if (enabled)
2350 func = trace_clock_global;
2351 else
2352 func = trace_clock_local;
2353
2354 mutex_lock(&trace_types_lock);
2355 ring_buffer_set_clock(global_trace.buffer, func);
2356
2357 if (max_tr.buffer)
2358 ring_buffer_set_clock(max_tr.buffer, func);
2359 mutex_unlock(&trace_types_lock);
2360 }
2361}
2362
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002364tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002365 size_t cnt, loff_t *ppos)
2366{
2367 char buf[64];
2368 char *cmp = buf;
2369 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002370 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002371 int i;
2372
Steven Rostedtcffae432008-05-12 21:21:00 +02002373 if (cnt >= sizeof(buf))
2374 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002375
2376 if (copy_from_user(&buf, ubuf, cnt))
2377 return -EFAULT;
2378
2379 buf[cnt] = 0;
2380
2381 if (strncmp(buf, "no", 2) == 0) {
2382 neg = 1;
2383 cmp += 2;
2384 }
2385
2386 for (i = 0; trace_options[i]; i++) {
2387 int len = strlen(trace_options[i]);
2388
2389 if (strncmp(cmp, trace_options[i], len) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002390 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002391 break;
2392 }
2393 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002394
2395 /* If no option could be set, test the specific tracer options */
2396 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002397 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002398 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002399 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002400 if (ret)
2401 return ret;
2402 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002403
2404 filp->f_pos += cnt;
2405
2406 return cnt;
2407}
2408
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002409static const struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002410 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002411 .read = tracing_trace_options_read,
2412 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002413};
2414
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002415static const char readme_msg[] =
2416 "tracing mini-HOWTO:\n\n"
2417 "# mkdir /debug\n"
2418 "# mount -t debugfs nodev /debug\n\n"
2419 "# cat /debug/tracing/available_tracers\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302420 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002421 "# cat /debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302422 "nop\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002423 "# echo sched_switch > /debug/tracing/current_tracer\n"
2424 "# cat /debug/tracing/current_tracer\n"
2425 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002426 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002427 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002428 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002429 "# echo 1 > /debug/tracing/tracing_enabled\n"
2430 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
GeunSik Lim88fc86c2009-05-14 17:23:38 +09002431 "# echo 0 > /debug/tracing/tracing_enabled\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002432;
2433
2434static ssize_t
2435tracing_readme_read(struct file *filp, char __user *ubuf,
2436 size_t cnt, loff_t *ppos)
2437{
2438 return simple_read_from_buffer(ubuf, cnt, ppos,
2439 readme_msg, strlen(readme_msg));
2440}
2441
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002442static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002443 .open = tracing_open_generic,
2444 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002445};
2446
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002447static ssize_t
Avadh Patel69abe6a2009-04-10 16:04:48 -04002448tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2449 size_t cnt, loff_t *ppos)
2450{
2451 char *buf_comm;
2452 char *file_buf;
2453 char *buf;
2454 int len = 0;
2455 int pid;
2456 int i;
2457
2458 file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2459 if (!file_buf)
2460 return -ENOMEM;
2461
2462 buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2463 if (!buf_comm) {
2464 kfree(file_buf);
2465 return -ENOMEM;
2466 }
2467
2468 buf = file_buf;
2469
2470 for (i = 0; i < SAVED_CMDLINES; i++) {
2471 int r;
2472
2473 pid = map_cmdline_to_pid[i];
2474 if (pid == -1 || pid == NO_CMDLINE_MAP)
2475 continue;
2476
2477 trace_find_cmdline(pid, buf_comm);
2478 r = sprintf(buf, "%d %s\n", pid, buf_comm);
2479 buf += r;
2480 len += r;
2481 }
2482
2483 len = simple_read_from_buffer(ubuf, cnt, ppos,
2484 file_buf, len);
2485
2486 kfree(file_buf);
2487 kfree(buf_comm);
2488
2489 return len;
2490}
2491
2492static const struct file_operations tracing_saved_cmdlines_fops = {
2493 .open = tracing_open_generic,
2494 .read = tracing_saved_cmdlines_read,
2495};
2496
2497static ssize_t
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002498tracing_ctrl_read(struct file *filp, char __user *ubuf,
2499 size_t cnt, loff_t *ppos)
2500{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002501 char buf[64];
2502 int r;
2503
Steven Rostedt90369902008-11-05 16:05:44 -05002504 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002505 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002506}
2507
2508static ssize_t
2509tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2510 size_t cnt, loff_t *ppos)
2511{
2512 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002513 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002514 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002515 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002516
Steven Rostedtcffae432008-05-12 21:21:00 +02002517 if (cnt >= sizeof(buf))
2518 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002519
2520 if (copy_from_user(&buf, ubuf, cnt))
2521 return -EFAULT;
2522
2523 buf[cnt] = 0;
2524
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002525 ret = strict_strtoul(buf, 10, &val);
2526 if (ret < 0)
2527 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002528
2529 val = !!val;
2530
2531 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002532 if (tracer_enabled ^ val) {
2533 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002534 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002535 if (current_trace->start)
2536 current_trace->start(tr);
2537 tracing_start();
2538 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002539 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002540 tracing_stop();
2541 if (current_trace->stop)
2542 current_trace->stop(tr);
2543 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002544 }
2545 mutex_unlock(&trace_types_lock);
2546
2547 filp->f_pos += cnt;
2548
2549 return cnt;
2550}
2551
2552static ssize_t
2553tracing_set_trace_read(struct file *filp, char __user *ubuf,
2554 size_t cnt, loff_t *ppos)
2555{
2556 char buf[max_tracer_type_len+2];
2557 int r;
2558
2559 mutex_lock(&trace_types_lock);
2560 if (current_trace)
2561 r = sprintf(buf, "%s\n", current_trace->name);
2562 else
2563 r = sprintf(buf, "\n");
2564 mutex_unlock(&trace_types_lock);
2565
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002566 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002567}
2568
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002569int tracer_init(struct tracer *t, struct trace_array *tr)
2570{
2571 tracing_reset_online_cpus(tr);
2572 return t->init(tr);
2573}
2574
Steven Rostedt73c51622009-03-11 13:42:01 -04002575static int tracing_resize_ring_buffer(unsigned long size)
2576{
2577 int ret;
2578
2579 /*
2580 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002581 * we use the size that was given, and we can forget about
2582 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002583 */
2584 ring_buffer_expanded = 1;
2585
2586 ret = ring_buffer_resize(global_trace.buffer, size);
2587 if (ret < 0)
2588 return ret;
2589
2590 ret = ring_buffer_resize(max_tr.buffer, size);
2591 if (ret < 0) {
2592 int r;
2593
2594 r = ring_buffer_resize(global_trace.buffer,
2595 global_trace.entries);
2596 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002597 /*
2598 * AARGH! We are left with different
2599 * size max buffer!!!!
2600 * The max buffer is our "snapshot" buffer.
2601 * When a tracer needs a snapshot (one of the
2602 * latency tracers), it swaps the max buffer
2603 * with the saved snap shot. We succeeded to
2604 * update the size of the main buffer, but failed to
2605 * update the size of the max buffer. But when we tried
2606 * to reset the main buffer to the original size, we
2607 * failed there too. This is very unlikely to
2608 * happen, but if it does, warn and kill all
2609 * tracing.
2610 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002611 WARN_ON(1);
2612 tracing_disabled = 1;
2613 }
2614 return ret;
2615 }
2616
2617 global_trace.entries = size;
2618
2619 return ret;
2620}
2621
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002622/**
2623 * tracing_update_buffers - used by tracing facility to expand ring buffers
2624 *
2625 * To save on memory when the tracing is never used on a system with it
2626 * configured in. The ring buffers are set to a minimum size. But once
2627 * a user starts to use the tracing facility, then they need to grow
2628 * to their default size.
2629 *
2630 * This function is to be called when a tracer is about to be used.
2631 */
2632int tracing_update_buffers(void)
2633{
2634 int ret = 0;
2635
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002636 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002637 if (!ring_buffer_expanded)
2638 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002639 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002640
2641 return ret;
2642}
2643
Steven Rostedt577b7852009-02-26 23:43:05 -05002644struct trace_option_dentry;
2645
2646static struct trace_option_dentry *
2647create_trace_option_files(struct tracer *tracer);
2648
2649static void
2650destroy_trace_option_files(struct trace_option_dentry *topts);
2651
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002652static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002653{
Steven Rostedt577b7852009-02-26 23:43:05 -05002654 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002655 struct trace_array *tr = &global_trace;
2656 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002657 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002658
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002659 mutex_lock(&trace_types_lock);
2660
Steven Rostedt73c51622009-03-11 13:42:01 -04002661 if (!ring_buffer_expanded) {
2662 ret = tracing_resize_ring_buffer(trace_buf_size);
2663 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01002664 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04002665 ret = 0;
2666 }
2667
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002668 for (t = trace_types; t; t = t->next) {
2669 if (strcmp(t->name, buf) == 0)
2670 break;
2671 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002672 if (!t) {
2673 ret = -EINVAL;
2674 goto out;
2675 }
2676 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002677 goto out;
2678
Steven Rostedt9f029e82008-11-12 15:24:24 -05002679 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002680 if (current_trace && current_trace->reset)
2681 current_trace->reset(tr);
2682
Steven Rostedt577b7852009-02-26 23:43:05 -05002683 destroy_trace_option_files(topts);
2684
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002685 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002686
2687 topts = create_trace_option_files(current_trace);
2688
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002689 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002690 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002691 if (ret)
2692 goto out;
2693 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002694
Steven Rostedt9f029e82008-11-12 15:24:24 -05002695 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002696 out:
2697 mutex_unlock(&trace_types_lock);
2698
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002699 return ret;
2700}
2701
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002702static ssize_t
2703tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2704 size_t cnt, loff_t *ppos)
2705{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002706 char buf[max_tracer_type_len+1];
2707 int i;
2708 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002709 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002710
Steven Rostedt60063a62008-10-28 10:44:24 -04002711 ret = cnt;
2712
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002713 if (cnt > max_tracer_type_len)
2714 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002715
2716 if (copy_from_user(&buf, ubuf, cnt))
2717 return -EFAULT;
2718
2719 buf[cnt] = 0;
2720
2721 /* strip ending whitespace. */
2722 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2723 buf[i] = 0;
2724
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002725 err = tracing_set_tracer(buf);
2726 if (err)
2727 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002728
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002729 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002730
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002731 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002732}
2733
2734static ssize_t
2735tracing_max_lat_read(struct file *filp, char __user *ubuf,
2736 size_t cnt, loff_t *ppos)
2737{
2738 unsigned long *ptr = filp->private_data;
2739 char buf[64];
2740 int r;
2741
Steven Rostedtcffae432008-05-12 21:21:00 +02002742 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002743 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002744 if (r > sizeof(buf))
2745 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002746 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002747}
2748
2749static ssize_t
2750tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2751 size_t cnt, loff_t *ppos)
2752{
Hannes Eder5e398412009-02-10 19:44:34 +01002753 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002754 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002755 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002756 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002757
Steven Rostedtcffae432008-05-12 21:21:00 +02002758 if (cnt >= sizeof(buf))
2759 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002760
2761 if (copy_from_user(&buf, ubuf, cnt))
2762 return -EFAULT;
2763
2764 buf[cnt] = 0;
2765
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002766 ret = strict_strtoul(buf, 10, &val);
2767 if (ret < 0)
2768 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002769
2770 *ptr = val * 1000;
2771
2772 return cnt;
2773}
2774
Steven Rostedtb3806b42008-05-12 21:20:46 +02002775static int tracing_open_pipe(struct inode *inode, struct file *filp)
2776{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002777 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002778 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002779 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002780
2781 if (tracing_disabled)
2782 return -ENODEV;
2783
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002784 mutex_lock(&trace_types_lock);
2785
2786 /* We only allow one reader per cpu */
2787 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2788 if (!cpumask_empty(tracing_reader_cpumask)) {
2789 ret = -EBUSY;
2790 goto out;
2791 }
2792 cpumask_setall(tracing_reader_cpumask);
2793 } else {
2794 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2795 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2796 else {
2797 ret = -EBUSY;
2798 goto out;
2799 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002800 }
2801
2802 /* create a buffer to store the information to pass to userspace */
2803 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002804 if (!iter) {
2805 ret = -ENOMEM;
2806 goto out;
2807 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002808
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002809 /*
2810 * We make a copy of the current tracer to avoid concurrent
2811 * changes on it while we are reading.
2812 */
2813 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2814 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002815 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002816 goto fail;
2817 }
2818 if (current_trace)
2819 *iter->trace = *current_trace;
2820
2821 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2822 ret = -ENOMEM;
2823 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302824 }
2825
Steven Rostedta3097202008-11-07 22:36:02 -05002826 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302827 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002828
Steven Rostedt112f38a72009-06-01 15:16:05 -04002829 if (trace_flags & TRACE_ITER_LATENCY_FMT)
2830 iter->iter_flags |= TRACE_FILE_LAT_FMT;
2831
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002832 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002833 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002834 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002835 filp->private_data = iter;
2836
Steven Rostedt107bad82008-05-12 21:21:01 +02002837 if (iter->trace->pipe_open)
2838 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002839
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002840out:
2841 mutex_unlock(&trace_types_lock);
2842 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002843
2844fail:
2845 kfree(iter->trace);
2846 kfree(iter);
2847 mutex_unlock(&trace_types_lock);
2848 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002849}
2850
2851static int tracing_release_pipe(struct inode *inode, struct file *file)
2852{
2853 struct trace_iterator *iter = file->private_data;
2854
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002855 mutex_lock(&trace_types_lock);
2856
2857 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2858 cpumask_clear(tracing_reader_cpumask);
2859 else
2860 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2861
2862 mutex_unlock(&trace_types_lock);
2863
Rusty Russell44623442009-01-01 10:12:23 +10302864 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002865 mutex_destroy(&iter->mutex);
2866 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002867 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002868
2869 return 0;
2870}
2871
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002872static unsigned int
2873tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2874{
2875 struct trace_iterator *iter = filp->private_data;
2876
2877 if (trace_flags & TRACE_ITER_BLOCK) {
2878 /*
2879 * Always select as readable when in blocking mode
2880 */
2881 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002882 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002883 if (!trace_empty(iter))
2884 return POLLIN | POLLRDNORM;
2885 poll_wait(filp, &trace_wait, poll_table);
2886 if (!trace_empty(iter))
2887 return POLLIN | POLLRDNORM;
2888
2889 return 0;
2890 }
2891}
2892
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002893
2894void default_wait_pipe(struct trace_iterator *iter)
2895{
2896 DEFINE_WAIT(wait);
2897
2898 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2899
2900 if (trace_empty(iter))
2901 schedule();
2902
2903 finish_wait(&trace_wait, &wait);
2904}
2905
2906/*
2907 * This is a make-shift waitqueue.
2908 * A tracer might use this callback on some rare cases:
2909 *
2910 * 1) the current tracer might hold the runqueue lock when it wakes up
2911 * a reader, hence a deadlock (sched, function, and function graph tracers)
2912 * 2) the function tracers, trace all functions, we don't want
2913 * the overhead of calling wake_up and friends
2914 * (and tracing them too)
2915 *
2916 * Anyway, this is really very primitive wakeup.
2917 */
2918void poll_wait_pipe(struct trace_iterator *iter)
2919{
2920 set_current_state(TASK_INTERRUPTIBLE);
2921 /* sleep for 100 msecs, and try again. */
2922 schedule_timeout(HZ / 10);
2923}
2924
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002925/* Must be called with trace_types_lock mutex held. */
2926static int tracing_wait_pipe(struct file *filp)
2927{
2928 struct trace_iterator *iter = filp->private_data;
2929
2930 while (trace_empty(iter)) {
2931
2932 if ((filp->f_flags & O_NONBLOCK)) {
2933 return -EAGAIN;
2934 }
2935
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002936 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002937
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002938 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002939
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002940 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002941
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002942 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002943 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002944
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002945 /*
2946 * We block until we read something and tracing is disabled.
2947 * We still block if tracing is disabled, but we have never
2948 * read anything. This allows a user to cat this file, and
2949 * then enable tracing. But after we have read something,
2950 * we give an EOF when tracing is again disabled.
2951 *
2952 * iter->pos will be 0 if we haven't read anything.
2953 */
2954 if (!tracer_enabled && iter->pos)
2955 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002956 }
2957
2958 return 1;
2959}
2960
Steven Rostedtb3806b42008-05-12 21:20:46 +02002961/*
2962 * Consumer reader.
2963 */
2964static ssize_t
2965tracing_read_pipe(struct file *filp, char __user *ubuf,
2966 size_t cnt, loff_t *ppos)
2967{
2968 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002969 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002970 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002971
2972 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002973 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2974 if (sret != -EBUSY)
2975 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002976
Steven Rostedtf9520752009-03-02 14:04:40 -05002977 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002978
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002979 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002980 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002981 if (unlikely(old_tracer != current_trace && current_trace)) {
2982 old_tracer = current_trace;
2983 *iter->trace = *current_trace;
2984 }
2985 mutex_unlock(&trace_types_lock);
2986
2987 /*
2988 * Avoid more than one consumer on a single file descriptor
2989 * This is just a matter of traces coherency, the ring buffer itself
2990 * is protected.
2991 */
2992 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002993 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002994 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2995 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002996 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002997 }
2998
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002999waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003000 sret = tracing_wait_pipe(filp);
3001 if (sret <= 0)
3002 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003003
3004 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003005 if (trace_empty(iter)) {
3006 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02003007 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02003008 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02003009
3010 if (cnt >= PAGE_SIZE)
3011 cnt = PAGE_SIZE - 1;
3012
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003013 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02003014 memset(&iter->seq, 0,
3015 sizeof(struct trace_iterator) -
3016 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02003017 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003018
Lai Jiangshan4f535962009-05-18 19:35:34 +08003019 trace_event_read_lock();
Steven Rostedt088b1e422008-05-12 21:20:48 +02003020 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003021 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003022 int len = iter->seq.len;
3023
Ingo Molnarf9896bf2008-05-12 21:20:47 +02003024 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02003025 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02003026 /* don't print partial lines */
3027 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003028 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02003029 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01003030 if (ret != TRACE_TYPE_NO_CONSUME)
3031 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003032
3033 if (iter->seq.len >= cnt)
3034 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003035 }
Lai Jiangshan4f535962009-05-18 19:35:34 +08003036 trace_event_read_unlock();
Steven Rostedtb3806b42008-05-12 21:20:46 +02003037
Steven Rostedtb3806b42008-05-12 21:20:46 +02003038 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003039 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3040 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05003041 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003042
3043 /*
3044 * If there was nothing to send to user, inspite of consuming trace
3045 * entries, go back to wait for more entries.
3046 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003047 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02003048 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003049
Steven Rostedt107bad82008-05-12 21:21:01 +02003050out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003051 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02003052
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02003053 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02003054}
3055
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003056static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3057 struct pipe_buffer *buf)
3058{
3059 __free_page(buf->page);
3060}
3061
3062static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3063 unsigned int idx)
3064{
3065 __free_page(spd->pages[idx]);
3066}
3067
3068static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003069 .can_merge = 0,
3070 .map = generic_pipe_buf_map,
3071 .unmap = generic_pipe_buf_unmap,
3072 .confirm = generic_pipe_buf_confirm,
3073 .release = tracing_pipe_buf_release,
3074 .steal = generic_pipe_buf_steal,
3075 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003076};
3077
Steven Rostedt34cd4992009-02-09 12:06:29 -05003078static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003079tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003080{
3081 size_t count;
3082 int ret;
3083
3084 /* Seq buffer is page-sized, exactly what we need. */
3085 for (;;) {
3086 count = iter->seq.len;
3087 ret = print_trace_line(iter);
3088 count = iter->seq.len - count;
3089 if (rem < count) {
3090 rem = 0;
3091 iter->seq.len -= count;
3092 break;
3093 }
3094 if (ret == TRACE_TYPE_PARTIAL_LINE) {
3095 iter->seq.len -= count;
3096 break;
3097 }
3098
3099 trace_consume(iter);
3100 rem -= count;
3101 if (!find_next_entry_inc(iter)) {
3102 rem = 0;
3103 iter->ent = NULL;
3104 break;
3105 }
3106 }
3107
3108 return rem;
3109}
3110
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003111static ssize_t tracing_splice_read_pipe(struct file *filp,
3112 loff_t *ppos,
3113 struct pipe_inode_info *pipe,
3114 size_t len,
3115 unsigned int flags)
3116{
3117 struct page *pages[PIPE_BUFFERS];
3118 struct partial_page partial[PIPE_BUFFERS];
3119 struct trace_iterator *iter = filp->private_data;
3120 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003121 .pages = pages,
3122 .partial = partial,
3123 .nr_pages = 0, /* This gets updated below. */
3124 .flags = flags,
3125 .ops = &tracing_pipe_buf_ops,
3126 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003127 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003128 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003129 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003130 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003131 unsigned int i;
3132
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003133 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003134 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003135 if (unlikely(old_tracer != current_trace && current_trace)) {
3136 old_tracer = current_trace;
3137 *iter->trace = *current_trace;
3138 }
3139 mutex_unlock(&trace_types_lock);
3140
3141 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003142
3143 if (iter->trace->splice_read) {
3144 ret = iter->trace->splice_read(iter, filp,
3145 ppos, pipe, len, flags);
3146 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003147 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003148 }
3149
3150 ret = tracing_wait_pipe(filp);
3151 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003152 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003153
3154 if (!iter->ent && !find_next_entry_inc(iter)) {
3155 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003156 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003157 }
3158
Lai Jiangshan4f535962009-05-18 19:35:34 +08003159 trace_event_read_lock();
3160
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003161 /* Fill as many pages as possible. */
3162 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3163 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003164 if (!pages[i])
3165 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003166
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003167 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003168
3169 /* Copy the data into the page, so we can start over. */
3170 ret = trace_seq_to_buffer(&iter->seq,
3171 page_address(pages[i]),
3172 iter->seq.len);
3173 if (ret < 0) {
3174 __free_page(pages[i]);
3175 break;
3176 }
3177 partial[i].offset = 0;
3178 partial[i].len = iter->seq.len;
3179
Steven Rostedtf9520752009-03-02 14:04:40 -05003180 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003181 }
3182
Lai Jiangshan4f535962009-05-18 19:35:34 +08003183 trace_event_read_unlock();
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003184 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003185
3186 spd.nr_pages = i;
3187
3188 return splice_to_pipe(pipe, &spd);
3189
Steven Rostedt34cd4992009-02-09 12:06:29 -05003190out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003191 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003192
3193 return ret;
3194}
3195
Steven Rostedta98a3c32008-05-12 21:20:59 +02003196static ssize_t
3197tracing_entries_read(struct file *filp, char __user *ubuf,
3198 size_t cnt, loff_t *ppos)
3199{
3200 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003201 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003202 int r;
3203
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003204 mutex_lock(&trace_types_lock);
3205 if (!ring_buffer_expanded)
3206 r = sprintf(buf, "%lu (expanded: %lu)\n",
3207 tr->entries >> 10,
3208 trace_buf_size >> 10);
3209 else
3210 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3211 mutex_unlock(&trace_types_lock);
3212
Steven Rostedta98a3c32008-05-12 21:20:59 +02003213 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3214}
3215
3216static ssize_t
3217tracing_entries_write(struct file *filp, const char __user *ubuf,
3218 size_t cnt, loff_t *ppos)
3219{
3220 unsigned long val;
3221 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003222 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003223
Steven Rostedtcffae432008-05-12 21:21:00 +02003224 if (cnt >= sizeof(buf))
3225 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003226
3227 if (copy_from_user(&buf, ubuf, cnt))
3228 return -EFAULT;
3229
3230 buf[cnt] = 0;
3231
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003232 ret = strict_strtoul(buf, 10, &val);
3233 if (ret < 0)
3234 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003235
3236 /* must have at least 1 entry */
3237 if (!val)
3238 return -EINVAL;
3239
3240 mutex_lock(&trace_types_lock);
3241
Steven Rostedtc76f0692008-11-07 22:36:02 -05003242 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003243
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003244 /* disable all cpu buffers */
3245 for_each_tracing_cpu(cpu) {
3246 if (global_trace.data[cpu])
3247 atomic_inc(&global_trace.data[cpu]->disabled);
3248 if (max_tr.data[cpu])
3249 atomic_inc(&max_tr.data[cpu]->disabled);
3250 }
3251
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003252 /* value is in KB */
3253 val <<= 10;
3254
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003255 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003256 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003257 if (ret < 0) {
3258 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003259 goto out;
3260 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003261 }
3262
3263 filp->f_pos += cnt;
3264
Steven Rostedt19384c02008-05-22 00:22:16 -04003265 /* If check pages failed, return ENOMEM */
3266 if (tracing_disabled)
3267 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003268 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003269 for_each_tracing_cpu(cpu) {
3270 if (global_trace.data[cpu])
3271 atomic_dec(&global_trace.data[cpu]->disabled);
3272 if (max_tr.data[cpu])
3273 atomic_dec(&max_tr.data[cpu]->disabled);
3274 }
3275
Steven Rostedtc76f0692008-11-07 22:36:02 -05003276 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003277 max_tr.entries = global_trace.entries;
3278 mutex_unlock(&trace_types_lock);
3279
3280 return cnt;
3281}
3282
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003283static int mark_printk(const char *fmt, ...)
3284{
3285 int ret;
3286 va_list args;
3287 va_start(args, fmt);
Steven Rostedt40ce74f2009-03-19 14:03:53 -04003288 ret = trace_vprintk(0, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003289 va_end(args);
3290 return ret;
3291}
3292
3293static ssize_t
3294tracing_mark_write(struct file *filp, const char __user *ubuf,
3295 size_t cnt, loff_t *fpos)
3296{
3297 char *buf;
3298 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003299
Steven Rostedtc76f0692008-11-07 22:36:02 -05003300 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003301 return -EINVAL;
3302
3303 if (cnt > TRACE_BUF_SIZE)
3304 cnt = TRACE_BUF_SIZE;
3305
3306 buf = kmalloc(cnt + 1, GFP_KERNEL);
3307 if (buf == NULL)
3308 return -ENOMEM;
3309
3310 if (copy_from_user(buf, ubuf, cnt)) {
3311 kfree(buf);
3312 return -EFAULT;
3313 }
3314
3315 /* Cut from the first nil or newline. */
3316 buf[cnt] = '\0';
3317 end = strchr(buf, '\n');
3318 if (end)
3319 *end = '\0';
3320
3321 cnt = mark_printk("%s\n", buf);
3322 kfree(buf);
3323 *fpos += cnt;
3324
3325 return cnt;
3326}
3327
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003328static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003329 .open = tracing_open_generic,
3330 .read = tracing_max_lat_read,
3331 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003332};
3333
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003334static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003335 .open = tracing_open_generic,
3336 .read = tracing_ctrl_read,
3337 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003338};
3339
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003340static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003341 .open = tracing_open_generic,
3342 .read = tracing_set_trace_read,
3343 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003344};
3345
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003346static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003347 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003348 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003349 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003350 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003351 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003352};
3353
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003354static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003355 .open = tracing_open_generic,
3356 .read = tracing_entries_read,
3357 .write = tracing_entries_write,
3358};
3359
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003360static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003361 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003362 .write = tracing_mark_write,
3363};
3364
Steven Rostedt2cadf912008-12-01 22:20:19 -05003365struct ftrace_buffer_info {
3366 struct trace_array *tr;
3367 void *spare;
3368 int cpu;
3369 unsigned int read;
3370};
3371
3372static int tracing_buffers_open(struct inode *inode, struct file *filp)
3373{
3374 int cpu = (int)(long)inode->i_private;
3375 struct ftrace_buffer_info *info;
3376
3377 if (tracing_disabled)
3378 return -ENODEV;
3379
3380 info = kzalloc(sizeof(*info), GFP_KERNEL);
3381 if (!info)
3382 return -ENOMEM;
3383
3384 info->tr = &global_trace;
3385 info->cpu = cpu;
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003386 info->spare = NULL;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003387 /* Force reading ring buffer for first read */
3388 info->read = (unsigned int)-1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003389
3390 filp->private_data = info;
3391
Lai Jiangshand1e7e022009-04-02 15:16:56 +08003392 return nonseekable_open(inode, filp);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003393}
3394
3395static ssize_t
3396tracing_buffers_read(struct file *filp, char __user *ubuf,
3397 size_t count, loff_t *ppos)
3398{
3399 struct ftrace_buffer_info *info = filp->private_data;
3400 unsigned int pos;
3401 ssize_t ret;
3402 size_t size;
3403
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003404 if (!count)
3405 return 0;
3406
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003407 if (!info->spare)
3408 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3409 if (!info->spare)
3410 return -ENOMEM;
3411
Steven Rostedt2cadf912008-12-01 22:20:19 -05003412 /* Do we have previous read data to read? */
3413 if (info->read < PAGE_SIZE)
3414 goto read;
3415
3416 info->read = 0;
3417
3418 ret = ring_buffer_read_page(info->tr->buffer,
3419 &info->spare,
3420 count,
3421 info->cpu, 0);
3422 if (ret < 0)
3423 return 0;
3424
3425 pos = ring_buffer_page_len(info->spare);
3426
3427 if (pos < PAGE_SIZE)
3428 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3429
3430read:
3431 size = PAGE_SIZE - info->read;
3432 if (size > count)
3433 size = count;
3434
3435 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003436 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003437 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003438 size -= ret;
3439
Steven Rostedt2cadf912008-12-01 22:20:19 -05003440 *ppos += size;
3441 info->read += size;
3442
3443 return size;
3444}
3445
3446static int tracing_buffers_release(struct inode *inode, struct file *file)
3447{
3448 struct ftrace_buffer_info *info = file->private_data;
3449
Lai Jiangshanddd538f2009-04-02 15:16:59 +08003450 if (info->spare)
3451 ring_buffer_free_read_page(info->tr->buffer, info->spare);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003452 kfree(info);
3453
3454 return 0;
3455}
3456
3457struct buffer_ref {
3458 struct ring_buffer *buffer;
3459 void *page;
3460 int ref;
3461};
3462
3463static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3464 struct pipe_buffer *buf)
3465{
3466 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3467
3468 if (--ref->ref)
3469 return;
3470
3471 ring_buffer_free_read_page(ref->buffer, ref->page);
3472 kfree(ref);
3473 buf->private = 0;
3474}
3475
3476static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3477 struct pipe_buffer *buf)
3478{
3479 return 1;
3480}
3481
3482static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3483 struct pipe_buffer *buf)
3484{
3485 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3486
3487 ref->ref++;
3488}
3489
3490/* Pipe buffer operations for a buffer. */
3491static struct pipe_buf_operations buffer_pipe_buf_ops = {
3492 .can_merge = 0,
3493 .map = generic_pipe_buf_map,
3494 .unmap = generic_pipe_buf_unmap,
3495 .confirm = generic_pipe_buf_confirm,
3496 .release = buffer_pipe_buf_release,
3497 .steal = buffer_pipe_buf_steal,
3498 .get = buffer_pipe_buf_get,
3499};
3500
3501/*
3502 * Callback from splice_to_pipe(), if we need to release some pages
3503 * at the end of the spd in case we error'ed out in filling the pipe.
3504 */
3505static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3506{
3507 struct buffer_ref *ref =
3508 (struct buffer_ref *)spd->partial[i].private;
3509
3510 if (--ref->ref)
3511 return;
3512
3513 ring_buffer_free_read_page(ref->buffer, ref->page);
3514 kfree(ref);
3515 spd->partial[i].private = 0;
3516}
3517
3518static ssize_t
3519tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3520 struct pipe_inode_info *pipe, size_t len,
3521 unsigned int flags)
3522{
3523 struct ftrace_buffer_info *info = file->private_data;
3524 struct partial_page partial[PIPE_BUFFERS];
3525 struct page *pages[PIPE_BUFFERS];
3526 struct splice_pipe_desc spd = {
3527 .pages = pages,
3528 .partial = partial,
3529 .flags = flags,
3530 .ops = &buffer_pipe_buf_ops,
3531 .spd_release = buffer_spd_release,
3532 };
3533 struct buffer_ref *ref;
Steven Rostedt93459c62009-04-29 00:23:13 -04003534 int entries, size, i;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003535 size_t ret;
3536
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003537 if (*ppos & (PAGE_SIZE - 1)) {
3538 WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3539 return -EINVAL;
3540 }
3541
3542 if (len & (PAGE_SIZE - 1)) {
3543 WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3544 if (len < PAGE_SIZE)
3545 return -EINVAL;
3546 len &= PAGE_MASK;
3547 }
3548
Steven Rostedt93459c62009-04-29 00:23:13 -04003549 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3550
3551 for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
Steven Rostedt2cadf912008-12-01 22:20:19 -05003552 struct page *page;
3553 int r;
3554
3555 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3556 if (!ref)
3557 break;
3558
Steven Rostedt7267fa62009-04-29 00:16:21 -04003559 ref->ref = 1;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003560 ref->buffer = info->tr->buffer;
3561 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3562 if (!ref->page) {
3563 kfree(ref);
3564 break;
3565 }
3566
3567 r = ring_buffer_read_page(ref->buffer, &ref->page,
Steven Rostedtf2957f12009-04-29 00:26:30 -04003568 len, info->cpu, 1);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003569 if (r < 0) {
3570 ring_buffer_free_read_page(ref->buffer,
3571 ref->page);
3572 kfree(ref);
3573 break;
3574 }
3575
3576 /*
3577 * zero out any left over data, this is going to
3578 * user land.
3579 */
3580 size = ring_buffer_page_len(ref->page);
3581 if (size < PAGE_SIZE)
3582 memset(ref->page + size, 0, PAGE_SIZE - size);
3583
3584 page = virt_to_page(ref->page);
3585
3586 spd.pages[i] = page;
3587 spd.partial[i].len = PAGE_SIZE;
3588 spd.partial[i].offset = 0;
3589 spd.partial[i].private = (unsigned long)ref;
3590 spd.nr_pages++;
Lai Jiangshan93cfb3c2009-04-02 15:17:08 +08003591 *ppos += PAGE_SIZE;
Steven Rostedt93459c62009-04-29 00:23:13 -04003592
3593 entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
Steven Rostedt2cadf912008-12-01 22:20:19 -05003594 }
3595
3596 spd.nr_pages = i;
3597
3598 /* did we read anything? */
3599 if (!spd.nr_pages) {
3600 if (flags & SPLICE_F_NONBLOCK)
3601 ret = -EAGAIN;
3602 else
3603 ret = 0;
3604 /* TODO: block */
3605 return ret;
3606 }
3607
3608 ret = splice_to_pipe(pipe, &spd);
3609
3610 return ret;
3611}
3612
3613static const struct file_operations tracing_buffers_fops = {
3614 .open = tracing_buffers_open,
3615 .read = tracing_buffers_read,
3616 .release = tracing_buffers_release,
3617 .splice_read = tracing_buffers_splice_read,
3618 .llseek = no_llseek,
3619};
3620
Steven Rostedtc8d77182009-04-29 18:03:45 -04003621static ssize_t
3622tracing_stats_read(struct file *filp, char __user *ubuf,
3623 size_t count, loff_t *ppos)
3624{
3625 unsigned long cpu = (unsigned long)filp->private_data;
3626 struct trace_array *tr = &global_trace;
3627 struct trace_seq *s;
3628 unsigned long cnt;
3629
Li Zefane4f2d102009-06-15 10:57:28 +08003630 s = kmalloc(sizeof(*s), GFP_KERNEL);
Steven Rostedtc8d77182009-04-29 18:03:45 -04003631 if (!s)
3632 return ENOMEM;
3633
3634 trace_seq_init(s);
3635
3636 cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3637 trace_seq_printf(s, "entries: %ld\n", cnt);
3638
3639 cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3640 trace_seq_printf(s, "overrun: %ld\n", cnt);
3641
3642 cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3643 trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3644
3645 cnt = ring_buffer_nmi_dropped_cpu(tr->buffer, cpu);
3646 trace_seq_printf(s, "nmi dropped: %ld\n", cnt);
3647
3648 count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3649
3650 kfree(s);
3651
3652 return count;
3653}
3654
3655static const struct file_operations tracing_stats_fops = {
3656 .open = tracing_open_generic,
3657 .read = tracing_stats_read,
3658};
3659
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003660#ifdef CONFIG_DYNAMIC_FTRACE
3661
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003662int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003663{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003664 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003665}
3666
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003667static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003668tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003669 size_t cnt, loff_t *ppos)
3670{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003671 static char ftrace_dyn_info_buffer[1024];
3672 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003673 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003674 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003675 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003676 int r;
3677
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003678 mutex_lock(&dyn_info_mutex);
3679 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003680
Steven Rostedta26a2a22008-10-31 00:03:22 -04003681 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003682 buf[r++] = '\n';
3683
3684 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3685
3686 mutex_unlock(&dyn_info_mutex);
3687
3688 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003689}
3690
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003691static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003692 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003693 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003694};
3695#endif
3696
3697static struct dentry *d_tracer;
3698
3699struct dentry *tracing_init_dentry(void)
3700{
3701 static int once;
3702
3703 if (d_tracer)
3704 return d_tracer;
3705
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01003706 if (!debugfs_initialized())
3707 return NULL;
3708
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003709 d_tracer = debugfs_create_dir("tracing", NULL);
3710
3711 if (!d_tracer && !once) {
3712 once = 1;
3713 pr_warning("Could not create debugfs directory 'tracing'\n");
3714 return NULL;
3715 }
3716
3717 return d_tracer;
3718}
3719
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003720static struct dentry *d_percpu;
3721
3722struct dentry *tracing_dentry_percpu(void)
3723{
3724 static int once;
3725 struct dentry *d_tracer;
3726
3727 if (d_percpu)
3728 return d_percpu;
3729
3730 d_tracer = tracing_init_dentry();
3731
3732 if (!d_tracer)
3733 return NULL;
3734
3735 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3736
3737 if (!d_percpu && !once) {
3738 once = 1;
3739 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3740 return NULL;
3741 }
3742
3743 return d_percpu;
3744}
3745
3746static void tracing_init_debugfs_percpu(long cpu)
3747{
3748 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003749 struct dentry *d_cpu;
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003750 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3751 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003752
3753 if (cpu > 999 || cpu < 0)
3754 return;
3755
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003756 sprintf(cpu_dir, "cpu%ld", cpu);
3757 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3758 if (!d_cpu) {
3759 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3760 return;
3761 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003762
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003763 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003764 trace_create_file("trace_pipe", 0444, d_cpu,
3765 (void *) cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003766
3767 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003768 trace_create_file("trace", 0644, d_cpu,
3769 (void *) cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04003770
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003771 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3772 (void *) cpu, &tracing_buffers_fops);
Steven Rostedtc8d77182009-04-29 18:03:45 -04003773
3774 trace_create_file("stats", 0444, d_cpu,
3775 (void *) cpu, &tracing_stats_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003776}
3777
Steven Rostedt60a11772008-05-12 21:20:44 +02003778#ifdef CONFIG_FTRACE_SELFTEST
3779/* Let selftest have access to static functions in this file */
3780#include "trace_selftest.c"
3781#endif
3782
Steven Rostedt577b7852009-02-26 23:43:05 -05003783struct trace_option_dentry {
3784 struct tracer_opt *opt;
3785 struct tracer_flags *flags;
3786 struct dentry *entry;
3787};
3788
3789static ssize_t
3790trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3791 loff_t *ppos)
3792{
3793 struct trace_option_dentry *topt = filp->private_data;
3794 char *buf;
3795
3796 if (topt->flags->val & topt->opt->bit)
3797 buf = "1\n";
3798 else
3799 buf = "0\n";
3800
3801 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3802}
3803
3804static ssize_t
3805trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3806 loff_t *ppos)
3807{
3808 struct trace_option_dentry *topt = filp->private_data;
3809 unsigned long val;
3810 char buf[64];
3811 int ret;
3812
3813 if (cnt >= sizeof(buf))
3814 return -EINVAL;
3815
3816 if (copy_from_user(&buf, ubuf, cnt))
3817 return -EFAULT;
3818
3819 buf[cnt] = 0;
3820
3821 ret = strict_strtoul(buf, 10, &val);
3822 if (ret < 0)
3823 return ret;
3824
3825 ret = 0;
3826 switch (val) {
3827 case 0:
3828 /* do nothing if already cleared */
3829 if (!(topt->flags->val & topt->opt->bit))
3830 break;
3831
3832 mutex_lock(&trace_types_lock);
3833 if (current_trace->set_flag)
3834 ret = current_trace->set_flag(topt->flags->val,
3835 topt->opt->bit, 0);
3836 mutex_unlock(&trace_types_lock);
3837 if (ret)
3838 return ret;
3839 topt->flags->val &= ~topt->opt->bit;
3840 break;
3841 case 1:
3842 /* do nothing if already set */
3843 if (topt->flags->val & topt->opt->bit)
3844 break;
3845
3846 mutex_lock(&trace_types_lock);
3847 if (current_trace->set_flag)
3848 ret = current_trace->set_flag(topt->flags->val,
3849 topt->opt->bit, 1);
3850 mutex_unlock(&trace_types_lock);
3851 if (ret)
3852 return ret;
3853 topt->flags->val |= topt->opt->bit;
3854 break;
3855
3856 default:
3857 return -EINVAL;
3858 }
3859
3860 *ppos += cnt;
3861
3862 return cnt;
3863}
3864
3865
3866static const struct file_operations trace_options_fops = {
3867 .open = tracing_open_generic,
3868 .read = trace_options_read,
3869 .write = trace_options_write,
3870};
3871
Steven Rostedta8259072009-02-26 22:19:12 -05003872static ssize_t
3873trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3874 loff_t *ppos)
3875{
3876 long index = (long)filp->private_data;
3877 char *buf;
3878
3879 if (trace_flags & (1 << index))
3880 buf = "1\n";
3881 else
3882 buf = "0\n";
3883
3884 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3885}
3886
3887static ssize_t
3888trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3889 loff_t *ppos)
3890{
3891 long index = (long)filp->private_data;
3892 char buf[64];
3893 unsigned long val;
3894 int ret;
3895
3896 if (cnt >= sizeof(buf))
3897 return -EINVAL;
3898
3899 if (copy_from_user(&buf, ubuf, cnt))
3900 return -EFAULT;
3901
3902 buf[cnt] = 0;
3903
3904 ret = strict_strtoul(buf, 10, &val);
3905 if (ret < 0)
3906 return ret;
3907
3908 switch (val) {
3909 case 0:
3910 trace_flags &= ~(1 << index);
3911 break;
3912 case 1:
3913 trace_flags |= 1 << index;
3914 break;
3915
3916 default:
3917 return -EINVAL;
3918 }
3919
3920 *ppos += cnt;
3921
3922 return cnt;
3923}
3924
Steven Rostedta8259072009-02-26 22:19:12 -05003925static const struct file_operations trace_options_core_fops = {
3926 .open = tracing_open_generic,
3927 .read = trace_options_core_read,
3928 .write = trace_options_core_write,
3929};
3930
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003931struct dentry *trace_create_file(const char *name,
3932 mode_t mode,
3933 struct dentry *parent,
3934 void *data,
3935 const struct file_operations *fops)
3936{
3937 struct dentry *ret;
3938
3939 ret = debugfs_create_file(name, mode, parent, data, fops);
3940 if (!ret)
3941 pr_warning("Could not create debugfs '%s' entry\n", name);
3942
3943 return ret;
3944}
3945
3946
Steven Rostedta8259072009-02-26 22:19:12 -05003947static struct dentry *trace_options_init_dentry(void)
3948{
3949 struct dentry *d_tracer;
3950 static struct dentry *t_options;
3951
3952 if (t_options)
3953 return t_options;
3954
3955 d_tracer = tracing_init_dentry();
3956 if (!d_tracer)
3957 return NULL;
3958
3959 t_options = debugfs_create_dir("options", d_tracer);
3960 if (!t_options) {
3961 pr_warning("Could not create debugfs directory 'options'\n");
3962 return NULL;
3963 }
3964
3965 return t_options;
3966}
3967
Steven Rostedt577b7852009-02-26 23:43:05 -05003968static void
3969create_trace_option_file(struct trace_option_dentry *topt,
3970 struct tracer_flags *flags,
3971 struct tracer_opt *opt)
3972{
3973 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05003974
3975 t_options = trace_options_init_dentry();
3976 if (!t_options)
3977 return;
3978
3979 topt->flags = flags;
3980 topt->opt = opt;
3981
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003982 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05003983 &trace_options_fops);
3984
Steven Rostedt577b7852009-02-26 23:43:05 -05003985}
3986
3987static struct trace_option_dentry *
3988create_trace_option_files(struct tracer *tracer)
3989{
3990 struct trace_option_dentry *topts;
3991 struct tracer_flags *flags;
3992 struct tracer_opt *opts;
3993 int cnt;
3994
3995 if (!tracer)
3996 return NULL;
3997
3998 flags = tracer->flags;
3999
4000 if (!flags || !flags->opts)
4001 return NULL;
4002
4003 opts = flags->opts;
4004
4005 for (cnt = 0; opts[cnt].name; cnt++)
4006 ;
4007
Steven Rostedt0cfe8242009-02-27 10:51:10 -05004008 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05004009 if (!topts)
4010 return NULL;
4011
4012 for (cnt = 0; opts[cnt].name; cnt++)
4013 create_trace_option_file(&topts[cnt], flags,
4014 &opts[cnt]);
4015
4016 return topts;
4017}
4018
4019static void
4020destroy_trace_option_files(struct trace_option_dentry *topts)
4021{
4022 int cnt;
4023
4024 if (!topts)
4025 return;
4026
4027 for (cnt = 0; topts[cnt].opt; cnt++) {
4028 if (topts[cnt].entry)
4029 debugfs_remove(topts[cnt].entry);
4030 }
4031
4032 kfree(topts);
4033}
4034
Steven Rostedta8259072009-02-26 22:19:12 -05004035static struct dentry *
4036create_trace_option_core_file(const char *option, long index)
4037{
4038 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004039
4040 t_options = trace_options_init_dentry();
4041 if (!t_options)
4042 return NULL;
4043
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004044 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05004045 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004046}
4047
4048static __init void create_trace_options_dir(void)
4049{
4050 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05004051 int i;
4052
4053 t_options = trace_options_init_dentry();
4054 if (!t_options)
4055 return;
4056
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004057 for (i = 0; trace_options[i]; i++)
4058 create_trace_option_core_file(trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05004059}
4060
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004061static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004062{
4063 struct dentry *d_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004064 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004065
4066 d_tracer = tracing_init_dentry();
4067
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004068 trace_create_file("tracing_enabled", 0644, d_tracer,
4069 &global_trace, &tracing_ctrl_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004070
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004071 trace_create_file("trace_options", 0644, d_tracer,
4072 NULL, &tracing_iter_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004073
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004074 trace_create_file("tracing_cpumask", 0644, d_tracer,
4075 NULL, &tracing_cpumask_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05004076
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004077 trace_create_file("trace", 0644, d_tracer,
4078 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02004079
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004080 trace_create_file("available_tracers", 0444, d_tracer,
4081 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004082
Li Zefan339ae5d2009-04-17 10:34:30 +08004083 trace_create_file("current_tracer", 0644, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004084 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004085
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004086 trace_create_file("tracing_max_latency", 0644, d_tracer,
4087 &tracing_max_latency, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004088
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004089 trace_create_file("tracing_thresh", 0644, d_tracer,
4090 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004091
Li Zefan339ae5d2009-04-17 10:34:30 +08004092 trace_create_file("README", 0444, d_tracer,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004093 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02004094
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004095 trace_create_file("trace_pipe", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004096 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004097
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004098 trace_create_file("buffer_size_kb", 0644, d_tracer,
4099 &global_trace, &tracing_entries_fops);
Steven Rostedta98a3c32008-05-12 21:20:59 +02004100
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004101 trace_create_file("trace_marker", 0220, d_tracer,
4102 NULL, &tracing_mark_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03004103
Avadh Patel69abe6a2009-04-10 16:04:48 -04004104 trace_create_file("saved_cmdlines", 0444, d_tracer,
4105 NULL, &tracing_saved_cmdlines_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004106
4107#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004108 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4109 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004110#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02004111#ifdef CONFIG_SYSPROF_TRACER
4112 init_tracer_sysprof_debugfs(d_tracer);
4113#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004114
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004115 create_trace_options_dir();
4116
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004117 for_each_tracing_cpu(cpu)
4118 tracing_init_debugfs_percpu(cpu);
4119
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004120 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004121}
4122
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004123static int trace_panic_handler(struct notifier_block *this,
4124 unsigned long event, void *unused)
4125{
Steven Rostedt944ac422008-10-23 19:26:08 -04004126 if (ftrace_dump_on_oops)
4127 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004128 return NOTIFY_OK;
4129}
4130
4131static struct notifier_block trace_panic_notifier = {
4132 .notifier_call = trace_panic_handler,
4133 .next = NULL,
4134 .priority = 150 /* priority: INT_MAX >= x >= 0 */
4135};
4136
4137static int trace_die_handler(struct notifier_block *self,
4138 unsigned long val,
4139 void *data)
4140{
4141 switch (val) {
4142 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04004143 if (ftrace_dump_on_oops)
4144 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004145 break;
4146 default:
4147 break;
4148 }
4149 return NOTIFY_OK;
4150}
4151
4152static struct notifier_block trace_die_notifier = {
4153 .notifier_call = trace_die_handler,
4154 .priority = 200
4155};
4156
4157/*
4158 * printk is set to max of 1024, we really don't need it that big.
4159 * Nothing should be printing 1000 characters anyway.
4160 */
4161#define TRACE_MAX_PRINT 1000
4162
4163/*
4164 * Define here KERN_TRACE so that we have one place to modify
4165 * it if we decide to change what log level the ftrace dump
4166 * should be at.
4167 */
Steven Rostedt428aee12009-01-14 12:24:42 -05004168#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004169
4170static void
4171trace_printk_seq(struct trace_seq *s)
4172{
4173 /* Probably should print a warning here. */
4174 if (s->len >= 1000)
4175 s->len = 1000;
4176
4177 /* should be zero ended, but we are paranoid. */
4178 s->buffer[s->len] = 0;
4179
4180 printk(KERN_TRACE "%s", s->buffer);
4181
Steven Rostedtf9520752009-03-02 14:04:40 -05004182 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004183}
4184
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004185static void __ftrace_dump(bool disable_tracing)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004186{
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004187 static raw_spinlock_t ftrace_dump_lock =
4188 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004189 /* use static because iter can be a bit big for the stack */
4190 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004191 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004192 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004193 unsigned long flags;
4194 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004195
4196 /* only one dump */
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004197 local_irq_save(flags);
4198 __raw_spin_lock(&ftrace_dump_lock);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004199 if (dump_ran)
4200 goto out;
4201
4202 dump_ran = 1;
4203
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004204 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004205
4206 if (disable_tracing)
4207 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004208
Steven Rostedtd7690412008-10-01 00:29:53 -04004209 for_each_tracing_cpu(cpu) {
4210 atomic_inc(&global_trace.data[cpu]->disabled);
4211 }
4212
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004213 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4214
Török Edwinb54d3de2008-11-22 13:28:48 +02004215 /* don't look at user memory in panic mode */
4216 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4217
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004218 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4219
Steven Rostedte543ad72009-03-04 18:20:36 -05004220 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004221 iter.tr = &global_trace;
4222 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05004223 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004224
4225 /*
4226 * We need to stop all tracing on all CPUS to read the
4227 * the next buffer. This is a bit expensive, but is
4228 * not done often. We fill all what we can read,
4229 * and then release the locks again.
4230 */
4231
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004232 while (!trace_empty(&iter)) {
4233
4234 if (!cnt)
4235 printk(KERN_TRACE "---------------------------------\n");
4236
4237 cnt++;
4238
4239 /* reset all but tr, trace, and overruns */
4240 memset(&iter.seq, 0,
4241 sizeof(struct trace_iterator) -
4242 offsetof(struct trace_iterator, seq));
4243 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4244 iter.pos = -1;
4245
4246 if (find_next_entry_inc(&iter) != NULL) {
4247 print_trace_line(&iter);
4248 trace_consume(&iter);
4249 }
4250
4251 trace_printk_seq(&iter.seq);
4252 }
4253
4254 if (!cnt)
4255 printk(KERN_TRACE " (ftrace buffer empty)\n");
4256 else
4257 printk(KERN_TRACE "---------------------------------\n");
4258
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004259 /* Re-enable tracing if requested */
4260 if (!disable_tracing) {
4261 trace_flags |= old_userobj;
4262
4263 for_each_tracing_cpu(cpu) {
4264 atomic_dec(&global_trace.data[cpu]->disabled);
4265 }
4266 tracing_on();
4267 }
4268
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004269 out:
Steven Rostedtcd891ae2009-04-28 11:39:34 -04004270 __raw_spin_unlock(&ftrace_dump_lock);
4271 local_irq_restore(flags);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004272}
4273
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004274/* By default: disable tracing after the dump */
4275void ftrace_dump(void)
4276{
4277 __ftrace_dump(true);
4278}
4279
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004280__init static int tracer_alloc_buffers(void)
4281{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004282 struct trace_array_cpu *data;
Steven Rostedt73c51622009-03-11 13:42:01 -04004283 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004284 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304285 int ret = -ENOMEM;
4286
4287 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4288 goto out;
4289
4290 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4291 goto out_free_buffer_mask;
4292
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004293 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4294 goto out_free_tracing_cpumask;
4295
Steven Rostedt73c51622009-03-11 13:42:01 -04004296 /* To save memory, keep the ring buffer size to its minimum */
4297 if (ring_buffer_expanded)
4298 ring_buf_size = trace_buf_size;
4299 else
4300 ring_buf_size = 1;
4301
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304302 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4303 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004304 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004305
Steven Rostedtab464282008-05-12 21:21:00 +02004306 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04004307 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004308 TRACE_BUFFER_FLAGS);
4309 if (!global_trace.buffer) {
4310 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4311 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304312 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004313 }
4314 global_trace.entries = ring_buffer_size(global_trace.buffer);
4315
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304316
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004317#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt73c51622009-03-11 13:42:01 -04004318 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004319 TRACE_BUFFER_FLAGS);
4320 if (!max_tr.buffer) {
4321 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4322 WARN_ON(1);
4323 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304324 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004325 }
4326 max_tr.entries = ring_buffer_size(max_tr.buffer);
4327 WARN_ON(max_tr.entries != global_trace.entries);
4328#endif
4329
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004330 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004331 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004332 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004333 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004334 }
4335
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004336 trace_init_cmdlines();
4337
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004338 register_tracer(&nop_trace);
Steven Rostedt79fb0768f2009-02-02 21:38:33 -05004339 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004340#ifdef CONFIG_BOOT_TRACER
4341 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004342#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004343 /* All seems OK, enable tracing */
4344 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004345
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004346 atomic_notifier_chain_register(&panic_notifier_list,
4347 &trace_panic_notifier);
4348
4349 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01004350
4351 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004352
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304353out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004354 free_cpumask_var(tracing_reader_cpumask);
4355out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304356 free_cpumask_var(tracing_cpumask);
4357out_free_buffer_mask:
4358 free_cpumask_var(tracing_buffer_mask);
4359out:
4360 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004361}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004362
4363__init static int clear_boot_tracer(void)
4364{
4365 /*
4366 * The default tracer at boot buffer is an init section.
4367 * This function is called in lateinit. If we did not
4368 * find the boot tracer, then clear it out, to prevent
4369 * later registration from accessing the buffer that is
4370 * about to be freed.
4371 */
4372 if (!default_bootup_tracer)
4373 return 0;
4374
4375 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4376 default_bootup_tracer);
4377 default_bootup_tracer = NULL;
4378
4379 return 0;
4380}
4381
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004382early_initcall(tracer_alloc_buffers);
4383fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004384late_initcall(clear_boot_tracer);