blob: 0615751a3ed7a43f24c7e324562296070e12b54c [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
Steven Rostedt37886f62009-03-17 17:22:06 -0400174cycle_t ftrace_now(int cpu)
175{
176 u64 ts;
177
178 /* Early boot up does not have a buffer yet */
179 if (!global_trace.buffer)
180 return trace_clock_local();
181
182 ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
183 ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
184
185 return ts;
186}
187
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200188/*
189 * The max_tr is used to snapshot the global_trace when a maximum
190 * latency is reached. Some tracers will use this to store a maximum
191 * trace while it continues examining live traces.
192 *
193 * The buffers for the max_tr are set up the same as the global_trace.
194 * When a snapshot is taken, the link list of the max_tr is swapped
195 * with the link list of the global_trace and the buffers are reset for
196 * the global_trace so the tracing can continue.
197 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200198static struct trace_array max_tr;
199
200static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
201
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200202/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200203static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200204
Steven Rostedt90369902008-11-05 16:05:44 -0500205/**
206 * tracing_is_enabled - return tracer_enabled status
207 *
208 * This function is used by other tracers to know the status
209 * of the tracer_enabled flag. Tracers may use this function
210 * to know if it should enable their features when starting
211 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
212 */
213int tracing_is_enabled(void)
214{
215 return tracer_enabled;
216}
217
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200218/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400219 * trace_buf_size is the size in bytes that is allocated
220 * for a buffer. Note, the number of bytes is always rounded
221 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400222 *
223 * This number is purposely set to a low number of 16384.
224 * If the dump on oops happens, it will be much appreciated
225 * to not have to wait for all that output. Anyway this can be
226 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200227 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400228#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400229
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400230static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200231
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200232/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200233static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200234
235/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200236static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200237
238/*
239 * max_tracer_type_len is used to simplify the allocating of
240 * buffers to read userspace tracer names. We keep track of
241 * the longest tracer name registered.
242 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200243static int max_tracer_type_len;
244
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200245/*
246 * trace_types_lock is used to protect the trace_types list.
247 * This lock is also used to keep user access serialized.
248 * Accesses from userspace will grab this lock while userspace
249 * activities happen inside the kernel.
250 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200251static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200252
253/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200254static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
255
Steven Rostedtee6bce52008-11-12 17:52:37 -0500256/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500257unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Steven Rostedta2a16d62009-03-24 23:17:58 -0400258 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
259 TRACE_ITER_GRAPH_TIME;
Ingo Molnar4e655512008-05-12 21:20:52 +0200260
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200261/**
262 * trace_wake_up - wake up tasks waiting for trace input
263 *
264 * Simply wakes up any task that is blocked on the trace_wait
265 * queue. These is used with trace_poll for tasks polling the trace.
266 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200267void trace_wake_up(void)
268{
Ingo Molnar017730c2008-05-12 21:20:52 +0200269 /*
270 * The runqueue_is_locked() can fail, but this is the best we
271 * have for now:
272 */
273 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200274 wake_up(&trace_wait);
275}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200276
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400277static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200278{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400279 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200280 int ret;
281
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200282 if (!str)
283 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400284 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200285 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400286 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200287 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400288 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200289 return 1;
290}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400291__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200292
Steven Rostedt57f50be2008-05-12 21:20:44 +0200293unsigned long nsecs_to_usecs(unsigned long nsecs)
294{
295 return nsecs / 1000;
296}
297
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200298/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200299static const char *trace_options[] = {
300 "print-parent",
301 "sym-offset",
302 "sym-addr",
303 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200304 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200305 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200306 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200307 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200308 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200309 "sched-tree",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100310 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500311 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500312 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500313 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200314 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200315 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100316 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200317 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500318 "latency-format",
Steven Rostedtaf4617b2009-03-17 18:09:55 -0400319 "global-clock",
Steven Rostedtbe6f1642009-03-24 11:06:24 -0400320 "sleep-time",
Steven Rostedta2a16d62009-03-24 23:17:58 -0400321 "graph-time",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200322 NULL
323};
324
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200325/*
326 * ftrace_max_lock is used to protect the swapping of buffers
327 * when taking a max snapshot. The buffers themselves are
328 * protected by per_cpu spinlocks. But the action of the swap
329 * needs its own lock.
330 *
331 * This is defined as a raw_spinlock_t in order to help
332 * with performance when lockdep debugging is enabled.
333 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200334static raw_spinlock_t ftrace_max_lock =
335 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200336
337/*
338 * Copy the new maximum trace into the separate maximum-trace
339 * structure. (this way the maximum trace is permanently saved,
340 * for later retrieval via /debugfs/tracing/latency_trace)
341 */
Ingo Molnare309b412008-05-12 21:20:51 +0200342static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200343__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
344{
345 struct trace_array_cpu *data = tr->data[cpu];
346
347 max_tr.cpu = cpu;
348 max_tr.time_start = data->preempt_timestamp;
349
350 data = max_tr.data[cpu];
351 data->saved_latency = tracing_max_latency;
352
353 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
354 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100355 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200356 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
357 data->policy = tsk->policy;
358 data->rt_priority = tsk->rt_priority;
359
360 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500361 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200362}
363
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200364ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
365{
366 int len;
367 int ret;
368
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500369 if (!cnt)
370 return 0;
371
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200372 if (s->len <= s->readpos)
373 return -EBUSY;
374
375 len = s->len - s->readpos;
376 if (cnt > len)
377 cnt = len;
378 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500379 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200380 return -EFAULT;
381
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500382 cnt -= ret;
383
Steven Rostedte74da522009-03-04 20:31:11 -0500384 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200385 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200386}
387
Dmitri Vorobievb8b94262009-03-22 19:11:11 +0200388static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200389{
390 int len;
391 void *ret;
392
393 if (s->len <= s->readpos)
394 return -EBUSY;
395
396 len = s->len - s->readpos;
397 if (cnt > len)
398 cnt = len;
399 ret = memcpy(buf, s->buffer + s->readpos, cnt);
400 if (!ret)
401 return -EFAULT;
402
Steven Rostedte74da522009-03-04 20:31:11 -0500403 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200404 return cnt;
405}
406
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200407/**
408 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
409 * @tr: tracer
410 * @tsk: the task with the latency
411 * @cpu: The cpu that initiated the trace.
412 *
413 * Flip the buffers between the @tr and the max_tr and record information
414 * about which task was the cause of this latency.
415 */
Ingo Molnare309b412008-05-12 21:20:51 +0200416void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200417update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
418{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400419 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200420
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200421 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200422 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400423
424 tr->buffer = max_tr.buffer;
425 max_tr.buffer = buf;
426
Steven Rostedtd7690412008-10-01 00:29:53 -0400427 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400428 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400429 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200430
431 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200432 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200433}
434
435/**
436 * update_max_tr_single - only copy one trace over, and reset the rest
437 * @tr - tracer
438 * @tsk - task with the latency
439 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200440 *
441 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200442 */
Ingo Molnare309b412008-05-12 21:20:51 +0200443void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200444update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
445{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400446 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200447
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200448 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200449 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200450
Steven Rostedtd7690412008-10-01 00:29:53 -0400451 ftrace_disable_cpu();
452
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400453 ring_buffer_reset(max_tr.buffer);
454 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
455
Steven Rostedtd7690412008-10-01 00:29:53 -0400456 ftrace_enable_cpu();
457
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500458 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200459
460 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200461 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200462}
463
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200464/**
465 * register_tracer - register a tracer with the ftrace system.
466 * @type - the plugin for the tracer
467 *
468 * Register a new plugin tracer.
469 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200470int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100471__releases(kernel_lock)
472__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200473{
474 struct tracer *t;
475 int len;
476 int ret = 0;
477
478 if (!type->name) {
479 pr_info("Tracer must have a name\n");
480 return -1;
481 }
482
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100483 /*
484 * When this gets called we hold the BKL which means that
485 * preemption is disabled. Various trace selftests however
486 * need to disable and enable preemption for successful tests.
487 * So we drop the BKL here and grab it after the tests again.
488 */
489 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200490 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100491
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100492 tracing_selftest_running = true;
493
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200494 for (t = trace_types; t; t = t->next) {
495 if (strcmp(type->name, t->name) == 0) {
496 /* already found */
497 pr_info("Trace %s already registered\n",
498 type->name);
499 ret = -1;
500 goto out;
501 }
502 }
503
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100504 if (!type->set_flag)
505 type->set_flag = &dummy_set_flag;
506 if (!type->flags)
507 type->flags = &dummy_tracer_flags;
508 else
509 if (!type->flags->opts)
510 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100511 if (!type->wait_pipe)
512 type->wait_pipe = default_wait_pipe;
513
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100514
Steven Rostedt60a11772008-05-12 21:20:44 +0200515#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500516 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200517 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200518 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200519 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100520
Steven Rostedt60a11772008-05-12 21:20:44 +0200521 /*
522 * Run a selftest on this tracer.
523 * Here we reset the trace buffer, and set the current
524 * tracer to be this tracer. The tracer can then run some
525 * internal tracing to verify that everything is in order.
526 * If we fail, we do not register this tracer.
527 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100528 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400529 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100530
Steven Rostedt60a11772008-05-12 21:20:44 +0200531 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200532 /* the test is responsible for initializing and enabling */
533 pr_info("Testing tracer %s: ", type->name);
534 ret = type->selftest(type, tr);
535 /* the test is responsible for resetting too */
536 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200537 if (ret) {
538 printk(KERN_CONT "FAILED!\n");
539 goto out;
540 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200541 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100542 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400543 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100544
Steven Rostedt60a11772008-05-12 21:20:44 +0200545 printk(KERN_CONT "PASSED\n");
546 }
547#endif
548
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200549 type->next = trace_types;
550 trace_types = type;
551 len = strlen(type->name);
552 if (len > max_tracer_type_len)
553 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200554
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200555 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100556 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200557 mutex_unlock(&trace_types_lock);
558
Steven Rostedtdac74942009-02-05 01:13:38 -0500559 if (ret || !default_bootup_tracer)
560 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500561
Steven Rostedtdac74942009-02-05 01:13:38 -0500562 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
563 goto out_unlock;
564
565 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
566 /* Do we want this tracer to start on bootup? */
567 tracing_set_tracer(type->name);
568 default_bootup_tracer = NULL;
569 /* disable other selftests, since this will break it. */
570 tracing_selftest_disabled = 1;
571#ifdef CONFIG_FTRACE_STARTUP_TEST
572 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
573 type->name);
574#endif
575
576 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500577 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200578 return ret;
579}
580
581void unregister_tracer(struct tracer *type)
582{
583 struct tracer **t;
584 int len;
585
586 mutex_lock(&trace_types_lock);
587 for (t = &trace_types; *t; t = &(*t)->next) {
588 if (*t == type)
589 goto found;
590 }
591 pr_info("Trace %s not registered\n", type->name);
592 goto out;
593
594 found:
595 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200596
597 if (type == current_trace && tracer_enabled) {
598 tracer_enabled = 0;
599 tracing_stop();
600 if (current_trace->stop)
601 current_trace->stop(&global_trace);
602 current_trace = &nop_trace;
603 }
604
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200605 if (strlen(type->name) != max_tracer_type_len)
606 goto out;
607
608 max_tracer_type_len = 0;
609 for (t = &trace_types; *t; t = &(*t)->next) {
610 len = strlen((*t)->name);
611 if (len > max_tracer_type_len)
612 max_tracer_type_len = len;
613 }
614 out:
615 mutex_unlock(&trace_types_lock);
616}
617
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400618void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200619{
Steven Rostedtd7690412008-10-01 00:29:53 -0400620 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400621 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400622 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200623}
624
Pekka J Enberg213cc062008-12-19 12:08:39 +0200625void tracing_reset_online_cpus(struct trace_array *tr)
626{
627 int cpu;
628
629 tr->time_start = ftrace_now(tr->cpu);
630
631 for_each_online_cpu(cpu)
632 tracing_reset(tr, cpu);
633}
634
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200635#define SAVED_CMDLINES 128
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100636#define NO_CMDLINE_MAP UINT_MAX
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200637static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
638static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
639static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
640static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100641static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200642
Steven Rostedt25b0b442008-05-12 21:21:00 +0200643/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100644static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200645
646static void trace_init_cmdlines(void)
647{
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100648 memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
649 memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200650 cmdline_idx = 0;
651}
652
Steven Rostedt0f048702008-11-05 16:05:44 -0500653static int trace_stop_count;
654static DEFINE_SPINLOCK(tracing_start_lock);
655
656/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500657 * ftrace_off_permanent - disable all ftrace code permanently
658 *
659 * This should only be called when a serious anomally has
660 * been detected. This will turn off the function tracing,
661 * ring buffers, and other tracing utilites. It takes no
662 * locks and can be called from any context.
663 */
664void ftrace_off_permanent(void)
665{
666 tracing_disabled = 1;
667 ftrace_stop();
668 tracing_off_permanent();
669}
670
671/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500672 * tracing_start - quick start of the tracer
673 *
674 * If tracing is enabled but was stopped by tracing_stop,
675 * this will start the tracer back up.
676 */
677void tracing_start(void)
678{
679 struct ring_buffer *buffer;
680 unsigned long flags;
681
682 if (tracing_disabled)
683 return;
684
685 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500686 if (--trace_stop_count) {
687 if (trace_stop_count < 0) {
688 /* Someone screwed up their debugging */
689 WARN_ON_ONCE(1);
690 trace_stop_count = 0;
691 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500692 goto out;
693 }
694
695
696 buffer = global_trace.buffer;
697 if (buffer)
698 ring_buffer_record_enable(buffer);
699
700 buffer = max_tr.buffer;
701 if (buffer)
702 ring_buffer_record_enable(buffer);
703
704 ftrace_start();
705 out:
706 spin_unlock_irqrestore(&tracing_start_lock, flags);
707}
708
709/**
710 * tracing_stop - quick stop of the tracer
711 *
712 * Light weight way to stop tracing. Use in conjunction with
713 * tracing_start.
714 */
715void tracing_stop(void)
716{
717 struct ring_buffer *buffer;
718 unsigned long flags;
719
720 ftrace_stop();
721 spin_lock_irqsave(&tracing_start_lock, flags);
722 if (trace_stop_count++)
723 goto out;
724
725 buffer = global_trace.buffer;
726 if (buffer)
727 ring_buffer_record_disable(buffer);
728
729 buffer = max_tr.buffer;
730 if (buffer)
731 ring_buffer_record_disable(buffer);
732
733 out:
734 spin_unlock_irqrestore(&tracing_start_lock, flags);
735}
736
Ingo Molnare309b412008-05-12 21:20:51 +0200737void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200738
Ingo Molnare309b412008-05-12 21:20:51 +0200739static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200740{
Carsten Emdea635cf02009-03-18 09:00:41 +0100741 unsigned pid, idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200742
743 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
744 return;
745
746 /*
747 * It's not the end of the world if we don't get
748 * the lock, but we also don't want to spin
749 * nor do we want to disable interrupts,
750 * so if we miss here, then better luck next time.
751 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100752 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200753 return;
754
755 idx = map_pid_to_cmdline[tsk->pid];
Thomas Gleixner2c7eea42009-03-18 09:03:19 +0100756 if (idx == NO_CMDLINE_MAP) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200757 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
758
Carsten Emdea635cf02009-03-18 09:00:41 +0100759 /*
760 * Check whether the cmdline buffer at idx has a pid
761 * mapped. We are going to overwrite that entry so we
762 * need to clear the map_pid_to_cmdline. Otherwise we
763 * would read the new comm for the old pid.
764 */
765 pid = map_cmdline_to_pid[idx];
766 if (pid != NO_CMDLINE_MAP)
767 map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200768
Carsten Emdea635cf02009-03-18 09:00:41 +0100769 map_cmdline_to_pid[idx] = tsk->pid;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200770 map_pid_to_cmdline[tsk->pid] = idx;
771
772 cmdline_idx = idx;
773 }
774
775 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
776
Peter Zijlstraefed7922009-03-04 12:32:55 +0100777 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200778}
779
Steven Rostedt4ca53082009-03-16 19:20:15 -0400780void trace_find_cmdline(int pid, char comm[])
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200782 unsigned map;
783
Steven Rostedt4ca53082009-03-16 19:20:15 -0400784 if (!pid) {
785 strcpy(comm, "<idle>");
786 return;
787 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200788
Steven Rostedt4ca53082009-03-16 19:20:15 -0400789 if (pid > PID_MAX_DEFAULT) {
790 strcpy(comm, "<...>");
791 return;
792 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793
Steven Rostedt4ca53082009-03-16 19:20:15 -0400794 __raw_spin_lock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200795 map = map_pid_to_cmdline[pid];
Thomas Gleixner50d88752009-03-18 08:58:44 +0100796 if (map != NO_CMDLINE_MAP)
797 strcpy(comm, saved_cmdlines[map]);
798 else
799 strcpy(comm, "<...>");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200800
Steven Rostedt4ca53082009-03-16 19:20:15 -0400801 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200802}
803
Ingo Molnare309b412008-05-12 21:20:51 +0200804void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200805{
Thomas Gleixner18aecd32009-03-18 08:56:58 +0100806 if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
807 !tracing_is_on())
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808 return;
809
810 trace_save_cmdline(tsk);
811}
812
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300813void
Steven Rostedt38697052008-10-01 13:14:09 -0400814tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
815 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200816{
817 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818
Steven Rostedt777e2082008-09-29 23:02:42 -0400819 entry->preempt_count = pc & 0xff;
820 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedtef180122009-03-10 14:10:56 -0400821 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400822 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400823#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400824 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400825#else
826 TRACE_FLAG_IRQS_NOSUPPORT |
827#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200828 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
829 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
830 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
831}
832
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200833struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
834 unsigned char type,
835 unsigned long len,
836 unsigned long flags, int pc)
837{
838 struct ring_buffer_event *event;
839
840 event = ring_buffer_lock_reserve(tr->buffer, len);
841 if (event != NULL) {
842 struct trace_entry *ent = ring_buffer_event_data(event);
843
844 tracing_generic_entry_update(ent, flags, pc);
845 ent->type = type;
846 }
847
848 return event;
849}
850static void ftrace_trace_stack(struct trace_array *tr,
851 unsigned long flags, int skip, int pc);
852static void ftrace_trace_userstack(struct trace_array *tr,
853 unsigned long flags, int pc);
854
Frederic Weisbecker07edf712009-03-22 23:10:46 +0100855static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
856 struct ring_buffer_event *event,
857 unsigned long flags, int pc,
858 int wake)
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200859{
860 ring_buffer_unlock_commit(tr->buffer, event);
861
862 ftrace_trace_stack(tr, flags, 6, pc);
863 ftrace_trace_userstack(tr, flags, pc);
Frederic Weisbecker07edf712009-03-22 23:10:46 +0100864
865 if (wake)
866 trace_wake_up();
867}
868
869void trace_buffer_unlock_commit(struct trace_array *tr,
870 struct ring_buffer_event *event,
871 unsigned long flags, int pc)
872{
873 __trace_buffer_unlock_commit(tr, event, flags, pc, 1);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200874}
875
Steven Rostedtef5580d2009-02-27 19:38:04 -0500876struct ring_buffer_event *
877trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
878 unsigned long flags, int pc)
879{
880 return trace_buffer_lock_reserve(&global_trace,
881 type, len, flags, pc);
882}
883
884void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
885 unsigned long flags, int pc)
886{
Frederic Weisbecker07edf712009-03-22 23:10:46 +0100887 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
888}
889
890void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
891 unsigned long flags, int pc)
892{
893 return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
Steven Rostedtef5580d2009-02-27 19:38:04 -0500894}
895
Ingo Molnare309b412008-05-12 21:20:51 +0200896void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500897trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400898 unsigned long ip, unsigned long parent_ip, unsigned long flags,
899 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200900{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400901 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400902 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200903
Steven Rostedtd7690412008-10-01 00:29:53 -0400904 /* If we are reading the ring buffer, don't trace */
905 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
906 return;
907
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200908 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
909 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400910 if (!event)
911 return;
912 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400913 entry->ip = ip;
914 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200915 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200916}
917
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100918#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker16185362009-03-23 22:17:01 +0100919static int __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100920 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100921 unsigned long flags,
922 int pc)
923{
924 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100925 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100926
927 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
Frederic Weisbecker16185362009-03-23 22:17:01 +0100928 return 0;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100929
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200930 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
931 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100932 if (!event)
Frederic Weisbecker16185362009-03-23 22:17:01 +0100933 return 0;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100934 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100935 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200936 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker16185362009-03-23 22:17:01 +0100937
938 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100939}
940
941static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100942 struct ftrace_graph_ret *trace,
943 unsigned long flags,
944 int pc)
945{
946 struct ring_buffer_event *event;
947 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100948
949 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
950 return;
951
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200952 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
953 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100954 if (!event)
955 return;
956 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100957 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200958 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100959}
960#endif
961
Ingo Molnare309b412008-05-12 21:20:51 +0200962void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200963ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400964 unsigned long ip, unsigned long parent_ip, unsigned long flags,
965 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200966{
967 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500968 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200969}
970
Steven Rostedt53614992009-01-15 19:12:40 -0500971static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500972 unsigned long flags,
973 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200974{
Al Viroc2c80522008-10-31 19:50:41 +0000975#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400976 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400977 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200978 struct stack_trace trace;
979
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200980 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
981 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400982 if (!event)
983 return;
984 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400985 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200986
987 trace.nr_entries = 0;
988 trace.max_entries = FTRACE_STACK_ENTRIES;
989 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400990 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200991
992 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200993 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000994#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200995}
996
Steven Rostedt53614992009-01-15 19:12:40 -0500997static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500998 unsigned long flags,
999 int skip, int pc)
1000{
1001 if (!(trace_flags & TRACE_ITER_STACKTRACE))
1002 return;
1003
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001004 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -05001005}
1006
Steven Rostedt38697052008-10-01 13:14:09 -04001007void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001008 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -05001009 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -04001010{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001011 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -04001012}
1013
Török Edwin02b67512008-11-22 13:28:47 +02001014static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001015 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +02001016{
Török Edwinc7425ac2008-11-28 11:17:56 +02001017#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +02001018 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +02001019 struct userstack_entry *entry;
1020 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +02001021
1022 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1023 return;
1024
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001025 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1026 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +02001027 if (!event)
1028 return;
1029 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +02001030
1031 memset(&entry->caller, 0, sizeof(entry->caller));
1032
1033 trace.nr_entries = 0;
1034 trace.max_entries = FTRACE_STACK_ENTRIES;
1035 trace.skip = 0;
1036 trace.entries = entry->caller;
1037
1038 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001039 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001040#endif
Török Edwin02b67512008-11-22 13:28:47 +02001041}
1042
Hannes Eder4fd27352009-02-10 19:44:12 +01001043#ifdef UNUSED
1044static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001045{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001046 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001047}
Hannes Eder4fd27352009-02-10 19:44:12 +01001048#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001049
Steven Rostedt38697052008-10-01 13:14:09 -04001050static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001051ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001052 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1053 int pc)
Ingo Molnara4feb832008-05-12 21:21:02 +02001054{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001055 struct ring_buffer_event *event;
Ingo Molnara4feb832008-05-12 21:21:02 +02001056 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001057 struct special_entry *entry;
Ingo Molnara4feb832008-05-12 21:21:02 +02001058
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001059 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1060 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001061 if (!event)
1062 return;
1063 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001064 entry->arg1 = arg1;
1065 entry->arg2 = arg2;
1066 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001067 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb832008-05-12 21:21:02 +02001068}
1069
1070void
Steven Rostedt38697052008-10-01 13:14:09 -04001071__trace_special(void *__tr, void *__data,
1072 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1073{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001074 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001075}
1076
1077void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001078tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001079 struct task_struct *prev,
1080 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001081 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001082{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001083 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001084 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001085
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001086 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1087 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001088 if (!event)
1089 return;
1090 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001091 entry->prev_pid = prev->pid;
1092 entry->prev_prio = prev->prio;
1093 entry->prev_state = prev->state;
1094 entry->next_pid = next->pid;
1095 entry->next_prio = next->prio;
1096 entry->next_state = next->state;
1097 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001098 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001099}
1100
Ingo Molnar57422792008-05-12 21:20:51 +02001101void
1102tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001103 struct task_struct *wakee,
1104 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001105 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001106{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001107 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001108 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001109
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001110 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1111 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001112 if (!event)
1113 return;
1114 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001115 entry->prev_pid = curr->pid;
1116 entry->prev_prio = curr->prio;
1117 entry->prev_state = curr->state;
1118 entry->next_pid = wakee->pid;
1119 entry->next_prio = wakee->prio;
1120 entry->next_state = wakee->state;
1121 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001122
1123 ring_buffer_unlock_commit(tr->buffer, event);
1124 ftrace_trace_stack(tr, flags, 6, pc);
1125 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001126}
1127
Steven Rostedt4902f882008-05-22 00:22:18 -04001128void
1129ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1130{
1131 struct trace_array *tr = &global_trace;
1132 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001133 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001134 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001135 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001136
Steven Rostedtc76f0692008-11-07 22:36:02 -05001137 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001138 return;
1139
Steven Rostedt38697052008-10-01 13:14:09 -04001140 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001141 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001142 cpu = raw_smp_processor_id();
1143 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001144
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001145 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001146 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001147
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001148 atomic_dec(&data->disabled);
1149 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001150}
1151
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001152#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001153int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001154{
1155 struct trace_array *tr = &global_trace;
1156 struct trace_array_cpu *data;
1157 unsigned long flags;
1158 long disabled;
Frederic Weisbecker16185362009-03-23 22:17:01 +01001159 int ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001160 int cpu;
1161 int pc;
1162
Steven Rostedt804a6852008-12-03 15:36:59 -05001163 if (!ftrace_trace_task(current))
1164 return 0;
1165
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001166 if (!ftrace_graph_addr(trace->func))
1167 return 0;
1168
Steven Rostedta5e25882008-12-02 15:34:05 -05001169 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001170 cpu = raw_smp_processor_id();
1171 data = tr->data[cpu];
1172 disabled = atomic_inc_return(&data->disabled);
1173 if (likely(disabled == 1)) {
1174 pc = preempt_count();
Frederic Weisbecker16185362009-03-23 22:17:01 +01001175 ret = __trace_graph_entry(tr, trace, flags, pc);
1176 } else {
1177 ret = 0;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001178 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001179 /* Only do the atomic if it is not already set */
1180 if (!test_tsk_trace_graph(current))
1181 set_tsk_trace_graph(current);
Frederic Weisbecker16185362009-03-23 22:17:01 +01001182
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001183 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001184 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001185
Frederic Weisbecker16185362009-03-23 22:17:01 +01001186 return ret;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001187}
1188
1189void trace_graph_return(struct ftrace_graph_ret *trace)
1190{
1191 struct trace_array *tr = &global_trace;
1192 struct trace_array_cpu *data;
1193 unsigned long flags;
1194 long disabled;
1195 int cpu;
1196 int pc;
1197
Steven Rostedta5e25882008-12-02 15:34:05 -05001198 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001199 cpu = raw_smp_processor_id();
1200 data = tr->data[cpu];
1201 disabled = atomic_inc_return(&data->disabled);
1202 if (likely(disabled == 1)) {
1203 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001204 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001205 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001206 if (!trace->depth)
1207 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001208 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001209 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001210}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001211#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001212
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001213
1214/**
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001215 * trace_vbprintk - write binary msg to tracing buffer
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001216 *
1217 */
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001218int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001219{
Steven Rostedt80370cb2009-03-10 17:16:35 -04001220 static raw_spinlock_t trace_buf_lock =
1221 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001222 static u32 trace_buf[TRACE_BUF_SIZE];
1223
1224 struct ring_buffer_event *event;
1225 struct trace_array *tr = &global_trace;
1226 struct trace_array_cpu *data;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001227 struct bprint_entry *entry;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001228 unsigned long flags;
1229 int resched;
1230 int cpu, len = 0, size, pc;
1231
1232 if (unlikely(tracing_selftest_running || tracing_disabled))
1233 return 0;
1234
1235 /* Don't pollute graph traces with trace_vprintk internals */
1236 pause_graph_tracing();
1237
1238 pc = preempt_count();
1239 resched = ftrace_preempt_disable();
1240 cpu = raw_smp_processor_id();
1241 data = tr->data[cpu];
1242
1243 if (unlikely(atomic_read(&data->disabled)))
1244 goto out;
1245
Steven Rostedt80370cb2009-03-10 17:16:35 -04001246 /* Lockdep uses trace_printk for lock tracing */
1247 local_irq_save(flags);
1248 __raw_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001249 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1250
1251 if (len > TRACE_BUF_SIZE || len < 0)
1252 goto out_unlock;
1253
1254 size = sizeof(*entry) + sizeof(u32) * len;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001255 event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001256 if (!event)
1257 goto out_unlock;
1258 entry = ring_buffer_event_data(event);
1259 entry->ip = ip;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001260 entry->fmt = fmt;
1261
1262 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1263 ring_buffer_unlock_commit(tr->buffer, event);
1264
1265out_unlock:
Steven Rostedt80370cb2009-03-10 17:16:35 -04001266 __raw_spin_unlock(&trace_buf_lock);
1267 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001268
1269out:
1270 ftrace_preempt_enable(resched);
1271 unpause_graph_tracing();
1272
1273 return len;
1274}
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001275EXPORT_SYMBOL_GPL(trace_vbprintk);
1276
Steven Rostedt40ce74f2009-03-19 14:03:53 -04001277int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001278{
1279 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1280 static char trace_buf[TRACE_BUF_SIZE];
1281
1282 struct ring_buffer_event *event;
1283 struct trace_array *tr = &global_trace;
1284 struct trace_array_cpu *data;
1285 int cpu, len = 0, size, pc;
1286 struct print_entry *entry;
1287 unsigned long irq_flags;
1288
1289 if (tracing_disabled || tracing_selftest_running)
1290 return 0;
1291
1292 pc = preempt_count();
1293 preempt_disable_notrace();
1294 cpu = raw_smp_processor_id();
1295 data = tr->data[cpu];
1296
1297 if (unlikely(atomic_read(&data->disabled)))
1298 goto out;
1299
1300 pause_graph_tracing();
1301 raw_local_irq_save(irq_flags);
1302 __raw_spin_lock(&trace_buf_lock);
1303 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1304
1305 len = min(len, TRACE_BUF_SIZE-1);
1306 trace_buf[len] = 0;
1307
1308 size = sizeof(*entry) + len + 1;
1309 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1310 if (!event)
1311 goto out_unlock;
1312 entry = ring_buffer_event_data(event);
1313 entry->ip = ip;
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001314
1315 memcpy(&entry->buf, trace_buf, len);
1316 entry->buf[len] = 0;
1317 ring_buffer_unlock_commit(tr->buffer, event);
1318
1319 out_unlock:
1320 __raw_spin_unlock(&trace_buf_lock);
1321 raw_local_irq_restore(irq_flags);
1322 unpause_graph_tracing();
1323 out:
1324 preempt_enable_notrace();
1325
1326 return len;
1327}
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001328EXPORT_SYMBOL_GPL(trace_vprintk);
1329
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001330enum trace_file_type {
1331 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001332 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001333};
1334
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001335static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001336{
Steven Rostedtd7690412008-10-01 00:29:53 -04001337 /* Don't allow ftrace to trace into the ring buffers */
1338 ftrace_disable_cpu();
1339
Steven Rostedt5a90f572008-09-03 17:42:51 -04001340 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001341 if (iter->buffer_iter[iter->cpu])
1342 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1343
1344 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001345}
1346
Ingo Molnare309b412008-05-12 21:20:51 +02001347static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001348peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001349{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001350 struct ring_buffer_event *event;
1351 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001352
Steven Rostedtd7690412008-10-01 00:29:53 -04001353 /* Don't allow ftrace to trace into the ring buffers */
1354 ftrace_disable_cpu();
1355
1356 if (buf_iter)
1357 event = ring_buffer_iter_peek(buf_iter, ts);
1358 else
1359 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1360
1361 ftrace_enable_cpu();
1362
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001363 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001364}
Steven Rostedtd7690412008-10-01 00:29:53 -04001365
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001366static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001367__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001368{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001369 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001371 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001372 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373 int next_cpu = -1;
1374 int cpu;
1375
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001376 /*
1377 * If we are in a per_cpu trace file, don't bother by iterating over
1378 * all cpu and peek directly.
1379 */
1380 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1381 if (ring_buffer_empty_cpu(buffer, cpu_file))
1382 return NULL;
1383 ent = peek_next_entry(iter, cpu_file, ent_ts);
1384 if (ent_cpu)
1385 *ent_cpu = cpu_file;
1386
1387 return ent;
1388 }
1389
Steven Rostedtab464282008-05-12 21:21:00 +02001390 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001391
1392 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001393 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001394
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001395 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001396
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001397 /*
1398 * Pick the entry with the smallest timestamp:
1399 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001400 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001401 next = ent;
1402 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001403 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001404 }
1405 }
1406
1407 if (ent_cpu)
1408 *ent_cpu = next_cpu;
1409
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001410 if (ent_ts)
1411 *ent_ts = next_ts;
1412
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001413 return next;
1414}
1415
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001416/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001417struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1418 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001419{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001420 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001421}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001422
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001423/* Find the next real entry, and increment the iterator to the next entry */
1424static void *find_next_entry_inc(struct trace_iterator *iter)
1425{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001426 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001427
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001428 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001429 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001430
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001431 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001432}
1433
Ingo Molnare309b412008-05-12 21:20:51 +02001434static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001435{
Steven Rostedtd7690412008-10-01 00:29:53 -04001436 /* Don't allow ftrace to trace into the ring buffers */
1437 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001438 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001439 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001440}
1441
Ingo Molnare309b412008-05-12 21:20:51 +02001442static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001443{
1444 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001445 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001446 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001447
1448 (*pos)++;
1449
1450 /* can't go backwards */
1451 if (iter->idx > i)
1452 return NULL;
1453
1454 if (iter->idx < 0)
1455 ent = find_next_entry_inc(iter);
1456 else
1457 ent = iter;
1458
1459 while (ent && iter->idx < i)
1460 ent = find_next_entry_inc(iter);
1461
1462 iter->pos = *pos;
1463
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001464 return ent;
1465}
1466
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001467/*
1468 * No necessary locking here. The worst thing which can
1469 * happen is loosing events consumed at the same time
1470 * by a trace_pipe reader.
1471 * Other than that, we don't risk to crash the ring buffer
1472 * because it serializes the readers.
1473 *
1474 * The current tracer is copied to avoid a global locking
1475 * all around.
1476 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477static void *s_start(struct seq_file *m, loff_t *pos)
1478{
1479 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001480 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001481 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001482 void *p = NULL;
1483 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001484 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001485
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001486 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001487 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001488 if (unlikely(old_tracer != current_trace && current_trace)) {
1489 old_tracer = current_trace;
1490 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001491 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001492 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001493
1494 atomic_inc(&trace_record_cmdline_disabled);
1495
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001496 if (*pos != iter->pos) {
1497 iter->ent = NULL;
1498 iter->cpu = 0;
1499 iter->idx = -1;
1500
Steven Rostedtd7690412008-10-01 00:29:53 -04001501 ftrace_disable_cpu();
1502
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001503 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1504 for_each_tracing_cpu(cpu)
1505 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1506 } else
1507 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1508
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001509
Steven Rostedtd7690412008-10-01 00:29:53 -04001510 ftrace_enable_cpu();
1511
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001512 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1513 ;
1514
1515 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001516 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001517 p = s_next(m, p, &l);
1518 }
1519
1520 return p;
1521}
1522
1523static void s_stop(struct seq_file *m, void *p)
1524{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001525 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001526}
1527
Ingo Molnare309b412008-05-12 21:20:51 +02001528static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001529{
Michael Ellermana6168352008-08-20 16:36:11 -07001530 seq_puts(m, "# _------=> CPU# \n");
1531 seq_puts(m, "# / _-----=> irqs-off \n");
1532 seq_puts(m, "# | / _----=> need-resched \n");
1533 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1534 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1535 seq_puts(m, "# |||| / \n");
1536 seq_puts(m, "# ||||| delay \n");
1537 seq_puts(m, "# cmd pid ||||| time | caller \n");
1538 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001539}
1540
Ingo Molnare309b412008-05-12 21:20:51 +02001541static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542{
Michael Ellermana6168352008-08-20 16:36:11 -07001543 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1544 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001545}
1546
1547
Ingo Molnare309b412008-05-12 21:20:51 +02001548static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001549print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1550{
1551 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1552 struct trace_array *tr = iter->tr;
1553 struct trace_array_cpu *data = tr->data[tr->cpu];
1554 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001555 unsigned long total;
1556 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001557 const char *name = "preemption";
1558
1559 if (type)
1560 name = type->name;
1561
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001562 entries = ring_buffer_entries(iter->tr->buffer);
1563 total = entries +
1564 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001565
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001566 seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001567 name, UTS_RELEASE);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001568 seq_puts(m, "# -----------------------------------"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001569 "---------------------------------\n");
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001570 seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001571 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001572 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001573 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001574 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001575 tr->cpu,
1576#if defined(CONFIG_PREEMPT_NONE)
1577 "server",
1578#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1579 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001580#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001581 "preempt",
1582#else
1583 "unknown",
1584#endif
1585 /* These are reserved for later use */
1586 0, 0, 0, 0);
1587#ifdef CONFIG_SMP
1588 seq_printf(m, " #P:%d)\n", num_online_cpus());
1589#else
1590 seq_puts(m, ")\n");
1591#endif
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001592 seq_puts(m, "# -----------------\n");
1593 seq_printf(m, "# | task: %.16s-%d "
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001594 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1595 data->comm, data->pid, data->uid, data->nice,
1596 data->policy, data->rt_priority);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001597 seq_puts(m, "# -----------------\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001598
1599 if (data->critical_start) {
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001600 seq_puts(m, "# => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001601 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1602 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001603 seq_puts(m, "\n# => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001604 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1605 trace_print_seq(m, &iter->seq);
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001606 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001607 }
1608
KOSAKI Motohiro888b55d2009-03-08 13:12:43 +09001609 seq_puts(m, "#\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001610}
1611
Steven Rostedta3097202008-11-07 22:36:02 -05001612static void test_cpu_buff_start(struct trace_iterator *iter)
1613{
1614 struct trace_seq *s = &iter->seq;
1615
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001616 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1617 return;
1618
1619 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1620 return;
1621
Rusty Russell44623442009-01-01 10:12:23 +10301622 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001623 return;
1624
Rusty Russell44623442009-01-01 10:12:23 +10301625 cpumask_set_cpu(iter->cpu, iter->started);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001626
1627 /* Don't print started cpu buffer for the first entry of the trace */
1628 if (iter->idx > 1)
1629 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1630 iter->cpu);
Steven Rostedta3097202008-11-07 22:36:02 -05001631}
1632
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001633static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001634{
Steven Rostedt214023c2008-05-12 21:20:46 +02001635 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001636 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001637 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001638 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001639
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001640 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001641
Steven Rostedta3097202008-11-07 22:36:02 -05001642 test_cpu_buff_start(iter);
1643
Steven Rostedtf633cef2008-12-23 23:24:13 -05001644 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001645
1646 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001647 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1648 if (!trace_print_lat_context(iter))
1649 goto partial;
1650 } else {
1651 if (!trace_print_context(iter))
1652 goto partial;
1653 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001654 }
1655
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001656 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001657 return event->trace(iter, sym_flags);
1658
1659 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1660 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001661
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001662 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001663partial:
1664 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001665}
1666
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001667static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001668{
1669 struct trace_seq *s = &iter->seq;
1670 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001671 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001672
1673 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001674
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001675 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001676 if (!trace_seq_printf(s, "%d %d %llu ",
1677 entry->pid, iter->cpu, iter->ts))
1678 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001679 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001680
Steven Rostedtf633cef2008-12-23 23:24:13 -05001681 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001682 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001683 return event->raw(iter, 0);
1684
1685 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1686 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001687
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001688 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001689partial:
1690 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001691}
1692
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001693static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001694{
1695 struct trace_seq *s = &iter->seq;
1696 unsigned char newline = '\n';
1697 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001698 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001699
1700 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001701
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001702 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1703 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1704 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1705 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1706 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001707
Steven Rostedtf633cef2008-12-23 23:24:13 -05001708 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001709 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001710 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001711 if (ret != TRACE_TYPE_HANDLED)
1712 return ret;
1713 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001714
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001715 SEQ_PUT_FIELD_RET(s, newline);
1716
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001717 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001718}
1719
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001720static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001721{
1722 struct trace_seq *s = &iter->seq;
1723 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001724 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001725
1726 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001727
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001728 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1729 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001730 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001731 SEQ_PUT_FIELD_RET(s, iter->ts);
1732 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001733
Steven Rostedtf633cef2008-12-23 23:24:13 -05001734 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001735 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001736}
1737
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001738static int trace_empty(struct trace_iterator *iter)
1739{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001740 int cpu;
1741
Steven Rostedt9aba60f2009-03-11 19:52:30 -04001742 /* If we are looking at one CPU buffer, only check that one */
1743 if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1744 cpu = iter->cpu_file;
1745 if (iter->buffer_iter[cpu]) {
1746 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1747 return 0;
1748 } else {
1749 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1750 return 0;
1751 }
1752 return 1;
1753 }
1754
Steven Rostedtab464282008-05-12 21:21:00 +02001755 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001756 if (iter->buffer_iter[cpu]) {
1757 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1758 return 0;
1759 } else {
1760 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1761 return 0;
1762 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001763 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001764
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001765 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001766}
1767
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001768static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001769{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001770 enum print_line_t ret;
1771
1772 if (iter->trace && iter->trace->print_line) {
1773 ret = iter->trace->print_line(iter);
1774 if (ret != TRACE_TYPE_UNHANDLED)
1775 return ret;
1776 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001777
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001778 if (iter->ent->type == TRACE_BPRINT &&
1779 trace_flags & TRACE_ITER_PRINTK &&
1780 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04001781 return trace_print_bprintk_msg_only(iter);
Frederic Weisbecker48ead022009-03-12 18:24:49 +01001782
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001783 if (iter->ent->type == TRACE_PRINT &&
1784 trace_flags & TRACE_ITER_PRINTK &&
1785 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
Steven Rostedt5ef841f2009-03-19 12:20:38 -04001786 return trace_print_printk_msg_only(iter);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001787
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001788 if (trace_flags & TRACE_ITER_BIN)
1789 return print_bin_fmt(iter);
1790
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001791 if (trace_flags & TRACE_ITER_HEX)
1792 return print_hex_fmt(iter);
1793
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001794 if (trace_flags & TRACE_ITER_RAW)
1795 return print_raw_fmt(iter);
1796
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001797 return print_trace_fmt(iter);
1798}
1799
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001800static int s_show(struct seq_file *m, void *v)
1801{
1802 struct trace_iterator *iter = v;
1803
1804 if (iter->ent == NULL) {
1805 if (iter->tr) {
1806 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1807 seq_puts(m, "#\n");
1808 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001809 if (iter->trace && iter->trace->print_header)
1810 iter->trace->print_header(m);
1811 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001812 /* print nothing if the buffers are empty */
1813 if (trace_empty(iter))
1814 return 0;
1815 print_trace_header(m, iter);
1816 if (!(trace_flags & TRACE_ITER_VERBOSE))
1817 print_lat_help_header(m);
1818 } else {
1819 if (!(trace_flags & TRACE_ITER_VERBOSE))
1820 print_func_help_header(m);
1821 }
1822 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001823 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001824 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001825 }
1826
1827 return 0;
1828}
1829
1830static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001831 .start = s_start,
1832 .next = s_next,
1833 .stop = s_stop,
1834 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001835};
1836
Ingo Molnare309b412008-05-12 21:20:51 +02001837static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001838__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001839{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001840 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001841 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001842 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001843 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001844 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001845
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001846 if (tracing_disabled)
1847 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001848
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001849 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001850 if (!iter)
1851 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001852
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001853 /*
1854 * We make a copy of the current tracer to avoid concurrent
1855 * changes on it while we are reading.
1856 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001857 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001858 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001859 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001860 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001861
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001862 if (current_trace)
1863 *iter->trace = *current_trace;
1864
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001865 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1866 goto fail;
1867
1868 cpumask_clear(iter->started);
1869
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001870 if (current_trace && current_trace->print_max)
1871 iter->tr = &max_tr;
1872 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001873 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001874 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001875 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001876 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001877
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001878 /* Notify the tracer early; before we stop tracing. */
1879 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001880 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001881
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001882 /* Annotate start of buffers if we had overruns */
1883 if (ring_buffer_overruns(iter->tr->buffer))
1884 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1885
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001886 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1887 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001888
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001889 iter->buffer_iter[cpu] =
1890 ring_buffer_read_start(iter->tr->buffer, cpu);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001891 }
1892 } else {
1893 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001894 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001895 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001896 }
1897
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001898 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001899 ret = seq_open(file, &tracer_seq_ops);
1900 if (ret < 0) {
1901 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001902 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001903 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001904
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001905 m = file->private_data;
1906 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001907
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001908 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001909 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001910
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001911 mutex_unlock(&trace_types_lock);
1912
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001913 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001914
1915 fail_buffer:
1916 for_each_tracing_cpu(cpu) {
1917 if (iter->buffer_iter[cpu])
1918 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1919 }
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001920 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001921 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001922 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001923 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001924 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001925
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001926 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001927}
1928
1929int tracing_open_generic(struct inode *inode, struct file *filp)
1930{
Steven Rostedt60a11772008-05-12 21:20:44 +02001931 if (tracing_disabled)
1932 return -ENODEV;
1933
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001934 filp->private_data = inode->i_private;
1935 return 0;
1936}
1937
Hannes Eder4fd27352009-02-10 19:44:12 +01001938static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001939{
1940 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001941 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001942 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001943
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001944 if (!(file->f_mode & FMODE_READ))
1945 return 0;
1946
1947 iter = m->private;
1948
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001949 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001950 for_each_tracing_cpu(cpu) {
1951 if (iter->buffer_iter[cpu])
1952 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1953 }
1954
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001955 if (iter->trace && iter->trace->close)
1956 iter->trace->close(iter);
1957
1958 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001959 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001960 mutex_unlock(&trace_types_lock);
1961
1962 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001963 mutex_destroy(&iter->mutex);
Frederic Weisbeckerb0dfa972009-04-01 22:53:08 +02001964 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001965 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001966 kfree(iter);
1967 return 0;
1968}
1969
1970static int tracing_open(struct inode *inode, struct file *file)
1971{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001972 struct trace_iterator *iter;
1973 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001974
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001975 /* If this file was open for write, then erase contents */
1976 if ((file->f_mode & FMODE_WRITE) &&
1977 !(file->f_flags & O_APPEND)) {
1978 long cpu = (long) inode->i_private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001979
Steven Rostedt4acd4d02009-03-18 10:40:24 -04001980 if (cpu == TRACE_PIPE_ALL_CPU)
1981 tracing_reset_online_cpus(&global_trace);
1982 else
1983 tracing_reset(&global_trace, cpu);
1984 }
1985
1986 if (file->f_mode & FMODE_READ) {
1987 iter = __tracing_open(inode, file);
1988 if (IS_ERR(iter))
1989 ret = PTR_ERR(iter);
1990 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
1991 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1992 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001993 return ret;
1994}
1995
Ingo Molnare309b412008-05-12 21:20:51 +02001996static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001997t_next(struct seq_file *m, void *v, loff_t *pos)
1998{
1999 struct tracer *t = m->private;
2000
2001 (*pos)++;
2002
2003 if (t)
2004 t = t->next;
2005
2006 m->private = t;
2007
2008 return t;
2009}
2010
2011static void *t_start(struct seq_file *m, loff_t *pos)
2012{
2013 struct tracer *t = m->private;
2014 loff_t l = 0;
2015
2016 mutex_lock(&trace_types_lock);
2017 for (; t && l < *pos; t = t_next(m, t, &l))
2018 ;
2019
2020 return t;
2021}
2022
2023static void t_stop(struct seq_file *m, void *p)
2024{
2025 mutex_unlock(&trace_types_lock);
2026}
2027
2028static int t_show(struct seq_file *m, void *v)
2029{
2030 struct tracer *t = v;
2031
2032 if (!t)
2033 return 0;
2034
2035 seq_printf(m, "%s", t->name);
2036 if (t->next)
2037 seq_putc(m, ' ');
2038 else
2039 seq_putc(m, '\n');
2040
2041 return 0;
2042}
2043
2044static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002045 .start = t_start,
2046 .next = t_next,
2047 .stop = t_stop,
2048 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002049};
2050
2051static int show_traces_open(struct inode *inode, struct file *file)
2052{
2053 int ret;
2054
Steven Rostedt60a11772008-05-12 21:20:44 +02002055 if (tracing_disabled)
2056 return -ENODEV;
2057
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002058 ret = seq_open(file, &show_traces_seq_ops);
2059 if (!ret) {
2060 struct seq_file *m = file->private_data;
2061 m->private = trace_types;
2062 }
2063
2064 return ret;
2065}
2066
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002067static ssize_t
2068tracing_write_stub(struct file *filp, const char __user *ubuf,
2069 size_t count, loff_t *ppos)
2070{
2071 return count;
2072}
2073
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002074static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002075 .open = tracing_open,
2076 .read = seq_read,
Steven Rostedt4acd4d02009-03-18 10:40:24 -04002077 .write = tracing_write_stub,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002078 .llseek = seq_lseek,
2079 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002080};
2081
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002082static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002083 .open = show_traces_open,
2084 .read = seq_read,
2085 .release = seq_release,
2086};
2087
Ingo Molnar36dfe922008-05-12 21:20:52 +02002088/*
2089 * Only trace on a CPU if the bitmask is set:
2090 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302091static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002092
2093/*
2094 * The tracer itself will not take this lock, but still we want
2095 * to provide a consistent cpumask to user-space:
2096 */
2097static DEFINE_MUTEX(tracing_cpumask_update_lock);
2098
2099/*
2100 * Temporary storage for the character representation of the
2101 * CPU bitmask (and one more byte for the newline):
2102 */
2103static char mask_str[NR_CPUS + 1];
2104
Ingo Molnarc7078de2008-05-12 21:20:52 +02002105static ssize_t
2106tracing_cpumask_read(struct file *filp, char __user *ubuf,
2107 size_t count, loff_t *ppos)
2108{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002109 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002110
2111 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002112
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302113 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002114 if (count - len < 2) {
2115 count = -EINVAL;
2116 goto out_err;
2117 }
2118 len += sprintf(mask_str + len, "\n");
2119 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2120
2121out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002122 mutex_unlock(&tracing_cpumask_update_lock);
2123
2124 return count;
2125}
2126
2127static ssize_t
2128tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2129 size_t count, loff_t *ppos)
2130{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002131 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302132 cpumask_var_t tracing_cpumask_new;
2133
2134 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2135 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002136
2137 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302138 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002139 if (err)
2140 goto err_unlock;
2141
Steven Rostedta5e25882008-12-02 15:34:05 -05002142 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02002143 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002144 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002145 /*
2146 * Increase/decrease the disabled counter if we are
2147 * about to flip a bit in the cpumask:
2148 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302149 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2150 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002151 atomic_inc(&global_trace.data[cpu]->disabled);
2152 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302153 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2154 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002155 atomic_dec(&global_trace.data[cpu]->disabled);
2156 }
2157 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002158 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002159 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002160
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302161 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002162
Ingo Molnarc7078de2008-05-12 21:20:52 +02002163 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302164 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002165
Ingo Molnarc7078de2008-05-12 21:20:52 +02002166 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002167
2168err_unlock:
2169 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302170 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002171
2172 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002173}
2174
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002175static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002176 .open = tracing_open_generic,
2177 .read = tracing_cpumask_read,
2178 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002179};
2180
2181static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002182tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002183 size_t cnt, loff_t *ppos)
2184{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002185 struct tracer_opt *trace_opts;
2186 u32 tracer_flags;
2187 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188 char *buf;
2189 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002190 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002191
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002192
Wenji Huangc3706f02009-02-10 01:03:18 -05002193 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002194 for (i = 0; trace_options[i]; i++) {
2195 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002196 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002197 }
2198
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002199 mutex_lock(&trace_types_lock);
2200 tracer_flags = current_trace->flags->val;
2201 trace_opts = current_trace->flags->opts;
2202
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002203 /*
2204 * Increase the size with names of options specific
2205 * of the current tracer.
2206 */
2207 for (i = 0; trace_opts[i].name; i++) {
2208 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002209 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002210 }
2211
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002212 /* +2 for \n and \0 */
2213 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002214 if (!buf) {
2215 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002216 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002217 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002218
2219 for (i = 0; trace_options[i]; i++) {
2220 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002221 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002222 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002223 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002224 }
2225
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002226 for (i = 0; trace_opts[i].name; i++) {
2227 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002228 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002229 trace_opts[i].name);
2230 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002231 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002232 trace_opts[i].name);
2233 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002234 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002235
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002236 WARN_ON(r >= len + 2);
2237
Ingo Molnar36dfe922008-05-12 21:20:52 +02002238 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002239
2240 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002241 return r;
2242}
2243
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002244/* Try to assign a tracer specific option */
2245static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2246{
2247 struct tracer_flags *trace_flags = trace->flags;
2248 struct tracer_opt *opts = NULL;
2249 int ret = 0, i = 0;
2250 int len;
2251
2252 for (i = 0; trace_flags->opts[i].name; i++) {
2253 opts = &trace_flags->opts[i];
2254 len = strlen(opts->name);
2255
2256 if (strncmp(cmp, opts->name, len) == 0) {
2257 ret = trace->set_flag(trace_flags->val,
2258 opts->bit, !neg);
2259 break;
2260 }
2261 }
2262 /* Not found */
2263 if (!trace_flags->opts[i].name)
2264 return -EINVAL;
2265
2266 /* Refused to handle */
2267 if (ret)
2268 return ret;
2269
2270 if (neg)
2271 trace_flags->val &= ~opts->bit;
2272 else
2273 trace_flags->val |= opts->bit;
2274
2275 return 0;
2276}
2277
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002278static void set_tracer_flags(unsigned int mask, int enabled)
2279{
2280 /* do nothing if flag is already set */
2281 if (!!(trace_flags & mask) == !!enabled)
2282 return;
2283
2284 if (enabled)
2285 trace_flags |= mask;
2286 else
2287 trace_flags &= ~mask;
2288
2289 if (mask == TRACE_ITER_GLOBAL_CLK) {
2290 u64 (*func)(void);
2291
2292 if (enabled)
2293 func = trace_clock_global;
2294 else
2295 func = trace_clock_local;
2296
2297 mutex_lock(&trace_types_lock);
2298 ring_buffer_set_clock(global_trace.buffer, func);
2299
2300 if (max_tr.buffer)
2301 ring_buffer_set_clock(max_tr.buffer, func);
2302 mutex_unlock(&trace_types_lock);
2303 }
2304}
2305
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002306static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002307tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002308 size_t cnt, loff_t *ppos)
2309{
2310 char buf[64];
2311 char *cmp = buf;
2312 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002313 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002314 int i;
2315
Steven Rostedtcffae432008-05-12 21:21:00 +02002316 if (cnt >= sizeof(buf))
2317 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002318
2319 if (copy_from_user(&buf, ubuf, cnt))
2320 return -EFAULT;
2321
2322 buf[cnt] = 0;
2323
2324 if (strncmp(buf, "no", 2) == 0) {
2325 neg = 1;
2326 cmp += 2;
2327 }
2328
2329 for (i = 0; trace_options[i]; i++) {
2330 int len = strlen(trace_options[i]);
2331
2332 if (strncmp(cmp, trace_options[i], len) == 0) {
Steven Rostedtaf4617b2009-03-17 18:09:55 -04002333 set_tracer_flags(1 << i, !neg);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002334 break;
2335 }
2336 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002337
2338 /* If no option could be set, test the specific tracer options */
2339 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002340 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002341 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002342 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002343 if (ret)
2344 return ret;
2345 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002346
2347 filp->f_pos += cnt;
2348
2349 return cnt;
2350}
2351
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002352static const struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002353 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002354 .read = tracing_trace_options_read,
2355 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356};
2357
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002358static const char readme_msg[] =
2359 "tracing mini-HOWTO:\n\n"
2360 "# mkdir /debug\n"
2361 "# mount -t debugfs nodev /debug\n\n"
2362 "# cat /debug/tracing/available_tracers\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302363 "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002364 "# cat /debug/tracing/current_tracer\n"
Nikanth Karthikesanbc2b6872009-03-23 11:58:31 +05302365 "nop\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002366 "# echo sched_switch > /debug/tracing/current_tracer\n"
2367 "# cat /debug/tracing/current_tracer\n"
2368 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002369 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002370 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002371 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002372 "# echo 1 > /debug/tracing/tracing_enabled\n"
2373 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2374 "echo 0 > /debug/tracing/tracing_enabled\n"
2375;
2376
2377static ssize_t
2378tracing_readme_read(struct file *filp, char __user *ubuf,
2379 size_t cnt, loff_t *ppos)
2380{
2381 return simple_read_from_buffer(ubuf, cnt, ppos,
2382 readme_msg, strlen(readme_msg));
2383}
2384
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002385static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002386 .open = tracing_open_generic,
2387 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002388};
2389
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002390static ssize_t
2391tracing_ctrl_read(struct file *filp, char __user *ubuf,
2392 size_t cnt, loff_t *ppos)
2393{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002394 char buf[64];
2395 int r;
2396
Steven Rostedt90369902008-11-05 16:05:44 -05002397 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002398 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002399}
2400
2401static ssize_t
2402tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2403 size_t cnt, loff_t *ppos)
2404{
2405 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002406 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002407 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002408 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002409
Steven Rostedtcffae432008-05-12 21:21:00 +02002410 if (cnt >= sizeof(buf))
2411 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002412
2413 if (copy_from_user(&buf, ubuf, cnt))
2414 return -EFAULT;
2415
2416 buf[cnt] = 0;
2417
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002418 ret = strict_strtoul(buf, 10, &val);
2419 if (ret < 0)
2420 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002421
2422 val = !!val;
2423
2424 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002425 if (tracer_enabled ^ val) {
2426 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002427 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002428 if (current_trace->start)
2429 current_trace->start(tr);
2430 tracing_start();
2431 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002432 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002433 tracing_stop();
2434 if (current_trace->stop)
2435 current_trace->stop(tr);
2436 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002437 }
2438 mutex_unlock(&trace_types_lock);
2439
2440 filp->f_pos += cnt;
2441
2442 return cnt;
2443}
2444
2445static ssize_t
2446tracing_set_trace_read(struct file *filp, char __user *ubuf,
2447 size_t cnt, loff_t *ppos)
2448{
2449 char buf[max_tracer_type_len+2];
2450 int r;
2451
2452 mutex_lock(&trace_types_lock);
2453 if (current_trace)
2454 r = sprintf(buf, "%s\n", current_trace->name);
2455 else
2456 r = sprintf(buf, "\n");
2457 mutex_unlock(&trace_types_lock);
2458
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002459 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002460}
2461
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002462int tracer_init(struct tracer *t, struct trace_array *tr)
2463{
2464 tracing_reset_online_cpus(tr);
2465 return t->init(tr);
2466}
2467
Steven Rostedt73c51622009-03-11 13:42:01 -04002468static int tracing_resize_ring_buffer(unsigned long size)
2469{
2470 int ret;
2471
2472 /*
2473 * If kernel or user changes the size of the ring buffer
Steven Rostedta123c522009-03-12 11:21:08 -04002474 * we use the size that was given, and we can forget about
2475 * expanding it later.
Steven Rostedt73c51622009-03-11 13:42:01 -04002476 */
2477 ring_buffer_expanded = 1;
2478
2479 ret = ring_buffer_resize(global_trace.buffer, size);
2480 if (ret < 0)
2481 return ret;
2482
2483 ret = ring_buffer_resize(max_tr.buffer, size);
2484 if (ret < 0) {
2485 int r;
2486
2487 r = ring_buffer_resize(global_trace.buffer,
2488 global_trace.entries);
2489 if (r < 0) {
Steven Rostedta123c522009-03-12 11:21:08 -04002490 /*
2491 * AARGH! We are left with different
2492 * size max buffer!!!!
2493 * The max buffer is our "snapshot" buffer.
2494 * When a tracer needs a snapshot (one of the
2495 * latency tracers), it swaps the max buffer
2496 * with the saved snap shot. We succeeded to
2497 * update the size of the main buffer, but failed to
2498 * update the size of the max buffer. But when we tried
2499 * to reset the main buffer to the original size, we
2500 * failed there too. This is very unlikely to
2501 * happen, but if it does, warn and kill all
2502 * tracing.
2503 */
Steven Rostedt73c51622009-03-11 13:42:01 -04002504 WARN_ON(1);
2505 tracing_disabled = 1;
2506 }
2507 return ret;
2508 }
2509
2510 global_trace.entries = size;
2511
2512 return ret;
2513}
2514
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002515/**
2516 * tracing_update_buffers - used by tracing facility to expand ring buffers
2517 *
2518 * To save on memory when the tracing is never used on a system with it
2519 * configured in. The ring buffers are set to a minimum size. But once
2520 * a user starts to use the tracing facility, then they need to grow
2521 * to their default size.
2522 *
2523 * This function is to be called when a tracer is about to be used.
2524 */
2525int tracing_update_buffers(void)
2526{
2527 int ret = 0;
2528
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002529 mutex_lock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002530 if (!ring_buffer_expanded)
2531 ret = tracing_resize_ring_buffer(trace_buf_size);
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002532 mutex_unlock(&trace_types_lock);
Steven Rostedt1852fcc2009-03-11 14:33:00 -04002533
2534 return ret;
2535}
2536
Steven Rostedt577b7852009-02-26 23:43:05 -05002537struct trace_option_dentry;
2538
2539static struct trace_option_dentry *
2540create_trace_option_files(struct tracer *tracer);
2541
2542static void
2543destroy_trace_option_files(struct trace_option_dentry *topts);
2544
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002545static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002546{
Steven Rostedt577b7852009-02-26 23:43:05 -05002547 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002548 struct trace_array *tr = &global_trace;
2549 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002550 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002551
Steven Rostedt1027fcb2009-03-12 11:33:20 -04002552 mutex_lock(&trace_types_lock);
2553
Steven Rostedt73c51622009-03-11 13:42:01 -04002554 if (!ring_buffer_expanded) {
2555 ret = tracing_resize_ring_buffer(trace_buf_size);
2556 if (ret < 0)
Frederic Weisbecker59f586d2009-03-15 22:10:39 +01002557 goto out;
Steven Rostedt73c51622009-03-11 13:42:01 -04002558 ret = 0;
2559 }
2560
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002561 for (t = trace_types; t; t = t->next) {
2562 if (strcmp(t->name, buf) == 0)
2563 break;
2564 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002565 if (!t) {
2566 ret = -EINVAL;
2567 goto out;
2568 }
2569 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002570 goto out;
2571
Steven Rostedt9f029e82008-11-12 15:24:24 -05002572 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002573 if (current_trace && current_trace->reset)
2574 current_trace->reset(tr);
2575
Steven Rostedt577b7852009-02-26 23:43:05 -05002576 destroy_trace_option_files(topts);
2577
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002578 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002579
2580 topts = create_trace_option_files(current_trace);
2581
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002582 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002583 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002584 if (ret)
2585 goto out;
2586 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002587
Steven Rostedt9f029e82008-11-12 15:24:24 -05002588 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002589 out:
2590 mutex_unlock(&trace_types_lock);
2591
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002592 return ret;
2593}
2594
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002595static ssize_t
2596tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2597 size_t cnt, loff_t *ppos)
2598{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002599 char buf[max_tracer_type_len+1];
2600 int i;
2601 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002602 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002603
Steven Rostedt60063a62008-10-28 10:44:24 -04002604 ret = cnt;
2605
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002606 if (cnt > max_tracer_type_len)
2607 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002608
2609 if (copy_from_user(&buf, ubuf, cnt))
2610 return -EFAULT;
2611
2612 buf[cnt] = 0;
2613
2614 /* strip ending whitespace. */
2615 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2616 buf[i] = 0;
2617
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002618 err = tracing_set_tracer(buf);
2619 if (err)
2620 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002621
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002622 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002623
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002624 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002625}
2626
2627static ssize_t
2628tracing_max_lat_read(struct file *filp, char __user *ubuf,
2629 size_t cnt, loff_t *ppos)
2630{
2631 unsigned long *ptr = filp->private_data;
2632 char buf[64];
2633 int r;
2634
Steven Rostedtcffae432008-05-12 21:21:00 +02002635 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002636 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002637 if (r > sizeof(buf))
2638 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002639 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002640}
2641
2642static ssize_t
2643tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2644 size_t cnt, loff_t *ppos)
2645{
Hannes Eder5e398412009-02-10 19:44:34 +01002646 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002647 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002648 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002649 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002650
Steven Rostedtcffae432008-05-12 21:21:00 +02002651 if (cnt >= sizeof(buf))
2652 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002653
2654 if (copy_from_user(&buf, ubuf, cnt))
2655 return -EFAULT;
2656
2657 buf[cnt] = 0;
2658
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002659 ret = strict_strtoul(buf, 10, &val);
2660 if (ret < 0)
2661 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002662
2663 *ptr = val * 1000;
2664
2665 return cnt;
2666}
2667
Steven Rostedtb3806b42008-05-12 21:20:46 +02002668static int tracing_open_pipe(struct inode *inode, struct file *filp)
2669{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002670 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002671 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002672 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002673
2674 if (tracing_disabled)
2675 return -ENODEV;
2676
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002677 mutex_lock(&trace_types_lock);
2678
2679 /* We only allow one reader per cpu */
2680 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2681 if (!cpumask_empty(tracing_reader_cpumask)) {
2682 ret = -EBUSY;
2683 goto out;
2684 }
2685 cpumask_setall(tracing_reader_cpumask);
2686 } else {
2687 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2688 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2689 else {
2690 ret = -EBUSY;
2691 goto out;
2692 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002693 }
2694
2695 /* create a buffer to store the information to pass to userspace */
2696 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002697 if (!iter) {
2698 ret = -ENOMEM;
2699 goto out;
2700 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002701
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002702 /*
2703 * We make a copy of the current tracer to avoid concurrent
2704 * changes on it while we are reading.
2705 */
2706 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2707 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002708 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002709 goto fail;
2710 }
2711 if (current_trace)
2712 *iter->trace = *current_trace;
2713
2714 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2715 ret = -ENOMEM;
2716 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302717 }
2718
Steven Rostedta3097202008-11-07 22:36:02 -05002719 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302720 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002721
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002722 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002723 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002724 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002725 filp->private_data = iter;
2726
Steven Rostedt107bad82008-05-12 21:21:01 +02002727 if (iter->trace->pipe_open)
2728 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002729
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002730out:
2731 mutex_unlock(&trace_types_lock);
2732 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002733
2734fail:
2735 kfree(iter->trace);
2736 kfree(iter);
2737 mutex_unlock(&trace_types_lock);
2738 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002739}
2740
2741static int tracing_release_pipe(struct inode *inode, struct file *file)
2742{
2743 struct trace_iterator *iter = file->private_data;
2744
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002745 mutex_lock(&trace_types_lock);
2746
2747 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2748 cpumask_clear(tracing_reader_cpumask);
2749 else
2750 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2751
2752 mutex_unlock(&trace_types_lock);
2753
Rusty Russell44623442009-01-01 10:12:23 +10302754 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002755 mutex_destroy(&iter->mutex);
2756 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002757 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002758
2759 return 0;
2760}
2761
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002762static unsigned int
2763tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2764{
2765 struct trace_iterator *iter = filp->private_data;
2766
2767 if (trace_flags & TRACE_ITER_BLOCK) {
2768 /*
2769 * Always select as readable when in blocking mode
2770 */
2771 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002772 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002773 if (!trace_empty(iter))
2774 return POLLIN | POLLRDNORM;
2775 poll_wait(filp, &trace_wait, poll_table);
2776 if (!trace_empty(iter))
2777 return POLLIN | POLLRDNORM;
2778
2779 return 0;
2780 }
2781}
2782
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002783
2784void default_wait_pipe(struct trace_iterator *iter)
2785{
2786 DEFINE_WAIT(wait);
2787
2788 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2789
2790 if (trace_empty(iter))
2791 schedule();
2792
2793 finish_wait(&trace_wait, &wait);
2794}
2795
2796/*
2797 * This is a make-shift waitqueue.
2798 * A tracer might use this callback on some rare cases:
2799 *
2800 * 1) the current tracer might hold the runqueue lock when it wakes up
2801 * a reader, hence a deadlock (sched, function, and function graph tracers)
2802 * 2) the function tracers, trace all functions, we don't want
2803 * the overhead of calling wake_up and friends
2804 * (and tracing them too)
2805 *
2806 * Anyway, this is really very primitive wakeup.
2807 */
2808void poll_wait_pipe(struct trace_iterator *iter)
2809{
2810 set_current_state(TASK_INTERRUPTIBLE);
2811 /* sleep for 100 msecs, and try again. */
2812 schedule_timeout(HZ / 10);
2813}
2814
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002815/* Must be called with trace_types_lock mutex held. */
2816static int tracing_wait_pipe(struct file *filp)
2817{
2818 struct trace_iterator *iter = filp->private_data;
2819
2820 while (trace_empty(iter)) {
2821
2822 if ((filp->f_flags & O_NONBLOCK)) {
2823 return -EAGAIN;
2824 }
2825
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002826 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002827
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002828 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002829
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002830 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002831
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002832 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002833 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002834
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002835 /*
2836 * We block until we read something and tracing is disabled.
2837 * We still block if tracing is disabled, but we have never
2838 * read anything. This allows a user to cat this file, and
2839 * then enable tracing. But after we have read something,
2840 * we give an EOF when tracing is again disabled.
2841 *
2842 * iter->pos will be 0 if we haven't read anything.
2843 */
2844 if (!tracer_enabled && iter->pos)
2845 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002846 }
2847
2848 return 1;
2849}
2850
Steven Rostedtb3806b42008-05-12 21:20:46 +02002851/*
2852 * Consumer reader.
2853 */
2854static ssize_t
2855tracing_read_pipe(struct file *filp, char __user *ubuf,
2856 size_t cnt, loff_t *ppos)
2857{
2858 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002859 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002860 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002861
2862 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002863 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2864 if (sret != -EBUSY)
2865 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002866
Steven Rostedtf9520752009-03-02 14:04:40 -05002867 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002868
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002869 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002870 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002871 if (unlikely(old_tracer != current_trace && current_trace)) {
2872 old_tracer = current_trace;
2873 *iter->trace = *current_trace;
2874 }
2875 mutex_unlock(&trace_types_lock);
2876
2877 /*
2878 * Avoid more than one consumer on a single file descriptor
2879 * This is just a matter of traces coherency, the ring buffer itself
2880 * is protected.
2881 */
2882 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002883 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002884 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2885 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002886 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002887 }
2888
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002889waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002890 sret = tracing_wait_pipe(filp);
2891 if (sret <= 0)
2892 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002893
2894 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002895 if (trace_empty(iter)) {
2896 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002897 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002898 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002899
2900 if (cnt >= PAGE_SIZE)
2901 cnt = PAGE_SIZE - 1;
2902
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002903 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002904 memset(&iter->seq, 0,
2905 sizeof(struct trace_iterator) -
2906 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002907 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002908
Steven Rostedt088b1e422008-05-12 21:20:48 +02002909 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002910 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002911 int len = iter->seq.len;
2912
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002913 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002914 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002915 /* don't print partial lines */
2916 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002917 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002918 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002919 if (ret != TRACE_TYPE_NO_CONSUME)
2920 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002921
2922 if (iter->seq.len >= cnt)
2923 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002924 }
2925
Steven Rostedtb3806b42008-05-12 21:20:46 +02002926 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002927 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2928 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002929 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002930
2931 /*
2932 * If there was nothing to send to user, inspite of consuming trace
2933 * entries, go back to wait for more entries.
2934 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002935 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002936 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002937
Steven Rostedt107bad82008-05-12 21:21:01 +02002938out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002939 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002940
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002941 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002942}
2943
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002944static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2945 struct pipe_buffer *buf)
2946{
2947 __free_page(buf->page);
2948}
2949
2950static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2951 unsigned int idx)
2952{
2953 __free_page(spd->pages[idx]);
2954}
2955
2956static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002957 .can_merge = 0,
2958 .map = generic_pipe_buf_map,
2959 .unmap = generic_pipe_buf_unmap,
2960 .confirm = generic_pipe_buf_confirm,
2961 .release = tracing_pipe_buf_release,
2962 .steal = generic_pipe_buf_steal,
2963 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002964};
2965
Steven Rostedt34cd4992009-02-09 12:06:29 -05002966static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002967tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002968{
2969 size_t count;
2970 int ret;
2971
2972 /* Seq buffer is page-sized, exactly what we need. */
2973 for (;;) {
2974 count = iter->seq.len;
2975 ret = print_trace_line(iter);
2976 count = iter->seq.len - count;
2977 if (rem < count) {
2978 rem = 0;
2979 iter->seq.len -= count;
2980 break;
2981 }
2982 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2983 iter->seq.len -= count;
2984 break;
2985 }
2986
2987 trace_consume(iter);
2988 rem -= count;
2989 if (!find_next_entry_inc(iter)) {
2990 rem = 0;
2991 iter->ent = NULL;
2992 break;
2993 }
2994 }
2995
2996 return rem;
2997}
2998
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002999static ssize_t tracing_splice_read_pipe(struct file *filp,
3000 loff_t *ppos,
3001 struct pipe_inode_info *pipe,
3002 size_t len,
3003 unsigned int flags)
3004{
3005 struct page *pages[PIPE_BUFFERS];
3006 struct partial_page partial[PIPE_BUFFERS];
3007 struct trace_iterator *iter = filp->private_data;
3008 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05003009 .pages = pages,
3010 .partial = partial,
3011 .nr_pages = 0, /* This gets updated below. */
3012 .flags = flags,
3013 .ops = &tracing_pipe_buf_ops,
3014 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003015 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003016 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003017 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003018 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003019 unsigned int i;
3020
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003021 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003022 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003023 if (unlikely(old_tracer != current_trace && current_trace)) {
3024 old_tracer = current_trace;
3025 *iter->trace = *current_trace;
3026 }
3027 mutex_unlock(&trace_types_lock);
3028
3029 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003030
3031 if (iter->trace->splice_read) {
3032 ret = iter->trace->splice_read(iter, filp,
3033 ppos, pipe, len, flags);
3034 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003035 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003036 }
3037
3038 ret = tracing_wait_pipe(filp);
3039 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05003040 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003041
3042 if (!iter->ent && !find_next_entry_inc(iter)) {
3043 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05003044 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003045 }
3046
3047 /* Fill as many pages as possible. */
3048 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3049 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05003050 if (!pages[i])
3051 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003052
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01003053 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003054
3055 /* Copy the data into the page, so we can start over. */
3056 ret = trace_seq_to_buffer(&iter->seq,
3057 page_address(pages[i]),
3058 iter->seq.len);
3059 if (ret < 0) {
3060 __free_page(pages[i]);
3061 break;
3062 }
3063 partial[i].offset = 0;
3064 partial[i].len = iter->seq.len;
3065
Steven Rostedtf9520752009-03-02 14:04:40 -05003066 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003067 }
3068
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003069 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003070
3071 spd.nr_pages = i;
3072
3073 return splice_to_pipe(pipe, &spd);
3074
Steven Rostedt34cd4992009-02-09 12:06:29 -05003075out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01003076 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003077
3078 return ret;
3079}
3080
Steven Rostedta98a3c32008-05-12 21:20:59 +02003081static ssize_t
3082tracing_entries_read(struct file *filp, char __user *ubuf,
3083 size_t cnt, loff_t *ppos)
3084{
3085 struct trace_array *tr = filp->private_data;
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003086 char buf[96];
Steven Rostedta98a3c32008-05-12 21:20:59 +02003087 int r;
3088
Steven Rostedtdb526ca2009-03-12 13:53:25 -04003089 mutex_lock(&trace_types_lock);
3090 if (!ring_buffer_expanded)
3091 r = sprintf(buf, "%lu (expanded: %lu)\n",
3092 tr->entries >> 10,
3093 trace_buf_size >> 10);
3094 else
3095 r = sprintf(buf, "%lu\n", tr->entries >> 10);
3096 mutex_unlock(&trace_types_lock);
3097
Steven Rostedta98a3c32008-05-12 21:20:59 +02003098 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3099}
3100
3101static ssize_t
3102tracing_entries_write(struct file *filp, const char __user *ubuf,
3103 size_t cnt, loff_t *ppos)
3104{
3105 unsigned long val;
3106 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003107 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003108
Steven Rostedtcffae432008-05-12 21:21:00 +02003109 if (cnt >= sizeof(buf))
3110 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003111
3112 if (copy_from_user(&buf, ubuf, cnt))
3113 return -EFAULT;
3114
3115 buf[cnt] = 0;
3116
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02003117 ret = strict_strtoul(buf, 10, &val);
3118 if (ret < 0)
3119 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003120
3121 /* must have at least 1 entry */
3122 if (!val)
3123 return -EINVAL;
3124
3125 mutex_lock(&trace_types_lock);
3126
Steven Rostedtc76f0692008-11-07 22:36:02 -05003127 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003128
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003129 /* disable all cpu buffers */
3130 for_each_tracing_cpu(cpu) {
3131 if (global_trace.data[cpu])
3132 atomic_inc(&global_trace.data[cpu]->disabled);
3133 if (max_tr.data[cpu])
3134 atomic_inc(&max_tr.data[cpu]->disabled);
3135 }
3136
Steven Rostedt1696b2b2008-11-13 00:09:35 -05003137 /* value is in KB */
3138 val <<= 10;
3139
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003140 if (val != global_trace.entries) {
Steven Rostedt73c51622009-03-11 13:42:01 -04003141 ret = tracing_resize_ring_buffer(val);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003142 if (ret < 0) {
3143 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02003144 goto out;
3145 }
Steven Rostedta98a3c32008-05-12 21:20:59 +02003146 }
3147
3148 filp->f_pos += cnt;
3149
Steven Rostedt19384c02008-05-22 00:22:16 -04003150 /* If check pages failed, return ENOMEM */
3151 if (tracing_disabled)
3152 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003153 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05003154 for_each_tracing_cpu(cpu) {
3155 if (global_trace.data[cpu])
3156 atomic_dec(&global_trace.data[cpu]->disabled);
3157 if (max_tr.data[cpu])
3158 atomic_dec(&max_tr.data[cpu]->disabled);
3159 }
3160
Steven Rostedtc76f0692008-11-07 22:36:02 -05003161 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02003162 max_tr.entries = global_trace.entries;
3163 mutex_unlock(&trace_types_lock);
3164
3165 return cnt;
3166}
3167
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003168static int mark_printk(const char *fmt, ...)
3169{
3170 int ret;
3171 va_list args;
3172 va_start(args, fmt);
Steven Rostedt40ce74f2009-03-19 14:03:53 -04003173 ret = trace_vprintk(0, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003174 va_end(args);
3175 return ret;
3176}
3177
3178static ssize_t
3179tracing_mark_write(struct file *filp, const char __user *ubuf,
3180 size_t cnt, loff_t *fpos)
3181{
3182 char *buf;
3183 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003184
Steven Rostedtc76f0692008-11-07 22:36:02 -05003185 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003186 return -EINVAL;
3187
3188 if (cnt > TRACE_BUF_SIZE)
3189 cnt = TRACE_BUF_SIZE;
3190
3191 buf = kmalloc(cnt + 1, GFP_KERNEL);
3192 if (buf == NULL)
3193 return -ENOMEM;
3194
3195 if (copy_from_user(buf, ubuf, cnt)) {
3196 kfree(buf);
3197 return -EFAULT;
3198 }
3199
3200 /* Cut from the first nil or newline. */
3201 buf[cnt] = '\0';
3202 end = strchr(buf, '\n');
3203 if (end)
3204 *end = '\0';
3205
3206 cnt = mark_printk("%s\n", buf);
3207 kfree(buf);
3208 *fpos += cnt;
3209
3210 return cnt;
3211}
3212
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003213static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003214 .open = tracing_open_generic,
3215 .read = tracing_max_lat_read,
3216 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003217};
3218
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003219static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003220 .open = tracing_open_generic,
3221 .read = tracing_ctrl_read,
3222 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003223};
3224
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003225static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003226 .open = tracing_open_generic,
3227 .read = tracing_set_trace_read,
3228 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003229};
3230
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003231static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003232 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003233 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003234 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003235 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003236 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003237};
3238
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003239static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003240 .open = tracing_open_generic,
3241 .read = tracing_entries_read,
3242 .write = tracing_entries_write,
3243};
3244
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003245static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003246 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003247 .write = tracing_mark_write,
3248};
3249
Steven Rostedt2cadf912008-12-01 22:20:19 -05003250struct ftrace_buffer_info {
3251 struct trace_array *tr;
3252 void *spare;
3253 int cpu;
3254 unsigned int read;
3255};
3256
3257static int tracing_buffers_open(struct inode *inode, struct file *filp)
3258{
3259 int cpu = (int)(long)inode->i_private;
3260 struct ftrace_buffer_info *info;
3261
3262 if (tracing_disabled)
3263 return -ENODEV;
3264
3265 info = kzalloc(sizeof(*info), GFP_KERNEL);
3266 if (!info)
3267 return -ENOMEM;
3268
3269 info->tr = &global_trace;
3270 info->cpu = cpu;
3271 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3272 /* Force reading ring buffer for first read */
3273 info->read = (unsigned int)-1;
3274 if (!info->spare)
3275 goto out;
3276
3277 filp->private_data = info;
3278
3279 return 0;
3280
3281 out:
3282 kfree(info);
3283 return -ENOMEM;
3284}
3285
3286static ssize_t
3287tracing_buffers_read(struct file *filp, char __user *ubuf,
3288 size_t count, loff_t *ppos)
3289{
3290 struct ftrace_buffer_info *info = filp->private_data;
3291 unsigned int pos;
3292 ssize_t ret;
3293 size_t size;
3294
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003295 if (!count)
3296 return 0;
3297
Steven Rostedt2cadf912008-12-01 22:20:19 -05003298 /* Do we have previous read data to read? */
3299 if (info->read < PAGE_SIZE)
3300 goto read;
3301
3302 info->read = 0;
3303
3304 ret = ring_buffer_read_page(info->tr->buffer,
3305 &info->spare,
3306 count,
3307 info->cpu, 0);
3308 if (ret < 0)
3309 return 0;
3310
3311 pos = ring_buffer_page_len(info->spare);
3312
3313 if (pos < PAGE_SIZE)
3314 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3315
3316read:
3317 size = PAGE_SIZE - info->read;
3318 if (size > count)
3319 size = count;
3320
3321 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003322 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003323 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003324 size -= ret;
3325
Steven Rostedt2cadf912008-12-01 22:20:19 -05003326 *ppos += size;
3327 info->read += size;
3328
3329 return size;
3330}
3331
3332static int tracing_buffers_release(struct inode *inode, struct file *file)
3333{
3334 struct ftrace_buffer_info *info = file->private_data;
3335
3336 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3337 kfree(info);
3338
3339 return 0;
3340}
3341
3342struct buffer_ref {
3343 struct ring_buffer *buffer;
3344 void *page;
3345 int ref;
3346};
3347
3348static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3349 struct pipe_buffer *buf)
3350{
3351 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3352
3353 if (--ref->ref)
3354 return;
3355
3356 ring_buffer_free_read_page(ref->buffer, ref->page);
3357 kfree(ref);
3358 buf->private = 0;
3359}
3360
3361static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3362 struct pipe_buffer *buf)
3363{
3364 return 1;
3365}
3366
3367static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3368 struct pipe_buffer *buf)
3369{
3370 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3371
3372 ref->ref++;
3373}
3374
3375/* Pipe buffer operations for a buffer. */
3376static struct pipe_buf_operations buffer_pipe_buf_ops = {
3377 .can_merge = 0,
3378 .map = generic_pipe_buf_map,
3379 .unmap = generic_pipe_buf_unmap,
3380 .confirm = generic_pipe_buf_confirm,
3381 .release = buffer_pipe_buf_release,
3382 .steal = buffer_pipe_buf_steal,
3383 .get = buffer_pipe_buf_get,
3384};
3385
3386/*
3387 * Callback from splice_to_pipe(), if we need to release some pages
3388 * at the end of the spd in case we error'ed out in filling the pipe.
3389 */
3390static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3391{
3392 struct buffer_ref *ref =
3393 (struct buffer_ref *)spd->partial[i].private;
3394
3395 if (--ref->ref)
3396 return;
3397
3398 ring_buffer_free_read_page(ref->buffer, ref->page);
3399 kfree(ref);
3400 spd->partial[i].private = 0;
3401}
3402
3403static ssize_t
3404tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3405 struct pipe_inode_info *pipe, size_t len,
3406 unsigned int flags)
3407{
3408 struct ftrace_buffer_info *info = file->private_data;
3409 struct partial_page partial[PIPE_BUFFERS];
3410 struct page *pages[PIPE_BUFFERS];
3411 struct splice_pipe_desc spd = {
3412 .pages = pages,
3413 .partial = partial,
3414 .flags = flags,
3415 .ops = &buffer_pipe_buf_ops,
3416 .spd_release = buffer_spd_release,
3417 };
3418 struct buffer_ref *ref;
3419 int size, i;
3420 size_t ret;
3421
3422 /*
3423 * We can't seek on a buffer input
3424 */
3425 if (unlikely(*ppos))
3426 return -ESPIPE;
3427
3428
3429 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3430 struct page *page;
3431 int r;
3432
3433 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3434 if (!ref)
3435 break;
3436
3437 ref->buffer = info->tr->buffer;
3438 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3439 if (!ref->page) {
3440 kfree(ref);
3441 break;
3442 }
3443
3444 r = ring_buffer_read_page(ref->buffer, &ref->page,
3445 len, info->cpu, 0);
3446 if (r < 0) {
3447 ring_buffer_free_read_page(ref->buffer,
3448 ref->page);
3449 kfree(ref);
3450 break;
3451 }
3452
3453 /*
3454 * zero out any left over data, this is going to
3455 * user land.
3456 */
3457 size = ring_buffer_page_len(ref->page);
3458 if (size < PAGE_SIZE)
3459 memset(ref->page + size, 0, PAGE_SIZE - size);
3460
3461 page = virt_to_page(ref->page);
3462
3463 spd.pages[i] = page;
3464 spd.partial[i].len = PAGE_SIZE;
3465 spd.partial[i].offset = 0;
3466 spd.partial[i].private = (unsigned long)ref;
3467 spd.nr_pages++;
3468 }
3469
3470 spd.nr_pages = i;
3471
3472 /* did we read anything? */
3473 if (!spd.nr_pages) {
3474 if (flags & SPLICE_F_NONBLOCK)
3475 ret = -EAGAIN;
3476 else
3477 ret = 0;
3478 /* TODO: block */
3479 return ret;
3480 }
3481
3482 ret = splice_to_pipe(pipe, &spd);
3483
3484 return ret;
3485}
3486
3487static const struct file_operations tracing_buffers_fops = {
3488 .open = tracing_buffers_open,
3489 .read = tracing_buffers_read,
3490 .release = tracing_buffers_release,
3491 .splice_read = tracing_buffers_splice_read,
3492 .llseek = no_llseek,
3493};
3494
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003495#ifdef CONFIG_DYNAMIC_FTRACE
3496
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003497int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003498{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003499 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003500}
3501
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003502static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003503tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003504 size_t cnt, loff_t *ppos)
3505{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003506 static char ftrace_dyn_info_buffer[1024];
3507 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003508 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003509 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003510 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003511 int r;
3512
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003513 mutex_lock(&dyn_info_mutex);
3514 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003515
Steven Rostedta26a2a22008-10-31 00:03:22 -04003516 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003517 buf[r++] = '\n';
3518
3519 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3520
3521 mutex_unlock(&dyn_info_mutex);
3522
3523 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003524}
3525
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003526static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003527 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003528 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003529};
3530#endif
3531
3532static struct dentry *d_tracer;
3533
3534struct dentry *tracing_init_dentry(void)
3535{
3536 static int once;
3537
3538 if (d_tracer)
3539 return d_tracer;
3540
Frederic Weisbecker3e1f60b2009-03-22 23:10:45 +01003541 if (!debugfs_initialized())
3542 return NULL;
3543
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003544 d_tracer = debugfs_create_dir("tracing", NULL);
3545
3546 if (!d_tracer && !once) {
3547 once = 1;
3548 pr_warning("Could not create debugfs directory 'tracing'\n");
3549 return NULL;
3550 }
3551
3552 return d_tracer;
3553}
3554
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003555static struct dentry *d_percpu;
3556
3557struct dentry *tracing_dentry_percpu(void)
3558{
3559 static int once;
3560 struct dentry *d_tracer;
3561
3562 if (d_percpu)
3563 return d_percpu;
3564
3565 d_tracer = tracing_init_dentry();
3566
3567 if (!d_tracer)
3568 return NULL;
3569
3570 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3571
3572 if (!d_percpu && !once) {
3573 once = 1;
3574 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3575 return NULL;
3576 }
3577
3578 return d_percpu;
3579}
3580
3581static void tracing_init_debugfs_percpu(long cpu)
3582{
3583 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003584 struct dentry *d_cpu;
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003585 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3586 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003587
3588 if (cpu > 999 || cpu < 0)
3589 return;
3590
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003591 sprintf(cpu_dir, "cpu%ld", cpu);
3592 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3593 if (!d_cpu) {
3594 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3595 return;
3596 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003597
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003598 /* per cpu trace_pipe */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003599 trace_create_file("trace_pipe", 0444, d_cpu,
3600 (void *) cpu, &tracing_pipe_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003601
3602 /* per cpu trace */
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003603 trace_create_file("trace", 0644, d_cpu,
3604 (void *) cpu, &tracing_fops);
Steven Rostedt7f96f932009-03-13 00:37:42 -04003605
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003606 trace_create_file("trace_pipe_raw", 0444, d_cpu,
3607 (void *) cpu, &tracing_buffers_fops);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003608}
3609
Steven Rostedt60a11772008-05-12 21:20:44 +02003610#ifdef CONFIG_FTRACE_SELFTEST
3611/* Let selftest have access to static functions in this file */
3612#include "trace_selftest.c"
3613#endif
3614
Steven Rostedt577b7852009-02-26 23:43:05 -05003615struct trace_option_dentry {
3616 struct tracer_opt *opt;
3617 struct tracer_flags *flags;
3618 struct dentry *entry;
3619};
3620
3621static ssize_t
3622trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3623 loff_t *ppos)
3624{
3625 struct trace_option_dentry *topt = filp->private_data;
3626 char *buf;
3627
3628 if (topt->flags->val & topt->opt->bit)
3629 buf = "1\n";
3630 else
3631 buf = "0\n";
3632
3633 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3634}
3635
3636static ssize_t
3637trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3638 loff_t *ppos)
3639{
3640 struct trace_option_dentry *topt = filp->private_data;
3641 unsigned long val;
3642 char buf[64];
3643 int ret;
3644
3645 if (cnt >= sizeof(buf))
3646 return -EINVAL;
3647
3648 if (copy_from_user(&buf, ubuf, cnt))
3649 return -EFAULT;
3650
3651 buf[cnt] = 0;
3652
3653 ret = strict_strtoul(buf, 10, &val);
3654 if (ret < 0)
3655 return ret;
3656
3657 ret = 0;
3658 switch (val) {
3659 case 0:
3660 /* do nothing if already cleared */
3661 if (!(topt->flags->val & topt->opt->bit))
3662 break;
3663
3664 mutex_lock(&trace_types_lock);
3665 if (current_trace->set_flag)
3666 ret = current_trace->set_flag(topt->flags->val,
3667 topt->opt->bit, 0);
3668 mutex_unlock(&trace_types_lock);
3669 if (ret)
3670 return ret;
3671 topt->flags->val &= ~topt->opt->bit;
3672 break;
3673 case 1:
3674 /* do nothing if already set */
3675 if (topt->flags->val & topt->opt->bit)
3676 break;
3677
3678 mutex_lock(&trace_types_lock);
3679 if (current_trace->set_flag)
3680 ret = current_trace->set_flag(topt->flags->val,
3681 topt->opt->bit, 1);
3682 mutex_unlock(&trace_types_lock);
3683 if (ret)
3684 return ret;
3685 topt->flags->val |= topt->opt->bit;
3686 break;
3687
3688 default:
3689 return -EINVAL;
3690 }
3691
3692 *ppos += cnt;
3693
3694 return cnt;
3695}
3696
3697
3698static const struct file_operations trace_options_fops = {
3699 .open = tracing_open_generic,
3700 .read = trace_options_read,
3701 .write = trace_options_write,
3702};
3703
Steven Rostedta8259072009-02-26 22:19:12 -05003704static ssize_t
3705trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3706 loff_t *ppos)
3707{
3708 long index = (long)filp->private_data;
3709 char *buf;
3710
3711 if (trace_flags & (1 << index))
3712 buf = "1\n";
3713 else
3714 buf = "0\n";
3715
3716 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3717}
3718
3719static ssize_t
3720trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3721 loff_t *ppos)
3722{
3723 long index = (long)filp->private_data;
3724 char buf[64];
3725 unsigned long val;
3726 int ret;
3727
3728 if (cnt >= sizeof(buf))
3729 return -EINVAL;
3730
3731 if (copy_from_user(&buf, ubuf, cnt))
3732 return -EFAULT;
3733
3734 buf[cnt] = 0;
3735
3736 ret = strict_strtoul(buf, 10, &val);
3737 if (ret < 0)
3738 return ret;
3739
3740 switch (val) {
3741 case 0:
3742 trace_flags &= ~(1 << index);
3743 break;
3744 case 1:
3745 trace_flags |= 1 << index;
3746 break;
3747
3748 default:
3749 return -EINVAL;
3750 }
3751
3752 *ppos += cnt;
3753
3754 return cnt;
3755}
3756
Steven Rostedta8259072009-02-26 22:19:12 -05003757static const struct file_operations trace_options_core_fops = {
3758 .open = tracing_open_generic,
3759 .read = trace_options_core_read,
3760 .write = trace_options_core_write,
3761};
3762
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003763struct dentry *trace_create_file(const char *name,
3764 mode_t mode,
3765 struct dentry *parent,
3766 void *data,
3767 const struct file_operations *fops)
3768{
3769 struct dentry *ret;
3770
3771 ret = debugfs_create_file(name, mode, parent, data, fops);
3772 if (!ret)
3773 pr_warning("Could not create debugfs '%s' entry\n", name);
3774
3775 return ret;
3776}
3777
3778
Steven Rostedta8259072009-02-26 22:19:12 -05003779static struct dentry *trace_options_init_dentry(void)
3780{
3781 struct dentry *d_tracer;
3782 static struct dentry *t_options;
3783
3784 if (t_options)
3785 return t_options;
3786
3787 d_tracer = tracing_init_dentry();
3788 if (!d_tracer)
3789 return NULL;
3790
3791 t_options = debugfs_create_dir("options", d_tracer);
3792 if (!t_options) {
3793 pr_warning("Could not create debugfs directory 'options'\n");
3794 return NULL;
3795 }
3796
3797 return t_options;
3798}
3799
Steven Rostedt577b7852009-02-26 23:43:05 -05003800static void
3801create_trace_option_file(struct trace_option_dentry *topt,
3802 struct tracer_flags *flags,
3803 struct tracer_opt *opt)
3804{
3805 struct dentry *t_options;
Steven Rostedt577b7852009-02-26 23:43:05 -05003806
3807 t_options = trace_options_init_dentry();
3808 if (!t_options)
3809 return;
3810
3811 topt->flags = flags;
3812 topt->opt = opt;
3813
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003814 topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
Steven Rostedt577b7852009-02-26 23:43:05 -05003815 &trace_options_fops);
3816
Steven Rostedt577b7852009-02-26 23:43:05 -05003817}
3818
3819static struct trace_option_dentry *
3820create_trace_option_files(struct tracer *tracer)
3821{
3822 struct trace_option_dentry *topts;
3823 struct tracer_flags *flags;
3824 struct tracer_opt *opts;
3825 int cnt;
3826
3827 if (!tracer)
3828 return NULL;
3829
3830 flags = tracer->flags;
3831
3832 if (!flags || !flags->opts)
3833 return NULL;
3834
3835 opts = flags->opts;
3836
3837 for (cnt = 0; opts[cnt].name; cnt++)
3838 ;
3839
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003840 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003841 if (!topts)
3842 return NULL;
3843
3844 for (cnt = 0; opts[cnt].name; cnt++)
3845 create_trace_option_file(&topts[cnt], flags,
3846 &opts[cnt]);
3847
3848 return topts;
3849}
3850
3851static void
3852destroy_trace_option_files(struct trace_option_dentry *topts)
3853{
3854 int cnt;
3855
3856 if (!topts)
3857 return;
3858
3859 for (cnt = 0; topts[cnt].opt; cnt++) {
3860 if (topts[cnt].entry)
3861 debugfs_remove(topts[cnt].entry);
3862 }
3863
3864 kfree(topts);
3865}
3866
Steven Rostedta8259072009-02-26 22:19:12 -05003867static struct dentry *
3868create_trace_option_core_file(const char *option, long index)
3869{
3870 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05003871
3872 t_options = trace_options_init_dentry();
3873 if (!t_options)
3874 return NULL;
3875
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003876 return trace_create_file(option, 0644, t_options, (void *)index,
Steven Rostedta8259072009-02-26 22:19:12 -05003877 &trace_options_core_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05003878}
3879
3880static __init void create_trace_options_dir(void)
3881{
3882 struct dentry *t_options;
Steven Rostedta8259072009-02-26 22:19:12 -05003883 int i;
3884
3885 t_options = trace_options_init_dentry();
3886 if (!t_options)
3887 return;
3888
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003889 for (i = 0; trace_options[i]; i++)
3890 create_trace_option_core_file(trace_options[i], i);
Steven Rostedta8259072009-02-26 22:19:12 -05003891}
3892
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003893static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003894{
3895 struct dentry *d_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003896 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003897
3898 d_tracer = tracing_init_dentry();
3899
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003900 trace_create_file("tracing_enabled", 0644, d_tracer,
3901 &global_trace, &tracing_ctrl_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003902
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003903 trace_create_file("trace_options", 0644, d_tracer,
3904 NULL, &tracing_iter_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003905
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003906 trace_create_file("tracing_cpumask", 0644, d_tracer,
3907 NULL, &tracing_cpumask_fops);
Steven Rostedta8259072009-02-26 22:19:12 -05003908
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003909 trace_create_file("trace", 0644, d_tracer,
3910 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Ingo Molnarc7078de2008-05-12 21:20:52 +02003911
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003912 trace_create_file("available_tracers", 0444, d_tracer,
3913 &global_trace, &show_traces_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003914
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003915 trace_create_file("current_tracer", 0444, d_tracer,
3916 &global_trace, &set_tracer_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003917
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003918 trace_create_file("tracing_max_latency", 0644, d_tracer,
3919 &tracing_max_latency, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003920
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003921 trace_create_file("tracing_thresh", 0644, d_tracer,
3922 &tracing_thresh, &tracing_max_lat_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003923
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003924 trace_create_file("README", 0644, d_tracer,
3925 NULL, &tracing_readme_fops);
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003926
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003927 trace_create_file("trace_pipe", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003928 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003929
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003930 trace_create_file("buffer_size_kb", 0644, d_tracer,
3931 &global_trace, &tracing_entries_fops);
Steven Rostedta98a3c32008-05-12 21:20:59 +02003932
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003933 trace_create_file("trace_marker", 0220, d_tracer,
3934 NULL, &tracing_mark_fops);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003935
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003936#ifdef CONFIG_DYNAMIC_FTRACE
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003937 trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3938 &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003939#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003940#ifdef CONFIG_SYSPROF_TRACER
3941 init_tracer_sysprof_debugfs(d_tracer);
3942#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003943
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003944 create_trace_options_dir();
3945
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003946 for_each_tracing_cpu(cpu)
3947 tracing_init_debugfs_percpu(cpu);
3948
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003949 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003950}
3951
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003952static int trace_panic_handler(struct notifier_block *this,
3953 unsigned long event, void *unused)
3954{
Steven Rostedt944ac422008-10-23 19:26:08 -04003955 if (ftrace_dump_on_oops)
3956 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003957 return NOTIFY_OK;
3958}
3959
3960static struct notifier_block trace_panic_notifier = {
3961 .notifier_call = trace_panic_handler,
3962 .next = NULL,
3963 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3964};
3965
3966static int trace_die_handler(struct notifier_block *self,
3967 unsigned long val,
3968 void *data)
3969{
3970 switch (val) {
3971 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003972 if (ftrace_dump_on_oops)
3973 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003974 break;
3975 default:
3976 break;
3977 }
3978 return NOTIFY_OK;
3979}
3980
3981static struct notifier_block trace_die_notifier = {
3982 .notifier_call = trace_die_handler,
3983 .priority = 200
3984};
3985
3986/*
3987 * printk is set to max of 1024, we really don't need it that big.
3988 * Nothing should be printing 1000 characters anyway.
3989 */
3990#define TRACE_MAX_PRINT 1000
3991
3992/*
3993 * Define here KERN_TRACE so that we have one place to modify
3994 * it if we decide to change what log level the ftrace dump
3995 * should be at.
3996 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003997#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003998
3999static void
4000trace_printk_seq(struct trace_seq *s)
4001{
4002 /* Probably should print a warning here. */
4003 if (s->len >= 1000)
4004 s->len = 1000;
4005
4006 /* should be zero ended, but we are paranoid. */
4007 s->buffer[s->len] = 0;
4008
4009 printk(KERN_TRACE "%s", s->buffer);
4010
Steven Rostedtf9520752009-03-02 14:04:40 -05004011 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004012}
4013
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004014static void __ftrace_dump(bool disable_tracing)
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004015{
4016 static DEFINE_SPINLOCK(ftrace_dump_lock);
4017 /* use static because iter can be a bit big for the stack */
4018 static struct trace_iterator iter;
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004019 unsigned int old_userobj;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004020 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04004021 unsigned long flags;
4022 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004023
4024 /* only one dump */
4025 spin_lock_irqsave(&ftrace_dump_lock, flags);
4026 if (dump_ran)
4027 goto out;
4028
4029 dump_ran = 1;
4030
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05004031 tracing_off();
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004032
4033 if (disable_tracing)
4034 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004035
Steven Rostedtd7690412008-10-01 00:29:53 -04004036 for_each_tracing_cpu(cpu) {
4037 atomic_inc(&global_trace.data[cpu]->disabled);
4038 }
4039
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004040 old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4041
Török Edwinb54d3de2008-11-22 13:28:48 +02004042 /* don't look at user memory in panic mode */
4043 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4044
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004045 printk(KERN_TRACE "Dumping ftrace buffer:\n");
4046
Steven Rostedte543ad72009-03-04 18:20:36 -05004047 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004048 iter.tr = &global_trace;
4049 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05004050 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004051
4052 /*
4053 * We need to stop all tracing on all CPUS to read the
4054 * the next buffer. This is a bit expensive, but is
4055 * not done often. We fill all what we can read,
4056 * and then release the locks again.
4057 */
4058
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004059 while (!trace_empty(&iter)) {
4060
4061 if (!cnt)
4062 printk(KERN_TRACE "---------------------------------\n");
4063
4064 cnt++;
4065
4066 /* reset all but tr, trace, and overruns */
4067 memset(&iter.seq, 0,
4068 sizeof(struct trace_iterator) -
4069 offsetof(struct trace_iterator, seq));
4070 iter.iter_flags |= TRACE_FILE_LAT_FMT;
4071 iter.pos = -1;
4072
4073 if (find_next_entry_inc(&iter) != NULL) {
4074 print_trace_line(&iter);
4075 trace_consume(&iter);
4076 }
4077
4078 trace_printk_seq(&iter.seq);
4079 }
4080
4081 if (!cnt)
4082 printk(KERN_TRACE " (ftrace buffer empty)\n");
4083 else
4084 printk(KERN_TRACE "---------------------------------\n");
4085
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004086 /* Re-enable tracing if requested */
4087 if (!disable_tracing) {
4088 trace_flags |= old_userobj;
4089
4090 for_each_tracing_cpu(cpu) {
4091 atomic_dec(&global_trace.data[cpu]->disabled);
4092 }
4093 tracing_on();
4094 }
4095
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004096 out:
4097 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
4098}
4099
Frederic Weisbeckercf586b62009-03-22 05:04:35 +01004100/* By default: disable tracing after the dump */
4101void ftrace_dump(void)
4102{
4103 __ftrace_dump(true);
4104}
4105
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004106__init static int tracer_alloc_buffers(void)
4107{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004108 struct trace_array_cpu *data;
Steven Rostedt73c51622009-03-11 13:42:01 -04004109 int ring_buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004110 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304111 int ret = -ENOMEM;
4112
4113 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4114 goto out;
4115
4116 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4117 goto out_free_buffer_mask;
4118
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004119 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4120 goto out_free_tracing_cpumask;
4121
Steven Rostedt73c51622009-03-11 13:42:01 -04004122 /* To save memory, keep the ring buffer size to its minimum */
4123 if (ring_buffer_expanded)
4124 ring_buf_size = trace_buf_size;
4125 else
4126 ring_buf_size = 1;
4127
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304128 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4129 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004130 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004131
Steven Rostedtab464282008-05-12 21:21:00 +02004132 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt73c51622009-03-11 13:42:01 -04004133 global_trace.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004134 TRACE_BUFFER_FLAGS);
4135 if (!global_trace.buffer) {
4136 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4137 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304138 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004139 }
4140 global_trace.entries = ring_buffer_size(global_trace.buffer);
4141
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304142
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004143#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt73c51622009-03-11 13:42:01 -04004144 max_tr.buffer = ring_buffer_alloc(ring_buf_size,
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004145 TRACE_BUFFER_FLAGS);
4146 if (!max_tr.buffer) {
4147 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4148 WARN_ON(1);
4149 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304150 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004151 }
4152 max_tr.entries = ring_buffer_size(max_tr.buffer);
4153 WARN_ON(max_tr.entries != global_trace.entries);
4154#endif
4155
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004156 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004157 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004158 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004159 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004160 }
4161
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004162 trace_init_cmdlines();
4163
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004164 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004165 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004166#ifdef CONFIG_BOOT_TRACER
4167 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004168#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004169 /* All seems OK, enable tracing */
4170 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004171
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004172 atomic_notifier_chain_register(&panic_notifier_list,
4173 &trace_panic_notifier);
4174
4175 register_die_notifier(&trace_die_notifier);
Frederic Weisbecker2fc1dfb2009-03-16 01:45:03 +01004176
4177 return 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004178
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304179out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004180 free_cpumask_var(tracing_reader_cpumask);
4181out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304182 free_cpumask_var(tracing_cpumask);
4183out_free_buffer_mask:
4184 free_cpumask_var(tracing_buffer_mask);
4185out:
4186 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004187}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004188
4189__init static int clear_boot_tracer(void)
4190{
4191 /*
4192 * The default tracer at boot buffer is an init section.
4193 * This function is called in lateinit. If we did not
4194 * find the boot tracer, then clear it out, to prevent
4195 * later registration from accessing the buffer that is
4196 * about to be freed.
4197 */
4198 if (!default_bootup_tracer)
4199 return 0;
4200
4201 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4202 default_bootup_tracer);
4203 default_bootup_tracer = NULL;
4204
4205 return 0;
4206}
4207
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004208early_initcall(tracer_alloc_buffers);
4209fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004210late_initcall(clear_boot_tracer);