blob: 627e39936ea75fff465b9b77630cf85e246c96e9 [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>
30
Ingo Molnar86387f72008-05-12 21:20:51 +020031#include <linux/stacktrace.h>
32
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020033#include "trace.h"
34
35unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36unsigned long __read_mostly tracing_thresh;
37
Steven Rostedta98a3c32008-05-12 21:20:59 +020038/* dummy trace to disable tracing */
39static struct tracer no_tracer __read_mostly =
40{
41 .name = "none",
42};
43
44static int trace_alloc_page(void);
45static int trace_free_page(void);
46
Steven Rostedt60a11772008-05-12 21:20:44 +020047static int tracing_disabled = 1;
48
Thomas Gleixner72829bc2008-05-23 21:37:28 +020049long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020050ns2usecs(cycle_t nsec)
51{
52 nsec += 500;
53 do_div(nsec, 1000);
54 return nsec;
55}
56
Ingo Molnare309b412008-05-12 21:20:51 +020057cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020058{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020059 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020060}
61
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020062static struct trace_array global_trace;
63
64static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
65
66static struct trace_array max_tr;
67
68static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
69
Steven Rostedt26994ea2008-05-12 21:20:48 +020070static int tracer_enabled = 1;
Ingo Molnar57422792008-05-12 21:20:51 +020071static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020072
73static struct tracer *trace_types __read_mostly;
74static struct tracer *current_trace __read_mostly;
75static int max_tracer_type_len;
76
77static DEFINE_MUTEX(trace_types_lock);
Ingo Molnar4e655512008-05-12 21:20:52 +020078static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
79
80unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
81
Ingo Molnar4e655512008-05-12 21:20:52 +020082void trace_wake_up(void)
83{
Ingo Molnar017730c2008-05-12 21:20:52 +020084 /*
85 * The runqueue_is_locked() can fail, but this is the best we
86 * have for now:
87 */
88 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +020089 wake_up(&trace_wait);
90}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020091
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020092#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
93
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020094static int __init set_nr_entries(char *str)
95{
96 if (!str)
97 return 0;
98 trace_nr_entries = simple_strtoul(str, &str, 0);
99 return 1;
100}
101__setup("trace_entries=", set_nr_entries);
102
Steven Rostedt57f50be2008-05-12 21:20:44 +0200103unsigned long nsecs_to_usecs(unsigned long nsecs)
104{
105 return nsecs / 1000;
106}
107
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200108enum trace_flag_type {
109 TRACE_FLAG_IRQS_OFF = 0x01,
110 TRACE_FLAG_NEED_RESCHED = 0x02,
111 TRACE_FLAG_HARDIRQ = 0x04,
112 TRACE_FLAG_SOFTIRQ = 0x08,
113};
114
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200115#define TRACE_ITER_SYM_MASK \
116 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
117
118/* These must match the bit postions above */
119static const char *trace_options[] = {
120 "print-parent",
121 "sym-offset",
122 "sym-addr",
123 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200124 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200125 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200126 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200127 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200128 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200129 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200130 NULL
131};
132
Steven Rostedt92205c22008-05-12 21:20:55 +0200133static raw_spinlock_t ftrace_max_lock =
134 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200135
136/*
137 * Copy the new maximum trace into the separate maximum-trace
138 * structure. (this way the maximum trace is permanently saved,
139 * for later retrieval via /debugfs/tracing/latency_trace)
140 */
Ingo Molnare309b412008-05-12 21:20:51 +0200141static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200142__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
143{
144 struct trace_array_cpu *data = tr->data[cpu];
145
146 max_tr.cpu = cpu;
147 max_tr.time_start = data->preempt_timestamp;
148
149 data = max_tr.data[cpu];
150 data->saved_latency = tracing_max_latency;
151
152 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
153 data->pid = tsk->pid;
154 data->uid = tsk->uid;
155 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
156 data->policy = tsk->policy;
157 data->rt_priority = tsk->rt_priority;
158
159 /* record this tasks comm */
160 tracing_record_cmdline(current);
161}
162
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200163void check_pages(struct trace_array_cpu *data)
164{
165 struct page *page, *tmp;
166
167 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
168 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
169
170 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
171 BUG_ON(page->lru.next->prev != &page->lru);
172 BUG_ON(page->lru.prev->next != &page->lru);
173 }
174}
175
176void *head_page(struct trace_array_cpu *data)
177{
178 struct page *page;
179
180 check_pages(data);
181 if (list_empty(&data->trace_pages))
182 return NULL;
183
184 page = list_entry(data->trace_pages.next, struct page, lru);
185 BUG_ON(&page->lru == &data->trace_pages);
186
187 return page_address(page);
188}
189
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200190int
Steven Rostedt214023c2008-05-12 21:20:46 +0200191trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
192{
193 int len = (PAGE_SIZE - 1) - s->len;
194 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200195 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200196
197 if (!len)
198 return 0;
199
200 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200201 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200202 va_end(ap);
203
Steven Rostedtb3806b42008-05-12 21:20:46 +0200204 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200205 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200206 return 0;
207
208 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200209
210 return len;
211}
212
Ingo Molnare309b412008-05-12 21:20:51 +0200213static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200214trace_seq_puts(struct trace_seq *s, const char *str)
215{
216 int len = strlen(str);
217
218 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200219 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200220
221 memcpy(s->buffer + s->len, str, len);
222 s->len += len;
223
224 return len;
225}
226
Ingo Molnare309b412008-05-12 21:20:51 +0200227static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200228trace_seq_putc(struct trace_seq *s, unsigned char c)
229{
230 if (s->len >= (PAGE_SIZE - 1))
231 return 0;
232
233 s->buffer[s->len++] = c;
234
235 return 1;
236}
237
Ingo Molnare309b412008-05-12 21:20:51 +0200238static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200239trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
240{
241 if (len > ((PAGE_SIZE - 1) - s->len))
242 return 0;
243
244 memcpy(s->buffer + s->len, mem, len);
245 s->len += len;
246
247 return len;
248}
249
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200250#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200251static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200252
Ingo Molnare309b412008-05-12 21:20:51 +0200253static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200254trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
255{
256 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200257 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200258 unsigned char byte;
259 int i, j;
260
261 BUG_ON(len >= HEX_CHARS);
262
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200263#ifdef __BIG_ENDIAN
264 for (i = 0, j = 0; i < len; i++) {
265#else
266 for (i = len-1, j = 0; i >= 0; i--) {
267#endif
268 byte = data[i];
269
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200270 hex[j++] = hex2asc[byte & 0x0f];
271 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200272 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200273 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200274
275 return trace_seq_putmem(s, hex, j);
276}
277
Ingo Molnare309b412008-05-12 21:20:51 +0200278static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200279trace_seq_reset(struct trace_seq *s)
280{
281 s->len = 0;
282}
283
Ingo Molnare309b412008-05-12 21:20:51 +0200284static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200285trace_print_seq(struct seq_file *m, struct trace_seq *s)
286{
287 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
288
289 s->buffer[len] = 0;
290 seq_puts(m, s->buffer);
291
292 trace_seq_reset(s);
293}
294
Ingo Molnare309b412008-05-12 21:20:51 +0200295static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200296flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
297{
298 struct list_head flip_pages;
299
300 INIT_LIST_HEAD(&flip_pages);
301
Steven Rostedt93a588f2008-05-12 21:20:45 +0200302 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200303 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200304 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200305
306 check_pages(tr1);
307 check_pages(tr2);
308 list_splice_init(&tr1->trace_pages, &flip_pages);
309 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
310 list_splice_init(&flip_pages, &tr2->trace_pages);
311 BUG_ON(!list_empty(&flip_pages));
312 check_pages(tr1);
313 check_pages(tr2);
314}
315
Ingo Molnare309b412008-05-12 21:20:51 +0200316void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200317update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
318{
319 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200320 int i;
321
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200322 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200323 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200324 /* clear out all the previous traces */
325 for_each_possible_cpu(i) {
326 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200327 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200328 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200329 }
330
331 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200332 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200333}
334
335/**
336 * update_max_tr_single - only copy one trace over, and reset the rest
337 * @tr - tracer
338 * @tsk - task with the latency
339 * @cpu - the cpu of the buffer to copy.
340 */
Ingo Molnare309b412008-05-12 21:20:51 +0200341void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200342update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
343{
344 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200345 int i;
346
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200347 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200348 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200349 for_each_possible_cpu(i)
350 tracing_reset(max_tr.data[i]);
351
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200352 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200353 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200354
355 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200356 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200357}
358
359int register_tracer(struct tracer *type)
360{
361 struct tracer *t;
362 int len;
363 int ret = 0;
364
365 if (!type->name) {
366 pr_info("Tracer must have a name\n");
367 return -1;
368 }
369
370 mutex_lock(&trace_types_lock);
371 for (t = trace_types; t; t = t->next) {
372 if (strcmp(type->name, t->name) == 0) {
373 /* already found */
374 pr_info("Trace %s already registered\n",
375 type->name);
376 ret = -1;
377 goto out;
378 }
379 }
380
Steven Rostedt60a11772008-05-12 21:20:44 +0200381#ifdef CONFIG_FTRACE_STARTUP_TEST
382 if (type->selftest) {
383 struct tracer *saved_tracer = current_trace;
384 struct trace_array_cpu *data;
385 struct trace_array *tr = &global_trace;
386 int saved_ctrl = tr->ctrl;
387 int i;
388 /*
389 * Run a selftest on this tracer.
390 * Here we reset the trace buffer, and set the current
391 * tracer to be this tracer. The tracer can then run some
392 * internal tracing to verify that everything is in order.
393 * If we fail, we do not register this tracer.
394 */
395 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200396 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200397 if (!head_page(data))
398 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200399 tracing_reset(data);
400 }
401 current_trace = type;
402 tr->ctrl = 0;
403 /* the test is responsible for initializing and enabling */
404 pr_info("Testing tracer %s: ", type->name);
405 ret = type->selftest(type, tr);
406 /* the test is responsible for resetting too */
407 current_trace = saved_tracer;
408 tr->ctrl = saved_ctrl;
409 if (ret) {
410 printk(KERN_CONT "FAILED!\n");
411 goto out;
412 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200413 /* Only reset on passing, to avoid touching corrupted buffers */
414 for_each_possible_cpu(i) {
415 data = tr->data[i];
416 if (!head_page(data))
417 continue;
418 tracing_reset(data);
419 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200420 printk(KERN_CONT "PASSED\n");
421 }
422#endif
423
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200424 type->next = trace_types;
425 trace_types = type;
426 len = strlen(type->name);
427 if (len > max_tracer_type_len)
428 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200429
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200430 out:
431 mutex_unlock(&trace_types_lock);
432
433 return ret;
434}
435
436void unregister_tracer(struct tracer *type)
437{
438 struct tracer **t;
439 int len;
440
441 mutex_lock(&trace_types_lock);
442 for (t = &trace_types; *t; t = &(*t)->next) {
443 if (*t == type)
444 goto found;
445 }
446 pr_info("Trace %s not registered\n", type->name);
447 goto out;
448
449 found:
450 *t = (*t)->next;
451 if (strlen(type->name) != max_tracer_type_len)
452 goto out;
453
454 max_tracer_type_len = 0;
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 len = strlen((*t)->name);
457 if (len > max_tracer_type_len)
458 max_tracer_type_len = len;
459 }
460 out:
461 mutex_unlock(&trace_types_lock);
462}
463
Ingo Molnare309b412008-05-12 21:20:51 +0200464void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200465{
466 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200467 data->trace_head = data->trace_tail = head_page(data);
468 data->trace_head_idx = 0;
469 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200470}
471
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200472#define SAVED_CMDLINES 128
473static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
474static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
475static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
476static int cmdline_idx;
477static DEFINE_SPINLOCK(trace_cmdline_lock);
478atomic_t trace_record_cmdline_disabled;
479
480static void trace_init_cmdlines(void)
481{
482 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
483 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
484 cmdline_idx = 0;
485}
486
Ingo Molnare309b412008-05-12 21:20:51 +0200487void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200488
Ingo Molnare309b412008-05-12 21:20:51 +0200489static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200490{
491 unsigned map;
492 unsigned idx;
493
494 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
495 return;
496
497 /*
498 * It's not the end of the world if we don't get
499 * the lock, but we also don't want to spin
500 * nor do we want to disable interrupts,
501 * so if we miss here, then better luck next time.
502 */
503 if (!spin_trylock(&trace_cmdline_lock))
504 return;
505
506 idx = map_pid_to_cmdline[tsk->pid];
507 if (idx >= SAVED_CMDLINES) {
508 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
509
510 map = map_cmdline_to_pid[idx];
511 if (map <= PID_MAX_DEFAULT)
512 map_pid_to_cmdline[map] = (unsigned)-1;
513
514 map_pid_to_cmdline[tsk->pid] = idx;
515
516 cmdline_idx = idx;
517 }
518
519 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
520
521 spin_unlock(&trace_cmdline_lock);
522}
523
Ingo Molnare309b412008-05-12 21:20:51 +0200524static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200525{
526 char *cmdline = "<...>";
527 unsigned map;
528
529 if (!pid)
530 return "<idle>";
531
532 if (pid > PID_MAX_DEFAULT)
533 goto out;
534
535 map = map_pid_to_cmdline[pid];
536 if (map >= SAVED_CMDLINES)
537 goto out;
538
539 cmdline = saved_cmdlines[map];
540
541 out:
542 return cmdline;
543}
544
Ingo Molnare309b412008-05-12 21:20:51 +0200545void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200546{
547 if (atomic_read(&trace_record_cmdline_disabled))
548 return;
549
550 trace_save_cmdline(tsk);
551}
552
Ingo Molnare309b412008-05-12 21:20:51 +0200553static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200554trace_next_list(struct trace_array_cpu *data, struct list_head *next)
555{
556 /*
557 * Roundrobin - but skip the head (which is not a real page):
558 */
559 next = next->next;
560 if (unlikely(next == &data->trace_pages))
561 next = next->next;
562 BUG_ON(next == &data->trace_pages);
563
564 return next;
565}
566
Ingo Molnare309b412008-05-12 21:20:51 +0200567static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200568trace_next_page(struct trace_array_cpu *data, void *addr)
569{
570 struct list_head *next;
571 struct page *page;
572
573 page = virt_to_page(addr);
574
575 next = trace_next_list(data, &page->lru);
576 page = list_entry(next, struct page, lru);
577
578 return page_address(page);
579}
580
Ingo Molnare309b412008-05-12 21:20:51 +0200581static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200582tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200583{
584 unsigned long idx, idx_next;
585 struct trace_entry *entry;
586
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200587 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200588 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200589 idx_next = idx + 1;
590
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200591 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
592
Steven Rostedt93a588f2008-05-12 21:20:45 +0200593 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200594
595 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200596 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200597 idx_next = 0;
598 }
599
Steven Rostedt93a588f2008-05-12 21:20:45 +0200600 if (data->trace_head == data->trace_tail &&
601 idx_next == data->trace_tail_idx) {
602 /* overrun */
603 data->trace_tail_idx++;
604 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
605 data->trace_tail =
606 trace_next_page(data, data->trace_tail);
607 data->trace_tail_idx = 0;
608 }
609 }
610
611 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200612
613 return entry;
614}
615
Ingo Molnare309b412008-05-12 21:20:51 +0200616static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200617tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200618{
619 struct task_struct *tsk = current;
620 unsigned long pc;
621
622 pc = preempt_count();
623
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200624 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200625 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200626 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200627 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
628 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
629 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
630 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
631}
632
Ingo Molnare309b412008-05-12 21:20:51 +0200633void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200634trace_function(struct trace_array *tr, struct trace_array_cpu *data,
635 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200636{
637 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200638 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200639
Steven Rostedt92205c22008-05-12 21:20:55 +0200640 raw_local_irq_save(irq_flags);
641 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200642 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200643 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200644 entry->type = TRACE_FN;
645 entry->fn.ip = ip;
646 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200647 __raw_spin_unlock(&data->lock);
648 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200649}
650
Ingo Molnare309b412008-05-12 21:20:51 +0200651void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200652ftrace(struct trace_array *tr, struct trace_array_cpu *data,
653 unsigned long ip, unsigned long parent_ip, unsigned long flags)
654{
655 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200656 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200657}
658
Ingo Molnare309b412008-05-12 21:20:51 +0200659void
Ingo Molnar4e655512008-05-12 21:20:52 +0200660__trace_special(void *__tr, void *__data,
661 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200662{
Ingo Molnar4e655512008-05-12 21:20:52 +0200663 struct trace_array_cpu *data = __data;
664 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200665 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200666 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200667
Steven Rostedt92205c22008-05-12 21:20:55 +0200668 raw_local_irq_save(irq_flags);
669 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200670 entry = tracing_get_trace_entry(tr, data);
671 tracing_generic_entry_update(entry, 0);
672 entry->type = TRACE_SPECIAL;
673 entry->special.arg1 = arg1;
674 entry->special.arg2 = arg2;
675 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200676 __raw_spin_unlock(&data->lock);
677 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200678
679 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200680}
681
682void __trace_stack(struct trace_array *tr,
683 struct trace_array_cpu *data,
684 unsigned long flags,
685 int skip)
686{
687 struct trace_entry *entry;
688 struct stack_trace trace;
689
690 if (!(trace_flags & TRACE_ITER_STACKTRACE))
691 return;
692
693 entry = tracing_get_trace_entry(tr, data);
694 tracing_generic_entry_update(entry, flags);
695 entry->type = TRACE_STACK;
696
697 memset(&entry->stack, 0, sizeof(entry->stack));
698
699 trace.nr_entries = 0;
700 trace.max_entries = FTRACE_STACK_ENTRIES;
701 trace.skip = skip;
702 trace.entries = entry->stack.caller;
703
704 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200705}
706
Ingo Molnare309b412008-05-12 21:20:51 +0200707void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200708tracing_sched_switch_trace(struct trace_array *tr,
709 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200710 struct task_struct *prev,
711 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200712 unsigned long flags)
713{
714 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200715 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200716
Steven Rostedt92205c22008-05-12 21:20:55 +0200717 raw_local_irq_save(irq_flags);
718 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200719 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200720 tracing_generic_entry_update(entry, flags);
721 entry->type = TRACE_CTX;
722 entry->ctx.prev_pid = prev->pid;
723 entry->ctx.prev_prio = prev->prio;
724 entry->ctx.prev_state = prev->state;
725 entry->ctx.next_pid = next->pid;
726 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200727 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200728 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200729 __raw_spin_unlock(&data->lock);
730 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200731}
732
Ingo Molnar57422792008-05-12 21:20:51 +0200733void
734tracing_sched_wakeup_trace(struct trace_array *tr,
735 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200736 struct task_struct *wakee,
737 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200738 unsigned long flags)
739{
740 struct trace_entry *entry;
741 unsigned long irq_flags;
742
Steven Rostedt92205c22008-05-12 21:20:55 +0200743 raw_local_irq_save(irq_flags);
744 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200745 entry = tracing_get_trace_entry(tr, data);
746 tracing_generic_entry_update(entry, flags);
747 entry->type = TRACE_WAKE;
748 entry->ctx.prev_pid = curr->pid;
749 entry->ctx.prev_prio = curr->prio;
750 entry->ctx.prev_state = curr->state;
751 entry->ctx.next_pid = wakee->pid;
752 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200753 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200754 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200755 __raw_spin_unlock(&data->lock);
756 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200757
758 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200759}
760
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200761#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200762static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200763function_trace_call(unsigned long ip, unsigned long parent_ip)
764{
765 struct trace_array *tr = &global_trace;
766 struct trace_array_cpu *data;
767 unsigned long flags;
768 long disabled;
769 int cpu;
770
771 if (unlikely(!tracer_enabled))
772 return;
773
774 local_irq_save(flags);
775 cpu = raw_smp_processor_id();
776 data = tr->data[cpu];
777 disabled = atomic_inc_return(&data->disabled);
778
779 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200780 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200781
782 atomic_dec(&data->disabled);
783 local_irq_restore(flags);
784}
785
786static struct ftrace_ops trace_ops __read_mostly =
787{
788 .func = function_trace_call,
789};
790
Ingo Molnare309b412008-05-12 21:20:51 +0200791void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200792{
793 register_ftrace_function(&trace_ops);
794}
795
Ingo Molnare309b412008-05-12 21:20:51 +0200796void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200797{
798 unregister_ftrace_function(&trace_ops);
799}
800#endif
801
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200802enum trace_file_type {
803 TRACE_FILE_LAT_FMT = 1,
804};
805
806static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200807trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
808 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200809{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200810 struct page *page;
811 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200812
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200813 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200814 iter->next_idx[cpu] >= data->trace_idx ||
815 (data->trace_head == data->trace_tail &&
816 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200817 return NULL;
818
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200819 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200820 /* Initialize the iterator for this cpu trace buffer */
821 WARN_ON(!data->trace_tail);
822 page = virt_to_page(data->trace_tail);
823 iter->next_page[cpu] = &page->lru;
824 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200825 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200826
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200827 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200828 BUG_ON(&data->trace_pages == &page->lru);
829
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200830 array = page_address(page);
831
Steven Rostedt93a588f2008-05-12 21:20:45 +0200832 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200833 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200834}
835
Ingo Molnare309b412008-05-12 21:20:51 +0200836static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200837find_next_entry(struct trace_iterator *iter, int *ent_cpu)
838{
839 struct trace_array *tr = iter->tr;
840 struct trace_entry *ent, *next = NULL;
841 int next_cpu = -1;
842 int cpu;
843
844 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200845 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200846 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200847 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200848 /*
849 * Pick the entry with the smallest timestamp:
850 */
851 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200852 next = ent;
853 next_cpu = cpu;
854 }
855 }
856
857 if (ent_cpu)
858 *ent_cpu = next_cpu;
859
860 return next;
861}
862
Ingo Molnare309b412008-05-12 21:20:51 +0200863static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200864{
865 iter->idx++;
866 iter->next_idx[iter->cpu]++;
867 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200868
Steven Rostedtb3806b42008-05-12 21:20:46 +0200869 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
870 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
871
872 iter->next_page_idx[iter->cpu] = 0;
873 iter->next_page[iter->cpu] =
874 trace_next_list(data, iter->next_page[iter->cpu]);
875 }
876}
877
Ingo Molnare309b412008-05-12 21:20:51 +0200878static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200879{
880 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
881
882 data->trace_tail_idx++;
883 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
884 data->trace_tail = trace_next_page(data, data->trace_tail);
885 data->trace_tail_idx = 0;
886 }
887
888 /* Check if we empty it, then reset the index */
889 if (data->trace_head == data->trace_tail &&
890 data->trace_head_idx == data->trace_tail_idx)
891 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200892}
893
Ingo Molnare309b412008-05-12 21:20:51 +0200894static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200895{
896 struct trace_entry *next;
897 int next_cpu = -1;
898
899 next = find_next_entry(iter, &next_cpu);
900
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200901 iter->prev_ent = iter->ent;
902 iter->prev_cpu = iter->cpu;
903
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200904 iter->ent = next;
905 iter->cpu = next_cpu;
906
Steven Rostedtb3806b42008-05-12 21:20:46 +0200907 if (next)
908 trace_iterator_increment(iter);
909
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200910 return next ? iter : NULL;
911}
912
Ingo Molnare309b412008-05-12 21:20:51 +0200913static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200914{
915 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200916 void *last_ent = iter->ent;
917 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200918 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200919
920 (*pos)++;
921
922 /* can't go backwards */
923 if (iter->idx > i)
924 return NULL;
925
926 if (iter->idx < 0)
927 ent = find_next_entry_inc(iter);
928 else
929 ent = iter;
930
931 while (ent && iter->idx < i)
932 ent = find_next_entry_inc(iter);
933
934 iter->pos = *pos;
935
936 if (last_ent && !ent)
937 seq_puts(m, "\n\nvim:ft=help\n");
938
939 return ent;
940}
941
942static void *s_start(struct seq_file *m, loff_t *pos)
943{
944 struct trace_iterator *iter = m->private;
945 void *p = NULL;
946 loff_t l = 0;
947 int i;
948
949 mutex_lock(&trace_types_lock);
950
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200951 if (!current_trace || current_trace != iter->trace) {
952 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200953 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200954 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200955
956 atomic_inc(&trace_record_cmdline_disabled);
957
958 /* let the tracer grab locks here if needed */
959 if (current_trace->start)
960 current_trace->start(iter);
961
962 if (*pos != iter->pos) {
963 iter->ent = NULL;
964 iter->cpu = 0;
965 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200966 iter->prev_ent = NULL;
967 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200968
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200969 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200970 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200971 iter->next_page[i] = NULL;
972 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200973
974 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
975 ;
976
977 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200978 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200979 p = s_next(m, p, &l);
980 }
981
982 return p;
983}
984
985static void s_stop(struct seq_file *m, void *p)
986{
987 struct trace_iterator *iter = m->private;
988
989 atomic_dec(&trace_record_cmdline_disabled);
990
991 /* let the tracer release locks here if needed */
992 if (current_trace && current_trace == iter->trace && iter->trace->stop)
993 iter->trace->stop(iter);
994
995 mutex_unlock(&trace_types_lock);
996}
997
Steven Rostedtb3806b42008-05-12 21:20:46 +0200998static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200999seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001000{
1001#ifdef CONFIG_KALLSYMS
1002 char str[KSYM_SYMBOL_LEN];
1003
1004 kallsyms_lookup(address, NULL, NULL, NULL, str);
1005
Steven Rostedtb3806b42008-05-12 21:20:46 +02001006 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001007#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001008 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001009}
1010
Steven Rostedtb3806b42008-05-12 21:20:46 +02001011static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001012seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1013 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001014{
1015#ifdef CONFIG_KALLSYMS
1016 char str[KSYM_SYMBOL_LEN];
1017
1018 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001019 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001020#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001021 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001022}
1023
1024#ifndef CONFIG_64BIT
1025# define IP_FMT "%08lx"
1026#else
1027# define IP_FMT "%016lx"
1028#endif
1029
Ingo Molnare309b412008-05-12 21:20:51 +02001030static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001031seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001032{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001033 int ret;
1034
1035 if (!ip)
1036 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001037
1038 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001039 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001040 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001041 ret = seq_print_sym_short(s, "%s", ip);
1042
1043 if (!ret)
1044 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001045
1046 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001047 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1048 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001049}
1050
Ingo Molnare309b412008-05-12 21:20:51 +02001051static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001052{
1053 seq_puts(m, "# _------=> CPU# \n");
1054 seq_puts(m, "# / _-----=> irqs-off \n");
1055 seq_puts(m, "# | / _----=> need-resched \n");
1056 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1057 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1058 seq_puts(m, "# |||| / \n");
1059 seq_puts(m, "# ||||| delay \n");
1060 seq_puts(m, "# cmd pid ||||| time | caller \n");
1061 seq_puts(m, "# \\ / ||||| \\ | / \n");
1062}
1063
Ingo Molnare309b412008-05-12 21:20:51 +02001064static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001065{
1066 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1067 seq_puts(m, "# | | | | |\n");
1068}
1069
1070
Ingo Molnare309b412008-05-12 21:20:51 +02001071static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001072print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1073{
1074 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1075 struct trace_array *tr = iter->tr;
1076 struct trace_array_cpu *data = tr->data[tr->cpu];
1077 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001078 unsigned long total = 0;
1079 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001080 int cpu;
1081 const char *name = "preemption";
1082
1083 if (type)
1084 name = type->name;
1085
1086 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001087 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001088 total += tr->data[cpu]->trace_idx;
1089 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001090 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001091 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001092 entries += tr->data[cpu]->trace_idx;
1093 }
1094 }
1095
1096 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1097 name, UTS_RELEASE);
1098 seq_puts(m, "-----------------------------------"
1099 "---------------------------------\n");
1100 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1101 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001102 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001103 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001104 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105 tr->cpu,
1106#if defined(CONFIG_PREEMPT_NONE)
1107 "server",
1108#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1109 "desktop",
1110#elif defined(CONFIG_PREEMPT_DESKTOP)
1111 "preempt",
1112#else
1113 "unknown",
1114#endif
1115 /* These are reserved for later use */
1116 0, 0, 0, 0);
1117#ifdef CONFIG_SMP
1118 seq_printf(m, " #P:%d)\n", num_online_cpus());
1119#else
1120 seq_puts(m, ")\n");
1121#endif
1122 seq_puts(m, " -----------------\n");
1123 seq_printf(m, " | task: %.16s-%d "
1124 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1125 data->comm, data->pid, data->uid, data->nice,
1126 data->policy, data->rt_priority);
1127 seq_puts(m, " -----------------\n");
1128
1129 if (data->critical_start) {
1130 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001131 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1132 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001133 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001134 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1135 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001136 seq_puts(m, "\n");
1137 }
1138
1139 seq_puts(m, "\n");
1140}
1141
Ingo Molnare309b412008-05-12 21:20:51 +02001142static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001143lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001144{
1145 int hardirq, softirq;
1146 char *comm;
1147
1148 comm = trace_find_cmdline(entry->pid);
1149
Steven Rostedt214023c2008-05-12 21:20:46 +02001150 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1151 trace_seq_printf(s, "%d", cpu);
1152 trace_seq_printf(s, "%c%c",
1153 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1154 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001155
1156 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1157 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1158 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001159 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001160 else {
1161 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001162 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001163 else {
1164 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001165 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001166 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001167 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001168 }
1169 }
1170
1171 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001172 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001173 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001174 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001175}
1176
1177unsigned long preempt_mark_thresh = 100;
1178
Ingo Molnare309b412008-05-12 21:20:51 +02001179static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001180lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001181 unsigned long rel_usecs)
1182{
Steven Rostedt214023c2008-05-12 21:20:46 +02001183 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001184 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001185 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001186 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001187 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001188 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001189 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001190}
1191
1192static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1193
Ingo Molnare309b412008-05-12 21:20:51 +02001194static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001195print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001196{
Steven Rostedt214023c2008-05-12 21:20:46 +02001197 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001198 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1199 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1200 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1201 struct trace_entry *entry = iter->ent;
1202 unsigned long abs_usecs;
1203 unsigned long rel_usecs;
1204 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001205 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001206 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001207 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001208
1209 if (!next_entry)
1210 next_entry = entry;
1211 rel_usecs = ns2usecs(next_entry->t - entry->t);
1212 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1213
1214 if (verbose) {
1215 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001216 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1217 " %ld.%03ldms (+%ld.%03ldms): ",
1218 comm,
1219 entry->pid, cpu, entry->flags,
1220 entry->preempt_count, trace_idx,
1221 ns2usecs(entry->t),
1222 abs_usecs/1000,
1223 abs_usecs % 1000, rel_usecs/1000,
1224 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001225 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001226 lat_print_generic(s, entry, cpu);
1227 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001228 }
1229 switch (entry->type) {
1230 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001231 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1232 trace_seq_puts(s, " (");
1233 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1234 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001235 break;
1236 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001237 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001238 T = entry->ctx.next_state < sizeof(state_to_char) ?
1239 state_to_char[entry->ctx.next_state] : 'X';
1240
Ankita Gargd17d9692008-05-12 21:20:58 +02001241 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1242 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001243 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001244 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001245 entry->ctx.prev_pid,
1246 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001247 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001248 entry->ctx.next_pid,
1249 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001250 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001251 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001252 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001253 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001254 entry->special.arg1,
1255 entry->special.arg2,
1256 entry->special.arg3);
1257 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001258 case TRACE_STACK:
1259 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1260 if (i)
1261 trace_seq_puts(s, " <= ");
1262 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1263 }
1264 trace_seq_puts(s, "\n");
1265 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001266 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001267 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001268 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001269 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001270}
1271
Ingo Molnare309b412008-05-12 21:20:51 +02001272static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001273{
Steven Rostedt214023c2008-05-12 21:20:46 +02001274 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001275 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001276 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001277 unsigned long usec_rem;
1278 unsigned long long t;
1279 unsigned long secs;
1280 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001281 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001282 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001283 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001284
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001285 entry = iter->ent;
1286
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001287 comm = trace_find_cmdline(iter->ent->pid);
1288
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001289 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290 usec_rem = do_div(t, 1000000ULL);
1291 secs = (unsigned long)t;
1292
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001293 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1294 if (!ret)
1295 return 0;
1296 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1297 if (!ret)
1298 return 0;
1299 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1300 if (!ret)
1301 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001302
1303 switch (entry->type) {
1304 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001305 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1306 if (!ret)
1307 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1309 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001310 ret = trace_seq_printf(s, " <-");
1311 if (!ret)
1312 return 0;
1313 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1314 sym_flags);
1315 if (!ret)
1316 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001317 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001318 ret = trace_seq_printf(s, "\n");
1319 if (!ret)
1320 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321 break;
1322 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001323 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001324 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1325 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001326 T = entry->ctx.next_state < sizeof(state_to_char) ?
1327 state_to_char[entry->ctx.next_state] : 'X';
1328 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001329 entry->ctx.prev_pid,
1330 entry->ctx.prev_prio,
1331 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001332 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001333 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001334 entry->ctx.next_prio,
1335 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001336 if (!ret)
1337 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001338 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001339 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001340 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001341 entry->special.arg1,
1342 entry->special.arg2,
1343 entry->special.arg3);
1344 if (!ret)
1345 return 0;
1346 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001347 case TRACE_STACK:
1348 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1349 if (i) {
1350 ret = trace_seq_puts(s, " <= ");
1351 if (!ret)
1352 return 0;
1353 }
1354 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1355 sym_flags);
1356 if (!ret)
1357 return 0;
1358 }
1359 ret = trace_seq_puts(s, "\n");
1360 if (!ret)
1361 return 0;
1362 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001363 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001364 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001365}
1366
Ingo Molnare309b412008-05-12 21:20:51 +02001367static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001368{
1369 struct trace_seq *s = &iter->seq;
1370 struct trace_entry *entry;
1371 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001372 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001373
1374 entry = iter->ent;
1375
1376 ret = trace_seq_printf(s, "%d %d %llu ",
1377 entry->pid, iter->cpu, entry->t);
1378 if (!ret)
1379 return 0;
1380
1381 switch (entry->type) {
1382 case TRACE_FN:
1383 ret = trace_seq_printf(s, "%x %x\n",
1384 entry->fn.ip, entry->fn.parent_ip);
1385 if (!ret)
1386 return 0;
1387 break;
1388 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001389 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001390 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1391 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001392 T = entry->ctx.next_state < sizeof(state_to_char) ?
1393 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001394 if (entry->type == TRACE_WAKE)
1395 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001396 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001397 entry->ctx.prev_pid,
1398 entry->ctx.prev_prio,
1399 S,
1400 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001401 entry->ctx.next_prio,
1402 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001403 if (!ret)
1404 return 0;
1405 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001406 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001407 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001408 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001409 entry->special.arg1,
1410 entry->special.arg2,
1411 entry->special.arg3);
1412 if (!ret)
1413 return 0;
1414 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001415 }
1416 return 1;
1417}
1418
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001419#define SEQ_PUT_FIELD_RET(s, x) \
1420do { \
1421 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1422 return 0; \
1423} while (0)
1424
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001425#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1426do { \
1427 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1428 return 0; \
1429} while (0)
1430
Ingo Molnare309b412008-05-12 21:20:51 +02001431static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001432{
1433 struct trace_seq *s = &iter->seq;
1434 unsigned char newline = '\n';
1435 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001436 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001437
1438 entry = iter->ent;
1439
1440 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1441 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1442 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1443
1444 switch (entry->type) {
1445 case TRACE_FN:
1446 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1447 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1448 break;
1449 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001450 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001451 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1452 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001453 T = entry->ctx.next_state < sizeof(state_to_char) ?
1454 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001455 if (entry->type == TRACE_WAKE)
1456 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001457 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1458 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1459 SEQ_PUT_HEX_FIELD_RET(s, S);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1462 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001463 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001464 break;
1465 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001466 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001467 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1468 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1469 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1470 break;
1471 }
1472 SEQ_PUT_FIELD_RET(s, newline);
1473
1474 return 1;
1475}
1476
Ingo Molnare309b412008-05-12 21:20:51 +02001477static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001478{
1479 struct trace_seq *s = &iter->seq;
1480 struct trace_entry *entry;
1481
1482 entry = iter->ent;
1483
1484 SEQ_PUT_FIELD_RET(s, entry->pid);
1485 SEQ_PUT_FIELD_RET(s, entry->cpu);
1486 SEQ_PUT_FIELD_RET(s, entry->t);
1487
1488 switch (entry->type) {
1489 case TRACE_FN:
1490 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1491 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1492 break;
1493 case TRACE_CTX:
1494 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1495 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1496 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1497 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1498 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001499 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001500 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001501 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001502 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001503 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1504 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1505 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1506 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001507 }
1508 return 1;
1509}
1510
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001511static int trace_empty(struct trace_iterator *iter)
1512{
1513 struct trace_array_cpu *data;
1514 int cpu;
1515
1516 for_each_possible_cpu(cpu) {
1517 data = iter->tr->data[cpu];
1518
Steven Rostedtb3806b42008-05-12 21:20:46 +02001519 if (head_page(data) && data->trace_idx &&
1520 (data->trace_tail != data->trace_head ||
1521 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001522 return 0;
1523 }
1524 return 1;
1525}
1526
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001527static int print_trace_line(struct trace_iterator *iter)
1528{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001529 if (iter->trace && iter->trace->print_line)
1530 return iter->trace->print_line(iter);
1531
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001532 if (trace_flags & TRACE_ITER_BIN)
1533 return print_bin_fmt(iter);
1534
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001535 if (trace_flags & TRACE_ITER_HEX)
1536 return print_hex_fmt(iter);
1537
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001538 if (trace_flags & TRACE_ITER_RAW)
1539 return print_raw_fmt(iter);
1540
1541 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1542 return print_lat_fmt(iter, iter->idx, iter->cpu);
1543
1544 return print_trace_fmt(iter);
1545}
1546
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547static int s_show(struct seq_file *m, void *v)
1548{
1549 struct trace_iterator *iter = v;
1550
1551 if (iter->ent == NULL) {
1552 if (iter->tr) {
1553 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1554 seq_puts(m, "#\n");
1555 }
1556 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1557 /* print nothing if the buffers are empty */
1558 if (trace_empty(iter))
1559 return 0;
1560 print_trace_header(m, iter);
1561 if (!(trace_flags & TRACE_ITER_VERBOSE))
1562 print_lat_help_header(m);
1563 } else {
1564 if (!(trace_flags & TRACE_ITER_VERBOSE))
1565 print_func_help_header(m);
1566 }
1567 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001568 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001569 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001570 }
1571
1572 return 0;
1573}
1574
1575static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001576 .start = s_start,
1577 .next = s_next,
1578 .stop = s_stop,
1579 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001580};
1581
Ingo Molnare309b412008-05-12 21:20:51 +02001582static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001583__tracing_open(struct inode *inode, struct file *file, int *ret)
1584{
1585 struct trace_iterator *iter;
1586
Steven Rostedt60a11772008-05-12 21:20:44 +02001587 if (tracing_disabled) {
1588 *ret = -ENODEV;
1589 return NULL;
1590 }
1591
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001592 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1593 if (!iter) {
1594 *ret = -ENOMEM;
1595 goto out;
1596 }
1597
1598 mutex_lock(&trace_types_lock);
1599 if (current_trace && current_trace->print_max)
1600 iter->tr = &max_tr;
1601 else
1602 iter->tr = inode->i_private;
1603 iter->trace = current_trace;
1604 iter->pos = -1;
1605
1606 /* TODO stop tracer */
1607 *ret = seq_open(file, &tracer_seq_ops);
1608 if (!*ret) {
1609 struct seq_file *m = file->private_data;
1610 m->private = iter;
1611
1612 /* stop the trace while dumping */
1613 if (iter->tr->ctrl)
1614 tracer_enabled = 0;
1615
1616 if (iter->trace && iter->trace->open)
1617 iter->trace->open(iter);
1618 } else {
1619 kfree(iter);
1620 iter = NULL;
1621 }
1622 mutex_unlock(&trace_types_lock);
1623
1624 out:
1625 return iter;
1626}
1627
1628int tracing_open_generic(struct inode *inode, struct file *filp)
1629{
Steven Rostedt60a11772008-05-12 21:20:44 +02001630 if (tracing_disabled)
1631 return -ENODEV;
1632
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001633 filp->private_data = inode->i_private;
1634 return 0;
1635}
1636
1637int tracing_release(struct inode *inode, struct file *file)
1638{
1639 struct seq_file *m = (struct seq_file *)file->private_data;
1640 struct trace_iterator *iter = m->private;
1641
1642 mutex_lock(&trace_types_lock);
1643 if (iter->trace && iter->trace->close)
1644 iter->trace->close(iter);
1645
1646 /* reenable tracing if it was previously enabled */
1647 if (iter->tr->ctrl)
1648 tracer_enabled = 1;
1649 mutex_unlock(&trace_types_lock);
1650
1651 seq_release(inode, file);
1652 kfree(iter);
1653 return 0;
1654}
1655
1656static int tracing_open(struct inode *inode, struct file *file)
1657{
1658 int ret;
1659
1660 __tracing_open(inode, file, &ret);
1661
1662 return ret;
1663}
1664
1665static int tracing_lt_open(struct inode *inode, struct file *file)
1666{
1667 struct trace_iterator *iter;
1668 int ret;
1669
1670 iter = __tracing_open(inode, file, &ret);
1671
1672 if (!ret)
1673 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1674
1675 return ret;
1676}
1677
1678
Ingo Molnare309b412008-05-12 21:20:51 +02001679static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001680t_next(struct seq_file *m, void *v, loff_t *pos)
1681{
1682 struct tracer *t = m->private;
1683
1684 (*pos)++;
1685
1686 if (t)
1687 t = t->next;
1688
1689 m->private = t;
1690
1691 return t;
1692}
1693
1694static void *t_start(struct seq_file *m, loff_t *pos)
1695{
1696 struct tracer *t = m->private;
1697 loff_t l = 0;
1698
1699 mutex_lock(&trace_types_lock);
1700 for (; t && l < *pos; t = t_next(m, t, &l))
1701 ;
1702
1703 return t;
1704}
1705
1706static void t_stop(struct seq_file *m, void *p)
1707{
1708 mutex_unlock(&trace_types_lock);
1709}
1710
1711static int t_show(struct seq_file *m, void *v)
1712{
1713 struct tracer *t = v;
1714
1715 if (!t)
1716 return 0;
1717
1718 seq_printf(m, "%s", t->name);
1719 if (t->next)
1720 seq_putc(m, ' ');
1721 else
1722 seq_putc(m, '\n');
1723
1724 return 0;
1725}
1726
1727static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001728 .start = t_start,
1729 .next = t_next,
1730 .stop = t_stop,
1731 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001732};
1733
1734static int show_traces_open(struct inode *inode, struct file *file)
1735{
1736 int ret;
1737
Steven Rostedt60a11772008-05-12 21:20:44 +02001738 if (tracing_disabled)
1739 return -ENODEV;
1740
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001741 ret = seq_open(file, &show_traces_seq_ops);
1742 if (!ret) {
1743 struct seq_file *m = file->private_data;
1744 m->private = trace_types;
1745 }
1746
1747 return ret;
1748}
1749
1750static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001751 .open = tracing_open,
1752 .read = seq_read,
1753 .llseek = seq_lseek,
1754 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001755};
1756
1757static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001758 .open = tracing_lt_open,
1759 .read = seq_read,
1760 .llseek = seq_lseek,
1761 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001762};
1763
1764static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001765 .open = show_traces_open,
1766 .read = seq_read,
1767 .release = seq_release,
1768};
1769
Ingo Molnar36dfe922008-05-12 21:20:52 +02001770/*
1771 * Only trace on a CPU if the bitmask is set:
1772 */
1773static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1774
1775/*
1776 * When tracing/tracing_cpu_mask is modified then this holds
1777 * the new bitmask we are about to install:
1778 */
1779static cpumask_t tracing_cpumask_new;
1780
1781/*
1782 * The tracer itself will not take this lock, but still we want
1783 * to provide a consistent cpumask to user-space:
1784 */
1785static DEFINE_MUTEX(tracing_cpumask_update_lock);
1786
1787/*
1788 * Temporary storage for the character representation of the
1789 * CPU bitmask (and one more byte for the newline):
1790 */
1791static char mask_str[NR_CPUS + 1];
1792
Ingo Molnarc7078de2008-05-12 21:20:52 +02001793static ssize_t
1794tracing_cpumask_read(struct file *filp, char __user *ubuf,
1795 size_t count, loff_t *ppos)
1796{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001797 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001798
1799 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001800
1801 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1802 if (count - len < 2) {
1803 count = -EINVAL;
1804 goto out_err;
1805 }
1806 len += sprintf(mask_str + len, "\n");
1807 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1808
1809out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001810 mutex_unlock(&tracing_cpumask_update_lock);
1811
1812 return count;
1813}
1814
1815static ssize_t
1816tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1817 size_t count, loff_t *ppos)
1818{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001819 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001820
1821 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001822 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1823 if (err)
1824 goto err_unlock;
1825
Steven Rostedt92205c22008-05-12 21:20:55 +02001826 raw_local_irq_disable();
1827 __raw_spin_lock(&ftrace_max_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001828 for_each_possible_cpu(cpu) {
1829 /*
1830 * Increase/decrease the disabled counter if we are
1831 * about to flip a bit in the cpumask:
1832 */
1833 if (cpu_isset(cpu, tracing_cpumask) &&
1834 !cpu_isset(cpu, tracing_cpumask_new)) {
1835 atomic_inc(&global_trace.data[cpu]->disabled);
1836 }
1837 if (!cpu_isset(cpu, tracing_cpumask) &&
1838 cpu_isset(cpu, tracing_cpumask_new)) {
1839 atomic_dec(&global_trace.data[cpu]->disabled);
1840 }
1841 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001842 __raw_spin_unlock(&ftrace_max_lock);
1843 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001844
1845 tracing_cpumask = tracing_cpumask_new;
1846
Ingo Molnarc7078de2008-05-12 21:20:52 +02001847 mutex_unlock(&tracing_cpumask_update_lock);
1848
Ingo Molnarc7078de2008-05-12 21:20:52 +02001849 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001850
1851err_unlock:
1852 mutex_unlock(&tracing_cpumask_update_lock);
1853
1854 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001855}
1856
1857static struct file_operations tracing_cpumask_fops = {
1858 .open = tracing_open_generic,
1859 .read = tracing_cpumask_read,
1860 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001861};
1862
1863static ssize_t
1864tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1865 size_t cnt, loff_t *ppos)
1866{
1867 char *buf;
1868 int r = 0;
1869 int len = 0;
1870 int i;
1871
1872 /* calulate max size */
1873 for (i = 0; trace_options[i]; i++) {
1874 len += strlen(trace_options[i]);
1875 len += 3; /* "no" and space */
1876 }
1877
1878 /* +2 for \n and \0 */
1879 buf = kmalloc(len + 2, GFP_KERNEL);
1880 if (!buf)
1881 return -ENOMEM;
1882
1883 for (i = 0; trace_options[i]; i++) {
1884 if (trace_flags & (1 << i))
1885 r += sprintf(buf + r, "%s ", trace_options[i]);
1886 else
1887 r += sprintf(buf + r, "no%s ", trace_options[i]);
1888 }
1889
1890 r += sprintf(buf + r, "\n");
1891 WARN_ON(r >= len + 2);
1892
Ingo Molnar36dfe922008-05-12 21:20:52 +02001893 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001894
1895 kfree(buf);
1896
1897 return r;
1898}
1899
1900static ssize_t
1901tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1902 size_t cnt, loff_t *ppos)
1903{
1904 char buf[64];
1905 char *cmp = buf;
1906 int neg = 0;
1907 int i;
1908
1909 if (cnt > 63)
1910 cnt = 63;
1911
1912 if (copy_from_user(&buf, ubuf, cnt))
1913 return -EFAULT;
1914
1915 buf[cnt] = 0;
1916
1917 if (strncmp(buf, "no", 2) == 0) {
1918 neg = 1;
1919 cmp += 2;
1920 }
1921
1922 for (i = 0; trace_options[i]; i++) {
1923 int len = strlen(trace_options[i]);
1924
1925 if (strncmp(cmp, trace_options[i], len) == 0) {
1926 if (neg)
1927 trace_flags &= ~(1 << i);
1928 else
1929 trace_flags |= (1 << i);
1930 break;
1931 }
1932 }
Ingo Molnar442e5442008-05-12 21:20:53 +02001933 /*
1934 * If no option could be set, return an error:
1935 */
1936 if (!trace_options[i])
1937 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001938
1939 filp->f_pos += cnt;
1940
1941 return cnt;
1942}
1943
1944static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001945 .open = tracing_open_generic,
1946 .read = tracing_iter_ctrl_read,
1947 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001948};
1949
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001950static const char readme_msg[] =
1951 "tracing mini-HOWTO:\n\n"
1952 "# mkdir /debug\n"
1953 "# mount -t debugfs nodev /debug\n\n"
1954 "# cat /debug/tracing/available_tracers\n"
1955 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1956 "# cat /debug/tracing/current_tracer\n"
1957 "none\n"
1958 "# echo sched_switch > /debug/tracing/current_tracer\n"
1959 "# cat /debug/tracing/current_tracer\n"
1960 "sched_switch\n"
1961 "# cat /debug/tracing/iter_ctrl\n"
1962 "noprint-parent nosym-offset nosym-addr noverbose\n"
1963 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1964 "# echo 1 > /debug/tracing/tracing_enabled\n"
1965 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1966 "echo 0 > /debug/tracing/tracing_enabled\n"
1967;
1968
1969static ssize_t
1970tracing_readme_read(struct file *filp, char __user *ubuf,
1971 size_t cnt, loff_t *ppos)
1972{
1973 return simple_read_from_buffer(ubuf, cnt, ppos,
1974 readme_msg, strlen(readme_msg));
1975}
1976
1977static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001978 .open = tracing_open_generic,
1979 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001980};
1981
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001982static ssize_t
1983tracing_ctrl_read(struct file *filp, char __user *ubuf,
1984 size_t cnt, loff_t *ppos)
1985{
1986 struct trace_array *tr = filp->private_data;
1987 char buf[64];
1988 int r;
1989
1990 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001992}
1993
1994static ssize_t
1995tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1996 size_t cnt, loff_t *ppos)
1997{
1998 struct trace_array *tr = filp->private_data;
1999 long val;
2000 char buf[64];
2001
2002 if (cnt > 63)
2003 cnt = 63;
2004
2005 if (copy_from_user(&buf, ubuf, cnt))
2006 return -EFAULT;
2007
2008 buf[cnt] = 0;
2009
2010 val = simple_strtoul(buf, NULL, 10);
2011
2012 val = !!val;
2013
2014 mutex_lock(&trace_types_lock);
2015 if (tr->ctrl ^ val) {
2016 if (val)
2017 tracer_enabled = 1;
2018 else
2019 tracer_enabled = 0;
2020
2021 tr->ctrl = val;
2022
2023 if (current_trace && current_trace->ctrl_update)
2024 current_trace->ctrl_update(tr);
2025 }
2026 mutex_unlock(&trace_types_lock);
2027
2028 filp->f_pos += cnt;
2029
2030 return cnt;
2031}
2032
2033static ssize_t
2034tracing_set_trace_read(struct file *filp, char __user *ubuf,
2035 size_t cnt, loff_t *ppos)
2036{
2037 char buf[max_tracer_type_len+2];
2038 int r;
2039
2040 mutex_lock(&trace_types_lock);
2041 if (current_trace)
2042 r = sprintf(buf, "%s\n", current_trace->name);
2043 else
2044 r = sprintf(buf, "\n");
2045 mutex_unlock(&trace_types_lock);
2046
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002047 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002048}
2049
2050static ssize_t
2051tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2052 size_t cnt, loff_t *ppos)
2053{
2054 struct trace_array *tr = &global_trace;
2055 struct tracer *t;
2056 char buf[max_tracer_type_len+1];
2057 int i;
2058
2059 if (cnt > max_tracer_type_len)
2060 cnt = max_tracer_type_len;
2061
2062 if (copy_from_user(&buf, ubuf, cnt))
2063 return -EFAULT;
2064
2065 buf[cnt] = 0;
2066
2067 /* strip ending whitespace. */
2068 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2069 buf[i] = 0;
2070
2071 mutex_lock(&trace_types_lock);
2072 for (t = trace_types; t; t = t->next) {
2073 if (strcmp(t->name, buf) == 0)
2074 break;
2075 }
2076 if (!t || t == current_trace)
2077 goto out;
2078
2079 if (current_trace && current_trace->reset)
2080 current_trace->reset(tr);
2081
2082 current_trace = t;
2083 if (t->init)
2084 t->init(tr);
2085
2086 out:
2087 mutex_unlock(&trace_types_lock);
2088
2089 filp->f_pos += cnt;
2090
2091 return cnt;
2092}
2093
2094static ssize_t
2095tracing_max_lat_read(struct file *filp, char __user *ubuf,
2096 size_t cnt, loff_t *ppos)
2097{
2098 unsigned long *ptr = filp->private_data;
2099 char buf[64];
2100 int r;
2101
2102 r = snprintf(buf, 64, "%ld\n",
2103 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2104 if (r > 64)
2105 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002106 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002107}
2108
2109static ssize_t
2110tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2111 size_t cnt, loff_t *ppos)
2112{
2113 long *ptr = filp->private_data;
2114 long val;
2115 char buf[64];
2116
2117 if (cnt > 63)
2118 cnt = 63;
2119
2120 if (copy_from_user(&buf, ubuf, cnt))
2121 return -EFAULT;
2122
2123 buf[cnt] = 0;
2124
2125 val = simple_strtoul(buf, NULL, 10);
2126
2127 *ptr = val * 1000;
2128
2129 return cnt;
2130}
2131
Steven Rostedtb3806b42008-05-12 21:20:46 +02002132static atomic_t tracing_reader;
2133
2134static int tracing_open_pipe(struct inode *inode, struct file *filp)
2135{
2136 struct trace_iterator *iter;
2137
2138 if (tracing_disabled)
2139 return -ENODEV;
2140
2141 /* We only allow for reader of the pipe */
2142 if (atomic_inc_return(&tracing_reader) != 1) {
2143 atomic_dec(&tracing_reader);
2144 return -EBUSY;
2145 }
2146
2147 /* create a buffer to store the information to pass to userspace */
2148 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2149 if (!iter)
2150 return -ENOMEM;
2151
2152 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002153 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002154
2155 filp->private_data = iter;
2156
2157 return 0;
2158}
2159
2160static int tracing_release_pipe(struct inode *inode, struct file *file)
2161{
2162 struct trace_iterator *iter = file->private_data;
2163
2164 kfree(iter);
2165 atomic_dec(&tracing_reader);
2166
2167 return 0;
2168}
2169
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002170static unsigned int
2171tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2172{
2173 struct trace_iterator *iter = filp->private_data;
2174
2175 if (trace_flags & TRACE_ITER_BLOCK) {
2176 /*
2177 * Always select as readable when in blocking mode
2178 */
2179 return POLLIN | POLLRDNORM;
2180 }
2181 else {
2182 if (!trace_empty(iter))
2183 return POLLIN | POLLRDNORM;
2184 poll_wait(filp, &trace_wait, poll_table);
2185 if (!trace_empty(iter))
2186 return POLLIN | POLLRDNORM;
2187
2188 return 0;
2189 }
2190}
2191
Steven Rostedtb3806b42008-05-12 21:20:46 +02002192/*
2193 * Consumer reader.
2194 */
2195static ssize_t
2196tracing_read_pipe(struct file *filp, char __user *ubuf,
2197 size_t cnt, loff_t *ppos)
2198{
2199 struct trace_iterator *iter = filp->private_data;
2200 struct trace_array_cpu *data;
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002201 struct trace_array *tr = iter->tr;
2202 struct tracer *tracer = iter->trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002203 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002204 static int start;
2205 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002206#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002207 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002208#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002209 int read = 0;
2210 int cpu;
2211 int len;
2212 int ret;
2213
2214 /* return any leftover data */
2215 if (iter->seq.len > start) {
2216 len = iter->seq.len - start;
2217 if (cnt > len)
2218 cnt = len;
2219 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2220 if (ret)
2221 cnt = -EFAULT;
2222
2223 start += len;
2224
2225 return cnt;
2226 }
2227
2228 trace_seq_reset(&iter->seq);
2229 start = 0;
2230
2231 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002232
2233 if ((filp->f_flags & O_NONBLOCK))
2234 return -EAGAIN;
2235
Steven Rostedtb3806b42008-05-12 21:20:46 +02002236 /*
2237 * This is a make-shift waitqueue. The reason we don't use
2238 * an actual wait queue is because:
2239 * 1) we only ever have one waiter
2240 * 2) the tracing, traces all functions, we don't want
2241 * the overhead of calling wake_up and friends
2242 * (and tracing them too)
2243 * Anyway, this is really very primitive wakeup.
2244 */
2245 set_current_state(TASK_INTERRUPTIBLE);
2246 iter->tr->waiter = current;
2247
2248 /* sleep for one second, and try again. */
2249 schedule_timeout(HZ);
2250
2251 iter->tr->waiter = NULL;
2252
2253 if (signal_pending(current))
2254 return -EINTR;
2255
Steven Rostedt84527992008-05-12 21:20:58 +02002256 if (iter->trace != current_trace)
2257 return 0;
2258
Steven Rostedtb3806b42008-05-12 21:20:46 +02002259 /*
2260 * We block until we read something and tracing is disabled.
2261 * We still block if tracing is disabled, but we have never
2262 * read anything. This allows a user to cat this file, and
2263 * then enable tracing. But after we have read something,
2264 * we give an EOF when tracing is again disabled.
2265 *
2266 * iter->pos will be 0 if we haven't read anything.
2267 */
2268 if (!tracer_enabled && iter->pos)
2269 break;
2270
2271 continue;
2272 }
2273
2274 /* stop when tracing is finished */
2275 if (trace_empty(iter))
2276 return 0;
2277
2278 if (cnt >= PAGE_SIZE)
2279 cnt = PAGE_SIZE - 1;
2280
2281 memset(iter, 0, sizeof(*iter));
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002282 iter->tr = tr;
2283 iter->trace = tracer;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002284 iter->pos = -1;
2285
2286 /*
2287 * We need to stop all tracing on all CPUS to read the
2288 * the next buffer. This is a bit expensive, but is
2289 * not done often. We fill all what we can read,
2290 * and then release the locks again.
2291 */
2292
2293 cpus_clear(mask);
2294 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002295#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002296 ftrace_save = ftrace_enabled;
2297 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002298#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002299 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002300 for_each_possible_cpu(cpu) {
2301 data = iter->tr->data[cpu];
2302
2303 if (!head_page(data) || !data->trace_idx)
2304 continue;
2305
2306 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002307 cpu_set(cpu, mask);
2308 }
2309
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002310 for_each_cpu_mask(cpu, mask) {
2311 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002312 __raw_spin_lock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002313 }
2314
Steven Rostedt088b1e42008-05-12 21:20:48 +02002315 while (find_next_entry_inc(iter) != NULL) {
2316 int len = iter->seq.len;
2317
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002318 ret = print_trace_line(iter);
Steven Rostedt088b1e42008-05-12 21:20:48 +02002319 if (!ret) {
2320 /* don't print partial lines */
2321 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002322 break;
Steven Rostedt088b1e42008-05-12 21:20:48 +02002323 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002324
2325 trace_consume(iter);
2326
2327 if (iter->seq.len >= cnt)
2328 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002329 }
2330
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002331 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002332 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002333 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002334 }
2335
2336 for_each_cpu_mask(cpu, mask) {
2337 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002338 atomic_dec(&data->disabled);
2339 }
Ingo Molnar25770462008-05-12 21:20:49 +02002340#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002341 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002342#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002343 local_irq_restore(flags);
2344
2345 /* Now copy what we have to the user */
2346 read = iter->seq.len;
2347 if (read > cnt)
2348 read = cnt;
2349
2350 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2351
2352 if (read < iter->seq.len)
2353 start = read;
2354 else
2355 trace_seq_reset(&iter->seq);
2356
2357 if (ret)
2358 read = -EFAULT;
2359
2360 return read;
2361}
2362
Steven Rostedta98a3c32008-05-12 21:20:59 +02002363static ssize_t
2364tracing_entries_read(struct file *filp, char __user *ubuf,
2365 size_t cnt, loff_t *ppos)
2366{
2367 struct trace_array *tr = filp->private_data;
2368 char buf[64];
2369 int r;
2370
2371 r = sprintf(buf, "%lu\n", tr->entries);
2372 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2373}
2374
2375static ssize_t
2376tracing_entries_write(struct file *filp, const char __user *ubuf,
2377 size_t cnt, loff_t *ppos)
2378{
2379 unsigned long val;
2380 char buf[64];
2381
2382 if (cnt > 63)
2383 cnt = 63;
2384
2385 if (copy_from_user(&buf, ubuf, cnt))
2386 return -EFAULT;
2387
2388 buf[cnt] = 0;
2389
2390 val = simple_strtoul(buf, NULL, 10);
2391
2392 /* must have at least 1 entry */
2393 if (!val)
2394 return -EINVAL;
2395
2396 mutex_lock(&trace_types_lock);
2397
2398 if (current_trace != &no_tracer) {
2399 cnt = -EBUSY;
2400 pr_info("ftrace: set current_tracer to none"
2401 " before modifying buffer size\n");
2402 goto out;
2403 }
2404
2405 if (val > global_trace.entries) {
2406 while (global_trace.entries < val) {
2407 if (trace_alloc_page()) {
2408 cnt = -ENOMEM;
2409 goto out;
2410 }
2411 }
2412 } else {
2413 /* include the number of entries in val (inc of page entries) */
2414 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2415 trace_free_page();
2416 }
2417
2418 filp->f_pos += cnt;
2419
2420 out:
2421 max_tr.entries = global_trace.entries;
2422 mutex_unlock(&trace_types_lock);
2423
2424 return cnt;
2425}
2426
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002427static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002428 .open = tracing_open_generic,
2429 .read = tracing_max_lat_read,
2430 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002431};
2432
2433static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002434 .open = tracing_open_generic,
2435 .read = tracing_ctrl_read,
2436 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002437};
2438
2439static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002440 .open = tracing_open_generic,
2441 .read = tracing_set_trace_read,
2442 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002443};
2444
Steven Rostedtb3806b42008-05-12 21:20:46 +02002445static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002446 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002447 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002448 .read = tracing_read_pipe,
2449 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002450};
2451
Steven Rostedta98a3c32008-05-12 21:20:59 +02002452static struct file_operations tracing_entries_fops = {
2453 .open = tracing_open_generic,
2454 .read = tracing_entries_read,
2455 .write = tracing_entries_write,
2456};
2457
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002458#ifdef CONFIG_DYNAMIC_FTRACE
2459
2460static ssize_t
2461tracing_read_long(struct file *filp, char __user *ubuf,
2462 size_t cnt, loff_t *ppos)
2463{
2464 unsigned long *p = filp->private_data;
2465 char buf[64];
2466 int r;
2467
2468 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002469
2470 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002471}
2472
2473static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002474 .open = tracing_open_generic,
2475 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002476};
2477#endif
2478
2479static struct dentry *d_tracer;
2480
2481struct dentry *tracing_init_dentry(void)
2482{
2483 static int once;
2484
2485 if (d_tracer)
2486 return d_tracer;
2487
2488 d_tracer = debugfs_create_dir("tracing", NULL);
2489
2490 if (!d_tracer && !once) {
2491 once = 1;
2492 pr_warning("Could not create debugfs directory 'tracing'\n");
2493 return NULL;
2494 }
2495
2496 return d_tracer;
2497}
2498
Steven Rostedt60a11772008-05-12 21:20:44 +02002499#ifdef CONFIG_FTRACE_SELFTEST
2500/* Let selftest have access to static functions in this file */
2501#include "trace_selftest.c"
2502#endif
2503
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002504static __init void tracer_init_debugfs(void)
2505{
2506 struct dentry *d_tracer;
2507 struct dentry *entry;
2508
2509 d_tracer = tracing_init_dentry();
2510
2511 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2512 &global_trace, &tracing_ctrl_fops);
2513 if (!entry)
2514 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2515
2516 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2517 NULL, &tracing_iter_fops);
2518 if (!entry)
2519 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2520
Ingo Molnarc7078de2008-05-12 21:20:52 +02002521 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2522 NULL, &tracing_cpumask_fops);
2523 if (!entry)
2524 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2525
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002526 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2527 &global_trace, &tracing_lt_fops);
2528 if (!entry)
2529 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2530
2531 entry = debugfs_create_file("trace", 0444, d_tracer,
2532 &global_trace, &tracing_fops);
2533 if (!entry)
2534 pr_warning("Could not create debugfs 'trace' entry\n");
2535
2536 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2537 &global_trace, &show_traces_fops);
2538 if (!entry)
2539 pr_warning("Could not create debugfs 'trace' entry\n");
2540
2541 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2542 &global_trace, &set_tracer_fops);
2543 if (!entry)
2544 pr_warning("Could not create debugfs 'trace' entry\n");
2545
2546 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2547 &tracing_max_latency,
2548 &tracing_max_lat_fops);
2549 if (!entry)
2550 pr_warning("Could not create debugfs "
2551 "'tracing_max_latency' entry\n");
2552
2553 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2554 &tracing_thresh, &tracing_max_lat_fops);
2555 if (!entry)
2556 pr_warning("Could not create debugfs "
2557 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002558 entry = debugfs_create_file("README", 0644, d_tracer,
2559 NULL, &tracing_readme_fops);
2560 if (!entry)
2561 pr_warning("Could not create debugfs 'README' entry\n");
2562
Steven Rostedtb3806b42008-05-12 21:20:46 +02002563 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2564 NULL, &tracing_pipe_fops);
2565 if (!entry)
2566 pr_warning("Could not create debugfs "
2567 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002568
Steven Rostedta98a3c32008-05-12 21:20:59 +02002569 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2570 &global_trace, &tracing_entries_fops);
2571 if (!entry)
2572 pr_warning("Could not create debugfs "
2573 "'tracing_threash' entry\n");
2574
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002575#ifdef CONFIG_DYNAMIC_FTRACE
2576 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2577 &ftrace_update_tot_cnt,
2578 &tracing_read_long_fops);
2579 if (!entry)
2580 pr_warning("Could not create debugfs "
2581 "'dyn_ftrace_total_info' entry\n");
2582#endif
2583}
2584
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002585static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002586{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002587 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002588 struct page *page, *tmp;
2589 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002590 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002591 int i;
2592
2593 /* first allocate a page for each CPU */
2594 for_each_possible_cpu(i) {
2595 array = (void *)__get_free_page(GFP_KERNEL);
2596 if (array == NULL) {
2597 printk(KERN_ERR "tracer: failed to allocate page"
2598 "for trace buffer!\n");
2599 goto free_pages;
2600 }
2601
2602 page = virt_to_page(array);
2603 list_add(&page->lru, &pages);
2604
2605/* Only allocate if we are actually using the max trace */
2606#ifdef CONFIG_TRACER_MAX_TRACE
2607 array = (void *)__get_free_page(GFP_KERNEL);
2608 if (array == NULL) {
2609 printk(KERN_ERR "tracer: failed to allocate page"
2610 "for trace buffer!\n");
2611 goto free_pages;
2612 }
2613 page = virt_to_page(array);
2614 list_add(&page->lru, &pages);
2615#endif
2616 }
2617
2618 /* Now that we successfully allocate a page per CPU, add them */
2619 for_each_possible_cpu(i) {
2620 data = global_trace.data[i];
2621 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002622 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002623 list_add_tail(&page->lru, &data->trace_pages);
2624 ClearPageLRU(page);
2625
2626#ifdef CONFIG_TRACER_MAX_TRACE
2627 data = max_tr.data[i];
2628 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002629 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002630 list_add_tail(&page->lru, &data->trace_pages);
2631 SetPageLRU(page);
2632#endif
2633 }
2634 global_trace.entries += ENTRIES_PER_PAGE;
2635
2636 return 0;
2637
2638 free_pages:
2639 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002640 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002641 __free_page(page);
2642 }
2643 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002644}
2645
Steven Rostedta98a3c32008-05-12 21:20:59 +02002646static int trace_free_page(void)
2647{
2648 struct trace_array_cpu *data;
2649 struct page *page;
2650 struct list_head *p;
2651 int i;
2652 int ret = 0;
2653
2654 /* free one page from each buffer */
2655 for_each_possible_cpu(i) {
2656 data = global_trace.data[i];
2657 p = data->trace_pages.next;
2658 if (p == &data->trace_pages) {
2659 /* should never happen */
2660 WARN_ON(1);
2661 tracing_disabled = 1;
2662 ret = -1;
2663 break;
2664 }
2665 page = list_entry(p, struct page, lru);
2666 ClearPageLRU(page);
2667 list_del(&page->lru);
2668 __free_page(page);
2669
2670 tracing_reset(data);
2671
2672#ifdef CONFIG_TRACER_MAX_TRACE
2673 data = max_tr.data[i];
2674 p = data->trace_pages.next;
2675 if (p == &data->trace_pages) {
2676 /* should never happen */
2677 WARN_ON(1);
2678 tracing_disabled = 1;
2679 ret = -1;
2680 break;
2681 }
2682 page = list_entry(p, struct page, lru);
2683 ClearPageLRU(page);
2684 list_del(&page->lru);
2685 __free_page(page);
2686
2687 tracing_reset(data);
2688#endif
2689 }
2690 global_trace.entries -= ENTRIES_PER_PAGE;
2691
2692 return ret;
2693}
2694
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002695__init static int tracer_alloc_buffers(void)
2696{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002697 struct trace_array_cpu *data;
2698 void *array;
2699 struct page *page;
2700 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002701 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002702 int i;
2703
Steven Rostedt26994ea2008-05-12 21:20:48 +02002704 global_trace.ctrl = tracer_enabled;
2705
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002706 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002707 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002708 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002709 max_tr.data[i] = &per_cpu(max_data, i);
2710
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002711 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002712 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002713 printk(KERN_ERR "tracer: failed to allocate page"
2714 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002715 goto free_buffers;
2716 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002717
2718 /* set the array to the list */
2719 INIT_LIST_HEAD(&data->trace_pages);
2720 page = virt_to_page(array);
2721 list_add(&page->lru, &data->trace_pages);
2722 /* use the LRU flag to differentiate the two buffers */
2723 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002724
Steven Rostedta98a3c32008-05-12 21:20:59 +02002725 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2726 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2727
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002728/* Only allocate if we are actually using the max trace */
2729#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002730 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002731 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002732 printk(KERN_ERR "tracer: failed to allocate page"
2733 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002734 goto free_buffers;
2735 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002736
2737 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2738 page = virt_to_page(array);
2739 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2740 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002741#endif
2742 }
2743
2744 /*
2745 * Since we allocate by orders of pages, we may be able to
2746 * round up a bit.
2747 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002748 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002749 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002750
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002751 while (global_trace.entries < trace_nr_entries) {
2752 if (trace_alloc_page())
2753 break;
2754 pages++;
2755 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002756 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002757
2758 pr_info("tracer: %d pages allocated for %ld",
2759 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002760 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2761 pr_info(" actual entries %ld\n", global_trace.entries);
2762
2763 tracer_init_debugfs();
2764
2765 trace_init_cmdlines();
2766
2767 register_tracer(&no_tracer);
2768 current_trace = &no_tracer;
2769
Steven Rostedt60a11772008-05-12 21:20:44 +02002770 /* All seems OK, enable tracing */
2771 tracing_disabled = 0;
2772
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002773 return 0;
2774
2775 free_buffers:
2776 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002777 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002778 struct trace_array_cpu *data = global_trace.data[i];
2779
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002780 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002781 list_for_each_entry_safe(page, tmp,
2782 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002783 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002784 __free_page(page);
2785 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002786 }
2787
2788#ifdef CONFIG_TRACER_MAX_TRACE
2789 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002790 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002791 list_for_each_entry_safe(page, tmp,
2792 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002793 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002794 __free_page(page);
2795 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002796 }
2797#endif
2798 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002799 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002800}
Steven Rostedt60a11772008-05-12 21:20:44 +02002801fs_initcall(tracer_alloc_buffers);