blob: dde6f0ace6dc7a9be77e16783ac3bbf9e72d21ff [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
99/*
100 * trace_nr_entries is the number of entries that is allocated
101 * for a buffer. Note, the number of entries is always rounded
102 * to ENTRIES_PER_PAGE.
103 */
Ingo Molnar57422792008-05-12 21:20:51 +0200104static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200105
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200106/* trace_types holds a link list of available tracers. */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200107static struct tracer *trace_types __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200108
109/* current_trace points to the tracer that is currently active */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200110static struct tracer *current_trace __read_mostly;
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200111
112/*
113 * max_tracer_type_len is used to simplify the allocating of
114 * buffers to read userspace tracer names. We keep track of
115 * the longest tracer name registered.
116 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200117static int max_tracer_type_len;
118
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200119/*
120 * trace_types_lock is used to protect the trace_types list.
121 * This lock is also used to keep user access serialized.
122 * Accesses from userspace will grab this lock while userspace
123 * activities happen inside the kernel.
124 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200125static DEFINE_MUTEX(trace_types_lock);
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200126
127/* trace_wait is a waitqueue for tasks blocked on trace_poll */
Ingo Molnar4e655512008-05-12 21:20:52 +0200128static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
129
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200130/* trace_flags holds iter_ctrl options */
Ingo Molnar4e655512008-05-12 21:20:52 +0200131unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
132
Ankita Garg2b1bce12008-06-09 14:10:25 +0530133static notrace void no_trace_init(struct trace_array *tr)
134{
135 int cpu;
136
137 if(tr->ctrl)
138 for_each_online_cpu(cpu)
139 tracing_reset(tr->data[cpu]);
140 tracer_enabled = 0;
141}
142
143/* dummy trace to disable tracing */
144static struct tracer no_tracer __read_mostly = {
145 .name = "none",
146 .init = no_trace_init
147};
148
149
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200150/**
151 * trace_wake_up - wake up tasks waiting for trace input
152 *
153 * Simply wakes up any task that is blocked on the trace_wait
154 * queue. These is used with trace_poll for tasks polling the trace.
155 */
Ingo Molnar4e655512008-05-12 21:20:52 +0200156void trace_wake_up(void)
157{
Ingo Molnar017730c2008-05-12 21:20:52 +0200158 /*
159 * The runqueue_is_locked() can fail, but this is the best we
160 * have for now:
161 */
162 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +0200163 wake_up(&trace_wait);
164}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200165
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200166#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
167
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200168static int __init set_nr_entries(char *str)
169{
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200170 unsigned long nr_entries;
171 int ret;
172
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200173 if (!str)
174 return 0;
Steven Rostedtc6caeeb2008-05-12 21:21:00 +0200175 ret = strict_strtoul(str, 0, &nr_entries);
176 /* nr_entries can not be zero */
177 if (ret < 0 || nr_entries == 0)
178 return 0;
179 trace_nr_entries = nr_entries;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200180 return 1;
181}
182__setup("trace_entries=", set_nr_entries);
183
Steven Rostedt57f50be2008-05-12 21:20:44 +0200184unsigned long nsecs_to_usecs(unsigned long nsecs)
185{
186 return nsecs / 1000;
187}
188
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200189/*
190 * trace_flag_type is an enumeration that holds different
191 * states when a trace occurs. These are:
192 * IRQS_OFF - interrupts were disabled
193 * NEED_RESCED - reschedule is requested
194 * HARDIRQ - inside an interrupt handler
195 * SOFTIRQ - inside a softirq handler
196 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200197enum trace_flag_type {
198 TRACE_FLAG_IRQS_OFF = 0x01,
199 TRACE_FLAG_NEED_RESCHED = 0x02,
200 TRACE_FLAG_HARDIRQ = 0x04,
201 TRACE_FLAG_SOFTIRQ = 0x08,
202};
203
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200204/*
205 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
206 * control the output of kernel symbols.
207 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200208#define TRACE_ITER_SYM_MASK \
209 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
210
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200211/* These must match the bit postions in trace_iterator_flags */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200212static const char *trace_options[] = {
213 "print-parent",
214 "sym-offset",
215 "sym-addr",
216 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200217 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200218 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200219 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200220 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200221 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200222 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200223 NULL
224};
225
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200226/*
227 * ftrace_max_lock is used to protect the swapping of buffers
228 * when taking a max snapshot. The buffers themselves are
229 * protected by per_cpu spinlocks. But the action of the swap
230 * needs its own lock.
231 *
232 * This is defined as a raw_spinlock_t in order to help
233 * with performance when lockdep debugging is enabled.
234 */
Steven Rostedt92205c22008-05-12 21:20:55 +0200235static raw_spinlock_t ftrace_max_lock =
236 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200237
238/*
239 * Copy the new maximum trace into the separate maximum-trace
240 * structure. (this way the maximum trace is permanently saved,
241 * for later retrieval via /debugfs/tracing/latency_trace)
242 */
Ingo Molnare309b412008-05-12 21:20:51 +0200243static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200244__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
245{
246 struct trace_array_cpu *data = tr->data[cpu];
247
248 max_tr.cpu = cpu;
249 max_tr.time_start = data->preempt_timestamp;
250
251 data = max_tr.data[cpu];
252 data->saved_latency = tracing_max_latency;
253
254 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
255 data->pid = tsk->pid;
256 data->uid = tsk->uid;
257 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
258 data->policy = tsk->policy;
259 data->rt_priority = tsk->rt_priority;
260
261 /* record this tasks comm */
262 tracing_record_cmdline(current);
263}
264
Steven Rostedt19384c02008-05-22 00:22:16 -0400265#define CHECK_COND(cond) \
266 if (unlikely(cond)) { \
267 tracing_disabled = 1; \
268 WARN_ON(1); \
269 return -1; \
270 }
271
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200272/**
273 * check_pages - integrity check of trace buffers
274 *
275 * As a safty measure we check to make sure the data pages have not
Steven Rostedt19384c02008-05-22 00:22:16 -0400276 * been corrupted.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200277 */
Steven Rostedt19384c02008-05-22 00:22:16 -0400278int check_pages(struct trace_array_cpu *data)
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200279{
280 struct page *page, *tmp;
281
Steven Rostedt19384c02008-05-22 00:22:16 -0400282 CHECK_COND(data->trace_pages.next->prev != &data->trace_pages);
283 CHECK_COND(data->trace_pages.prev->next != &data->trace_pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200284
285 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
Steven Rostedt19384c02008-05-22 00:22:16 -0400286 CHECK_COND(page->lru.next->prev != &page->lru);
287 CHECK_COND(page->lru.prev->next != &page->lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200288 }
Steven Rostedt19384c02008-05-22 00:22:16 -0400289
290 return 0;
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200291}
292
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200293/**
294 * head_page - page address of the first page in per_cpu buffer.
295 *
296 * head_page returns the page address of the first page in
297 * a per_cpu buffer. This also preforms various consistency
298 * checks to make sure the buffer has not been corrupted.
299 */
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200300void *head_page(struct trace_array_cpu *data)
301{
302 struct page *page;
303
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200304 if (list_empty(&data->trace_pages))
305 return NULL;
306
307 page = list_entry(data->trace_pages.next, struct page, lru);
308 BUG_ON(&page->lru == &data->trace_pages);
309
310 return page_address(page);
311}
312
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200313/**
314 * trace_seq_printf - sequence printing of trace information
315 * @s: trace sequence descriptor
316 * @fmt: printf format string
317 *
318 * The tracer may use either sequence operations or its own
319 * copy to user routines. To simplify formating of a trace
320 * trace_seq_printf is used to store strings into a special
321 * buffer (@s). Then the output may be either used by
322 * the sequencer or pulled into another buffer.
323 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200324int
Steven Rostedt214023c2008-05-12 21:20:46 +0200325trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
326{
327 int len = (PAGE_SIZE - 1) - s->len;
328 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200329 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200330
331 if (!len)
332 return 0;
333
334 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200335 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200336 va_end(ap);
337
Steven Rostedtb3806b42008-05-12 21:20:46 +0200338 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200339 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200340 return 0;
341
342 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200343
344 return len;
345}
346
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200347/**
348 * trace_seq_puts - trace sequence printing of simple string
349 * @s: trace sequence descriptor
350 * @str: simple string to record
351 *
352 * The tracer may use either the sequence operations or its own
353 * copy to user routines. This function records a simple string
354 * into a special buffer (@s) for later retrieval by a sequencer
355 * or other mechanism.
356 */
Ingo Molnare309b412008-05-12 21:20:51 +0200357static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200358trace_seq_puts(struct trace_seq *s, const char *str)
359{
360 int len = strlen(str);
361
362 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200363 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200364
365 memcpy(s->buffer + s->len, str, len);
366 s->len += len;
367
368 return len;
369}
370
Ingo Molnare309b412008-05-12 21:20:51 +0200371static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200372trace_seq_putc(struct trace_seq *s, unsigned char c)
373{
374 if (s->len >= (PAGE_SIZE - 1))
375 return 0;
376
377 s->buffer[s->len++] = c;
378
379 return 1;
380}
381
Ingo Molnare309b412008-05-12 21:20:51 +0200382static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200383trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
384{
385 if (len > ((PAGE_SIZE - 1) - s->len))
386 return 0;
387
388 memcpy(s->buffer + s->len, mem, len);
389 s->len += len;
390
391 return len;
392}
393
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200394#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200395static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200396
Ingo Molnare309b412008-05-12 21:20:51 +0200397static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200398trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
399{
400 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200401 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200402 unsigned char byte;
403 int i, j;
404
405 BUG_ON(len >= HEX_CHARS);
406
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200407#ifdef __BIG_ENDIAN
408 for (i = 0, j = 0; i < len; i++) {
409#else
410 for (i = len-1, j = 0; i >= 0; i--) {
411#endif
412 byte = data[i];
413
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200414 hex[j++] = hex2asc[byte & 0x0f];
415 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200416 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200417 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200418
419 return trace_seq_putmem(s, hex, j);
420}
421
Ingo Molnare309b412008-05-12 21:20:51 +0200422static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200423trace_seq_reset(struct trace_seq *s)
424{
425 s->len = 0;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +0200426 s->readpos = 0;
427}
428
429ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
430{
431 int len;
432 int ret;
433
434 if (s->len <= s->readpos)
435 return -EBUSY;
436
437 len = s->len - s->readpos;
438 if (cnt > len)
439 cnt = len;
440 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
441 if (ret)
442 return -EFAULT;
443
444 s->readpos += len;
445 return cnt;
Steven Rostedt214023c2008-05-12 21:20:46 +0200446}
447
Ingo Molnare309b412008-05-12 21:20:51 +0200448static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200449trace_print_seq(struct seq_file *m, struct trace_seq *s)
450{
451 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
452
453 s->buffer[len] = 0;
454 seq_puts(m, s->buffer);
455
456 trace_seq_reset(s);
457}
458
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200459/*
460 * flip the trace buffers between two trace descriptors.
461 * This usually is the buffers between the global_trace and
462 * the max_tr to record a snapshot of a current trace.
463 *
464 * The ftrace_max_lock must be held.
465 */
Ingo Molnare309b412008-05-12 21:20:51 +0200466static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200467flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
468{
469 struct list_head flip_pages;
470
471 INIT_LIST_HEAD(&flip_pages);
472
Steven Rostedt93a588f2008-05-12 21:20:45 +0200473 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200474 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200475 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200476
477 check_pages(tr1);
478 check_pages(tr2);
479 list_splice_init(&tr1->trace_pages, &flip_pages);
480 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
481 list_splice_init(&flip_pages, &tr2->trace_pages);
482 BUG_ON(!list_empty(&flip_pages));
483 check_pages(tr1);
484 check_pages(tr2);
485}
486
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200487/**
488 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
489 * @tr: tracer
490 * @tsk: the task with the latency
491 * @cpu: The cpu that initiated the trace.
492 *
493 * Flip the buffers between the @tr and the max_tr and record information
494 * about which task was the cause of this latency.
495 */
Ingo Molnare309b412008-05-12 21:20:51 +0200496void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200497update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
498{
499 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200500 int i;
501
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200502 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200503 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200504 /* clear out all the previous traces */
Steven Rostedtab464282008-05-12 21:21:00 +0200505 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200506 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200507 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200508 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200509 }
510
511 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200512 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200513}
514
515/**
516 * update_max_tr_single - only copy one trace over, and reset the rest
517 * @tr - tracer
518 * @tsk - task with the latency
519 * @cpu - the cpu of the buffer to copy.
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200520 *
521 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200522 */
Ingo Molnare309b412008-05-12 21:20:51 +0200523void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200524update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
525{
526 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200527 int i;
528
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200529 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200530 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +0200531 for_each_tracing_cpu(i)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200532 tracing_reset(max_tr.data[i]);
533
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200534 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200535 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200536
537 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200538 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200539}
540
Steven Rostedt4fcdae82008-05-12 21:21:00 +0200541/**
542 * register_tracer - register a tracer with the ftrace system.
543 * @type - the plugin for the tracer
544 *
545 * Register a new plugin tracer.
546 */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200547int register_tracer(struct tracer *type)
548{
549 struct tracer *t;
550 int len;
551 int ret = 0;
552
553 if (!type->name) {
554 pr_info("Tracer must have a name\n");
555 return -1;
556 }
557
558 mutex_lock(&trace_types_lock);
559 for (t = trace_types; t; t = t->next) {
560 if (strcmp(type->name, t->name) == 0) {
561 /* already found */
562 pr_info("Trace %s already registered\n",
563 type->name);
564 ret = -1;
565 goto out;
566 }
567 }
568
Steven Rostedt60a11772008-05-12 21:20:44 +0200569#ifdef CONFIG_FTRACE_STARTUP_TEST
570 if (type->selftest) {
571 struct tracer *saved_tracer = current_trace;
572 struct trace_array_cpu *data;
573 struct trace_array *tr = &global_trace;
574 int saved_ctrl = tr->ctrl;
575 int i;
576 /*
577 * Run a selftest on this tracer.
578 * Here we reset the trace buffer, and set the current
579 * tracer to be this tracer. The tracer can then run some
580 * internal tracing to verify that everything is in order.
581 * If we fail, we do not register this tracer.
582 */
Steven Rostedtab464282008-05-12 21:21:00 +0200583 for_each_tracing_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200584 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200585 if (!head_page(data))
586 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200587 tracing_reset(data);
588 }
589 current_trace = type;
590 tr->ctrl = 0;
591 /* the test is responsible for initializing and enabling */
592 pr_info("Testing tracer %s: ", type->name);
593 ret = type->selftest(type, tr);
594 /* the test is responsible for resetting too */
595 current_trace = saved_tracer;
596 tr->ctrl = saved_ctrl;
597 if (ret) {
598 printk(KERN_CONT "FAILED!\n");
599 goto out;
600 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200601 /* Only reset on passing, to avoid touching corrupted buffers */
Steven Rostedtab464282008-05-12 21:21:00 +0200602 for_each_tracing_cpu(i) {
Steven Rostedt1d4db002008-05-12 21:20:45 +0200603 data = tr->data[i];
604 if (!head_page(data))
605 continue;
606 tracing_reset(data);
607 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200608 printk(KERN_CONT "PASSED\n");
609 }
610#endif
611
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200612 type->next = trace_types;
613 trace_types = type;
614 len = strlen(type->name);
615 if (len > max_tracer_type_len)
616 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200617
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200618 out:
619 mutex_unlock(&trace_types_lock);
620
621 return ret;
622}
623
624void unregister_tracer(struct tracer *type)
625{
626 struct tracer **t;
627 int len;
628
629 mutex_lock(&trace_types_lock);
630 for (t = &trace_types; *t; t = &(*t)->next) {
631 if (*t == type)
632 goto found;
633 }
634 pr_info("Trace %s not registered\n", type->name);
635 goto out;
636
637 found:
638 *t = (*t)->next;
639 if (strlen(type->name) != max_tracer_type_len)
640 goto out;
641
642 max_tracer_type_len = 0;
643 for (t = &trace_types; *t; t = &(*t)->next) {
644 len = strlen((*t)->name);
645 if (len > max_tracer_type_len)
646 max_tracer_type_len = len;
647 }
648 out:
649 mutex_unlock(&trace_types_lock);
650}
651
Ingo Molnare309b412008-05-12 21:20:51 +0200652void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653{
654 data->trace_idx = 0;
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200655 data->overrun = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200656 data->trace_head = data->trace_tail = head_page(data);
657 data->trace_head_idx = 0;
658 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200659}
660
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200661#define SAVED_CMDLINES 128
662static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
663static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
664static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
665static int cmdline_idx;
666static DEFINE_SPINLOCK(trace_cmdline_lock);
Steven Rostedt25b0b442008-05-12 21:21:00 +0200667
Steven Rostedt25b0b442008-05-12 21:21:00 +0200668/* temporary disable recording */
669atomic_t trace_record_cmdline_disabled __read_mostly;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200670
671static void trace_init_cmdlines(void)
672{
673 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
674 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
675 cmdline_idx = 0;
676}
677
Ingo Molnare309b412008-05-12 21:20:51 +0200678void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200679
Ingo Molnare309b412008-05-12 21:20:51 +0200680static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200681{
682 unsigned map;
683 unsigned idx;
684
685 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
686 return;
687
688 /*
689 * It's not the end of the world if we don't get
690 * the lock, but we also don't want to spin
691 * nor do we want to disable interrupts,
692 * so if we miss here, then better luck next time.
693 */
694 if (!spin_trylock(&trace_cmdline_lock))
695 return;
696
697 idx = map_pid_to_cmdline[tsk->pid];
698 if (idx >= SAVED_CMDLINES) {
699 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
700
701 map = map_cmdline_to_pid[idx];
702 if (map <= PID_MAX_DEFAULT)
703 map_pid_to_cmdline[map] = (unsigned)-1;
704
705 map_pid_to_cmdline[tsk->pid] = idx;
706
707 cmdline_idx = idx;
708 }
709
710 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
711
712 spin_unlock(&trace_cmdline_lock);
713}
714
Ingo Molnare309b412008-05-12 21:20:51 +0200715static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200716{
717 char *cmdline = "<...>";
718 unsigned map;
719
720 if (!pid)
721 return "<idle>";
722
723 if (pid > PID_MAX_DEFAULT)
724 goto out;
725
726 map = map_pid_to_cmdline[pid];
727 if (map >= SAVED_CMDLINES)
728 goto out;
729
730 cmdline = saved_cmdlines[map];
731
732 out:
733 return cmdline;
734}
735
Ingo Molnare309b412008-05-12 21:20:51 +0200736void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200737{
738 if (atomic_read(&trace_record_cmdline_disabled))
739 return;
740
741 trace_save_cmdline(tsk);
742}
743
Ingo Molnare309b412008-05-12 21:20:51 +0200744static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200745trace_next_list(struct trace_array_cpu *data, struct list_head *next)
746{
747 /*
748 * Roundrobin - but skip the head (which is not a real page):
749 */
750 next = next->next;
751 if (unlikely(next == &data->trace_pages))
752 next = next->next;
753 BUG_ON(next == &data->trace_pages);
754
755 return next;
756}
757
Ingo Molnare309b412008-05-12 21:20:51 +0200758static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200759trace_next_page(struct trace_array_cpu *data, void *addr)
760{
761 struct list_head *next;
762 struct page *page;
763
764 page = virt_to_page(addr);
765
766 next = trace_next_list(data, &page->lru);
767 page = list_entry(next, struct page, lru);
768
769 return page_address(page);
770}
771
Ingo Molnare309b412008-05-12 21:20:51 +0200772static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200773tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200774{
775 unsigned long idx, idx_next;
776 struct trace_entry *entry;
777
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200778 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200779 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200780 idx_next = idx + 1;
781
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200782 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
783
Steven Rostedt93a588f2008-05-12 21:20:45 +0200784 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200785
786 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200787 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200788 idx_next = 0;
789 }
790
Steven Rostedt93a588f2008-05-12 21:20:45 +0200791 if (data->trace_head == data->trace_tail &&
792 idx_next == data->trace_tail_idx) {
793 /* overrun */
Steven Rostedt53d0aa72008-05-12 21:21:01 +0200794 data->overrun++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200795 data->trace_tail_idx++;
796 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
797 data->trace_tail =
798 trace_next_page(data, data->trace_tail);
799 data->trace_tail_idx = 0;
800 }
801 }
802
803 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200804
805 return entry;
806}
807
Ingo Molnare309b412008-05-12 21:20:51 +0200808static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200809tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200810{
811 struct task_struct *tsk = current;
812 unsigned long pc;
813
814 pc = preempt_count();
815
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200816 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200817 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200818 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200819 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
820 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
821 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
822 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
823}
824
Ingo Molnare309b412008-05-12 21:20:51 +0200825void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200826trace_function(struct trace_array *tr, struct trace_array_cpu *data,
827 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200828{
829 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200830 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200831
Steven Rostedt92205c22008-05-12 21:20:55 +0200832 raw_local_irq_save(irq_flags);
833 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200834 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200835 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200836 entry->type = TRACE_FN;
837 entry->fn.ip = ip;
838 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200839 __raw_spin_unlock(&data->lock);
840 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200841}
842
Ingo Molnare309b412008-05-12 21:20:51 +0200843void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200844ftrace(struct trace_array *tr, struct trace_array_cpu *data,
845 unsigned long ip, unsigned long parent_ip, unsigned long flags)
846{
847 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200848 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200849}
850
Ingo Molnar86387f72008-05-12 21:20:51 +0200851void __trace_stack(struct trace_array *tr,
852 struct trace_array_cpu *data,
853 unsigned long flags,
854 int skip)
855{
856 struct trace_entry *entry;
857 struct stack_trace trace;
858
859 if (!(trace_flags & TRACE_ITER_STACKTRACE))
860 return;
861
862 entry = tracing_get_trace_entry(tr, data);
863 tracing_generic_entry_update(entry, flags);
864 entry->type = TRACE_STACK;
865
866 memset(&entry->stack, 0, sizeof(entry->stack));
867
868 trace.nr_entries = 0;
869 trace.max_entries = FTRACE_STACK_ENTRIES;
870 trace.skip = skip;
871 trace.entries = entry->stack.caller;
872
873 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200874}
875
Ingo Molnare309b412008-05-12 21:20:51 +0200876void
Ingo Molnara4feb832008-05-12 21:21:02 +0200877__trace_special(void *__tr, void *__data,
878 unsigned long arg1, unsigned long arg2, unsigned long arg3)
879{
880 struct trace_array_cpu *data = __data;
881 struct trace_array *tr = __tr;
882 struct trace_entry *entry;
883 unsigned long irq_flags;
884
885 raw_local_irq_save(irq_flags);
886 __raw_spin_lock(&data->lock);
887 entry = tracing_get_trace_entry(tr, data);
888 tracing_generic_entry_update(entry, 0);
889 entry->type = TRACE_SPECIAL;
890 entry->special.arg1 = arg1;
891 entry->special.arg2 = arg2;
892 entry->special.arg3 = arg3;
893 __trace_stack(tr, data, irq_flags, 4);
894 __raw_spin_unlock(&data->lock);
895 raw_local_irq_restore(irq_flags);
896
897 trace_wake_up();
898}
899
900void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200901tracing_sched_switch_trace(struct trace_array *tr,
902 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200903 struct task_struct *prev,
904 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200905 unsigned long flags)
906{
907 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200908 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200909
Steven Rostedt92205c22008-05-12 21:20:55 +0200910 raw_local_irq_save(irq_flags);
911 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200912 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200913 tracing_generic_entry_update(entry, flags);
914 entry->type = TRACE_CTX;
915 entry->ctx.prev_pid = prev->pid;
916 entry->ctx.prev_prio = prev->prio;
917 entry->ctx.prev_state = prev->state;
918 entry->ctx.next_pid = next->pid;
919 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200920 entry->ctx.next_state = next->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200921 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200922 __raw_spin_unlock(&data->lock);
923 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200924}
925
Ingo Molnar57422792008-05-12 21:20:51 +0200926void
927tracing_sched_wakeup_trace(struct trace_array *tr,
928 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200929 struct task_struct *wakee,
930 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200931 unsigned long flags)
932{
933 struct trace_entry *entry;
934 unsigned long irq_flags;
935
Steven Rostedt92205c22008-05-12 21:20:55 +0200936 raw_local_irq_save(irq_flags);
937 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200938 entry = tracing_get_trace_entry(tr, data);
939 tracing_generic_entry_update(entry, flags);
940 entry->type = TRACE_WAKE;
941 entry->ctx.prev_pid = curr->pid;
942 entry->ctx.prev_prio = curr->prio;
943 entry->ctx.prev_state = curr->state;
944 entry->ctx.next_pid = wakee->pid;
945 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200946 entry->ctx.next_state = wakee->state;
Ingo Molnar74f4e362008-05-12 21:21:15 +0200947 __trace_stack(tr, data, flags, 6);
Steven Rostedt92205c22008-05-12 21:20:55 +0200948 __raw_spin_unlock(&data->lock);
949 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200950
951 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200952}
953
Steven Rostedt4902f882008-05-22 00:22:18 -0400954void
955ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
956{
957 struct trace_array *tr = &global_trace;
958 struct trace_array_cpu *data;
959 unsigned long flags;
960 long disabled;
961 int cpu;
962
963 if (tracing_disabled || current_trace == &no_tracer || !tr->ctrl)
964 return;
965
966 local_irq_save(flags);
967 cpu = raw_smp_processor_id();
968 data = tr->data[cpu];
969 disabled = atomic_inc_return(&data->disabled);
970
971 if (likely(disabled == 1))
972 __trace_special(tr, data, arg1, arg2, arg3);
973
974 atomic_dec(&data->disabled);
975 local_irq_restore(flags);
976}
977
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200978#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200979static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200980function_trace_call(unsigned long ip, unsigned long parent_ip)
981{
982 struct trace_array *tr = &global_trace;
983 struct trace_array_cpu *data;
984 unsigned long flags;
985 long disabled;
986 int cpu;
987
988 if (unlikely(!tracer_enabled))
989 return;
990
991 local_irq_save(flags);
992 cpu = raw_smp_processor_id();
993 data = tr->data[cpu];
994 disabled = atomic_inc_return(&data->disabled);
995
996 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200997 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200998
999 atomic_dec(&data->disabled);
1000 local_irq_restore(flags);
1001}
1002
1003static struct ftrace_ops trace_ops __read_mostly =
1004{
1005 .func = function_trace_call,
1006};
1007
Ingo Molnare309b412008-05-12 21:20:51 +02001008void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001009{
1010 register_ftrace_function(&trace_ops);
1011}
1012
Ingo Molnare309b412008-05-12 21:20:51 +02001013void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001014{
1015 unregister_ftrace_function(&trace_ops);
1016}
1017#endif
1018
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001019enum trace_file_type {
1020 TRACE_FILE_LAT_FMT = 1,
1021};
1022
1023static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001024trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
1025 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001026{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001027 struct page *page;
1028 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001029
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001030 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +02001031 iter->next_idx[cpu] >= data->trace_idx ||
1032 (data->trace_head == data->trace_tail &&
1033 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001034 return NULL;
1035
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001036 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +02001037 /* Initialize the iterator for this cpu trace buffer */
1038 WARN_ON(!data->trace_tail);
1039 page = virt_to_page(data->trace_tail);
1040 iter->next_page[cpu] = &page->lru;
1041 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001042 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001043
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001044 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001045 BUG_ON(&data->trace_pages == &page->lru);
1046
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001047 array = page_address(page);
1048
Steven Rostedt93a588f2008-05-12 21:20:45 +02001049 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001050 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001051}
1052
Ingo Molnare309b412008-05-12 21:20:51 +02001053static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001054find_next_entry(struct trace_iterator *iter, int *ent_cpu)
1055{
1056 struct trace_array *tr = iter->tr;
1057 struct trace_entry *ent, *next = NULL;
1058 int next_cpu = -1;
1059 int cpu;
1060
Steven Rostedtab464282008-05-12 21:21:00 +02001061 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001062 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001063 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001064 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001065 /*
1066 * Pick the entry with the smallest timestamp:
1067 */
1068 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001069 next = ent;
1070 next_cpu = cpu;
1071 }
1072 }
1073
1074 if (ent_cpu)
1075 *ent_cpu = next_cpu;
1076
1077 return next;
1078}
1079
Ingo Molnare309b412008-05-12 21:20:51 +02001080static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001081{
1082 iter->idx++;
1083 iter->next_idx[iter->cpu]++;
1084 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +02001085
Steven Rostedtb3806b42008-05-12 21:20:46 +02001086 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
1087 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1088
1089 iter->next_page_idx[iter->cpu] = 0;
1090 iter->next_page[iter->cpu] =
1091 trace_next_list(data, iter->next_page[iter->cpu]);
1092 }
1093}
1094
Ingo Molnare309b412008-05-12 21:20:51 +02001095static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001096{
1097 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
1098
1099 data->trace_tail_idx++;
1100 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
1101 data->trace_tail = trace_next_page(data, data->trace_tail);
1102 data->trace_tail_idx = 0;
1103 }
1104
1105 /* Check if we empty it, then reset the index */
1106 if (data->trace_head == data->trace_tail &&
1107 data->trace_head_idx == data->trace_tail_idx)
1108 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001109}
1110
Ingo Molnare309b412008-05-12 21:20:51 +02001111static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001112{
1113 struct trace_entry *next;
1114 int next_cpu = -1;
1115
1116 next = find_next_entry(iter, &next_cpu);
1117
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001118 iter->prev_ent = iter->ent;
1119 iter->prev_cpu = iter->cpu;
1120
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001121 iter->ent = next;
1122 iter->cpu = next_cpu;
1123
Steven Rostedtb3806b42008-05-12 21:20:46 +02001124 if (next)
1125 trace_iterator_increment(iter);
1126
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001127 return next ? iter : NULL;
1128}
1129
Ingo Molnare309b412008-05-12 21:20:51 +02001130static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001131{
1132 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001133 void *last_ent = iter->ent;
1134 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001135 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001136
1137 (*pos)++;
1138
1139 /* can't go backwards */
1140 if (iter->idx > i)
1141 return NULL;
1142
1143 if (iter->idx < 0)
1144 ent = find_next_entry_inc(iter);
1145 else
1146 ent = iter;
1147
1148 while (ent && iter->idx < i)
1149 ent = find_next_entry_inc(iter);
1150
1151 iter->pos = *pos;
1152
1153 if (last_ent && !ent)
1154 seq_puts(m, "\n\nvim:ft=help\n");
1155
1156 return ent;
1157}
1158
1159static void *s_start(struct seq_file *m, loff_t *pos)
1160{
1161 struct trace_iterator *iter = m->private;
1162 void *p = NULL;
1163 loff_t l = 0;
1164 int i;
1165
1166 mutex_lock(&trace_types_lock);
1167
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001168 if (!current_trace || current_trace != iter->trace) {
1169 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001170 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +02001171 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172
1173 atomic_inc(&trace_record_cmdline_disabled);
1174
1175 /* let the tracer grab locks here if needed */
1176 if (current_trace->start)
1177 current_trace->start(iter);
1178
1179 if (*pos != iter->pos) {
1180 iter->ent = NULL;
1181 iter->cpu = 0;
1182 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001183 iter->prev_ent = NULL;
1184 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185
Steven Rostedtab464282008-05-12 21:21:00 +02001186 for_each_tracing_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001187 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001188 iter->next_page[i] = NULL;
1189 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001190
1191 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1192 ;
1193
1194 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001195 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001196 p = s_next(m, p, &l);
1197 }
1198
1199 return p;
1200}
1201
1202static void s_stop(struct seq_file *m, void *p)
1203{
1204 struct trace_iterator *iter = m->private;
1205
1206 atomic_dec(&trace_record_cmdline_disabled);
1207
1208 /* let the tracer release locks here if needed */
1209 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1210 iter->trace->stop(iter);
1211
1212 mutex_unlock(&trace_types_lock);
1213}
1214
Abhishek Sagar76094a22008-05-28 00:03:18 +05301215#define KRETPROBE_MSG "[unknown/kretprobe'd]"
1216
1217#ifdef CONFIG_KRETPROBES
1218static inline int kretprobed(unsigned long addr)
1219{
1220 return addr == (unsigned long)kretprobe_trampoline;
1221}
1222#else
1223static inline int kretprobed(unsigned long addr)
1224{
1225 return 0;
1226}
1227#endif /* CONFIG_KRETPROBES */
1228
Steven Rostedtb3806b42008-05-12 21:20:46 +02001229static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001230seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001231{
1232#ifdef CONFIG_KALLSYMS
1233 char str[KSYM_SYMBOL_LEN];
1234
1235 kallsyms_lookup(address, NULL, NULL, NULL, str);
1236
Steven Rostedtb3806b42008-05-12 21:20:46 +02001237 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001238#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001239 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240}
1241
Steven Rostedtb3806b42008-05-12 21:20:46 +02001242static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001243seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1244 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001245{
1246#ifdef CONFIG_KALLSYMS
1247 char str[KSYM_SYMBOL_LEN];
1248
1249 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001250 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001251#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001252 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001253}
1254
1255#ifndef CONFIG_64BIT
1256# define IP_FMT "%08lx"
1257#else
1258# define IP_FMT "%016lx"
1259#endif
1260
Ingo Molnare309b412008-05-12 21:20:51 +02001261static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001262seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001263{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001264 int ret;
1265
1266 if (!ip)
1267 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001268
1269 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001270 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001271 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001272 ret = seq_print_sym_short(s, "%s", ip);
1273
1274 if (!ret)
1275 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276
1277 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001278 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1279 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001280}
1281
Ingo Molnare309b412008-05-12 21:20:51 +02001282static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283{
1284 seq_puts(m, "# _------=> CPU# \n");
1285 seq_puts(m, "# / _-----=> irqs-off \n");
1286 seq_puts(m, "# | / _----=> need-resched \n");
1287 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1288 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1289 seq_puts(m, "# |||| / \n");
1290 seq_puts(m, "# ||||| delay \n");
1291 seq_puts(m, "# cmd pid ||||| time | caller \n");
1292 seq_puts(m, "# \\ / ||||| \\ | / \n");
1293}
1294
Ingo Molnare309b412008-05-12 21:20:51 +02001295static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001296{
1297 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1298 seq_puts(m, "# | | | | |\n");
1299}
1300
1301
Ingo Molnare309b412008-05-12 21:20:51 +02001302static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001303print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1304{
1305 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1306 struct trace_array *tr = iter->tr;
1307 struct trace_array_cpu *data = tr->data[tr->cpu];
1308 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001309 unsigned long total = 0;
1310 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001311 int cpu;
1312 const char *name = "preemption";
1313
1314 if (type)
1315 name = type->name;
1316
Steven Rostedtab464282008-05-12 21:21:00 +02001317 for_each_tracing_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001318 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001319 total += tr->data[cpu]->trace_idx;
1320 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001322 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323 entries += tr->data[cpu]->trace_idx;
1324 }
1325 }
1326
1327 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1328 name, UTS_RELEASE);
1329 seq_puts(m, "-----------------------------------"
1330 "---------------------------------\n");
1331 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1332 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001333 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001334 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001335 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001336 tr->cpu,
1337#if defined(CONFIG_PREEMPT_NONE)
1338 "server",
1339#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1340 "desktop",
1341#elif defined(CONFIG_PREEMPT_DESKTOP)
1342 "preempt",
1343#else
1344 "unknown",
1345#endif
1346 /* These are reserved for later use */
1347 0, 0, 0, 0);
1348#ifdef CONFIG_SMP
1349 seq_printf(m, " #P:%d)\n", num_online_cpus());
1350#else
1351 seq_puts(m, ")\n");
1352#endif
1353 seq_puts(m, " -----------------\n");
1354 seq_printf(m, " | task: %.16s-%d "
1355 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1356 data->comm, data->pid, data->uid, data->nice,
1357 data->policy, data->rt_priority);
1358 seq_puts(m, " -----------------\n");
1359
1360 if (data->critical_start) {
1361 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001362 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1363 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001364 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001365 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1366 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001367 seq_puts(m, "\n");
1368 }
1369
1370 seq_puts(m, "\n");
1371}
1372
Ingo Molnare309b412008-05-12 21:20:51 +02001373static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001374lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001375{
1376 int hardirq, softirq;
1377 char *comm;
1378
1379 comm = trace_find_cmdline(entry->pid);
1380
Steven Rostedt214023c2008-05-12 21:20:46 +02001381 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1382 trace_seq_printf(s, "%d", cpu);
1383 trace_seq_printf(s, "%c%c",
1384 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1385 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001386
1387 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1388 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001389 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001390 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001391 } else {
1392 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001393 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001394 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001395 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001396 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001397 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001398 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001399 }
1400 }
1401
1402 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001403 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001404 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001405 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001406}
1407
1408unsigned long preempt_mark_thresh = 100;
1409
Ingo Molnare309b412008-05-12 21:20:51 +02001410static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001411lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001412 unsigned long rel_usecs)
1413{
Steven Rostedt214023c2008-05-12 21:20:46 +02001414 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001415 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001416 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001417 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001418 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001419 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001420 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001421}
1422
1423static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1424
Ingo Molnare309b412008-05-12 21:20:51 +02001425static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001426print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001427{
Steven Rostedt214023c2008-05-12 21:20:46 +02001428 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001429 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1430 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1431 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1432 struct trace_entry *entry = iter->ent;
1433 unsigned long abs_usecs;
1434 unsigned long rel_usecs;
1435 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001436 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001437 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001438 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001439
1440 if (!next_entry)
1441 next_entry = entry;
1442 rel_usecs = ns2usecs(next_entry->t - entry->t);
1443 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1444
1445 if (verbose) {
1446 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001447 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1448 " %ld.%03ldms (+%ld.%03ldms): ",
1449 comm,
1450 entry->pid, cpu, entry->flags,
1451 entry->preempt_count, trace_idx,
1452 ns2usecs(entry->t),
1453 abs_usecs/1000,
1454 abs_usecs % 1000, rel_usecs/1000,
1455 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001456 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001457 lat_print_generic(s, entry, cpu);
1458 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001459 }
1460 switch (entry->type) {
1461 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001462 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1463 trace_seq_puts(s, " (");
Abhishek Sagar76094a22008-05-28 00:03:18 +05301464 if (kretprobed(entry->fn.parent_ip))
1465 trace_seq_puts(s, KRETPROBE_MSG);
1466 else
1467 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
Steven Rostedt214023c2008-05-12 21:20:46 +02001468 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001469 break;
1470 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001471 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001472 T = entry->ctx.next_state < sizeof(state_to_char) ?
1473 state_to_char[entry->ctx.next_state] : 'X';
1474
Ankita Gargd17d9692008-05-12 21:20:58 +02001475 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1476 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001478 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001479 entry->ctx.prev_pid,
1480 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001481 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001482 entry->ctx.next_pid,
1483 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001484 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001485 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001486 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001487 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001488 entry->special.arg1,
1489 entry->special.arg2,
1490 entry->special.arg3);
1491 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001492 case TRACE_STACK:
1493 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1494 if (i)
1495 trace_seq_puts(s, " <= ");
1496 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1497 }
1498 trace_seq_puts(s, "\n");
1499 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001500 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001501 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001502 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001503 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001504}
1505
Ingo Molnare309b412008-05-12 21:20:51 +02001506static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001507{
Steven Rostedt214023c2008-05-12 21:20:46 +02001508 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001509 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001510 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001511 unsigned long usec_rem;
1512 unsigned long long t;
1513 unsigned long secs;
1514 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001515 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001516 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001517 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001518
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001519 entry = iter->ent;
1520
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521 comm = trace_find_cmdline(iter->ent->pid);
1522
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001523 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001524 usec_rem = do_div(t, 1000000ULL);
1525 secs = (unsigned long)t;
1526
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001527 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1528 if (!ret)
1529 return 0;
1530 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1531 if (!ret)
1532 return 0;
1533 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1534 if (!ret)
1535 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001536
1537 switch (entry->type) {
1538 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001539 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1540 if (!ret)
1541 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001542 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1543 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001544 ret = trace_seq_printf(s, " <-");
1545 if (!ret)
1546 return 0;
Abhishek Sagar76094a22008-05-28 00:03:18 +05301547 if (kretprobed(entry->fn.parent_ip))
1548 ret = trace_seq_puts(s, KRETPROBE_MSG);
1549 else
1550 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1551 sym_flags);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001552 if (!ret)
1553 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001554 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001555 ret = trace_seq_printf(s, "\n");
1556 if (!ret)
1557 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001558 break;
1559 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001560 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001561 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1562 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001563 T = entry->ctx.next_state < sizeof(state_to_char) ?
1564 state_to_char[entry->ctx.next_state] : 'X';
1565 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001566 entry->ctx.prev_pid,
1567 entry->ctx.prev_prio,
1568 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001569 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001570 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001571 entry->ctx.next_prio,
1572 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001573 if (!ret)
1574 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001575 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001576 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001577 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001578 entry->special.arg1,
1579 entry->special.arg2,
1580 entry->special.arg3);
1581 if (!ret)
1582 return 0;
1583 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001584 case TRACE_STACK:
1585 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1586 if (i) {
1587 ret = trace_seq_puts(s, " <= ");
1588 if (!ret)
1589 return 0;
1590 }
1591 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1592 sym_flags);
1593 if (!ret)
1594 return 0;
1595 }
1596 ret = trace_seq_puts(s, "\n");
1597 if (!ret)
1598 return 0;
1599 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001600 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001601 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001602}
1603
Ingo Molnare309b412008-05-12 21:20:51 +02001604static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001605{
1606 struct trace_seq *s = &iter->seq;
1607 struct trace_entry *entry;
1608 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001609 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001610
1611 entry = iter->ent;
1612
1613 ret = trace_seq_printf(s, "%d %d %llu ",
1614 entry->pid, iter->cpu, entry->t);
1615 if (!ret)
1616 return 0;
1617
1618 switch (entry->type) {
1619 case TRACE_FN:
1620 ret = trace_seq_printf(s, "%x %x\n",
1621 entry->fn.ip, entry->fn.parent_ip);
1622 if (!ret)
1623 return 0;
1624 break;
1625 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001626 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001627 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1628 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001629 T = entry->ctx.next_state < sizeof(state_to_char) ?
1630 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001631 if (entry->type == TRACE_WAKE)
1632 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001633 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001634 entry->ctx.prev_pid,
1635 entry->ctx.prev_prio,
1636 S,
1637 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001638 entry->ctx.next_prio,
1639 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001640 if (!ret)
1641 return 0;
1642 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001643 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001644 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001645 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001646 entry->special.arg1,
1647 entry->special.arg2,
1648 entry->special.arg3);
1649 if (!ret)
1650 return 0;
1651 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001652 }
1653 return 1;
1654}
1655
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001656#define SEQ_PUT_FIELD_RET(s, x) \
1657do { \
1658 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1659 return 0; \
1660} while (0)
1661
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001662#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1663do { \
1664 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1665 return 0; \
1666} while (0)
1667
Ingo Molnare309b412008-05-12 21:20:51 +02001668static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001669{
1670 struct trace_seq *s = &iter->seq;
1671 unsigned char newline = '\n';
1672 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001673 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001674
1675 entry = iter->ent;
1676
1677 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1678 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1679 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1680
1681 switch (entry->type) {
1682 case TRACE_FN:
1683 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1684 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1685 break;
1686 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001687 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001688 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1689 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001690 T = entry->ctx.next_state < sizeof(state_to_char) ?
1691 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001692 if (entry->type == TRACE_WAKE)
1693 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001694 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1695 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1696 SEQ_PUT_HEX_FIELD_RET(s, S);
1697 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1698 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1699 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001700 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001701 break;
1702 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001703 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001704 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1705 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1706 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1707 break;
1708 }
1709 SEQ_PUT_FIELD_RET(s, newline);
1710
1711 return 1;
1712}
1713
Ingo Molnare309b412008-05-12 21:20:51 +02001714static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001715{
1716 struct trace_seq *s = &iter->seq;
1717 struct trace_entry *entry;
1718
1719 entry = iter->ent;
1720
1721 SEQ_PUT_FIELD_RET(s, entry->pid);
1722 SEQ_PUT_FIELD_RET(s, entry->cpu);
1723 SEQ_PUT_FIELD_RET(s, entry->t);
1724
1725 switch (entry->type) {
1726 case TRACE_FN:
1727 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1728 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1729 break;
1730 case TRACE_CTX:
1731 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1732 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1733 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1734 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1735 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001736 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001737 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001738 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001739 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001740 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1741 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1742 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1743 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001744 }
1745 return 1;
1746}
1747
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001748static int trace_empty(struct trace_iterator *iter)
1749{
1750 struct trace_array_cpu *data;
1751 int cpu;
1752
Steven Rostedtab464282008-05-12 21:21:00 +02001753 for_each_tracing_cpu(cpu) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001754 data = iter->tr->data[cpu];
1755
Steven Rostedtb3806b42008-05-12 21:20:46 +02001756 if (head_page(data) && data->trace_idx &&
1757 (data->trace_tail != data->trace_head ||
1758 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001759 return 0;
1760 }
1761 return 1;
1762}
1763
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001764static int print_trace_line(struct trace_iterator *iter)
1765{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001766 if (iter->trace && iter->trace->print_line)
1767 return iter->trace->print_line(iter);
1768
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001769 if (trace_flags & TRACE_ITER_BIN)
1770 return print_bin_fmt(iter);
1771
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001772 if (trace_flags & TRACE_ITER_HEX)
1773 return print_hex_fmt(iter);
1774
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001775 if (trace_flags & TRACE_ITER_RAW)
1776 return print_raw_fmt(iter);
1777
1778 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1779 return print_lat_fmt(iter, iter->idx, iter->cpu);
1780
1781 return print_trace_fmt(iter);
1782}
1783
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001784static int s_show(struct seq_file *m, void *v)
1785{
1786 struct trace_iterator *iter = v;
1787
1788 if (iter->ent == NULL) {
1789 if (iter->tr) {
1790 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1791 seq_puts(m, "#\n");
1792 }
1793 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1794 /* print nothing if the buffers are empty */
1795 if (trace_empty(iter))
1796 return 0;
1797 print_trace_header(m, iter);
1798 if (!(trace_flags & TRACE_ITER_VERBOSE))
1799 print_lat_help_header(m);
1800 } else {
1801 if (!(trace_flags & TRACE_ITER_VERBOSE))
1802 print_func_help_header(m);
1803 }
1804 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001805 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001806 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001807 }
1808
1809 return 0;
1810}
1811
1812static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001813 .start = s_start,
1814 .next = s_next,
1815 .stop = s_stop,
1816 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001817};
1818
Ingo Molnare309b412008-05-12 21:20:51 +02001819static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001820__tracing_open(struct inode *inode, struct file *file, int *ret)
1821{
1822 struct trace_iterator *iter;
1823
Steven Rostedt60a11772008-05-12 21:20:44 +02001824 if (tracing_disabled) {
1825 *ret = -ENODEV;
1826 return NULL;
1827 }
1828
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001829 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1830 if (!iter) {
1831 *ret = -ENOMEM;
1832 goto out;
1833 }
1834
1835 mutex_lock(&trace_types_lock);
1836 if (current_trace && current_trace->print_max)
1837 iter->tr = &max_tr;
1838 else
1839 iter->tr = inode->i_private;
1840 iter->trace = current_trace;
1841 iter->pos = -1;
1842
1843 /* TODO stop tracer */
1844 *ret = seq_open(file, &tracer_seq_ops);
1845 if (!*ret) {
1846 struct seq_file *m = file->private_data;
1847 m->private = iter;
1848
1849 /* stop the trace while dumping */
1850 if (iter->tr->ctrl)
1851 tracer_enabled = 0;
1852
1853 if (iter->trace && iter->trace->open)
1854 iter->trace->open(iter);
1855 } else {
1856 kfree(iter);
1857 iter = NULL;
1858 }
1859 mutex_unlock(&trace_types_lock);
1860
1861 out:
1862 return iter;
1863}
1864
1865int tracing_open_generic(struct inode *inode, struct file *filp)
1866{
Steven Rostedt60a11772008-05-12 21:20:44 +02001867 if (tracing_disabled)
1868 return -ENODEV;
1869
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001870 filp->private_data = inode->i_private;
1871 return 0;
1872}
1873
1874int tracing_release(struct inode *inode, struct file *file)
1875{
1876 struct seq_file *m = (struct seq_file *)file->private_data;
1877 struct trace_iterator *iter = m->private;
1878
1879 mutex_lock(&trace_types_lock);
1880 if (iter->trace && iter->trace->close)
1881 iter->trace->close(iter);
1882
1883 /* reenable tracing if it was previously enabled */
1884 if (iter->tr->ctrl)
1885 tracer_enabled = 1;
1886 mutex_unlock(&trace_types_lock);
1887
1888 seq_release(inode, file);
1889 kfree(iter);
1890 return 0;
1891}
1892
1893static int tracing_open(struct inode *inode, struct file *file)
1894{
1895 int ret;
1896
1897 __tracing_open(inode, file, &ret);
1898
1899 return ret;
1900}
1901
1902static int tracing_lt_open(struct inode *inode, struct file *file)
1903{
1904 struct trace_iterator *iter;
1905 int ret;
1906
1907 iter = __tracing_open(inode, file, &ret);
1908
1909 if (!ret)
1910 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1911
1912 return ret;
1913}
1914
1915
Ingo Molnare309b412008-05-12 21:20:51 +02001916static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001917t_next(struct seq_file *m, void *v, loff_t *pos)
1918{
1919 struct tracer *t = m->private;
1920
1921 (*pos)++;
1922
1923 if (t)
1924 t = t->next;
1925
1926 m->private = t;
1927
1928 return t;
1929}
1930
1931static void *t_start(struct seq_file *m, loff_t *pos)
1932{
1933 struct tracer *t = m->private;
1934 loff_t l = 0;
1935
1936 mutex_lock(&trace_types_lock);
1937 for (; t && l < *pos; t = t_next(m, t, &l))
1938 ;
1939
1940 return t;
1941}
1942
1943static void t_stop(struct seq_file *m, void *p)
1944{
1945 mutex_unlock(&trace_types_lock);
1946}
1947
1948static int t_show(struct seq_file *m, void *v)
1949{
1950 struct tracer *t = v;
1951
1952 if (!t)
1953 return 0;
1954
1955 seq_printf(m, "%s", t->name);
1956 if (t->next)
1957 seq_putc(m, ' ');
1958 else
1959 seq_putc(m, '\n');
1960
1961 return 0;
1962}
1963
1964static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001965 .start = t_start,
1966 .next = t_next,
1967 .stop = t_stop,
1968 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001969};
1970
1971static int show_traces_open(struct inode *inode, struct file *file)
1972{
1973 int ret;
1974
Steven Rostedt60a11772008-05-12 21:20:44 +02001975 if (tracing_disabled)
1976 return -ENODEV;
1977
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001978 ret = seq_open(file, &show_traces_seq_ops);
1979 if (!ret) {
1980 struct seq_file *m = file->private_data;
1981 m->private = trace_types;
1982 }
1983
1984 return ret;
1985}
1986
1987static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001988 .open = tracing_open,
1989 .read = seq_read,
1990 .llseek = seq_lseek,
1991 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001992};
1993
1994static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001995 .open = tracing_lt_open,
1996 .read = seq_read,
1997 .llseek = seq_lseek,
1998 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001999};
2000
2001static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002002 .open = show_traces_open,
2003 .read = seq_read,
2004 .release = seq_release,
2005};
2006
Ingo Molnar36dfe922008-05-12 21:20:52 +02002007/*
2008 * Only trace on a CPU if the bitmask is set:
2009 */
2010static cpumask_t tracing_cpumask = CPU_MASK_ALL;
2011
2012/*
2013 * When tracing/tracing_cpu_mask is modified then this holds
2014 * the new bitmask we are about to install:
2015 */
2016static cpumask_t tracing_cpumask_new;
2017
2018/*
2019 * The tracer itself will not take this lock, but still we want
2020 * to provide a consistent cpumask to user-space:
2021 */
2022static DEFINE_MUTEX(tracing_cpumask_update_lock);
2023
2024/*
2025 * Temporary storage for the character representation of the
2026 * CPU bitmask (and one more byte for the newline):
2027 */
2028static char mask_str[NR_CPUS + 1];
2029
Ingo Molnarc7078de2008-05-12 21:20:52 +02002030static ssize_t
2031tracing_cpumask_read(struct file *filp, char __user *ubuf,
2032 size_t count, loff_t *ppos)
2033{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002034 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002035
2036 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002037
2038 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2039 if (count - len < 2) {
2040 count = -EINVAL;
2041 goto out_err;
2042 }
2043 len += sprintf(mask_str + len, "\n");
2044 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2045
2046out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02002047 mutex_unlock(&tracing_cpumask_update_lock);
2048
2049 return count;
2050}
2051
2052static ssize_t
2053tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2054 size_t count, loff_t *ppos)
2055{
Ingo Molnar36dfe922008-05-12 21:20:52 +02002056 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002057
2058 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02002059 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2060 if (err)
2061 goto err_unlock;
2062
Steven Rostedt92205c22008-05-12 21:20:55 +02002063 raw_local_irq_disable();
2064 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtab464282008-05-12 21:21:00 +02002065 for_each_tracing_cpu(cpu) {
Ingo Molnar36dfe922008-05-12 21:20:52 +02002066 /*
2067 * Increase/decrease the disabled counter if we are
2068 * about to flip a bit in the cpumask:
2069 */
2070 if (cpu_isset(cpu, tracing_cpumask) &&
2071 !cpu_isset(cpu, tracing_cpumask_new)) {
2072 atomic_inc(&global_trace.data[cpu]->disabled);
2073 }
2074 if (!cpu_isset(cpu, tracing_cpumask) &&
2075 cpu_isset(cpu, tracing_cpumask_new)) {
2076 atomic_dec(&global_trace.data[cpu]->disabled);
2077 }
2078 }
Steven Rostedt92205c22008-05-12 21:20:55 +02002079 __raw_spin_unlock(&ftrace_max_lock);
2080 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02002081
2082 tracing_cpumask = tracing_cpumask_new;
2083
Ingo Molnarc7078de2008-05-12 21:20:52 +02002084 mutex_unlock(&tracing_cpumask_update_lock);
2085
Ingo Molnarc7078de2008-05-12 21:20:52 +02002086 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02002087
2088err_unlock:
2089 mutex_unlock(&tracing_cpumask_update_lock);
2090
2091 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02002092}
2093
2094static struct file_operations tracing_cpumask_fops = {
2095 .open = tracing_open_generic,
2096 .read = tracing_cpumask_read,
2097 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002098};
2099
2100static ssize_t
2101tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
2102 size_t cnt, loff_t *ppos)
2103{
2104 char *buf;
2105 int r = 0;
2106 int len = 0;
2107 int i;
2108
2109 /* calulate max size */
2110 for (i = 0; trace_options[i]; i++) {
2111 len += strlen(trace_options[i]);
2112 len += 3; /* "no" and space */
2113 }
2114
2115 /* +2 for \n and \0 */
2116 buf = kmalloc(len + 2, GFP_KERNEL);
2117 if (!buf)
2118 return -ENOMEM;
2119
2120 for (i = 0; trace_options[i]; i++) {
2121 if (trace_flags & (1 << i))
2122 r += sprintf(buf + r, "%s ", trace_options[i]);
2123 else
2124 r += sprintf(buf + r, "no%s ", trace_options[i]);
2125 }
2126
2127 r += sprintf(buf + r, "\n");
2128 WARN_ON(r >= len + 2);
2129
Ingo Molnar36dfe922008-05-12 21:20:52 +02002130 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002131
2132 kfree(buf);
2133
2134 return r;
2135}
2136
2137static ssize_t
2138tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
2139 size_t cnt, loff_t *ppos)
2140{
2141 char buf[64];
2142 char *cmp = buf;
2143 int neg = 0;
2144 int i;
2145
Steven Rostedtcffae432008-05-12 21:21:00 +02002146 if (cnt >= sizeof(buf))
2147 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002148
2149 if (copy_from_user(&buf, ubuf, cnt))
2150 return -EFAULT;
2151
2152 buf[cnt] = 0;
2153
2154 if (strncmp(buf, "no", 2) == 0) {
2155 neg = 1;
2156 cmp += 2;
2157 }
2158
2159 for (i = 0; trace_options[i]; i++) {
2160 int len = strlen(trace_options[i]);
2161
2162 if (strncmp(cmp, trace_options[i], len) == 0) {
2163 if (neg)
2164 trace_flags &= ~(1 << i);
2165 else
2166 trace_flags |= (1 << i);
2167 break;
2168 }
2169 }
Ingo Molnar442e5442008-05-12 21:20:53 +02002170 /*
2171 * If no option could be set, return an error:
2172 */
2173 if (!trace_options[i])
2174 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002175
2176 filp->f_pos += cnt;
2177
2178 return cnt;
2179}
2180
2181static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002182 .open = tracing_open_generic,
2183 .read = tracing_iter_ctrl_read,
2184 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002185};
2186
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002187static const char readme_msg[] =
2188 "tracing mini-HOWTO:\n\n"
2189 "# mkdir /debug\n"
2190 "# mount -t debugfs nodev /debug\n\n"
2191 "# cat /debug/tracing/available_tracers\n"
2192 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2193 "# cat /debug/tracing/current_tracer\n"
2194 "none\n"
2195 "# echo sched_switch > /debug/tracing/current_tracer\n"
2196 "# cat /debug/tracing/current_tracer\n"
2197 "sched_switch\n"
2198 "# cat /debug/tracing/iter_ctrl\n"
2199 "noprint-parent nosym-offset nosym-addr noverbose\n"
2200 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2201 "# echo 1 > /debug/tracing/tracing_enabled\n"
2202 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2203 "echo 0 > /debug/tracing/tracing_enabled\n"
2204;
2205
2206static ssize_t
2207tracing_readme_read(struct file *filp, char __user *ubuf,
2208 size_t cnt, loff_t *ppos)
2209{
2210 return simple_read_from_buffer(ubuf, cnt, ppos,
2211 readme_msg, strlen(readme_msg));
2212}
2213
2214static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02002215 .open = tracing_open_generic,
2216 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002217};
2218
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002219static ssize_t
2220tracing_ctrl_read(struct file *filp, char __user *ubuf,
2221 size_t cnt, loff_t *ppos)
2222{
2223 struct trace_array *tr = filp->private_data;
2224 char buf[64];
2225 int r;
2226
2227 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002228 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002229}
2230
2231static ssize_t
2232tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2233 size_t cnt, loff_t *ppos)
2234{
2235 struct trace_array *tr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002236 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002237 long val;
2238 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002239
Steven Rostedtcffae432008-05-12 21:21:00 +02002240 if (cnt >= sizeof(buf))
2241 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002242
2243 if (copy_from_user(&buf, ubuf, cnt))
2244 return -EFAULT;
2245
2246 buf[cnt] = 0;
2247
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002248 ret = strict_strtoul(buf, 10, &val);
2249 if (ret < 0)
2250 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002251
2252 val = !!val;
2253
2254 mutex_lock(&trace_types_lock);
2255 if (tr->ctrl ^ val) {
2256 if (val)
2257 tracer_enabled = 1;
2258 else
2259 tracer_enabled = 0;
2260
2261 tr->ctrl = val;
2262
2263 if (current_trace && current_trace->ctrl_update)
2264 current_trace->ctrl_update(tr);
2265 }
2266 mutex_unlock(&trace_types_lock);
2267
2268 filp->f_pos += cnt;
2269
2270 return cnt;
2271}
2272
2273static ssize_t
2274tracing_set_trace_read(struct file *filp, char __user *ubuf,
2275 size_t cnt, loff_t *ppos)
2276{
2277 char buf[max_tracer_type_len+2];
2278 int r;
2279
2280 mutex_lock(&trace_types_lock);
2281 if (current_trace)
2282 r = sprintf(buf, "%s\n", current_trace->name);
2283 else
2284 r = sprintf(buf, "\n");
2285 mutex_unlock(&trace_types_lock);
2286
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002287 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002288}
2289
2290static ssize_t
2291tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2292 size_t cnt, loff_t *ppos)
2293{
2294 struct trace_array *tr = &global_trace;
2295 struct tracer *t;
2296 char buf[max_tracer_type_len+1];
2297 int i;
2298
2299 if (cnt > max_tracer_type_len)
2300 cnt = max_tracer_type_len;
2301
2302 if (copy_from_user(&buf, ubuf, cnt))
2303 return -EFAULT;
2304
2305 buf[cnt] = 0;
2306
2307 /* strip ending whitespace. */
2308 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2309 buf[i] = 0;
2310
2311 mutex_lock(&trace_types_lock);
2312 for (t = trace_types; t; t = t->next) {
2313 if (strcmp(t->name, buf) == 0)
2314 break;
2315 }
2316 if (!t || t == current_trace)
2317 goto out;
2318
2319 if (current_trace && current_trace->reset)
2320 current_trace->reset(tr);
2321
2322 current_trace = t;
2323 if (t->init)
2324 t->init(tr);
2325
2326 out:
2327 mutex_unlock(&trace_types_lock);
2328
2329 filp->f_pos += cnt;
2330
2331 return cnt;
2332}
2333
2334static ssize_t
2335tracing_max_lat_read(struct file *filp, char __user *ubuf,
2336 size_t cnt, loff_t *ppos)
2337{
2338 unsigned long *ptr = filp->private_data;
2339 char buf[64];
2340 int r;
2341
Steven Rostedtcffae432008-05-12 21:21:00 +02002342 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002343 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002344 if (r > sizeof(buf))
2345 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002346 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002347}
2348
2349static ssize_t
2350tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2351 size_t cnt, loff_t *ppos)
2352{
2353 long *ptr = filp->private_data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002354 char buf[64];
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002355 long val;
2356 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002357
Steven Rostedtcffae432008-05-12 21:21:00 +02002358 if (cnt >= sizeof(buf))
2359 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002360
2361 if (copy_from_user(&buf, ubuf, cnt))
2362 return -EFAULT;
2363
2364 buf[cnt] = 0;
2365
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002366 ret = strict_strtoul(buf, 10, &val);
2367 if (ret < 0)
2368 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002369
2370 *ptr = val * 1000;
2371
2372 return cnt;
2373}
2374
Steven Rostedtb3806b42008-05-12 21:20:46 +02002375static atomic_t tracing_reader;
2376
2377static int tracing_open_pipe(struct inode *inode, struct file *filp)
2378{
2379 struct trace_iterator *iter;
2380
2381 if (tracing_disabled)
2382 return -ENODEV;
2383
2384 /* We only allow for reader of the pipe */
2385 if (atomic_inc_return(&tracing_reader) != 1) {
2386 atomic_dec(&tracing_reader);
2387 return -EBUSY;
2388 }
2389
2390 /* create a buffer to store the information to pass to userspace */
2391 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2392 if (!iter)
2393 return -ENOMEM;
2394
Steven Rostedt107bad82008-05-12 21:21:01 +02002395 mutex_lock(&trace_types_lock);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002396 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002397 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002398 filp->private_data = iter;
2399
Steven Rostedt107bad82008-05-12 21:21:01 +02002400 if (iter->trace->pipe_open)
2401 iter->trace->pipe_open(iter);
2402 mutex_unlock(&trace_types_lock);
2403
Steven Rostedtb3806b42008-05-12 21:20:46 +02002404 return 0;
2405}
2406
2407static int tracing_release_pipe(struct inode *inode, struct file *file)
2408{
2409 struct trace_iterator *iter = file->private_data;
2410
2411 kfree(iter);
2412 atomic_dec(&tracing_reader);
2413
2414 return 0;
2415}
2416
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002417static unsigned int
2418tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2419{
2420 struct trace_iterator *iter = filp->private_data;
2421
2422 if (trace_flags & TRACE_ITER_BLOCK) {
2423 /*
2424 * Always select as readable when in blocking mode
2425 */
2426 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002427 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002428 if (!trace_empty(iter))
2429 return POLLIN | POLLRDNORM;
2430 poll_wait(filp, &trace_wait, poll_table);
2431 if (!trace_empty(iter))
2432 return POLLIN | POLLRDNORM;
2433
2434 return 0;
2435 }
2436}
2437
Steven Rostedtb3806b42008-05-12 21:20:46 +02002438/*
2439 * Consumer reader.
2440 */
2441static ssize_t
2442tracing_read_pipe(struct file *filp, char __user *ubuf,
2443 size_t cnt, loff_t *ppos)
2444{
2445 struct trace_iterator *iter = filp->private_data;
2446 struct trace_array_cpu *data;
2447 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002448 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002449#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002450 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002451#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002452 int cpu;
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002453 ssize_t sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002454
2455 /* return any leftover data */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002456 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2457 if (sret != -EBUSY)
2458 return sret;
2459 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002460
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002461 trace_seq_reset(&iter->seq);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002462
Steven Rostedt107bad82008-05-12 21:21:01 +02002463 mutex_lock(&trace_types_lock);
2464 if (iter->trace->read) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002465 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2466 if (sret)
Steven Rostedt107bad82008-05-12 21:21:01 +02002467 goto out;
Steven Rostedt107bad82008-05-12 21:21:01 +02002468 }
2469
Steven Rostedtb3806b42008-05-12 21:20:46 +02002470 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002471
Steven Rostedt107bad82008-05-12 21:21:01 +02002472 if ((filp->f_flags & O_NONBLOCK)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002473 sret = -EAGAIN;
Steven Rostedt107bad82008-05-12 21:21:01 +02002474 goto out;
2475 }
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002476
Steven Rostedtb3806b42008-05-12 21:20:46 +02002477 /*
2478 * This is a make-shift waitqueue. The reason we don't use
2479 * an actual wait queue is because:
2480 * 1) we only ever have one waiter
2481 * 2) the tracing, traces all functions, we don't want
2482 * the overhead of calling wake_up and friends
2483 * (and tracing them too)
2484 * Anyway, this is really very primitive wakeup.
2485 */
2486 set_current_state(TASK_INTERRUPTIBLE);
2487 iter->tr->waiter = current;
2488
Steven Rostedt107bad82008-05-12 21:21:01 +02002489 mutex_unlock(&trace_types_lock);
2490
Ingo Molnar9fe068e2008-05-12 21:21:02 +02002491 /* sleep for 100 msecs, and try again. */
2492 schedule_timeout(HZ/10);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002493
Steven Rostedt107bad82008-05-12 21:21:01 +02002494 mutex_lock(&trace_types_lock);
2495
Steven Rostedtb3806b42008-05-12 21:20:46 +02002496 iter->tr->waiter = NULL;
2497
Steven Rostedt107bad82008-05-12 21:21:01 +02002498 if (signal_pending(current)) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002499 sret = -EINTR;
Steven Rostedt107bad82008-05-12 21:21:01 +02002500 goto out;
2501 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002502
Steven Rostedt84527992008-05-12 21:20:58 +02002503 if (iter->trace != current_trace)
Steven Rostedt107bad82008-05-12 21:21:01 +02002504 goto out;
Steven Rostedt84527992008-05-12 21:20:58 +02002505
Steven Rostedtb3806b42008-05-12 21:20:46 +02002506 /*
2507 * We block until we read something and tracing is disabled.
2508 * We still block if tracing is disabled, but we have never
2509 * read anything. This allows a user to cat this file, and
2510 * then enable tracing. But after we have read something,
2511 * we give an EOF when tracing is again disabled.
2512 *
2513 * iter->pos will be 0 if we haven't read anything.
2514 */
2515 if (!tracer_enabled && iter->pos)
2516 break;
2517
2518 continue;
2519 }
2520
2521 /* stop when tracing is finished */
2522 if (trace_empty(iter))
Steven Rostedt107bad82008-05-12 21:21:01 +02002523 goto out;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002524
2525 if (cnt >= PAGE_SIZE)
2526 cnt = PAGE_SIZE - 1;
2527
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002528 /* reset all but tr, trace, and overruns */
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002529 memset(&iter->seq, 0,
2530 sizeof(struct trace_iterator) -
2531 offsetof(struct trace_iterator, seq));
Steven Rostedt4823ed72008-05-12 21:21:01 +02002532 iter->pos = -1;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002533
2534 /*
2535 * We need to stop all tracing on all CPUS to read the
2536 * the next buffer. This is a bit expensive, but is
2537 * not done often. We fill all what we can read,
2538 * and then release the locks again.
2539 */
2540
2541 cpus_clear(mask);
2542 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002543#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002544 ftrace_save = ftrace_enabled;
2545 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002546#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002547 smp_wmb();
Steven Rostedtab464282008-05-12 21:21:00 +02002548 for_each_tracing_cpu(cpu) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002549 data = iter->tr->data[cpu];
2550
2551 if (!head_page(data) || !data->trace_idx)
2552 continue;
2553
2554 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002555 cpu_set(cpu, mask);
2556 }
2557
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002558 for_each_cpu_mask(cpu, mask) {
2559 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002560 __raw_spin_lock(&data->lock);
Steven Rostedt53d0aa72008-05-12 21:21:01 +02002561
2562 if (data->overrun > iter->last_overrun[cpu])
2563 iter->overrun[cpu] +=
2564 data->overrun - iter->last_overrun[cpu];
2565 iter->last_overrun[cpu] = data->overrun;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002566 }
2567
Steven Rostedt088b1e422008-05-12 21:20:48 +02002568 while (find_next_entry_inc(iter) != NULL) {
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002569 int ret;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002570 int len = iter->seq.len;
2571
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002572 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002573 if (!ret) {
2574 /* don't print partial lines */
2575 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002576 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002577 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002578
2579 trace_consume(iter);
2580
2581 if (iter->seq.len >= cnt)
2582 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002583 }
2584
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002585 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002586 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002587 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002588 }
2589
2590 for_each_cpu_mask(cpu, mask) {
2591 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002592 atomic_dec(&data->disabled);
2593 }
Ingo Molnar25770462008-05-12 21:20:49 +02002594#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002595 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002596#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002597 local_irq_restore(flags);
2598
2599 /* Now copy what we have to the user */
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002600 sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2601 if (iter->seq.readpos >= iter->seq.len)
Steven Rostedtb3806b42008-05-12 21:20:46 +02002602 trace_seq_reset(&iter->seq);
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002603 if (sret == -EBUSY)
2604 sret = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002605
Steven Rostedt107bad82008-05-12 21:21:01 +02002606out:
2607 mutex_unlock(&trace_types_lock);
2608
Pekka Paalanen6c6c2792008-05-12 21:21:02 +02002609 return sret;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002610}
2611
Steven Rostedta98a3c32008-05-12 21:20:59 +02002612static ssize_t
2613tracing_entries_read(struct file *filp, char __user *ubuf,
2614 size_t cnt, loff_t *ppos)
2615{
2616 struct trace_array *tr = filp->private_data;
2617 char buf[64];
2618 int r;
2619
2620 r = sprintf(buf, "%lu\n", tr->entries);
2621 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2622}
2623
2624static ssize_t
2625tracing_entries_write(struct file *filp, const char __user *ubuf,
2626 size_t cnt, loff_t *ppos)
2627{
2628 unsigned long val;
2629 char buf[64];
Steven Rostedt19384c02008-05-22 00:22:16 -04002630 int i, ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002631
Steven Rostedtcffae432008-05-12 21:21:00 +02002632 if (cnt >= sizeof(buf))
2633 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002634
2635 if (copy_from_user(&buf, ubuf, cnt))
2636 return -EFAULT;
2637
2638 buf[cnt] = 0;
2639
Steven Rostedtc6caeeb2008-05-12 21:21:00 +02002640 ret = strict_strtoul(buf, 10, &val);
2641 if (ret < 0)
2642 return ret;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002643
2644 /* must have at least 1 entry */
2645 if (!val)
2646 return -EINVAL;
2647
2648 mutex_lock(&trace_types_lock);
2649
2650 if (current_trace != &no_tracer) {
2651 cnt = -EBUSY;
2652 pr_info("ftrace: set current_tracer to none"
2653 " before modifying buffer size\n");
2654 goto out;
2655 }
2656
2657 if (val > global_trace.entries) {
Steven Rostedt3eefae92008-05-12 21:21:04 +02002658 long pages_requested;
2659 unsigned long freeable_pages;
2660
2661 /* make sure we have enough memory before mapping */
2662 pages_requested =
2663 (val + (ENTRIES_PER_PAGE-1)) / ENTRIES_PER_PAGE;
2664
2665 /* account for each buffer (and max_tr) */
2666 pages_requested *= tracing_nr_buffers * 2;
2667
2668 /* Check for overflow */
2669 if (pages_requested < 0) {
2670 cnt = -ENOMEM;
2671 goto out;
2672 }
2673
2674 freeable_pages = determine_dirtyable_memory();
2675
2676 /* we only allow to request 1/4 of useable memory */
2677 if (pages_requested >
2678 ((freeable_pages + tracing_pages_allocated) / 4)) {
2679 cnt = -ENOMEM;
2680 goto out;
2681 }
2682
Steven Rostedta98a3c32008-05-12 21:20:59 +02002683 while (global_trace.entries < val) {
2684 if (trace_alloc_page()) {
2685 cnt = -ENOMEM;
2686 goto out;
2687 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002688 /* double check that we don't go over the known pages */
2689 if (tracing_pages_allocated > pages_requested)
2690 break;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002691 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002692
Steven Rostedta98a3c32008-05-12 21:20:59 +02002693 } else {
2694 /* include the number of entries in val (inc of page entries) */
2695 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2696 trace_free_page();
2697 }
2698
Steven Rostedt19384c02008-05-22 00:22:16 -04002699 /* check integrity */
2700 for_each_tracing_cpu(i)
2701 check_pages(global_trace.data[i]);
2702
Steven Rostedta98a3c32008-05-12 21:20:59 +02002703 filp->f_pos += cnt;
2704
Steven Rostedt19384c02008-05-22 00:22:16 -04002705 /* If check pages failed, return ENOMEM */
2706 if (tracing_disabled)
2707 cnt = -ENOMEM;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002708 out:
2709 max_tr.entries = global_trace.entries;
2710 mutex_unlock(&trace_types_lock);
2711
2712 return cnt;
2713}
2714
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002715static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002716 .open = tracing_open_generic,
2717 .read = tracing_max_lat_read,
2718 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002719};
2720
2721static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002722 .open = tracing_open_generic,
2723 .read = tracing_ctrl_read,
2724 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002725};
2726
2727static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002728 .open = tracing_open_generic,
2729 .read = tracing_set_trace_read,
2730 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002731};
2732
Steven Rostedtb3806b42008-05-12 21:20:46 +02002733static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002734 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002735 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002736 .read = tracing_read_pipe,
2737 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002738};
2739
Steven Rostedta98a3c32008-05-12 21:20:59 +02002740static struct file_operations tracing_entries_fops = {
2741 .open = tracing_open_generic,
2742 .read = tracing_entries_read,
2743 .write = tracing_entries_write,
2744};
2745
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002746#ifdef CONFIG_DYNAMIC_FTRACE
2747
2748static ssize_t
2749tracing_read_long(struct file *filp, char __user *ubuf,
2750 size_t cnt, loff_t *ppos)
2751{
2752 unsigned long *p = filp->private_data;
2753 char buf[64];
2754 int r;
2755
2756 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002757
2758 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002759}
2760
2761static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002762 .open = tracing_open_generic,
2763 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002764};
2765#endif
2766
2767static struct dentry *d_tracer;
2768
2769struct dentry *tracing_init_dentry(void)
2770{
2771 static int once;
2772
2773 if (d_tracer)
2774 return d_tracer;
2775
2776 d_tracer = debugfs_create_dir("tracing", NULL);
2777
2778 if (!d_tracer && !once) {
2779 once = 1;
2780 pr_warning("Could not create debugfs directory 'tracing'\n");
2781 return NULL;
2782 }
2783
2784 return d_tracer;
2785}
2786
Steven Rostedt60a11772008-05-12 21:20:44 +02002787#ifdef CONFIG_FTRACE_SELFTEST
2788/* Let selftest have access to static functions in this file */
2789#include "trace_selftest.c"
2790#endif
2791
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002792static __init void tracer_init_debugfs(void)
2793{
2794 struct dentry *d_tracer;
2795 struct dentry *entry;
2796
2797 d_tracer = tracing_init_dentry();
2798
2799 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2800 &global_trace, &tracing_ctrl_fops);
2801 if (!entry)
2802 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2803
2804 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2805 NULL, &tracing_iter_fops);
2806 if (!entry)
2807 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2808
Ingo Molnarc7078de2008-05-12 21:20:52 +02002809 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2810 NULL, &tracing_cpumask_fops);
2811 if (!entry)
2812 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2813
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002814 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2815 &global_trace, &tracing_lt_fops);
2816 if (!entry)
2817 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2818
2819 entry = debugfs_create_file("trace", 0444, d_tracer,
2820 &global_trace, &tracing_fops);
2821 if (!entry)
2822 pr_warning("Could not create debugfs 'trace' entry\n");
2823
2824 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2825 &global_trace, &show_traces_fops);
2826 if (!entry)
2827 pr_warning("Could not create debugfs 'trace' entry\n");
2828
2829 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2830 &global_trace, &set_tracer_fops);
2831 if (!entry)
2832 pr_warning("Could not create debugfs 'trace' entry\n");
2833
2834 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2835 &tracing_max_latency,
2836 &tracing_max_lat_fops);
2837 if (!entry)
2838 pr_warning("Could not create debugfs "
2839 "'tracing_max_latency' entry\n");
2840
2841 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2842 &tracing_thresh, &tracing_max_lat_fops);
2843 if (!entry)
2844 pr_warning("Could not create debugfs "
2845 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002846 entry = debugfs_create_file("README", 0644, d_tracer,
2847 NULL, &tracing_readme_fops);
2848 if (!entry)
2849 pr_warning("Could not create debugfs 'README' entry\n");
2850
Steven Rostedtb3806b42008-05-12 21:20:46 +02002851 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2852 NULL, &tracing_pipe_fops);
2853 if (!entry)
2854 pr_warning("Could not create debugfs "
2855 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002856
Steven Rostedta98a3c32008-05-12 21:20:59 +02002857 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2858 &global_trace, &tracing_entries_fops);
2859 if (!entry)
2860 pr_warning("Could not create debugfs "
2861 "'tracing_threash' entry\n");
2862
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002863#ifdef CONFIG_DYNAMIC_FTRACE
2864 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2865 &ftrace_update_tot_cnt,
2866 &tracing_read_long_fops);
2867 if (!entry)
2868 pr_warning("Could not create debugfs "
2869 "'dyn_ftrace_total_info' entry\n");
2870#endif
2871}
2872
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002873static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002874{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002875 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002876 struct page *page, *tmp;
2877 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002878 void *array;
Steven Rostedt3eefae92008-05-12 21:21:04 +02002879 unsigned pages_allocated = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002880 int i;
2881
2882 /* first allocate a page for each CPU */
Steven Rostedtab464282008-05-12 21:21:00 +02002883 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002884 array = (void *)__get_free_page(GFP_KERNEL);
2885 if (array == NULL) {
2886 printk(KERN_ERR "tracer: failed to allocate page"
2887 "for trace buffer!\n");
2888 goto free_pages;
2889 }
2890
Steven Rostedt3eefae92008-05-12 21:21:04 +02002891 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002892 page = virt_to_page(array);
2893 list_add(&page->lru, &pages);
2894
2895/* Only allocate if we are actually using the max trace */
2896#ifdef CONFIG_TRACER_MAX_TRACE
2897 array = (void *)__get_free_page(GFP_KERNEL);
2898 if (array == NULL) {
2899 printk(KERN_ERR "tracer: failed to allocate page"
2900 "for trace buffer!\n");
2901 goto free_pages;
2902 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002903 pages_allocated++;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002904 page = virt_to_page(array);
2905 list_add(&page->lru, &pages);
2906#endif
2907 }
2908
2909 /* Now that we successfully allocate a page per CPU, add them */
Steven Rostedtab464282008-05-12 21:21:00 +02002910 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002911 data = global_trace.data[i];
2912 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002913 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002914 list_add_tail(&page->lru, &data->trace_pages);
2915 ClearPageLRU(page);
2916
2917#ifdef CONFIG_TRACER_MAX_TRACE
2918 data = max_tr.data[i];
2919 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002920 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002921 list_add_tail(&page->lru, &data->trace_pages);
2922 SetPageLRU(page);
2923#endif
2924 }
Steven Rostedt3eefae92008-05-12 21:21:04 +02002925 tracing_pages_allocated += pages_allocated;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002926 global_trace.entries += ENTRIES_PER_PAGE;
2927
2928 return 0;
2929
2930 free_pages:
2931 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002932 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002933 __free_page(page);
2934 }
2935 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002936}
2937
Steven Rostedta98a3c32008-05-12 21:20:59 +02002938static int trace_free_page(void)
2939{
2940 struct trace_array_cpu *data;
2941 struct page *page;
2942 struct list_head *p;
2943 int i;
2944 int ret = 0;
2945
2946 /* free one page from each buffer */
Steven Rostedtab464282008-05-12 21:21:00 +02002947 for_each_tracing_cpu(i) {
Steven Rostedta98a3c32008-05-12 21:20:59 +02002948 data = global_trace.data[i];
2949 p = data->trace_pages.next;
2950 if (p == &data->trace_pages) {
2951 /* should never happen */
2952 WARN_ON(1);
2953 tracing_disabled = 1;
2954 ret = -1;
2955 break;
2956 }
2957 page = list_entry(p, struct page, lru);
2958 ClearPageLRU(page);
2959 list_del(&page->lru);
Steven Rostedt3eefae92008-05-12 21:21:04 +02002960 tracing_pages_allocated--;
2961 tracing_pages_allocated--;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002962 __free_page(page);
2963
2964 tracing_reset(data);
2965
2966#ifdef CONFIG_TRACER_MAX_TRACE
2967 data = max_tr.data[i];
2968 p = data->trace_pages.next;
2969 if (p == &data->trace_pages) {
2970 /* should never happen */
2971 WARN_ON(1);
2972 tracing_disabled = 1;
2973 ret = -1;
2974 break;
2975 }
2976 page = list_entry(p, struct page, lru);
2977 ClearPageLRU(page);
2978 list_del(&page->lru);
2979 __free_page(page);
2980
2981 tracing_reset(data);
2982#endif
2983 }
2984 global_trace.entries -= ENTRIES_PER_PAGE;
2985
2986 return ret;
2987}
2988
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002989__init static int tracer_alloc_buffers(void)
2990{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002991 struct trace_array_cpu *data;
2992 void *array;
2993 struct page *page;
2994 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002995 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002996 int i;
2997
Steven Rostedtab464282008-05-12 21:21:00 +02002998 /* TODO: make the number of buffers hot pluggable with CPUS */
2999 tracing_nr_buffers = num_possible_cpus();
3000 tracing_buffer_mask = cpu_possible_map;
3001
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003002 /* Allocate the first page for all buffers */
Steven Rostedtab464282008-05-12 21:21:00 +02003003 for_each_tracing_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003004 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003005 max_tr.data[i] = &per_cpu(max_data, i);
3006
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003007 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003008 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003009 printk(KERN_ERR "tracer: failed to allocate page"
3010 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003011 goto free_buffers;
3012 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003013
3014 /* set the array to the list */
3015 INIT_LIST_HEAD(&data->trace_pages);
3016 page = virt_to_page(array);
3017 list_add(&page->lru, &data->trace_pages);
3018 /* use the LRU flag to differentiate the two buffers */
3019 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003020
Steven Rostedta98a3c32008-05-12 21:20:59 +02003021 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3022 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
3023
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003024/* Only allocate if we are actually using the max trace */
3025#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003026 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003027 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003028 printk(KERN_ERR "tracer: failed to allocate page"
3029 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003030 goto free_buffers;
3031 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003032
3033 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
3034 page = virt_to_page(array);
3035 list_add(&page->lru, &max_tr.data[i]->trace_pages);
3036 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003037#endif
3038 }
3039
3040 /*
3041 * Since we allocate by orders of pages, we may be able to
3042 * round up a bit.
3043 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003044 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003045 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003046
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003047 while (global_trace.entries < trace_nr_entries) {
3048 if (trace_alloc_page())
3049 break;
3050 pages++;
3051 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02003052 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003053
3054 pr_info("tracer: %d pages allocated for %ld",
3055 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003056 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
3057 pr_info(" actual entries %ld\n", global_trace.entries);
3058
3059 tracer_init_debugfs();
3060
3061 trace_init_cmdlines();
3062
3063 register_tracer(&no_tracer);
3064 current_trace = &no_tracer;
3065
Steven Rostedt60a11772008-05-12 21:20:44 +02003066 /* All seems OK, enable tracing */
Steven Rostedt4902f882008-05-22 00:22:18 -04003067 global_trace.ctrl = tracer_enabled;
Steven Rostedt60a11772008-05-12 21:20:44 +02003068 tracing_disabled = 0;
3069
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003070 return 0;
3071
3072 free_buffers:
3073 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003074 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003075 struct trace_array_cpu *data = global_trace.data[i];
3076
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003077 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003078 list_for_each_entry_safe(page, tmp,
3079 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003080 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003081 __free_page(page);
3082 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003083 }
3084
3085#ifdef CONFIG_TRACER_MAX_TRACE
3086 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003087 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003088 list_for_each_entry_safe(page, tmp,
3089 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003090 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02003091 __free_page(page);
3092 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003093 }
3094#endif
3095 }
Steven Rostedt60a11772008-05-12 21:20:44 +02003096 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02003097}
Steven Rostedt60a11772008-05-12 21:20:44 +02003098fs_initcall(tracer_alloc_buffers);