blob: c8abbb0c8397de04283d0a572d7e5b9097572b61 [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 Rostedtbc0c38d2008-05-12 21:20:42 +0200302 NULL
303};
304
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200305/*
306 * ftrace_max_lock is used to protect the swapping of buffers
307 * when taking a max snapshot. The buffers themselves are
308 * protected by per_cpu spinlocks. But the action of the swap
309 * needs its own lock.
310 *
311 * This is defined as a raw_spinlock_t in order to help
312 * with performance when lockdep debugging is enabled.
313 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200314static raw_spinlock_t ftrace_max_lock =
315 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200316
317/*
318 * Copy the new maximum trace into the separate maximum-trace
319 * structure. (this way the maximum trace is permanently saved,
320 * for later retrieval via /debugfs/tracing/latency_trace)
321 */
Ingo Molnare309b412008-05-12 21:20:51 +0200322static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200323__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
324{
325 struct trace_array_cpu *data = tr->data[cpu];
326
327 max_tr.cpu = cpu;
328 max_tr.time_start = data->preempt_timestamp;
329
330 data = max_tr.data[cpu];
331 data->saved_latency = tracing_max_latency;
332
333 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
334 data->pid = tsk->pid;
David Howellsb6dff3e2008-11-14 10:39:16 +1100335 data->uid = task_uid(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200336 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
337 data->policy = tsk->policy;
338 data->rt_priority = tsk->rt_priority;
339
340 /* record this tasks comm */
Wenji Huangaf513092009-02-17 01:07:28 -0500341 tracing_record_cmdline(tsk);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200342}
343
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200344ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
345{
346 int len;
347 int ret;
348
349 if (s->len <= s->readpos)
350 return -EBUSY;
351
352 len = s->len - s->readpos;
353 if (cnt > len)
354 cnt = len;
355 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
356 if (ret)
357 return -EFAULT;
358
359 s->readpos += len;
360 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200361}
362
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200363ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
364{
365 int len;
366 void *ret;
367
368 if (s->len <= s->readpos)
369 return -EBUSY;
370
371 len = s->len - s->readpos;
372 if (cnt > len)
373 cnt = len;
374 ret = memcpy(buf, s->buffer + s->readpos, cnt);
375 if (!ret)
376 return -EFAULT;
377
378 s->readpos += len;
379 return cnt;
380}
381
Ingo Molnare309b412008-05-12 21:20:51 +0200382static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200383trace_print_seq(struct seq_file *m, struct trace_seq *s)
384{
385 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
386
387 s->buffer[len] = 0;
388 seq_puts(m, s->buffer);
389
Steven Rostedtf9520752009-03-02 14:04:40 -0500390 trace_seq_init(s);
Steven Rostedt214023c2008-05-12 21:20:46 +0200391}
392
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200393/**
394 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
395 * @tr: tracer
396 * @tsk: the task with the latency
397 * @cpu: The cpu that initiated the trace.
398 *
399 * Flip the buffers between the @tr and the max_tr and record information
400 * about which task was the cause of this latency.
401 */
Ingo Molnare309b412008-05-12 21:20:51 +0200402void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200403update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
404{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400405 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200406
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200407 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200408 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400409
410 tr->buffer = max_tr.buffer;
411 max_tr.buffer = buf;
412
Steven Rostedtd7690412008-10-01 00:29:53 -0400413 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400414 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400415 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200416
417 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200418 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200419}
420
421/**
422 * update_max_tr_single - only copy one trace over, and reset the rest
423 * @tr - tracer
424 * @tsk - task with the latency
425 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200426 *
427 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200428 */
Ingo Molnare309b412008-05-12 21:20:51 +0200429void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200430update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
431{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400432 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200433
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200434 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200435 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200436
Steven Rostedtd7690412008-10-01 00:29:53 -0400437 ftrace_disable_cpu();
438
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400439 ring_buffer_reset(max_tr.buffer);
440 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
441
Steven Rostedtd7690412008-10-01 00:29:53 -0400442 ftrace_enable_cpu();
443
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500444 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200445
446 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200447 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200448}
449
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200450/**
451 * register_tracer - register a tracer with the ftrace system.
452 * @type - the plugin for the tracer
453 *
454 * Register a new plugin tracer.
455 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200456int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100457__releases(kernel_lock)
458__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200459{
460 struct tracer *t;
461 int len;
462 int ret = 0;
463
464 if (!type->name) {
465 pr_info("Tracer must have a name\n");
466 return -1;
467 }
468
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100469 /*
470 * When this gets called we hold the BKL which means that
471 * preemption is disabled. Various trace selftests however
472 * need to disable and enable preemption for successful tests.
473 * So we drop the BKL here and grab it after the tests again.
474 */
475 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200476 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100477
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100478 tracing_selftest_running = true;
479
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200480 for (t = trace_types; t; t = t->next) {
481 if (strcmp(type->name, t->name) == 0) {
482 /* already found */
483 pr_info("Trace %s already registered\n",
484 type->name);
485 ret = -1;
486 goto out;
487 }
488 }
489
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100490 if (!type->set_flag)
491 type->set_flag = &dummy_set_flag;
492 if (!type->flags)
493 type->flags = &dummy_tracer_flags;
494 else
495 if (!type->flags->opts)
496 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100497 if (!type->wait_pipe)
498 type->wait_pipe = default_wait_pipe;
499
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100500
Steven Rostedt60a11772008-05-12 21:20:44 +0200501#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500502 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200503 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200504 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200505 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100506
Steven Rostedt60a11772008-05-12 21:20:44 +0200507 /*
508 * Run a selftest on this tracer.
509 * Here we reset the trace buffer, and set the current
510 * tracer to be this tracer. The tracer can then run some
511 * internal tracing to verify that everything is in order.
512 * If we fail, we do not register this tracer.
513 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100514 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400515 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100516
Steven Rostedt60a11772008-05-12 21:20:44 +0200517 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200518 /* the test is responsible for initializing and enabling */
519 pr_info("Testing tracer %s: ", type->name);
520 ret = type->selftest(type, tr);
521 /* the test is responsible for resetting too */
522 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200523 if (ret) {
524 printk(KERN_CONT "FAILED!\n");
525 goto out;
526 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200527 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100528 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400529 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100530
Steven Rostedt60a11772008-05-12 21:20:44 +0200531 printk(KERN_CONT "PASSED\n");
532 }
533#endif
534
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200535 type->next = trace_types;
536 trace_types = type;
537 len = strlen(type->name);
538 if (len > max_tracer_type_len)
539 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200540
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200541 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100542 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200543 mutex_unlock(&trace_types_lock);
544
Steven Rostedtdac74942009-02-05 01:13:38 -0500545 if (ret || !default_bootup_tracer)
546 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500547
Steven Rostedtdac74942009-02-05 01:13:38 -0500548 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
549 goto out_unlock;
550
551 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
552 /* Do we want this tracer to start on bootup? */
553 tracing_set_tracer(type->name);
554 default_bootup_tracer = NULL;
555 /* disable other selftests, since this will break it. */
556 tracing_selftest_disabled = 1;
557#ifdef CONFIG_FTRACE_STARTUP_TEST
558 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
559 type->name);
560#endif
561
562 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500563 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200564 return ret;
565}
566
567void unregister_tracer(struct tracer *type)
568{
569 struct tracer **t;
570 int len;
571
572 mutex_lock(&trace_types_lock);
573 for (t = &trace_types; *t; t = &(*t)->next) {
574 if (*t == type)
575 goto found;
576 }
577 pr_info("Trace %s not registered\n", type->name);
578 goto out;
579
580 found:
581 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200582
583 if (type == current_trace && tracer_enabled) {
584 tracer_enabled = 0;
585 tracing_stop();
586 if (current_trace->stop)
587 current_trace->stop(&global_trace);
588 current_trace = &nop_trace;
589 }
590
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200591 if (strlen(type->name) != max_tracer_type_len)
592 goto out;
593
594 max_tracer_type_len = 0;
595 for (t = &trace_types; *t; t = &(*t)->next) {
596 len = strlen((*t)->name);
597 if (len > max_tracer_type_len)
598 max_tracer_type_len = len;
599 }
600 out:
601 mutex_unlock(&trace_types_lock);
602}
603
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400604void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200605{
Steven Rostedtd7690412008-10-01 00:29:53 -0400606 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400607 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400608 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200609}
610
Pekka J Enberg213cc062008-12-19 12:08:39 +0200611void tracing_reset_online_cpus(struct trace_array *tr)
612{
613 int cpu;
614
615 tr->time_start = ftrace_now(tr->cpu);
616
617 for_each_online_cpu(cpu)
618 tracing_reset(tr, cpu);
619}
620
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200621#define SAVED_CMDLINES 128
622static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
623static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
624static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
625static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100626static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200627
Steven Rostedt25b0b442008-05-12 21:21:00 +0200628/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100629static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200630
631static void trace_init_cmdlines(void)
632{
633 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
634 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
635 cmdline_idx = 0;
636}
637
Steven Rostedt0f048702008-11-05 16:05:44 -0500638static int trace_stop_count;
639static DEFINE_SPINLOCK(tracing_start_lock);
640
641/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500642 * ftrace_off_permanent - disable all ftrace code permanently
643 *
644 * This should only be called when a serious anomally has
645 * been detected. This will turn off the function tracing,
646 * ring buffers, and other tracing utilites. It takes no
647 * locks and can be called from any context.
648 */
649void ftrace_off_permanent(void)
650{
651 tracing_disabled = 1;
652 ftrace_stop();
653 tracing_off_permanent();
654}
655
656/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500657 * tracing_start - quick start of the tracer
658 *
659 * If tracing is enabled but was stopped by tracing_stop,
660 * this will start the tracer back up.
661 */
662void tracing_start(void)
663{
664 struct ring_buffer *buffer;
665 unsigned long flags;
666
667 if (tracing_disabled)
668 return;
669
670 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500671 if (--trace_stop_count) {
672 if (trace_stop_count < 0) {
673 /* Someone screwed up their debugging */
674 WARN_ON_ONCE(1);
675 trace_stop_count = 0;
676 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500677 goto out;
678 }
679
680
681 buffer = global_trace.buffer;
682 if (buffer)
683 ring_buffer_record_enable(buffer);
684
685 buffer = max_tr.buffer;
686 if (buffer)
687 ring_buffer_record_enable(buffer);
688
689 ftrace_start();
690 out:
691 spin_unlock_irqrestore(&tracing_start_lock, flags);
692}
693
694/**
695 * tracing_stop - quick stop of the tracer
696 *
697 * Light weight way to stop tracing. Use in conjunction with
698 * tracing_start.
699 */
700void tracing_stop(void)
701{
702 struct ring_buffer *buffer;
703 unsigned long flags;
704
705 ftrace_stop();
706 spin_lock_irqsave(&tracing_start_lock, flags);
707 if (trace_stop_count++)
708 goto out;
709
710 buffer = global_trace.buffer;
711 if (buffer)
712 ring_buffer_record_disable(buffer);
713
714 buffer = max_tr.buffer;
715 if (buffer)
716 ring_buffer_record_disable(buffer);
717
718 out:
719 spin_unlock_irqrestore(&tracing_start_lock, flags);
720}
721
Ingo Molnare309b412008-05-12 21:20:51 +0200722void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200723
Ingo Molnare309b412008-05-12 21:20:51 +0200724static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200725{
726 unsigned map;
727 unsigned idx;
728
729 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
730 return;
731
732 /*
733 * It's not the end of the world if we don't get
734 * the lock, but we also don't want to spin
735 * nor do we want to disable interrupts,
736 * so if we miss here, then better luck next time.
737 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100738 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200739 return;
740
741 idx = map_pid_to_cmdline[tsk->pid];
742 if (idx >= SAVED_CMDLINES) {
743 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
744
745 map = map_cmdline_to_pid[idx];
746 if (map <= PID_MAX_DEFAULT)
747 map_pid_to_cmdline[map] = (unsigned)-1;
748
749 map_pid_to_cmdline[tsk->pid] = idx;
750
751 cmdline_idx = idx;
752 }
753
754 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
755
Peter Zijlstraefed7922009-03-04 12:32:55 +0100756 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200757}
758
Steven Rostedt660c7f92008-11-26 00:16:26 -0500759char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200760{
761 char *cmdline = "<...>";
762 unsigned map;
763
764 if (!pid)
765 return "<idle>";
766
767 if (pid > PID_MAX_DEFAULT)
768 goto out;
769
770 map = map_pid_to_cmdline[pid];
771 if (map >= SAVED_CMDLINES)
772 goto out;
773
774 cmdline = saved_cmdlines[map];
775
776 out:
777 return cmdline;
778}
779
Ingo Molnare309b412008-05-12 21:20:51 +0200780void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200781{
782 if (atomic_read(&trace_record_cmdline_disabled))
783 return;
784
785 trace_save_cmdline(tsk);
786}
787
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300788void
Steven Rostedt38697052008-10-01 13:14:09 -0400789tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
790 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200791{
792 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200793
Steven Rostedt777e2082008-09-29 23:02:42 -0400794 entry->preempt_count = pc & 0xff;
795 entry->pid = (tsk) ? tsk->pid : 0;
Török Edwinb54d3de2008-11-22 13:28:48 +0200796 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400797 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400798#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400799 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400800#else
801 TRACE_FLAG_IRQS_NOSUPPORT |
802#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200803 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
804 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
805 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
806}
807
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200808struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
809 unsigned char type,
810 unsigned long len,
811 unsigned long flags, int pc)
812{
813 struct ring_buffer_event *event;
814
815 event = ring_buffer_lock_reserve(tr->buffer, len);
816 if (event != NULL) {
817 struct trace_entry *ent = ring_buffer_event_data(event);
818
819 tracing_generic_entry_update(ent, flags, pc);
820 ent->type = type;
821 }
822
823 return event;
824}
825static void ftrace_trace_stack(struct trace_array *tr,
826 unsigned long flags, int skip, int pc);
827static void ftrace_trace_userstack(struct trace_array *tr,
828 unsigned long flags, int pc);
829
830void trace_buffer_unlock_commit(struct trace_array *tr,
831 struct ring_buffer_event *event,
832 unsigned long flags, int pc)
833{
834 ring_buffer_unlock_commit(tr->buffer, event);
835
836 ftrace_trace_stack(tr, flags, 6, pc);
837 ftrace_trace_userstack(tr, flags, pc);
838 trace_wake_up();
839}
840
Steven Rostedtef5580d2009-02-27 19:38:04 -0500841struct ring_buffer_event *
842trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
843 unsigned long flags, int pc)
844{
845 return trace_buffer_lock_reserve(&global_trace,
846 type, len, flags, pc);
847}
848
849void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
850 unsigned long flags, int pc)
851{
852 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
853}
854
Ingo Molnare309b412008-05-12 21:20:51 +0200855void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500856trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400857 unsigned long ip, unsigned long parent_ip, unsigned long flags,
858 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200859{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400860 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400861 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200862
Steven Rostedtd7690412008-10-01 00:29:53 -0400863 /* If we are reading the ring buffer, don't trace */
864 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
865 return;
866
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200867 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
868 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400869 if (!event)
870 return;
871 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400872 entry->ip = ip;
873 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200874 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200875}
876
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100877#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100878static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100879 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100880 unsigned long flags,
881 int pc)
882{
883 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100884 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100885
886 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
887 return;
888
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200889 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
890 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100891 if (!event)
892 return;
893 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100894 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200895 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100896}
897
898static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100899 struct ftrace_graph_ret *trace,
900 unsigned long flags,
901 int pc)
902{
903 struct ring_buffer_event *event;
904 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100905
906 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
907 return;
908
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200909 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
910 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100911 if (!event)
912 return;
913 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100914 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200915 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100916}
917#endif
918
Ingo Molnare309b412008-05-12 21:20:51 +0200919void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200920ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400921 unsigned long ip, unsigned long parent_ip, unsigned long flags,
922 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200923{
924 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500925 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200926}
927
Steven Rostedt53614992009-01-15 19:12:40 -0500928static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500929 unsigned long flags,
930 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200931{
Al Viroc2c80522008-10-31 19:50:41 +0000932#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400933 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400934 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200935 struct stack_trace trace;
936
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200937 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
938 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400939 if (!event)
940 return;
941 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400942 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200943
944 trace.nr_entries = 0;
945 trace.max_entries = FTRACE_STACK_ENTRIES;
946 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400947 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200948
949 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200950 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000951#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200952}
953
Steven Rostedt53614992009-01-15 19:12:40 -0500954static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500955 unsigned long flags,
956 int skip, int pc)
957{
958 if (!(trace_flags & TRACE_ITER_STACKTRACE))
959 return;
960
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500961 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500962}
963
Steven Rostedt38697052008-10-01 13:14:09 -0400964void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400965 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500966 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400967{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500968 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400969}
970
Török Edwin02b67512008-11-22 13:28:47 +0200971static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500972 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +0200973{
Török Edwinc7425ac2008-11-28 11:17:56 +0200974#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +0200975 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +0200976 struct userstack_entry *entry;
977 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +0200978
979 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
980 return;
981
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200982 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
983 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +0200984 if (!event)
985 return;
986 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +0200987
988 memset(&entry->caller, 0, sizeof(entry->caller));
989
990 trace.nr_entries = 0;
991 trace.max_entries = FTRACE_STACK_ENTRIES;
992 trace.skip = 0;
993 trace.entries = entry->caller;
994
995 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200996 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +0200997#endif
Török Edwin02b67512008-11-22 13:28:47 +0200998}
999
Hannes Eder4fd27352009-02-10 19:44:12 +01001000#ifdef UNUSED
1001static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001002{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001003 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001004}
Hannes Eder4fd27352009-02-10 19:44:12 +01001005#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001006
Steven Rostedt38697052008-10-01 13:14:09 -04001007static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001008ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001009 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1010 int pc)
Ingo Molnara4feb8342008-05-12 21:21:02 +02001011{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001012 struct ring_buffer_event *event;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001013 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001014 struct special_entry *entry;
Ingo Molnara4feb8342008-05-12 21:21:02 +02001015
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001016 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1017 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001018 if (!event)
1019 return;
1020 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001021 entry->arg1 = arg1;
1022 entry->arg2 = arg2;
1023 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001024 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb8342008-05-12 21:21:02 +02001025}
1026
1027void
Steven Rostedt38697052008-10-01 13:14:09 -04001028__trace_special(void *__tr, void *__data,
1029 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1030{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001031 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001032}
1033
1034void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001035tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001036 struct task_struct *prev,
1037 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001038 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001039{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001040 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001041 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001042
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001043 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1044 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001045 if (!event)
1046 return;
1047 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001048 entry->prev_pid = prev->pid;
1049 entry->prev_prio = prev->prio;
1050 entry->prev_state = prev->state;
1051 entry->next_pid = next->pid;
1052 entry->next_prio = next->prio;
1053 entry->next_state = next->state;
1054 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001055 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056}
1057
Ingo Molnar57422792008-05-12 21:20:51 +02001058void
1059tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001060 struct task_struct *wakee,
1061 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001062 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001063{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001064 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001065 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001066
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001067 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1068 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001069 if (!event)
1070 return;
1071 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001072 entry->prev_pid = curr->pid;
1073 entry->prev_prio = curr->prio;
1074 entry->prev_state = curr->state;
1075 entry->next_pid = wakee->pid;
1076 entry->next_prio = wakee->prio;
1077 entry->next_state = wakee->state;
1078 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001079
1080 ring_buffer_unlock_commit(tr->buffer, event);
1081 ftrace_trace_stack(tr, flags, 6, pc);
1082 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001083}
1084
Steven Rostedt4902f882008-05-22 00:22:18 -04001085void
1086ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1087{
1088 struct trace_array *tr = &global_trace;
1089 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001090 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001091 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001092 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001093
Steven Rostedtc76f0692008-11-07 22:36:02 -05001094 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001095 return;
1096
Steven Rostedt38697052008-10-01 13:14:09 -04001097 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001098 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001099 cpu = raw_smp_processor_id();
1100 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001101
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001102 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001103 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001104
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001105 atomic_dec(&data->disabled);
1106 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001107}
1108
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001109#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001110int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001111{
1112 struct trace_array *tr = &global_trace;
1113 struct trace_array_cpu *data;
1114 unsigned long flags;
1115 long disabled;
1116 int cpu;
1117 int pc;
1118
Steven Rostedt804a6852008-12-03 15:36:59 -05001119 if (!ftrace_trace_task(current))
1120 return 0;
1121
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001122 if (!ftrace_graph_addr(trace->func))
1123 return 0;
1124
Steven Rostedta5e25882008-12-02 15:34:05 -05001125 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001126 cpu = raw_smp_processor_id();
1127 data = tr->data[cpu];
1128 disabled = atomic_inc_return(&data->disabled);
1129 if (likely(disabled == 1)) {
1130 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001131 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001132 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001133 /* Only do the atomic if it is not already set */
1134 if (!test_tsk_trace_graph(current))
1135 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001136 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001137 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001138
1139 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001140}
1141
1142void trace_graph_return(struct ftrace_graph_ret *trace)
1143{
1144 struct trace_array *tr = &global_trace;
1145 struct trace_array_cpu *data;
1146 unsigned long flags;
1147 long disabled;
1148 int cpu;
1149 int pc;
1150
Steven Rostedta5e25882008-12-02 15:34:05 -05001151 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001152 cpu = raw_smp_processor_id();
1153 data = tr->data[cpu];
1154 disabled = atomic_inc_return(&data->disabled);
1155 if (likely(disabled == 1)) {
1156 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001157 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001158 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001159 if (!trace->depth)
1160 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001161 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001162 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001163}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001164#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001165
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001166enum trace_file_type {
1167 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001168 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001169};
1170
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001171static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001172{
Steven Rostedtd7690412008-10-01 00:29:53 -04001173 /* Don't allow ftrace to trace into the ring buffers */
1174 ftrace_disable_cpu();
1175
Steven Rostedt5a90f572008-09-03 17:42:51 -04001176 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001177 if (iter->buffer_iter[iter->cpu])
1178 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1179
1180 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001181}
1182
Ingo Molnare309b412008-05-12 21:20:51 +02001183static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001184peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001185{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001186 struct ring_buffer_event *event;
1187 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001188
Steven Rostedtd7690412008-10-01 00:29:53 -04001189 /* Don't allow ftrace to trace into the ring buffers */
1190 ftrace_disable_cpu();
1191
1192 if (buf_iter)
1193 event = ring_buffer_iter_peek(buf_iter, ts);
1194 else
1195 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1196
1197 ftrace_enable_cpu();
1198
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001199 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001200}
Steven Rostedtd7690412008-10-01 00:29:53 -04001201
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001202static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001203__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001204{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001205 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001206 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001207 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001208 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001209 int next_cpu = -1;
1210 int cpu;
1211
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001212 /*
1213 * If we are in a per_cpu trace file, don't bother by iterating over
1214 * all cpu and peek directly.
1215 */
1216 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1217 if (ring_buffer_empty_cpu(buffer, cpu_file))
1218 return NULL;
1219 ent = peek_next_entry(iter, cpu_file, ent_ts);
1220 if (ent_cpu)
1221 *ent_cpu = cpu_file;
1222
1223 return ent;
1224 }
1225
Steven Rostedtab464282008-05-12 21:21:00 +02001226 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001227
1228 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001229 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001230
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001231 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001232
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001233 /*
1234 * Pick the entry with the smallest timestamp:
1235 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001236 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001237 next = ent;
1238 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001239 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240 }
1241 }
1242
1243 if (ent_cpu)
1244 *ent_cpu = next_cpu;
1245
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001246 if (ent_ts)
1247 *ent_ts = next_ts;
1248
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001249 return next;
1250}
1251
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001252/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001253struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1254 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001255{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001256 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001257}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001258
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001259/* Find the next real entry, and increment the iterator to the next entry */
1260static void *find_next_entry_inc(struct trace_iterator *iter)
1261{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001262 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001263
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001264 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001265 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001266
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001267 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001268}
1269
Ingo Molnare309b412008-05-12 21:20:51 +02001270static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001271{
Steven Rostedtd7690412008-10-01 00:29:53 -04001272 /* Don't allow ftrace to trace into the ring buffers */
1273 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001274 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001275 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276}
1277
Ingo Molnare309b412008-05-12 21:20:51 +02001278static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001279{
1280 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001281 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001282 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283
1284 (*pos)++;
1285
1286 /* can't go backwards */
1287 if (iter->idx > i)
1288 return NULL;
1289
1290 if (iter->idx < 0)
1291 ent = find_next_entry_inc(iter);
1292 else
1293 ent = iter;
1294
1295 while (ent && iter->idx < i)
1296 ent = find_next_entry_inc(iter);
1297
1298 iter->pos = *pos;
1299
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001300 return ent;
1301}
1302
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001303/*
1304 * No necessary locking here. The worst thing which can
1305 * happen is loosing events consumed at the same time
1306 * by a trace_pipe reader.
1307 * Other than that, we don't risk to crash the ring buffer
1308 * because it serializes the readers.
1309 *
1310 * The current tracer is copied to avoid a global locking
1311 * all around.
1312 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001313static void *s_start(struct seq_file *m, loff_t *pos)
1314{
1315 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001316 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001317 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318 void *p = NULL;
1319 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001320 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001322 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001324 if (unlikely(old_tracer != current_trace && current_trace)) {
1325 old_tracer = current_trace;
1326 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001327 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001328 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001329
1330 atomic_inc(&trace_record_cmdline_disabled);
1331
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001332 if (*pos != iter->pos) {
1333 iter->ent = NULL;
1334 iter->cpu = 0;
1335 iter->idx = -1;
1336
Steven Rostedtd7690412008-10-01 00:29:53 -04001337 ftrace_disable_cpu();
1338
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001339 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1340 for_each_tracing_cpu(cpu)
1341 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1342 } else
1343 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1344
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001345
Steven Rostedtd7690412008-10-01 00:29:53 -04001346 ftrace_enable_cpu();
1347
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001348 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1349 ;
1350
1351 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001352 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353 p = s_next(m, p, &l);
1354 }
1355
1356 return p;
1357}
1358
1359static void s_stop(struct seq_file *m, void *p)
1360{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001361 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362}
1363
Ingo Molnare309b412008-05-12 21:20:51 +02001364static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001365{
Michael Ellermana6168352008-08-20 16:36:11 -07001366 seq_puts(m, "# _------=> CPU# \n");
1367 seq_puts(m, "# / _-----=> irqs-off \n");
1368 seq_puts(m, "# | / _----=> need-resched \n");
1369 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1370 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1371 seq_puts(m, "# |||| / \n");
1372 seq_puts(m, "# ||||| delay \n");
1373 seq_puts(m, "# cmd pid ||||| time | caller \n");
1374 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001375}
1376
Ingo Molnare309b412008-05-12 21:20:51 +02001377static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001378{
Michael Ellermana6168352008-08-20 16:36:11 -07001379 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1380 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001381}
1382
1383
Ingo Molnare309b412008-05-12 21:20:51 +02001384static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001385print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1386{
1387 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1388 struct trace_array *tr = iter->tr;
1389 struct trace_array_cpu *data = tr->data[tr->cpu];
1390 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001391 unsigned long total;
1392 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001393 const char *name = "preemption";
1394
1395 if (type)
1396 name = type->name;
1397
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001398 entries = ring_buffer_entries(iter->tr->buffer);
1399 total = entries +
1400 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001401
1402 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1403 name, UTS_RELEASE);
1404 seq_puts(m, "-----------------------------------"
1405 "---------------------------------\n");
1406 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1407 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001408 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001409 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001410 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001411 tr->cpu,
1412#if defined(CONFIG_PREEMPT_NONE)
1413 "server",
1414#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1415 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001416#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001417 "preempt",
1418#else
1419 "unknown",
1420#endif
1421 /* These are reserved for later use */
1422 0, 0, 0, 0);
1423#ifdef CONFIG_SMP
1424 seq_printf(m, " #P:%d)\n", num_online_cpus());
1425#else
1426 seq_puts(m, ")\n");
1427#endif
1428 seq_puts(m, " -----------------\n");
1429 seq_printf(m, " | task: %.16s-%d "
1430 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1431 data->comm, data->pid, data->uid, data->nice,
1432 data->policy, data->rt_priority);
1433 seq_puts(m, " -----------------\n");
1434
1435 if (data->critical_start) {
1436 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001437 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1438 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001439 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001440 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1441 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001442 seq_puts(m, "\n");
1443 }
1444
1445 seq_puts(m, "\n");
1446}
1447
Steven Rostedta3097202008-11-07 22:36:02 -05001448static void test_cpu_buff_start(struct trace_iterator *iter)
1449{
1450 struct trace_seq *s = &iter->seq;
1451
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001452 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1453 return;
1454
1455 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1456 return;
1457
Rusty Russell44623442009-01-01 10:12:23 +10301458 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001459 return;
1460
Rusty Russell44623442009-01-01 10:12:23 +10301461 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001462 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1463}
1464
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001465static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001466{
Steven Rostedt214023c2008-05-12 21:20:46 +02001467 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001468 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedtf633cef2008-12-23 23:24:13 -05001469 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001470 struct trace_entry *entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001471
Steven Rostedta3097202008-11-07 22:36:02 -05001472 test_cpu_buff_start(iter);
1473
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001474 event = ftrace_find_event(entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001475
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001476 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001477 if (!trace_print_lat_context(iter))
1478 goto partial;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001479 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001480
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001481 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001482 return event->latency_trace(iter, sym_flags);
Steven Rostedt7104f302008-10-01 10:52:51 -04001483
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001484 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1485 goto partial;
1486
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001487 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001488partial:
1489 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001490}
1491
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001492static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001493{
Steven Rostedt214023c2008-05-12 21:20:46 +02001494 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001495 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001496 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001497 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001498
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001499 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001500
Steven Rostedta3097202008-11-07 22:36:02 -05001501 test_cpu_buff_start(iter);
1502
Steven Rostedtf633cef2008-12-23 23:24:13 -05001503 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001504
1505 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001506 if (!trace_print_context(iter))
1507 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001508 }
1509
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001510 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001511 return event->trace(iter, sym_flags);
1512
1513 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1514 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001515
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001516 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001517partial:
1518 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001519}
1520
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001521static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001522{
1523 struct trace_seq *s = &iter->seq;
1524 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001525 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001526
1527 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001528
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001529 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001530 if (!trace_seq_printf(s, "%d %d %llu ",
1531 entry->pid, iter->cpu, iter->ts))
1532 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001533 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001534
Steven Rostedtf633cef2008-12-23 23:24:13 -05001535 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001536 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001537 return event->raw(iter, 0);
1538
1539 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1540 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001541
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001542 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001543partial:
1544 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001545}
1546
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001547static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001548{
1549 struct trace_seq *s = &iter->seq;
1550 unsigned char newline = '\n';
1551 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001552 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001553
1554 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001555
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001556 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1557 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1558 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1559 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1560 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001561
Steven Rostedtf633cef2008-12-23 23:24:13 -05001562 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001563 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001564 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001565 if (ret != TRACE_TYPE_HANDLED)
1566 return ret;
1567 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001568
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001569 SEQ_PUT_FIELD_RET(s, newline);
1570
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001571 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001572}
1573
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001574static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1575{
1576 struct trace_seq *s = &iter->seq;
1577 struct trace_entry *entry = iter->ent;
1578 struct print_entry *field;
1579 int ret;
1580
1581 trace_assign_type(field, entry);
1582
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001583 ret = trace_seq_printf(s, "%s", field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001584 if (!ret)
1585 return TRACE_TYPE_PARTIAL_LINE;
1586
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001587 return TRACE_TYPE_HANDLED;
1588}
1589
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001590static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001591{
1592 struct trace_seq *s = &iter->seq;
1593 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001594 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001595
1596 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001597
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001598 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1599 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001600 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001601 SEQ_PUT_FIELD_RET(s, iter->ts);
1602 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001603
Steven Rostedtf633cef2008-12-23 23:24:13 -05001604 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001605 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001606}
1607
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001608static int trace_empty(struct trace_iterator *iter)
1609{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001610 int cpu;
1611
Steven Rostedtab464282008-05-12 21:21:00 +02001612 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001613 if (iter->buffer_iter[cpu]) {
1614 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1615 return 0;
1616 } else {
1617 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1618 return 0;
1619 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001620 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001621
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001622 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001623}
1624
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001625static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001626{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001627 enum print_line_t ret;
1628
1629 if (iter->trace && iter->trace->print_line) {
1630 ret = iter->trace->print_line(iter);
1631 if (ret != TRACE_TYPE_UNHANDLED)
1632 return ret;
1633 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001634
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001635 if (iter->ent->type == TRACE_PRINT &&
1636 trace_flags & TRACE_ITER_PRINTK &&
1637 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1638 return print_printk_msg_only(iter);
1639
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001640 if (trace_flags & TRACE_ITER_BIN)
1641 return print_bin_fmt(iter);
1642
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001643 if (trace_flags & TRACE_ITER_HEX)
1644 return print_hex_fmt(iter);
1645
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001646 if (trace_flags & TRACE_ITER_RAW)
1647 return print_raw_fmt(iter);
1648
1649 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001650 return print_lat_fmt(iter);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001651
1652 return print_trace_fmt(iter);
1653}
1654
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001655static int s_show(struct seq_file *m, void *v)
1656{
1657 struct trace_iterator *iter = v;
1658
1659 if (iter->ent == NULL) {
1660 if (iter->tr) {
1661 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1662 seq_puts(m, "#\n");
1663 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001664 if (iter->trace && iter->trace->print_header)
1665 iter->trace->print_header(m);
1666 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001667 /* print nothing if the buffers are empty */
1668 if (trace_empty(iter))
1669 return 0;
1670 print_trace_header(m, iter);
1671 if (!(trace_flags & TRACE_ITER_VERBOSE))
1672 print_lat_help_header(m);
1673 } else {
1674 if (!(trace_flags & TRACE_ITER_VERBOSE))
1675 print_func_help_header(m);
1676 }
1677 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001678 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001679 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001680 }
1681
1682 return 0;
1683}
1684
1685static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001686 .start = s_start,
1687 .next = s_next,
1688 .stop = s_stop,
1689 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001690};
1691
Ingo Molnare309b412008-05-12 21:20:51 +02001692static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001693__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001694{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001695 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001696 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001697 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001698 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001699 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001700
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001701 if (tracing_disabled)
1702 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001703
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001704 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001705 if (!iter)
1706 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001707
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001708 /*
1709 * We make a copy of the current tracer to avoid concurrent
1710 * changes on it while we are reading.
1711 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001712 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001713 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001714 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001715 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001716
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001717 if (current_trace)
1718 *iter->trace = *current_trace;
1719
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001720 if (current_trace && current_trace->print_max)
1721 iter->tr = &max_tr;
1722 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001723 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001724 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001725 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001726 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001727
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001728 /* Notify the tracer early; before we stop tracing. */
1729 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001730 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001731
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001732 /* Annotate start of buffers if we had overruns */
1733 if (ring_buffer_overruns(iter->tr->buffer))
1734 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1735
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001736 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1737 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001738
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001739 iter->buffer_iter[cpu] =
1740 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001741
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001742 if (!iter->buffer_iter[cpu])
1743 goto fail_buffer;
1744 }
1745 } else {
1746 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001747 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001748 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001749
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001750 if (!iter->buffer_iter[cpu])
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001751 goto fail;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001752 }
1753
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001754 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001755 ret = seq_open(file, &tracer_seq_ops);
1756 if (ret < 0) {
1757 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001758 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001759 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001760
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001761 m = file->private_data;
1762 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001763
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001764 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001765 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001766
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001767 mutex_unlock(&trace_types_lock);
1768
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001769 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001770
1771 fail_buffer:
1772 for_each_tracing_cpu(cpu) {
1773 if (iter->buffer_iter[cpu])
1774 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1775 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001776 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001777 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001778 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001779 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001780
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001781 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001782}
1783
1784int tracing_open_generic(struct inode *inode, struct file *filp)
1785{
Steven Rostedt60a11772008-05-12 21:20:44 +02001786 if (tracing_disabled)
1787 return -ENODEV;
1788
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001789 filp->private_data = inode->i_private;
1790 return 0;
1791}
1792
Hannes Eder4fd27352009-02-10 19:44:12 +01001793static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001794{
1795 struct seq_file *m = (struct seq_file *)file->private_data;
1796 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001797 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001798
1799 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001800 for_each_tracing_cpu(cpu) {
1801 if (iter->buffer_iter[cpu])
1802 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1803 }
1804
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001805 if (iter->trace && iter->trace->close)
1806 iter->trace->close(iter);
1807
1808 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001809 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001810 mutex_unlock(&trace_types_lock);
1811
1812 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001813 mutex_destroy(&iter->mutex);
1814 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001815 kfree(iter);
1816 return 0;
1817}
1818
1819static int tracing_open(struct inode *inode, struct file *file)
1820{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001821 struct trace_iterator *iter;
1822 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001823
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001824 iter = __tracing_open(inode, file);
1825 if (IS_ERR(iter))
1826 ret = PTR_ERR(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001827
1828 return ret;
1829}
1830
1831static int tracing_lt_open(struct inode *inode, struct file *file)
1832{
1833 struct trace_iterator *iter;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001834 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001835
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001836 iter = __tracing_open(inode, file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001837
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001838 if (IS_ERR(iter))
1839 ret = PTR_ERR(iter);
1840 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001841 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1842
1843 return ret;
1844}
1845
1846
Ingo Molnare309b412008-05-12 21:20:51 +02001847static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001848t_next(struct seq_file *m, void *v, loff_t *pos)
1849{
1850 struct tracer *t = m->private;
1851
1852 (*pos)++;
1853
1854 if (t)
1855 t = t->next;
1856
1857 m->private = t;
1858
1859 return t;
1860}
1861
1862static void *t_start(struct seq_file *m, loff_t *pos)
1863{
1864 struct tracer *t = m->private;
1865 loff_t l = 0;
1866
1867 mutex_lock(&trace_types_lock);
1868 for (; t && l < *pos; t = t_next(m, t, &l))
1869 ;
1870
1871 return t;
1872}
1873
1874static void t_stop(struct seq_file *m, void *p)
1875{
1876 mutex_unlock(&trace_types_lock);
1877}
1878
1879static int t_show(struct seq_file *m, void *v)
1880{
1881 struct tracer *t = v;
1882
1883 if (!t)
1884 return 0;
1885
1886 seq_printf(m, "%s", t->name);
1887 if (t->next)
1888 seq_putc(m, ' ');
1889 else
1890 seq_putc(m, '\n');
1891
1892 return 0;
1893}
1894
1895static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001896 .start = t_start,
1897 .next = t_next,
1898 .stop = t_stop,
1899 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001900};
1901
1902static int show_traces_open(struct inode *inode, struct file *file)
1903{
1904 int ret;
1905
Steven Rostedt60a11772008-05-12 21:20:44 +02001906 if (tracing_disabled)
1907 return -ENODEV;
1908
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001909 ret = seq_open(file, &show_traces_seq_ops);
1910 if (!ret) {
1911 struct seq_file *m = file->private_data;
1912 m->private = trace_types;
1913 }
1914
1915 return ret;
1916}
1917
1918static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001919 .open = tracing_open,
1920 .read = seq_read,
1921 .llseek = seq_lseek,
1922 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001923};
1924
1925static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001926 .open = tracing_lt_open,
1927 .read = seq_read,
1928 .llseek = seq_lseek,
1929 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001930};
1931
1932static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001933 .open = show_traces_open,
1934 .read = seq_read,
1935 .release = seq_release,
1936};
1937
Ingo Molnar36dfe922008-05-12 21:20:52 +02001938/*
1939 * Only trace on a CPU if the bitmask is set:
1940 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301941static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001942
1943/*
1944 * The tracer itself will not take this lock, but still we want
1945 * to provide a consistent cpumask to user-space:
1946 */
1947static DEFINE_MUTEX(tracing_cpumask_update_lock);
1948
1949/*
1950 * Temporary storage for the character representation of the
1951 * CPU bitmask (and one more byte for the newline):
1952 */
1953static char mask_str[NR_CPUS + 1];
1954
Ingo Molnarc7078de2008-05-12 21:20:52 +02001955static ssize_t
1956tracing_cpumask_read(struct file *filp, char __user *ubuf,
1957 size_t count, loff_t *ppos)
1958{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001959 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001960
1961 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001962
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301963 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001964 if (count - len < 2) {
1965 count = -EINVAL;
1966 goto out_err;
1967 }
1968 len += sprintf(mask_str + len, "\n");
1969 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1970
1971out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001972 mutex_unlock(&tracing_cpumask_update_lock);
1973
1974 return count;
1975}
1976
1977static ssize_t
1978tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1979 size_t count, loff_t *ppos)
1980{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001981 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301982 cpumask_var_t tracing_cpumask_new;
1983
1984 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1985 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001986
1987 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301988 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001989 if (err)
1990 goto err_unlock;
1991
Steven Rostedta5e25882008-12-02 15:34:05 -05001992 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02001993 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001994 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02001995 /*
1996 * Increase/decrease the disabled counter if we are
1997 * about to flip a bit in the cpumask:
1998 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301999 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2000 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002001 atomic_inc(&global_trace.data[cpu]->disabled);
2002 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302003 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2004 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002005 atomic_dec(&global_trace.data[cpu]->disabled);
2006 }
2007 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002008 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002009 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002010
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302011 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002012
Ingo Molnarc7078de2008-05-12 21:20:52 +02002013 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302014 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002015
Ingo Molnarc7078de2008-05-12 21:20:52 +02002016 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002017
2018err_unlock:
2019 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302020 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002021
2022 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002023}
2024
2025static struct file_operations tracing_cpumask_fops = {
2026 .open = tracing_open_generic,
2027 .read = tracing_cpumask_read,
2028 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002029};
2030
2031static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002032tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002033 size_t cnt, loff_t *ppos)
2034{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002035 struct tracer_opt *trace_opts;
2036 u32 tracer_flags;
2037 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002038 char *buf;
2039 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002040 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002041
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002042
Wenji Huangc3706f02009-02-10 01:03:18 -05002043 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002044 for (i = 0; trace_options[i]; i++) {
2045 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002046 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002047 }
2048
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002049 mutex_lock(&trace_types_lock);
2050 tracer_flags = current_trace->flags->val;
2051 trace_opts = current_trace->flags->opts;
2052
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002053 /*
2054 * Increase the size with names of options specific
2055 * of the current tracer.
2056 */
2057 for (i = 0; trace_opts[i].name; i++) {
2058 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002059 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002060 }
2061
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062 /* +2 for \n and \0 */
2063 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002064 if (!buf) {
2065 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002066 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002067 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002068
2069 for (i = 0; trace_options[i]; i++) {
2070 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002071 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002072 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002073 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002074 }
2075
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002076 for (i = 0; trace_opts[i].name; i++) {
2077 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002078 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002079 trace_opts[i].name);
2080 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002081 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002082 trace_opts[i].name);
2083 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002084 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002085
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002086 WARN_ON(r >= len + 2);
2087
Ingo Molnar36dfe922008-05-12 21:20:52 +02002088 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002089
2090 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002091 return r;
2092}
2093
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002094/* Try to assign a tracer specific option */
2095static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2096{
2097 struct tracer_flags *trace_flags = trace->flags;
2098 struct tracer_opt *opts = NULL;
2099 int ret = 0, i = 0;
2100 int len;
2101
2102 for (i = 0; trace_flags->opts[i].name; i++) {
2103 opts = &trace_flags->opts[i];
2104 len = strlen(opts->name);
2105
2106 if (strncmp(cmp, opts->name, len) == 0) {
2107 ret = trace->set_flag(trace_flags->val,
2108 opts->bit, !neg);
2109 break;
2110 }
2111 }
2112 /* Not found */
2113 if (!trace_flags->opts[i].name)
2114 return -EINVAL;
2115
2116 /* Refused to handle */
2117 if (ret)
2118 return ret;
2119
2120 if (neg)
2121 trace_flags->val &= ~opts->bit;
2122 else
2123 trace_flags->val |= opts->bit;
2124
2125 return 0;
2126}
2127
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002128static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002129tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002130 size_t cnt, loff_t *ppos)
2131{
2132 char buf[64];
2133 char *cmp = buf;
2134 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002135 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002136 int i;
2137
Steven Rostedtcffae432008-05-12 21:21:00 +02002138 if (cnt >= sizeof(buf))
2139 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002140
2141 if (copy_from_user(&buf, ubuf, cnt))
2142 return -EFAULT;
2143
2144 buf[cnt] = 0;
2145
2146 if (strncmp(buf, "no", 2) == 0) {
2147 neg = 1;
2148 cmp += 2;
2149 }
2150
2151 for (i = 0; trace_options[i]; i++) {
2152 int len = strlen(trace_options[i]);
2153
2154 if (strncmp(cmp, trace_options[i], len) == 0) {
2155 if (neg)
2156 trace_flags &= ~(1 << i);
2157 else
2158 trace_flags |= (1 << i);
2159 break;
2160 }
2161 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002162
2163 /* If no option could be set, test the specific tracer options */
2164 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002165 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002166 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002167 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002168 if (ret)
2169 return ret;
2170 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002171
2172 filp->f_pos += cnt;
2173
2174 return cnt;
2175}
2176
2177static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002178 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002179 .read = tracing_trace_options_read,
2180 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002181};
2182
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002183static const char readme_msg[] =
2184 "tracing mini-HOWTO:\n\n"
2185 "# mkdir /debug\n"
2186 "# mount -t debugfs nodev /debug\n\n"
2187 "# cat /debug/tracing/available_tracers\n"
2188 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2189 "# cat /debug/tracing/current_tracer\n"
2190 "none\n"
2191 "# echo sched_switch > /debug/tracing/current_tracer\n"
2192 "# cat /debug/tracing/current_tracer\n"
2193 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002194 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002195 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002196 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002197 "# echo 1 > /debug/tracing/tracing_enabled\n"
2198 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2199 "echo 0 > /debug/tracing/tracing_enabled\n"
2200;
2201
2202static ssize_t
2203tracing_readme_read(struct file *filp, char __user *ubuf,
2204 size_t cnt, loff_t *ppos)
2205{
2206 return simple_read_from_buffer(ubuf, cnt, ppos,
2207 readme_msg, strlen(readme_msg));
2208}
2209
2210static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002211 .open = tracing_open_generic,
2212 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002213};
2214
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002215static ssize_t
2216tracing_ctrl_read(struct file *filp, char __user *ubuf,
2217 size_t cnt, loff_t *ppos)
2218{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002219 char buf[64];
2220 int r;
2221
Steven Rostedt90369902008-11-05 16:05:44 -05002222 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002223 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002224}
2225
2226static ssize_t
2227tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2228 size_t cnt, loff_t *ppos)
2229{
2230 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002231 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002232 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002233 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002234
Steven Rostedtcffae432008-05-12 21:21:00 +02002235 if (cnt >= sizeof(buf))
2236 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002237
2238 if (copy_from_user(&buf, ubuf, cnt))
2239 return -EFAULT;
2240
2241 buf[cnt] = 0;
2242
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002243 ret = strict_strtoul(buf, 10, &val);
2244 if (ret < 0)
2245 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002246
2247 val = !!val;
2248
2249 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002250 if (tracer_enabled ^ val) {
2251 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002252 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002253 if (current_trace->start)
2254 current_trace->start(tr);
2255 tracing_start();
2256 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002257 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002258 tracing_stop();
2259 if (current_trace->stop)
2260 current_trace->stop(tr);
2261 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002262 }
2263 mutex_unlock(&trace_types_lock);
2264
2265 filp->f_pos += cnt;
2266
2267 return cnt;
2268}
2269
2270static ssize_t
2271tracing_set_trace_read(struct file *filp, char __user *ubuf,
2272 size_t cnt, loff_t *ppos)
2273{
2274 char buf[max_tracer_type_len+2];
2275 int r;
2276
2277 mutex_lock(&trace_types_lock);
2278 if (current_trace)
2279 r = sprintf(buf, "%s\n", current_trace->name);
2280 else
2281 r = sprintf(buf, "\n");
2282 mutex_unlock(&trace_types_lock);
2283
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002284 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002285}
2286
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002287int tracer_init(struct tracer *t, struct trace_array *tr)
2288{
2289 tracing_reset_online_cpus(tr);
2290 return t->init(tr);
2291}
2292
Steven Rostedt577b7852009-02-26 23:43:05 -05002293struct trace_option_dentry;
2294
2295static struct trace_option_dentry *
2296create_trace_option_files(struct tracer *tracer);
2297
2298static void
2299destroy_trace_option_files(struct trace_option_dentry *topts);
2300
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002301static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002302{
Steven Rostedt577b7852009-02-26 23:43:05 -05002303 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002304 struct trace_array *tr = &global_trace;
2305 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002306 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002307
2308 mutex_lock(&trace_types_lock);
2309 for (t = trace_types; t; t = t->next) {
2310 if (strcmp(t->name, buf) == 0)
2311 break;
2312 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002313 if (!t) {
2314 ret = -EINVAL;
2315 goto out;
2316 }
2317 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002318 goto out;
2319
Steven Rostedt9f029e82008-11-12 15:24:24 -05002320 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002321 if (current_trace && current_trace->reset)
2322 current_trace->reset(tr);
2323
Steven Rostedt577b7852009-02-26 23:43:05 -05002324 destroy_trace_option_files(topts);
2325
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002326 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002327
2328 topts = create_trace_option_files(current_trace);
2329
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002330 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002331 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002332 if (ret)
2333 goto out;
2334 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002335
Steven Rostedt9f029e82008-11-12 15:24:24 -05002336 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002337 out:
2338 mutex_unlock(&trace_types_lock);
2339
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002340 return ret;
2341}
2342
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002343static ssize_t
2344tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2346{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002347 char buf[max_tracer_type_len+1];
2348 int i;
2349 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002350 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002351
Steven Rostedt60063a62008-10-28 10:44:24 -04002352 ret = cnt;
2353
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002354 if (cnt > max_tracer_type_len)
2355 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356
2357 if (copy_from_user(&buf, ubuf, cnt))
2358 return -EFAULT;
2359
2360 buf[cnt] = 0;
2361
2362 /* strip ending whitespace. */
2363 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2364 buf[i] = 0;
2365
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002366 err = tracing_set_tracer(buf);
2367 if (err)
2368 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002369
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002370 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002371
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002372 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002373}
2374
2375static ssize_t
2376tracing_max_lat_read(struct file *filp, char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2378{
2379 unsigned long *ptr = filp->private_data;
2380 char buf[64];
2381 int r;
2382
Steven Rostedtcffae432008-05-12 21:21:00 +02002383 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002384 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002385 if (r > sizeof(buf))
2386 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002387 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002388}
2389
2390static ssize_t
2391tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2392 size_t cnt, loff_t *ppos)
2393{
Hannes Eder5e398412009-02-10 19:44:34 +01002394 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002395 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002396 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002397 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002398
Steven Rostedtcffae432008-05-12 21:21:00 +02002399 if (cnt >= sizeof(buf))
2400 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002401
2402 if (copy_from_user(&buf, ubuf, cnt))
2403 return -EFAULT;
2404
2405 buf[cnt] = 0;
2406
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002407 ret = strict_strtoul(buf, 10, &val);
2408 if (ret < 0)
2409 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002410
2411 *ptr = val * 1000;
2412
2413 return cnt;
2414}
2415
Steven Rostedtb3806b42008-05-12 21:20:46 +02002416static int tracing_open_pipe(struct inode *inode, struct file *filp)
2417{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002418 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002419 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002420 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002421
2422 if (tracing_disabled)
2423 return -ENODEV;
2424
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002425 mutex_lock(&trace_types_lock);
2426
2427 /* We only allow one reader per cpu */
2428 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2429 if (!cpumask_empty(tracing_reader_cpumask)) {
2430 ret = -EBUSY;
2431 goto out;
2432 }
2433 cpumask_setall(tracing_reader_cpumask);
2434 } else {
2435 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2436 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2437 else {
2438 ret = -EBUSY;
2439 goto out;
2440 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002441 }
2442
2443 /* create a buffer to store the information to pass to userspace */
2444 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002445 if (!iter) {
2446 ret = -ENOMEM;
2447 goto out;
2448 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002449
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002450 /*
2451 * We make a copy of the current tracer to avoid concurrent
2452 * changes on it while we are reading.
2453 */
2454 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2455 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002456 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002457 goto fail;
2458 }
2459 if (current_trace)
2460 *iter->trace = *current_trace;
2461
2462 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2463 ret = -ENOMEM;
2464 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302465 }
2466
Steven Rostedta3097202008-11-07 22:36:02 -05002467 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302468 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002469
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002470 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002471 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002472 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002473 filp->private_data = iter;
2474
Steven Rostedt107bad82008-05-12 21:21:01 +02002475 if (iter->trace->pipe_open)
2476 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002477
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002478out:
2479 mutex_unlock(&trace_types_lock);
2480 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002481
2482fail:
2483 kfree(iter->trace);
2484 kfree(iter);
2485 mutex_unlock(&trace_types_lock);
2486 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002487}
2488
2489static int tracing_release_pipe(struct inode *inode, struct file *file)
2490{
2491 struct trace_iterator *iter = file->private_data;
2492
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002493 mutex_lock(&trace_types_lock);
2494
2495 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2496 cpumask_clear(tracing_reader_cpumask);
2497 else
2498 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2499
2500 mutex_unlock(&trace_types_lock);
2501
Rusty Russell44623442009-01-01 10:12:23 +10302502 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002503 mutex_destroy(&iter->mutex);
2504 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002505 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002506
2507 return 0;
2508}
2509
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002510static unsigned int
2511tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2512{
2513 struct trace_iterator *iter = filp->private_data;
2514
2515 if (trace_flags & TRACE_ITER_BLOCK) {
2516 /*
2517 * Always select as readable when in blocking mode
2518 */
2519 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002520 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002521 if (!trace_empty(iter))
2522 return POLLIN | POLLRDNORM;
2523 poll_wait(filp, &trace_wait, poll_table);
2524 if (!trace_empty(iter))
2525 return POLLIN | POLLRDNORM;
2526
2527 return 0;
2528 }
2529}
2530
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002531
2532void default_wait_pipe(struct trace_iterator *iter)
2533{
2534 DEFINE_WAIT(wait);
2535
2536 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2537
2538 if (trace_empty(iter))
2539 schedule();
2540
2541 finish_wait(&trace_wait, &wait);
2542}
2543
2544/*
2545 * This is a make-shift waitqueue.
2546 * A tracer might use this callback on some rare cases:
2547 *
2548 * 1) the current tracer might hold the runqueue lock when it wakes up
2549 * a reader, hence a deadlock (sched, function, and function graph tracers)
2550 * 2) the function tracers, trace all functions, we don't want
2551 * the overhead of calling wake_up and friends
2552 * (and tracing them too)
2553 *
2554 * Anyway, this is really very primitive wakeup.
2555 */
2556void poll_wait_pipe(struct trace_iterator *iter)
2557{
2558 set_current_state(TASK_INTERRUPTIBLE);
2559 /* sleep for 100 msecs, and try again. */
2560 schedule_timeout(HZ / 10);
2561}
2562
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002563/* Must be called with trace_types_lock mutex held. */
2564static int tracing_wait_pipe(struct file *filp)
2565{
2566 struct trace_iterator *iter = filp->private_data;
2567
2568 while (trace_empty(iter)) {
2569
2570 if ((filp->f_flags & O_NONBLOCK)) {
2571 return -EAGAIN;
2572 }
2573
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002574 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002575
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002576 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002577
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002578 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002579
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002580 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002581 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002582
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002583 /*
2584 * We block until we read something and tracing is disabled.
2585 * We still block if tracing is disabled, but we have never
2586 * read anything. This allows a user to cat this file, and
2587 * then enable tracing. But after we have read something,
2588 * we give an EOF when tracing is again disabled.
2589 *
2590 * iter->pos will be 0 if we haven't read anything.
2591 */
2592 if (!tracer_enabled && iter->pos)
2593 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002594 }
2595
2596 return 1;
2597}
2598
Steven Rostedtb3806b42008-05-12 21:20:46 +02002599/*
2600 * Consumer reader.
2601 */
2602static ssize_t
2603tracing_read_pipe(struct file *filp, char __user *ubuf,
2604 size_t cnt, loff_t *ppos)
2605{
2606 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002607 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002608 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002609
2610 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002611 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2612 if (sret != -EBUSY)
2613 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002614
Steven Rostedtf9520752009-03-02 14:04:40 -05002615 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002616
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002617 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002618 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002619 if (unlikely(old_tracer != current_trace && current_trace)) {
2620 old_tracer = current_trace;
2621 *iter->trace = *current_trace;
2622 }
2623 mutex_unlock(&trace_types_lock);
2624
2625 /*
2626 * Avoid more than one consumer on a single file descriptor
2627 * This is just a matter of traces coherency, the ring buffer itself
2628 * is protected.
2629 */
2630 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002631 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002632 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2633 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002634 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002635 }
2636
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002637waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002638 sret = tracing_wait_pipe(filp);
2639 if (sret <= 0)
2640 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002641
2642 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002643 if (trace_empty(iter)) {
2644 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002645 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002646 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002647
2648 if (cnt >= PAGE_SIZE)
2649 cnt = PAGE_SIZE - 1;
2650
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002651 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002652 memset(&iter->seq, 0,
2653 sizeof(struct trace_iterator) -
2654 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002655 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002656
Steven Rostedt088b1e422008-05-12 21:20:48 +02002657 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002658 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002659 int len = iter->seq.len;
2660
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002661 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002662 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002663 /* don't print partial lines */
2664 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002665 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002666 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002667 if (ret != TRACE_TYPE_NO_CONSUME)
2668 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002669
2670 if (iter->seq.len >= cnt)
2671 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002672 }
2673
Steven Rostedtb3806b42008-05-12 21:20:46 +02002674 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002675 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2676 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002677 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002678
2679 /*
2680 * If there was nothing to send to user, inspite of consuming trace
2681 * entries, go back to wait for more entries.
2682 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002683 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002684 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002685
Steven Rostedt107bad82008-05-12 21:21:01 +02002686out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002687 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002688
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002689 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002690}
2691
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002692static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2693 struct pipe_buffer *buf)
2694{
2695 __free_page(buf->page);
2696}
2697
2698static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2699 unsigned int idx)
2700{
2701 __free_page(spd->pages[idx]);
2702}
2703
2704static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002705 .can_merge = 0,
2706 .map = generic_pipe_buf_map,
2707 .unmap = generic_pipe_buf_unmap,
2708 .confirm = generic_pipe_buf_confirm,
2709 .release = tracing_pipe_buf_release,
2710 .steal = generic_pipe_buf_steal,
2711 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002712};
2713
Steven Rostedt34cd4992009-02-09 12:06:29 -05002714static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002715tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002716{
2717 size_t count;
2718 int ret;
2719
2720 /* Seq buffer is page-sized, exactly what we need. */
2721 for (;;) {
2722 count = iter->seq.len;
2723 ret = print_trace_line(iter);
2724 count = iter->seq.len - count;
2725 if (rem < count) {
2726 rem = 0;
2727 iter->seq.len -= count;
2728 break;
2729 }
2730 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2731 iter->seq.len -= count;
2732 break;
2733 }
2734
2735 trace_consume(iter);
2736 rem -= count;
2737 if (!find_next_entry_inc(iter)) {
2738 rem = 0;
2739 iter->ent = NULL;
2740 break;
2741 }
2742 }
2743
2744 return rem;
2745}
2746
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002747static ssize_t tracing_splice_read_pipe(struct file *filp,
2748 loff_t *ppos,
2749 struct pipe_inode_info *pipe,
2750 size_t len,
2751 unsigned int flags)
2752{
2753 struct page *pages[PIPE_BUFFERS];
2754 struct partial_page partial[PIPE_BUFFERS];
2755 struct trace_iterator *iter = filp->private_data;
2756 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002757 .pages = pages,
2758 .partial = partial,
2759 .nr_pages = 0, /* This gets updated below. */
2760 .flags = flags,
2761 .ops = &tracing_pipe_buf_ops,
2762 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002763 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002764 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002765 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002766 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002767 unsigned int i;
2768
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002769 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002770 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002771 if (unlikely(old_tracer != current_trace && current_trace)) {
2772 old_tracer = current_trace;
2773 *iter->trace = *current_trace;
2774 }
2775 mutex_unlock(&trace_types_lock);
2776
2777 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002778
2779 if (iter->trace->splice_read) {
2780 ret = iter->trace->splice_read(iter, filp,
2781 ppos, pipe, len, flags);
2782 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002783 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002784 }
2785
2786 ret = tracing_wait_pipe(filp);
2787 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002788 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002789
2790 if (!iter->ent && !find_next_entry_inc(iter)) {
2791 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002792 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002793 }
2794
2795 /* Fill as many pages as possible. */
2796 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2797 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05002798 if (!pages[i])
2799 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002800
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002801 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002802
2803 /* Copy the data into the page, so we can start over. */
2804 ret = trace_seq_to_buffer(&iter->seq,
2805 page_address(pages[i]),
2806 iter->seq.len);
2807 if (ret < 0) {
2808 __free_page(pages[i]);
2809 break;
2810 }
2811 partial[i].offset = 0;
2812 partial[i].len = iter->seq.len;
2813
Steven Rostedtf9520752009-03-02 14:04:40 -05002814 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002815 }
2816
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002817 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002818
2819 spd.nr_pages = i;
2820
2821 return splice_to_pipe(pipe, &spd);
2822
Steven Rostedt34cd4992009-02-09 12:06:29 -05002823out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002824 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002825
2826 return ret;
2827}
2828
Steven Rostedta98a3c32008-05-12 21:20:59 +02002829static ssize_t
2830tracing_entries_read(struct file *filp, char __user *ubuf,
2831 size_t cnt, loff_t *ppos)
2832{
2833 struct trace_array *tr = filp->private_data;
2834 char buf[64];
2835 int r;
2836
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002837 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002838 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2839}
2840
2841static ssize_t
2842tracing_entries_write(struct file *filp, const char __user *ubuf,
2843 size_t cnt, loff_t *ppos)
2844{
2845 unsigned long val;
2846 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002847 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002848
Steven Rostedtcffae432008-05-12 21:21:00 +02002849 if (cnt >= sizeof(buf))
2850 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002851
2852 if (copy_from_user(&buf, ubuf, cnt))
2853 return -EFAULT;
2854
2855 buf[cnt] = 0;
2856
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002857 ret = strict_strtoul(buf, 10, &val);
2858 if (ret < 0)
2859 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002860
2861 /* must have at least 1 entry */
2862 if (!val)
2863 return -EINVAL;
2864
2865 mutex_lock(&trace_types_lock);
2866
Steven Rostedtc76f0692008-11-07 22:36:02 -05002867 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002868
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002869 /* disable all cpu buffers */
2870 for_each_tracing_cpu(cpu) {
2871 if (global_trace.data[cpu])
2872 atomic_inc(&global_trace.data[cpu]->disabled);
2873 if (max_tr.data[cpu])
2874 atomic_inc(&max_tr.data[cpu]->disabled);
2875 }
2876
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002877 /* value is in KB */
2878 val <<= 10;
2879
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002880 if (val != global_trace.entries) {
2881 ret = ring_buffer_resize(global_trace.buffer, val);
2882 if (ret < 0) {
2883 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002884 goto out;
2885 }
2886
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002887 ret = ring_buffer_resize(max_tr.buffer, val);
2888 if (ret < 0) {
2889 int r;
2890 cnt = ret;
2891 r = ring_buffer_resize(global_trace.buffer,
2892 global_trace.entries);
2893 if (r < 0) {
2894 /* AARGH! We are left with different
2895 * size max buffer!!!! */
2896 WARN_ON(1);
2897 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002898 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002899 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002900 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002901
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002902 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002903 }
2904
2905 filp->f_pos += cnt;
2906
Steven Rostedt19384c02008-05-22 00:22:16 -04002907 /* If check pages failed, return ENOMEM */
2908 if (tracing_disabled)
2909 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002910 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002911 for_each_tracing_cpu(cpu) {
2912 if (global_trace.data[cpu])
2913 atomic_dec(&global_trace.data[cpu]->disabled);
2914 if (max_tr.data[cpu])
2915 atomic_dec(&max_tr.data[cpu]->disabled);
2916 }
2917
Steven Rostedtc76f0692008-11-07 22:36:02 -05002918 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002919 max_tr.entries = global_trace.entries;
2920 mutex_unlock(&trace_types_lock);
2921
2922 return cnt;
2923}
2924
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002925static int mark_printk(const char *fmt, ...)
2926{
2927 int ret;
2928 va_list args;
2929 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002930 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002931 va_end(args);
2932 return ret;
2933}
2934
2935static ssize_t
2936tracing_mark_write(struct file *filp, const char __user *ubuf,
2937 size_t cnt, loff_t *fpos)
2938{
2939 char *buf;
2940 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002941
Steven Rostedtc76f0692008-11-07 22:36:02 -05002942 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002943 return -EINVAL;
2944
2945 if (cnt > TRACE_BUF_SIZE)
2946 cnt = TRACE_BUF_SIZE;
2947
2948 buf = kmalloc(cnt + 1, GFP_KERNEL);
2949 if (buf == NULL)
2950 return -ENOMEM;
2951
2952 if (copy_from_user(buf, ubuf, cnt)) {
2953 kfree(buf);
2954 return -EFAULT;
2955 }
2956
2957 /* Cut from the first nil or newline. */
2958 buf[cnt] = '\0';
2959 end = strchr(buf, '\n');
2960 if (end)
2961 *end = '\0';
2962
2963 cnt = mark_printk("%s\n", buf);
2964 kfree(buf);
2965 *fpos += cnt;
2966
2967 return cnt;
2968}
2969
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002970static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002971 .open = tracing_open_generic,
2972 .read = tracing_max_lat_read,
2973 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002974};
2975
2976static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002977 .open = tracing_open_generic,
2978 .read = tracing_ctrl_read,
2979 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002980};
2981
2982static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002983 .open = tracing_open_generic,
2984 .read = tracing_set_trace_read,
2985 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002986};
2987
Steven Rostedtb3806b42008-05-12 21:20:46 +02002988static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002989 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002990 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002991 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002992 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002993 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002994};
2995
Steven Rostedta98a3c32008-05-12 21:20:59 +02002996static struct file_operations tracing_entries_fops = {
2997 .open = tracing_open_generic,
2998 .read = tracing_entries_read,
2999 .write = tracing_entries_write,
3000};
3001
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003002static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003003 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003004 .write = tracing_mark_write,
3005};
3006
Steven Rostedt2cadf912008-12-01 22:20:19 -05003007struct ftrace_buffer_info {
3008 struct trace_array *tr;
3009 void *spare;
3010 int cpu;
3011 unsigned int read;
3012};
3013
3014static int tracing_buffers_open(struct inode *inode, struct file *filp)
3015{
3016 int cpu = (int)(long)inode->i_private;
3017 struct ftrace_buffer_info *info;
3018
3019 if (tracing_disabled)
3020 return -ENODEV;
3021
3022 info = kzalloc(sizeof(*info), GFP_KERNEL);
3023 if (!info)
3024 return -ENOMEM;
3025
3026 info->tr = &global_trace;
3027 info->cpu = cpu;
3028 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3029 /* Force reading ring buffer for first read */
3030 info->read = (unsigned int)-1;
3031 if (!info->spare)
3032 goto out;
3033
3034 filp->private_data = info;
3035
3036 return 0;
3037
3038 out:
3039 kfree(info);
3040 return -ENOMEM;
3041}
3042
3043static ssize_t
3044tracing_buffers_read(struct file *filp, char __user *ubuf,
3045 size_t count, loff_t *ppos)
3046{
3047 struct ftrace_buffer_info *info = filp->private_data;
3048 unsigned int pos;
3049 ssize_t ret;
3050 size_t size;
3051
3052 /* Do we have previous read data to read? */
3053 if (info->read < PAGE_SIZE)
3054 goto read;
3055
3056 info->read = 0;
3057
3058 ret = ring_buffer_read_page(info->tr->buffer,
3059 &info->spare,
3060 count,
3061 info->cpu, 0);
3062 if (ret < 0)
3063 return 0;
3064
3065 pos = ring_buffer_page_len(info->spare);
3066
3067 if (pos < PAGE_SIZE)
3068 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3069
3070read:
3071 size = PAGE_SIZE - info->read;
3072 if (size > count)
3073 size = count;
3074
3075 ret = copy_to_user(ubuf, info->spare + info->read, size);
3076 if (ret)
3077 return -EFAULT;
3078 *ppos += size;
3079 info->read += size;
3080
3081 return size;
3082}
3083
3084static int tracing_buffers_release(struct inode *inode, struct file *file)
3085{
3086 struct ftrace_buffer_info *info = file->private_data;
3087
3088 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3089 kfree(info);
3090
3091 return 0;
3092}
3093
3094struct buffer_ref {
3095 struct ring_buffer *buffer;
3096 void *page;
3097 int ref;
3098};
3099
3100static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3101 struct pipe_buffer *buf)
3102{
3103 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3104
3105 if (--ref->ref)
3106 return;
3107
3108 ring_buffer_free_read_page(ref->buffer, ref->page);
3109 kfree(ref);
3110 buf->private = 0;
3111}
3112
3113static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3114 struct pipe_buffer *buf)
3115{
3116 return 1;
3117}
3118
3119static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3120 struct pipe_buffer *buf)
3121{
3122 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3123
3124 ref->ref++;
3125}
3126
3127/* Pipe buffer operations for a buffer. */
3128static struct pipe_buf_operations buffer_pipe_buf_ops = {
3129 .can_merge = 0,
3130 .map = generic_pipe_buf_map,
3131 .unmap = generic_pipe_buf_unmap,
3132 .confirm = generic_pipe_buf_confirm,
3133 .release = buffer_pipe_buf_release,
3134 .steal = buffer_pipe_buf_steal,
3135 .get = buffer_pipe_buf_get,
3136};
3137
3138/*
3139 * Callback from splice_to_pipe(), if we need to release some pages
3140 * at the end of the spd in case we error'ed out in filling the pipe.
3141 */
3142static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3143{
3144 struct buffer_ref *ref =
3145 (struct buffer_ref *)spd->partial[i].private;
3146
3147 if (--ref->ref)
3148 return;
3149
3150 ring_buffer_free_read_page(ref->buffer, ref->page);
3151 kfree(ref);
3152 spd->partial[i].private = 0;
3153}
3154
3155static ssize_t
3156tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3157 struct pipe_inode_info *pipe, size_t len,
3158 unsigned int flags)
3159{
3160 struct ftrace_buffer_info *info = file->private_data;
3161 struct partial_page partial[PIPE_BUFFERS];
3162 struct page *pages[PIPE_BUFFERS];
3163 struct splice_pipe_desc spd = {
3164 .pages = pages,
3165 .partial = partial,
3166 .flags = flags,
3167 .ops = &buffer_pipe_buf_ops,
3168 .spd_release = buffer_spd_release,
3169 };
3170 struct buffer_ref *ref;
3171 int size, i;
3172 size_t ret;
3173
3174 /*
3175 * We can't seek on a buffer input
3176 */
3177 if (unlikely(*ppos))
3178 return -ESPIPE;
3179
3180
3181 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3182 struct page *page;
3183 int r;
3184
3185 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3186 if (!ref)
3187 break;
3188
3189 ref->buffer = info->tr->buffer;
3190 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3191 if (!ref->page) {
3192 kfree(ref);
3193 break;
3194 }
3195
3196 r = ring_buffer_read_page(ref->buffer, &ref->page,
3197 len, info->cpu, 0);
3198 if (r < 0) {
3199 ring_buffer_free_read_page(ref->buffer,
3200 ref->page);
3201 kfree(ref);
3202 break;
3203 }
3204
3205 /*
3206 * zero out any left over data, this is going to
3207 * user land.
3208 */
3209 size = ring_buffer_page_len(ref->page);
3210 if (size < PAGE_SIZE)
3211 memset(ref->page + size, 0, PAGE_SIZE - size);
3212
3213 page = virt_to_page(ref->page);
3214
3215 spd.pages[i] = page;
3216 spd.partial[i].len = PAGE_SIZE;
3217 spd.partial[i].offset = 0;
3218 spd.partial[i].private = (unsigned long)ref;
3219 spd.nr_pages++;
3220 }
3221
3222 spd.nr_pages = i;
3223
3224 /* did we read anything? */
3225 if (!spd.nr_pages) {
3226 if (flags & SPLICE_F_NONBLOCK)
3227 ret = -EAGAIN;
3228 else
3229 ret = 0;
3230 /* TODO: block */
3231 return ret;
3232 }
3233
3234 ret = splice_to_pipe(pipe, &spd);
3235
3236 return ret;
3237}
3238
3239static const struct file_operations tracing_buffers_fops = {
3240 .open = tracing_buffers_open,
3241 .read = tracing_buffers_read,
3242 .release = tracing_buffers_release,
3243 .splice_read = tracing_buffers_splice_read,
3244 .llseek = no_llseek,
3245};
3246
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003247#ifdef CONFIG_DYNAMIC_FTRACE
3248
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003249int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003250{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003251 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003252}
3253
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003254static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003255tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003256 size_t cnt, loff_t *ppos)
3257{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003258 static char ftrace_dyn_info_buffer[1024];
3259 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003260 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003261 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003262 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003263 int r;
3264
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003265 mutex_lock(&dyn_info_mutex);
3266 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003267
Steven Rostedta26a2a22008-10-31 00:03:22 -04003268 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003269 buf[r++] = '\n';
3270
3271 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3272
3273 mutex_unlock(&dyn_info_mutex);
3274
3275 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003276}
3277
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003278static struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003279 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003280 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003281};
3282#endif
3283
3284static struct dentry *d_tracer;
3285
3286struct dentry *tracing_init_dentry(void)
3287{
3288 static int once;
3289
3290 if (d_tracer)
3291 return d_tracer;
3292
3293 d_tracer = debugfs_create_dir("tracing", NULL);
3294
3295 if (!d_tracer && !once) {
3296 once = 1;
3297 pr_warning("Could not create debugfs directory 'tracing'\n");
3298 return NULL;
3299 }
3300
3301 return d_tracer;
3302}
3303
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003304static struct dentry *d_percpu;
3305
3306struct dentry *tracing_dentry_percpu(void)
3307{
3308 static int once;
3309 struct dentry *d_tracer;
3310
3311 if (d_percpu)
3312 return d_percpu;
3313
3314 d_tracer = tracing_init_dentry();
3315
3316 if (!d_tracer)
3317 return NULL;
3318
3319 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3320
3321 if (!d_percpu && !once) {
3322 once = 1;
3323 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3324 return NULL;
3325 }
3326
3327 return d_percpu;
3328}
3329
3330static void tracing_init_debugfs_percpu(long cpu)
3331{
3332 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003333 struct dentry *entry, *d_cpu;
3334 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3335 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003336
3337 if (cpu > 999 || cpu < 0)
3338 return;
3339
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003340 sprintf(cpu_dir, "cpu%ld", cpu);
3341 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3342 if (!d_cpu) {
3343 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3344 return;
3345 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003346
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003347 /* per cpu trace_pipe */
3348 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003349 (void *) cpu, &tracing_pipe_fops);
3350 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003351 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003352
3353 /* per cpu trace */
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003354 entry = debugfs_create_file("trace", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003355 (void *) cpu, &tracing_fops);
3356 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003357 pr_warning("Could not create debugfs 'trace' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003358}
3359
Steven Rostedt60a11772008-05-12 21:20:44 +02003360#ifdef CONFIG_FTRACE_SELFTEST
3361/* Let selftest have access to static functions in this file */
3362#include "trace_selftest.c"
3363#endif
3364
Steven Rostedt577b7852009-02-26 23:43:05 -05003365struct trace_option_dentry {
3366 struct tracer_opt *opt;
3367 struct tracer_flags *flags;
3368 struct dentry *entry;
3369};
3370
3371static ssize_t
3372trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3373 loff_t *ppos)
3374{
3375 struct trace_option_dentry *topt = filp->private_data;
3376 char *buf;
3377
3378 if (topt->flags->val & topt->opt->bit)
3379 buf = "1\n";
3380 else
3381 buf = "0\n";
3382
3383 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3384}
3385
3386static ssize_t
3387trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3388 loff_t *ppos)
3389{
3390 struct trace_option_dentry *topt = filp->private_data;
3391 unsigned long val;
3392 char buf[64];
3393 int ret;
3394
3395 if (cnt >= sizeof(buf))
3396 return -EINVAL;
3397
3398 if (copy_from_user(&buf, ubuf, cnt))
3399 return -EFAULT;
3400
3401 buf[cnt] = 0;
3402
3403 ret = strict_strtoul(buf, 10, &val);
3404 if (ret < 0)
3405 return ret;
3406
3407 ret = 0;
3408 switch (val) {
3409 case 0:
3410 /* do nothing if already cleared */
3411 if (!(topt->flags->val & topt->opt->bit))
3412 break;
3413
3414 mutex_lock(&trace_types_lock);
3415 if (current_trace->set_flag)
3416 ret = current_trace->set_flag(topt->flags->val,
3417 topt->opt->bit, 0);
3418 mutex_unlock(&trace_types_lock);
3419 if (ret)
3420 return ret;
3421 topt->flags->val &= ~topt->opt->bit;
3422 break;
3423 case 1:
3424 /* do nothing if already set */
3425 if (topt->flags->val & topt->opt->bit)
3426 break;
3427
3428 mutex_lock(&trace_types_lock);
3429 if (current_trace->set_flag)
3430 ret = current_trace->set_flag(topt->flags->val,
3431 topt->opt->bit, 1);
3432 mutex_unlock(&trace_types_lock);
3433 if (ret)
3434 return ret;
3435 topt->flags->val |= topt->opt->bit;
3436 break;
3437
3438 default:
3439 return -EINVAL;
3440 }
3441
3442 *ppos += cnt;
3443
3444 return cnt;
3445}
3446
3447
3448static const struct file_operations trace_options_fops = {
3449 .open = tracing_open_generic,
3450 .read = trace_options_read,
3451 .write = trace_options_write,
3452};
3453
Steven Rostedta8259072009-02-26 22:19:12 -05003454static ssize_t
3455trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3456 loff_t *ppos)
3457{
3458 long index = (long)filp->private_data;
3459 char *buf;
3460
3461 if (trace_flags & (1 << index))
3462 buf = "1\n";
3463 else
3464 buf = "0\n";
3465
3466 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3467}
3468
3469static ssize_t
3470trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3471 loff_t *ppos)
3472{
3473 long index = (long)filp->private_data;
3474 char buf[64];
3475 unsigned long val;
3476 int ret;
3477
3478 if (cnt >= sizeof(buf))
3479 return -EINVAL;
3480
3481 if (copy_from_user(&buf, ubuf, cnt))
3482 return -EFAULT;
3483
3484 buf[cnt] = 0;
3485
3486 ret = strict_strtoul(buf, 10, &val);
3487 if (ret < 0)
3488 return ret;
3489
3490 switch (val) {
3491 case 0:
3492 trace_flags &= ~(1 << index);
3493 break;
3494 case 1:
3495 trace_flags |= 1 << index;
3496 break;
3497
3498 default:
3499 return -EINVAL;
3500 }
3501
3502 *ppos += cnt;
3503
3504 return cnt;
3505}
3506
Steven Rostedta8259072009-02-26 22:19:12 -05003507static const struct file_operations trace_options_core_fops = {
3508 .open = tracing_open_generic,
3509 .read = trace_options_core_read,
3510 .write = trace_options_core_write,
3511};
3512
3513static struct dentry *trace_options_init_dentry(void)
3514{
3515 struct dentry *d_tracer;
3516 static struct dentry *t_options;
3517
3518 if (t_options)
3519 return t_options;
3520
3521 d_tracer = tracing_init_dentry();
3522 if (!d_tracer)
3523 return NULL;
3524
3525 t_options = debugfs_create_dir("options", d_tracer);
3526 if (!t_options) {
3527 pr_warning("Could not create debugfs directory 'options'\n");
3528 return NULL;
3529 }
3530
3531 return t_options;
3532}
3533
Steven Rostedt577b7852009-02-26 23:43:05 -05003534static void
3535create_trace_option_file(struct trace_option_dentry *topt,
3536 struct tracer_flags *flags,
3537 struct tracer_opt *opt)
3538{
3539 struct dentry *t_options;
3540 struct dentry *entry;
3541
3542 t_options = trace_options_init_dentry();
3543 if (!t_options)
3544 return;
3545
3546 topt->flags = flags;
3547 topt->opt = opt;
3548
3549 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3550 &trace_options_fops);
3551
3552 topt->entry = entry;
3553
3554}
3555
3556static struct trace_option_dentry *
3557create_trace_option_files(struct tracer *tracer)
3558{
3559 struct trace_option_dentry *topts;
3560 struct tracer_flags *flags;
3561 struct tracer_opt *opts;
3562 int cnt;
3563
3564 if (!tracer)
3565 return NULL;
3566
3567 flags = tracer->flags;
3568
3569 if (!flags || !flags->opts)
3570 return NULL;
3571
3572 opts = flags->opts;
3573
3574 for (cnt = 0; opts[cnt].name; cnt++)
3575 ;
3576
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003577 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003578 if (!topts)
3579 return NULL;
3580
3581 for (cnt = 0; opts[cnt].name; cnt++)
3582 create_trace_option_file(&topts[cnt], flags,
3583 &opts[cnt]);
3584
3585 return topts;
3586}
3587
3588static void
3589destroy_trace_option_files(struct trace_option_dentry *topts)
3590{
3591 int cnt;
3592
3593 if (!topts)
3594 return;
3595
3596 for (cnt = 0; topts[cnt].opt; cnt++) {
3597 if (topts[cnt].entry)
3598 debugfs_remove(topts[cnt].entry);
3599 }
3600
3601 kfree(topts);
3602}
3603
Steven Rostedta8259072009-02-26 22:19:12 -05003604static struct dentry *
3605create_trace_option_core_file(const char *option, long index)
3606{
3607 struct dentry *t_options;
3608 struct dentry *entry;
3609
3610 t_options = trace_options_init_dentry();
3611 if (!t_options)
3612 return NULL;
3613
3614 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3615 &trace_options_core_fops);
3616
3617 return entry;
3618}
3619
3620static __init void create_trace_options_dir(void)
3621{
3622 struct dentry *t_options;
3623 struct dentry *entry;
3624 int i;
3625
3626 t_options = trace_options_init_dentry();
3627 if (!t_options)
3628 return;
3629
3630 for (i = 0; trace_options[i]; i++) {
3631 entry = create_trace_option_core_file(trace_options[i], i);
3632 if (!entry)
3633 pr_warning("Could not create debugfs %s entry\n",
3634 trace_options[i]);
3635 }
3636}
3637
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003638static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003639{
3640 struct dentry *d_tracer;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003641 struct dentry *buffers;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003642 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003643 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003644
3645 d_tracer = tracing_init_dentry();
3646
3647 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3648 &global_trace, &tracing_ctrl_fops);
3649 if (!entry)
3650 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3651
Steven Rostedtee6bce52008-11-12 17:52:37 -05003652 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003653 NULL, &tracing_iter_fops);
3654 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003655 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003656
Steven Rostedta8259072009-02-26 22:19:12 -05003657 create_trace_options_dir();
3658
Ingo Molnarc7078de2008-05-12 21:20:52 +02003659 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3660 NULL, &tracing_cpumask_fops);
3661 if (!entry)
3662 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3663
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003664 entry = debugfs_create_file("trace", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003665 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003666 if (!entry)
3667 pr_warning("Could not create debugfs 'trace' entry\n");
3668
3669 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3670 &global_trace, &show_traces_fops);
3671 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003672 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003673
3674 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3675 &global_trace, &set_tracer_fops);
3676 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003677 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003678
3679 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3680 &tracing_max_latency,
3681 &tracing_max_lat_fops);
3682 if (!entry)
3683 pr_warning("Could not create debugfs "
3684 "'tracing_max_latency' entry\n");
3685
3686 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3687 &tracing_thresh, &tracing_max_lat_fops);
3688 if (!entry)
3689 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003690 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003691 entry = debugfs_create_file("README", 0644, d_tracer,
3692 NULL, &tracing_readme_fops);
3693 if (!entry)
3694 pr_warning("Could not create debugfs 'README' entry\n");
3695
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003696 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3697 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003698 if (!entry)
3699 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003700 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003701
Steven Rostedta94c80e2008-11-12 17:52:36 -05003702 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003703 &global_trace, &tracing_entries_fops);
3704 if (!entry)
3705 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003706 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003707
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003708 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3709 NULL, &tracing_mark_fops);
3710 if (!entry)
3711 pr_warning("Could not create debugfs "
3712 "'trace_marker' entry\n");
3713
Steven Rostedt2cadf912008-12-01 22:20:19 -05003714 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3715
3716 if (!buffers)
3717 pr_warning("Could not create buffers directory\n");
3718 else {
3719 int cpu;
3720 char buf[64];
3721
3722 for_each_tracing_cpu(cpu) {
3723 sprintf(buf, "%d", cpu);
3724
3725 entry = debugfs_create_file(buf, 0444, buffers,
3726 (void *)(long)cpu,
3727 &tracing_buffers_fops);
3728 if (!entry)
3729 pr_warning("Could not create debugfs buffers "
3730 "'%s' entry\n", buf);
3731 }
3732 }
3733
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003734#ifdef CONFIG_DYNAMIC_FTRACE
3735 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3736 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003737 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003738 if (!entry)
3739 pr_warning("Could not create debugfs "
3740 "'dyn_ftrace_total_info' entry\n");
3741#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003742#ifdef CONFIG_SYSPROF_TRACER
3743 init_tracer_sysprof_debugfs(d_tracer);
3744#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003745
3746 for_each_tracing_cpu(cpu)
3747 tracing_init_debugfs_percpu(cpu);
3748
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003749 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003750}
3751
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003752int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003753{
Peter Zijlstraefed7922009-03-04 12:32:55 +01003754 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003755 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003756
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003757 struct ring_buffer_event *event;
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003758 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003759 struct trace_array_cpu *data;
Steven Rostedt38697052008-10-01 13:14:09 -04003760 int cpu, len = 0, size, pc;
Ingo Molnare726f5f2008-12-08 16:55:53 +01003761 struct print_entry *entry;
3762 unsigned long irq_flags;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003763
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01003764 if (tracing_disabled || tracing_selftest_running)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003765 return 0;
3766
Steven Rostedt38697052008-10-01 13:14:09 -04003767 pc = preempt_count();
3768 preempt_disable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003769 cpu = raw_smp_processor_id();
3770 data = tr->data[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003771
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -04003772 if (unlikely(atomic_read(&data->disabled)))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003773 goto out;
3774
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003775 pause_graph_tracing();
Peter Zijlstraefed7922009-03-04 12:32:55 +01003776 raw_local_irq_save(irq_flags);
3777 __raw_spin_lock(&trace_buf_lock);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003778 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003779
3780 len = min(len, TRACE_BUF_SIZE-1);
3781 trace_buf[len] = 0;
3782
Steven Rostedt777e2082008-09-29 23:02:42 -04003783 size = sizeof(*entry) + len + 1;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02003784 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003785 if (!event)
3786 goto out_unlock;
Steven Rostedt777e2082008-09-29 23:02:42 -04003787 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04003788 entry->ip = ip;
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003789 entry->depth = depth;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003790
Steven Rostedt777e2082008-09-29 23:02:42 -04003791 memcpy(&entry->buf, trace_buf, len);
3792 entry->buf[len] = 0;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02003793 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003794
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003795 out_unlock:
Peter Zijlstraefed7922009-03-04 12:32:55 +01003796 __raw_spin_unlock(&trace_buf_lock);
3797 raw_local_irq_restore(irq_flags);
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003798 unpause_graph_tracing();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003799 out:
Steven Rostedt38697052008-10-01 13:14:09 -04003800 preempt_enable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003801
3802 return len;
3803}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003804EXPORT_SYMBOL_GPL(trace_vprintk);
3805
3806int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3807{
3808 int ret;
3809 va_list ap;
3810
3811 if (!(trace_flags & TRACE_ITER_PRINTK))
3812 return 0;
3813
3814 va_start(ap, fmt);
Frederic Weisbecker21a8c462008-12-04 23:51:23 +01003815 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003816 va_end(ap);
3817 return ret;
3818}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003819EXPORT_SYMBOL_GPL(__ftrace_printk);
3820
Arnaldo Carvalho de Melo90112622009-01-23 12:06:23 -02003821int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3822{
3823 if (!(trace_flags & TRACE_ITER_PRINTK))
3824 return 0;
3825
3826 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3827}
3828EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3829
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003830static int trace_panic_handler(struct notifier_block *this,
3831 unsigned long event, void *unused)
3832{
Steven Rostedt944ac422008-10-23 19:26:08 -04003833 if (ftrace_dump_on_oops)
3834 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003835 return NOTIFY_OK;
3836}
3837
3838static struct notifier_block trace_panic_notifier = {
3839 .notifier_call = trace_panic_handler,
3840 .next = NULL,
3841 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3842};
3843
3844static int trace_die_handler(struct notifier_block *self,
3845 unsigned long val,
3846 void *data)
3847{
3848 switch (val) {
3849 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003850 if (ftrace_dump_on_oops)
3851 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003852 break;
3853 default:
3854 break;
3855 }
3856 return NOTIFY_OK;
3857}
3858
3859static struct notifier_block trace_die_notifier = {
3860 .notifier_call = trace_die_handler,
3861 .priority = 200
3862};
3863
3864/*
3865 * printk is set to max of 1024, we really don't need it that big.
3866 * Nothing should be printing 1000 characters anyway.
3867 */
3868#define TRACE_MAX_PRINT 1000
3869
3870/*
3871 * Define here KERN_TRACE so that we have one place to modify
3872 * it if we decide to change what log level the ftrace dump
3873 * should be at.
3874 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003875#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003876
3877static void
3878trace_printk_seq(struct trace_seq *s)
3879{
3880 /* Probably should print a warning here. */
3881 if (s->len >= 1000)
3882 s->len = 1000;
3883
3884 /* should be zero ended, but we are paranoid. */
3885 s->buffer[s->len] = 0;
3886
3887 printk(KERN_TRACE "%s", s->buffer);
3888
Steven Rostedtf9520752009-03-02 14:04:40 -05003889 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003890}
3891
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003892void ftrace_dump(void)
3893{
3894 static DEFINE_SPINLOCK(ftrace_dump_lock);
3895 /* use static because iter can be a bit big for the stack */
3896 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003897 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003898 unsigned long flags;
3899 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003900
3901 /* only one dump */
3902 spin_lock_irqsave(&ftrace_dump_lock, flags);
3903 if (dump_ran)
3904 goto out;
3905
3906 dump_ran = 1;
3907
3908 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05003909 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003910 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003911
Steven Rostedtd7690412008-10-01 00:29:53 -04003912 for_each_tracing_cpu(cpu) {
3913 atomic_inc(&global_trace.data[cpu]->disabled);
3914 }
3915
Török Edwinb54d3de2008-11-22 13:28:48 +02003916 /* don't look at user memory in panic mode */
3917 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3918
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003919 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3920
3921 iter.tr = &global_trace;
3922 iter.trace = current_trace;
3923
3924 /*
3925 * We need to stop all tracing on all CPUS to read the
3926 * the next buffer. This is a bit expensive, but is
3927 * not done often. We fill all what we can read,
3928 * and then release the locks again.
3929 */
3930
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003931 while (!trace_empty(&iter)) {
3932
3933 if (!cnt)
3934 printk(KERN_TRACE "---------------------------------\n");
3935
3936 cnt++;
3937
3938 /* reset all but tr, trace, and overruns */
3939 memset(&iter.seq, 0,
3940 sizeof(struct trace_iterator) -
3941 offsetof(struct trace_iterator, seq));
3942 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3943 iter.pos = -1;
3944
3945 if (find_next_entry_inc(&iter) != NULL) {
3946 print_trace_line(&iter);
3947 trace_consume(&iter);
3948 }
3949
3950 trace_printk_seq(&iter.seq);
3951 }
3952
3953 if (!cnt)
3954 printk(KERN_TRACE " (ftrace buffer empty)\n");
3955 else
3956 printk(KERN_TRACE "---------------------------------\n");
3957
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003958 out:
3959 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3960}
3961
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003962__init static int tracer_alloc_buffers(void)
3963{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003964 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003965 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303966 int ret = -ENOMEM;
3967
3968 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3969 goto out;
3970
3971 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3972 goto out_free_buffer_mask;
3973
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003974 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3975 goto out_free_tracing_cpumask;
3976
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303977 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3978 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003979 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003980
Steven Rostedtab464282008-05-12 21:21:00 +02003981 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003982 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3983 TRACE_BUFFER_FLAGS);
3984 if (!global_trace.buffer) {
3985 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3986 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303987 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003988 }
3989 global_trace.entries = ring_buffer_size(global_trace.buffer);
3990
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303991
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003992#ifdef CONFIG_TRACER_MAX_TRACE
3993 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
3994 TRACE_BUFFER_FLAGS);
3995 if (!max_tr.buffer) {
3996 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
3997 WARN_ON(1);
3998 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303999 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004000 }
4001 max_tr.entries = ring_buffer_size(max_tr.buffer);
4002 WARN_ON(max_tr.entries != global_trace.entries);
4003#endif
4004
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004005 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004006 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004007 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004008 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004009 }
4010
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004011 trace_init_cmdlines();
4012
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004013 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004014 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004015#ifdef CONFIG_BOOT_TRACER
4016 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004017#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004018 /* All seems OK, enable tracing */
4019 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004020
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004021 atomic_notifier_chain_register(&panic_notifier_list,
4022 &trace_panic_notifier);
4023
4024 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304025 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004026
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304027out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004028 free_cpumask_var(tracing_reader_cpumask);
4029out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304030 free_cpumask_var(tracing_cpumask);
4031out_free_buffer_mask:
4032 free_cpumask_var(tracing_buffer_mask);
4033out:
4034 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004035}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004036
4037__init static int clear_boot_tracer(void)
4038{
4039 /*
4040 * The default tracer at boot buffer is an init section.
4041 * This function is called in lateinit. If we did not
4042 * find the boot tracer, then clear it out, to prevent
4043 * later registration from accessing the buffer that is
4044 * about to be freed.
4045 */
4046 if (!default_bootup_tracer)
4047 return 0;
4048
4049 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4050 default_bootup_tracer);
4051 default_bootup_tracer = NULL;
4052
4053 return 0;
4054}
4055
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004056early_initcall(tracer_alloc_buffers);
4057fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004058late_initcall(clear_boot_tracer);