blob: 4c97947650ae54b2fb2be4338453c9c82ebae33c [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
Steven Rostedt2cadf912008-12-01 22:20:19 -050014#include <linux/ring_buffer.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020015#include <linux/utsrelease.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050016#include <linux/stacktrace.h>
17#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020018#include <linux/kallsyms.h>
19#include <linux/seq_file.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040020#include <linux/notifier.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050021#include <linux/irqflags.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020022#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020023#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020024#include <linux/hardirq.h>
25#include <linux/linkage.h>
26#include <linux/uaccess.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050027#include <linux/kprobes.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/ftrace.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
Steven Rostedt2cadf912008-12-01 22:20:19 -050031#include <linux/splice.h>
Steven Rostedt3f5a54e2008-07-30 22:36:46 -040032#include <linux/kdebug.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020033#include <linux/ctype.h>
34#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020035#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020036#include <linux/gfp.h>
37#include <linux/fs.h>
Ingo Molnar86387f72008-05-12 21:20:51 +020038
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020039#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050040#include "trace_output.h"
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020041
Steven Rostedt3928a8a2008-09-29 23:02:41 -040042#define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43
Steven Rostedt745b1622009-01-15 23:40:11 -050044unsigned long __read_mostly tracing_max_latency;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020045unsigned long __read_mostly tracing_thresh;
46
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010047/*
48 * We need to change this state when a selftest is running.
Frederic Weisbeckerff325042008-12-04 23:47:35 +010049 * A selftest will lurk into the ring-buffer to count the
50 * entries inserted during the selftest although some concurrent
Ingo Molnar5e1607a2009-03-05 10:24:48 +010051 * insertions into the ring-buffer such as trace_printk could occurred
Frederic Weisbeckerff325042008-12-04 23:47:35 +010052 * at the same time, giving false positive or negative results.
53 */
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +010054static bool __read_mostly tracing_selftest_running;
Frederic Weisbeckerff325042008-12-04 23:47:35 +010055
Steven Rostedtb2821ae2009-02-02 21:38:32 -050056/*
57 * If a tracer is running, we do not want to run SELFTEST.
58 */
59static bool __read_mostly tracing_selftest_disabled;
60
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +010061/* For tracers that don't implement custom flags */
62static struct tracer_opt dummy_tracer_opt[] = {
63 { }
64};
65
66static struct tracer_flags dummy_tracer_flags = {
67 .val = 0,
68 .opts = dummy_tracer_opt
69};
70
71static int dummy_set_flag(u32 old_flags, u32 bit, int set)
72{
73 return 0;
74}
Steven Rostedt0f048702008-11-05 16:05:44 -050075
76/*
77 * Kill all tracing for good (never come back).
78 * It is initialized to 1 but will turn to zero if the initialization
79 * of the tracer is successful. But that is the only place that sets
80 * this back to zero.
81 */
Hannes Eder4fd27352009-02-10 19:44:12 +010082static int tracing_disabled = 1;
Steven Rostedt0f048702008-11-05 16:05:44 -050083
Steven Rostedtd7690412008-10-01 00:29:53 -040084static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
85
86static inline void ftrace_disable_cpu(void)
87{
88 preempt_disable();
89 local_inc(&__get_cpu_var(ftrace_cpu_disabled));
90}
91
92static inline void ftrace_enable_cpu(void)
93{
94 local_dec(&__get_cpu_var(ftrace_cpu_disabled));
95 preempt_enable();
96}
97
Rusty Russell9e01c1b2009-01-01 10:12:22 +103098static cpumask_var_t __read_mostly tracing_buffer_mask;
Steven Rostedtab464282008-05-12 21:21:00 +020099
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +0100100/* Define which cpu buffers are currently read in trace_pipe */
101static cpumask_var_t tracing_reader_cpumask;
102
Steven Rostedtab464282008-05-12 21:21:00 +0200103#define for_each_tracing_cpu(cpu) \
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030104 for_each_cpu(cpu, tracing_buffer_mask)
Steven Rostedtab464282008-05-12 21:21:00 +0200105
Steven Rostedt944ac422008-10-23 19:26:08 -0400106/*
107 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
108 *
109 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
110 * is set, then ftrace_dump is called. This will output the contents
111 * of the ftrace buffers to the console. This is very useful for
112 * capturing traces that lead to crashes and outputing it to a
113 * serial console.
114 *
115 * It is default off, but you can enable it with either specifying
116 * "ftrace_dump_on_oops" in the kernel command line, or setting
117 * /proc/sys/kernel/ftrace_dump_on_oops to true.
118 */
119int ftrace_dump_on_oops;
120
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500121static int tracing_set_tracer(const char *buf);
122
123#define BOOTUP_TRACER_SIZE 100
124static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
125static char *default_bootup_tracer;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100126
127static int __init set_ftrace(char *str)
128{
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500129 strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
130 default_bootup_tracer = bootup_tracer_buf;
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100131 return 1;
132}
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500133__setup("ftrace=", set_ftrace);
Peter Zijlstrad9e54072008-11-01 19:57:37 +0100134
Steven Rostedt944ac422008-10-23 19:26:08 -0400135static int __init set_ftrace_dump_on_oops(char *str)
136{
137 ftrace_dump_on_oops = 1;
138 return 1;
139}
140__setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
Steven Rostedt60a11772008-05-12 21:20:44 +0200141
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200142long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200143ns2usecs(cycle_t nsec)
144{
145 nsec += 500;
146 do_div(nsec, 1000);
147 return nsec;
148}
149
Ingo Molnare309b412008-05-12 21:20:51 +0200150cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200151{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400152 u64 ts = ring_buffer_time_stamp(cpu);
153 ring_buffer_normalize_time_stamp(cpu, &ts);
154 return ts;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200155}
156
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200157/*
158 * The global_trace is the descriptor that holds the tracing
159 * buffers for the live tracing. For each CPU, it contains
160 * a link list of pages that will store trace entries. The
161 * page descriptor of the pages in the memory is used to hold
162 * the link list by linking the lru item in the page descriptor
163 * to each of the pages in the buffer per CPU.
164 *
165 * For each active CPU there is a data field that holds the
166 * pages for the buffer for that CPU. Each CPU has the same number
167 * of pages allocated for its buffer.
168 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200169static struct trace_array global_trace;
170
171static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
172
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200173/*
174 * The max_tr is used to snapshot the global_trace when a maximum
175 * latency is reached. Some tracers will use this to store a maximum
176 * trace while it continues examining live traces.
177 *
178 * The buffers for the max_tr are set up the same as the global_trace.
179 * When a snapshot is taken, the link list of the max_tr is swapped
180 * with the link list of the global_trace and the buffers are reset for
181 * the global_trace so the tracing can continue.
182 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200183static struct trace_array max_tr;
184
185static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
186
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200187/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +0200188static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200189
Steven Rostedt90369902008-11-05 16:05:44 -0500190/**
191 * tracing_is_enabled - return tracer_enabled status
192 *
193 * This function is used by other tracers to know the status
194 * of the tracer_enabled flag. Tracers may use this function
195 * to know if it should enable their features when starting
196 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
197 */
198int tracing_is_enabled(void)
199{
200 return tracer_enabled;
201}
202
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200203/*
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400204 * trace_buf_size is the size in bytes that is allocated
205 * for a buffer. Note, the number of bytes is always rounded
206 * to page size.
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400207 *
208 * This number is purposely set to a low number of 16384.
209 * If the dump on oops happens, it will be much appreciated
210 * to not have to wait for all that output. Anyway this can be
211 * boot time and run time configurable.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200212 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400213#define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -0400214
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400215static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200216
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200217/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200218static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200219
220/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200221static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200222
223/*
224 * max_tracer_type_len is used to simplify the allocating of
225 * buffers to read userspace tracer names. We keep track of
226 * the longest tracer name registered.
227 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200228static int max_tracer_type_len;
229
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200230/*
231 * trace_types_lock is used to protect the trace_types list.
232 * This lock is also used to keep user access serialized.
233 * Accesses from userspace will grab this lock while userspace
234 * activities happen inside the kernel.
235 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200236static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200237
238/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200239static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
240
Steven Rostedtee6bce52008-11-12 17:52:37 -0500241/* trace_flags holds trace_options default values */
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500242unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200243 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO;
Ingo Molnar4e655512008-05-12 21:20:52 +0200244
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200245/**
246 * trace_wake_up - wake up tasks waiting for trace input
247 *
248 * Simply wakes up any task that is blocked on the trace_wait
249 * queue. These is used with trace_poll for tasks polling the trace.
250 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200251void trace_wake_up(void)
252{
Ingo Molnar017730c2008-05-12 21:20:52 +0200253 /*
254 * The runqueue_is_locked() can fail, but this is the best we
255 * have for now:
256 */
257 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200258 wake_up(&trace_wait);
259}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200260
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400261static int __init set_buf_size(char *str)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200262{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400263 unsigned long buf_size;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200264 int ret;
265
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200266 if (!str)
267 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400268 ret = strict_strtoul(str, 0, &buf_size);
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200269 /* nr_entries can not be zero */
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400270 if (ret < 0 || buf_size == 0)
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200271 return 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400272 trace_buf_size = buf_size;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200273 return 1;
274}
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400275__setup("trace_buf_size=", set_buf_size);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200276
Steven Rostedt57f50be2008-05-12 21:20:44 +0200277unsigned long nsecs_to_usecs(unsigned long nsecs)
278{
279 return nsecs / 1000;
280}
281
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200282/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200283static const char *trace_options[] = {
284 "print-parent",
285 "sym-offset",
286 "sym-addr",
287 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200288 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200289 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200290 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200291 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200292 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200293 "sched-tree",
Ingo Molnar5e1607a2009-03-05 10:24:48 +0100294 "trace_printk",
Steven Rostedtb2a866f2008-11-03 23:15:57 -0500295 "ftrace_preempt",
Steven Rostedt9f029e82008-11-12 15:24:24 -0500296 "branch",
Steven Rostedt12ef7d42008-11-12 17:52:38 -0500297 "annotate",
Török Edwin02b67512008-11-22 13:28:47 +0200298 "userstacktrace",
Török Edwinb54d3de2008-11-22 13:28:48 +0200299 "sym-userobj",
Frederic Weisbecker66896a82008-12-13 20:18:13 +0100300 "printk-msg-only",
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -0200301 "context-info",
Steven Rostedtc032ef642009-03-04 20:34:24 -0500302 "latency-format",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200303 NULL
304};
305
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200306/*
307 * ftrace_max_lock is used to protect the swapping of buffers
308 * when taking a max snapshot. The buffers themselves are
309 * protected by per_cpu spinlocks. But the action of the swap
310 * needs its own lock.
311 *
312 * This is defined as a raw_spinlock_t in order to help
313 * with performance when lockdep debugging is enabled.
314 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200315static raw_spinlock_t ftrace_max_lock =
316 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200317
318/*
319 * Copy the new maximum trace into the separate maximum-trace
320 * structure. (this way the maximum trace is permanently saved,
321 * for later retrieval via /debugfs/tracing/latency_trace)
322 */
Ingo Molnare309b412008-05-12 21:20:51 +0200323static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200324__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
325{
326 struct trace_array_cpu *data = tr->data[cpu];
327
328 max_tr.cpu = cpu;
329 max_tr.time_start = data->preempt_timestamp;
330
331 data = max_tr.data[cpu];
332 data->saved_latency = tracing_max_latency;
333
334 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
335 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100336 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200337 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
338 data->policy = tsk->policy;
339 data->rt_priority = tsk->rt_priority;
340
341 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500342 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200343}
344
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200345ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
346{
347 int len;
348 int ret;
349
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500350 if (!cnt)
351 return 0;
352
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200353 if (s->len <= s->readpos)
354 return -EBUSY;
355
356 len = s->len - s->readpos;
357 if (cnt > len)
358 cnt = len;
359 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500360 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200361 return -EFAULT;
362
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500363 cnt -= ret;
364
Steven Rostedte74da522009-03-04 20:31:11 -0500365 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200366 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200367}
368
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200369ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
370{
371 int len;
372 void *ret;
373
374 if (s->len <= s->readpos)
375 return -EBUSY;
376
377 len = s->len - s->readpos;
378 if (cnt > len)
379 cnt = len;
380 ret = memcpy(buf, s->buffer + s->readpos, cnt);
381 if (!ret)
382 return -EFAULT;
383
Steven Rostedte74da522009-03-04 20:31:11 -0500384 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200385 return cnt;
386}
387
Ingo Molnare309b412008-05-12 21:20:51 +0200388static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200389trace_print_seq(struct seq_file *m, struct trace_seq *s)
390{
391 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
392
393 s->buffer[len] = 0;
394 seq_puts(m, s->buffer);
395
Steven Rostedtf9520752009-03-02 14:04:40 -0500396 trace_seq_init(s);
Steven Rostedt214023c2008-05-12 21:20:46 +0200397}
398
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200399/**
400 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
401 * @tr: tracer
402 * @tsk: the task with the latency
403 * @cpu: The cpu that initiated the trace.
404 *
405 * Flip the buffers between the @tr and the max_tr and record information
406 * about which task was the cause of this latency.
407 */
Ingo Molnare309b412008-05-12 21:20:51 +0200408void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200409update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
410{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400411 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200412
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200413 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200414 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400415
416 tr->buffer = max_tr.buffer;
417 max_tr.buffer = buf;
418
Steven Rostedtd7690412008-10-01 00:29:53 -0400419 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400420 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400421 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200422
423 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200424 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200425}
426
427/**
428 * update_max_tr_single - only copy one trace over, and reset the rest
429 * @tr - tracer
430 * @tsk - task with the latency
431 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200432 *
433 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200434 */
Ingo Molnare309b412008-05-12 21:20:51 +0200435void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200436update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
437{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400438 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200439
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200440 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200441 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200442
Steven Rostedtd7690412008-10-01 00:29:53 -0400443 ftrace_disable_cpu();
444
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400445 ring_buffer_reset(max_tr.buffer);
446 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
447
Steven Rostedtd7690412008-10-01 00:29:53 -0400448 ftrace_enable_cpu();
449
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500450 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200451
452 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200453 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200454}
455
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200456/**
457 * register_tracer - register a tracer with the ftrace system.
458 * @type - the plugin for the tracer
459 *
460 * Register a new plugin tracer.
461 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200462int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100463__releases(kernel_lock)
464__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200465{
466 struct tracer *t;
467 int len;
468 int ret = 0;
469
470 if (!type->name) {
471 pr_info("Tracer must have a name\n");
472 return -1;
473 }
474
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100475 /*
476 * When this gets called we hold the BKL which means that
477 * preemption is disabled. Various trace selftests however
478 * need to disable and enable preemption for successful tests.
479 * So we drop the BKL here and grab it after the tests again.
480 */
481 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200482 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100483
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100484 tracing_selftest_running = true;
485
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200486 for (t = trace_types; t; t = t->next) {
487 if (strcmp(type->name, t->name) == 0) {
488 /* already found */
489 pr_info("Trace %s already registered\n",
490 type->name);
491 ret = -1;
492 goto out;
493 }
494 }
495
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100496 if (!type->set_flag)
497 type->set_flag = &dummy_set_flag;
498 if (!type->flags)
499 type->flags = &dummy_tracer_flags;
500 else
501 if (!type->flags->opts)
502 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100503 if (!type->wait_pipe)
504 type->wait_pipe = default_wait_pipe;
505
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100506
Steven Rostedt60a11772008-05-12 21:20:44 +0200507#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500508 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200509 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200510 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200511 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100512
Steven Rostedt60a11772008-05-12 21:20:44 +0200513 /*
514 * Run a selftest on this tracer.
515 * Here we reset the trace buffer, and set the current
516 * tracer to be this tracer. The tracer can then run some
517 * internal tracing to verify that everything is in order.
518 * If we fail, we do not register this tracer.
519 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100520 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400521 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100522
Steven Rostedt60a11772008-05-12 21:20:44 +0200523 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200524 /* the test is responsible for initializing and enabling */
525 pr_info("Testing tracer %s: ", type->name);
526 ret = type->selftest(type, tr);
527 /* the test is responsible for resetting too */
528 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200529 if (ret) {
530 printk(KERN_CONT "FAILED!\n");
531 goto out;
532 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200533 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100534 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400535 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100536
Steven Rostedt60a11772008-05-12 21:20:44 +0200537 printk(KERN_CONT "PASSED\n");
538 }
539#endif
540
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200541 type->next = trace_types;
542 trace_types = type;
543 len = strlen(type->name);
544 if (len > max_tracer_type_len)
545 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200546
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200547 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100548 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200549 mutex_unlock(&trace_types_lock);
550
Steven Rostedtdac74942009-02-05 01:13:38 -0500551 if (ret || !default_bootup_tracer)
552 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500553
Steven Rostedtdac74942009-02-05 01:13:38 -0500554 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
555 goto out_unlock;
556
557 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
558 /* Do we want this tracer to start on bootup? */
559 tracing_set_tracer(type->name);
560 default_bootup_tracer = NULL;
561 /* disable other selftests, since this will break it. */
562 tracing_selftest_disabled = 1;
563#ifdef CONFIG_FTRACE_STARTUP_TEST
564 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
565 type->name);
566#endif
567
568 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500569 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200570 return ret;
571}
572
573void unregister_tracer(struct tracer *type)
574{
575 struct tracer **t;
576 int len;
577
578 mutex_lock(&trace_types_lock);
579 for (t = &trace_types; *t; t = &(*t)->next) {
580 if (*t == type)
581 goto found;
582 }
583 pr_info("Trace %s not registered\n", type->name);
584 goto out;
585
586 found:
587 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200588
589 if (type == current_trace && tracer_enabled) {
590 tracer_enabled = 0;
591 tracing_stop();
592 if (current_trace->stop)
593 current_trace->stop(&global_trace);
594 current_trace = &nop_trace;
595 }
596
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200597 if (strlen(type->name) != max_tracer_type_len)
598 goto out;
599
600 max_tracer_type_len = 0;
601 for (t = &trace_types; *t; t = &(*t)->next) {
602 len = strlen((*t)->name);
603 if (len > max_tracer_type_len)
604 max_tracer_type_len = len;
605 }
606 out:
607 mutex_unlock(&trace_types_lock);
608}
609
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400610void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200611{
Steven Rostedtd7690412008-10-01 00:29:53 -0400612 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400613 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400614 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200615}
616
Pekka J Enberg213cc062008-12-19 12:08:39 +0200617void tracing_reset_online_cpus(struct trace_array *tr)
618{
619 int cpu;
620
621 tr->time_start = ftrace_now(tr->cpu);
622
623 for_each_online_cpu(cpu)
624 tracing_reset(tr, cpu);
625}
626
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200627#define SAVED_CMDLINES 128
628static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
629static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
630static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
631static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100632static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200633
Steven Rostedt25b0b442008-05-12 21:21:00 +0200634/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100635static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200636
637static void trace_init_cmdlines(void)
638{
639 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
640 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
641 cmdline_idx = 0;
642}
643
Steven Rostedt0f048702008-11-05 16:05:44 -0500644static int trace_stop_count;
645static DEFINE_SPINLOCK(tracing_start_lock);
646
647/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500648 * ftrace_off_permanent - disable all ftrace code permanently
649 *
650 * This should only be called when a serious anomally has
651 * been detected. This will turn off the function tracing,
652 * ring buffers, and other tracing utilites. It takes no
653 * locks and can be called from any context.
654 */
655void ftrace_off_permanent(void)
656{
657 tracing_disabled = 1;
658 ftrace_stop();
659 tracing_off_permanent();
660}
661
662/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500663 * tracing_start - quick start of the tracer
664 *
665 * If tracing is enabled but was stopped by tracing_stop,
666 * this will start the tracer back up.
667 */
668void tracing_start(void)
669{
670 struct ring_buffer *buffer;
671 unsigned long flags;
672
673 if (tracing_disabled)
674 return;
675
676 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500677 if (--trace_stop_count) {
678 if (trace_stop_count < 0) {
679 /* Someone screwed up their debugging */
680 WARN_ON_ONCE(1);
681 trace_stop_count = 0;
682 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500683 goto out;
684 }
685
686
687 buffer = global_trace.buffer;
688 if (buffer)
689 ring_buffer_record_enable(buffer);
690
691 buffer = max_tr.buffer;
692 if (buffer)
693 ring_buffer_record_enable(buffer);
694
695 ftrace_start();
696 out:
697 spin_unlock_irqrestore(&tracing_start_lock, flags);
698}
699
700/**
701 * tracing_stop - quick stop of the tracer
702 *
703 * Light weight way to stop tracing. Use in conjunction with
704 * tracing_start.
705 */
706void tracing_stop(void)
707{
708 struct ring_buffer *buffer;
709 unsigned long flags;
710
711 ftrace_stop();
712 spin_lock_irqsave(&tracing_start_lock, flags);
713 if (trace_stop_count++)
714 goto out;
715
716 buffer = global_trace.buffer;
717 if (buffer)
718 ring_buffer_record_disable(buffer);
719
720 buffer = max_tr.buffer;
721 if (buffer)
722 ring_buffer_record_disable(buffer);
723
724 out:
725 spin_unlock_irqrestore(&tracing_start_lock, flags);
726}
727
Ingo Molnare309b412008-05-12 21:20:51 +0200728void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200729
Ingo Molnare309b412008-05-12 21:20:51 +0200730static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200731{
732 unsigned map;
733 unsigned idx;
734
735 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
736 return;
737
738 /*
739 * It's not the end of the world if we don't get
740 * the lock, but we also don't want to spin
741 * nor do we want to disable interrupts,
742 * so if we miss here, then better luck next time.
743 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100744 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200745 return;
746
747 idx = map_pid_to_cmdline[tsk->pid];
748 if (idx >= SAVED_CMDLINES) {
749 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
750
751 map = map_cmdline_to_pid[idx];
752 if (map <= PID_MAX_DEFAULT)
753 map_pid_to_cmdline[map] = (unsigned)-1;
754
755 map_pid_to_cmdline[tsk->pid] = idx;
756
757 cmdline_idx = idx;
758 }
759
760 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
761
Peter Zijlstraefed7922009-03-04 12:32:55 +0100762 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200763}
764
Steven Rostedt660c7f92008-11-26 00:16:26 -0500765char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200766{
767 char *cmdline = "<...>";
768 unsigned map;
769
770 if (!pid)
771 return "<idle>";
772
773 if (pid > PID_MAX_DEFAULT)
774 goto out;
775
776 map = map_pid_to_cmdline[pid];
777 if (map >= SAVED_CMDLINES)
778 goto out;
779
780 cmdline = saved_cmdlines[map];
781
782 out:
783 return cmdline;
784}
785
Ingo Molnare309b412008-05-12 21:20:51 +0200786void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200787{
788 if (atomic_read(&trace_record_cmdline_disabled))
789 return;
790
791 trace_save_cmdline(tsk);
792}
793
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300794void
Steven Rostedt38697052008-10-01 13:14:09 -0400795tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
796 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200797{
798 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200799
Steven Rostedt777e2082008-09-29 23:02:42 -0400800 entry->preempt_count = pc & 0xff;
801 entry->pid = (tsk) ? tsk->pid : 0;
Steven Rostedtef180122009-03-10 14:10:56 -0400802 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400803 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400804#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400805 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400806#else
807 TRACE_FLAG_IRQS_NOSUPPORT |
808#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200809 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
810 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
811 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
812}
813
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200814struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
815 unsigned char type,
816 unsigned long len,
817 unsigned long flags, int pc)
818{
819 struct ring_buffer_event *event;
820
821 event = ring_buffer_lock_reserve(tr->buffer, len);
822 if (event != NULL) {
823 struct trace_entry *ent = ring_buffer_event_data(event);
824
825 tracing_generic_entry_update(ent, flags, pc);
826 ent->type = type;
827 }
828
829 return event;
830}
831static void ftrace_trace_stack(struct trace_array *tr,
832 unsigned long flags, int skip, int pc);
833static void ftrace_trace_userstack(struct trace_array *tr,
834 unsigned long flags, int pc);
835
836void trace_buffer_unlock_commit(struct trace_array *tr,
837 struct ring_buffer_event *event,
838 unsigned long flags, int pc)
839{
840 ring_buffer_unlock_commit(tr->buffer, event);
841
842 ftrace_trace_stack(tr, flags, 6, pc);
843 ftrace_trace_userstack(tr, flags, pc);
844 trace_wake_up();
845}
846
Steven Rostedtef5580d2009-02-27 19:38:04 -0500847struct ring_buffer_event *
848trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
849 unsigned long flags, int pc)
850{
851 return trace_buffer_lock_reserve(&global_trace,
852 type, len, flags, pc);
853}
854
855void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
856 unsigned long flags, int pc)
857{
858 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
859}
860
Ingo Molnare309b412008-05-12 21:20:51 +0200861void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500862trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400863 unsigned long ip, unsigned long parent_ip, unsigned long flags,
864 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200865{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400866 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400867 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200868
Steven Rostedtd7690412008-10-01 00:29:53 -0400869 /* If we are reading the ring buffer, don't trace */
870 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
871 return;
872
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200873 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
874 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400875 if (!event)
876 return;
877 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400878 entry->ip = ip;
879 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200880 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200881}
882
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100883#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100884static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100885 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100886 unsigned long flags,
887 int pc)
888{
889 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100890 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100891
892 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
893 return;
894
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200895 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
896 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100897 if (!event)
898 return;
899 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100900 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200901 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100902}
903
904static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100905 struct ftrace_graph_ret *trace,
906 unsigned long flags,
907 int pc)
908{
909 struct ring_buffer_event *event;
910 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100911
912 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
913 return;
914
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200915 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
916 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100917 if (!event)
918 return;
919 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100920 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200921 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100922}
923#endif
924
Ingo Molnare309b412008-05-12 21:20:51 +0200925void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200926ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400927 unsigned long ip, unsigned long parent_ip, unsigned long flags,
928 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200929{
930 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500931 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200932}
933
Steven Rostedt53614992009-01-15 19:12:40 -0500934static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500935 unsigned long flags,
936 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200937{
Al Viroc2c80522008-10-31 19:50:41 +0000938#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400939 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400940 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200941 struct stack_trace trace;
942
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200943 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
944 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400945 if (!event)
946 return;
947 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400948 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200949
950 trace.nr_entries = 0;
951 trace.max_entries = FTRACE_STACK_ENTRIES;
952 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400953 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200954
955 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200956 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000957#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200958}
959
Steven Rostedt53614992009-01-15 19:12:40 -0500960static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500961 unsigned long flags,
962 int skip, int pc)
963{
964 if (!(trace_flags & TRACE_ITER_STACKTRACE))
965 return;
966
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500967 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500968}
969
Steven Rostedt38697052008-10-01 13:14:09 -0400970void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400971 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500972 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400973{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500974 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400975}
976
Török Edwin02b67512008-11-22 13:28:47 +0200977static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500978 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +0200979{
Török Edwinc7425ac2008-11-28 11:17:56 +0200980#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +0200981 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +0200982 struct userstack_entry *entry;
983 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +0200984
985 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
986 return;
987
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200988 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
989 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +0200990 if (!event)
991 return;
992 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +0200993
994 memset(&entry->caller, 0, sizeof(entry->caller));
995
996 trace.nr_entries = 0;
997 trace.max_entries = FTRACE_STACK_ENTRIES;
998 trace.skip = 0;
999 trace.entries = entry->caller;
1000
1001 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001002 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001003#endif
Török Edwin02b67512008-11-22 13:28:47 +02001004}
1005
Hannes Eder4fd27352009-02-10 19:44:12 +01001006#ifdef UNUSED
1007static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001008{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001009 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001010}
Hannes Eder4fd27352009-02-10 19:44:12 +01001011#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001012
Steven Rostedt38697052008-10-01 13:14:09 -04001013static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001014ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001015 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1016 int pc)
Ingo Molnara4feb832008-05-12 21:21:02 +02001017{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001018 struct ring_buffer_event *event;
Ingo Molnara4feb832008-05-12 21:21:02 +02001019 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001020 struct special_entry *entry;
Ingo Molnara4feb832008-05-12 21:21:02 +02001021
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001022 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1023 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001024 if (!event)
1025 return;
1026 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001027 entry->arg1 = arg1;
1028 entry->arg2 = arg2;
1029 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001030 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb832008-05-12 21:21:02 +02001031}
1032
1033void
Steven Rostedt38697052008-10-01 13:14:09 -04001034__trace_special(void *__tr, void *__data,
1035 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1036{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001037 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001038}
1039
1040void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001041tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001042 struct task_struct *prev,
1043 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001044 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001045{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001046 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001047 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001048
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001049 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1050 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001051 if (!event)
1052 return;
1053 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001054 entry->prev_pid = prev->pid;
1055 entry->prev_prio = prev->prio;
1056 entry->prev_state = prev->state;
1057 entry->next_pid = next->pid;
1058 entry->next_prio = next->prio;
1059 entry->next_state = next->state;
1060 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001061 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001062}
1063
Ingo Molnar57422792008-05-12 21:20:51 +02001064void
1065tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001066 struct task_struct *wakee,
1067 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001068 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001069{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001070 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001071 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001072
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001073 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1074 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001075 if (!event)
1076 return;
1077 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001078 entry->prev_pid = curr->pid;
1079 entry->prev_prio = curr->prio;
1080 entry->prev_state = curr->state;
1081 entry->next_pid = wakee->pid;
1082 entry->next_prio = wakee->prio;
1083 entry->next_state = wakee->state;
1084 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001085
1086 ring_buffer_unlock_commit(tr->buffer, event);
1087 ftrace_trace_stack(tr, flags, 6, pc);
1088 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001089}
1090
Steven Rostedt4902f882008-05-22 00:22:18 -04001091void
1092ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1093{
1094 struct trace_array *tr = &global_trace;
1095 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001096 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001097 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001098 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001099
Steven Rostedtc76f0692008-11-07 22:36:02 -05001100 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001101 return;
1102
Steven Rostedt38697052008-10-01 13:14:09 -04001103 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001104 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001105 cpu = raw_smp_processor_id();
1106 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001107
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001108 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001109 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001110
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001111 atomic_dec(&data->disabled);
1112 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001113}
1114
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001115#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001116int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001117{
1118 struct trace_array *tr = &global_trace;
1119 struct trace_array_cpu *data;
1120 unsigned long flags;
1121 long disabled;
1122 int cpu;
1123 int pc;
1124
Steven Rostedt804a6852008-12-03 15:36:59 -05001125 if (!ftrace_trace_task(current))
1126 return 0;
1127
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001128 if (!ftrace_graph_addr(trace->func))
1129 return 0;
1130
Steven Rostedta5e25882008-12-02 15:34:05 -05001131 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001132 cpu = raw_smp_processor_id();
1133 data = tr->data[cpu];
1134 disabled = atomic_inc_return(&data->disabled);
1135 if (likely(disabled == 1)) {
1136 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001137 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001138 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001139 /* Only do the atomic if it is not already set */
1140 if (!test_tsk_trace_graph(current))
1141 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001142 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001143 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001144
1145 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001146}
1147
1148void trace_graph_return(struct ftrace_graph_ret *trace)
1149{
1150 struct trace_array *tr = &global_trace;
1151 struct trace_array_cpu *data;
1152 unsigned long flags;
1153 long disabled;
1154 int cpu;
1155 int pc;
1156
Steven Rostedta5e25882008-12-02 15:34:05 -05001157 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001158 cpu = raw_smp_processor_id();
1159 data = tr->data[cpu];
1160 disabled = atomic_inc_return(&data->disabled);
1161 if (likely(disabled == 1)) {
1162 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001163 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001164 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001165 if (!trace->depth)
1166 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001167 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001168 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001169}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001170#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001171
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001172
1173/**
1174 * trace_vprintk - write binary msg to tracing buffer
1175 *
1176 */
1177int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1178{
Steven Rostedt80370cb2009-03-10 17:16:35 -04001179 static raw_spinlock_t trace_buf_lock =
1180 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001181 static u32 trace_buf[TRACE_BUF_SIZE];
1182
1183 struct ring_buffer_event *event;
1184 struct trace_array *tr = &global_trace;
1185 struct trace_array_cpu *data;
1186 struct print_entry *entry;
1187 unsigned long flags;
1188 int resched;
1189 int cpu, len = 0, size, pc;
1190
1191 if (unlikely(tracing_selftest_running || tracing_disabled))
1192 return 0;
1193
1194 /* Don't pollute graph traces with trace_vprintk internals */
1195 pause_graph_tracing();
1196
1197 pc = preempt_count();
1198 resched = ftrace_preempt_disable();
1199 cpu = raw_smp_processor_id();
1200 data = tr->data[cpu];
1201
1202 if (unlikely(atomic_read(&data->disabled)))
1203 goto out;
1204
Steven Rostedt80370cb2009-03-10 17:16:35 -04001205 /* Lockdep uses trace_printk for lock tracing */
1206 local_irq_save(flags);
1207 __raw_spin_lock(&trace_buf_lock);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001208 len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1209
1210 if (len > TRACE_BUF_SIZE || len < 0)
1211 goto out_unlock;
1212
1213 size = sizeof(*entry) + sizeof(u32) * len;
1214 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
1215 if (!event)
1216 goto out_unlock;
1217 entry = ring_buffer_event_data(event);
1218 entry->ip = ip;
1219 entry->depth = depth;
1220 entry->fmt = fmt;
1221
1222 memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1223 ring_buffer_unlock_commit(tr->buffer, event);
1224
1225out_unlock:
Steven Rostedt80370cb2009-03-10 17:16:35 -04001226 __raw_spin_unlock(&trace_buf_lock);
1227 local_irq_restore(flags);
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001228
1229out:
1230 ftrace_preempt_enable(resched);
1231 unpause_graph_tracing();
1232
1233 return len;
1234}
1235EXPORT_SYMBOL_GPL(trace_vprintk);
1236
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001237enum trace_file_type {
1238 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001239 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240};
1241
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001242static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001243{
Steven Rostedtd7690412008-10-01 00:29:53 -04001244 /* Don't allow ftrace to trace into the ring buffers */
1245 ftrace_disable_cpu();
1246
Steven Rostedt5a90f572008-09-03 17:42:51 -04001247 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001248 if (iter->buffer_iter[iter->cpu])
1249 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1250
1251 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001252}
1253
Ingo Molnare309b412008-05-12 21:20:51 +02001254static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001255peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001256{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001257 struct ring_buffer_event *event;
1258 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001259
Steven Rostedtd7690412008-10-01 00:29:53 -04001260 /* Don't allow ftrace to trace into the ring buffers */
1261 ftrace_disable_cpu();
1262
1263 if (buf_iter)
1264 event = ring_buffer_iter_peek(buf_iter, ts);
1265 else
1266 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1267
1268 ftrace_enable_cpu();
1269
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001270 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001271}
Steven Rostedtd7690412008-10-01 00:29:53 -04001272
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001273static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001274__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001275{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001276 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001277 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001278 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001279 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001280 int next_cpu = -1;
1281 int cpu;
1282
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001283 /*
1284 * If we are in a per_cpu trace file, don't bother by iterating over
1285 * all cpu and peek directly.
1286 */
1287 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1288 if (ring_buffer_empty_cpu(buffer, cpu_file))
1289 return NULL;
1290 ent = peek_next_entry(iter, cpu_file, ent_ts);
1291 if (ent_cpu)
1292 *ent_cpu = cpu_file;
1293
1294 return ent;
1295 }
1296
Steven Rostedtab464282008-05-12 21:21:00 +02001297 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001298
1299 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001300 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001301
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001302 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001303
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001304 /*
1305 * Pick the entry with the smallest timestamp:
1306 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001307 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308 next = ent;
1309 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001310 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001311 }
1312 }
1313
1314 if (ent_cpu)
1315 *ent_cpu = next_cpu;
1316
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001317 if (ent_ts)
1318 *ent_ts = next_ts;
1319
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001320 return next;
1321}
1322
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001323/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001324struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1325 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001326{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001327 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001328}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001329
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001330/* Find the next real entry, and increment the iterator to the next entry */
1331static void *find_next_entry_inc(struct trace_iterator *iter)
1332{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001333 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001334
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001335 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001336 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001337
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001338 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001339}
1340
Ingo Molnare309b412008-05-12 21:20:51 +02001341static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001342{
Steven Rostedtd7690412008-10-01 00:29:53 -04001343 /* Don't allow ftrace to trace into the ring buffers */
1344 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001345 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001346 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001347}
1348
Ingo Molnare309b412008-05-12 21:20:51 +02001349static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001350{
1351 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001352 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001353 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001354
1355 (*pos)++;
1356
1357 /* can't go backwards */
1358 if (iter->idx > i)
1359 return NULL;
1360
1361 if (iter->idx < 0)
1362 ent = find_next_entry_inc(iter);
1363 else
1364 ent = iter;
1365
1366 while (ent && iter->idx < i)
1367 ent = find_next_entry_inc(iter);
1368
1369 iter->pos = *pos;
1370
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001371 return ent;
1372}
1373
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001374/*
1375 * No necessary locking here. The worst thing which can
1376 * happen is loosing events consumed at the same time
1377 * by a trace_pipe reader.
1378 * Other than that, we don't risk to crash the ring buffer
1379 * because it serializes the readers.
1380 *
1381 * The current tracer is copied to avoid a global locking
1382 * all around.
1383 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384static void *s_start(struct seq_file *m, loff_t *pos)
1385{
1386 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001387 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001388 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001389 void *p = NULL;
1390 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001391 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001392
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001393 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001394 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001395 if (unlikely(old_tracer != current_trace && current_trace)) {
1396 old_tracer = current_trace;
1397 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001398 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001399 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001400
1401 atomic_inc(&trace_record_cmdline_disabled);
1402
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001403 if (*pos != iter->pos) {
1404 iter->ent = NULL;
1405 iter->cpu = 0;
1406 iter->idx = -1;
1407
Steven Rostedtd7690412008-10-01 00:29:53 -04001408 ftrace_disable_cpu();
1409
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001410 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1411 for_each_tracing_cpu(cpu)
1412 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1413 } else
1414 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1415
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001416
Steven Rostedtd7690412008-10-01 00:29:53 -04001417 ftrace_enable_cpu();
1418
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001419 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1420 ;
1421
1422 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001423 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001424 p = s_next(m, p, &l);
1425 }
1426
1427 return p;
1428}
1429
1430static void s_stop(struct seq_file *m, void *p)
1431{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001432 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001433}
1434
Ingo Molnare309b412008-05-12 21:20:51 +02001435static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001436{
Michael Ellermana6168352008-08-20 16:36:11 -07001437 seq_puts(m, "# _------=> CPU# \n");
1438 seq_puts(m, "# / _-----=> irqs-off \n");
1439 seq_puts(m, "# | / _----=> need-resched \n");
1440 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1441 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1442 seq_puts(m, "# |||| / \n");
1443 seq_puts(m, "# ||||| delay \n");
1444 seq_puts(m, "# cmd pid ||||| time | caller \n");
1445 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001446}
1447
Ingo Molnare309b412008-05-12 21:20:51 +02001448static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001449{
Michael Ellermana6168352008-08-20 16:36:11 -07001450 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1451 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001452}
1453
1454
Ingo Molnare309b412008-05-12 21:20:51 +02001455static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001456print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1457{
1458 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1459 struct trace_array *tr = iter->tr;
1460 struct trace_array_cpu *data = tr->data[tr->cpu];
1461 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001462 unsigned long total;
1463 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001464 const char *name = "preemption";
1465
1466 if (type)
1467 name = type->name;
1468
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001469 entries = ring_buffer_entries(iter->tr->buffer);
1470 total = entries +
1471 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001472
1473 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1474 name, UTS_RELEASE);
1475 seq_puts(m, "-----------------------------------"
1476 "---------------------------------\n");
1477 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1478 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001479 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001481 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001482 tr->cpu,
1483#if defined(CONFIG_PREEMPT_NONE)
1484 "server",
1485#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1486 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001487#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488 "preempt",
1489#else
1490 "unknown",
1491#endif
1492 /* These are reserved for later use */
1493 0, 0, 0, 0);
1494#ifdef CONFIG_SMP
1495 seq_printf(m, " #P:%d)\n", num_online_cpus());
1496#else
1497 seq_puts(m, ")\n");
1498#endif
1499 seq_puts(m, " -----------------\n");
1500 seq_printf(m, " | task: %.16s-%d "
1501 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1502 data->comm, data->pid, data->uid, data->nice,
1503 data->policy, data->rt_priority);
1504 seq_puts(m, " -----------------\n");
1505
1506 if (data->critical_start) {
1507 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001508 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1509 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001510 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001511 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1512 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001513 seq_puts(m, "\n");
1514 }
1515
1516 seq_puts(m, "\n");
1517}
1518
Steven Rostedta3097202008-11-07 22:36:02 -05001519static void test_cpu_buff_start(struct trace_iterator *iter)
1520{
1521 struct trace_seq *s = &iter->seq;
1522
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001523 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1524 return;
1525
1526 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1527 return;
1528
Rusty Russell44623442009-01-01 10:12:23 +10301529 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001530 return;
1531
Rusty Russell44623442009-01-01 10:12:23 +10301532 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001533 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1534}
1535
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001536static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001537{
Steven Rostedt214023c2008-05-12 21:20:46 +02001538 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001539 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001540 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001541 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001543 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001544
Steven Rostedta3097202008-11-07 22:36:02 -05001545 test_cpu_buff_start(iter);
1546
Steven Rostedtf633cef2008-12-23 23:24:13 -05001547 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001548
1549 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Steven Rostedt27d48be2009-03-04 21:57:29 -05001550 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1551 if (!trace_print_lat_context(iter))
1552 goto partial;
1553 } else {
1554 if (!trace_print_context(iter))
1555 goto partial;
1556 }
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001557 }
1558
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001559 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001560 return event->trace(iter, sym_flags);
1561
1562 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1563 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001564
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001565 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001566partial:
1567 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001568}
1569
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001570static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001571{
1572 struct trace_seq *s = &iter->seq;
1573 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001574 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001575
1576 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001577
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001578 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001579 if (!trace_seq_printf(s, "%d %d %llu ",
1580 entry->pid, iter->cpu, iter->ts))
1581 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001582 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001583
Steven Rostedtf633cef2008-12-23 23:24:13 -05001584 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001585 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001586 return event->raw(iter, 0);
1587
1588 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1589 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001590
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001591 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001592partial:
1593 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001594}
1595
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001596static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001597{
1598 struct trace_seq *s = &iter->seq;
1599 unsigned char newline = '\n';
1600 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001601 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001602
1603 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001604
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001605 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1606 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1607 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1608 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1609 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001610
Steven Rostedtf633cef2008-12-23 23:24:13 -05001611 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001612 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001613 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001614 if (ret != TRACE_TYPE_HANDLED)
1615 return ret;
1616 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001617
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001618 SEQ_PUT_FIELD_RET(s, newline);
1619
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001620 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001621}
1622
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001623static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1624{
1625 struct trace_seq *s = &iter->seq;
1626 struct trace_entry *entry = iter->ent;
1627 struct print_entry *field;
1628 int ret;
1629
1630 trace_assign_type(field, entry);
1631
Frederic Weisbecker769b0442009-03-06 17:21:49 +01001632 ret = trace_seq_bprintf(s, field->fmt, field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001633 if (!ret)
1634 return TRACE_TYPE_PARTIAL_LINE;
1635
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001636 return TRACE_TYPE_HANDLED;
1637}
1638
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001639static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001640{
1641 struct trace_seq *s = &iter->seq;
1642 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001643 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001644
1645 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001646
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001647 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1648 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001649 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001650 SEQ_PUT_FIELD_RET(s, iter->ts);
1651 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001652
Steven Rostedtf633cef2008-12-23 23:24:13 -05001653 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001654 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001655}
1656
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001657static int trace_empty(struct trace_iterator *iter)
1658{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001659 int cpu;
1660
Steven Rostedtab464282008-05-12 21:21:00 +02001661 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001662 if (iter->buffer_iter[cpu]) {
1663 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1664 return 0;
1665 } else {
1666 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1667 return 0;
1668 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001669 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001670
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001671 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001672}
1673
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001674static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001675{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001676 enum print_line_t ret;
1677
1678 if (iter->trace && iter->trace->print_line) {
1679 ret = iter->trace->print_line(iter);
1680 if (ret != TRACE_TYPE_UNHANDLED)
1681 return ret;
1682 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001683
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001684 if (iter->ent->type == TRACE_PRINT &&
1685 trace_flags & TRACE_ITER_PRINTK &&
1686 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1687 return print_printk_msg_only(iter);
1688
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001689 if (trace_flags & TRACE_ITER_BIN)
1690 return print_bin_fmt(iter);
1691
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001692 if (trace_flags & TRACE_ITER_HEX)
1693 return print_hex_fmt(iter);
1694
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001695 if (trace_flags & TRACE_ITER_RAW)
1696 return print_raw_fmt(iter);
1697
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001698 return print_trace_fmt(iter);
1699}
1700
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001701static int s_show(struct seq_file *m, void *v)
1702{
1703 struct trace_iterator *iter = v;
1704
1705 if (iter->ent == NULL) {
1706 if (iter->tr) {
1707 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1708 seq_puts(m, "#\n");
1709 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001710 if (iter->trace && iter->trace->print_header)
1711 iter->trace->print_header(m);
1712 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001713 /* print nothing if the buffers are empty */
1714 if (trace_empty(iter))
1715 return 0;
1716 print_trace_header(m, iter);
1717 if (!(trace_flags & TRACE_ITER_VERBOSE))
1718 print_lat_help_header(m);
1719 } else {
1720 if (!(trace_flags & TRACE_ITER_VERBOSE))
1721 print_func_help_header(m);
1722 }
1723 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001724 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001725 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001726 }
1727
1728 return 0;
1729}
1730
1731static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001732 .start = s_start,
1733 .next = s_next,
1734 .stop = s_stop,
1735 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001736};
1737
Ingo Molnare309b412008-05-12 21:20:51 +02001738static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001739__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001740{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001741 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001742 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001743 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001744 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001745 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001746
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001747 if (tracing_disabled)
1748 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001749
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001750 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001751 if (!iter)
1752 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001753
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001754 /*
1755 * We make a copy of the current tracer to avoid concurrent
1756 * changes on it while we are reading.
1757 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001758 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001759 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001760 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001761 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001762
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001763 if (current_trace)
1764 *iter->trace = *current_trace;
1765
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001766 if (current_trace && current_trace->print_max)
1767 iter->tr = &max_tr;
1768 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001769 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001770 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001771 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001772 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001773
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001774 /* Notify the tracer early; before we stop tracing. */
1775 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001776 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001777
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001778 /* Annotate start of buffers if we had overruns */
1779 if (ring_buffer_overruns(iter->tr->buffer))
1780 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1781
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001782 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1783 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001784
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001785 iter->buffer_iter[cpu] =
1786 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001787
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001788 if (!iter->buffer_iter[cpu])
1789 goto fail_buffer;
1790 }
1791 } else {
1792 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001793 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001794 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001795
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001796 if (!iter->buffer_iter[cpu])
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001797 goto fail;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001798 }
1799
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001800 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001801 ret = seq_open(file, &tracer_seq_ops);
1802 if (ret < 0) {
1803 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001804 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001805 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001806
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001807 m = file->private_data;
1808 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001809
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001810 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001811 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001812
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001813 mutex_unlock(&trace_types_lock);
1814
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001815 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001816
1817 fail_buffer:
1818 for_each_tracing_cpu(cpu) {
1819 if (iter->buffer_iter[cpu])
1820 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1821 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001822 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001823 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001824 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001825 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001826
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001827 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001828}
1829
1830int tracing_open_generic(struct inode *inode, struct file *filp)
1831{
Steven Rostedt60a11772008-05-12 21:20:44 +02001832 if (tracing_disabled)
1833 return -ENODEV;
1834
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001835 filp->private_data = inode->i_private;
1836 return 0;
1837}
1838
Hannes Eder4fd27352009-02-10 19:44:12 +01001839static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001840{
1841 struct seq_file *m = (struct seq_file *)file->private_data;
1842 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001843 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001844
1845 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001846 for_each_tracing_cpu(cpu) {
1847 if (iter->buffer_iter[cpu])
1848 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1849 }
1850
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001851 if (iter->trace && iter->trace->close)
1852 iter->trace->close(iter);
1853
1854 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001855 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001856 mutex_unlock(&trace_types_lock);
1857
1858 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001859 mutex_destroy(&iter->mutex);
1860 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001861 kfree(iter);
1862 return 0;
1863}
1864
1865static int tracing_open(struct inode *inode, struct file *file)
1866{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001867 struct trace_iterator *iter;
1868 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001869
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001870 iter = __tracing_open(inode, file);
1871 if (IS_ERR(iter))
1872 ret = PTR_ERR(iter);
Steven Rostedtc032ef642009-03-04 20:34:24 -05001873 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001874 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1875
1876 return ret;
1877}
1878
Ingo Molnare309b412008-05-12 21:20:51 +02001879static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001880t_next(struct seq_file *m, void *v, loff_t *pos)
1881{
1882 struct tracer *t = m->private;
1883
1884 (*pos)++;
1885
1886 if (t)
1887 t = t->next;
1888
1889 m->private = t;
1890
1891 return t;
1892}
1893
1894static void *t_start(struct seq_file *m, loff_t *pos)
1895{
1896 struct tracer *t = m->private;
1897 loff_t l = 0;
1898
1899 mutex_lock(&trace_types_lock);
1900 for (; t && l < *pos; t = t_next(m, t, &l))
1901 ;
1902
1903 return t;
1904}
1905
1906static void t_stop(struct seq_file *m, void *p)
1907{
1908 mutex_unlock(&trace_types_lock);
1909}
1910
1911static int t_show(struct seq_file *m, void *v)
1912{
1913 struct tracer *t = v;
1914
1915 if (!t)
1916 return 0;
1917
1918 seq_printf(m, "%s", t->name);
1919 if (t->next)
1920 seq_putc(m, ' ');
1921 else
1922 seq_putc(m, '\n');
1923
1924 return 0;
1925}
1926
1927static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001928 .start = t_start,
1929 .next = t_next,
1930 .stop = t_stop,
1931 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001932};
1933
1934static int show_traces_open(struct inode *inode, struct file *file)
1935{
1936 int ret;
1937
Steven Rostedt60a11772008-05-12 21:20:44 +02001938 if (tracing_disabled)
1939 return -ENODEV;
1940
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001941 ret = seq_open(file, &show_traces_seq_ops);
1942 if (!ret) {
1943 struct seq_file *m = file->private_data;
1944 m->private = trace_types;
1945 }
1946
1947 return ret;
1948}
1949
Steven Rostedt5e2336a2009-03-05 21:44:55 -05001950static const struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001951 .open = tracing_open,
1952 .read = seq_read,
1953 .llseek = seq_lseek,
1954 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001955};
1956
Steven Rostedt5e2336a2009-03-05 21:44:55 -05001957static const struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001958 .open = show_traces_open,
1959 .read = seq_read,
1960 .release = seq_release,
1961};
1962
Ingo Molnar36dfe922008-05-12 21:20:52 +02001963/*
1964 * Only trace on a CPU if the bitmask is set:
1965 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301966static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001967
1968/*
1969 * The tracer itself will not take this lock, but still we want
1970 * to provide a consistent cpumask to user-space:
1971 */
1972static DEFINE_MUTEX(tracing_cpumask_update_lock);
1973
1974/*
1975 * Temporary storage for the character representation of the
1976 * CPU bitmask (and one more byte for the newline):
1977 */
1978static char mask_str[NR_CPUS + 1];
1979
Ingo Molnarc7078de2008-05-12 21:20:52 +02001980static ssize_t
1981tracing_cpumask_read(struct file *filp, char __user *ubuf,
1982 size_t count, loff_t *ppos)
1983{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001984 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001985
1986 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001987
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301988 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001989 if (count - len < 2) {
1990 count = -EINVAL;
1991 goto out_err;
1992 }
1993 len += sprintf(mask_str + len, "\n");
1994 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1995
1996out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001997 mutex_unlock(&tracing_cpumask_update_lock);
1998
1999 return count;
2000}
2001
2002static ssize_t
2003tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2004 size_t count, loff_t *ppos)
2005{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002006 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302007 cpumask_var_t tracing_cpumask_new;
2008
2009 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2010 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002011
2012 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302013 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002014 if (err)
2015 goto err_unlock;
2016
Steven Rostedta5e25882008-12-02 15:34:05 -05002017 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02002018 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002019 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002020 /*
2021 * Increase/decrease the disabled counter if we are
2022 * about to flip a bit in the cpumask:
2023 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302024 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2025 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002026 atomic_inc(&global_trace.data[cpu]->disabled);
2027 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302028 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2029 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002030 atomic_dec(&global_trace.data[cpu]->disabled);
2031 }
2032 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002033 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002034 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002035
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302036 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002037
Ingo Molnarc7078de2008-05-12 21:20:52 +02002038 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302039 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002040
Ingo Molnarc7078de2008-05-12 21:20:52 +02002041 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002042
2043err_unlock:
2044 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302045 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002046
2047 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002048}
2049
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002050static const struct file_operations tracing_cpumask_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002051 .open = tracing_open_generic,
2052 .read = tracing_cpumask_read,
2053 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002054};
2055
2056static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002057tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002058 size_t cnt, loff_t *ppos)
2059{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002060 struct tracer_opt *trace_opts;
2061 u32 tracer_flags;
2062 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002063 char *buf;
2064 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002065 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002066
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002067
Wenji Huangc3706f02009-02-10 01:03:18 -05002068 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002069 for (i = 0; trace_options[i]; i++) {
2070 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002071 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002072 }
2073
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002074 mutex_lock(&trace_types_lock);
2075 tracer_flags = current_trace->flags->val;
2076 trace_opts = current_trace->flags->opts;
2077
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002078 /*
2079 * Increase the size with names of options specific
2080 * of the current tracer.
2081 */
2082 for (i = 0; trace_opts[i].name; i++) {
2083 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002084 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002085 }
2086
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002087 /* +2 for \n and \0 */
2088 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002089 if (!buf) {
2090 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002091 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002092 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002093
2094 for (i = 0; trace_options[i]; i++) {
2095 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002096 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002097 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002098 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002099 }
2100
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002101 for (i = 0; trace_opts[i].name; i++) {
2102 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002103 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002104 trace_opts[i].name);
2105 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002106 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002107 trace_opts[i].name);
2108 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002109 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002110
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002111 WARN_ON(r >= len + 2);
2112
Ingo Molnar36dfe922008-05-12 21:20:52 +02002113 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002114
2115 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002116 return r;
2117}
2118
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002119/* Try to assign a tracer specific option */
2120static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2121{
2122 struct tracer_flags *trace_flags = trace->flags;
2123 struct tracer_opt *opts = NULL;
2124 int ret = 0, i = 0;
2125 int len;
2126
2127 for (i = 0; trace_flags->opts[i].name; i++) {
2128 opts = &trace_flags->opts[i];
2129 len = strlen(opts->name);
2130
2131 if (strncmp(cmp, opts->name, len) == 0) {
2132 ret = trace->set_flag(trace_flags->val,
2133 opts->bit, !neg);
2134 break;
2135 }
2136 }
2137 /* Not found */
2138 if (!trace_flags->opts[i].name)
2139 return -EINVAL;
2140
2141 /* Refused to handle */
2142 if (ret)
2143 return ret;
2144
2145 if (neg)
2146 trace_flags->val &= ~opts->bit;
2147 else
2148 trace_flags->val |= opts->bit;
2149
2150 return 0;
2151}
2152
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002153static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002154tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002155 size_t cnt, loff_t *ppos)
2156{
2157 char buf[64];
2158 char *cmp = buf;
2159 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002160 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002161 int i;
2162
Steven Rostedtcffae432008-05-12 21:21:00 +02002163 if (cnt >= sizeof(buf))
2164 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002165
2166 if (copy_from_user(&buf, ubuf, cnt))
2167 return -EFAULT;
2168
2169 buf[cnt] = 0;
2170
2171 if (strncmp(buf, "no", 2) == 0) {
2172 neg = 1;
2173 cmp += 2;
2174 }
2175
2176 for (i = 0; trace_options[i]; i++) {
2177 int len = strlen(trace_options[i]);
2178
2179 if (strncmp(cmp, trace_options[i], len) == 0) {
2180 if (neg)
2181 trace_flags &= ~(1 << i);
2182 else
2183 trace_flags |= (1 << i);
2184 break;
2185 }
2186 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002187
2188 /* If no option could be set, test the specific tracer options */
2189 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002190 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002191 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002192 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002193 if (ret)
2194 return ret;
2195 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002196
2197 filp->f_pos += cnt;
2198
2199 return cnt;
2200}
2201
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002202static const struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002203 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002204 .read = tracing_trace_options_read,
2205 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002206};
2207
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002208static const char readme_msg[] =
2209 "tracing mini-HOWTO:\n\n"
2210 "# mkdir /debug\n"
2211 "# mount -t debugfs nodev /debug\n\n"
2212 "# cat /debug/tracing/available_tracers\n"
2213 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2214 "# cat /debug/tracing/current_tracer\n"
2215 "none\n"
2216 "# echo sched_switch > /debug/tracing/current_tracer\n"
2217 "# cat /debug/tracing/current_tracer\n"
2218 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002219 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002220 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002221 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002222 "# echo 1 > /debug/tracing/tracing_enabled\n"
2223 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2224 "echo 0 > /debug/tracing/tracing_enabled\n"
2225;
2226
2227static ssize_t
2228tracing_readme_read(struct file *filp, char __user *ubuf,
2229 size_t cnt, loff_t *ppos)
2230{
2231 return simple_read_from_buffer(ubuf, cnt, ppos,
2232 readme_msg, strlen(readme_msg));
2233}
2234
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002235static const struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002236 .open = tracing_open_generic,
2237 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002238};
2239
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002240static ssize_t
2241tracing_ctrl_read(struct file *filp, char __user *ubuf,
2242 size_t cnt, loff_t *ppos)
2243{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002244 char buf[64];
2245 int r;
2246
Steven Rostedt90369902008-11-05 16:05:44 -05002247 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002248 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002249}
2250
2251static ssize_t
2252tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2253 size_t cnt, loff_t *ppos)
2254{
2255 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002256 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002257 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002258 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002259
Steven Rostedtcffae432008-05-12 21:21:00 +02002260 if (cnt >= sizeof(buf))
2261 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002262
2263 if (copy_from_user(&buf, ubuf, cnt))
2264 return -EFAULT;
2265
2266 buf[cnt] = 0;
2267
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002268 ret = strict_strtoul(buf, 10, &val);
2269 if (ret < 0)
2270 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002271
2272 val = !!val;
2273
2274 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002275 if (tracer_enabled ^ val) {
2276 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002277 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002278 if (current_trace->start)
2279 current_trace->start(tr);
2280 tracing_start();
2281 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002282 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002283 tracing_stop();
2284 if (current_trace->stop)
2285 current_trace->stop(tr);
2286 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002287 }
2288 mutex_unlock(&trace_types_lock);
2289
2290 filp->f_pos += cnt;
2291
2292 return cnt;
2293}
2294
2295static ssize_t
2296tracing_set_trace_read(struct file *filp, char __user *ubuf,
2297 size_t cnt, loff_t *ppos)
2298{
2299 char buf[max_tracer_type_len+2];
2300 int r;
2301
2302 mutex_lock(&trace_types_lock);
2303 if (current_trace)
2304 r = sprintf(buf, "%s\n", current_trace->name);
2305 else
2306 r = sprintf(buf, "\n");
2307 mutex_unlock(&trace_types_lock);
2308
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002309 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002310}
2311
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002312int tracer_init(struct tracer *t, struct trace_array *tr)
2313{
2314 tracing_reset_online_cpus(tr);
2315 return t->init(tr);
2316}
2317
Steven Rostedt577b7852009-02-26 23:43:05 -05002318struct trace_option_dentry;
2319
2320static struct trace_option_dentry *
2321create_trace_option_files(struct tracer *tracer);
2322
2323static void
2324destroy_trace_option_files(struct trace_option_dentry *topts);
2325
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002326static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002327{
Steven Rostedt577b7852009-02-26 23:43:05 -05002328 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002329 struct trace_array *tr = &global_trace;
2330 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002331 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002332
2333 mutex_lock(&trace_types_lock);
2334 for (t = trace_types; t; t = t->next) {
2335 if (strcmp(t->name, buf) == 0)
2336 break;
2337 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002338 if (!t) {
2339 ret = -EINVAL;
2340 goto out;
2341 }
2342 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002343 goto out;
2344
Steven Rostedt9f029e82008-11-12 15:24:24 -05002345 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002346 if (current_trace && current_trace->reset)
2347 current_trace->reset(tr);
2348
Steven Rostedt577b7852009-02-26 23:43:05 -05002349 destroy_trace_option_files(topts);
2350
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002351 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002352
2353 topts = create_trace_option_files(current_trace);
2354
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002355 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002356 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002357 if (ret)
2358 goto out;
2359 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002360
Steven Rostedt9f029e82008-11-12 15:24:24 -05002361 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002362 out:
2363 mutex_unlock(&trace_types_lock);
2364
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002365 return ret;
2366}
2367
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002368static ssize_t
2369tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2370 size_t cnt, loff_t *ppos)
2371{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002372 char buf[max_tracer_type_len+1];
2373 int i;
2374 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002375 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002376
Steven Rostedt60063a62008-10-28 10:44:24 -04002377 ret = cnt;
2378
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002379 if (cnt > max_tracer_type_len)
2380 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002381
2382 if (copy_from_user(&buf, ubuf, cnt))
2383 return -EFAULT;
2384
2385 buf[cnt] = 0;
2386
2387 /* strip ending whitespace. */
2388 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2389 buf[i] = 0;
2390
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002391 err = tracing_set_tracer(buf);
2392 if (err)
2393 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002394
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002395 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002396
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002397 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002398}
2399
2400static ssize_t
2401tracing_max_lat_read(struct file *filp, char __user *ubuf,
2402 size_t cnt, loff_t *ppos)
2403{
2404 unsigned long *ptr = filp->private_data;
2405 char buf[64];
2406 int r;
2407
Steven Rostedtcffae432008-05-12 21:21:00 +02002408 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002409 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002410 if (r > sizeof(buf))
2411 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002412 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002413}
2414
2415static ssize_t
2416tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2417 size_t cnt, loff_t *ppos)
2418{
Hannes Eder5e398412009-02-10 19:44:34 +01002419 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002420 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002421 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002422 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002423
Steven Rostedtcffae432008-05-12 21:21:00 +02002424 if (cnt >= sizeof(buf))
2425 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002426
2427 if (copy_from_user(&buf, ubuf, cnt))
2428 return -EFAULT;
2429
2430 buf[cnt] = 0;
2431
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002432 ret = strict_strtoul(buf, 10, &val);
2433 if (ret < 0)
2434 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002435
2436 *ptr = val * 1000;
2437
2438 return cnt;
2439}
2440
Steven Rostedtb3806b42008-05-12 21:20:46 +02002441static int tracing_open_pipe(struct inode *inode, struct file *filp)
2442{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002443 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002444 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002445 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002446
2447 if (tracing_disabled)
2448 return -ENODEV;
2449
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002450 mutex_lock(&trace_types_lock);
2451
2452 /* We only allow one reader per cpu */
2453 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2454 if (!cpumask_empty(tracing_reader_cpumask)) {
2455 ret = -EBUSY;
2456 goto out;
2457 }
2458 cpumask_setall(tracing_reader_cpumask);
2459 } else {
2460 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2461 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2462 else {
2463 ret = -EBUSY;
2464 goto out;
2465 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002466 }
2467
2468 /* create a buffer to store the information to pass to userspace */
2469 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002470 if (!iter) {
2471 ret = -ENOMEM;
2472 goto out;
2473 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002474
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002475 /*
2476 * We make a copy of the current tracer to avoid concurrent
2477 * changes on it while we are reading.
2478 */
2479 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2480 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002481 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002482 goto fail;
2483 }
2484 if (current_trace)
2485 *iter->trace = *current_trace;
2486
2487 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2488 ret = -ENOMEM;
2489 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302490 }
2491
Steven Rostedta3097202008-11-07 22:36:02 -05002492 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302493 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002494
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002495 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002496 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002497 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002498 filp->private_data = iter;
2499
Steven Rostedt107bad82008-05-12 21:21:01 +02002500 if (iter->trace->pipe_open)
2501 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002502
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002503out:
2504 mutex_unlock(&trace_types_lock);
2505 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002506
2507fail:
2508 kfree(iter->trace);
2509 kfree(iter);
2510 mutex_unlock(&trace_types_lock);
2511 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002512}
2513
2514static int tracing_release_pipe(struct inode *inode, struct file *file)
2515{
2516 struct trace_iterator *iter = file->private_data;
2517
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002518 mutex_lock(&trace_types_lock);
2519
2520 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2521 cpumask_clear(tracing_reader_cpumask);
2522 else
2523 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2524
2525 mutex_unlock(&trace_types_lock);
2526
Rusty Russell44623442009-01-01 10:12:23 +10302527 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002528 mutex_destroy(&iter->mutex);
2529 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002530 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002531
2532 return 0;
2533}
2534
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002535static unsigned int
2536tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2537{
2538 struct trace_iterator *iter = filp->private_data;
2539
2540 if (trace_flags & TRACE_ITER_BLOCK) {
2541 /*
2542 * Always select as readable when in blocking mode
2543 */
2544 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002545 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002546 if (!trace_empty(iter))
2547 return POLLIN | POLLRDNORM;
2548 poll_wait(filp, &trace_wait, poll_table);
2549 if (!trace_empty(iter))
2550 return POLLIN | POLLRDNORM;
2551
2552 return 0;
2553 }
2554}
2555
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002556
2557void default_wait_pipe(struct trace_iterator *iter)
2558{
2559 DEFINE_WAIT(wait);
2560
2561 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2562
2563 if (trace_empty(iter))
2564 schedule();
2565
2566 finish_wait(&trace_wait, &wait);
2567}
2568
2569/*
2570 * This is a make-shift waitqueue.
2571 * A tracer might use this callback on some rare cases:
2572 *
2573 * 1) the current tracer might hold the runqueue lock when it wakes up
2574 * a reader, hence a deadlock (sched, function, and function graph tracers)
2575 * 2) the function tracers, trace all functions, we don't want
2576 * the overhead of calling wake_up and friends
2577 * (and tracing them too)
2578 *
2579 * Anyway, this is really very primitive wakeup.
2580 */
2581void poll_wait_pipe(struct trace_iterator *iter)
2582{
2583 set_current_state(TASK_INTERRUPTIBLE);
2584 /* sleep for 100 msecs, and try again. */
2585 schedule_timeout(HZ / 10);
2586}
2587
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002588/* Must be called with trace_types_lock mutex held. */
2589static int tracing_wait_pipe(struct file *filp)
2590{
2591 struct trace_iterator *iter = filp->private_data;
2592
2593 while (trace_empty(iter)) {
2594
2595 if ((filp->f_flags & O_NONBLOCK)) {
2596 return -EAGAIN;
2597 }
2598
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002599 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002600
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002601 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002602
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002603 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002604
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002605 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002606 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002607
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002608 /*
2609 * We block until we read something and tracing is disabled.
2610 * We still block if tracing is disabled, but we have never
2611 * read anything. This allows a user to cat this file, and
2612 * then enable tracing. But after we have read something,
2613 * we give an EOF when tracing is again disabled.
2614 *
2615 * iter->pos will be 0 if we haven't read anything.
2616 */
2617 if (!tracer_enabled && iter->pos)
2618 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002619 }
2620
2621 return 1;
2622}
2623
Steven Rostedtb3806b42008-05-12 21:20:46 +02002624/*
2625 * Consumer reader.
2626 */
2627static ssize_t
2628tracing_read_pipe(struct file *filp, char __user *ubuf,
2629 size_t cnt, loff_t *ppos)
2630{
2631 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002632 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002633 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002634
2635 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002636 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2637 if (sret != -EBUSY)
2638 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002639
Steven Rostedtf9520752009-03-02 14:04:40 -05002640 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002641
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002642 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002643 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002644 if (unlikely(old_tracer != current_trace && current_trace)) {
2645 old_tracer = current_trace;
2646 *iter->trace = *current_trace;
2647 }
2648 mutex_unlock(&trace_types_lock);
2649
2650 /*
2651 * Avoid more than one consumer on a single file descriptor
2652 * This is just a matter of traces coherency, the ring buffer itself
2653 * is protected.
2654 */
2655 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002656 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002657 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2658 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002659 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002660 }
2661
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002662waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002663 sret = tracing_wait_pipe(filp);
2664 if (sret <= 0)
2665 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002666
2667 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002668 if (trace_empty(iter)) {
2669 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002670 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002671 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002672
2673 if (cnt >= PAGE_SIZE)
2674 cnt = PAGE_SIZE - 1;
2675
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002676 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002677 memset(&iter->seq, 0,
2678 sizeof(struct trace_iterator) -
2679 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002680 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002681
Steven Rostedt088b1e422008-05-12 21:20:48 +02002682 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002683 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002684 int len = iter->seq.len;
2685
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002686 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002687 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002688 /* don't print partial lines */
2689 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002690 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002691 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002692 if (ret != TRACE_TYPE_NO_CONSUME)
2693 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002694
2695 if (iter->seq.len >= cnt)
2696 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002697 }
2698
Steven Rostedtb3806b42008-05-12 21:20:46 +02002699 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002700 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2701 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002702 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002703
2704 /*
2705 * If there was nothing to send to user, inspite of consuming trace
2706 * entries, go back to wait for more entries.
2707 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002708 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002709 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002710
Steven Rostedt107bad82008-05-12 21:21:01 +02002711out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002712 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002713
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002714 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002715}
2716
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002717static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2718 struct pipe_buffer *buf)
2719{
2720 __free_page(buf->page);
2721}
2722
2723static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2724 unsigned int idx)
2725{
2726 __free_page(spd->pages[idx]);
2727}
2728
2729static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002730 .can_merge = 0,
2731 .map = generic_pipe_buf_map,
2732 .unmap = generic_pipe_buf_unmap,
2733 .confirm = generic_pipe_buf_confirm,
2734 .release = tracing_pipe_buf_release,
2735 .steal = generic_pipe_buf_steal,
2736 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002737};
2738
Steven Rostedt34cd4992009-02-09 12:06:29 -05002739static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002740tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002741{
2742 size_t count;
2743 int ret;
2744
2745 /* Seq buffer is page-sized, exactly what we need. */
2746 for (;;) {
2747 count = iter->seq.len;
2748 ret = print_trace_line(iter);
2749 count = iter->seq.len - count;
2750 if (rem < count) {
2751 rem = 0;
2752 iter->seq.len -= count;
2753 break;
2754 }
2755 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2756 iter->seq.len -= count;
2757 break;
2758 }
2759
2760 trace_consume(iter);
2761 rem -= count;
2762 if (!find_next_entry_inc(iter)) {
2763 rem = 0;
2764 iter->ent = NULL;
2765 break;
2766 }
2767 }
2768
2769 return rem;
2770}
2771
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002772static ssize_t tracing_splice_read_pipe(struct file *filp,
2773 loff_t *ppos,
2774 struct pipe_inode_info *pipe,
2775 size_t len,
2776 unsigned int flags)
2777{
2778 struct page *pages[PIPE_BUFFERS];
2779 struct partial_page partial[PIPE_BUFFERS];
2780 struct trace_iterator *iter = filp->private_data;
2781 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002782 .pages = pages,
2783 .partial = partial,
2784 .nr_pages = 0, /* This gets updated below. */
2785 .flags = flags,
2786 .ops = &tracing_pipe_buf_ops,
2787 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002788 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002789 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002790 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002791 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002792 unsigned int i;
2793
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002794 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002795 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002796 if (unlikely(old_tracer != current_trace && current_trace)) {
2797 old_tracer = current_trace;
2798 *iter->trace = *current_trace;
2799 }
2800 mutex_unlock(&trace_types_lock);
2801
2802 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002803
2804 if (iter->trace->splice_read) {
2805 ret = iter->trace->splice_read(iter, filp,
2806 ppos, pipe, len, flags);
2807 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002808 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002809 }
2810
2811 ret = tracing_wait_pipe(filp);
2812 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002813 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002814
2815 if (!iter->ent && !find_next_entry_inc(iter)) {
2816 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002817 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002818 }
2819
2820 /* Fill as many pages as possible. */
2821 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2822 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05002823 if (!pages[i])
2824 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002825
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002826 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002827
2828 /* Copy the data into the page, so we can start over. */
2829 ret = trace_seq_to_buffer(&iter->seq,
2830 page_address(pages[i]),
2831 iter->seq.len);
2832 if (ret < 0) {
2833 __free_page(pages[i]);
2834 break;
2835 }
2836 partial[i].offset = 0;
2837 partial[i].len = iter->seq.len;
2838
Steven Rostedtf9520752009-03-02 14:04:40 -05002839 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002840 }
2841
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002842 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002843
2844 spd.nr_pages = i;
2845
2846 return splice_to_pipe(pipe, &spd);
2847
Steven Rostedt34cd4992009-02-09 12:06:29 -05002848out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002849 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002850
2851 return ret;
2852}
2853
Steven Rostedta98a3c32008-05-12 21:20:59 +02002854static ssize_t
2855tracing_entries_read(struct file *filp, char __user *ubuf,
2856 size_t cnt, loff_t *ppos)
2857{
2858 struct trace_array *tr = filp->private_data;
2859 char buf[64];
2860 int r;
2861
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002862 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002863 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2864}
2865
2866static ssize_t
2867tracing_entries_write(struct file *filp, const char __user *ubuf,
2868 size_t cnt, loff_t *ppos)
2869{
2870 unsigned long val;
2871 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002872 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002873
Steven Rostedtcffae432008-05-12 21:21:00 +02002874 if (cnt >= sizeof(buf))
2875 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002876
2877 if (copy_from_user(&buf, ubuf, cnt))
2878 return -EFAULT;
2879
2880 buf[cnt] = 0;
2881
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002882 ret = strict_strtoul(buf, 10, &val);
2883 if (ret < 0)
2884 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002885
2886 /* must have at least 1 entry */
2887 if (!val)
2888 return -EINVAL;
2889
2890 mutex_lock(&trace_types_lock);
2891
Steven Rostedtc76f0692008-11-07 22:36:02 -05002892 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002893
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002894 /* disable all cpu buffers */
2895 for_each_tracing_cpu(cpu) {
2896 if (global_trace.data[cpu])
2897 atomic_inc(&global_trace.data[cpu]->disabled);
2898 if (max_tr.data[cpu])
2899 atomic_inc(&max_tr.data[cpu]->disabled);
2900 }
2901
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002902 /* value is in KB */
2903 val <<= 10;
2904
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002905 if (val != global_trace.entries) {
2906 ret = ring_buffer_resize(global_trace.buffer, val);
2907 if (ret < 0) {
2908 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002909 goto out;
2910 }
2911
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002912 ret = ring_buffer_resize(max_tr.buffer, val);
2913 if (ret < 0) {
2914 int r;
2915 cnt = ret;
2916 r = ring_buffer_resize(global_trace.buffer,
2917 global_trace.entries);
2918 if (r < 0) {
2919 /* AARGH! We are left with different
2920 * size max buffer!!!! */
2921 WARN_ON(1);
2922 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002923 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002924 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002925 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002926
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002927 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002928 }
2929
2930 filp->f_pos += cnt;
2931
Steven Rostedt19384c02008-05-22 00:22:16 -04002932 /* If check pages failed, return ENOMEM */
2933 if (tracing_disabled)
2934 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002935 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002936 for_each_tracing_cpu(cpu) {
2937 if (global_trace.data[cpu])
2938 atomic_dec(&global_trace.data[cpu]->disabled);
2939 if (max_tr.data[cpu])
2940 atomic_dec(&max_tr.data[cpu]->disabled);
2941 }
2942
Steven Rostedtc76f0692008-11-07 22:36:02 -05002943 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002944 max_tr.entries = global_trace.entries;
2945 mutex_unlock(&trace_types_lock);
2946
2947 return cnt;
2948}
2949
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002950static int mark_printk(const char *fmt, ...)
2951{
2952 int ret;
2953 va_list args;
2954 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002955 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002956 va_end(args);
2957 return ret;
2958}
2959
2960static ssize_t
2961tracing_mark_write(struct file *filp, const char __user *ubuf,
2962 size_t cnt, loff_t *fpos)
2963{
2964 char *buf;
2965 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002966
Steven Rostedtc76f0692008-11-07 22:36:02 -05002967 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002968 return -EINVAL;
2969
2970 if (cnt > TRACE_BUF_SIZE)
2971 cnt = TRACE_BUF_SIZE;
2972
2973 buf = kmalloc(cnt + 1, GFP_KERNEL);
2974 if (buf == NULL)
2975 return -ENOMEM;
2976
2977 if (copy_from_user(buf, ubuf, cnt)) {
2978 kfree(buf);
2979 return -EFAULT;
2980 }
2981
2982 /* Cut from the first nil or newline. */
2983 buf[cnt] = '\0';
2984 end = strchr(buf, '\n');
2985 if (end)
2986 *end = '\0';
2987
2988 cnt = mark_printk("%s\n", buf);
2989 kfree(buf);
2990 *fpos += cnt;
2991
2992 return cnt;
2993}
2994
Steven Rostedt5e2336a2009-03-05 21:44:55 -05002995static const struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002996 .open = tracing_open_generic,
2997 .read = tracing_max_lat_read,
2998 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002999};
3000
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003001static const struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003002 .open = tracing_open_generic,
3003 .read = tracing_ctrl_read,
3004 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003005};
3006
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003007static const struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003008 .open = tracing_open_generic,
3009 .read = tracing_set_trace_read,
3010 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003011};
3012
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003013static const struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003014 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02003015 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003016 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02003017 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003018 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02003019};
3020
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003021static const struct file_operations tracing_entries_fops = {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003022 .open = tracing_open_generic,
3023 .read = tracing_entries_read,
3024 .write = tracing_entries_write,
3025};
3026
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003027static const struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003028 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003029 .write = tracing_mark_write,
3030};
3031
Steven Rostedt2cadf912008-12-01 22:20:19 -05003032struct ftrace_buffer_info {
3033 struct trace_array *tr;
3034 void *spare;
3035 int cpu;
3036 unsigned int read;
3037};
3038
3039static int tracing_buffers_open(struct inode *inode, struct file *filp)
3040{
3041 int cpu = (int)(long)inode->i_private;
3042 struct ftrace_buffer_info *info;
3043
3044 if (tracing_disabled)
3045 return -ENODEV;
3046
3047 info = kzalloc(sizeof(*info), GFP_KERNEL);
3048 if (!info)
3049 return -ENOMEM;
3050
3051 info->tr = &global_trace;
3052 info->cpu = cpu;
3053 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3054 /* Force reading ring buffer for first read */
3055 info->read = (unsigned int)-1;
3056 if (!info->spare)
3057 goto out;
3058
3059 filp->private_data = info;
3060
3061 return 0;
3062
3063 out:
3064 kfree(info);
3065 return -ENOMEM;
3066}
3067
3068static ssize_t
3069tracing_buffers_read(struct file *filp, char __user *ubuf,
3070 size_t count, loff_t *ppos)
3071{
3072 struct ftrace_buffer_info *info = filp->private_data;
3073 unsigned int pos;
3074 ssize_t ret;
3075 size_t size;
3076
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003077 if (!count)
3078 return 0;
3079
Steven Rostedt2cadf912008-12-01 22:20:19 -05003080 /* Do we have previous read data to read? */
3081 if (info->read < PAGE_SIZE)
3082 goto read;
3083
3084 info->read = 0;
3085
3086 ret = ring_buffer_read_page(info->tr->buffer,
3087 &info->spare,
3088 count,
3089 info->cpu, 0);
3090 if (ret < 0)
3091 return 0;
3092
3093 pos = ring_buffer_page_len(info->spare);
3094
3095 if (pos < PAGE_SIZE)
3096 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3097
3098read:
3099 size = PAGE_SIZE - info->read;
3100 if (size > count)
3101 size = count;
3102
3103 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003104 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003105 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003106 size -= ret;
3107
Steven Rostedt2cadf912008-12-01 22:20:19 -05003108 *ppos += size;
3109 info->read += size;
3110
3111 return size;
3112}
3113
3114static int tracing_buffers_release(struct inode *inode, struct file *file)
3115{
3116 struct ftrace_buffer_info *info = file->private_data;
3117
3118 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3119 kfree(info);
3120
3121 return 0;
3122}
3123
3124struct buffer_ref {
3125 struct ring_buffer *buffer;
3126 void *page;
3127 int ref;
3128};
3129
3130static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3131 struct pipe_buffer *buf)
3132{
3133 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3134
3135 if (--ref->ref)
3136 return;
3137
3138 ring_buffer_free_read_page(ref->buffer, ref->page);
3139 kfree(ref);
3140 buf->private = 0;
3141}
3142
3143static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3144 struct pipe_buffer *buf)
3145{
3146 return 1;
3147}
3148
3149static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3150 struct pipe_buffer *buf)
3151{
3152 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3153
3154 ref->ref++;
3155}
3156
3157/* Pipe buffer operations for a buffer. */
3158static struct pipe_buf_operations buffer_pipe_buf_ops = {
3159 .can_merge = 0,
3160 .map = generic_pipe_buf_map,
3161 .unmap = generic_pipe_buf_unmap,
3162 .confirm = generic_pipe_buf_confirm,
3163 .release = buffer_pipe_buf_release,
3164 .steal = buffer_pipe_buf_steal,
3165 .get = buffer_pipe_buf_get,
3166};
3167
3168/*
3169 * Callback from splice_to_pipe(), if we need to release some pages
3170 * at the end of the spd in case we error'ed out in filling the pipe.
3171 */
3172static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3173{
3174 struct buffer_ref *ref =
3175 (struct buffer_ref *)spd->partial[i].private;
3176
3177 if (--ref->ref)
3178 return;
3179
3180 ring_buffer_free_read_page(ref->buffer, ref->page);
3181 kfree(ref);
3182 spd->partial[i].private = 0;
3183}
3184
3185static ssize_t
3186tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3187 struct pipe_inode_info *pipe, size_t len,
3188 unsigned int flags)
3189{
3190 struct ftrace_buffer_info *info = file->private_data;
3191 struct partial_page partial[PIPE_BUFFERS];
3192 struct page *pages[PIPE_BUFFERS];
3193 struct splice_pipe_desc spd = {
3194 .pages = pages,
3195 .partial = partial,
3196 .flags = flags,
3197 .ops = &buffer_pipe_buf_ops,
3198 .spd_release = buffer_spd_release,
3199 };
3200 struct buffer_ref *ref;
3201 int size, i;
3202 size_t ret;
3203
3204 /*
3205 * We can't seek on a buffer input
3206 */
3207 if (unlikely(*ppos))
3208 return -ESPIPE;
3209
3210
3211 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3212 struct page *page;
3213 int r;
3214
3215 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3216 if (!ref)
3217 break;
3218
3219 ref->buffer = info->tr->buffer;
3220 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3221 if (!ref->page) {
3222 kfree(ref);
3223 break;
3224 }
3225
3226 r = ring_buffer_read_page(ref->buffer, &ref->page,
3227 len, info->cpu, 0);
3228 if (r < 0) {
3229 ring_buffer_free_read_page(ref->buffer,
3230 ref->page);
3231 kfree(ref);
3232 break;
3233 }
3234
3235 /*
3236 * zero out any left over data, this is going to
3237 * user land.
3238 */
3239 size = ring_buffer_page_len(ref->page);
3240 if (size < PAGE_SIZE)
3241 memset(ref->page + size, 0, PAGE_SIZE - size);
3242
3243 page = virt_to_page(ref->page);
3244
3245 spd.pages[i] = page;
3246 spd.partial[i].len = PAGE_SIZE;
3247 spd.partial[i].offset = 0;
3248 spd.partial[i].private = (unsigned long)ref;
3249 spd.nr_pages++;
3250 }
3251
3252 spd.nr_pages = i;
3253
3254 /* did we read anything? */
3255 if (!spd.nr_pages) {
3256 if (flags & SPLICE_F_NONBLOCK)
3257 ret = -EAGAIN;
3258 else
3259 ret = 0;
3260 /* TODO: block */
3261 return ret;
3262 }
3263
3264 ret = splice_to_pipe(pipe, &spd);
3265
3266 return ret;
3267}
3268
3269static const struct file_operations tracing_buffers_fops = {
3270 .open = tracing_buffers_open,
3271 .read = tracing_buffers_read,
3272 .release = tracing_buffers_release,
3273 .splice_read = tracing_buffers_splice_read,
3274 .llseek = no_llseek,
3275};
3276
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003277#ifdef CONFIG_DYNAMIC_FTRACE
3278
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003279int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003280{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003281 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003282}
3283
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003284static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003285tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003286 size_t cnt, loff_t *ppos)
3287{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003288 static char ftrace_dyn_info_buffer[1024];
3289 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003290 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003291 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003292 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003293 int r;
3294
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003295 mutex_lock(&dyn_info_mutex);
3296 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003297
Steven Rostedta26a2a22008-10-31 00:03:22 -04003298 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003299 buf[r++] = '\n';
3300
3301 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3302
3303 mutex_unlock(&dyn_info_mutex);
3304
3305 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003306}
3307
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003308static const struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003309 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003310 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003311};
3312#endif
3313
3314static struct dentry *d_tracer;
3315
3316struct dentry *tracing_init_dentry(void)
3317{
3318 static int once;
3319
3320 if (d_tracer)
3321 return d_tracer;
3322
3323 d_tracer = debugfs_create_dir("tracing", NULL);
3324
3325 if (!d_tracer && !once) {
3326 once = 1;
3327 pr_warning("Could not create debugfs directory 'tracing'\n");
3328 return NULL;
3329 }
3330
3331 return d_tracer;
3332}
3333
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003334static struct dentry *d_percpu;
3335
3336struct dentry *tracing_dentry_percpu(void)
3337{
3338 static int once;
3339 struct dentry *d_tracer;
3340
3341 if (d_percpu)
3342 return d_percpu;
3343
3344 d_tracer = tracing_init_dentry();
3345
3346 if (!d_tracer)
3347 return NULL;
3348
3349 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3350
3351 if (!d_percpu && !once) {
3352 once = 1;
3353 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3354 return NULL;
3355 }
3356
3357 return d_percpu;
3358}
3359
3360static void tracing_init_debugfs_percpu(long cpu)
3361{
3362 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003363 struct dentry *entry, *d_cpu;
3364 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3365 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003366
3367 if (cpu > 999 || cpu < 0)
3368 return;
3369
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003370 sprintf(cpu_dir, "cpu%ld", cpu);
3371 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3372 if (!d_cpu) {
3373 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3374 return;
3375 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003376
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003377 /* per cpu trace_pipe */
3378 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003379 (void *) cpu, &tracing_pipe_fops);
3380 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003381 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003382
3383 /* per cpu trace */
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003384 entry = debugfs_create_file("trace", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003385 (void *) cpu, &tracing_fops);
3386 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003387 pr_warning("Could not create debugfs 'trace' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003388}
3389
Steven Rostedt60a11772008-05-12 21:20:44 +02003390#ifdef CONFIG_FTRACE_SELFTEST
3391/* Let selftest have access to static functions in this file */
3392#include "trace_selftest.c"
3393#endif
3394
Steven Rostedt577b7852009-02-26 23:43:05 -05003395struct trace_option_dentry {
3396 struct tracer_opt *opt;
3397 struct tracer_flags *flags;
3398 struct dentry *entry;
3399};
3400
3401static ssize_t
3402trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3403 loff_t *ppos)
3404{
3405 struct trace_option_dentry *topt = filp->private_data;
3406 char *buf;
3407
3408 if (topt->flags->val & topt->opt->bit)
3409 buf = "1\n";
3410 else
3411 buf = "0\n";
3412
3413 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3414}
3415
3416static ssize_t
3417trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3418 loff_t *ppos)
3419{
3420 struct trace_option_dentry *topt = filp->private_data;
3421 unsigned long val;
3422 char buf[64];
3423 int ret;
3424
3425 if (cnt >= sizeof(buf))
3426 return -EINVAL;
3427
3428 if (copy_from_user(&buf, ubuf, cnt))
3429 return -EFAULT;
3430
3431 buf[cnt] = 0;
3432
3433 ret = strict_strtoul(buf, 10, &val);
3434 if (ret < 0)
3435 return ret;
3436
3437 ret = 0;
3438 switch (val) {
3439 case 0:
3440 /* do nothing if already cleared */
3441 if (!(topt->flags->val & topt->opt->bit))
3442 break;
3443
3444 mutex_lock(&trace_types_lock);
3445 if (current_trace->set_flag)
3446 ret = current_trace->set_flag(topt->flags->val,
3447 topt->opt->bit, 0);
3448 mutex_unlock(&trace_types_lock);
3449 if (ret)
3450 return ret;
3451 topt->flags->val &= ~topt->opt->bit;
3452 break;
3453 case 1:
3454 /* do nothing if already set */
3455 if (topt->flags->val & topt->opt->bit)
3456 break;
3457
3458 mutex_lock(&trace_types_lock);
3459 if (current_trace->set_flag)
3460 ret = current_trace->set_flag(topt->flags->val,
3461 topt->opt->bit, 1);
3462 mutex_unlock(&trace_types_lock);
3463 if (ret)
3464 return ret;
3465 topt->flags->val |= topt->opt->bit;
3466 break;
3467
3468 default:
3469 return -EINVAL;
3470 }
3471
3472 *ppos += cnt;
3473
3474 return cnt;
3475}
3476
3477
3478static const struct file_operations trace_options_fops = {
3479 .open = tracing_open_generic,
3480 .read = trace_options_read,
3481 .write = trace_options_write,
3482};
3483
Steven Rostedta8259072009-02-26 22:19:12 -05003484static ssize_t
3485trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3486 loff_t *ppos)
3487{
3488 long index = (long)filp->private_data;
3489 char *buf;
3490
3491 if (trace_flags & (1 << index))
3492 buf = "1\n";
3493 else
3494 buf = "0\n";
3495
3496 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3497}
3498
3499static ssize_t
3500trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3501 loff_t *ppos)
3502{
3503 long index = (long)filp->private_data;
3504 char buf[64];
3505 unsigned long val;
3506 int ret;
3507
3508 if (cnt >= sizeof(buf))
3509 return -EINVAL;
3510
3511 if (copy_from_user(&buf, ubuf, cnt))
3512 return -EFAULT;
3513
3514 buf[cnt] = 0;
3515
3516 ret = strict_strtoul(buf, 10, &val);
3517 if (ret < 0)
3518 return ret;
3519
3520 switch (val) {
3521 case 0:
3522 trace_flags &= ~(1 << index);
3523 break;
3524 case 1:
3525 trace_flags |= 1 << index;
3526 break;
3527
3528 default:
3529 return -EINVAL;
3530 }
3531
3532 *ppos += cnt;
3533
3534 return cnt;
3535}
3536
Steven Rostedta8259072009-02-26 22:19:12 -05003537static const struct file_operations trace_options_core_fops = {
3538 .open = tracing_open_generic,
3539 .read = trace_options_core_read,
3540 .write = trace_options_core_write,
3541};
3542
3543static struct dentry *trace_options_init_dentry(void)
3544{
3545 struct dentry *d_tracer;
3546 static struct dentry *t_options;
3547
3548 if (t_options)
3549 return t_options;
3550
3551 d_tracer = tracing_init_dentry();
3552 if (!d_tracer)
3553 return NULL;
3554
3555 t_options = debugfs_create_dir("options", d_tracer);
3556 if (!t_options) {
3557 pr_warning("Could not create debugfs directory 'options'\n");
3558 return NULL;
3559 }
3560
3561 return t_options;
3562}
3563
Steven Rostedt577b7852009-02-26 23:43:05 -05003564static void
3565create_trace_option_file(struct trace_option_dentry *topt,
3566 struct tracer_flags *flags,
3567 struct tracer_opt *opt)
3568{
3569 struct dentry *t_options;
3570 struct dentry *entry;
3571
3572 t_options = trace_options_init_dentry();
3573 if (!t_options)
3574 return;
3575
3576 topt->flags = flags;
3577 topt->opt = opt;
3578
3579 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3580 &trace_options_fops);
3581
3582 topt->entry = entry;
3583
3584}
3585
3586static struct trace_option_dentry *
3587create_trace_option_files(struct tracer *tracer)
3588{
3589 struct trace_option_dentry *topts;
3590 struct tracer_flags *flags;
3591 struct tracer_opt *opts;
3592 int cnt;
3593
3594 if (!tracer)
3595 return NULL;
3596
3597 flags = tracer->flags;
3598
3599 if (!flags || !flags->opts)
3600 return NULL;
3601
3602 opts = flags->opts;
3603
3604 for (cnt = 0; opts[cnt].name; cnt++)
3605 ;
3606
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003607 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003608 if (!topts)
3609 return NULL;
3610
3611 for (cnt = 0; opts[cnt].name; cnt++)
3612 create_trace_option_file(&topts[cnt], flags,
3613 &opts[cnt]);
3614
3615 return topts;
3616}
3617
3618static void
3619destroy_trace_option_files(struct trace_option_dentry *topts)
3620{
3621 int cnt;
3622
3623 if (!topts)
3624 return;
3625
3626 for (cnt = 0; topts[cnt].opt; cnt++) {
3627 if (topts[cnt].entry)
3628 debugfs_remove(topts[cnt].entry);
3629 }
3630
3631 kfree(topts);
3632}
3633
Steven Rostedta8259072009-02-26 22:19:12 -05003634static struct dentry *
3635create_trace_option_core_file(const char *option, long index)
3636{
3637 struct dentry *t_options;
3638 struct dentry *entry;
3639
3640 t_options = trace_options_init_dentry();
3641 if (!t_options)
3642 return NULL;
3643
3644 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3645 &trace_options_core_fops);
3646
3647 return entry;
3648}
3649
3650static __init void create_trace_options_dir(void)
3651{
3652 struct dentry *t_options;
3653 struct dentry *entry;
3654 int i;
3655
3656 t_options = trace_options_init_dentry();
3657 if (!t_options)
3658 return;
3659
3660 for (i = 0; trace_options[i]; i++) {
3661 entry = create_trace_option_core_file(trace_options[i], i);
3662 if (!entry)
3663 pr_warning("Could not create debugfs %s entry\n",
3664 trace_options[i]);
3665 }
3666}
3667
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003668static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003669{
3670 struct dentry *d_tracer;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003671 struct dentry *buffers;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003672 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003673 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003674
3675 d_tracer = tracing_init_dentry();
3676
3677 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3678 &global_trace, &tracing_ctrl_fops);
3679 if (!entry)
3680 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3681
Steven Rostedtee6bce52008-11-12 17:52:37 -05003682 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003683 NULL, &tracing_iter_fops);
3684 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003685 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003686
Steven Rostedta8259072009-02-26 22:19:12 -05003687 create_trace_options_dir();
3688
Ingo Molnarc7078de2008-05-12 21:20:52 +02003689 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3690 NULL, &tracing_cpumask_fops);
3691 if (!entry)
3692 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3693
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003694 entry = debugfs_create_file("trace", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003695 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003696 if (!entry)
3697 pr_warning("Could not create debugfs 'trace' entry\n");
3698
3699 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3700 &global_trace, &show_traces_fops);
3701 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003702 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003703
3704 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3705 &global_trace, &set_tracer_fops);
3706 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003707 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003708
3709 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3710 &tracing_max_latency,
3711 &tracing_max_lat_fops);
3712 if (!entry)
3713 pr_warning("Could not create debugfs "
3714 "'tracing_max_latency' entry\n");
3715
3716 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3717 &tracing_thresh, &tracing_max_lat_fops);
3718 if (!entry)
3719 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003720 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003721 entry = debugfs_create_file("README", 0644, d_tracer,
3722 NULL, &tracing_readme_fops);
3723 if (!entry)
3724 pr_warning("Could not create debugfs 'README' entry\n");
3725
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003726 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3727 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003728 if (!entry)
3729 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003730 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003731
Steven Rostedta94c80e2008-11-12 17:52:36 -05003732 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003733 &global_trace, &tracing_entries_fops);
3734 if (!entry)
3735 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003736 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003737
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003738 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3739 NULL, &tracing_mark_fops);
3740 if (!entry)
3741 pr_warning("Could not create debugfs "
3742 "'trace_marker' entry\n");
3743
Steven Rostedt2cadf912008-12-01 22:20:19 -05003744 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3745
3746 if (!buffers)
3747 pr_warning("Could not create buffers directory\n");
3748 else {
3749 int cpu;
3750 char buf[64];
3751
3752 for_each_tracing_cpu(cpu) {
3753 sprintf(buf, "%d", cpu);
3754
3755 entry = debugfs_create_file(buf, 0444, buffers,
3756 (void *)(long)cpu,
3757 &tracing_buffers_fops);
3758 if (!entry)
3759 pr_warning("Could not create debugfs buffers "
3760 "'%s' entry\n", buf);
3761 }
3762 }
3763
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003764#ifdef CONFIG_DYNAMIC_FTRACE
3765 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3766 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003767 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003768 if (!entry)
3769 pr_warning("Could not create debugfs "
3770 "'dyn_ftrace_total_info' entry\n");
3771#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003772#ifdef CONFIG_SYSPROF_TRACER
3773 init_tracer_sysprof_debugfs(d_tracer);
3774#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003775
3776 for_each_tracing_cpu(cpu)
3777 tracing_init_debugfs_percpu(cpu);
3778
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003779 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003780}
3781
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003782static int trace_panic_handler(struct notifier_block *this,
3783 unsigned long event, void *unused)
3784{
Steven Rostedt944ac422008-10-23 19:26:08 -04003785 if (ftrace_dump_on_oops)
3786 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003787 return NOTIFY_OK;
3788}
3789
3790static struct notifier_block trace_panic_notifier = {
3791 .notifier_call = trace_panic_handler,
3792 .next = NULL,
3793 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3794};
3795
3796static int trace_die_handler(struct notifier_block *self,
3797 unsigned long val,
3798 void *data)
3799{
3800 switch (val) {
3801 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003802 if (ftrace_dump_on_oops)
3803 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003804 break;
3805 default:
3806 break;
3807 }
3808 return NOTIFY_OK;
3809}
3810
3811static struct notifier_block trace_die_notifier = {
3812 .notifier_call = trace_die_handler,
3813 .priority = 200
3814};
3815
3816/*
3817 * printk is set to max of 1024, we really don't need it that big.
3818 * Nothing should be printing 1000 characters anyway.
3819 */
3820#define TRACE_MAX_PRINT 1000
3821
3822/*
3823 * Define here KERN_TRACE so that we have one place to modify
3824 * it if we decide to change what log level the ftrace dump
3825 * should be at.
3826 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003827#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003828
3829static void
3830trace_printk_seq(struct trace_seq *s)
3831{
3832 /* Probably should print a warning here. */
3833 if (s->len >= 1000)
3834 s->len = 1000;
3835
3836 /* should be zero ended, but we are paranoid. */
3837 s->buffer[s->len] = 0;
3838
3839 printk(KERN_TRACE "%s", s->buffer);
3840
Steven Rostedtf9520752009-03-02 14:04:40 -05003841 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003842}
3843
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003844void ftrace_dump(void)
3845{
3846 static DEFINE_SPINLOCK(ftrace_dump_lock);
3847 /* use static because iter can be a bit big for the stack */
3848 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003849 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003850 unsigned long flags;
3851 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003852
3853 /* only one dump */
3854 spin_lock_irqsave(&ftrace_dump_lock, flags);
3855 if (dump_ran)
3856 goto out;
3857
3858 dump_ran = 1;
3859
3860 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05003861 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003862 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003863
Steven Rostedtd7690412008-10-01 00:29:53 -04003864 for_each_tracing_cpu(cpu) {
3865 atomic_inc(&global_trace.data[cpu]->disabled);
3866 }
3867
Török Edwinb54d3de2008-11-22 13:28:48 +02003868 /* don't look at user memory in panic mode */
3869 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3870
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003871 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3872
Steven Rostedte543ad72009-03-04 18:20:36 -05003873 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003874 iter.tr = &global_trace;
3875 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05003876 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003877
3878 /*
3879 * We need to stop all tracing on all CPUS to read the
3880 * the next buffer. This is a bit expensive, but is
3881 * not done often. We fill all what we can read,
3882 * and then release the locks again.
3883 */
3884
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003885 while (!trace_empty(&iter)) {
3886
3887 if (!cnt)
3888 printk(KERN_TRACE "---------------------------------\n");
3889
3890 cnt++;
3891
3892 /* reset all but tr, trace, and overruns */
3893 memset(&iter.seq, 0,
3894 sizeof(struct trace_iterator) -
3895 offsetof(struct trace_iterator, seq));
3896 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3897 iter.pos = -1;
3898
3899 if (find_next_entry_inc(&iter) != NULL) {
3900 print_trace_line(&iter);
3901 trace_consume(&iter);
3902 }
3903
3904 trace_printk_seq(&iter.seq);
3905 }
3906
3907 if (!cnt)
3908 printk(KERN_TRACE " (ftrace buffer empty)\n");
3909 else
3910 printk(KERN_TRACE "---------------------------------\n");
3911
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003912 out:
3913 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3914}
3915
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003916__init static int tracer_alloc_buffers(void)
3917{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003918 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003919 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303920 int ret = -ENOMEM;
3921
3922 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3923 goto out;
3924
3925 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3926 goto out_free_buffer_mask;
3927
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003928 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3929 goto out_free_tracing_cpumask;
3930
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303931 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3932 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003933 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003934
Steven Rostedtab464282008-05-12 21:21:00 +02003935 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003936 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3937 TRACE_BUFFER_FLAGS);
3938 if (!global_trace.buffer) {
3939 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3940 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303941 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003942 }
3943 global_trace.entries = ring_buffer_size(global_trace.buffer);
3944
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303945
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003946#ifdef CONFIG_TRACER_MAX_TRACE
3947 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3948 TRACE_BUFFER_FLAGS);
3949 if (!max_tr.buffer) {
3950 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3951 WARN_ON(1);
3952 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303953 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003954 }
3955 max_tr.entries = ring_buffer_size(max_tr.buffer);
3956 WARN_ON(max_tr.entries != global_trace.entries);
3957#endif
3958
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003959 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003960 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003961 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003962 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003963 }
3964
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003965 trace_init_cmdlines();
3966
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003967 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05003968 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003969#ifdef CONFIG_BOOT_TRACER
3970 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003971#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02003972 /* All seems OK, enable tracing */
3973 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003974
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003975 atomic_notifier_chain_register(&panic_notifier_list,
3976 &trace_panic_notifier);
3977
3978 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303979 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003980
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303981out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003982 free_cpumask_var(tracing_reader_cpumask);
3983out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303984 free_cpumask_var(tracing_cpumask);
3985out_free_buffer_mask:
3986 free_cpumask_var(tracing_buffer_mask);
3987out:
3988 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003989}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05003990
3991__init static int clear_boot_tracer(void)
3992{
3993 /*
3994 * The default tracer at boot buffer is an init section.
3995 * This function is called in lateinit. If we did not
3996 * find the boot tracer, then clear it out, to prevent
3997 * later registration from accessing the buffer that is
3998 * about to be freed.
3999 */
4000 if (!default_bootup_tracer)
4001 return 0;
4002
4003 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4004 default_bootup_tracer);
4005 default_bootup_tracer = NULL;
4006
4007 return 0;
4008}
4009
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004010early_initcall(tracer_alloc_buffers);
4011fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004012late_initcall(clear_boot_tracer);