blob: 76dfe6d2466c376d61be5301c1b0c2340f153f5a [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
14#include <linux/utsrelease.h>
15#include <linux/kallsyms.h>
16#include <linux/seq_file.h>
17#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020018#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020019#include <linux/hardirq.h>
20#include <linux/linkage.h>
21#include <linux/uaccess.h>
22#include <linux/ftrace.h>
23#include <linux/module.h>
24#include <linux/percpu.h>
25#include <linux/ctype.h>
26#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020027#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/gfp.h>
29#include <linux/fs.h>
Abhishek Sagar76094a22008-05-28 00:03:18 +053030#include <linux/kprobes.h>
Steven Rostedt3eefae92008-05-12 21:21:04 +020031#include <linux/writeback.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020032
Ingo Molnar86387f72008-05-12 21:20:51 +020033#include <linux/stacktrace.h>
34
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020035#include "trace.h"
36
37unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
38unsigned long __read_mostly tracing_thresh;
39
Steven Rostedtab464282008-05-12 21:21:00 +020040static unsigned long __read_mostly tracing_nr_buffers;
41static cpumask_t __read_mostly tracing_buffer_mask;
42
43#define for_each_tracing_cpu(cpu) \
44 for_each_cpu_mask(cpu, tracing_buffer_mask)
45
Steven Rostedta98a3c32008-05-12 21:20:59 +020046static int trace_alloc_page(void);
47static int trace_free_page(void);
48
Steven Rostedt60a11772008-05-12 21:20:44 +020049static int tracing_disabled = 1;
50
Steven Rostedt3eefae92008-05-12 21:21:04 +020051static unsigned long tracing_pages_allocated;
52
Thomas Gleixner72829bc2008-05-23 21:37:28 +020053long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020054ns2usecs(cycle_t nsec)
55{
56 nsec += 500;
57 do_div(nsec, 1000);
58 return nsec;
59}
60
Ingo Molnare309b412008-05-12 21:20:51 +020061cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020062{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020063 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020064}
65
Steven Rostedt4fcdae82008-05-12 21:21:00 +020066/*
67 * The global_trace is the descriptor that holds the tracing
68 * buffers for the live tracing. For each CPU, it contains
69 * a link list of pages that will store trace entries. The
70 * page descriptor of the pages in the memory is used to hold
71 * the link list by linking the lru item in the page descriptor
72 * to each of the pages in the buffer per CPU.
73 *
74 * For each active CPU there is a data field that holds the
75 * pages for the buffer for that CPU. Each CPU has the same number
76 * of pages allocated for its buffer.
77 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020078static struct trace_array global_trace;
79
80static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
81
Steven Rostedt4fcdae82008-05-12 21:21:00 +020082/*
83 * The max_tr is used to snapshot the global_trace when a maximum
84 * latency is reached. Some tracers will use this to store a maximum
85 * trace while it continues examining live traces.
86 *
87 * The buffers for the max_tr are set up the same as the global_trace.
88 * When a snapshot is taken, the link list of the max_tr is swapped
89 * with the link list of the global_trace and the buffers are reset for
90 * the global_trace so the tracing can continue.
91 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020092static struct trace_array max_tr;
93
94static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
95
Steven Rostedt4fcdae82008-05-12 21:21:00 +020096/* tracer_enabled is used to toggle activation of a tracer */
Steven Rostedt26994ea2008-05-12 21:20:48 +020097static int tracer_enabled = 1;
Steven Rostedt4fcdae82008-05-12 21:21:00 +020098
Steven Rostedt60bc0802008-07-10 20:58:16 -040099/* function tracing enabled */
100int ftrace_function_enabled;
101
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200102/*
103 * trace_nr_entries is the number of entries that is allocated
104 * for a buffer. Note, the number of entries is always rounded
105 * to ENTRIES_PER_PAGE.
106 */
Ingo Molnar57422792008-05-12 21:20:51 +0200107static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200108
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200109/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200110static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200111
112/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200113static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200114
115/*
116 * max_tracer_type_len is used to simplify the allocating of
117 * buffers to read userspace tracer names. We keep track of
118 * the longest tracer name registered.
119 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200120static int max_tracer_type_len;
121
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200122/*
123 * trace_types_lock is used to protect the trace_types list.
124 * This lock is also used to keep user access serialized.
125 * Accesses from userspace will grab this lock while userspace
126 * activities happen inside the kernel.
127 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200128static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200129
130/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200131static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
132
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200133/* trace_flags holds iter_ctrl options */
Ingo Molnar4e655512008-05-12 21:20:52 +0200134unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
135
Ankita Garg2b1bce12008-06-09 14:10:25 +0530136static notrace void no_trace_init(struct trace_array *tr)
137{
138 int cpu;
139
Steven Rostedt60bc0802008-07-10 20:58:16 -0400140 ftrace_function_enabled = 0;
Ankita Garg2b1bce12008-06-09 14:10:25 +0530141 if(tr->ctrl)
142 for_each_online_cpu(cpu)
143 tracing_reset(tr->data[cpu]);
144 tracer_enabled = 0;
145}
146
147/* dummy trace to disable tracing */
148static struct tracer no_tracer __read_mostly = {
149 .name = "none",
150 .init = no_trace_init
151};
152
153
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200154/**
155 * trace_wake_up - wake up tasks waiting for trace input
156 *
157 * Simply wakes up any task that is blocked on the trace_wait
158 * queue. These is used with trace_poll for tasks polling the trace.
159 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200160void trace_wake_up(void)
161{
Ingo Molnar017730c2008-05-12 21:20:52 +0200162 /*
163 * The runqueue_is_locked() can fail, but this is the best we
164 * have for now:
165 */
166 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200167 wake_up(&trace_wait);
168}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200169
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200170#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
171
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200172static int __init set_nr_entries(char *str)
173{
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200174 unsigned long nr_entries;
175 int ret;
176
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200177 if (!str)
178 return 0;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200179 ret = strict_strtoul(str, 0, &nr_entries);
180 /* nr_entries can not be zero */
181 if (ret < 0 || nr_entries == 0)
182 return 0;
183 trace_nr_entries = nr_entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200184 return 1;
185}
186__setup("trace_entries=", set_nr_entries);
187
Steven Rostedt57f50be2008-05-12 21:20:44 +0200188unsigned long nsecs_to_usecs(unsigned long nsecs)
189{
190 return nsecs / 1000;
191}
192
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200193/*
194 * trace_flag_type is an enumeration that holds different
195 * states when a trace occurs. These are:
196 * IRQS_OFF - interrupts were disabled
197 * NEED_RESCED - reschedule is requested
198 * HARDIRQ - inside an interrupt handler
199 * SOFTIRQ - inside a softirq handler
200 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200201enum trace_flag_type {
202 TRACE_FLAG_IRQS_OFF = 0x01,
203 TRACE_FLAG_NEED_RESCHED = 0x02,
204 TRACE_FLAG_HARDIRQ = 0x04,
205 TRACE_FLAG_SOFTIRQ = 0x08,
206};
207
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200208/*
209 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
210 * control the output of kernel symbols.
211 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200212#define TRACE_ITER_SYM_MASK \
213 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
214
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200215/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200216static const char *trace_options[] = {
217 "print-parent",
218 "sym-offset",
219 "sym-addr",
220 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200221 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200222 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200223 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200224 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200225 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200226 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200227 NULL
228};
229
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200230/*
231 * ftrace_max_lock is used to protect the swapping of buffers
232 * when taking a max snapshot. The buffers themselves are
233 * protected by per_cpu spinlocks. But the action of the swap
234 * needs its own lock.
235 *
236 * This is defined as a raw_spinlock_t in order to help
237 * with performance when lockdep debugging is enabled.
238 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200239static raw_spinlock_t ftrace_max_lock =
240 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200241
242/*
243 * Copy the new maximum trace into the separate maximum-trace
244 * structure. (this way the maximum trace is permanently saved,
245 * for later retrieval via /debugfs/tracing/latency_trace)
246 */
Ingo Molnare309b412008-05-12 21:20:51 +0200247static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200248__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
249{
250 struct trace_array_cpu *data = tr->data[cpu];
251
252 max_tr.cpu = cpu;
253 max_tr.time_start = data->preempt_timestamp;
254
255 data = max_tr.data[cpu];
256 data->saved_latency = tracing_max_latency;
257
258 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
259 data->pid = tsk->pid;
260 data->uid = tsk->uid;
261 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
262 data->policy = tsk->policy;
263 data->rt_priority = tsk->rt_priority;
264
265 /* record this tasks comm */
266 tracing_record_cmdline(current);
267}
268
Steven Rostedt19384c02008-05-22 00:22:16 -0400269#define CHECK_COND(cond) \
270 if (unlikely(cond)) { \
271 tracing_disabled = 1; \
272 WARN_ON(1); \
273 return -1; \
274 }
275
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200276/**
277 * check_pages - integrity check of trace buffers
278 *
279 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400280 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200281 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400282int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200283{
284 struct page *page, *tmp;
285
Steven Rostedt19384c02008-05-22 00:22:16 -0400286 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
287 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200288
289 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400290 CHECK_COND(page->lru.next->prev != &page->lru);
291 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200292 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400293
294 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200295}
296
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200297/**
298 * head_page - page address of the first page in per_cpu buffer.
299 *
300 * head_page returns the page address of the first page in
301 * a per_cpu buffer. This also preforms various consistency
302 * checks to make sure the buffer has not been corrupted.
303 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200304void *head_page(struct trace_array_cpu *data)
305{
306 struct page *page;
307
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200308 if (list_empty(&data->trace_pages))
309 return NULL;
310
311 page = list_entry(data->trace_pages.next, struct page, lru);
312 BUG_ON(&page->lru == &data->trace_pages);
313
314 return page_address(page);
315}
316
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200317/**
318 * trace_seq_printf - sequence printing of trace information
319 * @s: trace sequence descriptor
320 * @fmt: printf format string
321 *
322 * The tracer may use either sequence operations or its own
323 * copy to user routines. To simplify formating of a trace
324 * trace_seq_printf is used to store strings into a special
325 * buffer (@s). Then the output may be either used by
326 * the sequencer or pulled into another buffer.
327 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200328int
Steven Rostedt214023c2008-05-12 21:20:46 +0200329trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
330{
331 int len = (PAGE_SIZE - 1) - s->len;
332 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200333 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200334
335 if (!len)
336 return 0;
337
338 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200339 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200340 va_end(ap);
341
Steven Rostedtb3806b42008-05-12 21:20:46 +0200342 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200343 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200344 return 0;
345
346 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200347
348 return len;
349}
350
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200351/**
352 * trace_seq_puts - trace sequence printing of simple string
353 * @s: trace sequence descriptor
354 * @str: simple string to record
355 *
356 * The tracer may use either the sequence operations or its own
357 * copy to user routines. This function records a simple string
358 * into a special buffer (@s) for later retrieval by a sequencer
359 * or other mechanism.
360 */
Ingo Molnare309b412008-05-12 21:20:51 +0200361static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200362trace_seq_puts(struct trace_seq *s, const char *str)
363{
364 int len = strlen(str);
365
366 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200367 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200368
369 memcpy(s->buffer + s->len, str, len);
370 s->len += len;
371
372 return len;
373}
374
Ingo Molnare309b412008-05-12 21:20:51 +0200375static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200376trace_seq_putc(struct trace_seq *s, unsigned char c)
377{
378 if (s->len >= (PAGE_SIZE - 1))
379 return 0;
380
381 s->buffer[s->len++] = c;
382
383 return 1;
384}
385
Ingo Molnare309b412008-05-12 21:20:51 +0200386static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200387trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
388{
389 if (len > ((PAGE_SIZE - 1) - s->len))
390 return 0;
391
392 memcpy(s->buffer + s->len, mem, len);
393 s->len += len;
394
395 return len;
396}
397
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200398#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200399static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200400
Ingo Molnare309b412008-05-12 21:20:51 +0200401static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200402trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
403{
404 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200405 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200406 unsigned char byte;
407 int i, j;
408
409 BUG_ON(len >= HEX_CHARS);
410
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200411#ifdef __BIG_ENDIAN
412 for (i = 0, j = 0; i < len; i++) {
413#else
414 for (i = len-1, j = 0; i >= 0; i--) {
415#endif
416 byte = data[i];
417
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200418 hex[j++] = hex2asc[byte & 0x0f];
419 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200420 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200421 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200422
423 return trace_seq_putmem(s, hex, j);
424}
425
Ingo Molnare309b412008-05-12 21:20:51 +0200426static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200427trace_seq_reset(struct trace_seq *s)
428{
429 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200430 s->readpos = 0;
431}
432
433ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
434{
435 int len;
436 int ret;
437
438 if (s->len <= s->readpos)
439 return -EBUSY;
440
441 len = s->len - s->readpos;
442 if (cnt > len)
443 cnt = len;
444 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
445 if (ret)
446 return -EFAULT;
447
448 s->readpos += len;
449 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200450}
451
Ingo Molnare309b412008-05-12 21:20:51 +0200452static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200453trace_print_seq(struct seq_file *m, struct trace_seq *s)
454{
455 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
456
457 s->buffer[len] = 0;
458 seq_puts(m, s->buffer);
459
460 trace_seq_reset(s);
461}
462
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200463/*
464 * flip the trace buffers between two trace descriptors.
465 * This usually is the buffers between the global_trace and
466 * the max_tr to record a snapshot of a current trace.
467 *
468 * The ftrace_max_lock must be held.
469 */
Ingo Molnare309b412008-05-12 21:20:51 +0200470static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200471flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
472{
473 struct list_head flip_pages;
474
475 INIT_LIST_HEAD(&flip_pages);
476
Steven Rostedt93a588f2008-05-12 21:20:45 +0200477 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200478 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200479 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200480
481 check_pages(tr1);
482 check_pages(tr2);
483 list_splice_init(&tr1->trace_pages, &flip_pages);
484 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
485 list_splice_init(&flip_pages, &tr2->trace_pages);
486 BUG_ON(!list_empty(&flip_pages));
487 check_pages(tr1);
488 check_pages(tr2);
489}
490
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200491/**
492 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
493 * @tr: tracer
494 * @tsk: the task with the latency
495 * @cpu: The cpu that initiated the trace.
496 *
497 * Flip the buffers between the @tr and the max_tr and record information
498 * about which task was the cause of this latency.
499 */
Ingo Molnare309b412008-05-12 21:20:51 +0200500void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
502{
503 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200504 int i;
505
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200506 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200507 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200508 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200509 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200510 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200511 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200512 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200513 }
514
515 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200516 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200517}
518
519/**
520 * update_max_tr_single - only copy one trace over, and reset the rest
521 * @tr - tracer
522 * @tsk - task with the latency
523 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200524 *
525 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200526 */
Ingo Molnare309b412008-05-12 21:20:51 +0200527void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200528update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
529{
530 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200531 int i;
532
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200533 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200534 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200535 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200536 tracing_reset(max_tr.data[i]);
537
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200538 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200539 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200540
541 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200542 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200543}
544
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200545/**
546 * register_tracer - register a tracer with the ftrace system.
547 * @type - the plugin for the tracer
548 *
549 * Register a new plugin tracer.
550 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200551int register_tracer(struct tracer *type)
552{
553 struct tracer *t;
554 int len;
555 int ret = 0;
556
557 if (!type->name) {
558 pr_info("Tracer must have a name\n");
559 return -1;
560 }
561
562 mutex_lock(&trace_types_lock);
563 for (t = trace_types; t; t = t->next) {
564 if (strcmp(type->name, t->name) == 0) {
565 /* already found */
566 pr_info("Trace %s already registered\n",
567 type->name);
568 ret = -1;
569 goto out;
570 }
571 }
572
Steven Rostedt60a11772008-05-12 21:20:44 +0200573#ifdef CONFIG_FTRACE_STARTUP_TEST
574 if (type->selftest) {
575 struct tracer *saved_tracer = current_trace;
576 struct trace_array_cpu *data;
577 struct trace_array *tr = &global_trace;
578 int saved_ctrl = tr->ctrl;
579 int i;
580 /*
581 * Run a selftest on this tracer.
582 * Here we reset the trace buffer, and set the current
583 * tracer to be this tracer. The tracer can then run some
584 * internal tracing to verify that everything is in order.
585 * If we fail, we do not register this tracer.
586 */
Steven Rostedtab464282008-05-12 21:21:00 +0200587 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200588 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200589 if (!head_page(data))
590 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200591 tracing_reset(data);
592 }
593 current_trace = type;
594 tr->ctrl = 0;
595 /* the test is responsible for initializing and enabling */
596 pr_info("Testing tracer %s: ", type->name);
597 ret = type->selftest(type, tr);
598 /* the test is responsible for resetting too */
599 current_trace = saved_tracer;
600 tr->ctrl = saved_ctrl;
601 if (ret) {
602 printk(KERN_CONT "FAILED!\n");
603 goto out;
604 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200605 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200606 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200607 data = tr->data[i];
608 if (!head_page(data))
609 continue;
610 tracing_reset(data);
611 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200612 printk(KERN_CONT "PASSED\n");
613 }
614#endif
615
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200616 type->next = trace_types;
617 trace_types = type;
618 len = strlen(type->name);
619 if (len > max_tracer_type_len)
620 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200621
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200622 out:
623 mutex_unlock(&trace_types_lock);
624
625 return ret;
626}
627
628void unregister_tracer(struct tracer *type)
629{
630 struct tracer **t;
631 int len;
632
633 mutex_lock(&trace_types_lock);
634 for (t = &trace_types; *t; t = &(*t)->next) {
635 if (*t == type)
636 goto found;
637 }
638 pr_info("Trace %s not registered\n", type->name);
639 goto out;
640
641 found:
642 *t = (*t)->next;
643 if (strlen(type->name) != max_tracer_type_len)
644 goto out;
645
646 max_tracer_type_len = 0;
647 for (t = &trace_types; *t; t = &(*t)->next) {
648 len = strlen((*t)->name);
649 if (len > max_tracer_type_len)
650 max_tracer_type_len = len;
651 }
652 out:
653 mutex_unlock(&trace_types_lock);
654}
655
Ingo Molnare309b412008-05-12 21:20:51 +0200656void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200657{
658 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200659 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200660 data->trace_head = data->trace_tail = head_page(data);
661 data->trace_head_idx = 0;
662 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200663}
664
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200665#define SAVED_CMDLINES 128
666static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
667static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
668static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
669static int cmdline_idx;
670static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200671
Steven Rostedt25b0b442008-05-12 21:21:00 +0200672/* temporary disable recording */
673atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200674
675static void trace_init_cmdlines(void)
676{
677 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
678 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
679 cmdline_idx = 0;
680}
681
Ingo Molnare309b412008-05-12 21:20:51 +0200682void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200683
Ingo Molnare309b412008-05-12 21:20:51 +0200684static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200685{
686 unsigned map;
687 unsigned idx;
688
689 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
690 return;
691
692 /*
693 * It's not the end of the world if we don't get
694 * the lock, but we also don't want to spin
695 * nor do we want to disable interrupts,
696 * so if we miss here, then better luck next time.
697 */
698 if (!spin_trylock(&trace_cmdline_lock))
699 return;
700
701 idx = map_pid_to_cmdline[tsk->pid];
702 if (idx >= SAVED_CMDLINES) {
703 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
704
705 map = map_cmdline_to_pid[idx];
706 if (map <= PID_MAX_DEFAULT)
707 map_pid_to_cmdline[map] = (unsigned)-1;
708
709 map_pid_to_cmdline[tsk->pid] = idx;
710
711 cmdline_idx = idx;
712 }
713
714 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
715
716 spin_unlock(&trace_cmdline_lock);
717}
718
Ingo Molnare309b412008-05-12 21:20:51 +0200719static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200720{
721 char *cmdline = "<...>";
722 unsigned map;
723
724 if (!pid)
725 return "<idle>";
726
727 if (pid > PID_MAX_DEFAULT)
728 goto out;
729
730 map = map_pid_to_cmdline[pid];
731 if (map >= SAVED_CMDLINES)
732 goto out;
733
734 cmdline = saved_cmdlines[map];
735
736 out:
737 return cmdline;
738}
739
Ingo Molnare309b412008-05-12 21:20:51 +0200740void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200741{
742 if (atomic_read(&trace_record_cmdline_disabled))
743 return;
744
745 trace_save_cmdline(tsk);
746}
747
Ingo Molnare309b412008-05-12 21:20:51 +0200748static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200749trace_next_list(struct trace_array_cpu *data, struct list_head *next)
750{
751 /*
752 * Roundrobin - but skip the head (which is not a real page):
753 */
754 next = next->next;
755 if (unlikely(next == &data->trace_pages))
756 next = next->next;
757 BUG_ON(next == &data->trace_pages);
758
759 return next;
760}
761
Ingo Molnare309b412008-05-12 21:20:51 +0200762static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200763trace_next_page(struct trace_array_cpu *data, void *addr)
764{
765 struct list_head *next;
766 struct page *page;
767
768 page = virt_to_page(addr);
769
770 next = trace_next_list(data, &page->lru);
771 page = list_entry(next, struct page, lru);
772
773 return page_address(page);
774}
775
Ingo Molnare309b412008-05-12 21:20:51 +0200776static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200777tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200778{
779 unsigned long idx, idx_next;
780 struct trace_entry *entry;
781
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200782 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200783 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200784 idx_next = idx + 1;
785
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200786 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
787
Steven Rostedt93a588f2008-05-12 21:20:45 +0200788 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200789
790 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200791 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200792 idx_next = 0;
793 }
794
Steven Rostedt93a588f2008-05-12 21:20:45 +0200795 if (data->trace_head == data->trace_tail &&
796 idx_next == data->trace_tail_idx) {
797 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200798 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200799 data->trace_tail_idx++;
800 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
801 data->trace_tail =
802 trace_next_page(data, data->trace_tail);
803 data->trace_tail_idx = 0;
804 }
805 }
806
807 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808
809 return entry;
810}
811
Ingo Molnare309b412008-05-12 21:20:51 +0200812static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200813tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200814{
815 struct task_struct *tsk = current;
816 unsigned long pc;
817
818 pc = preempt_count();
819
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400820 entry->field.preempt_count = pc & 0xff;
821 entry->field.pid = (tsk) ? tsk->pid : 0;
822 entry->field.t = ftrace_now(raw_smp_processor_id());
823 entry->field.flags =
824 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200825 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
826 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
827 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
828}
829
Ingo Molnare309b412008-05-12 21:20:51 +0200830void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200831trace_function(struct trace_array *tr, struct trace_array_cpu *data,
832 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200833{
834 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200835 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200836
Steven Rostedt92205c22008-05-12 21:20:55 +0200837 raw_local_irq_save(irq_flags);
838 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400839 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200840 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400841 entry->type = TRACE_FN;
842 entry->field.fn.ip = ip;
843 entry->field.fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200844 __raw_spin_unlock(&data->lock);
845 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200846}
847
Ingo Molnare309b412008-05-12 21:20:51 +0200848void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200849ftrace(struct trace_array *tr, struct trace_array_cpu *data,
850 unsigned long ip, unsigned long parent_ip, unsigned long flags)
851{
852 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200853 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200854}
855
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200856#ifdef CONFIG_MMIOTRACE
857void __trace_mmiotrace_rw(struct trace_array *tr, struct trace_array_cpu *data,
858 struct mmiotrace_rw *rw)
859{
860 struct trace_entry *entry;
861 unsigned long irq_flags;
862
Ingo Molnar801a1752008-05-12 21:20:58 +0200863 raw_local_irq_save(irq_flags);
864 __raw_spin_lock(&data->lock);
865
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400866 entry = tracing_get_trace_entry(tr, data);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200867 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400868 entry->type = TRACE_MMIO_RW;
869 entry->field.mmiorw = *rw;
Ingo Molnar801a1752008-05-12 21:20:58 +0200870
871 __raw_spin_unlock(&data->lock);
872 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200873
874 trace_wake_up();
875}
876
877void __trace_mmiotrace_map(struct trace_array *tr, struct trace_array_cpu *data,
878 struct mmiotrace_map *map)
879{
880 struct trace_entry *entry;
881 unsigned long irq_flags;
882
Ingo Molnar801a1752008-05-12 21:20:58 +0200883 raw_local_irq_save(irq_flags);
884 __raw_spin_lock(&data->lock);
885
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400886 entry = tracing_get_trace_entry(tr, data);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200887 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400888 entry->type = TRACE_MMIO_MAP;
889 entry->field.mmiomap = *map;
Ingo Molnar801a1752008-05-12 21:20:58 +0200890
891 __raw_spin_unlock(&data->lock);
892 raw_local_irq_restore(irq_flags);
Pekka Paalanenbd8ac682008-05-12 21:20:57 +0200893
894 trace_wake_up();
895}
896#endif
897
Ingo Molnar86387f72008-05-12 21:20:51 +0200898void __trace_stack(struct trace_array *tr,
899 struct trace_array_cpu *data,
900 unsigned long flags,
901 int skip)
902{
903 struct trace_entry *entry;
904 struct stack_trace trace;
905
906 if (!(trace_flags & TRACE_ITER_STACKTRACE))
907 return;
908
909 entry = tracing_get_trace_entry(tr, data);
910 tracing_generic_entry_update(entry, flags);
911 entry->type = TRACE_STACK;
912
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400913 memset(&entry->field.stack, 0, sizeof(entry->field.stack));
Ingo Molnar86387f72008-05-12 21:20:51 +0200914
915 trace.nr_entries = 0;
916 trace.max_entries = FTRACE_STACK_ENTRIES;
917 trace.skip = skip;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400918 trace.entries = entry->field.stack.caller;
Ingo Molnar86387f72008-05-12 21:20:51 +0200919
920 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200921}
922
Ingo Molnare309b412008-05-12 21:20:51 +0200923void
Ingo Molnara4feb832008-05-12 21:21:02 +0200924__trace_special(void *__tr, void *__data,
925 unsigned long arg1, unsigned long arg2, unsigned long arg3)
926{
927 struct trace_array_cpu *data = __data;
928 struct trace_array *tr = __tr;
929 struct trace_entry *entry;
930 unsigned long irq_flags;
931
932 raw_local_irq_save(irq_flags);
933 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400934 entry = tracing_get_trace_entry(tr, data);
Ingo Molnara4feb832008-05-12 21:21:02 +0200935 tracing_generic_entry_update(entry, 0);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400936 entry->type = TRACE_SPECIAL;
937 entry->field.special.arg1 = arg1;
938 entry->field.special.arg2 = arg2;
939 entry->field.special.arg3 = arg3;
Ingo Molnara4feb832008-05-12 21:21:02 +0200940 __trace_stack(tr, data, irq_flags, 4);
941 __raw_spin_unlock(&data->lock);
942 raw_local_irq_restore(irq_flags);
943
944 trace_wake_up();
945}
946
947void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200948tracing_sched_switch_trace(struct trace_array *tr,
949 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200950 struct task_struct *prev,
951 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200952 unsigned long flags)
953{
954 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200955 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200956
Steven Rostedt92205c22008-05-12 21:20:55 +0200957 raw_local_irq_save(irq_flags);
958 __raw_spin_lock(&data->lock);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400959 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200960 tracing_generic_entry_update(entry, flags);
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400961 entry->type = TRACE_CTX;
962 entry->field.ctx.prev_pid = prev->pid;
963 entry->field.ctx.prev_prio = prev->prio;
964 entry->field.ctx.prev_state = prev->state;
965 entry->field.ctx.next_pid = next->pid;
966 entry->field.ctx.next_prio = next->prio;
967 entry->field.ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200968 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200969 __raw_spin_unlock(&data->lock);
970 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200971}
972
Ingo Molnar57422792008-05-12 21:20:51 +0200973void
974tracing_sched_wakeup_trace(struct trace_array *tr,
975 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200976 struct task_struct *wakee,
977 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200978 unsigned long flags)
979{
980 struct trace_entry *entry;
981 unsigned long irq_flags;
982
Steven Rostedt92205c22008-05-12 21:20:55 +0200983 raw_local_irq_save(irq_flags);
984 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200985 entry = tracing_get_trace_entry(tr, data);
986 tracing_generic_entry_update(entry, flags);
987 entry->type = TRACE_WAKE;
Steven Rostedt2e2ca152008-08-01 12:26:40 -0400988 entry->field.ctx.prev_pid = curr->pid;
989 entry->field.ctx.prev_prio = curr->prio;
990 entry->field.ctx.prev_state = curr->state;
991 entry->field.ctx.next_pid = wakee->pid;
992 entry->field.ctx.next_prio = wakee->prio;
993 entry->field.ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200994 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200995 __raw_spin_unlock(&data->lock);
996 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200997
998 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200999}
1000
Steven Rostedt4902f882008-05-22 00:22:18 -04001001void
1002ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1003{
1004 struct trace_array *tr = &global_trace;
1005 struct trace_array_cpu *data;
1006 unsigned long flags;
1007 long disabled;
1008 int cpu;
1009
1010 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
1011 return;
1012
1013 local_irq_save(flags);
1014 cpu = raw_smp_processor_id();
1015 data = tr->data[cpu];
1016 disabled = atomic_inc_return(&data->disabled);
1017
1018 if (likely(disabled == 1))
1019 __trace_special(tr, data, arg1, arg2, arg3);
1020
1021 atomic_dec(&data->disabled);
1022 local_irq_restore(flags);
1023}
1024
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001025#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +02001026static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001027function_trace_call(unsigned long ip, unsigned long parent_ip)
1028{
1029 struct trace_array *tr = &global_trace;
1030 struct trace_array_cpu *data;
1031 unsigned long flags;
1032 long disabled;
1033 int cpu;
1034
Steven Rostedt60bc0802008-07-10 20:58:16 -04001035 if (unlikely(!ftrace_function_enabled))
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001036 return;
1037
Abhishek Sagarecea6562008-06-21 23:47:53 +05301038 if (skip_trace(ip))
1039 return;
1040
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001041 local_irq_save(flags);
1042 cpu = raw_smp_processor_id();
1043 data = tr->data[cpu];
1044 disabled = atomic_inc_return(&data->disabled);
1045
1046 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +02001047 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001048
1049 atomic_dec(&data->disabled);
1050 local_irq_restore(flags);
1051}
1052
1053static struct ftrace_ops trace_ops __read_mostly =
1054{
1055 .func = function_trace_call,
1056};
1057
Ingo Molnare309b412008-05-12 21:20:51 +02001058void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001059{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001060 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001061 register_ftrace_function(&trace_ops);
Steven Rostedt60bc0802008-07-10 20:58:16 -04001062 if (tracer_enabled)
1063 ftrace_function_enabled = 1;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001064}
1065
Ingo Molnare309b412008-05-12 21:20:51 +02001066void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001067{
Steven Rostedt60bc0802008-07-10 20:58:16 -04001068 ftrace_function_enabled = 0;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001069 unregister_ftrace_function(&trace_ops);
1070}
1071#endif
1072
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001073enum trace_file_type {
1074 TRACE_FILE_LAT_FMT = 1,
1075};
1076
1077static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001078trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1079 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001080{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001081 struct page *page;
1082 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001083
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001084 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001085 iter->next_idx[cpu] >= data->trace_idx ||
1086 (data->trace_head == data->trace_tail &&
1087 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001088 return NULL;
1089
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001090 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001091 /* Initialize the iterator for this cpu trace buffer */
1092 WARN_ON(!data->trace_tail);
1093 page = virt_to_page(data->trace_tail);
1094 iter->next_page[cpu] = &page->lru;
1095 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001096 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001097
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001098 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001099 BUG_ON(&data->trace_pages == &page->lru);
1100
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001101 array = page_address(page);
1102
Steven Rostedt93a588f2008-05-12 21:20:45 +02001103 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001104 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105}
1106
Ingo Molnare309b412008-05-12 21:20:51 +02001107static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001108find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1109{
1110 struct trace_array *tr = iter->tr;
1111 struct trace_entry *ent, *next = NULL;
1112 int next_cpu = -1;
1113 int cpu;
1114
Steven Rostedtab464282008-05-12 21:21:00 +02001115 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001116 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001117 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001118 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001119 /*
1120 * Pick the entry with the smallest timestamp:
1121 */
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001122 if (ent && (!next || ent->field.t < next->field.t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001123 next = ent;
1124 next_cpu = cpu;
1125 }
1126 }
1127
1128 if (ent_cpu)
1129 *ent_cpu = next_cpu;
1130
1131 return next;
1132}
1133
Ingo Molnare309b412008-05-12 21:20:51 +02001134static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001135{
1136 iter->idx++;
1137 iter->next_idx[iter->cpu]++;
1138 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001139
Steven Rostedtb3806b42008-05-12 21:20:46 +02001140 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1141 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1142
1143 iter->next_page_idx[iter->cpu] = 0;
1144 iter->next_page[iter->cpu] =
1145 trace_next_list(data, iter->next_page[iter->cpu]);
1146 }
1147}
1148
Ingo Molnare309b412008-05-12 21:20:51 +02001149static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001150{
1151 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1152
1153 data->trace_tail_idx++;
1154 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1155 data->trace_tail = trace_next_page(data, data->trace_tail);
1156 data->trace_tail_idx = 0;
1157 }
1158
1159 /* Check if we empty it, then reset the index */
1160 if (data->trace_head == data->trace_tail &&
1161 data->trace_head_idx == data->trace_tail_idx)
1162 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001163}
1164
Ingo Molnare309b412008-05-12 21:20:51 +02001165static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001166{
1167 struct trace_entry *next;
1168 int next_cpu = -1;
1169
1170 next = find_next_entry(iter, &next_cpu);
1171
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001172 iter->prev_ent = iter->ent;
1173 iter->prev_cpu = iter->cpu;
1174
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001175 iter->ent = next;
1176 iter->cpu = next_cpu;
1177
Steven Rostedtb3806b42008-05-12 21:20:46 +02001178 if (next)
1179 trace_iterator_increment(iter);
1180
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001181 return next ? iter : NULL;
1182}
1183
Ingo Molnare309b412008-05-12 21:20:51 +02001184static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185{
1186 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001187 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001188 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001189
1190 (*pos)++;
1191
1192 /* can't go backwards */
1193 if (iter->idx > i)
1194 return NULL;
1195
1196 if (iter->idx < 0)
1197 ent = find_next_entry_inc(iter);
1198 else
1199 ent = iter;
1200
1201 while (ent && iter->idx < i)
1202 ent = find_next_entry_inc(iter);
1203
1204 iter->pos = *pos;
1205
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001206 return ent;
1207}
1208
1209static void *s_start(struct seq_file *m, loff_t *pos)
1210{
1211 struct trace_iterator *iter = m->private;
1212 void *p = NULL;
1213 loff_t l = 0;
1214 int i;
1215
1216 mutex_lock(&trace_types_lock);
1217
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001218 if (!current_trace || current_trace != iter->trace) {
1219 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001220 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001221 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001222
1223 atomic_inc(&trace_record_cmdline_disabled);
1224
1225 /* let the tracer grab locks here if needed */
1226 if (current_trace->start)
1227 current_trace->start(iter);
1228
1229 if (*pos != iter->pos) {
1230 iter->ent = NULL;
1231 iter->cpu = 0;
1232 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001233 iter->prev_ent = NULL;
1234 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001235
Steven Rostedtab464282008-05-12 21:21:00 +02001236 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001237 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001238 iter->next_page[i] = NULL;
1239 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240
1241 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1242 ;
1243
1244 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001245 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001246 p = s_next(m, p, &l);
1247 }
1248
1249 return p;
1250}
1251
1252static void s_stop(struct seq_file *m, void *p)
1253{
1254 struct trace_iterator *iter = m->private;
1255
1256 atomic_dec(&trace_record_cmdline_disabled);
1257
1258 /* let the tracer release locks here if needed */
1259 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1260 iter->trace->stop(iter);
1261
1262 mutex_unlock(&trace_types_lock);
1263}
1264
Abhishek Sagar76094a22008-05-28 00:03:18 +05301265#define KRETPROBE_MSG "[unknown/kretprobe'd]"
1266
1267#ifdef CONFIG_KRETPROBES
1268static inline int kretprobed(unsigned long addr)
1269{
1270 return addr == (unsigned long)kretprobe_trampoline;
1271}
1272#else
1273static inline int kretprobed(unsigned long addr)
1274{
1275 return 0;
1276}
1277#endif /* CONFIG_KRETPROBES */
1278
Steven Rostedtb3806b42008-05-12 21:20:46 +02001279static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001280seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001281{
1282#ifdef CONFIG_KALLSYMS
1283 char str[KSYM_SYMBOL_LEN];
1284
1285 kallsyms_lookup(address, NULL, NULL, NULL, str);
1286
Steven Rostedtb3806b42008-05-12 21:20:46 +02001287 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001289 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290}
1291
Steven Rostedtb3806b42008-05-12 21:20:46 +02001292static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001293seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1294 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001295{
1296#ifdef CONFIG_KALLSYMS
1297 char str[KSYM_SYMBOL_LEN];
1298
1299 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001300 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001301#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001302 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001303}
1304
1305#ifndef CONFIG_64BIT
1306# define IP_FMT "%08lx"
1307#else
1308# define IP_FMT "%016lx"
1309#endif
1310
Ingo Molnare309b412008-05-12 21:20:51 +02001311static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001312seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001313{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001314 int ret;
1315
1316 if (!ip)
1317 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001318
1319 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001320 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001322 ret = seq_print_sym_short(s, "%s", ip);
1323
1324 if (!ret)
1325 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001326
1327 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001328 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1329 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001330}
1331
Ingo Molnare309b412008-05-12 21:20:51 +02001332static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001333{
1334 seq_puts(m, "# _------=> CPU# \n");
1335 seq_puts(m, "# / _-----=> irqs-off \n");
1336 seq_puts(m, "# | / _----=> need-resched \n");
1337 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1338 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1339 seq_puts(m, "# |||| / \n");
1340 seq_puts(m, "# ||||| delay \n");
1341 seq_puts(m, "# cmd pid ||||| time | caller \n");
1342 seq_puts(m, "# \\ / ||||| \\ | / \n");
1343}
1344
Ingo Molnare309b412008-05-12 21:20:51 +02001345static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001346{
1347 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1348 seq_puts(m, "# | | | | |\n");
1349}
1350
1351
Ingo Molnare309b412008-05-12 21:20:51 +02001352static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1354{
1355 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1356 struct trace_array *tr = iter->tr;
1357 struct trace_array_cpu *data = tr->data[tr->cpu];
1358 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001359 unsigned long total = 0;
1360 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001361 int cpu;
1362 const char *name = "preemption";
1363
1364 if (type)
1365 name = type->name;
1366
Steven Rostedtab464282008-05-12 21:21:00 +02001367 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001368 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001369 total += tr->data[cpu]->trace_idx;
1370 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001371 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001372 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001373 entries += tr->data[cpu]->trace_idx;
1374 }
1375 }
1376
1377 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1378 name, UTS_RELEASE);
1379 seq_puts(m, "-----------------------------------"
1380 "---------------------------------\n");
1381 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1382 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001383 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001385 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386 tr->cpu,
1387#if defined(CONFIG_PREEMPT_NONE)
1388 "server",
1389#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1390 "desktop",
Steven Rostedtb5c21b42008-07-10 20:58:12 -04001391#elif defined(CONFIG_PREEMPT)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001392 "preempt",
1393#else
1394 "unknown",
1395#endif
1396 /* These are reserved for later use */
1397 0, 0, 0, 0);
1398#ifdef CONFIG_SMP
1399 seq_printf(m, " #P:%d)\n", num_online_cpus());
1400#else
1401 seq_puts(m, ")\n");
1402#endif
1403 seq_puts(m, " -----------------\n");
1404 seq_printf(m, " | task: %.16s-%d "
1405 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1406 data->comm, data->pid, data->uid, data->nice,
1407 data->policy, data->rt_priority);
1408 seq_puts(m, " -----------------\n");
1409
1410 if (data->critical_start) {
1411 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001412 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1413 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001414 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001415 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1416 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001417 seq_puts(m, "\n");
1418 }
1419
1420 seq_puts(m, "\n");
1421}
1422
Ingo Molnare309b412008-05-12 21:20:51 +02001423static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001424lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001425{
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001426 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427 int hardirq, softirq;
1428 char *comm;
1429
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001430 comm = trace_find_cmdline(field->pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001431
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001432 trace_seq_printf(s, "%8.8s-%-5d ", comm, field->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001433 trace_seq_printf(s, "%d", cpu);
1434 trace_seq_printf(s, "%c%c",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001435 (field->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1436 ((field->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001437
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001438 hardirq = field->flags & TRACE_FLAG_HARDIRQ;
1439 softirq = field->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001440 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001441 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001442 } else {
1443 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001444 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001445 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001446 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001447 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001448 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001449 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001450 }
1451 }
1452
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001453 if (field->preempt_count)
1454 trace_seq_printf(s, "%x", field->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001456 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001457}
1458
1459unsigned long preempt_mark_thresh = 100;
1460
Ingo Molnare309b412008-05-12 21:20:51 +02001461static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001462lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001463 unsigned long rel_usecs)
1464{
Steven Rostedt214023c2008-05-12 21:20:46 +02001465 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001466 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001467 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001468 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001469 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001470 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001471 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001472}
1473
1474static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1475
Ingo Molnare309b412008-05-12 21:20:51 +02001476static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001477print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001478{
Steven Rostedt214023c2008-05-12 21:20:46 +02001479 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001480 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1481 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1482 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1483 struct trace_entry *entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001484 struct trace_field *field = &entry->field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001485 unsigned long abs_usecs;
1486 unsigned long rel_usecs;
1487 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001488 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001489 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001490 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001491
1492 if (!next_entry)
1493 next_entry = entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001494 rel_usecs = ns2usecs(next_entry->field.t - entry->field.t);
1495 abs_usecs = ns2usecs(entry->field.t - iter->tr->time_start);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001496
1497 if (verbose) {
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001498 comm = trace_find_cmdline(field->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001499 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1500 " %ld.%03ldms (+%ld.%03ldms): ",
1501 comm,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001502 field->pid, cpu, field->flags,
1503 field->preempt_count, trace_idx,
1504 ns2usecs(field->t),
Steven Rostedt214023c2008-05-12 21:20:46 +02001505 abs_usecs/1000,
1506 abs_usecs % 1000, rel_usecs/1000,
1507 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001508 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001509 lat_print_generic(s, entry, cpu);
1510 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001511 }
1512 switch (entry->type) {
1513 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001514 seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001515 trace_seq_puts(s, " (");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001516 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301517 trace_seq_puts(s, KRETPROBE_MSG);
1518 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001519 seq_print_ip_sym(s, field->fn.parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001520 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521 break;
1522 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001523 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001524 T = field->ctx.next_state < sizeof(state_to_char) ?
1525 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001526
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001527 state = field->ctx.prev_state ?
1528 __ffs(field->ctx.prev_state) + 1 : 0;
Ankita Gargd17d9692008-05-12 21:20:58 +02001529 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001530 comm = trace_find_cmdline(field->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001531 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001532 field->ctx.prev_pid,
1533 field->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001534 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001535 field->ctx.next_pid,
1536 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001537 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001538 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001539 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001540 trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001541 field->special.arg1,
1542 field->special.arg2,
1543 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001544 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001545 case TRACE_STACK:
1546 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1547 if (i)
1548 trace_seq_puts(s, " <= ");
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001549 seq_print_ip_sym(s, field->stack.caller[i], sym_flags);
Ingo Molnar86387f72008-05-12 21:20:51 +02001550 }
1551 trace_seq_puts(s, "\n");
1552 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001553 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001554 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001555 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001556 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001557}
1558
Ingo Molnare309b412008-05-12 21:20:51 +02001559static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001560{
Steven Rostedt214023c2008-05-12 21:20:46 +02001561 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001562 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001563 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001564 struct trace_field *field;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001565 unsigned long usec_rem;
1566 unsigned long long t;
1567 unsigned long secs;
1568 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001569 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001570 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001571 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001572
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001573 entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001574 field = &entry->field;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001575
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001576 comm = trace_find_cmdline(iter->ent->field.pid);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001577
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001578 t = ns2usecs(field->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001579 usec_rem = do_div(t, 1000000ULL);
1580 secs = (unsigned long)t;
1581
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001582 ret = trace_seq_printf(s, "%16s-%-5d ", comm, field->pid);
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001583 if (!ret)
1584 return 0;
1585 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1586 if (!ret)
1587 return 0;
1588 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1589 if (!ret)
1590 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001591
1592 switch (entry->type) {
1593 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001594 ret = seq_print_ip_sym(s, field->fn.ip, sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001595 if (!ret)
1596 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001597 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001598 field->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001599 ret = trace_seq_printf(s, " <-");
1600 if (!ret)
1601 return 0;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001602 if (kretprobed(field->fn.parent_ip))
Abhishek Sagar76094a22008-05-28 00:03:18 +05301603 ret = trace_seq_puts(s, KRETPROBE_MSG);
1604 else
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001605 ret = seq_print_ip_sym(s,
1606 field->fn.parent_ip,
Abhishek Sagar76094a22008-05-28 00:03:18 +05301607 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001608 if (!ret)
1609 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001610 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001611 ret = trace_seq_printf(s, "\n");
1612 if (!ret)
1613 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001614 break;
1615 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001616 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001617 S = field->ctx.prev_state < sizeof(state_to_char) ?
1618 state_to_char[field->ctx.prev_state] : 'X';
1619 T = field->ctx.next_state < sizeof(state_to_char) ?
1620 state_to_char[field->ctx.next_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001621 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001622 field->ctx.prev_pid,
1623 field->ctx.prev_prio,
Steven Rostedtb3806b42008-05-12 21:20:46 +02001624 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001625 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001626 field->ctx.next_pid,
1627 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001628 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001629 if (!ret)
1630 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001631 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001632 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001633 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001634 field->special.arg1,
1635 field->special.arg2,
1636 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001637 if (!ret)
1638 return 0;
1639 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001640 case TRACE_STACK:
1641 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1642 if (i) {
1643 ret = trace_seq_puts(s, " <= ");
1644 if (!ret)
1645 return 0;
1646 }
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001647 ret = seq_print_ip_sym(s, field->stack.caller[i],
Ingo Molnar86387f72008-05-12 21:20:51 +02001648 sym_flags);
1649 if (!ret)
1650 return 0;
1651 }
1652 ret = trace_seq_puts(s, "\n");
1653 if (!ret)
1654 return 0;
1655 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001656 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001657 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001658}
1659
Ingo Molnare309b412008-05-12 21:20:51 +02001660static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001661{
1662 struct trace_seq *s = &iter->seq;
1663 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001664 struct trace_field *field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001665 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001666 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001667
1668 entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001669 field = &entry->field;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001670
1671 ret = trace_seq_printf(s, "%d %d %llu ",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001672 field->pid, iter->cpu, field->t);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001673 if (!ret)
1674 return 0;
1675
1676 switch (entry->type) {
1677 case TRACE_FN:
1678 ret = trace_seq_printf(s, "%x %x\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001679 field->fn.ip,
1680 field->fn.parent_ip);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001681 if (!ret)
1682 return 0;
1683 break;
1684 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001685 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001686 S = field->ctx.prev_state < sizeof(state_to_char) ?
1687 state_to_char[field->ctx.prev_state] : 'X';
1688 T = field->ctx.next_state < sizeof(state_to_char) ?
1689 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001690 if (entry->type == TRACE_WAKE)
1691 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001692 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001693 field->ctx.prev_pid,
1694 field->ctx.prev_prio,
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001695 S,
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001696 field->ctx.next_pid,
1697 field->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001698 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001699 if (!ret)
1700 return 0;
1701 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001702 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001703 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001704 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001705 field->special.arg1,
1706 field->special.arg2,
1707 field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001708 if (!ret)
1709 return 0;
1710 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001711 }
1712 return 1;
1713}
1714
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001715#define SEQ_PUT_FIELD_RET(s, x) \
1716do { \
1717 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1718 return 0; \
1719} while (0)
1720
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001721#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1722do { \
1723 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1724 return 0; \
1725} while (0)
1726
Ingo Molnare309b412008-05-12 21:20:51 +02001727static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001728{
1729 struct trace_seq *s = &iter->seq;
1730 unsigned char newline = '\n';
1731 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001732 struct trace_field *field;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001733 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001734
1735 entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001736 field = &entry->field;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001737
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001738 SEQ_PUT_HEX_FIELD_RET(s, field->pid);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001739 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001740 SEQ_PUT_HEX_FIELD_RET(s, field->t);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001741
1742 switch (entry->type) {
1743 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001744 SEQ_PUT_HEX_FIELD_RET(s, field->fn.ip);
1745 SEQ_PUT_HEX_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001746 break;
1747 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001748 case TRACE_WAKE:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001749 S = field->ctx.prev_state < sizeof(state_to_char) ?
1750 state_to_char[field->ctx.prev_state] : 'X';
1751 T = field->ctx.next_state < sizeof(state_to_char) ?
1752 state_to_char[field->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001753 if (entry->type == TRACE_WAKE)
1754 S = '+';
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001755 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_pid);
1756 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.prev_prio);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001757 SEQ_PUT_HEX_FIELD_RET(s, S);
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001758 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_pid);
1759 SEQ_PUT_HEX_FIELD_RET(s, field->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001760 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001761 break;
1762 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001763 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001764 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg1);
1765 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg2);
1766 SEQ_PUT_HEX_FIELD_RET(s, field->special.arg3);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001767 break;
1768 }
1769 SEQ_PUT_FIELD_RET(s, newline);
1770
1771 return 1;
1772}
1773
Ingo Molnare309b412008-05-12 21:20:51 +02001774static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001775{
1776 struct trace_seq *s = &iter->seq;
1777 struct trace_entry *entry;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001778 struct trace_field *field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001779
1780 entry = iter->ent;
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001781 field = &entry->field;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001782
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001783 SEQ_PUT_FIELD_RET(s, field->pid);
1784 SEQ_PUT_FIELD_RET(s, field->cpu);
1785 SEQ_PUT_FIELD_RET(s, field->t);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001786
1787 switch (entry->type) {
1788 case TRACE_FN:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001789 SEQ_PUT_FIELD_RET(s, field->fn.ip);
1790 SEQ_PUT_FIELD_RET(s, field->fn.parent_ip);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001791 break;
1792 case TRACE_CTX:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001793 SEQ_PUT_FIELD_RET(s, field->ctx.prev_pid);
1794 SEQ_PUT_FIELD_RET(s, field->ctx.prev_prio);
1795 SEQ_PUT_FIELD_RET(s, field->ctx.prev_state);
1796 SEQ_PUT_FIELD_RET(s, field->ctx.next_pid);
1797 SEQ_PUT_FIELD_RET(s, field->ctx.next_prio);
1798 SEQ_PUT_FIELD_RET(s, field->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001799 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001800 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001801 case TRACE_STACK:
Steven Rostedt2e2ca152008-08-01 12:26:40 -04001802 SEQ_PUT_FIELD_RET(s, field->special.arg1);
1803 SEQ_PUT_FIELD_RET(s, field->special.arg2);
1804 SEQ_PUT_FIELD_RET(s, field->special.arg3);
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001805 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001806 }
1807 return 1;
1808}
1809
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001810static int trace_empty(struct trace_iterator *iter)
1811{
1812 struct trace_array_cpu *data;
1813 int cpu;
1814
Steven Rostedtab464282008-05-12 21:21:00 +02001815 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001816 data = iter->tr->data[cpu];
1817
Steven Rostedtb3806b42008-05-12 21:20:46 +02001818 if (head_page(data) && data->trace_idx &&
1819 (data->trace_tail != data->trace_head ||
1820 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001821 return 0;
1822 }
1823 return 1;
1824}
1825
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001826static int print_trace_line(struct trace_iterator *iter)
1827{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001828 if (iter->trace && iter->trace->print_line)
1829 return iter->trace->print_line(iter);
1830
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001831 if (trace_flags & TRACE_ITER_BIN)
1832 return print_bin_fmt(iter);
1833
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001834 if (trace_flags & TRACE_ITER_HEX)
1835 return print_hex_fmt(iter);
1836
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001837 if (trace_flags & TRACE_ITER_RAW)
1838 return print_raw_fmt(iter);
1839
1840 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1841 return print_lat_fmt(iter, iter->idx, iter->cpu);
1842
1843 return print_trace_fmt(iter);
1844}
1845
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001846static int s_show(struct seq_file *m, void *v)
1847{
1848 struct trace_iterator *iter = v;
1849
1850 if (iter->ent == NULL) {
1851 if (iter->tr) {
1852 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1853 seq_puts(m, "#\n");
1854 }
1855 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1856 /* print nothing if the buffers are empty */
1857 if (trace_empty(iter))
1858 return 0;
1859 print_trace_header(m, iter);
1860 if (!(trace_flags & TRACE_ITER_VERBOSE))
1861 print_lat_help_header(m);
1862 } else {
1863 if (!(trace_flags & TRACE_ITER_VERBOSE))
1864 print_func_help_header(m);
1865 }
1866 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001867 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001868 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001869 }
1870
1871 return 0;
1872}
1873
1874static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001875 .start = s_start,
1876 .next = s_next,
1877 .stop = s_stop,
1878 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001879};
1880
Ingo Molnare309b412008-05-12 21:20:51 +02001881static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001882__tracing_open(struct inode *inode, struct file *file, int *ret)
1883{
1884 struct trace_iterator *iter;
1885
Steven Rostedt60a11772008-05-12 21:20:44 +02001886 if (tracing_disabled) {
1887 *ret = -ENODEV;
1888 return NULL;
1889 }
1890
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001891 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1892 if (!iter) {
1893 *ret = -ENOMEM;
1894 goto out;
1895 }
1896
1897 mutex_lock(&trace_types_lock);
1898 if (current_trace && current_trace->print_max)
1899 iter->tr = &max_tr;
1900 else
1901 iter->tr = inode->i_private;
1902 iter->trace = current_trace;
1903 iter->pos = -1;
1904
1905 /* TODO stop tracer */
1906 *ret = seq_open(file, &tracer_seq_ops);
1907 if (!*ret) {
1908 struct seq_file *m = file->private_data;
1909 m->private = iter;
1910
1911 /* stop the trace while dumping */
Steven Rostedt60bc0802008-07-10 20:58:16 -04001912 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001913 tracer_enabled = 0;
Steven Rostedt60bc0802008-07-10 20:58:16 -04001914 ftrace_function_enabled = 0;
1915 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001916
1917 if (iter->trace && iter->trace->open)
1918 iter->trace->open(iter);
1919 } else {
1920 kfree(iter);
1921 iter = NULL;
1922 }
1923 mutex_unlock(&trace_types_lock);
1924
1925 out:
1926 return iter;
1927}
1928
1929int tracing_open_generic(struct inode *inode, struct file *filp)
1930{
Steven Rostedt60a11772008-05-12 21:20:44 +02001931 if (tracing_disabled)
1932 return -ENODEV;
1933
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001934 filp->private_data = inode->i_private;
1935 return 0;
1936}
1937
1938int tracing_release(struct inode *inode, struct file *file)
1939{
1940 struct seq_file *m = (struct seq_file *)file->private_data;
1941 struct trace_iterator *iter = m->private;
1942
1943 mutex_lock(&trace_types_lock);
1944 if (iter->trace && iter->trace->close)
1945 iter->trace->close(iter);
1946
1947 /* reenable tracing if it was previously enabled */
Steven Rostedt60bc0802008-07-10 20:58:16 -04001948 if (iter->tr->ctrl) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001949 tracer_enabled = 1;
Steven Rostedt60bc0802008-07-10 20:58:16 -04001950 /*
1951 * It is safe to enable function tracing even if it
1952 * isn't used
1953 */
1954 ftrace_function_enabled = 1;
1955 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001956 mutex_unlock(&trace_types_lock);
1957
1958 seq_release(inode, file);
1959 kfree(iter);
1960 return 0;
1961}
1962
1963static int tracing_open(struct inode *inode, struct file *file)
1964{
1965 int ret;
1966
1967 __tracing_open(inode, file, &ret);
1968
1969 return ret;
1970}
1971
1972static int tracing_lt_open(struct inode *inode, struct file *file)
1973{
1974 struct trace_iterator *iter;
1975 int ret;
1976
1977 iter = __tracing_open(inode, file, &ret);
1978
1979 if (!ret)
1980 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1981
1982 return ret;
1983}
1984
1985
Ingo Molnare309b412008-05-12 21:20:51 +02001986static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001987t_next(struct seq_file *m, void *v, loff_t *pos)
1988{
1989 struct tracer *t = m->private;
1990
1991 (*pos)++;
1992
1993 if (t)
1994 t = t->next;
1995
1996 m->private = t;
1997
1998 return t;
1999}
2000
2001static void *t_start(struct seq_file *m, loff_t *pos)
2002{
2003 struct tracer *t = m->private;
2004 loff_t l = 0;
2005
2006 mutex_lock(&trace_types_lock);
2007 for (; t && l < *pos; t = t_next(m, t, &l))
2008 ;
2009
2010 return t;
2011}
2012
2013static void t_stop(struct seq_file *m, void *p)
2014{
2015 mutex_unlock(&trace_types_lock);
2016}
2017
2018static int t_show(struct seq_file *m, void *v)
2019{
2020 struct tracer *t = v;
2021
2022 if (!t)
2023 return 0;
2024
2025 seq_printf(m, "%s", t->name);
2026 if (t->next)
2027 seq_putc(m, ' ');
2028 else
2029 seq_putc(m, '\n');
2030
2031 return 0;
2032}
2033
2034static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002035 .start = t_start,
2036 .next = t_next,
2037 .stop = t_stop,
2038 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002039};
2040
2041static int show_traces_open(struct inode *inode, struct file *file)
2042{
2043 int ret;
2044
Steven Rostedt60a11772008-05-12 21:20:44 +02002045 if (tracing_disabled)
2046 return -ENODEV;
2047
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002048 ret = seq_open(file, &show_traces_seq_ops);
2049 if (!ret) {
2050 struct seq_file *m = file->private_data;
2051 m->private = trace_types;
2052 }
2053
2054 return ret;
2055}
2056
2057static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002058 .open = tracing_open,
2059 .read = seq_read,
2060 .llseek = seq_lseek,
2061 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062};
2063
2064static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002065 .open = tracing_lt_open,
2066 .read = seq_read,
2067 .llseek = seq_lseek,
2068 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002069};
2070
2071static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002072 .open = show_traces_open,
2073 .read = seq_read,
2074 .release = seq_release,
2075};
2076
Ingo Molnar36dfe922008-05-12 21:20:52 +02002077/*
2078 * Only trace on a CPU if the bitmask is set:
2079 */
2080static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2081
2082/*
2083 * When tracing/tracing_cpu_mask is modified then this holds
2084 * the new bitmask we are about to install:
2085 */
2086static cpumask_t tracing_cpumask_new;
2087
2088/*
2089 * The tracer itself will not take this lock, but still we want
2090 * to provide a consistent cpumask to user-space:
2091 */
2092static DEFINE_MUTEX(tracing_cpumask_update_lock);
2093
2094/*
2095 * Temporary storage for the character representation of the
2096 * CPU bitmask (and one more byte for the newline):
2097 */
2098static char mask_str[NR_CPUS + 1];
2099
Ingo Molnarc7078de2008-05-12 21:20:52 +02002100static ssize_t
2101tracing_cpumask_read(struct file *filp, char __user *ubuf,
2102 size_t count, loff_t *ppos)
2103{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002104 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002105
2106 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002107
2108 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2109 if (count - len < 2) {
2110 count = -EINVAL;
2111 goto out_err;
2112 }
2113 len += sprintf(mask_str + len, "\n");
2114 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2115
2116out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002117 mutex_unlock(&tracing_cpumask_update_lock);
2118
2119 return count;
2120}
2121
2122static ssize_t
2123tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2124 size_t count, loff_t *ppos)
2125{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002126 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002127
2128 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002129 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2130 if (err)
2131 goto err_unlock;
2132
Steven Rostedt92205c22008-05-12 21:20:55 +02002133 raw_local_irq_disable();
2134 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002135 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002136 /*
2137 * Increase/decrease the disabled counter if we are
2138 * about to flip a bit in the cpumask:
2139 */
2140 if (cpu_isset(cpu, tracing_cpumask) &&
2141 !cpu_isset(cpu, tracing_cpumask_new)) {
2142 atomic_inc(&global_trace.data[cpu]->disabled);
2143 }
2144 if (!cpu_isset(cpu, tracing_cpumask) &&
2145 cpu_isset(cpu, tracing_cpumask_new)) {
2146 atomic_dec(&global_trace.data[cpu]->disabled);
2147 }
2148 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002149 __raw_spin_unlock(&ftrace_max_lock);
2150 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002151
2152 tracing_cpumask = tracing_cpumask_new;
2153
Ingo Molnarc7078de2008-05-12 21:20:52 +02002154 mutex_unlock(&tracing_cpumask_update_lock);
2155
Ingo Molnarc7078de2008-05-12 21:20:52 +02002156 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002157
2158err_unlock:
2159 mutex_unlock(&tracing_cpumask_update_lock);
2160
2161 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002162}
2163
2164static struct file_operations tracing_cpumask_fops = {
2165 .open = tracing_open_generic,
2166 .read = tracing_cpumask_read,
2167 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002168};
2169
2170static ssize_t
2171tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2172 size_t cnt, loff_t *ppos)
2173{
2174 char *buf;
2175 int r = 0;
2176 int len = 0;
2177 int i;
2178
2179 /* calulate max size */
2180 for (i = 0; trace_options[i]; i++) {
2181 len += strlen(trace_options[i]);
2182 len += 3; /* "no" and space */
2183 }
2184
2185 /* +2 for \n and \0 */
2186 buf = kmalloc(len + 2, GFP_KERNEL);
2187 if (!buf)
2188 return -ENOMEM;
2189
2190 for (i = 0; trace_options[i]; i++) {
2191 if (trace_flags & (1 << i))
2192 r += sprintf(buf + r, "%s ", trace_options[i]);
2193 else
2194 r += sprintf(buf + r, "no%s ", trace_options[i]);
2195 }
2196
2197 r += sprintf(buf + r, "\n");
2198 WARN_ON(r >= len + 2);
2199
Ingo Molnar36dfe922008-05-12 21:20:52 +02002200 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002201
2202 kfree(buf);
2203
2204 return r;
2205}
2206
2207static ssize_t
2208tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2209 size_t cnt, loff_t *ppos)
2210{
2211 char buf[64];
2212 char *cmp = buf;
2213 int neg = 0;
2214 int i;
2215
Steven Rostedtcffae432008-05-12 21:21:00 +02002216 if (cnt >= sizeof(buf))
2217 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002218
2219 if (copy_from_user(&buf, ubuf, cnt))
2220 return -EFAULT;
2221
2222 buf[cnt] = 0;
2223
2224 if (strncmp(buf, "no", 2) == 0) {
2225 neg = 1;
2226 cmp += 2;
2227 }
2228
2229 for (i = 0; trace_options[i]; i++) {
2230 int len = strlen(trace_options[i]);
2231
2232 if (strncmp(cmp, trace_options[i], len) == 0) {
2233 if (neg)
2234 trace_flags &= ~(1 << i);
2235 else
2236 trace_flags |= (1 << i);
2237 break;
2238 }
2239 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002240 /*
2241 * If no option could be set, return an error:
2242 */
2243 if (!trace_options[i])
2244 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002245
2246 filp->f_pos += cnt;
2247
2248 return cnt;
2249}
2250
2251static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002252 .open = tracing_open_generic,
2253 .read = tracing_iter_ctrl_read,
2254 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002255};
2256
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002257static const char readme_msg[] =
2258 "tracing mini-HOWTO:\n\n"
2259 "# mkdir /debug\n"
2260 "# mount -t debugfs nodev /debug\n\n"
2261 "# cat /debug/tracing/available_tracers\n"
2262 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2263 "# cat /debug/tracing/current_tracer\n"
2264 "none\n"
2265 "# echo sched_switch > /debug/tracing/current_tracer\n"
2266 "# cat /debug/tracing/current_tracer\n"
2267 "sched_switch\n"
2268 "# cat /debug/tracing/iter_ctrl\n"
2269 "noprint-parent nosym-offset nosym-addr noverbose\n"
2270 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2271 "# echo 1 > /debug/tracing/tracing_enabled\n"
2272 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2273 "echo 0 > /debug/tracing/tracing_enabled\n"
2274;
2275
2276static ssize_t
2277tracing_readme_read(struct file *filp, char __user *ubuf,
2278 size_t cnt, loff_t *ppos)
2279{
2280 return simple_read_from_buffer(ubuf, cnt, ppos,
2281 readme_msg, strlen(readme_msg));
2282}
2283
2284static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002285 .open = tracing_open_generic,
2286 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002287};
2288
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002289static ssize_t
2290tracing_ctrl_read(struct file *filp, char __user *ubuf,
2291 size_t cnt, loff_t *ppos)
2292{
2293 struct trace_array *tr = filp->private_data;
2294 char buf[64];
2295 int r;
2296
2297 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002298 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002299}
2300
2301static ssize_t
2302tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2303 size_t cnt, loff_t *ppos)
2304{
2305 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002306 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002307 long val;
2308 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002309
Steven Rostedtcffae432008-05-12 21:21:00 +02002310 if (cnt >= sizeof(buf))
2311 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002312
2313 if (copy_from_user(&buf, ubuf, cnt))
2314 return -EFAULT;
2315
2316 buf[cnt] = 0;
2317
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002318 ret = strict_strtoul(buf, 10, &val);
2319 if (ret < 0)
2320 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002321
2322 val = !!val;
2323
2324 mutex_lock(&trace_types_lock);
2325 if (tr->ctrl ^ val) {
2326 if (val)
2327 tracer_enabled = 1;
2328 else
2329 tracer_enabled = 0;
2330
2331 tr->ctrl = val;
2332
2333 if (current_trace && current_trace->ctrl_update)
2334 current_trace->ctrl_update(tr);
2335 }
2336 mutex_unlock(&trace_types_lock);
2337
2338 filp->f_pos += cnt;
2339
2340 return cnt;
2341}
2342
2343static ssize_t
2344tracing_set_trace_read(struct file *filp, char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2346{
2347 char buf[max_tracer_type_len+2];
2348 int r;
2349
2350 mutex_lock(&trace_types_lock);
2351 if (current_trace)
2352 r = sprintf(buf, "%s\n", current_trace->name);
2353 else
2354 r = sprintf(buf, "\n");
2355 mutex_unlock(&trace_types_lock);
2356
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002357 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002358}
2359
2360static ssize_t
2361tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2362 size_t cnt, loff_t *ppos)
2363{
2364 struct trace_array *tr = &global_trace;
2365 struct tracer *t;
2366 char buf[max_tracer_type_len+1];
2367 int i;
2368
2369 if (cnt > max_tracer_type_len)
2370 cnt = max_tracer_type_len;
2371
2372 if (copy_from_user(&buf, ubuf, cnt))
2373 return -EFAULT;
2374
2375 buf[cnt] = 0;
2376
2377 /* strip ending whitespace. */
2378 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2379 buf[i] = 0;
2380
2381 mutex_lock(&trace_types_lock);
2382 for (t = trace_types; t; t = t->next) {
2383 if (strcmp(t->name, buf) == 0)
2384 break;
2385 }
2386 if (!t || t == current_trace)
2387 goto out;
2388
2389 if (current_trace && current_trace->reset)
2390 current_trace->reset(tr);
2391
2392 current_trace = t;
2393 if (t->init)
2394 t->init(tr);
2395
2396 out:
2397 mutex_unlock(&trace_types_lock);
2398
2399 filp->f_pos += cnt;
2400
2401 return cnt;
2402}
2403
2404static ssize_t
2405tracing_max_lat_read(struct file *filp, char __user *ubuf,
2406 size_t cnt, loff_t *ppos)
2407{
2408 unsigned long *ptr = filp->private_data;
2409 char buf[64];
2410 int r;
2411
Steven Rostedtcffae432008-05-12 21:21:00 +02002412 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002413 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002414 if (r > sizeof(buf))
2415 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002416 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002417}
2418
2419static ssize_t
2420tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2421 size_t cnt, loff_t *ppos)
2422{
2423 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002424 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002425 long val;
2426 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002427
Steven Rostedtcffae432008-05-12 21:21:00 +02002428 if (cnt >= sizeof(buf))
2429 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002430
2431 if (copy_from_user(&buf, ubuf, cnt))
2432 return -EFAULT;
2433
2434 buf[cnt] = 0;
2435
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002436 ret = strict_strtoul(buf, 10, &val);
2437 if (ret < 0)
2438 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002439
2440 *ptr = val * 1000;
2441
2442 return cnt;
2443}
2444
Steven Rostedtb3806b42008-05-12 21:20:46 +02002445static atomic_t tracing_reader;
2446
2447static int tracing_open_pipe(struct inode *inode, struct file *filp)
2448{
2449 struct trace_iterator *iter;
2450
2451 if (tracing_disabled)
2452 return -ENODEV;
2453
2454 /* We only allow for reader of the pipe */
2455 if (atomic_inc_return(&tracing_reader) != 1) {
2456 atomic_dec(&tracing_reader);
2457 return -EBUSY;
2458 }
2459
2460 /* create a buffer to store the information to pass to userspace */
2461 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2462 if (!iter)
2463 return -ENOMEM;
2464
Steven Rostedt107bad82008-05-12 21:21:01 +02002465 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002466 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002467 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002468 filp->private_data = iter;
2469
Steven Rostedt107bad82008-05-12 21:21:01 +02002470 if (iter->trace->pipe_open)
2471 iter->trace->pipe_open(iter);
2472 mutex_unlock(&trace_types_lock);
2473
Steven Rostedtb3806b42008-05-12 21:20:46 +02002474 return 0;
2475}
2476
2477static int tracing_release_pipe(struct inode *inode, struct file *file)
2478{
2479 struct trace_iterator *iter = file->private_data;
2480
2481 kfree(iter);
2482 atomic_dec(&tracing_reader);
2483
2484 return 0;
2485}
2486
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002487static unsigned int
2488tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2489{
2490 struct trace_iterator *iter = filp->private_data;
2491
2492 if (trace_flags & TRACE_ITER_BLOCK) {
2493 /*
2494 * Always select as readable when in blocking mode
2495 */
2496 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002497 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002498 if (!trace_empty(iter))
2499 return POLLIN | POLLRDNORM;
2500 poll_wait(filp, &trace_wait, poll_table);
2501 if (!trace_empty(iter))
2502 return POLLIN | POLLRDNORM;
2503
2504 return 0;
2505 }
2506}
2507
Steven Rostedtb3806b42008-05-12 21:20:46 +02002508/*
2509 * Consumer reader.
2510 */
2511static ssize_t
2512tracing_read_pipe(struct file *filp, char __user *ubuf,
2513 size_t cnt, loff_t *ppos)
2514{
2515 struct trace_iterator *iter = filp->private_data;
2516 struct trace_array_cpu *data;
2517 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002518 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002519#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002520 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002521#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002522 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002523 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002524
2525 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002526 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2527 if (sret != -EBUSY)
2528 return sret;
2529 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002530
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002531 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002532
Steven Rostedt107bad82008-05-12 21:21:01 +02002533 mutex_lock(&trace_types_lock);
2534 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002535 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2536 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002537 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002538 }
2539
Steven Rostedtb3806b42008-05-12 21:20:46 +02002540 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002541
Steven Rostedt107bad82008-05-12 21:21:01 +02002542 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002543 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002544 goto out;
2545 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002546
Steven Rostedtb3806b42008-05-12 21:20:46 +02002547 /*
2548 * This is a make-shift waitqueue. The reason we don't use
2549 * an actual wait queue is because:
2550 * 1) we only ever have one waiter
2551 * 2) the tracing, traces all functions, we don't want
2552 * the overhead of calling wake_up and friends
2553 * (and tracing them too)
2554 * Anyway, this is really very primitive wakeup.
2555 */
2556 set_current_state(TASK_INTERRUPTIBLE);
2557 iter->tr->waiter = current;
2558
Steven Rostedt107bad82008-05-12 21:21:01 +02002559 mutex_unlock(&trace_types_lock);
2560
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002561 /* sleep for 100 msecs, and try again. */
2562 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002563
Steven Rostedt107bad82008-05-12 21:21:01 +02002564 mutex_lock(&trace_types_lock);
2565
Steven Rostedtb3806b42008-05-12 21:20:46 +02002566 iter->tr->waiter = NULL;
2567
Steven Rostedt107bad82008-05-12 21:21:01 +02002568 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002569 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002570 goto out;
2571 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002572
Steven Rostedt84527992008-05-12 21:20:58 +02002573 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002574 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002575
Steven Rostedtb3806b42008-05-12 21:20:46 +02002576 /*
2577 * We block until we read something and tracing is disabled.
2578 * We still block if tracing is disabled, but we have never
2579 * read anything. This allows a user to cat this file, and
2580 * then enable tracing. But after we have read something,
2581 * we give an EOF when tracing is again disabled.
2582 *
2583 * iter->pos will be 0 if we haven't read anything.
2584 */
2585 if (!tracer_enabled && iter->pos)
2586 break;
2587
2588 continue;
2589 }
2590
2591 /* stop when tracing is finished */
2592 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002593 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002594
2595 if (cnt >= PAGE_SIZE)
2596 cnt = PAGE_SIZE - 1;
2597
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002598 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002599 memset(&iter->seq, 0,
2600 sizeof(struct trace_iterator) -
2601 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002602 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002603
2604 /*
2605 * We need to stop all tracing on all CPUS to read the
2606 * the next buffer. This is a bit expensive, but is
2607 * not done often. We fill all what we can read,
2608 * and then release the locks again.
2609 */
2610
2611 cpus_clear(mask);
2612 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002613#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002614 ftrace_save = ftrace_enabled;
2615 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002616#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002617 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002618 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002619 data = iter->tr->data[cpu];
2620
2621 if (!head_page(data) || !data->trace_idx)
2622 continue;
2623
2624 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002625 cpu_set(cpu, mask);
2626 }
2627
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002628 for_each_cpu_mask(cpu, mask) {
2629 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002630 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002631
2632 if (data->overrun > iter->last_overrun[cpu])
2633 iter->overrun[cpu] +=
2634 data->overrun - iter->last_overrun[cpu];
2635 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002636 }
2637
Steven Rostedt088b1e422008-05-12 21:20:48 +02002638 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002639 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002640 int len = iter->seq.len;
2641
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002642 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002643 if (!ret) {
2644 /* don't print partial lines */
2645 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002646 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002647 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002648
2649 trace_consume(iter);
2650
2651 if (iter->seq.len >= cnt)
2652 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002653 }
2654
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002655 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002656 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002657 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002658 }
2659
2660 for_each_cpu_mask(cpu, mask) {
2661 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002662 atomic_dec(&data->disabled);
2663 }
Ingo Molnar25770462008-05-12 21:20:49 +02002664#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002665 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002666#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002667 local_irq_restore(flags);
2668
2669 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002670 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2671 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002672 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002673 if (sret == -EBUSY)
2674 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002675
Steven Rostedt107bad82008-05-12 21:21:01 +02002676out:
2677 mutex_unlock(&trace_types_lock);
2678
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002679 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002680}
2681
Steven Rostedta98a3c32008-05-12 21:20:59 +02002682static ssize_t
2683tracing_entries_read(struct file *filp, char __user *ubuf,
2684 size_t cnt, loff_t *ppos)
2685{
2686 struct trace_array *tr = filp->private_data;
2687 char buf[64];
2688 int r;
2689
2690 r = sprintf(buf, "%lu\n", tr->entries);
2691 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2692}
2693
2694static ssize_t
2695tracing_entries_write(struct file *filp, const char __user *ubuf,
2696 size_t cnt, loff_t *ppos)
2697{
2698 unsigned long val;
2699 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002700 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002701
Steven Rostedtcffae432008-05-12 21:21:00 +02002702 if (cnt >= sizeof(buf))
2703 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002704
2705 if (copy_from_user(&buf, ubuf, cnt))
2706 return -EFAULT;
2707
2708 buf[cnt] = 0;
2709
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002710 ret = strict_strtoul(buf, 10, &val);
2711 if (ret < 0)
2712 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002713
2714 /* must have at least 1 entry */
2715 if (!val)
2716 return -EINVAL;
2717
2718 mutex_lock(&trace_types_lock);
2719
2720 if (current_trace != &no_tracer) {
2721 cnt = -EBUSY;
2722 pr_info("ftrace: set current_tracer to none"
2723 " before modifying buffer size\n");
2724 goto out;
2725 }
2726
2727 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002728 long pages_requested;
2729 unsigned long freeable_pages;
2730
2731 /* make sure we have enough memory before mapping */
2732 pages_requested =
2733 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2734
2735 /* account for each buffer (and max_tr) */
2736 pages_requested *= tracing_nr_buffers * 2;
2737
2738 /* Check for overflow */
2739 if (pages_requested < 0) {
2740 cnt = -ENOMEM;
2741 goto out;
2742 }
2743
2744 freeable_pages = determine_dirtyable_memory();
2745
2746 /* we only allow to request 1/4 of useable memory */
2747 if (pages_requested >
2748 ((freeable_pages + tracing_pages_allocated) / 4)) {
2749 cnt = -ENOMEM;
2750 goto out;
2751 }
2752
Steven Rostedta98a3c32008-05-12 21:20:59 +02002753 while (global_trace.entries < val) {
2754 if (trace_alloc_page()) {
2755 cnt = -ENOMEM;
2756 goto out;
2757 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002758 /* double check that we don't go over the known pages */
2759 if (tracing_pages_allocated > pages_requested)
2760 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002761 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002762
Steven Rostedta98a3c32008-05-12 21:20:59 +02002763 } else {
2764 /* include the number of entries in val (inc of page entries) */
2765 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2766 trace_free_page();
2767 }
2768
Steven Rostedt19384c02008-05-22 00:22:16 -04002769 /* check integrity */
2770 for_each_tracing_cpu(i)
2771 check_pages(global_trace.data[i]);
2772
Steven Rostedta98a3c32008-05-12 21:20:59 +02002773 filp->f_pos += cnt;
2774
Steven Rostedt19384c02008-05-22 00:22:16 -04002775 /* If check pages failed, return ENOMEM */
2776 if (tracing_disabled)
2777 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002778 out:
2779 max_tr.entries = global_trace.entries;
2780 mutex_unlock(&trace_types_lock);
2781
2782 return cnt;
2783}
2784
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002785static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002786 .open = tracing_open_generic,
2787 .read = tracing_max_lat_read,
2788 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002789};
2790
2791static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002792 .open = tracing_open_generic,
2793 .read = tracing_ctrl_read,
2794 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002795};
2796
2797static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002798 .open = tracing_open_generic,
2799 .read = tracing_set_trace_read,
2800 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002801};
2802
Steven Rostedtb3806b42008-05-12 21:20:46 +02002803static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002804 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002805 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002806 .read = tracing_read_pipe,
2807 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002808};
2809
Steven Rostedta98a3c32008-05-12 21:20:59 +02002810static struct file_operations tracing_entries_fops = {
2811 .open = tracing_open_generic,
2812 .read = tracing_entries_read,
2813 .write = tracing_entries_write,
2814};
2815
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002816#ifdef CONFIG_DYNAMIC_FTRACE
2817
2818static ssize_t
2819tracing_read_long(struct file *filp, char __user *ubuf,
2820 size_t cnt, loff_t *ppos)
2821{
2822 unsigned long *p = filp->private_data;
2823 char buf[64];
2824 int r;
2825
2826 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002827
2828 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002829}
2830
2831static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002832 .open = tracing_open_generic,
2833 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002834};
2835#endif
2836
2837static struct dentry *d_tracer;
2838
2839struct dentry *tracing_init_dentry(void)
2840{
2841 static int once;
2842
2843 if (d_tracer)
2844 return d_tracer;
2845
2846 d_tracer = debugfs_create_dir("tracing", NULL);
2847
2848 if (!d_tracer && !once) {
2849 once = 1;
2850 pr_warning("Could not create debugfs directory 'tracing'\n");
2851 return NULL;
2852 }
2853
2854 return d_tracer;
2855}
2856
Steven Rostedt60a11772008-05-12 21:20:44 +02002857#ifdef CONFIG_FTRACE_SELFTEST
2858/* Let selftest have access to static functions in this file */
2859#include "trace_selftest.c"
2860#endif
2861
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002862static __init void tracer_init_debugfs(void)
2863{
2864 struct dentry *d_tracer;
2865 struct dentry *entry;
2866
2867 d_tracer = tracing_init_dentry();
2868
2869 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2870 &global_trace, &tracing_ctrl_fops);
2871 if (!entry)
2872 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2873
2874 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2875 NULL, &tracing_iter_fops);
2876 if (!entry)
2877 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2878
Ingo Molnarc7078de2008-05-12 21:20:52 +02002879 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2880 NULL, &tracing_cpumask_fops);
2881 if (!entry)
2882 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2883
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002884 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2885 &global_trace, &tracing_lt_fops);
2886 if (!entry)
2887 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2888
2889 entry = debugfs_create_file("trace", 0444, d_tracer,
2890 &global_trace, &tracing_fops);
2891 if (!entry)
2892 pr_warning("Could not create debugfs 'trace' entry\n");
2893
2894 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2895 &global_trace, &show_traces_fops);
2896 if (!entry)
2897 pr_warning("Could not create debugfs 'trace' entry\n");
2898
2899 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2900 &global_trace, &set_tracer_fops);
2901 if (!entry)
2902 pr_warning("Could not create debugfs 'trace' entry\n");
2903
2904 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2905 &tracing_max_latency,
2906 &tracing_max_lat_fops);
2907 if (!entry)
2908 pr_warning("Could not create debugfs "
2909 "'tracing_max_latency' entry\n");
2910
2911 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2912 &tracing_thresh, &tracing_max_lat_fops);
2913 if (!entry)
2914 pr_warning("Could not create debugfs "
2915 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002916 entry = debugfs_create_file("README", 0644, d_tracer,
2917 NULL, &tracing_readme_fops);
2918 if (!entry)
2919 pr_warning("Could not create debugfs 'README' entry\n");
2920
Steven Rostedtb3806b42008-05-12 21:20:46 +02002921 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2922 NULL, &tracing_pipe_fops);
2923 if (!entry)
2924 pr_warning("Could not create debugfs "
2925 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002926
Steven Rostedta98a3c32008-05-12 21:20:59 +02002927 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2928 &global_trace, &tracing_entries_fops);
2929 if (!entry)
2930 pr_warning("Could not create debugfs "
2931 "'tracing_threash' entry\n");
2932
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002933#ifdef CONFIG_DYNAMIC_FTRACE
2934 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2935 &ftrace_update_tot_cnt,
2936 &tracing_read_long_fops);
2937 if (!entry)
2938 pr_warning("Could not create debugfs "
2939 "'dyn_ftrace_total_info' entry\n");
2940#endif
Ingo Molnard618b3e2008-05-12 21:20:49 +02002941#ifdef CONFIG_SYSPROF_TRACER
2942 init_tracer_sysprof_debugfs(d_tracer);
2943#endif
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002944}
2945
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002946static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002947{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002948 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002949 struct page *page, *tmp;
2950 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002951 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002952 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002953 int i;
2954
2955 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002956 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002957 array = (void *)__get_free_page(GFP_KERNEL);
2958 if (array == NULL) {
2959 printk(KERN_ERR "tracer: failed to allocate page"
2960 "for trace buffer!\n");
2961 goto free_pages;
2962 }
2963
Steven Rostedt3eefae92008-05-12 21:21:04 +02002964 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002965 page = virt_to_page(array);
2966 list_add(&page->lru, &pages);
2967
2968/* Only allocate if we are actually using the max trace */
2969#ifdef CONFIG_TRACER_MAX_TRACE
2970 array = (void *)__get_free_page(GFP_KERNEL);
2971 if (array == NULL) {
2972 printk(KERN_ERR "tracer: failed to allocate page"
2973 "for trace buffer!\n");
2974 goto free_pages;
2975 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002976 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002977 page = virt_to_page(array);
2978 list_add(&page->lru, &pages);
2979#endif
2980 }
2981
2982 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002983 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002984 data = global_trace.data[i];
2985 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002986 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002987 list_add_tail(&page->lru, &data->trace_pages);
2988 ClearPageLRU(page);
2989
2990#ifdef CONFIG_TRACER_MAX_TRACE
2991 data = max_tr.data[i];
2992 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002993 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002994 list_add_tail(&page->lru, &data->trace_pages);
2995 SetPageLRU(page);
2996#endif
2997 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002998 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002999 global_trace.entries += ENTRIES_PER_PAGE;
3000
3001 return 0;
3002
3003 free_pages:
3004 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003005 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003006 __free_page(page);
3007 }
3008 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003009}
3010
Steven Rostedta98a3c32008-05-12 21:20:59 +02003011static int trace_free_page(void)
3012{
3013 struct trace_array_cpu *data;
3014 struct page *page;
3015 struct list_head *p;
3016 int i;
3017 int ret = 0;
3018
3019 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02003020 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02003021 data = global_trace.data[i];
3022 p = data->trace_pages.next;
3023 if (p == &data->trace_pages) {
3024 /* should never happen */
3025 WARN_ON(1);
3026 tracing_disabled = 1;
3027 ret = -1;
3028 break;
3029 }
3030 page = list_entry(p, struct page, lru);
3031 ClearPageLRU(page);
3032 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02003033 tracing_pages_allocated--;
3034 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02003035 __free_page(page);
3036
3037 tracing_reset(data);
3038
3039#ifdef CONFIG_TRACER_MAX_TRACE
3040 data = max_tr.data[i];
3041 p = data->trace_pages.next;
3042 if (p == &data->trace_pages) {
3043 /* should never happen */
3044 WARN_ON(1);
3045 tracing_disabled = 1;
3046 ret = -1;
3047 break;
3048 }
3049 page = list_entry(p, struct page, lru);
3050 ClearPageLRU(page);
3051 list_del(&page->lru);
3052 __free_page(page);
3053
3054 tracing_reset(data);
3055#endif
3056 }
3057 global_trace.entries -= ENTRIES_PER_PAGE;
3058
3059 return ret;
3060}
3061
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003062__init static int tracer_alloc_buffers(void)
3063{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003064 struct trace_array_cpu *data;
3065 void *array;
3066 struct page *page;
3067 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02003068 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003069 int i;
3070
Steven Rostedtab464282008-05-12 21:21:00 +02003071 /* TODO: make the number of buffers hot pluggable with CPUS */
3072 tracing_nr_buffers = num_possible_cpus();
3073 tracing_buffer_mask = cpu_possible_map;
3074
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003075 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003076 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003077 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003078 max_tr.data[i] = &per_cpu(max_data, i);
3079
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003080 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003081 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003082 printk(KERN_ERR "tracer: failed to allocate page"
3083 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003084 goto free_buffers;
3085 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003086
3087 /* set the array to the list */
3088 INIT_LIST_HEAD(&data->trace_pages);
3089 page = virt_to_page(array);
3090 list_add(&page->lru, &data->trace_pages);
3091 /* use the LRU flag to differentiate the two buffers */
3092 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003093
Steven Rostedta98a3c32008-05-12 21:20:59 +02003094 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3095 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3096
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003097/* Only allocate if we are actually using the max trace */
3098#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003099 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003100 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003101 printk(KERN_ERR "tracer: failed to allocate page"
3102 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003103 goto free_buffers;
3104 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003105
3106 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3107 page = virt_to_page(array);
3108 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3109 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003110#endif
3111 }
3112
3113 /*
3114 * Since we allocate by orders of pages, we may be able to
3115 * round up a bit.
3116 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003117 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003118 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003119
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003120 while (global_trace.entries < trace_nr_entries) {
3121 if (trace_alloc_page())
3122 break;
3123 pages++;
3124 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003125 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003126
Jiri Slaby20764ff2008-06-12 11:27:03 +02003127 pr_info("tracer: %d pages allocated for %ld entries of %ld bytes\n",
3128 pages, trace_nr_entries, (long)TRACE_ENTRY_SIZE);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003129 pr_info(" actual entries %ld\n", global_trace.entries);
3130
3131 tracer_init_debugfs();
3132
3133 trace_init_cmdlines();
3134
3135 register_tracer(&no_tracer);
3136 current_trace = &no_tracer;
3137
Steven Rostedt60a11772008-05-12 21:20:44 +02003138 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003139 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003140 tracing_disabled = 0;
3141
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003142 return 0;
3143
3144 free_buffers:
3145 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003146 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003147 struct trace_array_cpu *data = global_trace.data[i];
3148
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003149 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003150 list_for_each_entry_safe(page, tmp,
3151 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003152 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003153 __free_page(page);
3154 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003155 }
3156
3157#ifdef CONFIG_TRACER_MAX_TRACE
3158 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003159 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003160 list_for_each_entry_safe(page, tmp,
3161 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003162 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003163 __free_page(page);
3164 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003165 }
3166#endif
3167 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003168 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003169}
Steven Rostedt60a11772008-05-12 21:20:44 +02003170fs_initcall(tracer_alloc_buffers);