blob: 55fcbb56795085b0dd02c1d57578eaa629bcd4e6 [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
51 * insertions into the ring-buffer such as ftrace_printk could occurred
52 * 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",
Peter Zijlstraf09ce572008-09-04 10:24:14 +0200294 "ftrace_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;
Török Edwinb54d3de2008-11-22 13:28:48 +0200802 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 Molnara4feb8342008-05-12 21:21:02 +02001017{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001018 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001019 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001020 struct special_entry *entry;
Ingo Molnara4feb8342008-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 Molnara4feb8342008-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
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172enum trace_file_type {
1173 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001174 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001175};
1176
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001177static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001178{
Steven Rostedtd7690412008-10-01 00:29:53 -04001179 /* Don't allow ftrace to trace into the ring buffers */
1180 ftrace_disable_cpu();
1181
Steven Rostedt5a90f572008-09-03 17:42:51 -04001182 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001183 if (iter->buffer_iter[iter->cpu])
1184 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1185
1186 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001187}
1188
Ingo Molnare309b412008-05-12 21:20:51 +02001189static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001190peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001191{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001192 struct ring_buffer_event *event;
1193 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001194
Steven Rostedtd7690412008-10-01 00:29:53 -04001195 /* Don't allow ftrace to trace into the ring buffers */
1196 ftrace_disable_cpu();
1197
1198 if (buf_iter)
1199 event = ring_buffer_iter_peek(buf_iter, ts);
1200 else
1201 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1202
1203 ftrace_enable_cpu();
1204
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001205 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001206}
Steven Rostedtd7690412008-10-01 00:29:53 -04001207
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001208static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001209__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001210{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001211 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001212 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001213 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001214 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001215 int next_cpu = -1;
1216 int cpu;
1217
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001218 /*
1219 * If we are in a per_cpu trace file, don't bother by iterating over
1220 * all cpu and peek directly.
1221 */
1222 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1223 if (ring_buffer_empty_cpu(buffer, cpu_file))
1224 return NULL;
1225 ent = peek_next_entry(iter, cpu_file, ent_ts);
1226 if (ent_cpu)
1227 *ent_cpu = cpu_file;
1228
1229 return ent;
1230 }
1231
Steven Rostedtab464282008-05-12 21:21:00 +02001232 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001233
1234 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001235 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001236
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001237 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001238
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001239 /*
1240 * Pick the entry with the smallest timestamp:
1241 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001242 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001243 next = ent;
1244 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001245 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001246 }
1247 }
1248
1249 if (ent_cpu)
1250 *ent_cpu = next_cpu;
1251
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001252 if (ent_ts)
1253 *ent_ts = next_ts;
1254
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001255 return next;
1256}
1257
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001258/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001259struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1260 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001261{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001262 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001263}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001264
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001265/* Find the next real entry, and increment the iterator to the next entry */
1266static void *find_next_entry_inc(struct trace_iterator *iter)
1267{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001268 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001269
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001270 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001271 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001272
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001273 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001274}
1275
Ingo Molnare309b412008-05-12 21:20:51 +02001276static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001277{
Steven Rostedtd7690412008-10-01 00:29:53 -04001278 /* Don't allow ftrace to trace into the ring buffers */
1279 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001280 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001281 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001282}
1283
Ingo Molnare309b412008-05-12 21:20:51 +02001284static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001285{
1286 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001287 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001288 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001289
1290 (*pos)++;
1291
1292 /* can't go backwards */
1293 if (iter->idx > i)
1294 return NULL;
1295
1296 if (iter->idx < 0)
1297 ent = find_next_entry_inc(iter);
1298 else
1299 ent = iter;
1300
1301 while (ent && iter->idx < i)
1302 ent = find_next_entry_inc(iter);
1303
1304 iter->pos = *pos;
1305
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001306 return ent;
1307}
1308
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001309/*
1310 * No necessary locking here. The worst thing which can
1311 * happen is loosing events consumed at the same time
1312 * by a trace_pipe reader.
1313 * Other than that, we don't risk to crash the ring buffer
1314 * because it serializes the readers.
1315 *
1316 * The current tracer is copied to avoid a global locking
1317 * all around.
1318 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001319static void *s_start(struct seq_file *m, loff_t *pos)
1320{
1321 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001322 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001323 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001324 void *p = NULL;
1325 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001326 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001327
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001328 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001329 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001330 if (unlikely(old_tracer != current_trace && current_trace)) {
1331 old_tracer = current_trace;
1332 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001333 }
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001334 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001335
1336 atomic_inc(&trace_record_cmdline_disabled);
1337
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001338 if (*pos != iter->pos) {
1339 iter->ent = NULL;
1340 iter->cpu = 0;
1341 iter->idx = -1;
1342
Steven Rostedtd7690412008-10-01 00:29:53 -04001343 ftrace_disable_cpu();
1344
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001345 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1346 for_each_tracing_cpu(cpu)
1347 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1348 } else
1349 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1350
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001351
Steven Rostedtd7690412008-10-01 00:29:53 -04001352 ftrace_enable_cpu();
1353
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001354 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1355 ;
1356
1357 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001358 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001359 p = s_next(m, p, &l);
1360 }
1361
1362 return p;
1363}
1364
1365static void s_stop(struct seq_file *m, void *p)
1366{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001367 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001368}
1369
Ingo Molnare309b412008-05-12 21:20:51 +02001370static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001371{
Michael Ellermana6168352008-08-20 16:36:11 -07001372 seq_puts(m, "# _------=> CPU# \n");
1373 seq_puts(m, "# / _-----=> irqs-off \n");
1374 seq_puts(m, "# | / _----=> need-resched \n");
1375 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1376 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1377 seq_puts(m, "# |||| / \n");
1378 seq_puts(m, "# ||||| delay \n");
1379 seq_puts(m, "# cmd pid ||||| time | caller \n");
1380 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001381}
1382
Ingo Molnare309b412008-05-12 21:20:51 +02001383static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384{
Michael Ellermana6168352008-08-20 16:36:11 -07001385 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1386 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001387}
1388
1389
Ingo Molnare309b412008-05-12 21:20:51 +02001390static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001391print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1392{
1393 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1394 struct trace_array *tr = iter->tr;
1395 struct trace_array_cpu *data = tr->data[tr->cpu];
1396 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001397 unsigned long total;
1398 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001399 const char *name = "preemption";
1400
1401 if (type)
1402 name = type->name;
1403
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001404 entries = ring_buffer_entries(iter->tr->buffer);
1405 total = entries +
1406 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001407
1408 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1409 name, UTS_RELEASE);
1410 seq_puts(m, "-----------------------------------"
1411 "---------------------------------\n");
1412 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1413 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001414 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001415 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001416 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001417 tr->cpu,
1418#if defined(CONFIG_PREEMPT_NONE)
1419 "server",
1420#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1421 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001422#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001423 "preempt",
1424#else
1425 "unknown",
1426#endif
1427 /* These are reserved for later use */
1428 0, 0, 0, 0);
1429#ifdef CONFIG_SMP
1430 seq_printf(m, " #P:%d)\n", num_online_cpus());
1431#else
1432 seq_puts(m, ")\n");
1433#endif
1434 seq_puts(m, " -----------------\n");
1435 seq_printf(m, " | task: %.16s-%d "
1436 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1437 data->comm, data->pid, data->uid, data->nice,
1438 data->policy, data->rt_priority);
1439 seq_puts(m, " -----------------\n");
1440
1441 if (data->critical_start) {
1442 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001443 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1444 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001445 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001446 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1447 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001448 seq_puts(m, "\n");
1449 }
1450
1451 seq_puts(m, "\n");
1452}
1453
Steven Rostedta3097202008-11-07 22:36:02 -05001454static void test_cpu_buff_start(struct trace_iterator *iter)
1455{
1456 struct trace_seq *s = &iter->seq;
1457
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001458 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1459 return;
1460
1461 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1462 return;
1463
Rusty Russell44623442009-01-01 10:12:23 +10301464 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001465 return;
1466
Rusty Russell44623442009-01-01 10:12:23 +10301467 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001468 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1469}
1470
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001471static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001472{
Steven Rostedt214023c2008-05-12 21:20:46 +02001473 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001474 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedtf633cef2008-12-23 23:24:13 -05001475 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001476 struct trace_entry *entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001477
Steven Rostedta3097202008-11-07 22:36:02 -05001478 test_cpu_buff_start(iter);
1479
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001480 event = ftrace_find_event(entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001481
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001482 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001483 if (!trace_print_lat_context(iter))
1484 goto partial;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001485 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001486
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001487 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001488 return event->latency_trace(iter, sym_flags);
Steven Rostedt7104f302008-10-01 10:52:51 -04001489
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001490 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1491 goto partial;
1492
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001493 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001494partial:
1495 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001496}
1497
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001498static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001499{
Steven Rostedt214023c2008-05-12 21:20:46 +02001500 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001501 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001502 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001503 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001504
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001505 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001506
Steven Rostedta3097202008-11-07 22:36:02 -05001507 test_cpu_buff_start(iter);
1508
Steven Rostedtf633cef2008-12-23 23:24:13 -05001509 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001510
1511 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001512 if (!trace_print_context(iter))
1513 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001514 }
1515
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001516 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001517 return event->trace(iter, sym_flags);
1518
1519 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1520 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001521
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001522 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001523partial:
1524 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001525}
1526
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001527static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001528{
1529 struct trace_seq *s = &iter->seq;
1530 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001531 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001532
1533 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001534
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001535 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001536 if (!trace_seq_printf(s, "%d %d %llu ",
1537 entry->pid, iter->cpu, iter->ts))
1538 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001539 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001540
Steven Rostedtf633cef2008-12-23 23:24:13 -05001541 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001542 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001543 return event->raw(iter, 0);
1544
1545 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1546 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001547
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001548 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001549partial:
1550 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001551}
1552
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001553static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001554{
1555 struct trace_seq *s = &iter->seq;
1556 unsigned char newline = '\n';
1557 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001558 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001559
1560 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001561
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001562 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1563 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1564 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1565 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1566 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001567
Steven Rostedtf633cef2008-12-23 23:24:13 -05001568 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001569 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001570 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001571 if (ret != TRACE_TYPE_HANDLED)
1572 return ret;
1573 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001574
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001575 SEQ_PUT_FIELD_RET(s, newline);
1576
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001577 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001578}
1579
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001580static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1581{
1582 struct trace_seq *s = &iter->seq;
1583 struct trace_entry *entry = iter->ent;
1584 struct print_entry *field;
1585 int ret;
1586
1587 trace_assign_type(field, entry);
1588
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001589 ret = trace_seq_printf(s, "%s", field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001590 if (!ret)
1591 return TRACE_TYPE_PARTIAL_LINE;
1592
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001593 return TRACE_TYPE_HANDLED;
1594}
1595
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001596static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001597{
1598 struct trace_seq *s = &iter->seq;
1599 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001600 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001601
1602 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001603
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001604 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1605 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001606 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001607 SEQ_PUT_FIELD_RET(s, iter->ts);
1608 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001609
Steven Rostedtf633cef2008-12-23 23:24:13 -05001610 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001611 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001612}
1613
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001614static int trace_empty(struct trace_iterator *iter)
1615{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001616 int cpu;
1617
Steven Rostedtab464282008-05-12 21:21:00 +02001618 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001619 if (iter->buffer_iter[cpu]) {
1620 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1621 return 0;
1622 } else {
1623 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1624 return 0;
1625 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001626 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001627
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001628 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001629}
1630
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001631static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001632{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001633 enum print_line_t ret;
1634
1635 if (iter->trace && iter->trace->print_line) {
1636 ret = iter->trace->print_line(iter);
1637 if (ret != TRACE_TYPE_UNHANDLED)
1638 return ret;
1639 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001640
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001641 if (iter->ent->type == TRACE_PRINT &&
1642 trace_flags & TRACE_ITER_PRINTK &&
1643 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1644 return print_printk_msg_only(iter);
1645
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001646 if (trace_flags & TRACE_ITER_BIN)
1647 return print_bin_fmt(iter);
1648
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001649 if (trace_flags & TRACE_ITER_HEX)
1650 return print_hex_fmt(iter);
1651
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001652 if (trace_flags & TRACE_ITER_RAW)
1653 return print_raw_fmt(iter);
1654
1655 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001656 return print_lat_fmt(iter);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001657
1658 return print_trace_fmt(iter);
1659}
1660
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001661static int s_show(struct seq_file *m, void *v)
1662{
1663 struct trace_iterator *iter = v;
1664
1665 if (iter->ent == NULL) {
1666 if (iter->tr) {
1667 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1668 seq_puts(m, "#\n");
1669 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001670 if (iter->trace && iter->trace->print_header)
1671 iter->trace->print_header(m);
1672 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001673 /* print nothing if the buffers are empty */
1674 if (trace_empty(iter))
1675 return 0;
1676 print_trace_header(m, iter);
1677 if (!(trace_flags & TRACE_ITER_VERBOSE))
1678 print_lat_help_header(m);
1679 } else {
1680 if (!(trace_flags & TRACE_ITER_VERBOSE))
1681 print_func_help_header(m);
1682 }
1683 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001684 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001685 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001686 }
1687
1688 return 0;
1689}
1690
1691static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001692 .start = s_start,
1693 .next = s_next,
1694 .stop = s_stop,
1695 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001696};
1697
Ingo Molnare309b412008-05-12 21:20:51 +02001698static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001699__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001700{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001701 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001702 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001703 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001704 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001705 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001706
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001707 if (tracing_disabled)
1708 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001709
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001710 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001711 if (!iter)
1712 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001713
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001714 /*
1715 * We make a copy of the current tracer to avoid concurrent
1716 * changes on it while we are reading.
1717 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001718 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001719 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001720 if (!iter->trace)
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001721 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001722
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001723 if (current_trace)
1724 *iter->trace = *current_trace;
1725
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001726 if (current_trace && current_trace->print_max)
1727 iter->tr = &max_tr;
1728 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001729 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001730 iter->pos = -1;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001731 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001732 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001733
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001734 /* Notify the tracer early; before we stop tracing. */
1735 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001736 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001737
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001738 /* Annotate start of buffers if we had overruns */
1739 if (ring_buffer_overruns(iter->tr->buffer))
1740 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1741
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001742 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1743 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001744
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001745 iter->buffer_iter[cpu] =
1746 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001747
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001748 if (!iter->buffer_iter[cpu])
1749 goto fail_buffer;
1750 }
1751 } else {
1752 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001753 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001754 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001755
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001756 if (!iter->buffer_iter[cpu])
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001757 goto fail;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001758 }
1759
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001760 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001761 ret = seq_open(file, &tracer_seq_ops);
1762 if (ret < 0) {
1763 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001764 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001765 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001766
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001767 m = file->private_data;
1768 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001769
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001770 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001771 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001772
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001773 mutex_unlock(&trace_types_lock);
1774
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001775 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001776
1777 fail_buffer:
1778 for_each_tracing_cpu(cpu) {
1779 if (iter->buffer_iter[cpu])
1780 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1781 }
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001782 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001783 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001784 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001785 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001786
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001787 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001788}
1789
1790int tracing_open_generic(struct inode *inode, struct file *filp)
1791{
Steven Rostedt60a11772008-05-12 21:20:44 +02001792 if (tracing_disabled)
1793 return -ENODEV;
1794
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001795 filp->private_data = inode->i_private;
1796 return 0;
1797}
1798
Hannes Eder4fd27352009-02-10 19:44:12 +01001799static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001800{
1801 struct seq_file *m = (struct seq_file *)file->private_data;
1802 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001803 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001804
1805 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001806 for_each_tracing_cpu(cpu) {
1807 if (iter->buffer_iter[cpu])
1808 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1809 }
1810
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001811 if (iter->trace && iter->trace->close)
1812 iter->trace->close(iter);
1813
1814 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001815 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001816 mutex_unlock(&trace_types_lock);
1817
1818 seq_release(inode, file);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01001819 mutex_destroy(&iter->mutex);
1820 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001821 kfree(iter);
1822 return 0;
1823}
1824
1825static int tracing_open(struct inode *inode, struct file *file)
1826{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001827 struct trace_iterator *iter;
1828 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001829
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001830 iter = __tracing_open(inode, file);
1831 if (IS_ERR(iter))
1832 ret = PTR_ERR(iter);
Steven Rostedtc032ef642009-03-04 20:34:24 -05001833 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001834 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1835
1836 return ret;
1837}
1838
Ingo Molnare309b412008-05-12 21:20:51 +02001839static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001840t_next(struct seq_file *m, void *v, loff_t *pos)
1841{
1842 struct tracer *t = m->private;
1843
1844 (*pos)++;
1845
1846 if (t)
1847 t = t->next;
1848
1849 m->private = t;
1850
1851 return t;
1852}
1853
1854static void *t_start(struct seq_file *m, loff_t *pos)
1855{
1856 struct tracer *t = m->private;
1857 loff_t l = 0;
1858
1859 mutex_lock(&trace_types_lock);
1860 for (; t && l < *pos; t = t_next(m, t, &l))
1861 ;
1862
1863 return t;
1864}
1865
1866static void t_stop(struct seq_file *m, void *p)
1867{
1868 mutex_unlock(&trace_types_lock);
1869}
1870
1871static int t_show(struct seq_file *m, void *v)
1872{
1873 struct tracer *t = v;
1874
1875 if (!t)
1876 return 0;
1877
1878 seq_printf(m, "%s", t->name);
1879 if (t->next)
1880 seq_putc(m, ' ');
1881 else
1882 seq_putc(m, '\n');
1883
1884 return 0;
1885}
1886
1887static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001888 .start = t_start,
1889 .next = t_next,
1890 .stop = t_stop,
1891 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001892};
1893
1894static int show_traces_open(struct inode *inode, struct file *file)
1895{
1896 int ret;
1897
Steven Rostedt60a11772008-05-12 21:20:44 +02001898 if (tracing_disabled)
1899 return -ENODEV;
1900
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001901 ret = seq_open(file, &show_traces_seq_ops);
1902 if (!ret) {
1903 struct seq_file *m = file->private_data;
1904 m->private = trace_types;
1905 }
1906
1907 return ret;
1908}
1909
1910static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001911 .open = tracing_open,
1912 .read = seq_read,
1913 .llseek = seq_lseek,
1914 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001915};
1916
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001917static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001918 .open = show_traces_open,
1919 .read = seq_read,
1920 .release = seq_release,
1921};
1922
Ingo Molnar36dfe922008-05-12 21:20:52 +02001923/*
1924 * Only trace on a CPU if the bitmask is set:
1925 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301926static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001927
1928/*
1929 * The tracer itself will not take this lock, but still we want
1930 * to provide a consistent cpumask to user-space:
1931 */
1932static DEFINE_MUTEX(tracing_cpumask_update_lock);
1933
1934/*
1935 * Temporary storage for the character representation of the
1936 * CPU bitmask (and one more byte for the newline):
1937 */
1938static char mask_str[NR_CPUS + 1];
1939
Ingo Molnarc7078de2008-05-12 21:20:52 +02001940static ssize_t
1941tracing_cpumask_read(struct file *filp, char __user *ubuf,
1942 size_t count, loff_t *ppos)
1943{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001944 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001945
1946 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001947
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301948 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001949 if (count - len < 2) {
1950 count = -EINVAL;
1951 goto out_err;
1952 }
1953 len += sprintf(mask_str + len, "\n");
1954 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1955
1956out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001957 mutex_unlock(&tracing_cpumask_update_lock);
1958
1959 return count;
1960}
1961
1962static ssize_t
1963tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1964 size_t count, loff_t *ppos)
1965{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001966 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301967 cpumask_var_t tracing_cpumask_new;
1968
1969 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1970 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001971
1972 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301973 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001974 if (err)
1975 goto err_unlock;
1976
Steven Rostedta5e25882008-12-02 15:34:05 -05001977 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02001978 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001979 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001980 /*
1981 * Increase/decrease the disabled counter if we are
1982 * about to flip a bit in the cpumask:
1983 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301984 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
1985 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001986 atomic_inc(&global_trace.data[cpu]->disabled);
1987 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301988 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
1989 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001990 atomic_dec(&global_trace.data[cpu]->disabled);
1991 }
1992 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001993 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05001994 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001995
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301996 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001997
Ingo Molnarc7078de2008-05-12 21:20:52 +02001998 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301999 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002000
Ingo Molnarc7078de2008-05-12 21:20:52 +02002001 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002002
2003err_unlock:
2004 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302005 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002006
2007 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002008}
2009
2010static struct file_operations tracing_cpumask_fops = {
2011 .open = tracing_open_generic,
2012 .read = tracing_cpumask_read,
2013 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002014};
2015
2016static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002017tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002018 size_t cnt, loff_t *ppos)
2019{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002020 struct tracer_opt *trace_opts;
2021 u32 tracer_flags;
2022 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002023 char *buf;
2024 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002025 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002026
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002027
Wenji Huangc3706f02009-02-10 01:03:18 -05002028 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002029 for (i = 0; trace_options[i]; i++) {
2030 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002031 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002032 }
2033
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002034 mutex_lock(&trace_types_lock);
2035 tracer_flags = current_trace->flags->val;
2036 trace_opts = current_trace->flags->opts;
2037
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002038 /*
2039 * Increase the size with names of options specific
2040 * of the current tracer.
2041 */
2042 for (i = 0; trace_opts[i].name; i++) {
2043 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002044 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002045 }
2046
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002047 /* +2 for \n and \0 */
2048 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002049 if (!buf) {
2050 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002051 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002052 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002053
2054 for (i = 0; trace_options[i]; i++) {
2055 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002056 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002057 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002058 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002059 }
2060
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002061 for (i = 0; trace_opts[i].name; i++) {
2062 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002063 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002064 trace_opts[i].name);
2065 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002066 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002067 trace_opts[i].name);
2068 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002069 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002070
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002071 WARN_ON(r >= len + 2);
2072
Ingo Molnar36dfe922008-05-12 21:20:52 +02002073 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002074
2075 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002076 return r;
2077}
2078
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002079/* Try to assign a tracer specific option */
2080static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2081{
2082 struct tracer_flags *trace_flags = trace->flags;
2083 struct tracer_opt *opts = NULL;
2084 int ret = 0, i = 0;
2085 int len;
2086
2087 for (i = 0; trace_flags->opts[i].name; i++) {
2088 opts = &trace_flags->opts[i];
2089 len = strlen(opts->name);
2090
2091 if (strncmp(cmp, opts->name, len) == 0) {
2092 ret = trace->set_flag(trace_flags->val,
2093 opts->bit, !neg);
2094 break;
2095 }
2096 }
2097 /* Not found */
2098 if (!trace_flags->opts[i].name)
2099 return -EINVAL;
2100
2101 /* Refused to handle */
2102 if (ret)
2103 return ret;
2104
2105 if (neg)
2106 trace_flags->val &= ~opts->bit;
2107 else
2108 trace_flags->val |= opts->bit;
2109
2110 return 0;
2111}
2112
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002113static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002114tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002115 size_t cnt, loff_t *ppos)
2116{
2117 char buf[64];
2118 char *cmp = buf;
2119 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002120 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002121 int i;
2122
Steven Rostedtcffae432008-05-12 21:21:00 +02002123 if (cnt >= sizeof(buf))
2124 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002125
2126 if (copy_from_user(&buf, ubuf, cnt))
2127 return -EFAULT;
2128
2129 buf[cnt] = 0;
2130
2131 if (strncmp(buf, "no", 2) == 0) {
2132 neg = 1;
2133 cmp += 2;
2134 }
2135
2136 for (i = 0; trace_options[i]; i++) {
2137 int len = strlen(trace_options[i]);
2138
2139 if (strncmp(cmp, trace_options[i], len) == 0) {
2140 if (neg)
2141 trace_flags &= ~(1 << i);
2142 else
2143 trace_flags |= (1 << i);
2144 break;
2145 }
2146 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002147
2148 /* If no option could be set, test the specific tracer options */
2149 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002150 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002151 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002152 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002153 if (ret)
2154 return ret;
2155 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002156
2157 filp->f_pos += cnt;
2158
2159 return cnt;
2160}
2161
2162static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002163 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002164 .read = tracing_trace_options_read,
2165 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002166};
2167
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002168static const char readme_msg[] =
2169 "tracing mini-HOWTO:\n\n"
2170 "# mkdir /debug\n"
2171 "# mount -t debugfs nodev /debug\n\n"
2172 "# cat /debug/tracing/available_tracers\n"
2173 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2174 "# cat /debug/tracing/current_tracer\n"
2175 "none\n"
2176 "# echo sched_switch > /debug/tracing/current_tracer\n"
2177 "# cat /debug/tracing/current_tracer\n"
2178 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002179 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002180 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002181 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002182 "# echo 1 > /debug/tracing/tracing_enabled\n"
2183 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2184 "echo 0 > /debug/tracing/tracing_enabled\n"
2185;
2186
2187static ssize_t
2188tracing_readme_read(struct file *filp, char __user *ubuf,
2189 size_t cnt, loff_t *ppos)
2190{
2191 return simple_read_from_buffer(ubuf, cnt, ppos,
2192 readme_msg, strlen(readme_msg));
2193}
2194
2195static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002196 .open = tracing_open_generic,
2197 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002198};
2199
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002200static ssize_t
2201tracing_ctrl_read(struct file *filp, char __user *ubuf,
2202 size_t cnt, loff_t *ppos)
2203{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002204 char buf[64];
2205 int r;
2206
Steven Rostedt90369902008-11-05 16:05:44 -05002207 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002208 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002209}
2210
2211static ssize_t
2212tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2213 size_t cnt, loff_t *ppos)
2214{
2215 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002216 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002217 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002218 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002219
Steven Rostedtcffae432008-05-12 21:21:00 +02002220 if (cnt >= sizeof(buf))
2221 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002222
2223 if (copy_from_user(&buf, ubuf, cnt))
2224 return -EFAULT;
2225
2226 buf[cnt] = 0;
2227
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002228 ret = strict_strtoul(buf, 10, &val);
2229 if (ret < 0)
2230 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002231
2232 val = !!val;
2233
2234 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002235 if (tracer_enabled ^ val) {
2236 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002237 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002238 if (current_trace->start)
2239 current_trace->start(tr);
2240 tracing_start();
2241 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002242 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002243 tracing_stop();
2244 if (current_trace->stop)
2245 current_trace->stop(tr);
2246 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002247 }
2248 mutex_unlock(&trace_types_lock);
2249
2250 filp->f_pos += cnt;
2251
2252 return cnt;
2253}
2254
2255static ssize_t
2256tracing_set_trace_read(struct file *filp, char __user *ubuf,
2257 size_t cnt, loff_t *ppos)
2258{
2259 char buf[max_tracer_type_len+2];
2260 int r;
2261
2262 mutex_lock(&trace_types_lock);
2263 if (current_trace)
2264 r = sprintf(buf, "%s\n", current_trace->name);
2265 else
2266 r = sprintf(buf, "\n");
2267 mutex_unlock(&trace_types_lock);
2268
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002269 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002270}
2271
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002272int tracer_init(struct tracer *t, struct trace_array *tr)
2273{
2274 tracing_reset_online_cpus(tr);
2275 return t->init(tr);
2276}
2277
Steven Rostedt577b7852009-02-26 23:43:05 -05002278struct trace_option_dentry;
2279
2280static struct trace_option_dentry *
2281create_trace_option_files(struct tracer *tracer);
2282
2283static void
2284destroy_trace_option_files(struct trace_option_dentry *topts);
2285
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002286static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002287{
Steven Rostedt577b7852009-02-26 23:43:05 -05002288 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002289 struct trace_array *tr = &global_trace;
2290 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002291 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002292
2293 mutex_lock(&trace_types_lock);
2294 for (t = trace_types; t; t = t->next) {
2295 if (strcmp(t->name, buf) == 0)
2296 break;
2297 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002298 if (!t) {
2299 ret = -EINVAL;
2300 goto out;
2301 }
2302 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002303 goto out;
2304
Steven Rostedt9f029e82008-11-12 15:24:24 -05002305 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002306 if (current_trace && current_trace->reset)
2307 current_trace->reset(tr);
2308
Steven Rostedt577b7852009-02-26 23:43:05 -05002309 destroy_trace_option_files(topts);
2310
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002311 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002312
2313 topts = create_trace_option_files(current_trace);
2314
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002315 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002316 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002317 if (ret)
2318 goto out;
2319 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002320
Steven Rostedt9f029e82008-11-12 15:24:24 -05002321 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002322 out:
2323 mutex_unlock(&trace_types_lock);
2324
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002325 return ret;
2326}
2327
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002328static ssize_t
2329tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2330 size_t cnt, loff_t *ppos)
2331{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002332 char buf[max_tracer_type_len+1];
2333 int i;
2334 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002335 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002336
Steven Rostedt60063a62008-10-28 10:44:24 -04002337 ret = cnt;
2338
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002339 if (cnt > max_tracer_type_len)
2340 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002341
2342 if (copy_from_user(&buf, ubuf, cnt))
2343 return -EFAULT;
2344
2345 buf[cnt] = 0;
2346
2347 /* strip ending whitespace. */
2348 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2349 buf[i] = 0;
2350
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002351 err = tracing_set_tracer(buf);
2352 if (err)
2353 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002354
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002355 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002357 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002358}
2359
2360static ssize_t
2361tracing_max_lat_read(struct file *filp, char __user *ubuf,
2362 size_t cnt, loff_t *ppos)
2363{
2364 unsigned long *ptr = filp->private_data;
2365 char buf[64];
2366 int r;
2367
Steven Rostedtcffae432008-05-12 21:21:00 +02002368 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002369 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002370 if (r > sizeof(buf))
2371 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002372 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002373}
2374
2375static ssize_t
2376tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2378{
Hannes Eder5e398412009-02-10 19:44:34 +01002379 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002380 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002381 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002382 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002383
Steven Rostedtcffae432008-05-12 21:21:00 +02002384 if (cnt >= sizeof(buf))
2385 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002386
2387 if (copy_from_user(&buf, ubuf, cnt))
2388 return -EFAULT;
2389
2390 buf[cnt] = 0;
2391
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002392 ret = strict_strtoul(buf, 10, &val);
2393 if (ret < 0)
2394 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002395
2396 *ptr = val * 1000;
2397
2398 return cnt;
2399}
2400
Steven Rostedtb3806b42008-05-12 21:20:46 +02002401static int tracing_open_pipe(struct inode *inode, struct file *filp)
2402{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002403 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002404 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002405 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002406
2407 if (tracing_disabled)
2408 return -ENODEV;
2409
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002410 mutex_lock(&trace_types_lock);
2411
2412 /* We only allow one reader per cpu */
2413 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2414 if (!cpumask_empty(tracing_reader_cpumask)) {
2415 ret = -EBUSY;
2416 goto out;
2417 }
2418 cpumask_setall(tracing_reader_cpumask);
2419 } else {
2420 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2421 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2422 else {
2423 ret = -EBUSY;
2424 goto out;
2425 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002426 }
2427
2428 /* create a buffer to store the information to pass to userspace */
2429 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002430 if (!iter) {
2431 ret = -ENOMEM;
2432 goto out;
2433 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002434
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002435 /*
2436 * We make a copy of the current tracer to avoid concurrent
2437 * changes on it while we are reading.
2438 */
2439 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2440 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002441 ret = -ENOMEM;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002442 goto fail;
2443 }
2444 if (current_trace)
2445 *iter->trace = *current_trace;
2446
2447 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2448 ret = -ENOMEM;
2449 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302450 }
2451
Steven Rostedta3097202008-11-07 22:36:02 -05002452 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302453 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002454
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002455 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002456 iter->tr = &global_trace;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002457 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002458 filp->private_data = iter;
2459
Steven Rostedt107bad82008-05-12 21:21:01 +02002460 if (iter->trace->pipe_open)
2461 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002462
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002463out:
2464 mutex_unlock(&trace_types_lock);
2465 return ret;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002466
2467fail:
2468 kfree(iter->trace);
2469 kfree(iter);
2470 mutex_unlock(&trace_types_lock);
2471 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002472}
2473
2474static int tracing_release_pipe(struct inode *inode, struct file *file)
2475{
2476 struct trace_iterator *iter = file->private_data;
2477
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002478 mutex_lock(&trace_types_lock);
2479
2480 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2481 cpumask_clear(tracing_reader_cpumask);
2482 else
2483 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2484
2485 mutex_unlock(&trace_types_lock);
2486
Rusty Russell44623442009-01-01 10:12:23 +10302487 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002488 mutex_destroy(&iter->mutex);
2489 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002490 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002491
2492 return 0;
2493}
2494
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002495static unsigned int
2496tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2497{
2498 struct trace_iterator *iter = filp->private_data;
2499
2500 if (trace_flags & TRACE_ITER_BLOCK) {
2501 /*
2502 * Always select as readable when in blocking mode
2503 */
2504 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002505 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002506 if (!trace_empty(iter))
2507 return POLLIN | POLLRDNORM;
2508 poll_wait(filp, &trace_wait, poll_table);
2509 if (!trace_empty(iter))
2510 return POLLIN | POLLRDNORM;
2511
2512 return 0;
2513 }
2514}
2515
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002516
2517void default_wait_pipe(struct trace_iterator *iter)
2518{
2519 DEFINE_WAIT(wait);
2520
2521 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2522
2523 if (trace_empty(iter))
2524 schedule();
2525
2526 finish_wait(&trace_wait, &wait);
2527}
2528
2529/*
2530 * This is a make-shift waitqueue.
2531 * A tracer might use this callback on some rare cases:
2532 *
2533 * 1) the current tracer might hold the runqueue lock when it wakes up
2534 * a reader, hence a deadlock (sched, function, and function graph tracers)
2535 * 2) the function tracers, trace all functions, we don't want
2536 * the overhead of calling wake_up and friends
2537 * (and tracing them too)
2538 *
2539 * Anyway, this is really very primitive wakeup.
2540 */
2541void poll_wait_pipe(struct trace_iterator *iter)
2542{
2543 set_current_state(TASK_INTERRUPTIBLE);
2544 /* sleep for 100 msecs, and try again. */
2545 schedule_timeout(HZ / 10);
2546}
2547
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002548/* Must be called with trace_types_lock mutex held. */
2549static int tracing_wait_pipe(struct file *filp)
2550{
2551 struct trace_iterator *iter = filp->private_data;
2552
2553 while (trace_empty(iter)) {
2554
2555 if ((filp->f_flags & O_NONBLOCK)) {
2556 return -EAGAIN;
2557 }
2558
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002559 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002560
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002561 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002562
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002563 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002564
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002565 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002566 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002567
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002568 /*
2569 * We block until we read something and tracing is disabled.
2570 * We still block if tracing is disabled, but we have never
2571 * read anything. This allows a user to cat this file, and
2572 * then enable tracing. But after we have read something,
2573 * we give an EOF when tracing is again disabled.
2574 *
2575 * iter->pos will be 0 if we haven't read anything.
2576 */
2577 if (!tracer_enabled && iter->pos)
2578 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002579 }
2580
2581 return 1;
2582}
2583
Steven Rostedtb3806b42008-05-12 21:20:46 +02002584/*
2585 * Consumer reader.
2586 */
2587static ssize_t
2588tracing_read_pipe(struct file *filp, char __user *ubuf,
2589 size_t cnt, loff_t *ppos)
2590{
2591 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002592 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002593 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002594
2595 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002596 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2597 if (sret != -EBUSY)
2598 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002599
Steven Rostedtf9520752009-03-02 14:04:40 -05002600 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002601
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002602 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002603 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002604 if (unlikely(old_tracer != current_trace && current_trace)) {
2605 old_tracer = current_trace;
2606 *iter->trace = *current_trace;
2607 }
2608 mutex_unlock(&trace_types_lock);
2609
2610 /*
2611 * Avoid more than one consumer on a single file descriptor
2612 * This is just a matter of traces coherency, the ring buffer itself
2613 * is protected.
2614 */
2615 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002616 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002617 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2618 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002619 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002620 }
2621
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002622waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002623 sret = tracing_wait_pipe(filp);
2624 if (sret <= 0)
2625 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002626
2627 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002628 if (trace_empty(iter)) {
2629 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002630 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002631 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002632
2633 if (cnt >= PAGE_SIZE)
2634 cnt = PAGE_SIZE - 1;
2635
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002636 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002637 memset(&iter->seq, 0,
2638 sizeof(struct trace_iterator) -
2639 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002640 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002641
Steven Rostedt088b1e422008-05-12 21:20:48 +02002642 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002643 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002644 int len = iter->seq.len;
2645
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002646 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002647 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002648 /* don't print partial lines */
2649 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002650 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002651 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002652 if (ret != TRACE_TYPE_NO_CONSUME)
2653 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002654
2655 if (iter->seq.len >= cnt)
2656 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002657 }
2658
Steven Rostedtb3806b42008-05-12 21:20:46 +02002659 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002660 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2661 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002662 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002663
2664 /*
2665 * If there was nothing to send to user, inspite of consuming trace
2666 * entries, go back to wait for more entries.
2667 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002668 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002669 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002670
Steven Rostedt107bad82008-05-12 21:21:01 +02002671out:
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002672 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002673
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002674 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002675}
2676
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002677static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2678 struct pipe_buffer *buf)
2679{
2680 __free_page(buf->page);
2681}
2682
2683static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2684 unsigned int idx)
2685{
2686 __free_page(spd->pages[idx]);
2687}
2688
2689static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002690 .can_merge = 0,
2691 .map = generic_pipe_buf_map,
2692 .unmap = generic_pipe_buf_unmap,
2693 .confirm = generic_pipe_buf_confirm,
2694 .release = tracing_pipe_buf_release,
2695 .steal = generic_pipe_buf_steal,
2696 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002697};
2698
Steven Rostedt34cd4992009-02-09 12:06:29 -05002699static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002700tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002701{
2702 size_t count;
2703 int ret;
2704
2705 /* Seq buffer is page-sized, exactly what we need. */
2706 for (;;) {
2707 count = iter->seq.len;
2708 ret = print_trace_line(iter);
2709 count = iter->seq.len - count;
2710 if (rem < count) {
2711 rem = 0;
2712 iter->seq.len -= count;
2713 break;
2714 }
2715 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2716 iter->seq.len -= count;
2717 break;
2718 }
2719
2720 trace_consume(iter);
2721 rem -= count;
2722 if (!find_next_entry_inc(iter)) {
2723 rem = 0;
2724 iter->ent = NULL;
2725 break;
2726 }
2727 }
2728
2729 return rem;
2730}
2731
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002732static ssize_t tracing_splice_read_pipe(struct file *filp,
2733 loff_t *ppos,
2734 struct pipe_inode_info *pipe,
2735 size_t len,
2736 unsigned int flags)
2737{
2738 struct page *pages[PIPE_BUFFERS];
2739 struct partial_page partial[PIPE_BUFFERS];
2740 struct trace_iterator *iter = filp->private_data;
2741 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002742 .pages = pages,
2743 .partial = partial,
2744 .nr_pages = 0, /* This gets updated below. */
2745 .flags = flags,
2746 .ops = &tracing_pipe_buf_ops,
2747 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002748 };
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002749 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002750 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002751 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002752 unsigned int i;
2753
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002754 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002755 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002756 if (unlikely(old_tracer != current_trace && current_trace)) {
2757 old_tracer = current_trace;
2758 *iter->trace = *current_trace;
2759 }
2760 mutex_unlock(&trace_types_lock);
2761
2762 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002763
2764 if (iter->trace->splice_read) {
2765 ret = iter->trace->splice_read(iter, filp,
2766 ppos, pipe, len, flags);
2767 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002768 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002769 }
2770
2771 ret = tracing_wait_pipe(filp);
2772 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002773 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002774
2775 if (!iter->ent && !find_next_entry_inc(iter)) {
2776 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002777 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002778 }
2779
2780 /* Fill as many pages as possible. */
2781 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2782 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05002783 if (!pages[i])
2784 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002785
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002786 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002787
2788 /* Copy the data into the page, so we can start over. */
2789 ret = trace_seq_to_buffer(&iter->seq,
2790 page_address(pages[i]),
2791 iter->seq.len);
2792 if (ret < 0) {
2793 __free_page(pages[i]);
2794 break;
2795 }
2796 partial[i].offset = 0;
2797 partial[i].len = iter->seq.len;
2798
Steven Rostedtf9520752009-03-02 14:04:40 -05002799 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002800 }
2801
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002802 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002803
2804 spd.nr_pages = i;
2805
2806 return splice_to_pipe(pipe, &spd);
2807
Steven Rostedt34cd4992009-02-09 12:06:29 -05002808out_err:
Frederic Weisbeckerd7350c32009-02-25 06:13:16 +01002809 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002810
2811 return ret;
2812}
2813
Steven Rostedta98a3c32008-05-12 21:20:59 +02002814static ssize_t
2815tracing_entries_read(struct file *filp, char __user *ubuf,
2816 size_t cnt, loff_t *ppos)
2817{
2818 struct trace_array *tr = filp->private_data;
2819 char buf[64];
2820 int r;
2821
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002822 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002823 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2824}
2825
2826static ssize_t
2827tracing_entries_write(struct file *filp, const char __user *ubuf,
2828 size_t cnt, loff_t *ppos)
2829{
2830 unsigned long val;
2831 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002832 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002833
Steven Rostedtcffae432008-05-12 21:21:00 +02002834 if (cnt >= sizeof(buf))
2835 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002836
2837 if (copy_from_user(&buf, ubuf, cnt))
2838 return -EFAULT;
2839
2840 buf[cnt] = 0;
2841
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002842 ret = strict_strtoul(buf, 10, &val);
2843 if (ret < 0)
2844 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002845
2846 /* must have at least 1 entry */
2847 if (!val)
2848 return -EINVAL;
2849
2850 mutex_lock(&trace_types_lock);
2851
Steven Rostedtc76f0692008-11-07 22:36:02 -05002852 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002853
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002854 /* disable all cpu buffers */
2855 for_each_tracing_cpu(cpu) {
2856 if (global_trace.data[cpu])
2857 atomic_inc(&global_trace.data[cpu]->disabled);
2858 if (max_tr.data[cpu])
2859 atomic_inc(&max_tr.data[cpu]->disabled);
2860 }
2861
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002862 /* value is in KB */
2863 val <<= 10;
2864
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002865 if (val != global_trace.entries) {
2866 ret = ring_buffer_resize(global_trace.buffer, val);
2867 if (ret < 0) {
2868 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002869 goto out;
2870 }
2871
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002872 ret = ring_buffer_resize(max_tr.buffer, val);
2873 if (ret < 0) {
2874 int r;
2875 cnt = ret;
2876 r = ring_buffer_resize(global_trace.buffer,
2877 global_trace.entries);
2878 if (r < 0) {
2879 /* AARGH! We are left with different
2880 * size max buffer!!!! */
2881 WARN_ON(1);
2882 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002883 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002884 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002885 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002886
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002887 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002888 }
2889
2890 filp->f_pos += cnt;
2891
Steven Rostedt19384c02008-05-22 00:22:16 -04002892 /* If check pages failed, return ENOMEM */
2893 if (tracing_disabled)
2894 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002895 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002896 for_each_tracing_cpu(cpu) {
2897 if (global_trace.data[cpu])
2898 atomic_dec(&global_trace.data[cpu]->disabled);
2899 if (max_tr.data[cpu])
2900 atomic_dec(&max_tr.data[cpu]->disabled);
2901 }
2902
Steven Rostedtc76f0692008-11-07 22:36:02 -05002903 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002904 max_tr.entries = global_trace.entries;
2905 mutex_unlock(&trace_types_lock);
2906
2907 return cnt;
2908}
2909
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002910static int mark_printk(const char *fmt, ...)
2911{
2912 int ret;
2913 va_list args;
2914 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002915 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002916 va_end(args);
2917 return ret;
2918}
2919
2920static ssize_t
2921tracing_mark_write(struct file *filp, const char __user *ubuf,
2922 size_t cnt, loff_t *fpos)
2923{
2924 char *buf;
2925 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002926
Steven Rostedtc76f0692008-11-07 22:36:02 -05002927 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002928 return -EINVAL;
2929
2930 if (cnt > TRACE_BUF_SIZE)
2931 cnt = TRACE_BUF_SIZE;
2932
2933 buf = kmalloc(cnt + 1, GFP_KERNEL);
2934 if (buf == NULL)
2935 return -ENOMEM;
2936
2937 if (copy_from_user(buf, ubuf, cnt)) {
2938 kfree(buf);
2939 return -EFAULT;
2940 }
2941
2942 /* Cut from the first nil or newline. */
2943 buf[cnt] = '\0';
2944 end = strchr(buf, '\n');
2945 if (end)
2946 *end = '\0';
2947
2948 cnt = mark_printk("%s\n", buf);
2949 kfree(buf);
2950 *fpos += cnt;
2951
2952 return cnt;
2953}
2954
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002955static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002956 .open = tracing_open_generic,
2957 .read = tracing_max_lat_read,
2958 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002959};
2960
2961static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002962 .open = tracing_open_generic,
2963 .read = tracing_ctrl_read,
2964 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002965};
2966
2967static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002968 .open = tracing_open_generic,
2969 .read = tracing_set_trace_read,
2970 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002971};
2972
Steven Rostedtb3806b42008-05-12 21:20:46 +02002973static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002974 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002975 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002976 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002977 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002978 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002979};
2980
Steven Rostedta98a3c32008-05-12 21:20:59 +02002981static struct file_operations tracing_entries_fops = {
2982 .open = tracing_open_generic,
2983 .read = tracing_entries_read,
2984 .write = tracing_entries_write,
2985};
2986
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002987static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02002988 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002989 .write = tracing_mark_write,
2990};
2991
Steven Rostedt2cadf912008-12-01 22:20:19 -05002992struct ftrace_buffer_info {
2993 struct trace_array *tr;
2994 void *spare;
2995 int cpu;
2996 unsigned int read;
2997};
2998
2999static int tracing_buffers_open(struct inode *inode, struct file *filp)
3000{
3001 int cpu = (int)(long)inode->i_private;
3002 struct ftrace_buffer_info *info;
3003
3004 if (tracing_disabled)
3005 return -ENODEV;
3006
3007 info = kzalloc(sizeof(*info), GFP_KERNEL);
3008 if (!info)
3009 return -ENOMEM;
3010
3011 info->tr = &global_trace;
3012 info->cpu = cpu;
3013 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3014 /* Force reading ring buffer for first read */
3015 info->read = (unsigned int)-1;
3016 if (!info->spare)
3017 goto out;
3018
3019 filp->private_data = info;
3020
3021 return 0;
3022
3023 out:
3024 kfree(info);
3025 return -ENOMEM;
3026}
3027
3028static ssize_t
3029tracing_buffers_read(struct file *filp, char __user *ubuf,
3030 size_t count, loff_t *ppos)
3031{
3032 struct ftrace_buffer_info *info = filp->private_data;
3033 unsigned int pos;
3034 ssize_t ret;
3035 size_t size;
3036
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003037 if (!count)
3038 return 0;
3039
Steven Rostedt2cadf912008-12-01 22:20:19 -05003040 /* Do we have previous read data to read? */
3041 if (info->read < PAGE_SIZE)
3042 goto read;
3043
3044 info->read = 0;
3045
3046 ret = ring_buffer_read_page(info->tr->buffer,
3047 &info->spare,
3048 count,
3049 info->cpu, 0);
3050 if (ret < 0)
3051 return 0;
3052
3053 pos = ring_buffer_page_len(info->spare);
3054
3055 if (pos < PAGE_SIZE)
3056 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3057
3058read:
3059 size = PAGE_SIZE - info->read;
3060 if (size > count)
3061 size = count;
3062
3063 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003064 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003065 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003066 size -= ret;
3067
Steven Rostedt2cadf912008-12-01 22:20:19 -05003068 *ppos += size;
3069 info->read += size;
3070
3071 return size;
3072}
3073
3074static int tracing_buffers_release(struct inode *inode, struct file *file)
3075{
3076 struct ftrace_buffer_info *info = file->private_data;
3077
3078 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3079 kfree(info);
3080
3081 return 0;
3082}
3083
3084struct buffer_ref {
3085 struct ring_buffer *buffer;
3086 void *page;
3087 int ref;
3088};
3089
3090static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3091 struct pipe_buffer *buf)
3092{
3093 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3094
3095 if (--ref->ref)
3096 return;
3097
3098 ring_buffer_free_read_page(ref->buffer, ref->page);
3099 kfree(ref);
3100 buf->private = 0;
3101}
3102
3103static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3104 struct pipe_buffer *buf)
3105{
3106 return 1;
3107}
3108
3109static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3110 struct pipe_buffer *buf)
3111{
3112 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3113
3114 ref->ref++;
3115}
3116
3117/* Pipe buffer operations for a buffer. */
3118static struct pipe_buf_operations buffer_pipe_buf_ops = {
3119 .can_merge = 0,
3120 .map = generic_pipe_buf_map,
3121 .unmap = generic_pipe_buf_unmap,
3122 .confirm = generic_pipe_buf_confirm,
3123 .release = buffer_pipe_buf_release,
3124 .steal = buffer_pipe_buf_steal,
3125 .get = buffer_pipe_buf_get,
3126};
3127
3128/*
3129 * Callback from splice_to_pipe(), if we need to release some pages
3130 * at the end of the spd in case we error'ed out in filling the pipe.
3131 */
3132static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3133{
3134 struct buffer_ref *ref =
3135 (struct buffer_ref *)spd->partial[i].private;
3136
3137 if (--ref->ref)
3138 return;
3139
3140 ring_buffer_free_read_page(ref->buffer, ref->page);
3141 kfree(ref);
3142 spd->partial[i].private = 0;
3143}
3144
3145static ssize_t
3146tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3147 struct pipe_inode_info *pipe, size_t len,
3148 unsigned int flags)
3149{
3150 struct ftrace_buffer_info *info = file->private_data;
3151 struct partial_page partial[PIPE_BUFFERS];
3152 struct page *pages[PIPE_BUFFERS];
3153 struct splice_pipe_desc spd = {
3154 .pages = pages,
3155 .partial = partial,
3156 .flags = flags,
3157 .ops = &buffer_pipe_buf_ops,
3158 .spd_release = buffer_spd_release,
3159 };
3160 struct buffer_ref *ref;
3161 int size, i;
3162 size_t ret;
3163
3164 /*
3165 * We can't seek on a buffer input
3166 */
3167 if (unlikely(*ppos))
3168 return -ESPIPE;
3169
3170
3171 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3172 struct page *page;
3173 int r;
3174
3175 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3176 if (!ref)
3177 break;
3178
3179 ref->buffer = info->tr->buffer;
3180 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3181 if (!ref->page) {
3182 kfree(ref);
3183 break;
3184 }
3185
3186 r = ring_buffer_read_page(ref->buffer, &ref->page,
3187 len, info->cpu, 0);
3188 if (r < 0) {
3189 ring_buffer_free_read_page(ref->buffer,
3190 ref->page);
3191 kfree(ref);
3192 break;
3193 }
3194
3195 /*
3196 * zero out any left over data, this is going to
3197 * user land.
3198 */
3199 size = ring_buffer_page_len(ref->page);
3200 if (size < PAGE_SIZE)
3201 memset(ref->page + size, 0, PAGE_SIZE - size);
3202
3203 page = virt_to_page(ref->page);
3204
3205 spd.pages[i] = page;
3206 spd.partial[i].len = PAGE_SIZE;
3207 spd.partial[i].offset = 0;
3208 spd.partial[i].private = (unsigned long)ref;
3209 spd.nr_pages++;
3210 }
3211
3212 spd.nr_pages = i;
3213
3214 /* did we read anything? */
3215 if (!spd.nr_pages) {
3216 if (flags & SPLICE_F_NONBLOCK)
3217 ret = -EAGAIN;
3218 else
3219 ret = 0;
3220 /* TODO: block */
3221 return ret;
3222 }
3223
3224 ret = splice_to_pipe(pipe, &spd);
3225
3226 return ret;
3227}
3228
3229static const struct file_operations tracing_buffers_fops = {
3230 .open = tracing_buffers_open,
3231 .read = tracing_buffers_read,
3232 .release = tracing_buffers_release,
3233 .splice_read = tracing_buffers_splice_read,
3234 .llseek = no_llseek,
3235};
3236
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003237#ifdef CONFIG_DYNAMIC_FTRACE
3238
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003239int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003240{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003241 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003242}
3243
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003244static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003245tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003246 size_t cnt, loff_t *ppos)
3247{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003248 static char ftrace_dyn_info_buffer[1024];
3249 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003250 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003251 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003252 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003253 int r;
3254
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003255 mutex_lock(&dyn_info_mutex);
3256 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003257
Steven Rostedta26a2a22008-10-31 00:03:22 -04003258 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003259 buf[r++] = '\n';
3260
3261 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3262
3263 mutex_unlock(&dyn_info_mutex);
3264
3265 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003266}
3267
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003268static struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003269 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003270 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003271};
3272#endif
3273
3274static struct dentry *d_tracer;
3275
3276struct dentry *tracing_init_dentry(void)
3277{
3278 static int once;
3279
3280 if (d_tracer)
3281 return d_tracer;
3282
3283 d_tracer = debugfs_create_dir("tracing", NULL);
3284
3285 if (!d_tracer && !once) {
3286 once = 1;
3287 pr_warning("Could not create debugfs directory 'tracing'\n");
3288 return NULL;
3289 }
3290
3291 return d_tracer;
3292}
3293
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003294static struct dentry *d_percpu;
3295
3296struct dentry *tracing_dentry_percpu(void)
3297{
3298 static int once;
3299 struct dentry *d_tracer;
3300
3301 if (d_percpu)
3302 return d_percpu;
3303
3304 d_tracer = tracing_init_dentry();
3305
3306 if (!d_tracer)
3307 return NULL;
3308
3309 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3310
3311 if (!d_percpu && !once) {
3312 once = 1;
3313 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3314 return NULL;
3315 }
3316
3317 return d_percpu;
3318}
3319
3320static void tracing_init_debugfs_percpu(long cpu)
3321{
3322 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003323 struct dentry *entry, *d_cpu;
3324 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3325 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003326
3327 if (cpu > 999 || cpu < 0)
3328 return;
3329
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003330 sprintf(cpu_dir, "cpu%ld", cpu);
3331 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3332 if (!d_cpu) {
3333 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3334 return;
3335 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003336
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003337 /* per cpu trace_pipe */
3338 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003339 (void *) cpu, &tracing_pipe_fops);
3340 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003341 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003342
3343 /* per cpu trace */
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003344 entry = debugfs_create_file("trace", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003345 (void *) cpu, &tracing_fops);
3346 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003347 pr_warning("Could not create debugfs 'trace' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003348}
3349
Steven Rostedt60a11772008-05-12 21:20:44 +02003350#ifdef CONFIG_FTRACE_SELFTEST
3351/* Let selftest have access to static functions in this file */
3352#include "trace_selftest.c"
3353#endif
3354
Steven Rostedt577b7852009-02-26 23:43:05 -05003355struct trace_option_dentry {
3356 struct tracer_opt *opt;
3357 struct tracer_flags *flags;
3358 struct dentry *entry;
3359};
3360
3361static ssize_t
3362trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3363 loff_t *ppos)
3364{
3365 struct trace_option_dentry *topt = filp->private_data;
3366 char *buf;
3367
3368 if (topt->flags->val & topt->opt->bit)
3369 buf = "1\n";
3370 else
3371 buf = "0\n";
3372
3373 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3374}
3375
3376static ssize_t
3377trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3378 loff_t *ppos)
3379{
3380 struct trace_option_dentry *topt = filp->private_data;
3381 unsigned long val;
3382 char buf[64];
3383 int ret;
3384
3385 if (cnt >= sizeof(buf))
3386 return -EINVAL;
3387
3388 if (copy_from_user(&buf, ubuf, cnt))
3389 return -EFAULT;
3390
3391 buf[cnt] = 0;
3392
3393 ret = strict_strtoul(buf, 10, &val);
3394 if (ret < 0)
3395 return ret;
3396
3397 ret = 0;
3398 switch (val) {
3399 case 0:
3400 /* do nothing if already cleared */
3401 if (!(topt->flags->val & topt->opt->bit))
3402 break;
3403
3404 mutex_lock(&trace_types_lock);
3405 if (current_trace->set_flag)
3406 ret = current_trace->set_flag(topt->flags->val,
3407 topt->opt->bit, 0);
3408 mutex_unlock(&trace_types_lock);
3409 if (ret)
3410 return ret;
3411 topt->flags->val &= ~topt->opt->bit;
3412 break;
3413 case 1:
3414 /* do nothing if already set */
3415 if (topt->flags->val & topt->opt->bit)
3416 break;
3417
3418 mutex_lock(&trace_types_lock);
3419 if (current_trace->set_flag)
3420 ret = current_trace->set_flag(topt->flags->val,
3421 topt->opt->bit, 1);
3422 mutex_unlock(&trace_types_lock);
3423 if (ret)
3424 return ret;
3425 topt->flags->val |= topt->opt->bit;
3426 break;
3427
3428 default:
3429 return -EINVAL;
3430 }
3431
3432 *ppos += cnt;
3433
3434 return cnt;
3435}
3436
3437
3438static const struct file_operations trace_options_fops = {
3439 .open = tracing_open_generic,
3440 .read = trace_options_read,
3441 .write = trace_options_write,
3442};
3443
Steven Rostedta8259072009-02-26 22:19:12 -05003444static ssize_t
3445trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3446 loff_t *ppos)
3447{
3448 long index = (long)filp->private_data;
3449 char *buf;
3450
3451 if (trace_flags & (1 << index))
3452 buf = "1\n";
3453 else
3454 buf = "0\n";
3455
3456 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3457}
3458
3459static ssize_t
3460trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3461 loff_t *ppos)
3462{
3463 long index = (long)filp->private_data;
3464 char buf[64];
3465 unsigned long val;
3466 int ret;
3467
3468 if (cnt >= sizeof(buf))
3469 return -EINVAL;
3470
3471 if (copy_from_user(&buf, ubuf, cnt))
3472 return -EFAULT;
3473
3474 buf[cnt] = 0;
3475
3476 ret = strict_strtoul(buf, 10, &val);
3477 if (ret < 0)
3478 return ret;
3479
3480 switch (val) {
3481 case 0:
3482 trace_flags &= ~(1 << index);
3483 break;
3484 case 1:
3485 trace_flags |= 1 << index;
3486 break;
3487
3488 default:
3489 return -EINVAL;
3490 }
3491
3492 *ppos += cnt;
3493
3494 return cnt;
3495}
3496
Steven Rostedta8259072009-02-26 22:19:12 -05003497static const struct file_operations trace_options_core_fops = {
3498 .open = tracing_open_generic,
3499 .read = trace_options_core_read,
3500 .write = trace_options_core_write,
3501};
3502
3503static struct dentry *trace_options_init_dentry(void)
3504{
3505 struct dentry *d_tracer;
3506 static struct dentry *t_options;
3507
3508 if (t_options)
3509 return t_options;
3510
3511 d_tracer = tracing_init_dentry();
3512 if (!d_tracer)
3513 return NULL;
3514
3515 t_options = debugfs_create_dir("options", d_tracer);
3516 if (!t_options) {
3517 pr_warning("Could not create debugfs directory 'options'\n");
3518 return NULL;
3519 }
3520
3521 return t_options;
3522}
3523
Steven Rostedt577b7852009-02-26 23:43:05 -05003524static void
3525create_trace_option_file(struct trace_option_dentry *topt,
3526 struct tracer_flags *flags,
3527 struct tracer_opt *opt)
3528{
3529 struct dentry *t_options;
3530 struct dentry *entry;
3531
3532 t_options = trace_options_init_dentry();
3533 if (!t_options)
3534 return;
3535
3536 topt->flags = flags;
3537 topt->opt = opt;
3538
3539 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3540 &trace_options_fops);
3541
3542 topt->entry = entry;
3543
3544}
3545
3546static struct trace_option_dentry *
3547create_trace_option_files(struct tracer *tracer)
3548{
3549 struct trace_option_dentry *topts;
3550 struct tracer_flags *flags;
3551 struct tracer_opt *opts;
3552 int cnt;
3553
3554 if (!tracer)
3555 return NULL;
3556
3557 flags = tracer->flags;
3558
3559 if (!flags || !flags->opts)
3560 return NULL;
3561
3562 opts = flags->opts;
3563
3564 for (cnt = 0; opts[cnt].name; cnt++)
3565 ;
3566
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003567 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003568 if (!topts)
3569 return NULL;
3570
3571 for (cnt = 0; opts[cnt].name; cnt++)
3572 create_trace_option_file(&topts[cnt], flags,
3573 &opts[cnt]);
3574
3575 return topts;
3576}
3577
3578static void
3579destroy_trace_option_files(struct trace_option_dentry *topts)
3580{
3581 int cnt;
3582
3583 if (!topts)
3584 return;
3585
3586 for (cnt = 0; topts[cnt].opt; cnt++) {
3587 if (topts[cnt].entry)
3588 debugfs_remove(topts[cnt].entry);
3589 }
3590
3591 kfree(topts);
3592}
3593
Steven Rostedta8259072009-02-26 22:19:12 -05003594static struct dentry *
3595create_trace_option_core_file(const char *option, long index)
3596{
3597 struct dentry *t_options;
3598 struct dentry *entry;
3599
3600 t_options = trace_options_init_dentry();
3601 if (!t_options)
3602 return NULL;
3603
3604 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3605 &trace_options_core_fops);
3606
3607 return entry;
3608}
3609
3610static __init void create_trace_options_dir(void)
3611{
3612 struct dentry *t_options;
3613 struct dentry *entry;
3614 int i;
3615
3616 t_options = trace_options_init_dentry();
3617 if (!t_options)
3618 return;
3619
3620 for (i = 0; trace_options[i]; i++) {
3621 entry = create_trace_option_core_file(trace_options[i], i);
3622 if (!entry)
3623 pr_warning("Could not create debugfs %s entry\n",
3624 trace_options[i]);
3625 }
3626}
3627
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003628static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003629{
3630 struct dentry *d_tracer;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003631 struct dentry *buffers;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003632 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003633 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003634
3635 d_tracer = tracing_init_dentry();
3636
3637 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3638 &global_trace, &tracing_ctrl_fops);
3639 if (!entry)
3640 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3641
Steven Rostedtee6bce52008-11-12 17:52:37 -05003642 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003643 NULL, &tracing_iter_fops);
3644 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003645 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003646
Steven Rostedta8259072009-02-26 22:19:12 -05003647 create_trace_options_dir();
3648
Ingo Molnarc7078de2008-05-12 21:20:52 +02003649 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3650 NULL, &tracing_cpumask_fops);
3651 if (!entry)
3652 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3653
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003654 entry = debugfs_create_file("trace", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003655 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003656 if (!entry)
3657 pr_warning("Could not create debugfs 'trace' entry\n");
3658
3659 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3660 &global_trace, &show_traces_fops);
3661 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003662 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003663
3664 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3665 &global_trace, &set_tracer_fops);
3666 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003667 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003668
3669 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3670 &tracing_max_latency,
3671 &tracing_max_lat_fops);
3672 if (!entry)
3673 pr_warning("Could not create debugfs "
3674 "'tracing_max_latency' entry\n");
3675
3676 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3677 &tracing_thresh, &tracing_max_lat_fops);
3678 if (!entry)
3679 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003680 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003681 entry = debugfs_create_file("README", 0644, d_tracer,
3682 NULL, &tracing_readme_fops);
3683 if (!entry)
3684 pr_warning("Could not create debugfs 'README' entry\n");
3685
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003686 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3687 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003688 if (!entry)
3689 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003690 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003691
Steven Rostedta94c80e2008-11-12 17:52:36 -05003692 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003693 &global_trace, &tracing_entries_fops);
3694 if (!entry)
3695 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003696 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003697
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003698 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3699 NULL, &tracing_mark_fops);
3700 if (!entry)
3701 pr_warning("Could not create debugfs "
3702 "'trace_marker' entry\n");
3703
Steven Rostedt2cadf912008-12-01 22:20:19 -05003704 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3705
3706 if (!buffers)
3707 pr_warning("Could not create buffers directory\n");
3708 else {
3709 int cpu;
3710 char buf[64];
3711
3712 for_each_tracing_cpu(cpu) {
3713 sprintf(buf, "%d", cpu);
3714
3715 entry = debugfs_create_file(buf, 0444, buffers,
3716 (void *)(long)cpu,
3717 &tracing_buffers_fops);
3718 if (!entry)
3719 pr_warning("Could not create debugfs buffers "
3720 "'%s' entry\n", buf);
3721 }
3722 }
3723
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003724#ifdef CONFIG_DYNAMIC_FTRACE
3725 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3726 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003727 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003728 if (!entry)
3729 pr_warning("Could not create debugfs "
3730 "'dyn_ftrace_total_info' entry\n");
3731#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003732#ifdef CONFIG_SYSPROF_TRACER
3733 init_tracer_sysprof_debugfs(d_tracer);
3734#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003735
3736 for_each_tracing_cpu(cpu)
3737 tracing_init_debugfs_percpu(cpu);
3738
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003739 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003740}
3741
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003742int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003743{
Peter Zijlstraefed7922009-03-04 12:32:55 +01003744 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003745 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003746
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003747 struct ring_buffer_event *event;
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003748 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003749 struct trace_array_cpu *data;
Steven Rostedt38697052008-10-01 13:14:09 -04003750 int cpu, len = 0, size, pc;
Ingo Molnare726f5f2008-12-08 16:55:53 +01003751 struct print_entry *entry;
3752 unsigned long irq_flags;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003753
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01003754 if (tracing_disabled || tracing_selftest_running)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003755 return 0;
3756
Steven Rostedt38697052008-10-01 13:14:09 -04003757 pc = preempt_count();
3758 preempt_disable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003759 cpu = raw_smp_processor_id();
3760 data = tr->data[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003761
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -04003762 if (unlikely(atomic_read(&data->disabled)))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003763 goto out;
3764
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003765 pause_graph_tracing();
Peter Zijlstraefed7922009-03-04 12:32:55 +01003766 raw_local_irq_save(irq_flags);
3767 __raw_spin_lock(&trace_buf_lock);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003768 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003769
3770 len = min(len, TRACE_BUF_SIZE-1);
3771 trace_buf[len] = 0;
3772
Steven Rostedt777e2082008-09-29 23:02:42 -04003773 size = sizeof(*entry) + len + 1;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02003774 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003775 if (!event)
3776 goto out_unlock;
Steven Rostedt777e2082008-09-29 23:02:42 -04003777 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04003778 entry->ip = ip;
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003779 entry->depth = depth;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003780
Steven Rostedt777e2082008-09-29 23:02:42 -04003781 memcpy(&entry->buf, trace_buf, len);
3782 entry->buf[len] = 0;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02003783 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003784
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003785 out_unlock:
Peter Zijlstraefed7922009-03-04 12:32:55 +01003786 __raw_spin_unlock(&trace_buf_lock);
3787 raw_local_irq_restore(irq_flags);
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003788 unpause_graph_tracing();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003789 out:
Steven Rostedt38697052008-10-01 13:14:09 -04003790 preempt_enable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003791
3792 return len;
3793}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003794EXPORT_SYMBOL_GPL(trace_vprintk);
3795
3796int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3797{
3798 int ret;
3799 va_list ap;
3800
3801 if (!(trace_flags & TRACE_ITER_PRINTK))
3802 return 0;
3803
3804 va_start(ap, fmt);
Frederic Weisbecker21a8c462008-12-04 23:51:23 +01003805 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003806 va_end(ap);
3807 return ret;
3808}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003809EXPORT_SYMBOL_GPL(__ftrace_printk);
3810
Arnaldo Carvalho de Melo90112622009-01-23 12:06:23 -02003811int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3812{
3813 if (!(trace_flags & TRACE_ITER_PRINTK))
3814 return 0;
3815
3816 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3817}
3818EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3819
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003820static int trace_panic_handler(struct notifier_block *this,
3821 unsigned long event, void *unused)
3822{
Steven Rostedt944ac422008-10-23 19:26:08 -04003823 if (ftrace_dump_on_oops)
3824 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003825 return NOTIFY_OK;
3826}
3827
3828static struct notifier_block trace_panic_notifier = {
3829 .notifier_call = trace_panic_handler,
3830 .next = NULL,
3831 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3832};
3833
3834static int trace_die_handler(struct notifier_block *self,
3835 unsigned long val,
3836 void *data)
3837{
3838 switch (val) {
3839 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003840 if (ftrace_dump_on_oops)
3841 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003842 break;
3843 default:
3844 break;
3845 }
3846 return NOTIFY_OK;
3847}
3848
3849static struct notifier_block trace_die_notifier = {
3850 .notifier_call = trace_die_handler,
3851 .priority = 200
3852};
3853
3854/*
3855 * printk is set to max of 1024, we really don't need it that big.
3856 * Nothing should be printing 1000 characters anyway.
3857 */
3858#define TRACE_MAX_PRINT 1000
3859
3860/*
3861 * Define here KERN_TRACE so that we have one place to modify
3862 * it if we decide to change what log level the ftrace dump
3863 * should be at.
3864 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003865#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003866
3867static void
3868trace_printk_seq(struct trace_seq *s)
3869{
3870 /* Probably should print a warning here. */
3871 if (s->len >= 1000)
3872 s->len = 1000;
3873
3874 /* should be zero ended, but we are paranoid. */
3875 s->buffer[s->len] = 0;
3876
3877 printk(KERN_TRACE "%s", s->buffer);
3878
Steven Rostedtf9520752009-03-02 14:04:40 -05003879 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003880}
3881
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003882void ftrace_dump(void)
3883{
3884 static DEFINE_SPINLOCK(ftrace_dump_lock);
3885 /* use static because iter can be a bit big for the stack */
3886 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003887 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003888 unsigned long flags;
3889 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003890
3891 /* only one dump */
3892 spin_lock_irqsave(&ftrace_dump_lock, flags);
3893 if (dump_ran)
3894 goto out;
3895
3896 dump_ran = 1;
3897
3898 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05003899 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003900 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003901
Steven Rostedtd7690412008-10-01 00:29:53 -04003902 for_each_tracing_cpu(cpu) {
3903 atomic_inc(&global_trace.data[cpu]->disabled);
3904 }
3905
Török Edwinb54d3de2008-11-22 13:28:48 +02003906 /* don't look at user memory in panic mode */
3907 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3908
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003909 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3910
Steven Rostedte543ad72009-03-04 18:20:36 -05003911 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003912 iter.tr = &global_trace;
3913 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05003914 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003915
3916 /*
3917 * We need to stop all tracing on all CPUS to read the
3918 * the next buffer. This is a bit expensive, but is
3919 * not done often. We fill all what we can read,
3920 * and then release the locks again.
3921 */
3922
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003923 while (!trace_empty(&iter)) {
3924
3925 if (!cnt)
3926 printk(KERN_TRACE "---------------------------------\n");
3927
3928 cnt++;
3929
3930 /* reset all but tr, trace, and overruns */
3931 memset(&iter.seq, 0,
3932 sizeof(struct trace_iterator) -
3933 offsetof(struct trace_iterator, seq));
3934 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3935 iter.pos = -1;
3936
3937 if (find_next_entry_inc(&iter) != NULL) {
3938 print_trace_line(&iter);
3939 trace_consume(&iter);
3940 }
3941
3942 trace_printk_seq(&iter.seq);
3943 }
3944
3945 if (!cnt)
3946 printk(KERN_TRACE " (ftrace buffer empty)\n");
3947 else
3948 printk(KERN_TRACE "---------------------------------\n");
3949
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003950 out:
3951 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3952}
3953
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003954__init static int tracer_alloc_buffers(void)
3955{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003956 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003957 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303958 int ret = -ENOMEM;
3959
3960 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3961 goto out;
3962
3963 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3964 goto out_free_buffer_mask;
3965
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003966 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3967 goto out_free_tracing_cpumask;
3968
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303969 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3970 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003971 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003972
Steven Rostedtab464282008-05-12 21:21:00 +02003973 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003974 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3975 TRACE_BUFFER_FLAGS);
3976 if (!global_trace.buffer) {
3977 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3978 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303979 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003980 }
3981 global_trace.entries = ring_buffer_size(global_trace.buffer);
3982
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303983
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003984#ifdef CONFIG_TRACER_MAX_TRACE
3985 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3986 TRACE_BUFFER_FLAGS);
3987 if (!max_tr.buffer) {
3988 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3989 WARN_ON(1);
3990 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303991 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003992 }
3993 max_tr.entries = ring_buffer_size(max_tr.buffer);
3994 WARN_ON(max_tr.entries != global_trace.entries);
3995#endif
3996
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003997 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003998 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003999 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004000 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004001 }
4002
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004003 trace_init_cmdlines();
4004
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004005 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004006 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004007#ifdef CONFIG_BOOT_TRACER
4008 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004009#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004010 /* All seems OK, enable tracing */
4011 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004012
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004013 atomic_notifier_chain_register(&panic_notifier_list,
4014 &trace_panic_notifier);
4015
4016 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304017 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004018
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304019out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004020 free_cpumask_var(tracing_reader_cpumask);
4021out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304022 free_cpumask_var(tracing_cpumask);
4023out_free_buffer_mask:
4024 free_cpumask_var(tracing_buffer_mask);
4025out:
4026 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004027}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004028
4029__init static int clear_boot_tracer(void)
4030{
4031 /*
4032 * The default tracer at boot buffer is an init section.
4033 * This function is called in lateinit. If we did not
4034 * find the boot tracer, then clear it out, to prevent
4035 * later registration from accessing the buffer that is
4036 * about to be freed.
4037 */
4038 if (!default_bootup_tracer)
4039 return 0;
4040
4041 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4042 default_bootup_tracer);
4043 default_bootup_tracer = NULL;
4044
4045 return 0;
4046}
4047
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004048early_initcall(tracer_alloc_buffers);
4049fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004050late_initcall(clear_boot_tracer);