blob: 2e53e6f094403801b37fb543ec9055c03b77f339 [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
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500349 if (!cnt)
350 return 0;
351
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200352 if (s->len <= s->readpos)
353 return -EBUSY;
354
355 len = s->len - s->readpos;
356 if (cnt > len)
357 cnt = len;
358 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500359 if (ret == cnt)
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200360 return -EFAULT;
361
Steven Rostedt2dc5d122009-03-04 19:10:05 -0500362 cnt -= ret;
363
Steven Rostedte74da522009-03-04 20:31:11 -0500364 s->readpos += cnt;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200365 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200366}
367
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200368ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
369{
370 int len;
371 void *ret;
372
373 if (s->len <= s->readpos)
374 return -EBUSY;
375
376 len = s->len - s->readpos;
377 if (cnt > len)
378 cnt = len;
379 ret = memcpy(buf, s->buffer + s->readpos, cnt);
380 if (!ret)
381 return -EFAULT;
382
Steven Rostedte74da522009-03-04 20:31:11 -0500383 s->readpos += cnt;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +0200384 return cnt;
385}
386
Ingo Molnare309b412008-05-12 21:20:51 +0200387static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200388trace_print_seq(struct seq_file *m, struct trace_seq *s)
389{
390 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
391
392 s->buffer[len] = 0;
393 seq_puts(m, s->buffer);
394
Steven Rostedtf9520752009-03-02 14:04:40 -0500395 trace_seq_init(s);
Steven Rostedt214023c2008-05-12 21:20:46 +0200396}
397
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200398/**
399 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
400 * @tr: tracer
401 * @tsk: the task with the latency
402 * @cpu: The cpu that initiated the trace.
403 *
404 * Flip the buffers between the @tr and the max_tr and record information
405 * about which task was the cause of this latency.
406 */
Ingo Molnare309b412008-05-12 21:20:51 +0200407void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200408update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
409{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400410 struct ring_buffer *buf = tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200411
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200412 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200413 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400414
415 tr->buffer = max_tr.buffer;
416 max_tr.buffer = buf;
417
Steven Rostedtd7690412008-10-01 00:29:53 -0400418 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400419 ring_buffer_reset(tr->buffer);
Steven Rostedtd7690412008-10-01 00:29:53 -0400420 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200421
422 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200423 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200424}
425
426/**
427 * update_max_tr_single - only copy one trace over, and reset the rest
428 * @tr - tracer
429 * @tsk - task with the latency
430 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200431 *
432 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200433 */
Ingo Molnare309b412008-05-12 21:20:51 +0200434void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200435update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
436{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400437 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200438
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200439 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200440 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200441
Steven Rostedtd7690412008-10-01 00:29:53 -0400442 ftrace_disable_cpu();
443
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400444 ring_buffer_reset(max_tr.buffer);
445 ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
446
Steven Rostedtd7690412008-10-01 00:29:53 -0400447 ftrace_enable_cpu();
448
Steven Rostedt97b17ef2009-01-21 15:24:56 -0500449 WARN_ON_ONCE(ret && ret != -EAGAIN);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200450
451 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200452 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200453}
454
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200455/**
456 * register_tracer - register a tracer with the ftrace system.
457 * @type - the plugin for the tracer
458 *
459 * Register a new plugin tracer.
460 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200461int register_tracer(struct tracer *type)
Hannes Edere7669b82009-02-10 19:44:45 +0100462__releases(kernel_lock)
463__acquires(kernel_lock)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200464{
465 struct tracer *t;
466 int len;
467 int ret = 0;
468
469 if (!type->name) {
470 pr_info("Tracer must have a name\n");
471 return -1;
472 }
473
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100474 /*
475 * When this gets called we hold the BKL which means that
476 * preemption is disabled. Various trace selftests however
477 * need to disable and enable preemption for successful tests.
478 * So we drop the BKL here and grab it after the tests again.
479 */
480 unlock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200481 mutex_lock(&trace_types_lock);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100482
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100483 tracing_selftest_running = true;
484
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200485 for (t = trace_types; t; t = t->next) {
486 if (strcmp(type->name, t->name) == 0) {
487 /* already found */
488 pr_info("Trace %s already registered\n",
489 type->name);
490 ret = -1;
491 goto out;
492 }
493 }
494
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100495 if (!type->set_flag)
496 type->set_flag = &dummy_set_flag;
497 if (!type->flags)
498 type->flags = &dummy_tracer_flags;
499 else
500 if (!type->flags->opts)
501 type->flags->opts = dummy_tracer_opt;
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +0100502 if (!type->wait_pipe)
503 type->wait_pipe = default_wait_pipe;
504
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +0100505
Steven Rostedt60a11772008-05-12 21:20:44 +0200506#ifdef CONFIG_FTRACE_STARTUP_TEST
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500507 if (type->selftest && !tracing_selftest_disabled) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200508 struct tracer *saved_tracer = current_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200509 struct trace_array *tr = &global_trace;
Steven Rostedt60a11772008-05-12 21:20:44 +0200510 int i;
Frederic Weisbeckerff325042008-12-04 23:47:35 +0100511
Steven Rostedt60a11772008-05-12 21:20:44 +0200512 /*
513 * Run a selftest on this tracer.
514 * Here we reset the trace buffer, and set the current
515 * tracer to be this tracer. The tracer can then run some
516 * internal tracing to verify that everything is in order.
517 * If we fail, we do not register this tracer.
518 */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100519 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400520 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100521
Steven Rostedt60a11772008-05-12 21:20:44 +0200522 current_trace = type;
Steven Rostedt60a11772008-05-12 21:20:44 +0200523 /* the test is responsible for initializing and enabling */
524 pr_info("Testing tracer %s: ", type->name);
525 ret = type->selftest(type, tr);
526 /* the test is responsible for resetting too */
527 current_trace = saved_tracer;
Steven Rostedt60a11772008-05-12 21:20:44 +0200528 if (ret) {
529 printk(KERN_CONT "FAILED!\n");
530 goto out;
531 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200532 /* Only reset on passing, to avoid touching corrupted buffers */
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100533 for_each_tracing_cpu(i)
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400534 tracing_reset(tr, i);
Ingo Molnar86fa2f62008-11-19 10:00:15 +0100535
Steven Rostedt60a11772008-05-12 21:20:44 +0200536 printk(KERN_CONT "PASSED\n");
537 }
538#endif
539
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200540 type->next = trace_types;
541 trace_types = type;
542 len = strlen(type->name);
543 if (len > max_tracer_type_len)
544 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200545
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200546 out:
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +0100547 tracing_selftest_running = false;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200548 mutex_unlock(&trace_types_lock);
549
Steven Rostedtdac74942009-02-05 01:13:38 -0500550 if (ret || !default_bootup_tracer)
551 goto out_unlock;
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500552
Steven Rostedtdac74942009-02-05 01:13:38 -0500553 if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
554 goto out_unlock;
555
556 printk(KERN_INFO "Starting tracer '%s'\n", type->name);
557 /* Do we want this tracer to start on bootup? */
558 tracing_set_tracer(type->name);
559 default_bootup_tracer = NULL;
560 /* disable other selftests, since this will break it. */
561 tracing_selftest_disabled = 1;
562#ifdef CONFIG_FTRACE_STARTUP_TEST
563 printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
564 type->name);
565#endif
566
567 out_unlock:
Steven Rostedtb2821ae2009-02-02 21:38:32 -0500568 lock_kernel();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200569 return ret;
570}
571
572void unregister_tracer(struct tracer *type)
573{
574 struct tracer **t;
575 int len;
576
577 mutex_lock(&trace_types_lock);
578 for (t = &trace_types; *t; t = &(*t)->next) {
579 if (*t == type)
580 goto found;
581 }
582 pr_info("Trace %s not registered\n", type->name);
583 goto out;
584
585 found:
586 *t = (*t)->next;
Arnaldo Carvalho de Melob5db03c2009-02-07 18:52:59 -0200587
588 if (type == current_trace && tracer_enabled) {
589 tracer_enabled = 0;
590 tracing_stop();
591 if (current_trace->stop)
592 current_trace->stop(&global_trace);
593 current_trace = &nop_trace;
594 }
595
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200596 if (strlen(type->name) != max_tracer_type_len)
597 goto out;
598
599 max_tracer_type_len = 0;
600 for (t = &trace_types; *t; t = &(*t)->next) {
601 len = strlen((*t)->name);
602 if (len > max_tracer_type_len)
603 max_tracer_type_len = len;
604 }
605 out:
606 mutex_unlock(&trace_types_lock);
607}
608
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400609void tracing_reset(struct trace_array *tr, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200610{
Steven Rostedtd7690412008-10-01 00:29:53 -0400611 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400612 ring_buffer_reset_cpu(tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -0400613 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200614}
615
Pekka J Enberg213cc062008-12-19 12:08:39 +0200616void tracing_reset_online_cpus(struct trace_array *tr)
617{
618 int cpu;
619
620 tr->time_start = ftrace_now(tr->cpu);
621
622 for_each_online_cpu(cpu)
623 tracing_reset(tr, cpu);
624}
625
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200626#define SAVED_CMDLINES 128
627static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
628static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
629static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
630static int cmdline_idx;
Peter Zijlstraefed7922009-03-04 12:32:55 +0100631static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt25b0b442008-05-12 21:21:00 +0200632
Steven Rostedt25b0b442008-05-12 21:21:00 +0200633/* temporary disable recording */
Hannes Eder4fd27352009-02-10 19:44:12 +0100634static atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200635
636static void trace_init_cmdlines(void)
637{
638 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
639 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
640 cmdline_idx = 0;
641}
642
Steven Rostedt0f048702008-11-05 16:05:44 -0500643static int trace_stop_count;
644static DEFINE_SPINLOCK(tracing_start_lock);
645
646/**
Steven Rostedt69bb54e2008-11-21 12:59:38 -0500647 * ftrace_off_permanent - disable all ftrace code permanently
648 *
649 * This should only be called when a serious anomally has
650 * been detected. This will turn off the function tracing,
651 * ring buffers, and other tracing utilites. It takes no
652 * locks and can be called from any context.
653 */
654void ftrace_off_permanent(void)
655{
656 tracing_disabled = 1;
657 ftrace_stop();
658 tracing_off_permanent();
659}
660
661/**
Steven Rostedt0f048702008-11-05 16:05:44 -0500662 * tracing_start - quick start of the tracer
663 *
664 * If tracing is enabled but was stopped by tracing_stop,
665 * this will start the tracer back up.
666 */
667void tracing_start(void)
668{
669 struct ring_buffer *buffer;
670 unsigned long flags;
671
672 if (tracing_disabled)
673 return;
674
675 spin_lock_irqsave(&tracing_start_lock, flags);
Steven Rostedtb06a8302009-01-22 14:26:15 -0500676 if (--trace_stop_count) {
677 if (trace_stop_count < 0) {
678 /* Someone screwed up their debugging */
679 WARN_ON_ONCE(1);
680 trace_stop_count = 0;
681 }
Steven Rostedt0f048702008-11-05 16:05:44 -0500682 goto out;
683 }
684
685
686 buffer = global_trace.buffer;
687 if (buffer)
688 ring_buffer_record_enable(buffer);
689
690 buffer = max_tr.buffer;
691 if (buffer)
692 ring_buffer_record_enable(buffer);
693
694 ftrace_start();
695 out:
696 spin_unlock_irqrestore(&tracing_start_lock, flags);
697}
698
699/**
700 * tracing_stop - quick stop of the tracer
701 *
702 * Light weight way to stop tracing. Use in conjunction with
703 * tracing_start.
704 */
705void tracing_stop(void)
706{
707 struct ring_buffer *buffer;
708 unsigned long flags;
709
710 ftrace_stop();
711 spin_lock_irqsave(&tracing_start_lock, flags);
712 if (trace_stop_count++)
713 goto out;
714
715 buffer = global_trace.buffer;
716 if (buffer)
717 ring_buffer_record_disable(buffer);
718
719 buffer = max_tr.buffer;
720 if (buffer)
721 ring_buffer_record_disable(buffer);
722
723 out:
724 spin_unlock_irqrestore(&tracing_start_lock, flags);
725}
726
Ingo Molnare309b412008-05-12 21:20:51 +0200727void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200728
Ingo Molnare309b412008-05-12 21:20:51 +0200729static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730{
731 unsigned map;
732 unsigned idx;
733
734 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
735 return;
736
737 /*
738 * It's not the end of the world if we don't get
739 * the lock, but we also don't want to spin
740 * nor do we want to disable interrupts,
741 * so if we miss here, then better luck next time.
742 */
Peter Zijlstraefed7922009-03-04 12:32:55 +0100743 if (!__raw_spin_trylock(&trace_cmdline_lock))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200744 return;
745
746 idx = map_pid_to_cmdline[tsk->pid];
747 if (idx >= SAVED_CMDLINES) {
748 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
749
750 map = map_cmdline_to_pid[idx];
751 if (map <= PID_MAX_DEFAULT)
752 map_pid_to_cmdline[map] = (unsigned)-1;
753
754 map_pid_to_cmdline[tsk->pid] = idx;
755
756 cmdline_idx = idx;
757 }
758
759 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
760
Peter Zijlstraefed7922009-03-04 12:32:55 +0100761 __raw_spin_unlock(&trace_cmdline_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200762}
763
Steven Rostedt660c7f92008-11-26 00:16:26 -0500764char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200765{
766 char *cmdline = "<...>";
767 unsigned map;
768
769 if (!pid)
770 return "<idle>";
771
772 if (pid > PID_MAX_DEFAULT)
773 goto out;
774
775 map = map_pid_to_cmdline[pid];
776 if (map >= SAVED_CMDLINES)
777 goto out;
778
779 cmdline = saved_cmdlines[map];
780
781 out:
782 return cmdline;
783}
784
Ingo Molnare309b412008-05-12 21:20:51 +0200785void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200786{
787 if (atomic_read(&trace_record_cmdline_disabled))
788 return;
789
790 trace_save_cmdline(tsk);
791}
792
Pekka Paalanen45dcd8b2008-09-16 21:56:41 +0300793void
Steven Rostedt38697052008-10-01 13:14:09 -0400794tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
795 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200796{
797 struct task_struct *tsk = current;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200798
Steven Rostedt777e2082008-09-29 23:02:42 -0400799 entry->preempt_count = pc & 0xff;
800 entry->pid = (tsk) ? tsk->pid : 0;
Török Edwinb54d3de2008-11-22 13:28:48 +0200801 entry->tgid = (tsk) ? tsk->tgid : 0;
Steven Rostedt777e2082008-09-29 23:02:42 -0400802 entry->flags =
Steven Rostedt92444892008-10-24 09:42:59 -0400803#ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400804 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedt92444892008-10-24 09:42:59 -0400805#else
806 TRACE_FLAG_IRQS_NOSUPPORT |
807#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
809 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
810 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
811}
812
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200813struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
814 unsigned char type,
815 unsigned long len,
816 unsigned long flags, int pc)
817{
818 struct ring_buffer_event *event;
819
820 event = ring_buffer_lock_reserve(tr->buffer, len);
821 if (event != NULL) {
822 struct trace_entry *ent = ring_buffer_event_data(event);
823
824 tracing_generic_entry_update(ent, flags, pc);
825 ent->type = type;
826 }
827
828 return event;
829}
830static void ftrace_trace_stack(struct trace_array *tr,
831 unsigned long flags, int skip, int pc);
832static void ftrace_trace_userstack(struct trace_array *tr,
833 unsigned long flags, int pc);
834
835void trace_buffer_unlock_commit(struct trace_array *tr,
836 struct ring_buffer_event *event,
837 unsigned long flags, int pc)
838{
839 ring_buffer_unlock_commit(tr->buffer, event);
840
841 ftrace_trace_stack(tr, flags, 6, pc);
842 ftrace_trace_userstack(tr, flags, pc);
843 trace_wake_up();
844}
845
Steven Rostedtef5580d2009-02-27 19:38:04 -0500846struct ring_buffer_event *
847trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
848 unsigned long flags, int pc)
849{
850 return trace_buffer_lock_reserve(&global_trace,
851 type, len, flags, pc);
852}
853
854void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
855 unsigned long flags, int pc)
856{
857 return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
858}
859
Ingo Molnare309b412008-05-12 21:20:51 +0200860void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500861trace_function(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400862 unsigned long ip, unsigned long parent_ip, unsigned long flags,
863 int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200864{
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400865 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400866 struct ftrace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200867
Steven Rostedtd7690412008-10-01 00:29:53 -0400868 /* If we are reading the ring buffer, don't trace */
869 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
870 return;
871
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200872 event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
873 flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400874 if (!event)
875 return;
876 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400877 entry->ip = ip;
878 entry->parent_ip = parent_ip;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200879 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200880}
881
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100882#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100883static void __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100884 struct ftrace_graph_ent *trace,
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100885 unsigned long flags,
886 int pc)
887{
888 struct ring_buffer_event *event;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100889 struct ftrace_graph_ent_entry *entry;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100890
891 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
892 return;
893
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200894 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
895 sizeof(*entry), flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100896 if (!event)
897 return;
898 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100899 entry->graph_ent = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200900 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100901}
902
903static void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100904 struct ftrace_graph_ret *trace,
905 unsigned long flags,
906 int pc)
907{
908 struct ring_buffer_event *event;
909 struct ftrace_graph_ret_entry *entry;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100910
911 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
912 return;
913
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200914 event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
915 sizeof(*entry), flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100916 if (!event)
917 return;
918 entry = ring_buffer_event_data(event);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100919 entry->ret = *trace;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200920 ring_buffer_unlock_commit(global_trace.buffer, event);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +0100921}
922#endif
923
Ingo Molnare309b412008-05-12 21:20:51 +0200924void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200925ftrace(struct trace_array *tr, struct trace_array_cpu *data,
Steven Rostedt38697052008-10-01 13:14:09 -0400926 unsigned long ip, unsigned long parent_ip, unsigned long flags,
927 int pc)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200928{
929 if (likely(!atomic_read(&data->disabled)))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500930 trace_function(tr, ip, parent_ip, flags, pc);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200931}
932
Steven Rostedt53614992009-01-15 19:12:40 -0500933static void __ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500934 unsigned long flags,
935 int skip, int pc)
Ingo Molnar86387f72008-05-12 21:20:51 +0200936{
Al Viroc2c80522008-10-31 19:50:41 +0000937#ifdef CONFIG_STACKTRACE
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400938 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -0400939 struct stack_entry *entry;
Ingo Molnar86387f72008-05-12 21:20:51 +0200940 struct stack_trace trace;
941
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200942 event = trace_buffer_lock_reserve(tr, TRACE_STACK,
943 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400944 if (!event)
945 return;
946 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -0400947 memset(&entry->caller, 0, sizeof(entry->caller));
Ingo Molnar86387f72008-05-12 21:20:51 +0200948
949 trace.nr_entries = 0;
950 trace.max_entries = FTRACE_STACK_ENTRIES;
951 trace.skip = skip;
Steven Rostedt777e2082008-09-29 23:02:42 -0400952 trace.entries = entry->caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200953
954 save_stack_trace(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200955 ring_buffer_unlock_commit(tr->buffer, event);
Al Viroc2c80522008-10-31 19:50:41 +0000956#endif
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200957}
958
Steven Rostedt53614992009-01-15 19:12:40 -0500959static void ftrace_trace_stack(struct trace_array *tr,
Steven Rostedt53614992009-01-15 19:12:40 -0500960 unsigned long flags,
961 int skip, int pc)
962{
963 if (!(trace_flags & TRACE_ITER_STACKTRACE))
964 return;
965
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500966 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt53614992009-01-15 19:12:40 -0500967}
968
Steven Rostedt38697052008-10-01 13:14:09 -0400969void __trace_stack(struct trace_array *tr,
Steven Rostedt38697052008-10-01 13:14:09 -0400970 unsigned long flags,
Steven Rostedt53614992009-01-15 19:12:40 -0500971 int skip, int pc)
Steven Rostedt38697052008-10-01 13:14:09 -0400972{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500973 __ftrace_trace_stack(tr, flags, skip, pc);
Steven Rostedt38697052008-10-01 13:14:09 -0400974}
975
Török Edwin02b67512008-11-22 13:28:47 +0200976static void ftrace_trace_userstack(struct trace_array *tr,
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500977 unsigned long flags, int pc)
Török Edwin02b67512008-11-22 13:28:47 +0200978{
Török Edwinc7425ac2008-11-28 11:17:56 +0200979#ifdef CONFIG_STACKTRACE
Török Edwin8d7c6a92008-11-23 12:39:06 +0200980 struct ring_buffer_event *event;
Török Edwin02b67512008-11-22 13:28:47 +0200981 struct userstack_entry *entry;
982 struct stack_trace trace;
Török Edwin02b67512008-11-22 13:28:47 +0200983
984 if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
985 return;
986
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200987 event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
988 sizeof(*entry), flags, pc);
Török Edwin02b67512008-11-22 13:28:47 +0200989 if (!event)
990 return;
991 entry = ring_buffer_event_data(event);
Török Edwin02b67512008-11-22 13:28:47 +0200992
993 memset(&entry->caller, 0, sizeof(entry->caller));
994
995 trace.nr_entries = 0;
996 trace.max_entries = FTRACE_STACK_ENTRIES;
997 trace.skip = 0;
998 trace.entries = entry->caller;
999
1000 save_stack_trace_user(&trace);
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02001001 ring_buffer_unlock_commit(tr->buffer, event);
Török Edwinc7425ac2008-11-28 11:17:56 +02001002#endif
Török Edwin02b67512008-11-22 13:28:47 +02001003}
1004
Hannes Eder4fd27352009-02-10 19:44:12 +01001005#ifdef UNUSED
1006static void __trace_userstack(struct trace_array *tr, unsigned long flags)
Török Edwin02b67512008-11-22 13:28:47 +02001007{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001008 ftrace_trace_userstack(tr, flags, preempt_count());
Török Edwin02b67512008-11-22 13:28:47 +02001009}
Hannes Eder4fd27352009-02-10 19:44:12 +01001010#endif /* UNUSED */
Török Edwin02b67512008-11-22 13:28:47 +02001011
Steven Rostedt38697052008-10-01 13:14:09 -04001012static void
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001013ftrace_trace_special(void *__tr,
Steven Rostedt38697052008-10-01 13:14:09 -04001014 unsigned long arg1, unsigned long arg2, unsigned long arg3,
1015 int pc)
Ingo Molnara4feb832008-05-12 21:21:02 +02001016{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001017 struct ring_buffer_event *event;
Ingo Molnara4feb832008-05-12 21:21:02 +02001018 struct trace_array *tr = __tr;
Steven Rostedt777e2082008-09-29 23:02:42 -04001019 struct special_entry *entry;
Ingo Molnara4feb832008-05-12 21:21:02 +02001020
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001021 event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1022 sizeof(*entry), 0, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001023 if (!event)
1024 return;
1025 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001026 entry->arg1 = arg1;
1027 entry->arg2 = arg2;
1028 entry->arg3 = arg3;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001029 trace_buffer_unlock_commit(tr, event, 0, pc);
Ingo Molnara4feb832008-05-12 21:21:02 +02001030}
1031
1032void
Steven Rostedt38697052008-10-01 13:14:09 -04001033__trace_special(void *__tr, void *__data,
1034 unsigned long arg1, unsigned long arg2, unsigned long arg3)
1035{
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001036 ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
Steven Rostedt38697052008-10-01 13:14:09 -04001037}
1038
1039void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001040tracing_sched_switch_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001041 struct task_struct *prev,
1042 struct task_struct *next,
Steven Rostedt38697052008-10-01 13:14:09 -04001043 unsigned long flags, int pc)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001045 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001046 struct ctx_switch_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001047
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001048 event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1049 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001050 if (!event)
1051 return;
1052 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001053 entry->prev_pid = prev->pid;
1054 entry->prev_prio = prev->prio;
1055 entry->prev_state = prev->state;
1056 entry->next_pid = next->pid;
1057 entry->next_prio = next->prio;
1058 entry->next_state = next->state;
1059 entry->next_cpu = task_cpu(next);
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001060 trace_buffer_unlock_commit(tr, event, flags, pc);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001061}
1062
Ingo Molnar57422792008-05-12 21:20:51 +02001063void
1064tracing_sched_wakeup_trace(struct trace_array *tr,
Ingo Molnar86387f72008-05-12 21:20:51 +02001065 struct task_struct *wakee,
1066 struct task_struct *curr,
Steven Rostedt38697052008-10-01 13:14:09 -04001067 unsigned long flags, int pc)
Ingo Molnar57422792008-05-12 21:20:51 +02001068{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001069 struct ring_buffer_event *event;
Steven Rostedt777e2082008-09-29 23:02:42 -04001070 struct ctx_switch_entry *entry;
Ingo Molnar57422792008-05-12 21:20:51 +02001071
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02001072 event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1073 sizeof(*entry), flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001074 if (!event)
1075 return;
1076 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04001077 entry->prev_pid = curr->pid;
1078 entry->prev_prio = curr->prio;
1079 entry->prev_state = curr->state;
1080 entry->next_pid = wakee->pid;
1081 entry->next_prio = wakee->prio;
1082 entry->next_state = wakee->state;
1083 entry->next_cpu = task_cpu(wakee);
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01001084
1085 ring_buffer_unlock_commit(tr->buffer, event);
1086 ftrace_trace_stack(tr, flags, 6, pc);
1087 ftrace_trace_userstack(tr, flags, pc);
Ingo Molnar57422792008-05-12 21:20:51 +02001088}
1089
Steven Rostedt4902f882008-05-22 00:22:18 -04001090void
1091ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1092{
1093 struct trace_array *tr = &global_trace;
1094 struct trace_array_cpu *data;
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001095 unsigned long flags;
Steven Rostedt4902f882008-05-22 00:22:18 -04001096 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -04001097 int pc;
Steven Rostedt4902f882008-05-22 00:22:18 -04001098
Steven Rostedtc76f0692008-11-07 22:36:02 -05001099 if (tracing_disabled)
Steven Rostedt4902f882008-05-22 00:22:18 -04001100 return;
1101
Steven Rostedt38697052008-10-01 13:14:09 -04001102 pc = preempt_count();
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001103 local_irq_save(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001104 cpu = raw_smp_processor_id();
1105 data = tr->data[cpu];
Steven Rostedt4902f882008-05-22 00:22:18 -04001106
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001107 if (likely(atomic_inc_return(&data->disabled) == 1))
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001108 ftrace_trace_special(tr, arg1, arg2, arg3, pc);
Steven Rostedt4902f882008-05-22 00:22:18 -04001109
Steven Rostedt5aa1ba62008-11-10 23:07:30 -05001110 atomic_dec(&data->disabled);
1111 local_irq_restore(flags);
Steven Rostedt4902f882008-05-22 00:22:18 -04001112}
1113
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001114#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedte49dc192008-12-02 23:50:05 -05001115int trace_graph_entry(struct ftrace_graph_ent *trace)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001116{
1117 struct trace_array *tr = &global_trace;
1118 struct trace_array_cpu *data;
1119 unsigned long flags;
1120 long disabled;
1121 int cpu;
1122 int pc;
1123
Steven Rostedt804a6852008-12-03 15:36:59 -05001124 if (!ftrace_trace_task(current))
1125 return 0;
1126
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001127 if (!ftrace_graph_addr(trace->func))
1128 return 0;
1129
Steven Rostedta5e25882008-12-02 15:34:05 -05001130 local_irq_save(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001131 cpu = raw_smp_processor_id();
1132 data = tr->data[cpu];
1133 disabled = atomic_inc_return(&data->disabled);
1134 if (likely(disabled == 1)) {
1135 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001136 __trace_graph_entry(tr, trace, flags, pc);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001137 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001138 /* Only do the atomic if it is not already set */
1139 if (!test_tsk_trace_graph(current))
1140 set_tsk_trace_graph(current);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001141 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001142 local_irq_restore(flags);
Steven Rostedte49dc192008-12-02 23:50:05 -05001143
1144 return 1;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001145}
1146
1147void trace_graph_return(struct ftrace_graph_ret *trace)
1148{
1149 struct trace_array *tr = &global_trace;
1150 struct trace_array_cpu *data;
1151 unsigned long flags;
1152 long disabled;
1153 int cpu;
1154 int pc;
1155
Steven Rostedta5e25882008-12-02 15:34:05 -05001156 local_irq_save(flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001157 cpu = raw_smp_processor_id();
1158 data = tr->data[cpu];
1159 disabled = atomic_inc_return(&data->disabled);
1160 if (likely(disabled == 1)) {
1161 pc = preempt_count();
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -05001162 __trace_graph_return(tr, trace, flags, pc);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001163 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05001164 if (!trace->depth)
1165 clear_tsk_trace_graph(current);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001166 atomic_dec(&data->disabled);
Steven Rostedta5e25882008-12-02 15:34:05 -05001167 local_irq_restore(flags);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001168}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001169#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01001170
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001171enum trace_file_type {
1172 TRACE_FILE_LAT_FMT = 1,
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001173 TRACE_FILE_ANNOTATE = 2,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174};
1175
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001176static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedt5a90f572008-09-03 17:42:51 -04001177{
Steven Rostedtd7690412008-10-01 00:29:53 -04001178 /* Don't allow ftrace to trace into the ring buffers */
1179 ftrace_disable_cpu();
1180
Steven Rostedt5a90f572008-09-03 17:42:51 -04001181 iter->idx++;
Steven Rostedtd7690412008-10-01 00:29:53 -04001182 if (iter->buffer_iter[iter->cpu])
1183 ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1184
1185 ftrace_enable_cpu();
Steven Rostedt5a90f572008-09-03 17:42:51 -04001186}
1187
Ingo Molnare309b412008-05-12 21:20:51 +02001188static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001189peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001190{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001191 struct ring_buffer_event *event;
1192 struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001193
Steven Rostedtd7690412008-10-01 00:29:53 -04001194 /* Don't allow ftrace to trace into the ring buffers */
1195 ftrace_disable_cpu();
1196
1197 if (buf_iter)
1198 event = ring_buffer_iter_peek(buf_iter, ts);
1199 else
1200 event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1201
1202 ftrace_enable_cpu();
1203
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001204 return event ? ring_buffer_event_data(event) : NULL;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001205}
Steven Rostedtd7690412008-10-01 00:29:53 -04001206
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001207static struct trace_entry *
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001208__find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001209{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001210 struct ring_buffer *buffer = iter->tr->buffer;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001211 struct trace_entry *ent, *next = NULL;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001212 int cpu_file = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001213 u64 next_ts = 0, ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001214 int next_cpu = -1;
1215 int cpu;
1216
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001217 /*
1218 * If we are in a per_cpu trace file, don't bother by iterating over
1219 * all cpu and peek directly.
1220 */
1221 if (cpu_file > TRACE_PIPE_ALL_CPU) {
1222 if (ring_buffer_empty_cpu(buffer, cpu_file))
1223 return NULL;
1224 ent = peek_next_entry(iter, cpu_file, ent_ts);
1225 if (ent_cpu)
1226 *ent_cpu = cpu_file;
1227
1228 return ent;
1229 }
1230
Steven Rostedtab464282008-05-12 21:21:00 +02001231 for_each_tracing_cpu(cpu) {
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001232
1233 if (ring_buffer_empty_cpu(buffer, cpu))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001234 continue;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001235
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001236 ent = peek_next_entry(iter, cpu, &ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001237
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001238 /*
1239 * Pick the entry with the smallest timestamp:
1240 */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001241 if (ent && (!next || ts < next_ts)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001242 next = ent;
1243 next_cpu = cpu;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001244 next_ts = ts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001245 }
1246 }
1247
1248 if (ent_cpu)
1249 *ent_cpu = next_cpu;
1250
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001251 if (ent_ts)
1252 *ent_ts = next_ts;
1253
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001254 return next;
1255}
1256
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001257/* Find the next real entry, without updating the iterator itself */
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001258struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1259 int *ent_cpu, u64 *ent_ts)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001260{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001261 return __find_next_entry(iter, ent_cpu, ent_ts);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001262}
Ingo Molnar8c523a92008-05-12 21:20:46 +02001263
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001264/* Find the next real entry, and increment the iterator to the next entry */
1265static void *find_next_entry_inc(struct trace_iterator *iter)
1266{
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001267 iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001268
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001269 if (iter->ent)
Robert Richtere2ac8ef2008-11-12 12:59:32 +01001270 trace_iterator_increment(iter);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001271
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001272 return iter->ent ? iter : NULL;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001273}
1274
Ingo Molnare309b412008-05-12 21:20:51 +02001275static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001276{
Steven Rostedtd7690412008-10-01 00:29:53 -04001277 /* Don't allow ftrace to trace into the ring buffers */
1278 ftrace_disable_cpu();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001279 ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
Steven Rostedtd7690412008-10-01 00:29:53 -04001280 ftrace_enable_cpu();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001281}
1282
Ingo Molnare309b412008-05-12 21:20:51 +02001283static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001284{
1285 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001286 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001287 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288
1289 (*pos)++;
1290
1291 /* can't go backwards */
1292 if (iter->idx > i)
1293 return NULL;
1294
1295 if (iter->idx < 0)
1296 ent = find_next_entry_inc(iter);
1297 else
1298 ent = iter;
1299
1300 while (ent && iter->idx < i)
1301 ent = find_next_entry_inc(iter);
1302
1303 iter->pos = *pos;
1304
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001305 return ent;
1306}
1307
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001308/*
1309 * No necessary locking here. The worst thing which can
1310 * happen is loosing events consumed at the same time
1311 * by a trace_pipe reader.
1312 * Other than that, we don't risk to crash the ring buffer
1313 * because it serializes the readers.
1314 *
1315 * The current tracer is copied to avoid a global locking
1316 * all around.
1317 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318static void *s_start(struct seq_file *m, loff_t *pos)
1319{
1320 struct trace_iterator *iter = m->private;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001321 static struct tracer *old_tracer;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001322 int cpu_file = iter->cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323 void *p = NULL;
1324 loff_t l = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001325 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001326
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001327 /* copy the tracer to avoid using a global lock all around */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001328 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001329 if (unlikely(old_tracer != current_trace && current_trace)) {
1330 old_tracer = current_trace;
1331 *iter->trace = *current_trace;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001332 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001333 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001334
1335 atomic_inc(&trace_record_cmdline_disabled);
1336
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001337 if (*pos != iter->pos) {
1338 iter->ent = NULL;
1339 iter->cpu = 0;
1340 iter->idx = -1;
1341
Steven Rostedtd7690412008-10-01 00:29:53 -04001342 ftrace_disable_cpu();
1343
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001344 if (cpu_file == TRACE_PIPE_ALL_CPU) {
1345 for_each_tracing_cpu(cpu)
1346 ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1347 } else
1348 ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1349
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001350
Steven Rostedtd7690412008-10-01 00:29:53 -04001351 ftrace_enable_cpu();
1352
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1354 ;
1355
1356 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001357 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001358 p = s_next(m, p, &l);
1359 }
1360
1361 return p;
1362}
1363
1364static void s_stop(struct seq_file *m, void *p)
1365{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001366 atomic_dec(&trace_record_cmdline_disabled);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001367}
1368
Ingo Molnare309b412008-05-12 21:20:51 +02001369static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001370{
Michael Ellermana6168352008-08-20 16:36:11 -07001371 seq_puts(m, "# _------=> CPU# \n");
1372 seq_puts(m, "# / _-----=> irqs-off \n");
1373 seq_puts(m, "# | / _----=> need-resched \n");
1374 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1375 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1376 seq_puts(m, "# |||| / \n");
1377 seq_puts(m, "# ||||| delay \n");
1378 seq_puts(m, "# cmd pid ||||| time | caller \n");
1379 seq_puts(m, "# \\ / ||||| \\ | / \n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380}
1381
Ingo Molnare309b412008-05-12 21:20:51 +02001382static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001383{
Michael Ellermana6168352008-08-20 16:36:11 -07001384 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1385 seq_puts(m, "# | | | | |\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386}
1387
1388
Ingo Molnare309b412008-05-12 21:20:51 +02001389static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001390print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1391{
1392 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1393 struct trace_array *tr = iter->tr;
1394 struct trace_array_cpu *data = tr->data[tr->cpu];
1395 struct tracer *type = current_trace;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001396 unsigned long total;
1397 unsigned long entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001398 const char *name = "preemption";
1399
1400 if (type)
1401 name = type->name;
1402
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001403 entries = ring_buffer_entries(iter->tr->buffer);
1404 total = entries +
1405 ring_buffer_overruns(iter->tr->buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001406
1407 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1408 name, UTS_RELEASE);
1409 seq_puts(m, "-----------------------------------"
1410 "---------------------------------\n");
1411 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1412 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001413 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001414 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001415 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001416 tr->cpu,
1417#if defined(CONFIG_PREEMPT_NONE)
1418 "server",
1419#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1420 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001421#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001422 "preempt",
1423#else
1424 "unknown",
1425#endif
1426 /* These are reserved for later use */
1427 0, 0, 0, 0);
1428#ifdef CONFIG_SMP
1429 seq_printf(m, " #P:%d)\n", num_online_cpus());
1430#else
1431 seq_puts(m, ")\n");
1432#endif
1433 seq_puts(m, " -----------------\n");
1434 seq_printf(m, " | task: %.16s-%d "
1435 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1436 data->comm, data->pid, data->uid, data->nice,
1437 data->policy, data->rt_priority);
1438 seq_puts(m, " -----------------\n");
1439
1440 if (data->critical_start) {
1441 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001442 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1443 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001444 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001445 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1446 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001447 seq_puts(m, "\n");
1448 }
1449
1450 seq_puts(m, "\n");
1451}
1452
Steven Rostedta3097202008-11-07 22:36:02 -05001453static void test_cpu_buff_start(struct trace_iterator *iter)
1454{
1455 struct trace_seq *s = &iter->seq;
1456
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001457 if (!(trace_flags & TRACE_ITER_ANNOTATE))
1458 return;
1459
1460 if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1461 return;
1462
Rusty Russell44623442009-01-01 10:12:23 +10301463 if (cpumask_test_cpu(iter->cpu, iter->started))
Steven Rostedta3097202008-11-07 22:36:02 -05001464 return;
1465
Rusty Russell44623442009-01-01 10:12:23 +10301466 cpumask_set_cpu(iter->cpu, iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05001467 trace_seq_printf(s, "##### CPU %u buffer started ####\n", iter->cpu);
1468}
1469
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001470static enum print_line_t print_lat_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001471{
Steven Rostedt214023c2008-05-12 21:20:46 +02001472 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001473 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedtf633cef2008-12-23 23:24:13 -05001474 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001475 struct trace_entry *entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001476
Steven Rostedta3097202008-11-07 22:36:02 -05001477 test_cpu_buff_start(iter);
1478
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001479 event = ftrace_find_event(entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001481 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001482 if (!trace_print_lat_context(iter))
1483 goto partial;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001484 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001485
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001486 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001487 return event->latency_trace(iter, sym_flags);
Steven Rostedt7104f302008-10-01 10:52:51 -04001488
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001489 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1490 goto partial;
1491
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001492 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001493partial:
1494 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001495}
1496
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001497static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001498{
Steven Rostedt214023c2008-05-12 21:20:46 +02001499 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001500 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001501 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001502 struct trace_event *event;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001503
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001504 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001505
Steven Rostedta3097202008-11-07 22:36:02 -05001506 test_cpu_buff_start(iter);
1507
Steven Rostedtf633cef2008-12-23 23:24:13 -05001508 event = ftrace_find_event(entry->type);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001509
1510 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001511 if (!trace_print_context(iter))
1512 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001513 }
1514
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001515 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001516 return event->trace(iter, sym_flags);
1517
1518 if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1519 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001520
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001521 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001522partial:
1523 return TRACE_TYPE_PARTIAL_LINE;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001524}
1525
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001526static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001527{
1528 struct trace_seq *s = &iter->seq;
1529 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001530 struct trace_event *event;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001531
1532 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001533
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001534 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001535 if (!trace_seq_printf(s, "%d %d %llu ",
1536 entry->pid, iter->cpu, iter->ts))
1537 goto partial;
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001538 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001539
Steven Rostedtf633cef2008-12-23 23:24:13 -05001540 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001541 if (event)
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001542 return event->raw(iter, 0);
1543
1544 if (!trace_seq_printf(s, "%d ?\n", entry->type))
1545 goto partial;
Steven Rostedt7104f302008-10-01 10:52:51 -04001546
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001547 return TRACE_TYPE_HANDLED;
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001548partial:
1549 return TRACE_TYPE_PARTIAL_LINE;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001550}
1551
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001552static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001553{
1554 struct trace_seq *s = &iter->seq;
1555 unsigned char newline = '\n';
1556 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001557 struct trace_event *event;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001558
1559 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001560
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001561 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1562 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1563 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1564 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1565 }
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001566
Steven Rostedtf633cef2008-12-23 23:24:13 -05001567 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001568 if (event) {
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001569 enum print_line_t ret = event->hex(iter, 0);
Arnaldo Carvalho de Melod9793bd2009-02-03 20:20:41 -02001570 if (ret != TRACE_TYPE_HANDLED)
1571 return ret;
1572 }
Steven Rostedt7104f302008-10-01 10:52:51 -04001573
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001574 SEQ_PUT_FIELD_RET(s, newline);
1575
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001576 return TRACE_TYPE_HANDLED;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001577}
1578
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001579static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
1580{
1581 struct trace_seq *s = &iter->seq;
1582 struct trace_entry *entry = iter->ent;
1583 struct print_entry *field;
1584 int ret;
1585
1586 trace_assign_type(field, entry);
1587
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001588 ret = trace_seq_printf(s, "%s", field->buf);
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001589 if (!ret)
1590 return TRACE_TYPE_PARTIAL_LINE;
1591
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001592 return TRACE_TYPE_HANDLED;
1593}
1594
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001595static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001596{
1597 struct trace_seq *s = &iter->seq;
1598 struct trace_entry *entry;
Steven Rostedtf633cef2008-12-23 23:24:13 -05001599 struct trace_event *event;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001600
1601 entry = iter->ent;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04001602
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001603 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1604 SEQ_PUT_FIELD_RET(s, entry->pid);
Steven Rostedt1830b522009-02-07 19:38:43 -05001605 SEQ_PUT_FIELD_RET(s, iter->cpu);
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001606 SEQ_PUT_FIELD_RET(s, iter->ts);
1607 }
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001608
Steven Rostedtf633cef2008-12-23 23:24:13 -05001609 event = ftrace_find_event(entry->type);
Arnaldo Carvalho de Melo268ccda2009-02-04 20:16:39 -02001610 return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001611}
1612
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001613static int trace_empty(struct trace_iterator *iter)
1614{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001615 int cpu;
1616
Steven Rostedtab464282008-05-12 21:21:00 +02001617 for_each_tracing_cpu(cpu) {
Steven Rostedtd7690412008-10-01 00:29:53 -04001618 if (iter->buffer_iter[cpu]) {
1619 if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1620 return 0;
1621 } else {
1622 if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1623 return 0;
1624 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001625 }
Steven Rostedtd7690412008-10-01 00:29:53 -04001626
Frederic Weisbecker797d3712008-09-30 18:13:45 +02001627 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001628}
1629
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001630static enum print_line_t print_trace_line(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001631{
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02001632 enum print_line_t ret;
1633
1634 if (iter->trace && iter->trace->print_line) {
1635 ret = iter->trace->print_line(iter);
1636 if (ret != TRACE_TYPE_UNHANDLED)
1637 return ret;
1638 }
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001639
Frederic Weisbecker66896a82008-12-13 20:18:13 +01001640 if (iter->ent->type == TRACE_PRINT &&
1641 trace_flags & TRACE_ITER_PRINTK &&
1642 trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1643 return print_printk_msg_only(iter);
1644
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001645 if (trace_flags & TRACE_ITER_BIN)
1646 return print_bin_fmt(iter);
1647
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001648 if (trace_flags & TRACE_ITER_HEX)
1649 return print_hex_fmt(iter);
1650
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001651 if (trace_flags & TRACE_ITER_RAW)
1652 return print_raw_fmt(iter);
1653
1654 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
Frederic Weisbeckerc4a8e8b2009-02-02 20:29:21 -02001655 return print_lat_fmt(iter);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001656
1657 return print_trace_fmt(iter);
1658}
1659
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001660static int s_show(struct seq_file *m, void *v)
1661{
1662 struct trace_iterator *iter = v;
1663
1664 if (iter->ent == NULL) {
1665 if (iter->tr) {
1666 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1667 seq_puts(m, "#\n");
1668 }
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001669 if (iter->trace && iter->trace->print_header)
1670 iter->trace->print_header(m);
1671 else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001672 /* print nothing if the buffers are empty */
1673 if (trace_empty(iter))
1674 return 0;
1675 print_trace_header(m, iter);
1676 if (!(trace_flags & TRACE_ITER_VERBOSE))
1677 print_lat_help_header(m);
1678 } else {
1679 if (!(trace_flags & TRACE_ITER_VERBOSE))
1680 print_func_help_header(m);
1681 }
1682 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001683 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001684 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001685 }
1686
1687 return 0;
1688}
1689
1690static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001691 .start = s_start,
1692 .next = s_next,
1693 .stop = s_stop,
1694 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001695};
1696
Ingo Molnare309b412008-05-12 21:20:51 +02001697static struct trace_iterator *
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001698__tracing_open(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001699{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001700 long cpu_file = (long) inode->i_private;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001701 void *fail_ret = ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001702 struct trace_iterator *iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001703 struct seq_file *m;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001704 int cpu, ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001705
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001706 if (tracing_disabled)
1707 return ERR_PTR(-ENODEV);
Steven Rostedt60a11772008-05-12 21:20:44 +02001708
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001709 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001710 if (!iter)
1711 return ERR_PTR(-ENOMEM);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001712
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001713 /*
1714 * We make a copy of the current tracer to avoid concurrent
1715 * changes on it while we are reading.
1716 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001717 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001718 iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001719 if (!iter->trace)
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001720 goto fail;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001721
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001722 if (current_trace)
1723 *iter->trace = *current_trace;
1724
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001725 if (current_trace && current_trace->print_max)
1726 iter->tr = &max_tr;
1727 else
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001728 iter->tr = &global_trace;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001729 iter->pos = -1;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001730 mutex_init(&iter->mutex);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001731 iter->cpu_file = cpu_file;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001733 /* Notify the tracer early; before we stop tracing. */
1734 if (iter->trace && iter->trace->open)
Markus Metzgera93751c2008-12-11 13:53:26 +01001735 iter->trace->open(iter);
Markus Metzger8bba1bf2008-11-25 09:12:31 +01001736
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001737 /* Annotate start of buffers if we had overruns */
1738 if (ring_buffer_overruns(iter->tr->buffer))
1739 iter->iter_flags |= TRACE_FILE_ANNOTATE;
1740
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001741 if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1742 for_each_tracing_cpu(cpu) {
Steven Rostedt12ef7d42008-11-12 17:52:38 -05001743
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001744 iter->buffer_iter[cpu] =
1745 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001746
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001747 if (!iter->buffer_iter[cpu])
1748 goto fail_buffer;
1749 }
1750 } else {
1751 cpu = iter->cpu_file;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001752 iter->buffer_iter[cpu] =
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001753 ring_buffer_read_start(iter->tr->buffer, cpu);
Steven Rostedtd7690412008-10-01 00:29:53 -04001754
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001755 if (!iter->buffer_iter[cpu])
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01001756 goto fail;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001757 }
1758
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001759 /* TODO stop tracer */
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001760 ret = seq_open(file, &tracer_seq_ops);
1761 if (ret < 0) {
1762 fail_ret = ERR_PTR(ret);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001763 goto fail_buffer;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001764 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001766 m = file->private_data;
1767 m->private = iter;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001768
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001769 /* stop the trace while dumping */
Steven Rostedt90369902008-11-05 16:05:44 -05001770 tracing_stop();
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001771
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001772 mutex_unlock(&trace_types_lock);
1773
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001774 return iter;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001775
1776 fail_buffer:
1777 for_each_tracing_cpu(cpu) {
1778 if (iter->buffer_iter[cpu])
1779 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1780 }
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001781 fail:
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001782 mutex_unlock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001783 kfree(iter->trace);
Julia Lawall0bb943c2008-11-14 19:05:31 +01001784 kfree(iter);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001785
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001786 return fail_ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001787}
1788
1789int tracing_open_generic(struct inode *inode, struct file *filp)
1790{
Steven Rostedt60a11772008-05-12 21:20:44 +02001791 if (tracing_disabled)
1792 return -ENODEV;
1793
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001794 filp->private_data = inode->i_private;
1795 return 0;
1796}
1797
Hannes Eder4fd27352009-02-10 19:44:12 +01001798static int tracing_release(struct inode *inode, struct file *file)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001799{
1800 struct seq_file *m = (struct seq_file *)file->private_data;
1801 struct trace_iterator *iter = m->private;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001802 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001803
1804 mutex_lock(&trace_types_lock);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04001805 for_each_tracing_cpu(cpu) {
1806 if (iter->buffer_iter[cpu])
1807 ring_buffer_read_finish(iter->buffer_iter[cpu]);
1808 }
1809
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001810 if (iter->trace && iter->trace->close)
1811 iter->trace->close(iter);
1812
1813 /* reenable tracing if it was previously enabled */
Steven Rostedt90369902008-11-05 16:05:44 -05001814 tracing_start();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001815 mutex_unlock(&trace_types_lock);
1816
1817 seq_release(inode, file);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01001818 mutex_destroy(&iter->mutex);
1819 kfree(iter->trace);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001820 kfree(iter);
1821 return 0;
1822}
1823
1824static int tracing_open(struct inode *inode, struct file *file)
1825{
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001826 struct trace_iterator *iter;
1827 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001828
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001829 iter = __tracing_open(inode, file);
1830 if (IS_ERR(iter))
1831 ret = PTR_ERR(iter);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001832
1833 return ret;
1834}
1835
1836static int tracing_lt_open(struct inode *inode, struct file *file)
1837{
1838 struct trace_iterator *iter;
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001839 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001840
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001841 iter = __tracing_open(inode, file);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001842
Steven Rostedt85a2f9b2009-02-27 00:12:38 -05001843 if (IS_ERR(iter))
1844 ret = PTR_ERR(iter);
1845 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001846 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1847
1848 return ret;
1849}
1850
1851
Ingo Molnare309b412008-05-12 21:20:51 +02001852static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001853t_next(struct seq_file *m, void *v, loff_t *pos)
1854{
1855 struct tracer *t = m->private;
1856
1857 (*pos)++;
1858
1859 if (t)
1860 t = t->next;
1861
1862 m->private = t;
1863
1864 return t;
1865}
1866
1867static void *t_start(struct seq_file *m, loff_t *pos)
1868{
1869 struct tracer *t = m->private;
1870 loff_t l = 0;
1871
1872 mutex_lock(&trace_types_lock);
1873 for (; t && l < *pos; t = t_next(m, t, &l))
1874 ;
1875
1876 return t;
1877}
1878
1879static void t_stop(struct seq_file *m, void *p)
1880{
1881 mutex_unlock(&trace_types_lock);
1882}
1883
1884static int t_show(struct seq_file *m, void *v)
1885{
1886 struct tracer *t = v;
1887
1888 if (!t)
1889 return 0;
1890
1891 seq_printf(m, "%s", t->name);
1892 if (t->next)
1893 seq_putc(m, ' ');
1894 else
1895 seq_putc(m, '\n');
1896
1897 return 0;
1898}
1899
1900static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001901 .start = t_start,
1902 .next = t_next,
1903 .stop = t_stop,
1904 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001905};
1906
1907static int show_traces_open(struct inode *inode, struct file *file)
1908{
1909 int ret;
1910
Steven Rostedt60a11772008-05-12 21:20:44 +02001911 if (tracing_disabled)
1912 return -ENODEV;
1913
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001914 ret = seq_open(file, &show_traces_seq_ops);
1915 if (!ret) {
1916 struct seq_file *m = file->private_data;
1917 m->private = trace_types;
1918 }
1919
1920 return ret;
1921}
1922
1923static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001924 .open = tracing_open,
1925 .read = seq_read,
1926 .llseek = seq_lseek,
1927 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001928};
1929
1930static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001931 .open = tracing_lt_open,
1932 .read = seq_read,
1933 .llseek = seq_lseek,
1934 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001935};
1936
1937static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001938 .open = show_traces_open,
1939 .read = seq_read,
1940 .release = seq_release,
1941};
1942
Ingo Molnar36dfe922008-05-12 21:20:52 +02001943/*
1944 * Only trace on a CPU if the bitmask is set:
1945 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301946static cpumask_var_t tracing_cpumask;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001947
1948/*
1949 * The tracer itself will not take this lock, but still we want
1950 * to provide a consistent cpumask to user-space:
1951 */
1952static DEFINE_MUTEX(tracing_cpumask_update_lock);
1953
1954/*
1955 * Temporary storage for the character representation of the
1956 * CPU bitmask (and one more byte for the newline):
1957 */
1958static char mask_str[NR_CPUS + 1];
1959
Ingo Molnarc7078de2008-05-12 21:20:52 +02001960static ssize_t
1961tracing_cpumask_read(struct file *filp, char __user *ubuf,
1962 size_t count, loff_t *ppos)
1963{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001964 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001965
1966 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001967
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301968 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001969 if (count - len < 2) {
1970 count = -EINVAL;
1971 goto out_err;
1972 }
1973 len += sprintf(mask_str + len, "\n");
1974 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1975
1976out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001977 mutex_unlock(&tracing_cpumask_update_lock);
1978
1979 return count;
1980}
1981
1982static ssize_t
1983tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1984 size_t count, loff_t *ppos)
1985{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001986 int err, cpu;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301987 cpumask_var_t tracing_cpumask_new;
1988
1989 if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
1990 return -ENOMEM;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001991
1992 mutex_lock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10301993 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001994 if (err)
1995 goto err_unlock;
1996
Steven Rostedta5e25882008-12-02 15:34:05 -05001997 local_irq_disable();
Steven Rostedt92205c22008-05-12 21:20:55 +02001998 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02001999 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002000 /*
2001 * Increase/decrease the disabled counter if we are
2002 * about to flip a bit in the cpumask:
2003 */
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302004 if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2005 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002006 atomic_inc(&global_trace.data[cpu]->disabled);
2007 }
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302008 if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2009 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002010 atomic_dec(&global_trace.data[cpu]->disabled);
2011 }
2012 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002013 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -05002014 local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002015
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302016 cpumask_copy(tracing_cpumask, tracing_cpumask_new);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002017
Ingo Molnarc7078de2008-05-12 21:20:52 +02002018 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302019 free_cpumask_var(tracing_cpumask_new);
Ingo Molnarc7078de2008-05-12 21:20:52 +02002020
Ingo Molnarc7078de2008-05-12 21:20:52 +02002021 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002022
2023err_unlock:
2024 mutex_unlock(&tracing_cpumask_update_lock);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10302025 free_cpumask_var(tracing_cpumask);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002026
2027 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002028}
2029
2030static struct file_operations tracing_cpumask_fops = {
2031 .open = tracing_open_generic,
2032 .read = tracing_cpumask_read,
2033 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002034};
2035
2036static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002037tracing_trace_options_read(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002038 size_t cnt, loff_t *ppos)
2039{
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002040 struct tracer_opt *trace_opts;
2041 u32 tracer_flags;
2042 int len = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002043 char *buf;
2044 int r = 0;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002045 int i;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002046
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002047
Wenji Huangc3706f02009-02-10 01:03:18 -05002048 /* calculate max size */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002049 for (i = 0; trace_options[i]; i++) {
2050 len += strlen(trace_options[i]);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002051 len += 3; /* "no" and newline */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002052 }
2053
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002054 mutex_lock(&trace_types_lock);
2055 tracer_flags = current_trace->flags->val;
2056 trace_opts = current_trace->flags->opts;
2057
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002058 /*
2059 * Increase the size with names of options specific
2060 * of the current tracer.
2061 */
2062 for (i = 0; trace_opts[i].name; i++) {
2063 len += strlen(trace_opts[i].name);
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002064 len += 3; /* "no" and newline */
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002065 }
2066
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002067 /* +2 for \n and \0 */
2068 buf = kmalloc(len + 2, GFP_KERNEL);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002069 if (!buf) {
2070 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002071 return -ENOMEM;
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002072 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002073
2074 for (i = 0; trace_options[i]; i++) {
2075 if (trace_flags & (1 << i))
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002076 r += sprintf(buf + r, "%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002077 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002078 r += sprintf(buf + r, "no%s\n", trace_options[i]);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002079 }
2080
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002081 for (i = 0; trace_opts[i].name; i++) {
2082 if (tracer_flags & trace_opts[i].bit)
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002083 r += sprintf(buf + r, "%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002084 trace_opts[i].name);
2085 else
Steven Rostedt5c6a3ae2009-02-27 00:22:21 -05002086 r += sprintf(buf + r, "no%s\n",
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002087 trace_opts[i].name);
2088 }
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002089 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002090
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002091 WARN_ON(r >= len + 2);
2092
Ingo Molnar36dfe922008-05-12 21:20:52 +02002093 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002094
2095 kfree(buf);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002096 return r;
2097}
2098
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002099/* Try to assign a tracer specific option */
2100static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2101{
2102 struct tracer_flags *trace_flags = trace->flags;
2103 struct tracer_opt *opts = NULL;
2104 int ret = 0, i = 0;
2105 int len;
2106
2107 for (i = 0; trace_flags->opts[i].name; i++) {
2108 opts = &trace_flags->opts[i];
2109 len = strlen(opts->name);
2110
2111 if (strncmp(cmp, opts->name, len) == 0) {
2112 ret = trace->set_flag(trace_flags->val,
2113 opts->bit, !neg);
2114 break;
2115 }
2116 }
2117 /* Not found */
2118 if (!trace_flags->opts[i].name)
2119 return -EINVAL;
2120
2121 /* Refused to handle */
2122 if (ret)
2123 return ret;
2124
2125 if (neg)
2126 trace_flags->val &= ~opts->bit;
2127 else
2128 trace_flags->val |= opts->bit;
2129
2130 return 0;
2131}
2132
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002133static ssize_t
Steven Rostedtee6bce52008-11-12 17:52:37 -05002134tracing_trace_options_write(struct file *filp, const char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002135 size_t cnt, loff_t *ppos)
2136{
2137 char buf[64];
2138 char *cmp = buf;
2139 int neg = 0;
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002140 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002141 int i;
2142
Steven Rostedtcffae432008-05-12 21:21:00 +02002143 if (cnt >= sizeof(buf))
2144 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002145
2146 if (copy_from_user(&buf, ubuf, cnt))
2147 return -EFAULT;
2148
2149 buf[cnt] = 0;
2150
2151 if (strncmp(buf, "no", 2) == 0) {
2152 neg = 1;
2153 cmp += 2;
2154 }
2155
2156 for (i = 0; trace_options[i]; i++) {
2157 int len = strlen(trace_options[i]);
2158
2159 if (strncmp(cmp, trace_options[i], len) == 0) {
2160 if (neg)
2161 trace_flags &= ~(1 << i);
2162 else
2163 trace_flags |= (1 << i);
2164 break;
2165 }
2166 }
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002167
2168 /* If no option could be set, test the specific tracer options */
2169 if (!trace_options[i]) {
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002170 mutex_lock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002171 ret = set_tracer_option(current_trace, cmp, neg);
Steven Rostedtd8e83d22009-02-26 23:55:58 -05002172 mutex_unlock(&trace_types_lock);
Frederic Weisbeckeradf9f192008-11-17 19:23:42 +01002173 if (ret)
2174 return ret;
2175 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002176
2177 filp->f_pos += cnt;
2178
2179 return cnt;
2180}
2181
2182static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002183 .open = tracing_open_generic,
Steven Rostedtee6bce52008-11-12 17:52:37 -05002184 .read = tracing_trace_options_read,
2185 .write = tracing_trace_options_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002186};
2187
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002188static const char readme_msg[] =
2189 "tracing mini-HOWTO:\n\n"
2190 "# mkdir /debug\n"
2191 "# mount -t debugfs nodev /debug\n\n"
2192 "# cat /debug/tracing/available_tracers\n"
2193 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2194 "# cat /debug/tracing/current_tracer\n"
2195 "none\n"
2196 "# echo sched_switch > /debug/tracing/current_tracer\n"
2197 "# cat /debug/tracing/current_tracer\n"
2198 "sched_switch\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002199 "# cat /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002200 "noprint-parent nosym-offset nosym-addr noverbose\n"
Steven Rostedtee6bce52008-11-12 17:52:37 -05002201 "# echo print-parent > /debug/tracing/trace_options\n"
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002202 "# echo 1 > /debug/tracing/tracing_enabled\n"
2203 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2204 "echo 0 > /debug/tracing/tracing_enabled\n"
2205;
2206
2207static ssize_t
2208tracing_readme_read(struct file *filp, char __user *ubuf,
2209 size_t cnt, loff_t *ppos)
2210{
2211 return simple_read_from_buffer(ubuf, cnt, ppos,
2212 readme_msg, strlen(readme_msg));
2213}
2214
2215static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002216 .open = tracing_open_generic,
2217 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002218};
2219
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002220static ssize_t
2221tracing_ctrl_read(struct file *filp, char __user *ubuf,
2222 size_t cnt, loff_t *ppos)
2223{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002224 char buf[64];
2225 int r;
2226
Steven Rostedt90369902008-11-05 16:05:44 -05002227 r = sprintf(buf, "%u\n", tracer_enabled);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002228 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002229}
2230
2231static ssize_t
2232tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2233 size_t cnt, loff_t *ppos)
2234{
2235 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002236 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002237 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002238 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002239
Steven Rostedtcffae432008-05-12 21:21:00 +02002240 if (cnt >= sizeof(buf))
2241 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002242
2243 if (copy_from_user(&buf, ubuf, cnt))
2244 return -EFAULT;
2245
2246 buf[cnt] = 0;
2247
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002248 ret = strict_strtoul(buf, 10, &val);
2249 if (ret < 0)
2250 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002251
2252 val = !!val;
2253
2254 mutex_lock(&trace_types_lock);
Steven Rostedt90369902008-11-05 16:05:44 -05002255 if (tracer_enabled ^ val) {
2256 if (val) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002257 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -05002258 if (current_trace->start)
2259 current_trace->start(tr);
2260 tracing_start();
2261 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002262 tracer_enabled = 0;
Steven Rostedt90369902008-11-05 16:05:44 -05002263 tracing_stop();
2264 if (current_trace->stop)
2265 current_trace->stop(tr);
2266 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002267 }
2268 mutex_unlock(&trace_types_lock);
2269
2270 filp->f_pos += cnt;
2271
2272 return cnt;
2273}
2274
2275static ssize_t
2276tracing_set_trace_read(struct file *filp, char __user *ubuf,
2277 size_t cnt, loff_t *ppos)
2278{
2279 char buf[max_tracer_type_len+2];
2280 int r;
2281
2282 mutex_lock(&trace_types_lock);
2283 if (current_trace)
2284 r = sprintf(buf, "%s\n", current_trace->name);
2285 else
2286 r = sprintf(buf, "\n");
2287 mutex_unlock(&trace_types_lock);
2288
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002289 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002290}
2291
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002292int tracer_init(struct tracer *t, struct trace_array *tr)
2293{
2294 tracing_reset_online_cpus(tr);
2295 return t->init(tr);
2296}
2297
Steven Rostedt577b7852009-02-26 23:43:05 -05002298struct trace_option_dentry;
2299
2300static struct trace_option_dentry *
2301create_trace_option_files(struct tracer *tracer);
2302
2303static void
2304destroy_trace_option_files(struct trace_option_dentry *topts);
2305
Steven Rostedtb2821ae2009-02-02 21:38:32 -05002306static int tracing_set_tracer(const char *buf)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002307{
Steven Rostedt577b7852009-02-26 23:43:05 -05002308 static struct trace_option_dentry *topts;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002309 struct trace_array *tr = &global_trace;
2310 struct tracer *t;
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002311 int ret = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002312
2313 mutex_lock(&trace_types_lock);
2314 for (t = trace_types; t; t = t->next) {
2315 if (strcmp(t->name, buf) == 0)
2316 break;
2317 }
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002318 if (!t) {
2319 ret = -EINVAL;
2320 goto out;
2321 }
2322 if (t == current_trace)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002323 goto out;
2324
Steven Rostedt9f029e82008-11-12 15:24:24 -05002325 trace_branch_disable();
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002326 if (current_trace && current_trace->reset)
2327 current_trace->reset(tr);
2328
Steven Rostedt577b7852009-02-26 23:43:05 -05002329 destroy_trace_option_files(topts);
2330
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002331 current_trace = t;
Steven Rostedt577b7852009-02-26 23:43:05 -05002332
2333 topts = create_trace_option_files(current_trace);
2334
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002335 if (t->init) {
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -02002336 ret = tracer_init(t, tr);
Frederic Weisbecker1c800252008-11-16 05:57:26 +01002337 if (ret)
2338 goto out;
2339 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002340
Steven Rostedt9f029e82008-11-12 15:24:24 -05002341 trace_branch_enable(tr);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002342 out:
2343 mutex_unlock(&trace_types_lock);
2344
Peter Zijlstrad9e54072008-11-01 19:57:37 +01002345 return ret;
2346}
2347
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002348static ssize_t
2349tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2350 size_t cnt, loff_t *ppos)
2351{
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002352 char buf[max_tracer_type_len+1];
2353 int i;
2354 size_t ret;
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002355 int err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002356
Steven Rostedt60063a62008-10-28 10:44:24 -04002357 ret = cnt;
2358
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002359 if (cnt > max_tracer_type_len)
2360 cnt = max_tracer_type_len;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002361
2362 if (copy_from_user(&buf, ubuf, cnt))
2363 return -EFAULT;
2364
2365 buf[cnt] = 0;
2366
2367 /* strip ending whitespace. */
2368 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2369 buf[i] = 0;
2370
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002371 err = tracing_set_tracer(buf);
2372 if (err)
2373 return err;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002374
Frederic Weisbeckere6e7a652008-11-16 05:53:19 +01002375 filp->f_pos += ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002376
Frederic Weisbeckerc2931e02008-10-04 22:04:44 +02002377 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002378}
2379
2380static ssize_t
2381tracing_max_lat_read(struct file *filp, char __user *ubuf,
2382 size_t cnt, loff_t *ppos)
2383{
2384 unsigned long *ptr = filp->private_data;
2385 char buf[64];
2386 int r;
2387
Steven Rostedtcffae432008-05-12 21:21:00 +02002388 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002389 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002390 if (r > sizeof(buf))
2391 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002392 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002393}
2394
2395static ssize_t
2396tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2397 size_t cnt, loff_t *ppos)
2398{
Hannes Eder5e398412009-02-10 19:44:34 +01002399 unsigned long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002400 char buf[64];
Hannes Eder5e398412009-02-10 19:44:34 +01002401 unsigned long val;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002402 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002403
Steven Rostedtcffae432008-05-12 21:21:00 +02002404 if (cnt >= sizeof(buf))
2405 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002406
2407 if (copy_from_user(&buf, ubuf, cnt))
2408 return -EFAULT;
2409
2410 buf[cnt] = 0;
2411
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002412 ret = strict_strtoul(buf, 10, &val);
2413 if (ret < 0)
2414 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002415
2416 *ptr = val * 1000;
2417
2418 return cnt;
2419}
2420
Steven Rostedtb3806b42008-05-12 21:20:46 +02002421static int tracing_open_pipe(struct inode *inode, struct file *filp)
2422{
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002423 long cpu_file = (long) inode->i_private;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002424 struct trace_iterator *iter;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002425 int ret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002426
2427 if (tracing_disabled)
2428 return -ENODEV;
2429
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002430 mutex_lock(&trace_types_lock);
2431
2432 /* We only allow one reader per cpu */
2433 if (cpu_file == TRACE_PIPE_ALL_CPU) {
2434 if (!cpumask_empty(tracing_reader_cpumask)) {
2435 ret = -EBUSY;
2436 goto out;
2437 }
2438 cpumask_setall(tracing_reader_cpumask);
2439 } else {
2440 if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2441 cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2442 else {
2443 ret = -EBUSY;
2444 goto out;
2445 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002446 }
2447
2448 /* create a buffer to store the information to pass to userspace */
2449 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002450 if (!iter) {
2451 ret = -ENOMEM;
2452 goto out;
2453 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002454
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002455 /*
2456 * We make a copy of the current tracer to avoid concurrent
2457 * changes on it while we are reading.
2458 */
2459 iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2460 if (!iter->trace) {
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002461 ret = -ENOMEM;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002462 goto fail;
2463 }
2464 if (current_trace)
2465 *iter->trace = *current_trace;
2466
2467 if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2468 ret = -ENOMEM;
2469 goto fail;
Rusty Russell44623442009-01-01 10:12:23 +10302470 }
2471
Steven Rostedta3097202008-11-07 22:36:02 -05002472 /* trace pipe does not show start of buffer */
Rusty Russell44623442009-01-01 10:12:23 +10302473 cpumask_setall(iter->started);
Steven Rostedta3097202008-11-07 22:36:02 -05002474
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002475 iter->cpu_file = cpu_file;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002476 iter->tr = &global_trace;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002477 mutex_init(&iter->mutex);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002478 filp->private_data = iter;
2479
Steven Rostedt107bad82008-05-12 21:21:01 +02002480 if (iter->trace->pipe_open)
2481 iter->trace->pipe_open(iter);
Steven Rostedt107bad82008-05-12 21:21:01 +02002482
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002483out:
2484 mutex_unlock(&trace_types_lock);
2485 return ret;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002486
2487fail:
2488 kfree(iter->trace);
2489 kfree(iter);
2490 mutex_unlock(&trace_types_lock);
2491 return ret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002492}
2493
2494static int tracing_release_pipe(struct inode *inode, struct file *file)
2495{
2496 struct trace_iterator *iter = file->private_data;
2497
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01002498 mutex_lock(&trace_types_lock);
2499
2500 if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2501 cpumask_clear(tracing_reader_cpumask);
2502 else
2503 cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2504
2505 mutex_unlock(&trace_types_lock);
2506
Rusty Russell44623442009-01-01 10:12:23 +10302507 free_cpumask_var(iter->started);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002508 mutex_destroy(&iter->mutex);
2509 kfree(iter->trace);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002510 kfree(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002511
2512 return 0;
2513}
2514
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002515static unsigned int
2516tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2517{
2518 struct trace_iterator *iter = filp->private_data;
2519
2520 if (trace_flags & TRACE_ITER_BLOCK) {
2521 /*
2522 * Always select as readable when in blocking mode
2523 */
2524 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002525 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002526 if (!trace_empty(iter))
2527 return POLLIN | POLLRDNORM;
2528 poll_wait(filp, &trace_wait, poll_table);
2529 if (!trace_empty(iter))
2530 return POLLIN | POLLRDNORM;
2531
2532 return 0;
2533 }
2534}
2535
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002536
2537void default_wait_pipe(struct trace_iterator *iter)
2538{
2539 DEFINE_WAIT(wait);
2540
2541 prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2542
2543 if (trace_empty(iter))
2544 schedule();
2545
2546 finish_wait(&trace_wait, &wait);
2547}
2548
2549/*
2550 * This is a make-shift waitqueue.
2551 * A tracer might use this callback on some rare cases:
2552 *
2553 * 1) the current tracer might hold the runqueue lock when it wakes up
2554 * a reader, hence a deadlock (sched, function, and function graph tracers)
2555 * 2) the function tracers, trace all functions, we don't want
2556 * the overhead of calling wake_up and friends
2557 * (and tracing them too)
2558 *
2559 * Anyway, this is really very primitive wakeup.
2560 */
2561void poll_wait_pipe(struct trace_iterator *iter)
2562{
2563 set_current_state(TASK_INTERRUPTIBLE);
2564 /* sleep for 100 msecs, and try again. */
2565 schedule_timeout(HZ / 10);
2566}
2567
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002568/* Must be called with trace_types_lock mutex held. */
2569static int tracing_wait_pipe(struct file *filp)
2570{
2571 struct trace_iterator *iter = filp->private_data;
2572
2573 while (trace_empty(iter)) {
2574
2575 if ((filp->f_flags & O_NONBLOCK)) {
2576 return -EAGAIN;
2577 }
2578
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002579 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002580
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002581 iter->trace->wait_pipe(iter);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002582
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002583 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002584
Frederic Weisbecker6eaaa5d2009-02-11 02:25:00 +01002585 if (signal_pending(current))
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002586 return -EINTR;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002587
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002588 /*
2589 * We block until we read something and tracing is disabled.
2590 * We still block if tracing is disabled, but we have never
2591 * read anything. This allows a user to cat this file, and
2592 * then enable tracing. But after we have read something,
2593 * we give an EOF when tracing is again disabled.
2594 *
2595 * iter->pos will be 0 if we haven't read anything.
2596 */
2597 if (!tracer_enabled && iter->pos)
2598 break;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002599 }
2600
2601 return 1;
2602}
2603
Steven Rostedtb3806b42008-05-12 21:20:46 +02002604/*
2605 * Consumer reader.
2606 */
2607static ssize_t
2608tracing_read_pipe(struct file *filp, char __user *ubuf,
2609 size_t cnt, loff_t *ppos)
2610{
2611 struct trace_iterator *iter = filp->private_data;
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002612 static struct tracer *old_tracer;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002613 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002614
2615 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002616 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2617 if (sret != -EBUSY)
2618 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002619
Steven Rostedtf9520752009-03-02 14:04:40 -05002620 trace_seq_init(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002621
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002622 /* copy the tracer to avoid using a global lock all around */
Steven Rostedt107bad82008-05-12 21:21:01 +02002623 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002624 if (unlikely(old_tracer != current_trace && current_trace)) {
2625 old_tracer = current_trace;
2626 *iter->trace = *current_trace;
2627 }
2628 mutex_unlock(&trace_types_lock);
2629
2630 /*
2631 * Avoid more than one consumer on a single file descriptor
2632 * This is just a matter of traces coherency, the ring buffer itself
2633 * is protected.
2634 */
2635 mutex_lock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002636 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002637 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2638 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002639 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002640 }
2641
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002642waitagain:
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002643 sret = tracing_wait_pipe(filp);
2644 if (sret <= 0)
2645 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002646
2647 /* stop when tracing is finished */
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002648 if (trace_empty(iter)) {
2649 sret = 0;
Steven Rostedt107bad82008-05-12 21:21:01 +02002650 goto out;
Eduard - Gabriel Munteanuff987812009-02-09 08:15:55 +02002651 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002652
2653 if (cnt >= PAGE_SIZE)
2654 cnt = PAGE_SIZE - 1;
2655
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002656 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002657 memset(&iter->seq, 0,
2658 sizeof(struct trace_iterator) -
2659 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002660 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002661
Steven Rostedt088b1e422008-05-12 21:20:48 +02002662 while (find_next_entry_inc(iter) != NULL) {
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002663 enum print_line_t ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002664 int len = iter->seq.len;
2665
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002666 ret = print_trace_line(iter);
Frederic Weisbecker2c4f0352008-09-29 20:18:34 +02002667 if (ret == TRACE_TYPE_PARTIAL_LINE) {
Steven Rostedt088b1e422008-05-12 21:20:48 +02002668 /* don't print partial lines */
2669 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002670 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002671 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +01002672 if (ret != TRACE_TYPE_NO_CONSUME)
2673 trace_consume(iter);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002674
2675 if (iter->seq.len >= cnt)
2676 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002677 }
2678
Steven Rostedtb3806b42008-05-12 21:20:46 +02002679 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002680 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2681 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtf9520752009-03-02 14:04:40 -05002682 trace_seq_init(&iter->seq);
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002683
2684 /*
2685 * If there was nothing to send to user, inspite of consuming trace
2686 * entries, go back to wait for more entries.
2687 */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002688 if (sret == -EBUSY)
Pekka Paalanen9ff4b972008-09-29 20:23:48 +02002689 goto waitagain;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002690
Steven Rostedt107bad82008-05-12 21:21:01 +02002691out:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002692 mutex_unlock(&iter->mutex);
Steven Rostedt107bad82008-05-12 21:21:01 +02002693
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002694 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002695}
2696
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002697static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
2698 struct pipe_buffer *buf)
2699{
2700 __free_page(buf->page);
2701}
2702
2703static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
2704 unsigned int idx)
2705{
2706 __free_page(spd->pages[idx]);
2707}
2708
2709static struct pipe_buf_operations tracing_pipe_buf_ops = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002710 .can_merge = 0,
2711 .map = generic_pipe_buf_map,
2712 .unmap = generic_pipe_buf_unmap,
2713 .confirm = generic_pipe_buf_confirm,
2714 .release = tracing_pipe_buf_release,
2715 .steal = generic_pipe_buf_steal,
2716 .get = generic_pipe_buf_get,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002717};
2718
Steven Rostedt34cd4992009-02-09 12:06:29 -05002719static size_t
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002720tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002721{
2722 size_t count;
2723 int ret;
2724
2725 /* Seq buffer is page-sized, exactly what we need. */
2726 for (;;) {
2727 count = iter->seq.len;
2728 ret = print_trace_line(iter);
2729 count = iter->seq.len - count;
2730 if (rem < count) {
2731 rem = 0;
2732 iter->seq.len -= count;
2733 break;
2734 }
2735 if (ret == TRACE_TYPE_PARTIAL_LINE) {
2736 iter->seq.len -= count;
2737 break;
2738 }
2739
2740 trace_consume(iter);
2741 rem -= count;
2742 if (!find_next_entry_inc(iter)) {
2743 rem = 0;
2744 iter->ent = NULL;
2745 break;
2746 }
2747 }
2748
2749 return rem;
2750}
2751
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002752static ssize_t tracing_splice_read_pipe(struct file *filp,
2753 loff_t *ppos,
2754 struct pipe_inode_info *pipe,
2755 size_t len,
2756 unsigned int flags)
2757{
2758 struct page *pages[PIPE_BUFFERS];
2759 struct partial_page partial[PIPE_BUFFERS];
2760 struct trace_iterator *iter = filp->private_data;
2761 struct splice_pipe_desc spd = {
Steven Rostedt34cd4992009-02-09 12:06:29 -05002762 .pages = pages,
2763 .partial = partial,
2764 .nr_pages = 0, /* This gets updated below. */
2765 .flags = flags,
2766 .ops = &tracing_pipe_buf_ops,
2767 .spd_release = tracing_spd_release_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002768 };
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002769 static struct tracer *old_tracer;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002770 ssize_t ret;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002771 size_t rem;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002772 unsigned int i;
2773
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002774 /* copy the tracer to avoid using a global lock all around */
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002775 mutex_lock(&trace_types_lock);
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002776 if (unlikely(old_tracer != current_trace && current_trace)) {
2777 old_tracer = current_trace;
2778 *iter->trace = *current_trace;
2779 }
2780 mutex_unlock(&trace_types_lock);
2781
2782 mutex_lock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002783
2784 if (iter->trace->splice_read) {
2785 ret = iter->trace->splice_read(iter, filp,
2786 ppos, pipe, len, flags);
2787 if (ret)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002788 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002789 }
2790
2791 ret = tracing_wait_pipe(filp);
2792 if (ret <= 0)
Steven Rostedt34cd4992009-02-09 12:06:29 -05002793 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002794
2795 if (!iter->ent && !find_next_entry_inc(iter)) {
2796 ret = -EFAULT;
Steven Rostedt34cd4992009-02-09 12:06:29 -05002797 goto out_err;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002798 }
2799
2800 /* Fill as many pages as possible. */
2801 for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
2802 pages[i] = alloc_page(GFP_KERNEL);
Steven Rostedt34cd4992009-02-09 12:06:29 -05002803 if (!pages[i])
2804 break;
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002805
Frederic Weisbeckerfa7c7f62009-02-11 02:51:30 +01002806 rem = tracing_fill_pipe_page(rem, iter);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002807
2808 /* Copy the data into the page, so we can start over. */
2809 ret = trace_seq_to_buffer(&iter->seq,
2810 page_address(pages[i]),
2811 iter->seq.len);
2812 if (ret < 0) {
2813 __free_page(pages[i]);
2814 break;
2815 }
2816 partial[i].offset = 0;
2817 partial[i].len = iter->seq.len;
2818
Steven Rostedtf9520752009-03-02 14:04:40 -05002819 trace_seq_init(&iter->seq);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002820 }
2821
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002822 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002823
2824 spd.nr_pages = i;
2825
2826 return splice_to_pipe(pipe, &spd);
2827
Steven Rostedt34cd4992009-02-09 12:06:29 -05002828out_err:
Frederic Weisbeckerd7350c3f2009-02-25 06:13:16 +01002829 mutex_unlock(&iter->mutex);
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002830
2831 return ret;
2832}
2833
Steven Rostedta98a3c32008-05-12 21:20:59 +02002834static ssize_t
2835tracing_entries_read(struct file *filp, char __user *ubuf,
2836 size_t cnt, loff_t *ppos)
2837{
2838 struct trace_array *tr = filp->private_data;
2839 char buf[64];
2840 int r;
2841
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002842 r = sprintf(buf, "%lu\n", tr->entries >> 10);
Steven Rostedta98a3c32008-05-12 21:20:59 +02002843 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2844}
2845
2846static ssize_t
2847tracing_entries_write(struct file *filp, const char __user *ubuf,
2848 size_t cnt, loff_t *ppos)
2849{
2850 unsigned long val;
2851 char buf[64];
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002852 int ret, cpu;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002853
Steven Rostedtcffae432008-05-12 21:21:00 +02002854 if (cnt >= sizeof(buf))
2855 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002856
2857 if (copy_from_user(&buf, ubuf, cnt))
2858 return -EFAULT;
2859
2860 buf[cnt] = 0;
2861
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002862 ret = strict_strtoul(buf, 10, &val);
2863 if (ret < 0)
2864 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002865
2866 /* must have at least 1 entry */
2867 if (!val)
2868 return -EINVAL;
2869
2870 mutex_lock(&trace_types_lock);
2871
Steven Rostedtc76f0692008-11-07 22:36:02 -05002872 tracing_stop();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002873
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002874 /* disable all cpu buffers */
2875 for_each_tracing_cpu(cpu) {
2876 if (global_trace.data[cpu])
2877 atomic_inc(&global_trace.data[cpu]->disabled);
2878 if (max_tr.data[cpu])
2879 atomic_inc(&max_tr.data[cpu]->disabled);
2880 }
2881
Steven Rostedt1696b2b2008-11-13 00:09:35 -05002882 /* value is in KB */
2883 val <<= 10;
2884
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002885 if (val != global_trace.entries) {
2886 ret = ring_buffer_resize(global_trace.buffer, val);
2887 if (ret < 0) {
2888 cnt = ret;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002889 goto out;
2890 }
2891
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002892 ret = ring_buffer_resize(max_tr.buffer, val);
2893 if (ret < 0) {
2894 int r;
2895 cnt = ret;
2896 r = ring_buffer_resize(global_trace.buffer,
2897 global_trace.entries);
2898 if (r < 0) {
2899 /* AARGH! We are left with different
2900 * size max buffer!!!! */
2901 WARN_ON(1);
2902 tracing_disabled = 1;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002903 }
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002904 goto out;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002905 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002906
Steven Rostedt3928a8a2008-09-29 23:02:41 -04002907 global_trace.entries = val;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002908 }
2909
2910 filp->f_pos += cnt;
2911
Steven Rostedt19384c02008-05-22 00:22:16 -04002912 /* If check pages failed, return ENOMEM */
2913 if (tracing_disabled)
2914 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002915 out:
Steven Rostedtbf5e6512008-11-10 21:46:00 -05002916 for_each_tracing_cpu(cpu) {
2917 if (global_trace.data[cpu])
2918 atomic_dec(&global_trace.data[cpu]->disabled);
2919 if (max_tr.data[cpu])
2920 atomic_dec(&max_tr.data[cpu]->disabled);
2921 }
2922
Steven Rostedtc76f0692008-11-07 22:36:02 -05002923 tracing_start();
Steven Rostedta98a3c32008-05-12 21:20:59 +02002924 max_tr.entries = global_trace.entries;
2925 mutex_unlock(&trace_types_lock);
2926
2927 return cnt;
2928}
2929
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002930static int mark_printk(const char *fmt, ...)
2931{
2932 int ret;
2933 va_list args;
2934 va_start(args, fmt);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01002935 ret = trace_vprintk(0, -1, fmt, args);
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002936 va_end(args);
2937 return ret;
2938}
2939
2940static ssize_t
2941tracing_mark_write(struct file *filp, const char __user *ubuf,
2942 size_t cnt, loff_t *fpos)
2943{
2944 char *buf;
2945 char *end;
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002946
Steven Rostedtc76f0692008-11-07 22:36:02 -05002947 if (tracing_disabled)
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03002948 return -EINVAL;
2949
2950 if (cnt > TRACE_BUF_SIZE)
2951 cnt = TRACE_BUF_SIZE;
2952
2953 buf = kmalloc(cnt + 1, GFP_KERNEL);
2954 if (buf == NULL)
2955 return -ENOMEM;
2956
2957 if (copy_from_user(buf, ubuf, cnt)) {
2958 kfree(buf);
2959 return -EFAULT;
2960 }
2961
2962 /* Cut from the first nil or newline. */
2963 buf[cnt] = '\0';
2964 end = strchr(buf, '\n');
2965 if (end)
2966 *end = '\0';
2967
2968 cnt = mark_printk("%s\n", buf);
2969 kfree(buf);
2970 *fpos += cnt;
2971
2972 return cnt;
2973}
2974
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002975static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002976 .open = tracing_open_generic,
2977 .read = tracing_max_lat_read,
2978 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002979};
2980
2981static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002982 .open = tracing_open_generic,
2983 .read = tracing_ctrl_read,
2984 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002985};
2986
2987static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002988 .open = tracing_open_generic,
2989 .read = tracing_set_trace_read,
2990 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002991};
2992
Steven Rostedtb3806b42008-05-12 21:20:46 +02002993static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002994 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002995 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002996 .read = tracing_read_pipe,
Eduard - Gabriel Munteanu3c568192009-02-09 08:15:56 +02002997 .splice_read = tracing_splice_read_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002998 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002999};
3000
Steven Rostedta98a3c32008-05-12 21:20:59 +02003001static struct file_operations tracing_entries_fops = {
3002 .open = tracing_open_generic,
3003 .read = tracing_entries_read,
3004 .write = tracing_entries_write,
3005};
3006
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003007static struct file_operations tracing_mark_fops = {
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02003008 .open = tracing_open_generic,
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003009 .write = tracing_mark_write,
3010};
3011
Steven Rostedt2cadf912008-12-01 22:20:19 -05003012struct ftrace_buffer_info {
3013 struct trace_array *tr;
3014 void *spare;
3015 int cpu;
3016 unsigned int read;
3017};
3018
3019static int tracing_buffers_open(struct inode *inode, struct file *filp)
3020{
3021 int cpu = (int)(long)inode->i_private;
3022 struct ftrace_buffer_info *info;
3023
3024 if (tracing_disabled)
3025 return -ENODEV;
3026
3027 info = kzalloc(sizeof(*info), GFP_KERNEL);
3028 if (!info)
3029 return -ENOMEM;
3030
3031 info->tr = &global_trace;
3032 info->cpu = cpu;
3033 info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3034 /* Force reading ring buffer for first read */
3035 info->read = (unsigned int)-1;
3036 if (!info->spare)
3037 goto out;
3038
3039 filp->private_data = info;
3040
3041 return 0;
3042
3043 out:
3044 kfree(info);
3045 return -ENOMEM;
3046}
3047
3048static ssize_t
3049tracing_buffers_read(struct file *filp, char __user *ubuf,
3050 size_t count, loff_t *ppos)
3051{
3052 struct ftrace_buffer_info *info = filp->private_data;
3053 unsigned int pos;
3054 ssize_t ret;
3055 size_t size;
3056
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003057 if (!count)
3058 return 0;
3059
Steven Rostedt2cadf912008-12-01 22:20:19 -05003060 /* Do we have previous read data to read? */
3061 if (info->read < PAGE_SIZE)
3062 goto read;
3063
3064 info->read = 0;
3065
3066 ret = ring_buffer_read_page(info->tr->buffer,
3067 &info->spare,
3068 count,
3069 info->cpu, 0);
3070 if (ret < 0)
3071 return 0;
3072
3073 pos = ring_buffer_page_len(info->spare);
3074
3075 if (pos < PAGE_SIZE)
3076 memset(info->spare + pos, 0, PAGE_SIZE - pos);
3077
3078read:
3079 size = PAGE_SIZE - info->read;
3080 if (size > count)
3081 size = count;
3082
3083 ret = copy_to_user(ubuf, info->spare + info->read, size);
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003084 if (ret == size)
Steven Rostedt2cadf912008-12-01 22:20:19 -05003085 return -EFAULT;
Steven Rostedt2dc5d122009-03-04 19:10:05 -05003086 size -= ret;
3087
Steven Rostedt2cadf912008-12-01 22:20:19 -05003088 *ppos += size;
3089 info->read += size;
3090
3091 return size;
3092}
3093
3094static int tracing_buffers_release(struct inode *inode, struct file *file)
3095{
3096 struct ftrace_buffer_info *info = file->private_data;
3097
3098 ring_buffer_free_read_page(info->tr->buffer, info->spare);
3099 kfree(info);
3100
3101 return 0;
3102}
3103
3104struct buffer_ref {
3105 struct ring_buffer *buffer;
3106 void *page;
3107 int ref;
3108};
3109
3110static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3111 struct pipe_buffer *buf)
3112{
3113 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3114
3115 if (--ref->ref)
3116 return;
3117
3118 ring_buffer_free_read_page(ref->buffer, ref->page);
3119 kfree(ref);
3120 buf->private = 0;
3121}
3122
3123static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3124 struct pipe_buffer *buf)
3125{
3126 return 1;
3127}
3128
3129static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3130 struct pipe_buffer *buf)
3131{
3132 struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3133
3134 ref->ref++;
3135}
3136
3137/* Pipe buffer operations for a buffer. */
3138static struct pipe_buf_operations buffer_pipe_buf_ops = {
3139 .can_merge = 0,
3140 .map = generic_pipe_buf_map,
3141 .unmap = generic_pipe_buf_unmap,
3142 .confirm = generic_pipe_buf_confirm,
3143 .release = buffer_pipe_buf_release,
3144 .steal = buffer_pipe_buf_steal,
3145 .get = buffer_pipe_buf_get,
3146};
3147
3148/*
3149 * Callback from splice_to_pipe(), if we need to release some pages
3150 * at the end of the spd in case we error'ed out in filling the pipe.
3151 */
3152static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3153{
3154 struct buffer_ref *ref =
3155 (struct buffer_ref *)spd->partial[i].private;
3156
3157 if (--ref->ref)
3158 return;
3159
3160 ring_buffer_free_read_page(ref->buffer, ref->page);
3161 kfree(ref);
3162 spd->partial[i].private = 0;
3163}
3164
3165static ssize_t
3166tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3167 struct pipe_inode_info *pipe, size_t len,
3168 unsigned int flags)
3169{
3170 struct ftrace_buffer_info *info = file->private_data;
3171 struct partial_page partial[PIPE_BUFFERS];
3172 struct page *pages[PIPE_BUFFERS];
3173 struct splice_pipe_desc spd = {
3174 .pages = pages,
3175 .partial = partial,
3176 .flags = flags,
3177 .ops = &buffer_pipe_buf_ops,
3178 .spd_release = buffer_spd_release,
3179 };
3180 struct buffer_ref *ref;
3181 int size, i;
3182 size_t ret;
3183
3184 /*
3185 * We can't seek on a buffer input
3186 */
3187 if (unlikely(*ppos))
3188 return -ESPIPE;
3189
3190
3191 for (i = 0; i < PIPE_BUFFERS && len; i++, len -= size) {
3192 struct page *page;
3193 int r;
3194
3195 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3196 if (!ref)
3197 break;
3198
3199 ref->buffer = info->tr->buffer;
3200 ref->page = ring_buffer_alloc_read_page(ref->buffer);
3201 if (!ref->page) {
3202 kfree(ref);
3203 break;
3204 }
3205
3206 r = ring_buffer_read_page(ref->buffer, &ref->page,
3207 len, info->cpu, 0);
3208 if (r < 0) {
3209 ring_buffer_free_read_page(ref->buffer,
3210 ref->page);
3211 kfree(ref);
3212 break;
3213 }
3214
3215 /*
3216 * zero out any left over data, this is going to
3217 * user land.
3218 */
3219 size = ring_buffer_page_len(ref->page);
3220 if (size < PAGE_SIZE)
3221 memset(ref->page + size, 0, PAGE_SIZE - size);
3222
3223 page = virt_to_page(ref->page);
3224
3225 spd.pages[i] = page;
3226 spd.partial[i].len = PAGE_SIZE;
3227 spd.partial[i].offset = 0;
3228 spd.partial[i].private = (unsigned long)ref;
3229 spd.nr_pages++;
3230 }
3231
3232 spd.nr_pages = i;
3233
3234 /* did we read anything? */
3235 if (!spd.nr_pages) {
3236 if (flags & SPLICE_F_NONBLOCK)
3237 ret = -EAGAIN;
3238 else
3239 ret = 0;
3240 /* TODO: block */
3241 return ret;
3242 }
3243
3244 ret = splice_to_pipe(pipe, &spd);
3245
3246 return ret;
3247}
3248
3249static const struct file_operations tracing_buffers_fops = {
3250 .open = tracing_buffers_open,
3251 .read = tracing_buffers_read,
3252 .release = tracing_buffers_release,
3253 .splice_read = tracing_buffers_splice_read,
3254 .llseek = no_llseek,
3255};
3256
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003257#ifdef CONFIG_DYNAMIC_FTRACE
3258
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003259int __weak ftrace_arch_read_dyn_info(char *buf, int size)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003260{
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003261 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003262}
3263
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003264static ssize_t
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003265tracing_read_dyn_info(struct file *filp, char __user *ubuf,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003266 size_t cnt, loff_t *ppos)
3267{
Steven Rostedta26a2a22008-10-31 00:03:22 -04003268 static char ftrace_dyn_info_buffer[1024];
3269 static DEFINE_MUTEX(dyn_info_mutex);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003270 unsigned long *p = filp->private_data;
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003271 char *buf = ftrace_dyn_info_buffer;
Steven Rostedta26a2a22008-10-31 00:03:22 -04003272 int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003273 int r;
3274
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003275 mutex_lock(&dyn_info_mutex);
3276 r = sprintf(buf, "%ld ", *p);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003277
Steven Rostedta26a2a22008-10-31 00:03:22 -04003278 r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003279 buf[r++] = '\n';
3280
3281 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3282
3283 mutex_unlock(&dyn_info_mutex);
3284
3285 return r;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003286}
3287
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003288static struct file_operations tracing_dyn_info_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003289 .open = tracing_open_generic,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003290 .read = tracing_read_dyn_info,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003291};
3292#endif
3293
3294static struct dentry *d_tracer;
3295
3296struct dentry *tracing_init_dentry(void)
3297{
3298 static int once;
3299
3300 if (d_tracer)
3301 return d_tracer;
3302
3303 d_tracer = debugfs_create_dir("tracing", NULL);
3304
3305 if (!d_tracer && !once) {
3306 once = 1;
3307 pr_warning("Could not create debugfs directory 'tracing'\n");
3308 return NULL;
3309 }
3310
3311 return d_tracer;
3312}
3313
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003314static struct dentry *d_percpu;
3315
3316struct dentry *tracing_dentry_percpu(void)
3317{
3318 static int once;
3319 struct dentry *d_tracer;
3320
3321 if (d_percpu)
3322 return d_percpu;
3323
3324 d_tracer = tracing_init_dentry();
3325
3326 if (!d_tracer)
3327 return NULL;
3328
3329 d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3330
3331 if (!d_percpu && !once) {
3332 once = 1;
3333 pr_warning("Could not create debugfs directory 'per_cpu'\n");
3334 return NULL;
3335 }
3336
3337 return d_percpu;
3338}
3339
3340static void tracing_init_debugfs_percpu(long cpu)
3341{
3342 struct dentry *d_percpu = tracing_dentry_percpu();
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003343 struct dentry *entry, *d_cpu;
3344 /* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3345 char cpu_dir[7];
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003346
3347 if (cpu > 999 || cpu < 0)
3348 return;
3349
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003350 sprintf(cpu_dir, "cpu%ld", cpu);
3351 d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3352 if (!d_cpu) {
3353 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3354 return;
3355 }
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003356
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003357 /* per cpu trace_pipe */
3358 entry = debugfs_create_file("trace_pipe", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003359 (void *) cpu, &tracing_pipe_fops);
3360 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003361 pr_warning("Could not create debugfs 'trace_pipe' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003362
3363 /* per cpu trace */
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003364 entry = debugfs_create_file("trace", 0444, d_cpu,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003365 (void *) cpu, &tracing_fops);
3366 if (!entry)
Frederic Weisbecker8656e7a2009-02-26 00:41:38 +01003367 pr_warning("Could not create debugfs 'trace' entry\n");
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003368}
3369
Steven Rostedt60a11772008-05-12 21:20:44 +02003370#ifdef CONFIG_FTRACE_SELFTEST
3371/* Let selftest have access to static functions in this file */
3372#include "trace_selftest.c"
3373#endif
3374
Steven Rostedt577b7852009-02-26 23:43:05 -05003375struct trace_option_dentry {
3376 struct tracer_opt *opt;
3377 struct tracer_flags *flags;
3378 struct dentry *entry;
3379};
3380
3381static ssize_t
3382trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3383 loff_t *ppos)
3384{
3385 struct trace_option_dentry *topt = filp->private_data;
3386 char *buf;
3387
3388 if (topt->flags->val & topt->opt->bit)
3389 buf = "1\n";
3390 else
3391 buf = "0\n";
3392
3393 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3394}
3395
3396static ssize_t
3397trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3398 loff_t *ppos)
3399{
3400 struct trace_option_dentry *topt = filp->private_data;
3401 unsigned long val;
3402 char buf[64];
3403 int ret;
3404
3405 if (cnt >= sizeof(buf))
3406 return -EINVAL;
3407
3408 if (copy_from_user(&buf, ubuf, cnt))
3409 return -EFAULT;
3410
3411 buf[cnt] = 0;
3412
3413 ret = strict_strtoul(buf, 10, &val);
3414 if (ret < 0)
3415 return ret;
3416
3417 ret = 0;
3418 switch (val) {
3419 case 0:
3420 /* do nothing if already cleared */
3421 if (!(topt->flags->val & topt->opt->bit))
3422 break;
3423
3424 mutex_lock(&trace_types_lock);
3425 if (current_trace->set_flag)
3426 ret = current_trace->set_flag(topt->flags->val,
3427 topt->opt->bit, 0);
3428 mutex_unlock(&trace_types_lock);
3429 if (ret)
3430 return ret;
3431 topt->flags->val &= ~topt->opt->bit;
3432 break;
3433 case 1:
3434 /* do nothing if already set */
3435 if (topt->flags->val & topt->opt->bit)
3436 break;
3437
3438 mutex_lock(&trace_types_lock);
3439 if (current_trace->set_flag)
3440 ret = current_trace->set_flag(topt->flags->val,
3441 topt->opt->bit, 1);
3442 mutex_unlock(&trace_types_lock);
3443 if (ret)
3444 return ret;
3445 topt->flags->val |= topt->opt->bit;
3446 break;
3447
3448 default:
3449 return -EINVAL;
3450 }
3451
3452 *ppos += cnt;
3453
3454 return cnt;
3455}
3456
3457
3458static const struct file_operations trace_options_fops = {
3459 .open = tracing_open_generic,
3460 .read = trace_options_read,
3461 .write = trace_options_write,
3462};
3463
Steven Rostedta8259072009-02-26 22:19:12 -05003464static ssize_t
3465trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3466 loff_t *ppos)
3467{
3468 long index = (long)filp->private_data;
3469 char *buf;
3470
3471 if (trace_flags & (1 << index))
3472 buf = "1\n";
3473 else
3474 buf = "0\n";
3475
3476 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3477}
3478
3479static ssize_t
3480trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3481 loff_t *ppos)
3482{
3483 long index = (long)filp->private_data;
3484 char buf[64];
3485 unsigned long val;
3486 int ret;
3487
3488 if (cnt >= sizeof(buf))
3489 return -EINVAL;
3490
3491 if (copy_from_user(&buf, ubuf, cnt))
3492 return -EFAULT;
3493
3494 buf[cnt] = 0;
3495
3496 ret = strict_strtoul(buf, 10, &val);
3497 if (ret < 0)
3498 return ret;
3499
3500 switch (val) {
3501 case 0:
3502 trace_flags &= ~(1 << index);
3503 break;
3504 case 1:
3505 trace_flags |= 1 << index;
3506 break;
3507
3508 default:
3509 return -EINVAL;
3510 }
3511
3512 *ppos += cnt;
3513
3514 return cnt;
3515}
3516
Steven Rostedta8259072009-02-26 22:19:12 -05003517static const struct file_operations trace_options_core_fops = {
3518 .open = tracing_open_generic,
3519 .read = trace_options_core_read,
3520 .write = trace_options_core_write,
3521};
3522
3523static struct dentry *trace_options_init_dentry(void)
3524{
3525 struct dentry *d_tracer;
3526 static struct dentry *t_options;
3527
3528 if (t_options)
3529 return t_options;
3530
3531 d_tracer = tracing_init_dentry();
3532 if (!d_tracer)
3533 return NULL;
3534
3535 t_options = debugfs_create_dir("options", d_tracer);
3536 if (!t_options) {
3537 pr_warning("Could not create debugfs directory 'options'\n");
3538 return NULL;
3539 }
3540
3541 return t_options;
3542}
3543
Steven Rostedt577b7852009-02-26 23:43:05 -05003544static void
3545create_trace_option_file(struct trace_option_dentry *topt,
3546 struct tracer_flags *flags,
3547 struct tracer_opt *opt)
3548{
3549 struct dentry *t_options;
3550 struct dentry *entry;
3551
3552 t_options = trace_options_init_dentry();
3553 if (!t_options)
3554 return;
3555
3556 topt->flags = flags;
3557 topt->opt = opt;
3558
3559 entry = debugfs_create_file(opt->name, 0644, t_options, topt,
3560 &trace_options_fops);
3561
3562 topt->entry = entry;
3563
3564}
3565
3566static struct trace_option_dentry *
3567create_trace_option_files(struct tracer *tracer)
3568{
3569 struct trace_option_dentry *topts;
3570 struct tracer_flags *flags;
3571 struct tracer_opt *opts;
3572 int cnt;
3573
3574 if (!tracer)
3575 return NULL;
3576
3577 flags = tracer->flags;
3578
3579 if (!flags || !flags->opts)
3580 return NULL;
3581
3582 opts = flags->opts;
3583
3584 for (cnt = 0; opts[cnt].name; cnt++)
3585 ;
3586
Steven Rostedt0cfe8242009-02-27 10:51:10 -05003587 topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
Steven Rostedt577b7852009-02-26 23:43:05 -05003588 if (!topts)
3589 return NULL;
3590
3591 for (cnt = 0; opts[cnt].name; cnt++)
3592 create_trace_option_file(&topts[cnt], flags,
3593 &opts[cnt]);
3594
3595 return topts;
3596}
3597
3598static void
3599destroy_trace_option_files(struct trace_option_dentry *topts)
3600{
3601 int cnt;
3602
3603 if (!topts)
3604 return;
3605
3606 for (cnt = 0; topts[cnt].opt; cnt++) {
3607 if (topts[cnt].entry)
3608 debugfs_remove(topts[cnt].entry);
3609 }
3610
3611 kfree(topts);
3612}
3613
Steven Rostedta8259072009-02-26 22:19:12 -05003614static struct dentry *
3615create_trace_option_core_file(const char *option, long index)
3616{
3617 struct dentry *t_options;
3618 struct dentry *entry;
3619
3620 t_options = trace_options_init_dentry();
3621 if (!t_options)
3622 return NULL;
3623
3624 entry = debugfs_create_file(option, 0644, t_options, (void *)index,
3625 &trace_options_core_fops);
3626
3627 return entry;
3628}
3629
3630static __init void create_trace_options_dir(void)
3631{
3632 struct dentry *t_options;
3633 struct dentry *entry;
3634 int i;
3635
3636 t_options = trace_options_init_dentry();
3637 if (!t_options)
3638 return;
3639
3640 for (i = 0; trace_options[i]; i++) {
3641 entry = create_trace_option_core_file(trace_options[i], i);
3642 if (!entry)
3643 pr_warning("Could not create debugfs %s entry\n",
3644 trace_options[i]);
3645 }
3646}
3647
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003648static __init int tracer_init_debugfs(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003649{
3650 struct dentry *d_tracer;
Steven Rostedt2cadf912008-12-01 22:20:19 -05003651 struct dentry *buffers;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003652 struct dentry *entry;
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003653 int cpu;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003654
3655 d_tracer = tracing_init_dentry();
3656
3657 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
3658 &global_trace, &tracing_ctrl_fops);
3659 if (!entry)
3660 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3661
Steven Rostedtee6bce52008-11-12 17:52:37 -05003662 entry = debugfs_create_file("trace_options", 0644, d_tracer,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003663 NULL, &tracing_iter_fops);
3664 if (!entry)
Steven Rostedtee6bce52008-11-12 17:52:37 -05003665 pr_warning("Could not create debugfs 'trace_options' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003666
Steven Rostedta8259072009-02-26 22:19:12 -05003667 create_trace_options_dir();
3668
Ingo Molnarc7078de2008-05-12 21:20:52 +02003669 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
3670 NULL, &tracing_cpumask_fops);
3671 if (!entry)
3672 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3673
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003674 entry = debugfs_create_file("trace", 0444, d_tracer,
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003675 (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003676 if (!entry)
3677 pr_warning("Could not create debugfs 'trace' entry\n");
3678
3679 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
3680 &global_trace, &show_traces_fops);
3681 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003682 pr_warning("Could not create debugfs 'available_tracers' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003683
3684 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
3685 &global_trace, &set_tracer_fops);
3686 if (!entry)
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003687 pr_warning("Could not create debugfs 'current_tracer' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003688
3689 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
3690 &tracing_max_latency,
3691 &tracing_max_lat_fops);
3692 if (!entry)
3693 pr_warning("Could not create debugfs "
3694 "'tracing_max_latency' entry\n");
3695
3696 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
3697 &tracing_thresh, &tracing_max_lat_fops);
3698 if (!entry)
3699 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003700 "'tracing_thresh' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02003701 entry = debugfs_create_file("README", 0644, d_tracer,
3702 NULL, &tracing_readme_fops);
3703 if (!entry)
3704 pr_warning("Could not create debugfs 'README' entry\n");
3705
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003706 entry = debugfs_create_file("trace_pipe", 0444, d_tracer,
3707 (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
Steven Rostedtb3806b42008-05-12 21:20:46 +02003708 if (!entry)
3709 pr_warning("Could not create debugfs "
Frédéric Weisbecker98a983a2008-08-15 21:08:22 +02003710 "'trace_pipe' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003711
Steven Rostedta94c80e2008-11-12 17:52:36 -05003712 entry = debugfs_create_file("buffer_size_kb", 0644, d_tracer,
Steven Rostedta98a3c32008-05-12 21:20:59 +02003713 &global_trace, &tracing_entries_fops);
3714 if (!entry)
3715 pr_warning("Could not create debugfs "
Steven Rostedta94c80e2008-11-12 17:52:36 -05003716 "'buffer_size_kb' entry\n");
Steven Rostedta98a3c32008-05-12 21:20:59 +02003717
Pekka Paalanen5bf9a1e2008-09-16 22:06:42 +03003718 entry = debugfs_create_file("trace_marker", 0220, d_tracer,
3719 NULL, &tracing_mark_fops);
3720 if (!entry)
3721 pr_warning("Could not create debugfs "
3722 "'trace_marker' entry\n");
3723
Steven Rostedt2cadf912008-12-01 22:20:19 -05003724 buffers = debugfs_create_dir("binary_buffers", d_tracer);
3725
3726 if (!buffers)
3727 pr_warning("Could not create buffers directory\n");
3728 else {
3729 int cpu;
3730 char buf[64];
3731
3732 for_each_tracing_cpu(cpu) {
3733 sprintf(buf, "%d", cpu);
3734
3735 entry = debugfs_create_file(buf, 0444, buffers,
3736 (void *)(long)cpu,
3737 &tracing_buffers_fops);
3738 if (!entry)
3739 pr_warning("Could not create debugfs buffers "
3740 "'%s' entry\n", buf);
3741 }
3742 }
3743
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003744#ifdef CONFIG_DYNAMIC_FTRACE
3745 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
3746 &ftrace_update_tot_cnt,
Steven Rostedtb807c3d2008-10-30 16:08:33 -04003747 &tracing_dyn_info_fops);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003748 if (!entry)
3749 pr_warning("Could not create debugfs "
3750 "'dyn_ftrace_total_info' entry\n");
3751#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02003752#ifdef CONFIG_SYSPROF_TRACER
3753 init_tracer_sysprof_debugfs(d_tracer);
3754#endif
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003755
3756 for_each_tracing_cpu(cpu)
3757 tracing_init_debugfs_percpu(cpu);
3758
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01003759 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003760}
3761
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003762int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003763{
Peter Zijlstraefed7922009-03-04 12:32:55 +01003764 static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003765 static char trace_buf[TRACE_BUF_SIZE];
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003766
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003767 struct ring_buffer_event *event;
Peter Zijlstraf09ce572008-09-04 10:24:14 +02003768 struct trace_array *tr = &global_trace;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003769 struct trace_array_cpu *data;
Steven Rostedt38697052008-10-01 13:14:09 -04003770 int cpu, len = 0, size, pc;
Ingo Molnare726f5f92008-12-08 16:55:53 +01003771 struct print_entry *entry;
3772 unsigned long irq_flags;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003773
Frederic Weisbecker8e1b82e2008-12-06 03:41:33 +01003774 if (tracing_disabled || tracing_selftest_running)
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003775 return 0;
3776
Steven Rostedt38697052008-10-01 13:14:09 -04003777 pc = preempt_count();
3778 preempt_disable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003779 cpu = raw_smp_processor_id();
3780 data = tr->data[cpu];
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003781
Steven Rostedt3ea2e6d2008-10-04 02:01:00 -04003782 if (unlikely(atomic_read(&data->disabled)))
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003783 goto out;
3784
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003785 pause_graph_tracing();
Peter Zijlstraefed7922009-03-04 12:32:55 +01003786 raw_local_irq_save(irq_flags);
3787 __raw_spin_lock(&trace_buf_lock);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003788 len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003789
3790 len = min(len, TRACE_BUF_SIZE-1);
3791 trace_buf[len] = 0;
3792
Steven Rostedt777e2082008-09-29 23:02:42 -04003793 size = sizeof(*entry) + len + 1;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -02003794 event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003795 if (!event)
3796 goto out_unlock;
Steven Rostedt777e2082008-09-29 23:02:42 -04003797 entry = ring_buffer_event_data(event);
Steven Rostedt777e2082008-09-29 23:02:42 -04003798 entry->ip = ip;
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01003799 entry->depth = depth;
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003800
Steven Rostedt777e2082008-09-29 23:02:42 -04003801 memcpy(&entry->buf, trace_buf, len);
3802 entry->buf[len] = 0;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -02003803 ring_buffer_unlock_commit(tr->buffer, event);
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003804
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003805 out_unlock:
Peter Zijlstraefed7922009-03-04 12:32:55 +01003806 __raw_spin_unlock(&trace_buf_lock);
3807 raw_local_irq_restore(irq_flags);
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01003808 unpause_graph_tracing();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003809 out:
Steven Rostedt38697052008-10-01 13:14:09 -04003810 preempt_enable_notrace();
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003811
3812 return len;
3813}
Pekka Paalanen801fe402008-09-16 21:58:24 +03003814EXPORT_SYMBOL_GPL(trace_vprintk);
3815
3816int __ftrace_printk(unsigned long ip, const char *fmt, ...)
3817{
3818 int ret;
3819 va_list ap;
3820
3821 if (!(trace_flags & TRACE_ITER_PRINTK))
3822 return 0;
3823
3824 va_start(ap, fmt);
Frederic Weisbecker21a8c462008-12-04 23:51:23 +01003825 ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
Pekka Paalanen801fe402008-09-16 21:58:24 +03003826 va_end(ap);
3827 return ret;
3828}
Steven Rostedtdd0e5452008-08-01 12:26:41 -04003829EXPORT_SYMBOL_GPL(__ftrace_printk);
3830
Arnaldo Carvalho de Melo90112622009-01-23 12:06:23 -02003831int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3832{
3833 if (!(trace_flags & TRACE_ITER_PRINTK))
3834 return 0;
3835
3836 return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3837}
3838EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3839
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003840static int trace_panic_handler(struct notifier_block *this,
3841 unsigned long event, void *unused)
3842{
Steven Rostedt944ac422008-10-23 19:26:08 -04003843 if (ftrace_dump_on_oops)
3844 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003845 return NOTIFY_OK;
3846}
3847
3848static struct notifier_block trace_panic_notifier = {
3849 .notifier_call = trace_panic_handler,
3850 .next = NULL,
3851 .priority = 150 /* priority: INT_MAX >= x >= 0 */
3852};
3853
3854static int trace_die_handler(struct notifier_block *self,
3855 unsigned long val,
3856 void *data)
3857{
3858 switch (val) {
3859 case DIE_OOPS:
Steven Rostedt944ac422008-10-23 19:26:08 -04003860 if (ftrace_dump_on_oops)
3861 ftrace_dump();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003862 break;
3863 default:
3864 break;
3865 }
3866 return NOTIFY_OK;
3867}
3868
3869static struct notifier_block trace_die_notifier = {
3870 .notifier_call = trace_die_handler,
3871 .priority = 200
3872};
3873
3874/*
3875 * printk is set to max of 1024, we really don't need it that big.
3876 * Nothing should be printing 1000 characters anyway.
3877 */
3878#define TRACE_MAX_PRINT 1000
3879
3880/*
3881 * Define here KERN_TRACE so that we have one place to modify
3882 * it if we decide to change what log level the ftrace dump
3883 * should be at.
3884 */
Steven Rostedt428aee12009-01-14 12:24:42 -05003885#define KERN_TRACE KERN_EMERG
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003886
3887static void
3888trace_printk_seq(struct trace_seq *s)
3889{
3890 /* Probably should print a warning here. */
3891 if (s->len >= 1000)
3892 s->len = 1000;
3893
3894 /* should be zero ended, but we are paranoid. */
3895 s->buffer[s->len] = 0;
3896
3897 printk(KERN_TRACE "%s", s->buffer);
3898
Steven Rostedtf9520752009-03-02 14:04:40 -05003899 trace_seq_init(s);
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003900}
3901
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003902void ftrace_dump(void)
3903{
3904 static DEFINE_SPINLOCK(ftrace_dump_lock);
3905 /* use static because iter can be a bit big for the stack */
3906 static struct trace_iterator iter;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003907 static int dump_ran;
Steven Rostedtd7690412008-10-01 00:29:53 -04003908 unsigned long flags;
3909 int cnt = 0, cpu;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003910
3911 /* only one dump */
3912 spin_lock_irqsave(&ftrace_dump_lock, flags);
3913 if (dump_ran)
3914 goto out;
3915
3916 dump_ran = 1;
3917
3918 /* No turning back! */
Steven Rostedt0ee6b6c2009-01-14 14:50:19 -05003919 tracing_off();
Steven Rostedt81adbdc2008-10-23 09:33:02 -04003920 ftrace_kill();
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003921
Steven Rostedtd7690412008-10-01 00:29:53 -04003922 for_each_tracing_cpu(cpu) {
3923 atomic_inc(&global_trace.data[cpu]->disabled);
3924 }
3925
Török Edwinb54d3de2008-11-22 13:28:48 +02003926 /* don't look at user memory in panic mode */
3927 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
3928
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003929 printk(KERN_TRACE "Dumping ftrace buffer:\n");
3930
Steven Rostedte543ad72009-03-04 18:20:36 -05003931 /* Simulate the iterator */
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003932 iter.tr = &global_trace;
3933 iter.trace = current_trace;
Steven Rostedte543ad72009-03-04 18:20:36 -05003934 iter.cpu_file = TRACE_PIPE_ALL_CPU;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003935
3936 /*
3937 * We need to stop all tracing on all CPUS to read the
3938 * the next buffer. This is a bit expensive, but is
3939 * not done often. We fill all what we can read,
3940 * and then release the locks again.
3941 */
3942
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003943 while (!trace_empty(&iter)) {
3944
3945 if (!cnt)
3946 printk(KERN_TRACE "---------------------------------\n");
3947
3948 cnt++;
3949
3950 /* reset all but tr, trace, and overruns */
3951 memset(&iter.seq, 0,
3952 sizeof(struct trace_iterator) -
3953 offsetof(struct trace_iterator, seq));
3954 iter.iter_flags |= TRACE_FILE_LAT_FMT;
3955 iter.pos = -1;
3956
3957 if (find_next_entry_inc(&iter) != NULL) {
3958 print_trace_line(&iter);
3959 trace_consume(&iter);
3960 }
3961
3962 trace_printk_seq(&iter.seq);
3963 }
3964
3965 if (!cnt)
3966 printk(KERN_TRACE " (ftrace buffer empty)\n");
3967 else
3968 printk(KERN_TRACE "---------------------------------\n");
3969
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04003970 out:
3971 spin_unlock_irqrestore(&ftrace_dump_lock, flags);
3972}
3973
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003974__init static int tracer_alloc_buffers(void)
3975{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003976 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003977 int i;
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303978 int ret = -ENOMEM;
3979
3980 if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
3981 goto out;
3982
3983 if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
3984 goto out_free_buffer_mask;
3985
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003986 if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
3987 goto out_free_tracing_cpumask;
3988
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303989 cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
3990 cpumask_copy(tracing_cpumask, cpu_all_mask);
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01003991 cpumask_clear(tracing_reader_cpumask);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003992
Steven Rostedtab464282008-05-12 21:21:00 +02003993 /* TODO: make the number of buffers hot pluggable with CPUS */
Steven Rostedt3928a8a2008-09-29 23:02:41 -04003994 global_trace.buffer = ring_buffer_alloc(trace_buf_size,
3995 TRACE_BUFFER_FLAGS);
3996 if (!global_trace.buffer) {
3997 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
3998 WARN_ON(1);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10303999 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004000 }
4001 global_trace.entries = ring_buffer_size(global_trace.buffer);
4002
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304003
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004004#ifdef CONFIG_TRACER_MAX_TRACE
4005 max_tr.buffer = ring_buffer_alloc(trace_buf_size,
4006 TRACE_BUFFER_FLAGS);
4007 if (!max_tr.buffer) {
4008 printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4009 WARN_ON(1);
4010 ring_buffer_free(global_trace.buffer);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304011 goto out_free_cpumask;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004012 }
4013 max_tr.entries = ring_buffer_size(max_tr.buffer);
4014 WARN_ON(max_tr.entries != global_trace.entries);
4015#endif
4016
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004017 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02004018 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02004019 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004020 max_tr.data[i] = &per_cpu(max_data, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004021 }
4022
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004023 trace_init_cmdlines();
4024
Frédéric Weisbecker43a15382008-09-21 20:16:30 +02004025 register_tracer(&nop_trace);
Steven Rostedt79fb0762009-02-02 21:38:33 -05004026 current_trace = &nop_trace;
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004027#ifdef CONFIG_BOOT_TRACER
4028 register_tracer(&boot_tracer);
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004029#endif
Steven Rostedt60a11772008-05-12 21:20:44 +02004030 /* All seems OK, enable tracing */
4031 tracing_disabled = 0;
Steven Rostedt3928a8a2008-09-29 23:02:41 -04004032
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004033 atomic_notifier_chain_register(&panic_notifier_list,
4034 &trace_panic_notifier);
4035
4036 register_die_notifier(&trace_die_notifier);
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304037 ret = 0;
Steven Rostedt3f5a54e2008-07-30 22:36:46 -04004038
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304039out_free_cpumask:
Frederic Weisbeckerb04cc6b2009-02-25 03:22:28 +01004040 free_cpumask_var(tracing_reader_cpumask);
4041out_free_tracing_cpumask:
Rusty Russell9e01c1b2009-01-01 10:12:22 +10304042 free_cpumask_var(tracing_cpumask);
4043out_free_buffer_mask:
4044 free_cpumask_var(tracing_buffer_mask);
4045out:
4046 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02004047}
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004048
4049__init static int clear_boot_tracer(void)
4050{
4051 /*
4052 * The default tracer at boot buffer is an init section.
4053 * This function is called in lateinit. If we did not
4054 * find the boot tracer, then clear it out, to prevent
4055 * later registration from accessing the buffer that is
4056 * about to be freed.
4057 */
4058 if (!default_bootup_tracer)
4059 return 0;
4060
4061 printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4062 default_bootup_tracer);
4063 default_bootup_tracer = NULL;
4064
4065 return 0;
4066}
4067
Frédéric Weisbeckerb5ad3842008-09-23 11:34:32 +01004068early_initcall(tracer_alloc_buffers);
4069fs_initcall(tracer_init_debugfs);
Steven Rostedtb2821ae2009-02-02 21:38:32 -05004070late_initcall(clear_boot_tracer);