blob: 881a94474d79d9a65697f4c135efbaa2ebd8ccdc [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 */
14#include <linux/utsrelease.h>
15#include <linux/kallsyms.h>
16#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040017#include <linux/notifier.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020018#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020019#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020020#include <linux/hardirq.h>
21#include <linux/linkage.h>
22#include <linux/uaccess.h>
23#include <linux/ftrace.h>
24#include <linux/module.h>
25#include <linux/percpu.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040026#include <linux/kdebug.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020027#include <linux/ctype.h>
28#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020029#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020030#include <linux/gfp.h>
31#include <linux/fs.h>
Abhishek Sagar76094a22008-05-28 00:03:18 +053032#include <linux/kprobes.h>
Steven Rostedt3eefae92008-05-12 21:21:04 +020033#include <linux/writeback.h>
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +020034#include <linux/splice.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020035
Ingo Molnar86387f72008-05-12 21:20:51 +020036#include <linux/stacktrace.h>
Steven Rostedt3928a8a2008-09-29 23:02:41 -040037#include <linux/ring_buffer.h>
Frederic Weisbecker21798a82008-10-28 09:43:26 +010038#include <linux/irqflags.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/*
49 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010050 * A selftest will lurk into the ring-buffer to count the
51 * entries inserted during the selftest although some concurrent
52 * insertions into the ring-buffer such as ftrace_printk could occurred
53 * at the same time, giving false positive or negative results.
54 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010055static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010056
Steven Rostedtb2821ae2009-02-02 21:38:32 -050057/*
58 * If a tracer is running, we do not want to run SELFTEST.
59 */
60static bool __read_mostly tracing_selftest_disabled;
61
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010062/* For tracers that don't implement custom flags */
63static struct tracer_opt dummy_tracer_opt[] = {
64 { }
65};
66
67static struct tracer_flags dummy_tracer_flags = {
68 .val = 0,
69 .opts = dummy_tracer_opt
70};
71
72static int dummy_set_flag(u32 old_flags, u32 bit, int set)
73{
74 return 0;
75}
Steven Rostedt0f048702008-11-05 16:05:44 -050076
77/*
78 * Kill all tracing for good (never come back).
79 * It is initialized to 1 but will turn to zero if the initialization
80 * of the tracer is successful. But that is the only place that sets
81 * this back to zero.
82 */
Hannes Eder4fd27352009-02-10 19:44:12 +010083static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050084
Steven Rostedtd7690412008-10-01 00:29:53 -040085static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
86
87static inline void ftrace_disable_cpu(void)
88{
89 preempt_disable();
90 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
91}
92
93static inline void ftrace_enable_cpu(void)
94{
95 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
96 preempt_enable();
97}
98
Rusty Russell9e01c1b2009-01-01 10:12:22 +103099static cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +0200100
101#define for_each_tracing_cpu(cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030102 for_each_cpu(cpu, tracing_buffer_mask)
Steven Rostedtab464282008-05-12 21:21:00 +0200103
Steven Rostedt944ac422008-10-23 19:26:08 -0400104/*
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
106 *
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
111 * serial console.
112 *
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
115 * /proc/sys/kernel/ftrace_dump_on_oops to true.
116 */
117int ftrace_dump_on_oops;
118
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500119static int tracing_set_tracer(const char *buf);
120
121#define BOOTUP_TRACER_SIZE 100
122static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
123static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100124
125static int __init set_ftrace(char *str)
126{
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500127 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
128 default_bootup_tracer = bootup_tracer_buf;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100129 return 1;
130}
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500131__setup("ftrace=", set_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100132
Steven Rostedt944ac422008-10-23 19:26:08 -0400133static int __init set_ftrace_dump_on_oops(char *str)
134{
135 ftrace_dump_on_oops = 1;
136 return 1;
137}
138__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200139
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200140long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200141ns2usecs(cycle_t nsec)
142{
143 nsec += 500;
144 do_div(nsec, 1000);
145 return nsec;
146}
147
Ingo Molnare309b412008-05-12 21:20:51 +0200148cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200149{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400150 u64 ts = ring_buffer_time_stamp(cpu);
151 ring_buffer_normalize_time_stamp(cpu, &ts);
152 return ts;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200153}
154
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200155/*
156 * The global_trace is the descriptor that holds the tracing
157 * buffers for the live tracing. For each CPU, it contains
158 * a link list of pages that will store trace entries. The
159 * page descriptor of the pages in the memory is used to hold
160 * the link list by linking the lru item in the page descriptor
161 * to each of the pages in the buffer per CPU.
162 *
163 * For each active CPU there is a data field that holds the
164 * pages for the buffer for that CPU. Each CPU has the same number
165 * of pages allocated for its buffer.
166 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200167static struct trace_array global_trace;
168
169static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
170
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200171/*
172 * The max_tr is used to snapshot the global_trace when a maximum
173 * latency is reached. Some tracers will use this to store a maximum
174 * trace while it continues examining live traces.
175 *
176 * The buffers for the max_tr are set up the same as the global_trace.
177 * When a snapshot is taken, the link list of the max_tr is swapped
178 * with the link list of the global_trace and the buffers are reset for
179 * the global_trace so the tracing can continue.
180 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200181static struct trace_array max_tr;
182
183static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
184
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200185/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200186static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200187
Steven Rostedt90369902008-11-05 16:05:44 -0500188/**
189 * tracing_is_enabled - return tracer_enabled status
190 *
191 * This function is used by other tracers to know the status
192 * of the tracer_enabled flag. Tracers may use this function
193 * to know if it should enable their features when starting
194 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
195 */
196int tracing_is_enabled(void)
197{
198 return tracer_enabled;
199}
200
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200201/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400202 * trace_buf_size is the size in bytes that is allocated
203 * for a buffer. Note, the number of bytes is always rounded
204 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400205 *
206 * This number is purposely set to a low number of 16384.
207 * If the dump on oops happens, it will be much appreciated
208 * to not have to wait for all that output. Anyway this can be
209 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200210 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400211#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400212
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400213static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200214
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200215/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200216static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200217
218/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200219static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200220
221/*
222 * max_tracer_type_len is used to simplify the allocating of
223 * buffers to read userspace tracer names. We keep track of
224 * the longest tracer name registered.
225 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200226static int max_tracer_type_len;
227
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200228/*
229 * trace_types_lock is used to protect the trace_types list.
230 * This lock is also used to keep user access serialized.
231 * Accesses from userspace will grab this lock while userspace
232 * activities happen inside the kernel.
233 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200234static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200235
236/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200237static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
238
Steven Rostedtee6bce52008-11-12 17:52:37 -0500239/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500240unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200241 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
Ingo Molnar4e655512008-05-12 21:20:52 +0200242
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200243/**
244 * trace_wake_up - wake up tasks waiting for trace input
245 *
246 * Simply wakes up any task that is blocked on the trace_wait
247 * queue. These is used with trace_poll for tasks polling the trace.
248 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200249void trace_wake_up(void)
250{
Ingo Molnar017730c2008-05-12 21:20:52 +0200251 /*
252 * The runqueue_is_locked() can fail, but this is the best we
253 * have for now:
254 */
255 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200256 wake_up(&trace_wait);
257}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200258
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400259static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200260{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400261 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200262 int ret;
263
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200264 if (!str)
265 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400266 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200267 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400268 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200269 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400270 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200271 return 1;
272}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400273__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200274
Steven Rostedt57f50be2008-05-12 21:20:44 +0200275unsigned long nsecs_to_usecs(unsigned long nsecs)
276{
277 return nsecs / 1000;
278}
279
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200280/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200281static const char *trace_options[] = {
282 "print-parent",
283 "sym-offset",
284 "sym-addr",
285 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200286 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200287 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200288 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200289 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200290 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200291 "sched-tree",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200292 "ftrace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500293 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500294 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500295 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200296 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200297 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100298 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200299 "context-info",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200300 NULL
301};
302
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200303/*
304 * ftrace_max_lock is used to protect the swapping of buffers
305 * when taking a max snapshot. The buffers themselves are
306 * protected by per_cpu spinlocks. But the action of the swap
307 * needs its own lock.
308 *
309 * This is defined as a raw_spinlock_t in order to help
310 * with performance when lockdep debugging is enabled.
311 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200312static raw_spinlock_t ftrace_max_lock =
313 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200314
315/*
316 * Copy the new maximum trace into the separate maximum-trace
317 * structure. (this way the maximum trace is permanently saved,
318 * for later retrieval via /debugfs/tracing/latency_trace)
319 */
Ingo Molnare309b412008-05-12 21:20:51 +0200320static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200321__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
322{
323 struct trace_array_cpu *data = tr->data[cpu];
324
325 max_tr.cpu = cpu;
326 max_tr.time_start = data->preempt_timestamp;
327
328 data = max_tr.data[cpu];
329 data->saved_latency = tracing_max_latency;
330
331 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
332 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100333 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200334 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
335 data->policy = tsk->policy;
336 data->rt_priority = tsk->rt_priority;
337
338 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500339 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200340}
341
Ingo Molnare309b412008-05-12 21:20:51 +0200342static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200343trace_seq_reset(struct trace_seq *s)
344{
345 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200346 s->readpos = 0;
347}
348
349ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
350{
351 int len;
352 int ret;
353
354 if (s->len <= s->readpos)
355 return -EBUSY;
356
357 len = s->len - s->readpos;
358 if (cnt > len)
359 cnt = len;
360 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
361 if (ret)
362 return -EFAULT;
363
364 s->readpos += len;
365 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200366}
367
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200368ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
369{
370 int len;
371 void *ret;
372
373 if (s->len <= s->readpos)
374 return -EBUSY;
375
376 len = s->len - s->readpos;
377 if (cnt > len)
378 cnt = len;
379 ret = memcpy(buf, s->buffer + s->readpos, cnt);
380 if (!ret)
381 return -EFAULT;
382
383 s->readpos += len;
384 return cnt;
385}
386
Ingo Molnare309b412008-05-12 21:20:51 +0200387static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200388trace_print_seq(struct seq_file *m, struct trace_seq *s)
389{
390 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
391
392 s->buffer[len] = 0;
393 seq_puts(m, s->buffer);
394
395 trace_seq_reset(s);
396}
397
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200398/**
399 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
400 * @tr: tracer
401 * @tsk: the task with the latency
402 * @cpu: The cpu that initiated the trace.
403 *
404 * Flip the buffers between the @tr and the max_tr and record information
405 * about which task was the cause of this latency.
406 */
Ingo Molnare309b412008-05-12 21:20:51 +0200407void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200408update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
409{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400410 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200411
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200412 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200413 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400414
415 tr->buffer = max_tr.buffer;
416 max_tr.buffer = buf;
417
Steven Rostedtd7690412008-10-01 00:29:53 -0400418 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400419 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400420 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200421
422 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200423 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200424}
425
426/**
427 * update_max_tr_single - only copy one trace over, and reset the rest
428 * @tr - tracer
429 * @tsk - task with the latency
430 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200431 *
432 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200433 */
Ingo Molnare309b412008-05-12 21:20:51 +0200434void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200435update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
436{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400437 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200438
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200439 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200440 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200441
Steven Rostedtd7690412008-10-01 00:29:53 -0400442 ftrace_disable_cpu();
443
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400444 ring_buffer_reset(max_tr.buffer);
445 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
446
Steven Rostedtd7690412008-10-01 00:29:53 -0400447 ftrace_enable_cpu();
448
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500449 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200450
451 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200452 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200453}
454
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200455/**
456 * register_tracer - register a tracer with the ftrace system.
457 * @type - the plugin for the tracer
458 *
459 * Register a new plugin tracer.
460 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200461int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100462__releases(kernel_lock)
463__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200464{
465 struct tracer *t;
466 int len;
467 int ret = 0;
468
469 if (!type->name) {
470 pr_info("Tracer must have a name\n");
471 return -1;
472 }
473
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100474 /*
475 * When this gets called we hold the BKL which means that
476 * preemption is disabled. Various trace selftests however
477 * need to disable and enable preemption for successful tests.
478 * So we drop the BKL here and grab it after the tests again.
479 */
480 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200481 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100482
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100483 tracing_selftest_running = true;
484
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200485 for (t = trace_types; t; t = t->next) {
486 if (strcmp(type->name, t->name) == 0) {
487 /* already found */
488 pr_info("Trace %s already registered\n",
489 type->name);
490 ret = -1;
491 goto out;
492 }
493 }
494
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100495 if (!type->set_flag)
496 type->set_flag = &dummy_set_flag;
497 if (!type->flags)
498 type->flags = &dummy_tracer_flags;
499 else
500 if (!type->flags->opts)
501 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100502 if (!type->wait_pipe)
503 type->wait_pipe = default_wait_pipe;
504
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100505
Steven Rostedt60a11772008-05-12 21:20:44 +0200506#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500507 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200508 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200509 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200510 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100511
Steven Rostedt60a11772008-05-12 21:20:44 +0200512 /*
513 * Run a selftest on this tracer.
514 * Here we reset the trace buffer, and set the current
515 * tracer to be this tracer. The tracer can then run some
516 * internal tracing to verify that everything is in order.
517 * If we fail, we do not register this tracer.
518 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100519 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400520 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100521
Steven Rostedt60a11772008-05-12 21:20:44 +0200522 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200523 /* the test is responsible for initializing and enabling */
524 pr_info("Testing tracer %s: ", type->name);
525 ret = type->selftest(type, tr);
526 /* the test is responsible for resetting too */
527 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200528 if (ret) {
529 printk(KERN_CONT "FAILED!\n");
530 goto out;
531 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200532 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100533 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400534 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100535
Steven Rostedt60a11772008-05-12 21:20:44 +0200536 printk(KERN_CONT "PASSED\n");
537 }
538#endif
539
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200540 type->next = trace_types;
541 trace_types = type;
542 len = strlen(type->name);
543 if (len > max_tracer_type_len)
544 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200545
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200546 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100547 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200548 mutex_unlock(&trace_types_lock);
549
Steven Rostedtdac74942009-02-05 01:13:38 -0500550 if (ret || !default_bootup_tracer)
551 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500552
Steven Rostedtdac74942009-02-05 01:13:38 -0500553 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
554 goto out_unlock;
555
556 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
557 /* Do we want this tracer to start on bootup? */
558 tracing_set_tracer(type->name);
559 default_bootup_tracer = NULL;
560 /* disable other selftests, since this will break it. */
561 tracing_selftest_disabled = 1;
562#ifdef CONFIG_FTRACE_STARTUP_TEST
563 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
564 type->name);
565#endif
566
567 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500568 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200569 return ret;
570}
571
572void unregister_tracer(struct tracer *type)
573{
574 struct tracer **t;
575 int len;
576
577 mutex_lock(&trace_types_lock);
578 for (t = &trace_types; *t; t = &(*t)->next) {
579 if (*t == type)
580 goto found;
581 }
582 pr_info("Trace %s not registered\n", type->name);
583 goto out;
584
585 found:
586 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200587
588 if (type == current_trace && tracer_enabled) {
589 tracer_enabled = 0;
590 tracing_stop();
591 if (current_trace->stop)
592 current_trace->stop(&global_trace);
593 current_trace = &nop_trace;
594 }
595
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200596 if (strlen(type->name) != max_tracer_type_len)
597 goto out;
598
599 max_tracer_type_len = 0;
600 for (t = &trace_types; *t; t = &(*t)->next) {
601 len = strlen((*t)->name);
602 if (len > max_tracer_type_len)
603 max_tracer_type_len = len;
604 }
605 out:
606 mutex_unlock(&trace_types_lock);
607}
608
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400609void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200610{
Steven Rostedtd7690412008-10-01 00:29:53 -0400611 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400612 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400613 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200614}
615
Pekka J Enberg213cc062008-12-19 12:08:39 +0200616void tracing_reset_online_cpus(struct trace_array *tr)
617{
618 int cpu;
619
620 tr->time_start = ftrace_now(tr->cpu);
621
622 for_each_online_cpu(cpu)
623 tracing_reset(tr, cpu);
624}
625
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200626#define SAVED_CMDLINES 128
627static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
628static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
629static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
630static int cmdline_idx;
631static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200632
Steven Rostedt25b0b442008-05-12 21:21:00 +0200633/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100634static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200635
636static void trace_init_cmdlines(void)
637{
638 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
639 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
640 cmdline_idx = 0;
641}
642
Steven Rostedt0f048702008-11-05 16:05:44 -0500643static int trace_stop_count;
644static DEFINE_SPINLOCK(tracing_start_lock);
645
646/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500647 * ftrace_off_permanent - disable all ftrace code permanently
648 *
649 * This should only be called when a serious anomally has
650 * been detected. This will turn off the function tracing,
651 * ring buffers, and other tracing utilites. It takes no
652 * locks and can be called from any context.
653 */
654void ftrace_off_permanent(void)
655{
656 tracing_disabled = 1;
657 ftrace_stop();
658 tracing_off_permanent();
659}
660
661/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500662 * tracing_start - quick start of the tracer
663 *
664 * If tracing is enabled but was stopped by tracing_stop,
665 * this will start the tracer back up.
666 */
667void tracing_start(void)
668{
669 struct ring_buffer *buffer;
670 unsigned long flags;
671
672 if (tracing_disabled)
673 return;
674
675 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500676 if (--trace_stop_count) {
677 if (trace_stop_count < 0) {
678 /* Someone screwed up their debugging */
679 WARN_ON_ONCE(1);
680 trace_stop_count = 0;
681 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500682 goto out;
683 }
684
685
686 buffer = global_trace.buffer;
687 if (buffer)
688 ring_buffer_record_enable(buffer);
689
690 buffer = max_tr.buffer;
691 if (buffer)
692 ring_buffer_record_enable(buffer);
693
694 ftrace_start();
695 out:
696 spin_unlock_irqrestore(&tracing_start_lock, flags);
697}
698
699/**
700 * tracing_stop - quick stop of the tracer
701 *
702 * Light weight way to stop tracing. Use in conjunction with
703 * tracing_start.
704 */
705void tracing_stop(void)
706{
707 struct ring_buffer *buffer;
708 unsigned long flags;
709
710 ftrace_stop();
711 spin_lock_irqsave(&tracing_start_lock, flags);
712 if (trace_stop_count++)
713 goto out;
714
715 buffer = global_trace.buffer;
716 if (buffer)
717 ring_buffer_record_disable(buffer);
718
719 buffer = max_tr.buffer;
720 if (buffer)
721 ring_buffer_record_disable(buffer);
722
723 out:
724 spin_unlock_irqrestore(&tracing_start_lock, flags);
725}
726
Ingo Molnare309b412008-05-12 21:20:51 +0200727void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200728
Ingo Molnare309b412008-05-12 21:20:51 +0200729static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730{
731 unsigned map;
732 unsigned idx;
733
734 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
735 return;
736
737 /*
738 * It's not the end of the world if we don't get
739 * the lock, but we also don't want to spin
740 * nor do we want to disable interrupts,
741 * so if we miss here, then better luck next time.
742 */
743 if (!spin_trylock(&trace_cmdline_lock))
744 return;
745
746 idx = map_pid_to_cmdline[tsk->pid];
747 if (idx >= SAVED_CMDLINES) {
748 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
749
750 map = map_cmdline_to_pid[idx];
751 if (map <= PID_MAX_DEFAULT)
752 map_pid_to_cmdline[map] = (unsigned)-1;
753
754 map_pid_to_cmdline[tsk->pid] = idx;
755
756 cmdline_idx = idx;
757 }
758
759 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
760
761 spin_unlock(&trace_cmdline_lock);
762}
763
Steven Rostedt660c7f92008-11-26 00:16:26 -0500764char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200765{
766 char *cmdline = "<...>";
767 unsigned map;
768
769 if (!pid)
770 return "<idle>";
771
772 if (pid > PID_MAX_DEFAULT)
773 goto out;
774
775 map = map_pid_to_cmdline[pid];
776 if (map >= SAVED_CMDLINES)
777 goto out;
778
779 cmdline = saved_cmdlines[map];
780
781 out:
782 return cmdline;
783}
784
Ingo Molnare309b412008-05-12 21:20:51 +0200785void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200786{
787 if (atomic_read(&trace_record_cmdline_disabled))
788 return;
789
790 trace_save_cmdline(tsk);
791}
792
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300793void
Steven Rostedt38697052008-10-01 13:14:09 -0400794tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
795 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200796{
797 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200798
Steven Rostedt777e2082008-09-29 23:02:42 -0400799 entry->preempt_count = pc & 0xff;
800 entry->pid = (tsk) ? tsk->pid : 0;
Török Edwinb54d3de2008-11-22 13:28:48 +0200801 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400802 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400803#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400804 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400805#else
806 TRACE_FLAG_IRQS_NOSUPPORT |
807#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
809 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
810 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
811}
812
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200813struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
814 unsigned char type,
815 unsigned long len,
816 unsigned long flags, int pc)
817{
818 struct ring_buffer_event *event;
819
820 event = ring_buffer_lock_reserve(tr->buffer, len);
821 if (event != NULL) {
822 struct trace_entry *ent = ring_buffer_event_data(event);
823
824 tracing_generic_entry_update(ent, flags, pc);
825 ent->type = type;
826 }
827
828 return event;
829}
830static void ftrace_trace_stack(struct trace_array *tr,
831 unsigned long flags, int skip, int pc);
832static void ftrace_trace_userstack(struct trace_array *tr,
833 unsigned long flags, int pc);
834
835void trace_buffer_unlock_commit(struct trace_array *tr,
836 struct ring_buffer_event *event,
837 unsigned long flags, int pc)
838{
839 ring_buffer_unlock_commit(tr->buffer, event);
840
841 ftrace_trace_stack(tr, flags, 6, pc);
842 ftrace_trace_userstack(tr, flags, pc);
843 trace_wake_up();
844}
845
Ingo Molnare309b412008-05-12 21:20:51 +0200846void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500847trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400848 unsigned long ip, unsigned long parent_ip, unsigned long flags,
849 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200850{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400851 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400852 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200853
Steven Rostedtd7690412008-10-01 00:29:53 -0400854 /* If we are reading the ring buffer, don't trace */
855 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
856 return;
857
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200858 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
859 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400860 if (!event)
861 return;
862 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400863 entry->ip = ip;
864 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200865 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200866}
867
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100868#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100869static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100870 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100871 unsigned long flags,
872 int pc)
873{
874 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100875 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100876
877 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
878 return;
879
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200880 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
881 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100882 if (!event)
883 return;
884 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100885 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200886 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100887}
888
889static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100890 struct ftrace_graph_ret *trace,
891 unsigned long flags,
892 int pc)
893{
894 struct ring_buffer_event *event;
895 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100896
897 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
898 return;
899
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200900 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
901 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100902 if (!event)
903 return;
904 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100905 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200906 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100907}
908#endif
909
Ingo Molnare309b412008-05-12 21:20:51 +0200910void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200911ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400912 unsigned long ip, unsigned long parent_ip, unsigned long flags,
913 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200914{
915 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500916 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200917}
918
Steven Rostedt53614992009-01-15 19:12:40 -0500919static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500920 unsigned long flags,
921 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200922{
Al Viroc2c80522008-10-31 19:50:41 +0000923#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400924 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400925 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200926 struct stack_trace trace;
927
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200928 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
929 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400930 if (!event)
931 return;
932 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400933 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200934
935 trace.nr_entries = 0;
936 trace.max_entries = FTRACE_STACK_ENTRIES;
937 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400938 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200939
940 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200941 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000942#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200943}
944
Steven Rostedt53614992009-01-15 19:12:40 -0500945static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500946 unsigned long flags,
947 int skip, int pc)
948{
949 if (!(trace_flags & TRACE_ITER_STACKTRACE))
950 return;
951
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500952 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500953}
954
Steven Rostedt38697052008-10-01 13:14:09 -0400955void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400956 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500957 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400958{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500959 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400960}
961
Török Edwin02b67512008-11-22 13:28:47 +0200962static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500963 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +0200964{
Török Edwinc7425ac2008-11-28 11:17:56 +0200965#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +0200966 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +0200967 struct userstack_entry *entry;
968 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +0200969
970 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
971 return;
972
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200973 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
974 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +0200975 if (!event)
976 return;
977 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +0200978
979 memset(&entry->caller, 0, sizeof(entry->caller));
980
981 trace.nr_entries = 0;
982 trace.max_entries = FTRACE_STACK_ENTRIES;
983 trace.skip = 0;
984 trace.entries = entry->caller;
985
986 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200987 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +0200988#endif
Török Edwin02b67512008-11-22 13:28:47 +0200989}
990
Hannes Eder4fd27352009-02-10 19:44:12 +0100991#ifdef UNUSED
992static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +0200993{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500994 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +0200995}
Hannes Eder4fd27352009-02-10 19:44:12 +0100996#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +0200997
Steven Rostedt38697052008-10-01 13:14:09 -0400998static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500999ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001000 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1001 int pc)
Ingo Molnara4feb832008-05-12 21:21:02 +02001002{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001003 struct ring_buffer_event *event;
Ingo Molnara4feb832008-05-12 21:21:02 +02001004 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001005 struct special_entry *entry;
Ingo Molnara4feb832008-05-12 21:21:02 +02001006
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001007 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1008 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001009 if (!event)
1010 return;
1011 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001012 entry->arg1 = arg1;
1013 entry->arg2 = arg2;
1014 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001015 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb832008-05-12 21:21:02 +02001016}
1017
1018void
Steven Rostedt38697052008-10-01 13:14:09 -04001019__trace_special(void *__tr, void *__data,
1020 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1021{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001022 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001023}
1024
1025void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001026tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001027 struct task_struct *prev,
1028 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001029 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001030{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001031 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001032 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001033
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001034 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1035 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001036 if (!event)
1037 return;
1038 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001039 entry->prev_pid = prev->pid;
1040 entry->prev_prio = prev->prio;
1041 entry->prev_state = prev->state;
1042 entry->next_pid = next->pid;
1043 entry->next_prio = next->prio;
1044 entry->next_state = next->state;
1045 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001046 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001047}
1048
Ingo Molnar57422792008-05-12 21:20:51 +02001049void
1050tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001051 struct task_struct *wakee,
1052 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001053 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001054{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001055 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001056 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001057
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001058 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1059 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001060 if (!event)
1061 return;
1062 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001063 entry->prev_pid = curr->pid;
1064 entry->prev_prio = curr->prio;
1065 entry->prev_state = curr->state;
1066 entry->next_pid = wakee->pid;
1067 entry->next_prio = wakee->prio;
1068 entry->next_state = wakee->state;
1069 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001070
1071 ring_buffer_unlock_commit(tr->buffer, event);
1072 ftrace_trace_stack(tr, flags, 6, pc);
1073 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001074}
1075
Steven Rostedt4902f882008-05-22 00:22:18 -04001076void
1077ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1078{
1079 struct trace_array *tr = &global_trace;
1080 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001081 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001082 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001083 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001084
Steven Rostedtc76f0692008-11-07 22:36:02 -05001085 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001086 return;
1087
Steven Rostedt38697052008-10-01 13:14:09 -04001088 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001089 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001090 cpu = raw_smp_processor_id();
1091 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001092
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001093 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001094 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001095
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001096 atomic_dec(&data->disabled);
1097 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001098}
1099
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001100#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001101int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001102{
1103 struct trace_array *tr = &global_trace;
1104 struct trace_array_cpu *data;
1105 unsigned long flags;
1106 long disabled;
1107 int cpu;
1108 int pc;
1109
Steven Rostedt804a6852008-12-03 15:36:59 -05001110 if (!ftrace_trace_task(current))
1111 return 0;
1112
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001113 if (!ftrace_graph_addr(trace->func))
1114 return 0;
1115
Steven Rostedta5e25882008-12-02 15:34:05 -05001116 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001117 cpu = raw_smp_processor_id();
1118 data = tr->data[cpu];
1119 disabled = atomic_inc_return(&data->disabled);
1120 if (likely(disabled == 1)) {
1121 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001122 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001123 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001124 /* Only do the atomic if it is not already set */
1125 if (!test_tsk_trace_graph(current))
1126 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001127 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001128 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001129
1130 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001131}
1132
1133void trace_graph_return(struct ftrace_graph_ret *trace)
1134{
1135 struct trace_array *tr = &global_trace;
1136 struct trace_array_cpu *data;
1137 unsigned long flags;
1138 long disabled;
1139 int cpu;
1140 int pc;
1141
Steven Rostedta5e25882008-12-02 15:34:05 -05001142 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001143 cpu = raw_smp_processor_id();
1144 data = tr->data[cpu];
1145 disabled = atomic_inc_return(&data->disabled);
1146 if (likely(disabled == 1)) {
1147 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001148 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001149 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001150 if (!trace->depth)
1151 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001152 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001153 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001154}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001155#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001156
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001157enum trace_file_type {
1158 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001159 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001160};
1161
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001162static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001163{
Steven Rostedtd7690412008-10-01 00:29:53 -04001164 /* Don't allow ftrace to trace into the ring buffers */
1165 ftrace_disable_cpu();
1166
Steven Rostedt5a90f572008-09-03 17:42:51 -04001167 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001168 if (iter->buffer_iter[iter->cpu])
1169 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1170
1171 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001172}
1173
Ingo Molnare309b412008-05-12 21:20:51 +02001174static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001175peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001176{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001177 struct ring_buffer_event *event;
1178 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001179
Steven Rostedtd7690412008-10-01 00:29:53 -04001180 /* Don't allow ftrace to trace into the ring buffers */
1181 ftrace_disable_cpu();
1182
1183 if (buf_iter)
1184 event = ring_buffer_iter_peek(buf_iter, ts);
1185 else
1186 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1187
1188 ftrace_enable_cpu();
1189
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001190 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001191}
Steven Rostedtd7690412008-10-01 00:29:53 -04001192
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001193static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001194__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001195{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001196 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197 struct trace_entry *ent, *next = NULL;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001198 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001199 int next_cpu = -1;
1200 int cpu;
1201
Steven Rostedtab464282008-05-12 21:21:00 +02001202 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001203
1204 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001205 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001206
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001207 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001208
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001209 /*
1210 * Pick the entry with the smallest timestamp:
1211 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001212 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001213 next = ent;
1214 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001215 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001216 }
1217 }
1218
1219 if (ent_cpu)
1220 *ent_cpu = next_cpu;
1221
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001222 if (ent_ts)
1223 *ent_ts = next_ts;
1224
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001225 return next;
1226}
1227
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001228/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001229struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1230 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001231{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001232 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001233}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001234
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001235/* Find the next real entry, and increment the iterator to the next entry */
1236static void *find_next_entry_inc(struct trace_iterator *iter)
1237{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001238 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001239
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001240 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001241 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001242
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001243 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001244}
1245
Ingo Molnare309b412008-05-12 21:20:51 +02001246static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001247{
Steven Rostedtd7690412008-10-01 00:29:53 -04001248 /* Don't allow ftrace to trace into the ring buffers */
1249 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001250 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001251 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001252}
1253
Ingo Molnare309b412008-05-12 21:20:51 +02001254static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001255{
1256 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001257 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001258 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001259
1260 (*pos)++;
1261
1262 /* can't go backwards */
1263 if (iter->idx > i)
1264 return NULL;
1265
1266 if (iter->idx < 0)
1267 ent = find_next_entry_inc(iter);
1268 else
1269 ent = iter;
1270
1271 while (ent && iter->idx < i)
1272 ent = find_next_entry_inc(iter);
1273
1274 iter->pos = *pos;
1275
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276 return ent;
1277}
1278
1279static void *s_start(struct seq_file *m, loff_t *pos)
1280{
1281 struct trace_iterator *iter = m->private;
1282 void *p = NULL;
1283 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001284 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001285
1286 mutex_lock(&trace_types_lock);
1287
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001288 if (!current_trace || current_trace != iter->trace) {
1289 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001291 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001292
1293 atomic_inc(&trace_record_cmdline_disabled);
1294
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001295 if (*pos != iter->pos) {
1296 iter->ent = NULL;
1297 iter->cpu = 0;
1298 iter->idx = -1;
1299
Steven Rostedtd7690412008-10-01 00:29:53 -04001300 ftrace_disable_cpu();
1301
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001302 for_each_tracing_cpu(cpu) {
1303 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001304 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001305
Steven Rostedtd7690412008-10-01 00:29:53 -04001306 ftrace_enable_cpu();
1307
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1309 ;
1310
1311 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001312 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001313 p = s_next(m, p, &l);
1314 }
1315
1316 return p;
1317}
1318
1319static void s_stop(struct seq_file *m, void *p)
1320{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001322 mutex_unlock(&trace_types_lock);
1323}
1324
Ingo Molnare309b412008-05-12 21:20:51 +02001325static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001326{
Michael Ellermana6168352008-08-20 16:36:11 -07001327 seq_puts(m, "# _------=> CPU# \n");
1328 seq_puts(m, "# / _-----=> irqs-off \n");
1329 seq_puts(m, "# | / _----=> need-resched \n");
1330 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1331 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1332 seq_puts(m, "# |||| / \n");
1333 seq_puts(m, "# ||||| delay \n");
1334 seq_puts(m, "# cmd pid ||||| time | caller \n");
1335 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001336}
1337
Ingo Molnare309b412008-05-12 21:20:51 +02001338static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001339{
Michael Ellermana6168352008-08-20 16:36:11 -07001340 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1341 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001342}
1343
1344
Ingo Molnare309b412008-05-12 21:20:51 +02001345static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001346print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1347{
1348 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1349 struct trace_array *tr = iter->tr;
1350 struct trace_array_cpu *data = tr->data[tr->cpu];
1351 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001352 unsigned long total;
1353 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001354 const char *name = "preemption";
1355
1356 if (type)
1357 name = type->name;
1358
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001359 entries = ring_buffer_entries(iter->tr->buffer);
1360 total = entries +
1361 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362
1363 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1364 name, UTS_RELEASE);
1365 seq_puts(m, "-----------------------------------"
1366 "---------------------------------\n");
1367 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1368 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001369 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001371 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001372 tr->cpu,
1373#if defined(CONFIG_PREEMPT_NONE)
1374 "server",
1375#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1376 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001377#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001378 "preempt",
1379#else
1380 "unknown",
1381#endif
1382 /* These are reserved for later use */
1383 0, 0, 0, 0);
1384#ifdef CONFIG_SMP
1385 seq_printf(m, " #P:%d)\n", num_online_cpus());
1386#else
1387 seq_puts(m, ")\n");
1388#endif
1389 seq_puts(m, " -----------------\n");
1390 seq_printf(m, " | task: %.16s-%d "
1391 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1392 data->comm, data->pid, data->uid, data->nice,
1393 data->policy, data->rt_priority);
1394 seq_puts(m, " -----------------\n");
1395
1396 if (data->critical_start) {
1397 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001398 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1399 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001400 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001401 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1402 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001403 seq_puts(m, "\n");
1404 }
1405
1406 seq_puts(m, "\n");
1407}
1408
Steven Rostedta3097202008-11-07 22:36:02 -05001409static void test_cpu_buff_start(struct trace_iterator *iter)
1410{
1411 struct trace_seq *s = &iter->seq;
1412
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001413 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1414 return;
1415
1416 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1417 return;
1418
Rusty Russell44623442009-01-01 10:12:23 +10301419 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001420 return;
1421
Rusty Russell44623442009-01-01 10:12:23 +10301422 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001423 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1424}
1425
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001426static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427{
Steven Rostedt214023c2008-05-12 21:20:46 +02001428 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001429 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedtf633cef2008-12-23 23:24:13 -05001430 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001431 struct trace_entry *entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001432
Steven Rostedta3097202008-11-07 22:36:02 -05001433 test_cpu_buff_start(iter);
1434
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001435 event = ftrace_find_event(entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001436
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001437 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001438 if (!trace_print_lat_context(iter))
1439 goto partial;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001440 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001441
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001442 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001443 return event->latency_trace(iter, sym_flags);
Steven Rostedt7104f302008-10-01 10:52:51 -04001444
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001445 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1446 goto partial;
1447
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001448 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001449partial:
1450 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001451}
1452
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001453static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001454{
Steven Rostedt214023c2008-05-12 21:20:46 +02001455 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001456 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001457 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001458 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001459
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001460 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001461
Steven Rostedta3097202008-11-07 22:36:02 -05001462 test_cpu_buff_start(iter);
1463
Steven Rostedtf633cef2008-12-23 23:24:13 -05001464 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001465
1466 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001467 if (!trace_print_context(iter))
1468 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001469 }
1470
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001471 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001472 return event->trace(iter, sym_flags);
1473
1474 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1475 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001476
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001477 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001478partial:
1479 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480}
1481
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001482static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001483{
1484 struct trace_seq *s = &iter->seq;
1485 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001486 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001487
1488 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001489
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001490 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001491 if (!trace_seq_printf(s, "%d %d %llu ",
1492 entry->pid, iter->cpu, iter->ts))
1493 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001494 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001495
Steven Rostedtf633cef2008-12-23 23:24:13 -05001496 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001497 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001498 return event->raw(iter, 0);
1499
1500 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1501 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001502
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001503 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001504partial:
1505 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001506}
1507
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001508static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001509{
1510 struct trace_seq *s = &iter->seq;
1511 unsigned char newline = '\n';
1512 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001513 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001514
1515 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001516
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001517 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1518 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1519 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1520 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1521 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001522
Steven Rostedtf633cef2008-12-23 23:24:13 -05001523 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001524 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001525 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001526 if (ret != TRACE_TYPE_HANDLED)
1527 return ret;
1528 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001529
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001530 SEQ_PUT_FIELD_RET(s, newline);
1531
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001532 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001533}
1534
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001535static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1536{
1537 struct trace_seq *s = &iter->seq;
1538 struct trace_entry *entry = iter->ent;
1539 struct print_entry *field;
1540 int ret;
1541
1542 trace_assign_type(field, entry);
1543
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001544 ret = trace_seq_printf(s, "%s", field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001545 if (!ret)
1546 return TRACE_TYPE_PARTIAL_LINE;
1547
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001548 return TRACE_TYPE_HANDLED;
1549}
1550
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001551static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001552{
1553 struct trace_seq *s = &iter->seq;
1554 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001555 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001556
1557 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001558
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001559 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1560 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001561 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001562 SEQ_PUT_FIELD_RET(s, iter->ts);
1563 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001564
Steven Rostedtf633cef2008-12-23 23:24:13 -05001565 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001566 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001567}
1568
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001569static int trace_empty(struct trace_iterator *iter)
1570{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001571 int cpu;
1572
Steven Rostedtab464282008-05-12 21:21:00 +02001573 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001574 if (iter->buffer_iter[cpu]) {
1575 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1576 return 0;
1577 } else {
1578 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1579 return 0;
1580 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001581 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001582
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001583 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001584}
1585
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001586static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001587{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001588 enum print_line_t ret;
1589
1590 if (iter->trace && iter->trace->print_line) {
1591 ret = iter->trace->print_line(iter);
1592 if (ret != TRACE_TYPE_UNHANDLED)
1593 return ret;
1594 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001595
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001596 if (iter->ent->type == TRACE_PRINT &&
1597 trace_flags & TRACE_ITER_PRINTK &&
1598 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1599 return print_printk_msg_only(iter);
1600
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001601 if (trace_flags & TRACE_ITER_BIN)
1602 return print_bin_fmt(iter);
1603
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001604 if (trace_flags & TRACE_ITER_HEX)
1605 return print_hex_fmt(iter);
1606
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001607 if (trace_flags & TRACE_ITER_RAW)
1608 return print_raw_fmt(iter);
1609
1610 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001611 return print_lat_fmt(iter);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001612
1613 return print_trace_fmt(iter);
1614}
1615
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001616static int s_show(struct seq_file *m, void *v)
1617{
1618 struct trace_iterator *iter = v;
1619
1620 if (iter->ent == NULL) {
1621 if (iter->tr) {
1622 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1623 seq_puts(m, "#\n");
1624 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001625 if (iter->trace && iter->trace->print_header)
1626 iter->trace->print_header(m);
1627 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001628 /* print nothing if the buffers are empty */
1629 if (trace_empty(iter))
1630 return 0;
1631 print_trace_header(m, iter);
1632 if (!(trace_flags & TRACE_ITER_VERBOSE))
1633 print_lat_help_header(m);
1634 } else {
1635 if (!(trace_flags & TRACE_ITER_VERBOSE))
1636 print_func_help_header(m);
1637 }
1638 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001639 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001640 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001641 }
1642
1643 return 0;
1644}
1645
1646static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001647 .start = s_start,
1648 .next = s_next,
1649 .stop = s_stop,
1650 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001651};
1652
Ingo Molnare309b412008-05-12 21:20:51 +02001653static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001654__tracing_open(struct inode *inode, struct file *file, int *ret)
1655{
1656 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001657 struct seq_file *m;
1658 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001659
Steven Rostedt60a11772008-05-12 21:20:44 +02001660 if (tracing_disabled) {
1661 *ret = -ENODEV;
1662 return NULL;
1663 }
1664
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001665 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1666 if (!iter) {
1667 *ret = -ENOMEM;
1668 goto out;
1669 }
1670
1671 mutex_lock(&trace_types_lock);
1672 if (current_trace && current_trace->print_max)
1673 iter->tr = &max_tr;
1674 else
1675 iter->tr = inode->i_private;
1676 iter->trace = current_trace;
1677 iter->pos = -1;
1678
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001679 /* Notify the tracer early; before we stop tracing. */
1680 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001681 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001682
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001683 /* Annotate start of buffers if we had overruns */
1684 if (ring_buffer_overruns(iter->tr->buffer))
1685 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1686
1687
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001688 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001689
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001690 iter->buffer_iter[cpu] =
1691 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001692
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001693 if (!iter->buffer_iter[cpu])
1694 goto fail_buffer;
1695 }
1696
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001697 /* TODO stop tracer */
1698 *ret = seq_open(file, &tracer_seq_ops);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001699 if (*ret)
1700 goto fail_buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001701
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001702 m = file->private_data;
1703 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001704
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001705 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001706 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001707
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001708 mutex_unlock(&trace_types_lock);
1709
1710 out:
1711 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001712
1713 fail_buffer:
1714 for_each_tracing_cpu(cpu) {
1715 if (iter->buffer_iter[cpu])
1716 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1717 }
1718 mutex_unlock(&trace_types_lock);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001719 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001720
1721 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001722}
1723
1724int tracing_open_generic(struct inode *inode, struct file *filp)
1725{
Steven Rostedt60a11772008-05-12 21:20:44 +02001726 if (tracing_disabled)
1727 return -ENODEV;
1728
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001729 filp->private_data = inode->i_private;
1730 return 0;
1731}
1732
Hannes Eder4fd27352009-02-10 19:44:12 +01001733static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001734{
1735 struct seq_file *m = (struct seq_file *)file->private_data;
1736 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001737 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001738
1739 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001740 for_each_tracing_cpu(cpu) {
1741 if (iter->buffer_iter[cpu])
1742 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1743 }
1744
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001745 if (iter->trace && iter->trace->close)
1746 iter->trace->close(iter);
1747
1748 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001749 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001750 mutex_unlock(&trace_types_lock);
1751
1752 seq_release(inode, file);
1753 kfree(iter);
1754 return 0;
1755}
1756
1757static int tracing_open(struct inode *inode, struct file *file)
1758{
1759 int ret;
1760
1761 __tracing_open(inode, file, &ret);
1762
1763 return ret;
1764}
1765
1766static int tracing_lt_open(struct inode *inode, struct file *file)
1767{
1768 struct trace_iterator *iter;
1769 int ret;
1770
1771 iter = __tracing_open(inode, file, &ret);
1772
1773 if (!ret)
1774 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1775
1776 return ret;
1777}
1778
1779
Ingo Molnare309b412008-05-12 21:20:51 +02001780static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001781t_next(struct seq_file *m, void *v, loff_t *pos)
1782{
1783 struct tracer *t = m->private;
1784
1785 (*pos)++;
1786
1787 if (t)
1788 t = t->next;
1789
1790 m->private = t;
1791
1792 return t;
1793}
1794
1795static void *t_start(struct seq_file *m, loff_t *pos)
1796{
1797 struct tracer *t = m->private;
1798 loff_t l = 0;
1799
1800 mutex_lock(&trace_types_lock);
1801 for (; t && l < *pos; t = t_next(m, t, &l))
1802 ;
1803
1804 return t;
1805}
1806
1807static void t_stop(struct seq_file *m, void *p)
1808{
1809 mutex_unlock(&trace_types_lock);
1810}
1811
1812static int t_show(struct seq_file *m, void *v)
1813{
1814 struct tracer *t = v;
1815
1816 if (!t)
1817 return 0;
1818
1819 seq_printf(m, "%s", t->name);
1820 if (t->next)
1821 seq_putc(m, ' ');
1822 else
1823 seq_putc(m, '\n');
1824
1825 return 0;
1826}
1827
1828static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001829 .start = t_start,
1830 .next = t_next,
1831 .stop = t_stop,
1832 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001833};
1834
1835static int show_traces_open(struct inode *inode, struct file *file)
1836{
1837 int ret;
1838
Steven Rostedt60a11772008-05-12 21:20:44 +02001839 if (tracing_disabled)
1840 return -ENODEV;
1841
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001842 ret = seq_open(file, &show_traces_seq_ops);
1843 if (!ret) {
1844 struct seq_file *m = file->private_data;
1845 m->private = trace_types;
1846 }
1847
1848 return ret;
1849}
1850
1851static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001852 .open = tracing_open,
1853 .read = seq_read,
1854 .llseek = seq_lseek,
1855 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001856};
1857
1858static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001859 .open = tracing_lt_open,
1860 .read = seq_read,
1861 .llseek = seq_lseek,
1862 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001863};
1864
1865static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001866 .open = show_traces_open,
1867 .read = seq_read,
1868 .release = seq_release,
1869};
1870
Ingo Molnar36dfe922008-05-12 21:20:52 +02001871/*
1872 * Only trace on a CPU if the bitmask is set:
1873 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301874static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001875
1876/*
1877 * The tracer itself will not take this lock, but still we want
1878 * to provide a consistent cpumask to user-space:
1879 */
1880static DEFINE_MUTEX(tracing_cpumask_update_lock);
1881
1882/*
1883 * Temporary storage for the character representation of the
1884 * CPU bitmask (and one more byte for the newline):
1885 */
1886static char mask_str[NR_CPUS + 1];
1887
Ingo Molnarc7078de2008-05-12 21:20:52 +02001888static ssize_t
1889tracing_cpumask_read(struct file *filp, char __user *ubuf,
1890 size_t count, loff_t *ppos)
1891{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001892 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001893
1894 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001895
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301896 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001897 if (count - len < 2) {
1898 count = -EINVAL;
1899 goto out_err;
1900 }
1901 len += sprintf(mask_str + len, "\n");
1902 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1903
1904out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001905 mutex_unlock(&tracing_cpumask_update_lock);
1906
1907 return count;
1908}
1909
1910static ssize_t
1911tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1912 size_t count, loff_t *ppos)
1913{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001914 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301915 cpumask_var_t tracing_cpumask_new;
1916
1917 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1918 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001919
1920 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301921 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001922 if (err)
1923 goto err_unlock;
1924
Steven Rostedta5e25882008-12-02 15:34:05 -05001925 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02001926 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001927 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001928 /*
1929 * Increase/decrease the disabled counter if we are
1930 * about to flip a bit in the cpumask:
1931 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301932 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1933 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001934 atomic_inc(&global_trace.data[cpu]->disabled);
1935 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301936 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1937 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001938 atomic_dec(&global_trace.data[cpu]->disabled);
1939 }
1940 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001941 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05001942 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001943
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301944 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001945
Ingo Molnarc7078de2008-05-12 21:20:52 +02001946 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301947 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02001948
Ingo Molnarc7078de2008-05-12 21:20:52 +02001949 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001950
1951err_unlock:
1952 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301953 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001954
1955 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001956}
1957
1958static struct file_operations tracing_cpumask_fops = {
1959 .open = tracing_open_generic,
1960 .read = tracing_cpumask_read,
1961 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001962};
1963
1964static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05001965tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001966 size_t cnt, loff_t *ppos)
1967{
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001968 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001969 char *buf;
1970 int r = 0;
1971 int len = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001972 u32 tracer_flags = current_trace->flags->val;
1973 struct tracer_opt *trace_opts = current_trace->flags->opts;
1974
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001975
Wenji Huangc3706f02009-02-10 01:03:18 -05001976 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001977 for (i = 0; trace_options[i]; i++) {
1978 len += strlen(trace_options[i]);
1979 len += 3; /* "no" and space */
1980 }
1981
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01001982 /*
1983 * Increase the size with names of options specific
1984 * of the current tracer.
1985 */
1986 for (i = 0; trace_opts[i].name; i++) {
1987 len += strlen(trace_opts[i].name);
1988 len += 3; /* "no" and space */
1989 }
1990
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001991 /* +2 for \n and \0 */
1992 buf = kmalloc(len + 2, GFP_KERNEL);
1993 if (!buf)
1994 return -ENOMEM;
1995
1996 for (i = 0; trace_options[i]; i++) {
1997 if (trace_flags & (1 << i))
1998 r += sprintf(buf + r, "%s ", trace_options[i]);
1999 else
2000 r += sprintf(buf + r, "no%s ", trace_options[i]);
2001 }
2002
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002003 for (i = 0; trace_opts[i].name; i++) {
2004 if (tracer_flags & trace_opts[i].bit)
2005 r += sprintf(buf + r, "%s ",
2006 trace_opts[i].name);
2007 else
2008 r += sprintf(buf + r, "no%s ",
2009 trace_opts[i].name);
2010 }
2011
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002012 r += sprintf(buf + r, "\n");
2013 WARN_ON(r >= len + 2);
2014
Ingo Molnar36dfe922008-05-12 21:20:52 +02002015 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002016
2017 kfree(buf);
2018
2019 return r;
2020}
2021
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002022/* Try to assign a tracer specific option */
2023static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2024{
2025 struct tracer_flags *trace_flags = trace->flags;
2026 struct tracer_opt *opts = NULL;
2027 int ret = 0, i = 0;
2028 int len;
2029
2030 for (i = 0; trace_flags->opts[i].name; i++) {
2031 opts = &trace_flags->opts[i];
2032 len = strlen(opts->name);
2033
2034 if (strncmp(cmp, opts->name, len) == 0) {
2035 ret = trace->set_flag(trace_flags->val,
2036 opts->bit, !neg);
2037 break;
2038 }
2039 }
2040 /* Not found */
2041 if (!trace_flags->opts[i].name)
2042 return -EINVAL;
2043
2044 /* Refused to handle */
2045 if (ret)
2046 return ret;
2047
2048 if (neg)
2049 trace_flags->val &= ~opts->bit;
2050 else
2051 trace_flags->val |= opts->bit;
2052
2053 return 0;
2054}
2055
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002056static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002057tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002058 size_t cnt, loff_t *ppos)
2059{
2060 char buf[64];
2061 char *cmp = buf;
2062 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002063 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002064 int i;
2065
Steven Rostedtcffae432008-05-12 21:21:00 +02002066 if (cnt >= sizeof(buf))
2067 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002068
2069 if (copy_from_user(&buf, ubuf, cnt))
2070 return -EFAULT;
2071
2072 buf[cnt] = 0;
2073
2074 if (strncmp(buf, "no", 2) == 0) {
2075 neg = 1;
2076 cmp += 2;
2077 }
2078
2079 for (i = 0; trace_options[i]; i++) {
2080 int len = strlen(trace_options[i]);
2081
2082 if (strncmp(cmp, trace_options[i], len) == 0) {
2083 if (neg)
2084 trace_flags &= ~(1 << i);
2085 else
2086 trace_flags |= (1 << i);
2087 break;
2088 }
2089 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002090
2091 /* If no option could be set, test the specific tracer options */
2092 if (!trace_options[i]) {
2093 ret = set_tracer_option(current_trace, cmp, neg);
2094 if (ret)
2095 return ret;
2096 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002097
2098 filp->f_pos += cnt;
2099
2100 return cnt;
2101}
2102
2103static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002104 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002105 .read = tracing_trace_options_read,
2106 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002107};
2108
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002109static const char readme_msg[] =
2110 "tracing mini-HOWTO:\n\n"
2111 "# mkdir /debug\n"
2112 "# mount -t debugfs nodev /debug\n\n"
2113 "# cat /debug/tracing/available_tracers\n"
2114 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2115 "# cat /debug/tracing/current_tracer\n"
2116 "none\n"
2117 "# echo sched_switch > /debug/tracing/current_tracer\n"
2118 "# cat /debug/tracing/current_tracer\n"
2119 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002120 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002121 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002122 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002123 "# echo 1 > /debug/tracing/tracing_enabled\n"
2124 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2125 "echo 0 > /debug/tracing/tracing_enabled\n"
2126;
2127
2128static ssize_t
2129tracing_readme_read(struct file *filp, char __user *ubuf,
2130 size_t cnt, loff_t *ppos)
2131{
2132 return simple_read_from_buffer(ubuf, cnt, ppos,
2133 readme_msg, strlen(readme_msg));
2134}
2135
2136static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002137 .open = tracing_open_generic,
2138 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002139};
2140
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002141static ssize_t
2142tracing_ctrl_read(struct file *filp, char __user *ubuf,
2143 size_t cnt, loff_t *ppos)
2144{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002145 char buf[64];
2146 int r;
2147
Steven Rostedt90369902008-11-05 16:05:44 -05002148 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002149 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002150}
2151
2152static ssize_t
2153tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2154 size_t cnt, loff_t *ppos)
2155{
2156 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002157 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002158 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002159 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002160
Steven Rostedtcffae432008-05-12 21:21:00 +02002161 if (cnt >= sizeof(buf))
2162 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002163
2164 if (copy_from_user(&buf, ubuf, cnt))
2165 return -EFAULT;
2166
2167 buf[cnt] = 0;
2168
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002169 ret = strict_strtoul(buf, 10, &val);
2170 if (ret < 0)
2171 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002172
2173 val = !!val;
2174
2175 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002176 if (tracer_enabled ^ val) {
2177 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002178 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002179 if (current_trace->start)
2180 current_trace->start(tr);
2181 tracing_start();
2182 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002183 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002184 tracing_stop();
2185 if (current_trace->stop)
2186 current_trace->stop(tr);
2187 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002188 }
2189 mutex_unlock(&trace_types_lock);
2190
2191 filp->f_pos += cnt;
2192
2193 return cnt;
2194}
2195
2196static ssize_t
2197tracing_set_trace_read(struct file *filp, char __user *ubuf,
2198 size_t cnt, loff_t *ppos)
2199{
2200 char buf[max_tracer_type_len+2];
2201 int r;
2202
2203 mutex_lock(&trace_types_lock);
2204 if (current_trace)
2205 r = sprintf(buf, "%s\n", current_trace->name);
2206 else
2207 r = sprintf(buf, "\n");
2208 mutex_unlock(&trace_types_lock);
2209
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002210 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002211}
2212
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002213int tracer_init(struct tracer *t, struct trace_array *tr)
2214{
2215 tracing_reset_online_cpus(tr);
2216 return t->init(tr);
2217}
2218
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002219static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002220{
2221 struct trace_array *tr = &global_trace;
2222 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002223 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002224
2225 mutex_lock(&trace_types_lock);
2226 for (t = trace_types; t; t = t->next) {
2227 if (strcmp(t->name, buf) == 0)
2228 break;
2229 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002230 if (!t) {
2231 ret = -EINVAL;
2232 goto out;
2233 }
2234 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002235 goto out;
2236
Steven Rostedt9f029e82008-11-12 15:24:24 -05002237 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002238 if (current_trace && current_trace->reset)
2239 current_trace->reset(tr);
2240
2241 current_trace = t;
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002242 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002243 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002244 if (ret)
2245 goto out;
2246 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002247
Steven Rostedt9f029e82008-11-12 15:24:24 -05002248 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002249 out:
2250 mutex_unlock(&trace_types_lock);
2251
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002252 return ret;
2253}
2254
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255static ssize_t
2256tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2257 size_t cnt, loff_t *ppos)
2258{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002259 char buf[max_tracer_type_len+1];
2260 int i;
2261 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002262 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002263
Steven Rostedt60063a62008-10-28 10:44:24 -04002264 ret = cnt;
2265
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002266 if (cnt > max_tracer_type_len)
2267 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002268
2269 if (copy_from_user(&buf, ubuf, cnt))
2270 return -EFAULT;
2271
2272 buf[cnt] = 0;
2273
2274 /* strip ending whitespace. */
2275 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2276 buf[i] = 0;
2277
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002278 err = tracing_set_tracer(buf);
2279 if (err)
2280 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002281
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002282 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002283
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002284 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002285}
2286
2287static ssize_t
2288tracing_max_lat_read(struct file *filp, char __user *ubuf,
2289 size_t cnt, loff_t *ppos)
2290{
2291 unsigned long *ptr = filp->private_data;
2292 char buf[64];
2293 int r;
2294
Steven Rostedtcffae432008-05-12 21:21:00 +02002295 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002296 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002297 if (r > sizeof(buf))
2298 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002299 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002300}
2301
2302static ssize_t
2303tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2304 size_t cnt, loff_t *ppos)
2305{
Hannes Eder5e398412009-02-10 19:44:34 +01002306 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002307 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002308 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002309 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002310
Steven Rostedtcffae432008-05-12 21:21:00 +02002311 if (cnt >= sizeof(buf))
2312 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002313
2314 if (copy_from_user(&buf, ubuf, cnt))
2315 return -EFAULT;
2316
2317 buf[cnt] = 0;
2318
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002319 ret = strict_strtoul(buf, 10, &val);
2320 if (ret < 0)
2321 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002322
2323 *ptr = val * 1000;
2324
2325 return cnt;
2326}
2327
Steven Rostedtb3806b42008-05-12 21:20:46 +02002328static atomic_t tracing_reader;
2329
2330static int tracing_open_pipe(struct inode *inode, struct file *filp)
2331{
2332 struct trace_iterator *iter;
2333
2334 if (tracing_disabled)
2335 return -ENODEV;
2336
2337 /* We only allow for reader of the pipe */
2338 if (atomic_inc_return(&tracing_reader) != 1) {
2339 atomic_dec(&tracing_reader);
2340 return -EBUSY;
2341 }
2342
2343 /* create a buffer to store the information to pass to userspace */
2344 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2345 if (!iter)
2346 return -ENOMEM;
2347
Rusty Russell44623442009-01-01 10:12:23 +10302348 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2349 kfree(iter);
2350 return -ENOMEM;
2351 }
2352
Steven Rostedt107bad82008-05-12 21:21:01 +02002353 mutex_lock(&trace_types_lock);
Steven Rostedta3097202008-11-07 22:36:02 -05002354
2355 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302356 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002357
Steven Rostedtb3806b42008-05-12 21:20:46 +02002358 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002359 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002360 filp->private_data = iter;
2361
Steven Rostedt107bad82008-05-12 21:21:01 +02002362 if (iter->trace->pipe_open)
2363 iter->trace->pipe_open(iter);
2364 mutex_unlock(&trace_types_lock);
2365
Steven Rostedtb3806b42008-05-12 21:20:46 +02002366 return 0;
2367}
2368
2369static int tracing_release_pipe(struct inode *inode, struct file *file)
2370{
2371 struct trace_iterator *iter = file->private_data;
2372
Rusty Russell44623442009-01-01 10:12:23 +10302373 free_cpumask_var(iter->started);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002374 kfree(iter);
2375 atomic_dec(&tracing_reader);
2376
2377 return 0;
2378}
2379
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002380static unsigned int
2381tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2382{
2383 struct trace_iterator *iter = filp->private_data;
2384
2385 if (trace_flags & TRACE_ITER_BLOCK) {
2386 /*
2387 * Always select as readable when in blocking mode
2388 */
2389 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002390 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002391 if (!trace_empty(iter))
2392 return POLLIN | POLLRDNORM;
2393 poll_wait(filp, &trace_wait, poll_table);
2394 if (!trace_empty(iter))
2395 return POLLIN | POLLRDNORM;
2396
2397 return 0;
2398 }
2399}
2400
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002401
2402void default_wait_pipe(struct trace_iterator *iter)
2403{
2404 DEFINE_WAIT(wait);
2405
2406 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2407
2408 if (trace_empty(iter))
2409 schedule();
2410
2411 finish_wait(&trace_wait, &wait);
2412}
2413
2414/*
2415 * This is a make-shift waitqueue.
2416 * A tracer might use this callback on some rare cases:
2417 *
2418 * 1) the current tracer might hold the runqueue lock when it wakes up
2419 * a reader, hence a deadlock (sched, function, and function graph tracers)
2420 * 2) the function tracers, trace all functions, we don't want
2421 * the overhead of calling wake_up and friends
2422 * (and tracing them too)
2423 *
2424 * Anyway, this is really very primitive wakeup.
2425 */
2426void poll_wait_pipe(struct trace_iterator *iter)
2427{
2428 set_current_state(TASK_INTERRUPTIBLE);
2429 /* sleep for 100 msecs, and try again. */
2430 schedule_timeout(HZ / 10);
2431}
2432
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002433/* Must be called with trace_types_lock mutex held. */
2434static int tracing_wait_pipe(struct file *filp)
2435{
2436 struct trace_iterator *iter = filp->private_data;
2437
2438 while (trace_empty(iter)) {
2439
2440 if ((filp->f_flags & O_NONBLOCK)) {
2441 return -EAGAIN;
2442 }
2443
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002444 mutex_unlock(&trace_types_lock);
2445
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002446 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002447
2448 mutex_lock(&trace_types_lock);
2449
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002450 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002451 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002452
2453 if (iter->trace != current_trace)
2454 return 0;
2455
2456 /*
2457 * We block until we read something and tracing is disabled.
2458 * We still block if tracing is disabled, but we have never
2459 * read anything. This allows a user to cat this file, and
2460 * then enable tracing. But after we have read something,
2461 * we give an EOF when tracing is again disabled.
2462 *
2463 * iter->pos will be 0 if we haven't read anything.
2464 */
2465 if (!tracer_enabled && iter->pos)
2466 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002467 }
2468
2469 return 1;
2470}
2471
Steven Rostedtb3806b42008-05-12 21:20:46 +02002472/*
2473 * Consumer reader.
2474 */
2475static ssize_t
2476tracing_read_pipe(struct file *filp, char __user *ubuf,
2477 size_t cnt, loff_t *ppos)
2478{
2479 struct trace_iterator *iter = filp->private_data;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002480 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002481
2482 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002483 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2484 if (sret != -EBUSY)
2485 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002486
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002487 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002488
Steven Rostedt107bad82008-05-12 21:21:01 +02002489 mutex_lock(&trace_types_lock);
2490 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002491 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2492 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002493 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002494 }
2495
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002496waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002497 sret = tracing_wait_pipe(filp);
2498 if (sret <= 0)
2499 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002500
2501 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002502 if (trace_empty(iter)) {
2503 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002504 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002505 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002506
2507 if (cnt >= PAGE_SIZE)
2508 cnt = PAGE_SIZE - 1;
2509
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002510 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002511 memset(&iter->seq, 0,
2512 sizeof(struct trace_iterator) -
2513 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002514 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002515
Steven Rostedt088b1e422008-05-12 21:20:48 +02002516 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002517 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002518 int len = iter->seq.len;
2519
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002520 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002521 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002522 /* don't print partial lines */
2523 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002524 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002525 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002526 if (ret != TRACE_TYPE_NO_CONSUME)
2527 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002528
2529 if (iter->seq.len >= cnt)
2530 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002531 }
2532
Steven Rostedtb3806b42008-05-12 21:20:46 +02002533 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002534 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2535 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002536 trace_seq_reset(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002537
2538 /*
2539 * If there was nothing to send to user, inspite of consuming trace
2540 * entries, go back to wait for more entries.
2541 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002542 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002543 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002544
Steven Rostedt107bad82008-05-12 21:21:01 +02002545out:
2546 mutex_unlock(&trace_types_lock);
2547
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002548 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002549}
2550
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002551static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2552 struct pipe_buffer *buf)
2553{
2554 __free_page(buf->page);
2555}
2556
2557static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2558 unsigned int idx)
2559{
2560 __free_page(spd->pages[idx]);
2561}
2562
2563static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002564 .can_merge = 0,
2565 .map = generic_pipe_buf_map,
2566 .unmap = generic_pipe_buf_unmap,
2567 .confirm = generic_pipe_buf_confirm,
2568 .release = tracing_pipe_buf_release,
2569 .steal = generic_pipe_buf_steal,
2570 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002571};
2572
Steven Rostedt34cd4992009-02-09 12:06:29 -05002573static size_t
2574tracing_fill_pipe_page(struct page *pages, size_t rem,
2575 struct trace_iterator *iter)
2576{
2577 size_t count;
2578 int ret;
2579
2580 /* Seq buffer is page-sized, exactly what we need. */
2581 for (;;) {
2582 count = iter->seq.len;
2583 ret = print_trace_line(iter);
2584 count = iter->seq.len - count;
2585 if (rem < count) {
2586 rem = 0;
2587 iter->seq.len -= count;
2588 break;
2589 }
2590 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2591 iter->seq.len -= count;
2592 break;
2593 }
2594
2595 trace_consume(iter);
2596 rem -= count;
2597 if (!find_next_entry_inc(iter)) {
2598 rem = 0;
2599 iter->ent = NULL;
2600 break;
2601 }
2602 }
2603
2604 return rem;
2605}
2606
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002607static ssize_t tracing_splice_read_pipe(struct file *filp,
2608 loff_t *ppos,
2609 struct pipe_inode_info *pipe,
2610 size_t len,
2611 unsigned int flags)
2612{
2613 struct page *pages[PIPE_BUFFERS];
2614 struct partial_page partial[PIPE_BUFFERS];
2615 struct trace_iterator *iter = filp->private_data;
2616 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002617 .pages = pages,
2618 .partial = partial,
2619 .nr_pages = 0, /* This gets updated below. */
2620 .flags = flags,
2621 .ops = &tracing_pipe_buf_ops,
2622 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002623 };
2624 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002625 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002626 unsigned int i;
2627
2628 mutex_lock(&trace_types_lock);
2629
2630 if (iter->trace->splice_read) {
2631 ret = iter->trace->splice_read(iter, filp,
2632 ppos, pipe, len, flags);
2633 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002634 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002635 }
2636
2637 ret = tracing_wait_pipe(filp);
2638 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002639 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002640
2641 if (!iter->ent && !find_next_entry_inc(iter)) {
2642 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002643 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002644 }
2645
2646 /* Fill as many pages as possible. */
2647 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2648 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05002649 if (!pages[i])
2650 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002651
Steven Rostedt34cd4992009-02-09 12:06:29 -05002652 rem = tracing_fill_pipe_page(pages[i], rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002653
2654 /* Copy the data into the page, so we can start over. */
2655 ret = trace_seq_to_buffer(&iter->seq,
2656 page_address(pages[i]),
2657 iter->seq.len);
2658 if (ret < 0) {
2659 __free_page(pages[i]);
2660 break;
2661 }
2662 partial[i].offset = 0;
2663 partial[i].len = iter->seq.len;
2664
2665 trace_seq_reset(&iter->seq);
2666 }
2667
2668 mutex_unlock(&trace_types_lock);
2669
2670 spd.nr_pages = i;
2671
2672 return splice_to_pipe(pipe, &spd);
2673
Steven Rostedt34cd4992009-02-09 12:06:29 -05002674out_err:
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002675 mutex_unlock(&trace_types_lock);
2676
2677 return ret;
2678}
2679
Steven Rostedta98a3c32008-05-12 21:20:59 +02002680static ssize_t
2681tracing_entries_read(struct file *filp, char __user *ubuf,
2682 size_t cnt, loff_t *ppos)
2683{
2684 struct trace_array *tr = filp->private_data;
2685 char buf[64];
2686 int r;
2687
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002688 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002689 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2690}
2691
2692static ssize_t
2693tracing_entries_write(struct file *filp, const char __user *ubuf,
2694 size_t cnt, loff_t *ppos)
2695{
2696 unsigned long val;
2697 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002698 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002699
Steven Rostedtcffae432008-05-12 21:21:00 +02002700 if (cnt >= sizeof(buf))
2701 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002702
2703 if (copy_from_user(&buf, ubuf, cnt))
2704 return -EFAULT;
2705
2706 buf[cnt] = 0;
2707
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002708 ret = strict_strtoul(buf, 10, &val);
2709 if (ret < 0)
2710 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002711
2712 /* must have at least 1 entry */
2713 if (!val)
2714 return -EINVAL;
2715
2716 mutex_lock(&trace_types_lock);
2717
Steven Rostedtc76f0692008-11-07 22:36:02 -05002718 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002719
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002720 /* disable all cpu buffers */
2721 for_each_tracing_cpu(cpu) {
2722 if (global_trace.data[cpu])
2723 atomic_inc(&global_trace.data[cpu]->disabled);
2724 if (max_tr.data[cpu])
2725 atomic_inc(&max_tr.data[cpu]->disabled);
2726 }
2727
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002728 /* value is in KB */
2729 val <<= 10;
2730
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002731 if (val != global_trace.entries) {
2732 ret = ring_buffer_resize(global_trace.buffer, val);
2733 if (ret < 0) {
2734 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002735 goto out;
2736 }
2737
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002738 ret = ring_buffer_resize(max_tr.buffer, val);
2739 if (ret < 0) {
2740 int r;
2741 cnt = ret;
2742 r = ring_buffer_resize(global_trace.buffer,
2743 global_trace.entries);
2744 if (r < 0) {
2745 /* AARGH! We are left with different
2746 * size max buffer!!!! */
2747 WARN_ON(1);
2748 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002749 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002750 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002751 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002752
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002753 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002754 }
2755
2756 filp->f_pos += cnt;
2757
Steven Rostedt19384c02008-05-22 00:22:16 -04002758 /* If check pages failed, return ENOMEM */
2759 if (tracing_disabled)
2760 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002761 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002762 for_each_tracing_cpu(cpu) {
2763 if (global_trace.data[cpu])
2764 atomic_dec(&global_trace.data[cpu]->disabled);
2765 if (max_tr.data[cpu])
2766 atomic_dec(&max_tr.data[cpu]->disabled);
2767 }
2768
Steven Rostedtc76f0692008-11-07 22:36:02 -05002769 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002770 max_tr.entries = global_trace.entries;
2771 mutex_unlock(&trace_types_lock);
2772
2773 return cnt;
2774}
2775
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002776static int mark_printk(const char *fmt, ...)
2777{
2778 int ret;
2779 va_list args;
2780 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002781 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002782 va_end(args);
2783 return ret;
2784}
2785
2786static ssize_t
2787tracing_mark_write(struct file *filp, const char __user *ubuf,
2788 size_t cnt, loff_t *fpos)
2789{
2790 char *buf;
2791 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002792
Steven Rostedtc76f0692008-11-07 22:36:02 -05002793 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002794 return -EINVAL;
2795
2796 if (cnt > TRACE_BUF_SIZE)
2797 cnt = TRACE_BUF_SIZE;
2798
2799 buf = kmalloc(cnt + 1, GFP_KERNEL);
2800 if (buf == NULL)
2801 return -ENOMEM;
2802
2803 if (copy_from_user(buf, ubuf, cnt)) {
2804 kfree(buf);
2805 return -EFAULT;
2806 }
2807
2808 /* Cut from the first nil or newline. */
2809 buf[cnt] = '\0';
2810 end = strchr(buf, '\n');
2811 if (end)
2812 *end = '\0';
2813
2814 cnt = mark_printk("%s\n", buf);
2815 kfree(buf);
2816 *fpos += cnt;
2817
2818 return cnt;
2819}
2820
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002821static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002822 .open = tracing_open_generic,
2823 .read = tracing_max_lat_read,
2824 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002825};
2826
2827static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002828 .open = tracing_open_generic,
2829 .read = tracing_ctrl_read,
2830 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002831};
2832
2833static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002834 .open = tracing_open_generic,
2835 .read = tracing_set_trace_read,
2836 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002837};
2838
Steven Rostedtb3806b42008-05-12 21:20:46 +02002839static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002840 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002841 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002842 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002843 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002844 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002845};
2846
Steven Rostedta98a3c32008-05-12 21:20:59 +02002847static struct file_operations tracing_entries_fops = {
2848 .open = tracing_open_generic,
2849 .read = tracing_entries_read,
2850 .write = tracing_entries_write,
2851};
2852
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002853static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002854 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002855 .write = tracing_mark_write,
2856};
2857
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002858#ifdef CONFIG_DYNAMIC_FTRACE
2859
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002860int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002861{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002862 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002863}
2864
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002865static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002866tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002867 size_t cnt, loff_t *ppos)
2868{
Steven Rostedta26a2a22008-10-31 00:03:22 -04002869 static char ftrace_dyn_info_buffer[1024];
2870 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002871 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002872 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04002873 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002874 int r;
2875
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002876 mutex_lock(&dyn_info_mutex);
2877 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002878
Steven Rostedta26a2a22008-10-31 00:03:22 -04002879 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002880 buf[r++] = '\n';
2881
2882 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2883
2884 mutex_unlock(&dyn_info_mutex);
2885
2886 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002887}
2888
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002889static struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002890 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04002891 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002892};
2893#endif
2894
2895static struct dentry *d_tracer;
2896
2897struct dentry *tracing_init_dentry(void)
2898{
2899 static int once;
2900
2901 if (d_tracer)
2902 return d_tracer;
2903
2904 d_tracer = debugfs_create_dir("tracing", NULL);
2905
2906 if (!d_tracer && !once) {
2907 once = 1;
2908 pr_warning("Could not create debugfs directory 'tracing'\n");
2909 return NULL;
2910 }
2911
2912 return d_tracer;
2913}
2914
Steven Rostedt60a11772008-05-12 21:20:44 +02002915#ifdef CONFIG_FTRACE_SELFTEST
2916/* Let selftest have access to static functions in this file */
2917#include "trace_selftest.c"
2918#endif
2919
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01002920static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002921{
2922 struct dentry *d_tracer;
2923 struct dentry *entry;
2924
2925 d_tracer = tracing_init_dentry();
2926
2927 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2928 &global_trace, &tracing_ctrl_fops);
2929 if (!entry)
2930 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2931
Steven Rostedtee6bce52008-11-12 17:52:37 -05002932 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002933 NULL, &tracing_iter_fops);
2934 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05002935 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002936
Ingo Molnarc7078de2008-05-12 21:20:52 +02002937 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2938 NULL, &tracing_cpumask_fops);
2939 if (!entry)
2940 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2941
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002942 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2943 &global_trace, &tracing_lt_fops);
2944 if (!entry)
2945 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2946
2947 entry = debugfs_create_file("trace", 0444, d_tracer,
2948 &global_trace, &tracing_fops);
2949 if (!entry)
2950 pr_warning("Could not create debugfs 'trace' entry\n");
2951
2952 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2953 &global_trace, &show_traces_fops);
2954 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002955 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002956
2957 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2958 &global_trace, &set_tracer_fops);
2959 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002960 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002961
2962 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2963 &tracing_max_latency,
2964 &tracing_max_lat_fops);
2965 if (!entry)
2966 pr_warning("Could not create debugfs "
2967 "'tracing_max_latency' entry\n");
2968
2969 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2970 &tracing_thresh, &tracing_max_lat_fops);
2971 if (!entry)
2972 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002973 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002974 entry = debugfs_create_file("README", 0644, d_tracer,
2975 NULL, &tracing_readme_fops);
2976 if (!entry)
2977 pr_warning("Could not create debugfs 'README' entry\n");
2978
Steven Rostedtb3806b42008-05-12 21:20:46 +02002979 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2980 NULL, &tracing_pipe_fops);
2981 if (!entry)
2982 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02002983 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002984
Steven Rostedta94c80e2008-11-12 17:52:36 -05002985 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02002986 &global_trace, &tracing_entries_fops);
2987 if (!entry)
2988 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05002989 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02002990
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002991 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
2992 NULL, &tracing_mark_fops);
2993 if (!entry)
2994 pr_warning("Could not create debugfs "
2995 "'trace_marker' entry\n");
2996
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002997#ifdef CONFIG_DYNAMIC_FTRACE
2998 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2999 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003000 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003001 if (!entry)
3002 pr_warning("Could not create debugfs "
3003 "'dyn_ftrace_total_info' entry\n");
3004#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003005#ifdef CONFIG_SYSPROF_TRACER
3006 init_tracer_sysprof_debugfs(d_tracer);
3007#endif
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003008 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003009}
3010
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003011int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003012{
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003013 static DEFINE_SPINLOCK(trace_buf_lock);
3014 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003015
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003016 struct ring_buffer_event *event;
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003017 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003018 struct trace_array_cpu *data;
Steven Rostedt38697052008-10-01 13:14:09 -04003019 int cpu, len = 0, size, pc;
Ingo Molnare726f5f2008-12-08 16:55:53 +01003020 struct print_entry *entry;
3021 unsigned long irq_flags;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003022
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01003023 if (tracing_disabled || tracing_selftest_running)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003024 return 0;
3025
Steven Rostedt38697052008-10-01 13:14:09 -04003026 pc = preempt_count();
3027 preempt_disable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003028 cpu = raw_smp_processor_id();
3029 data = tr->data[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003030
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -04003031 if (unlikely(atomic_read(&data->disabled)))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003032 goto out;
3033
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003034 pause_graph_tracing();
3035 spin_lock_irqsave(&trace_buf_lock, irq_flags);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003036 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003037
3038 len = min(len, TRACE_BUF_SIZE-1);
3039 trace_buf[len] = 0;
3040
Steven Rostedt777e2082008-09-29 23:02:42 -04003041 size = sizeof(*entry) + len + 1;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02003042 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003043 if (!event)
3044 goto out_unlock;
Steven Rostedt777e2082008-09-29 23:02:42 -04003045 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04003046 entry->ip = ip;
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003047 entry->depth = depth;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003048
Steven Rostedt777e2082008-09-29 23:02:42 -04003049 memcpy(&entry->buf, trace_buf, len);
3050 entry->buf[len] = 0;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02003051 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003052
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003053 out_unlock:
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003054 spin_unlock_irqrestore(&trace_buf_lock, irq_flags);
3055 unpause_graph_tracing();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003056 out:
Steven Rostedt38697052008-10-01 13:14:09 -04003057 preempt_enable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003058
3059 return len;
3060}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003061EXPORT_SYMBOL_GPL(trace_vprintk);
3062
3063int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3064{
3065 int ret;
3066 va_list ap;
3067
3068 if (!(trace_flags & TRACE_ITER_PRINTK))
3069 return 0;
3070
3071 va_start(ap, fmt);
Frederic Weisbecker21a8c462008-12-04 23:51:23 +01003072 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003073 va_end(ap);
3074 return ret;
3075}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003076EXPORT_SYMBOL_GPL(__ftrace_printk);
3077
Arnaldo Carvalho de Melo90112622009-01-23 12:06:23 -02003078int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3079{
3080 if (!(trace_flags & TRACE_ITER_PRINTK))
3081 return 0;
3082
3083 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3084}
3085EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3086
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003087static int trace_panic_handler(struct notifier_block *this,
3088 unsigned long event, void *unused)
3089{
Steven Rostedt944ac422008-10-23 19:26:08 -04003090 if (ftrace_dump_on_oops)
3091 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003092 return NOTIFY_OK;
3093}
3094
3095static struct notifier_block trace_panic_notifier = {
3096 .notifier_call = trace_panic_handler,
3097 .next = NULL,
3098 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3099};
3100
3101static int trace_die_handler(struct notifier_block *self,
3102 unsigned long val,
3103 void *data)
3104{
3105 switch (val) {
3106 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003107 if (ftrace_dump_on_oops)
3108 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003109 break;
3110 default:
3111 break;
3112 }
3113 return NOTIFY_OK;
3114}
3115
3116static struct notifier_block trace_die_notifier = {
3117 .notifier_call = trace_die_handler,
3118 .priority = 200
3119};
3120
3121/*
3122 * printk is set to max of 1024, we really don't need it that big.
3123 * Nothing should be printing 1000 characters anyway.
3124 */
3125#define TRACE_MAX_PRINT 1000
3126
3127/*
3128 * Define here KERN_TRACE so that we have one place to modify
3129 * it if we decide to change what log level the ftrace dump
3130 * should be at.
3131 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003132#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003133
3134static void
3135trace_printk_seq(struct trace_seq *s)
3136{
3137 /* Probably should print a warning here. */
3138 if (s->len >= 1000)
3139 s->len = 1000;
3140
3141 /* should be zero ended, but we are paranoid. */
3142 s->buffer[s->len] = 0;
3143
3144 printk(KERN_TRACE "%s", s->buffer);
3145
3146 trace_seq_reset(s);
3147}
3148
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003149void ftrace_dump(void)
3150{
3151 static DEFINE_SPINLOCK(ftrace_dump_lock);
3152 /* use static because iter can be a bit big for the stack */
3153 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003154 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003155 unsigned long flags;
3156 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003157
3158 /* only one dump */
3159 spin_lock_irqsave(&ftrace_dump_lock, flags);
3160 if (dump_ran)
3161 goto out;
3162
3163 dump_ran = 1;
3164
3165 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05003166 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003167 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003168
Steven Rostedtd7690412008-10-01 00:29:53 -04003169 for_each_tracing_cpu(cpu) {
3170 atomic_inc(&global_trace.data[cpu]->disabled);
3171 }
3172
Török Edwinb54d3de2008-11-22 13:28:48 +02003173 /* don't look at user memory in panic mode */
3174 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3175
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003176 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3177
3178 iter.tr = &global_trace;
3179 iter.trace = current_trace;
3180
3181 /*
3182 * We need to stop all tracing on all CPUS to read the
3183 * the next buffer. This is a bit expensive, but is
3184 * not done often. We fill all what we can read,
3185 * and then release the locks again.
3186 */
3187
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003188 while (!trace_empty(&iter)) {
3189
3190 if (!cnt)
3191 printk(KERN_TRACE "---------------------------------\n");
3192
3193 cnt++;
3194
3195 /* reset all but tr, trace, and overruns */
3196 memset(&iter.seq, 0,
3197 sizeof(struct trace_iterator) -
3198 offsetof(struct trace_iterator, seq));
3199 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3200 iter.pos = -1;
3201
3202 if (find_next_entry_inc(&iter) != NULL) {
3203 print_trace_line(&iter);
3204 trace_consume(&iter);
3205 }
3206
3207 trace_printk_seq(&iter.seq);
3208 }
3209
3210 if (!cnt)
3211 printk(KERN_TRACE " (ftrace buffer empty)\n");
3212 else
3213 printk(KERN_TRACE "---------------------------------\n");
3214
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003215 out:
3216 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3217}
3218
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003219__init static int tracer_alloc_buffers(void)
3220{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003221 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003222 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303223 int ret = -ENOMEM;
3224
3225 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3226 goto out;
3227
3228 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3229 goto out_free_buffer_mask;
3230
3231 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3232 cpumask_copy(tracing_cpumask, cpu_all_mask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003233
Steven Rostedtab464282008-05-12 21:21:00 +02003234 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003235 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3236 TRACE_BUFFER_FLAGS);
3237 if (!global_trace.buffer) {
3238 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3239 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303240 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003241 }
3242 global_trace.entries = ring_buffer_size(global_trace.buffer);
3243
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303244
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003245#ifdef CONFIG_TRACER_MAX_TRACE
3246 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3247 TRACE_BUFFER_FLAGS);
3248 if (!max_tr.buffer) {
3249 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3250 WARN_ON(1);
3251 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303252 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003253 }
3254 max_tr.entries = ring_buffer_size(max_tr.buffer);
3255 WARN_ON(max_tr.entries != global_trace.entries);
3256#endif
3257
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003258 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003259 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003260 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003261 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003262 }
3263
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003264 trace_init_cmdlines();
3265
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003266 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05003267 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003268#ifdef CONFIG_BOOT_TRACER
3269 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003270#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02003271 /* All seems OK, enable tracing */
3272 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003273
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003274 atomic_notifier_chain_register(&panic_notifier_list,
3275 &trace_panic_notifier);
3276
3277 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303278 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003279
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303280out_free_cpumask:
3281 free_cpumask_var(tracing_cpumask);
3282out_free_buffer_mask:
3283 free_cpumask_var(tracing_buffer_mask);
3284out:
3285 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003286}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003287
3288__init static int clear_boot_tracer(void)
3289{
3290 /*
3291 * The default tracer at boot buffer is an init section.
3292 * This function is called in lateinit. If we did not
3293 * find the boot tracer, then clear it out, to prevent
3294 * later registration from accessing the buffer that is
3295 * about to be freed.
3296 */
3297 if (!default_bootup_tracer)
3298 return 0;
3299
3300 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
3301 default_bootup_tracer);
3302 default_bootup_tracer = NULL;
3303
3304 return 0;
3305}
3306
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003307early_initcall(tracer_alloc_buffers);
3308fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003309late_initcall(clear_boot_tracer);