blob: 0a367362ba2fbbfbedb932351e4e6934790c7e25 [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 */
Steven Rostedtcffae432008-05-12 21:21:00 +020039static struct tracer no_tracer __read_mostly = {
Steven Rostedta98a3c32008-05-12 21:20:59 +020040 .name = "none",
41};
42
43static int trace_alloc_page(void);
44static int trace_free_page(void);
45
Steven Rostedt60a11772008-05-12 21:20:44 +020046static int tracing_disabled = 1;
47
Thomas Gleixner72829bc2008-05-23 21:37:28 +020048long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020049ns2usecs(cycle_t nsec)
50{
51 nsec += 500;
52 do_div(nsec, 1000);
53 return nsec;
54}
55
Ingo Molnare309b412008-05-12 21:20:51 +020056cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020057{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020058 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020059}
60
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020061static struct trace_array global_trace;
62
63static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
64
65static struct trace_array max_tr;
66
67static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
68
Steven Rostedt26994ea2008-05-12 21:20:48 +020069static int tracer_enabled = 1;
Ingo Molnar57422792008-05-12 21:20:51 +020070static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020071
72static struct tracer *trace_types __read_mostly;
73static struct tracer *current_trace __read_mostly;
74static int max_tracer_type_len;
75
76static DEFINE_MUTEX(trace_types_lock);
Ingo Molnar4e655512008-05-12 21:20:52 +020077static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
78
79unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
80
Ingo Molnar4e655512008-05-12 21:20:52 +020081void trace_wake_up(void)
82{
Ingo Molnar017730c2008-05-12 21:20:52 +020083 /*
84 * The runqueue_is_locked() can fail, but this is the best we
85 * have for now:
86 */
87 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +020088 wake_up(&trace_wait);
89}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020090
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020091#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
92
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020093static int __init set_nr_entries(char *str)
94{
95 if (!str)
96 return 0;
97 trace_nr_entries = simple_strtoul(str, &str, 0);
98 return 1;
99}
100__setup("trace_entries=", set_nr_entries);
101
Steven Rostedt57f50be2008-05-12 21:20:44 +0200102unsigned long nsecs_to_usecs(unsigned long nsecs)
103{
104 return nsecs / 1000;
105}
106
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200107enum trace_flag_type {
108 TRACE_FLAG_IRQS_OFF = 0x01,
109 TRACE_FLAG_NEED_RESCHED = 0x02,
110 TRACE_FLAG_HARDIRQ = 0x04,
111 TRACE_FLAG_SOFTIRQ = 0x08,
112};
113
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200114#define TRACE_ITER_SYM_MASK \
115 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
116
117/* These must match the bit postions above */
118static const char *trace_options[] = {
119 "print-parent",
120 "sym-offset",
121 "sym-addr",
122 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200123 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200124 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200125 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200126 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200127 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200128 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200129 NULL
130};
131
Steven Rostedt92205c22008-05-12 21:20:55 +0200132static raw_spinlock_t ftrace_max_lock =
133 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200134
135/*
136 * Copy the new maximum trace into the separate maximum-trace
137 * structure. (this way the maximum trace is permanently saved,
138 * for later retrieval via /debugfs/tracing/latency_trace)
139 */
Ingo Molnare309b412008-05-12 21:20:51 +0200140static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200141__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
142{
143 struct trace_array_cpu *data = tr->data[cpu];
144
145 max_tr.cpu = cpu;
146 max_tr.time_start = data->preempt_timestamp;
147
148 data = max_tr.data[cpu];
149 data->saved_latency = tracing_max_latency;
150
151 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
152 data->pid = tsk->pid;
153 data->uid = tsk->uid;
154 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
155 data->policy = tsk->policy;
156 data->rt_priority = tsk->rt_priority;
157
158 /* record this tasks comm */
159 tracing_record_cmdline(current);
160}
161
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200162void check_pages(struct trace_array_cpu *data)
163{
164 struct page *page, *tmp;
165
166 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
167 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
168
169 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
170 BUG_ON(page->lru.next->prev != &page->lru);
171 BUG_ON(page->lru.prev->next != &page->lru);
172 }
173}
174
175void *head_page(struct trace_array_cpu *data)
176{
177 struct page *page;
178
179 check_pages(data);
180 if (list_empty(&data->trace_pages))
181 return NULL;
182
183 page = list_entry(data->trace_pages.next, struct page, lru);
184 BUG_ON(&page->lru == &data->trace_pages);
185
186 return page_address(page);
187}
188
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200189int
Steven Rostedt214023c2008-05-12 21:20:46 +0200190trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
191{
192 int len = (PAGE_SIZE - 1) - s->len;
193 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200194 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200195
196 if (!len)
197 return 0;
198
199 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200200 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200201 va_end(ap);
202
Steven Rostedtb3806b42008-05-12 21:20:46 +0200203 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200204 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200205 return 0;
206
207 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200208
209 return len;
210}
211
Ingo Molnare309b412008-05-12 21:20:51 +0200212static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200213trace_seq_puts(struct trace_seq *s, const char *str)
214{
215 int len = strlen(str);
216
217 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200218 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200219
220 memcpy(s->buffer + s->len, str, len);
221 s->len += len;
222
223 return len;
224}
225
Ingo Molnare309b412008-05-12 21:20:51 +0200226static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200227trace_seq_putc(struct trace_seq *s, unsigned char c)
228{
229 if (s->len >= (PAGE_SIZE - 1))
230 return 0;
231
232 s->buffer[s->len++] = c;
233
234 return 1;
235}
236
Ingo Molnare309b412008-05-12 21:20:51 +0200237static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200238trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
239{
240 if (len > ((PAGE_SIZE - 1) - s->len))
241 return 0;
242
243 memcpy(s->buffer + s->len, mem, len);
244 s->len += len;
245
246 return len;
247}
248
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200249#define HEX_CHARS 17
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200250static const char hex2asc[] = "0123456789abcdef";
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200251
Ingo Molnare309b412008-05-12 21:20:51 +0200252static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200253trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
254{
255 unsigned char hex[HEX_CHARS];
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200256 unsigned char *data = mem;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200257 unsigned char byte;
258 int i, j;
259
260 BUG_ON(len >= HEX_CHARS);
261
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200262#ifdef __BIG_ENDIAN
263 for (i = 0, j = 0; i < len; i++) {
264#else
265 for (i = len-1, j = 0; i >= 0; i--) {
266#endif
267 byte = data[i];
268
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200269 hex[j++] = hex2asc[byte & 0x0f];
270 hex[j++] = hex2asc[byte >> 4];
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200271 }
Thomas Gleixner93dcc6e2008-05-12 21:21:00 +0200272 hex[j++] = ' ';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200273
274 return trace_seq_putmem(s, hex, j);
275}
276
Ingo Molnare309b412008-05-12 21:20:51 +0200277static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200278trace_seq_reset(struct trace_seq *s)
279{
280 s->len = 0;
281}
282
Ingo Molnare309b412008-05-12 21:20:51 +0200283static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200284trace_print_seq(struct seq_file *m, struct trace_seq *s)
285{
286 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
287
288 s->buffer[len] = 0;
289 seq_puts(m, s->buffer);
290
291 trace_seq_reset(s);
292}
293
Ingo Molnare309b412008-05-12 21:20:51 +0200294static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200295flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
296{
297 struct list_head flip_pages;
298
299 INIT_LIST_HEAD(&flip_pages);
300
Steven Rostedt93a588f2008-05-12 21:20:45 +0200301 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200302 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200303 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200304
305 check_pages(tr1);
306 check_pages(tr2);
307 list_splice_init(&tr1->trace_pages, &flip_pages);
308 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
309 list_splice_init(&flip_pages, &tr2->trace_pages);
310 BUG_ON(!list_empty(&flip_pages));
311 check_pages(tr1);
312 check_pages(tr2);
313}
314
Ingo Molnare309b412008-05-12 21:20:51 +0200315void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200316update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
317{
318 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200319 int i;
320
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200321 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200322 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200323 /* clear out all the previous traces */
324 for_each_possible_cpu(i) {
325 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200326 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200327 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200328 }
329
330 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200331 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200332}
333
334/**
335 * update_max_tr_single - only copy one trace over, and reset the rest
336 * @tr - tracer
337 * @tsk - task with the latency
338 * @cpu - the cpu of the buffer to copy.
339 */
Ingo Molnare309b412008-05-12 21:20:51 +0200340void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200341update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
342{
343 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200344 int i;
345
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200346 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200347 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200348 for_each_possible_cpu(i)
349 tracing_reset(max_tr.data[i]);
350
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200351 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200352 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200353
354 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200355 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200356}
357
358int register_tracer(struct tracer *type)
359{
360 struct tracer *t;
361 int len;
362 int ret = 0;
363
364 if (!type->name) {
365 pr_info("Tracer must have a name\n");
366 return -1;
367 }
368
369 mutex_lock(&trace_types_lock);
370 for (t = trace_types; t; t = t->next) {
371 if (strcmp(type->name, t->name) == 0) {
372 /* already found */
373 pr_info("Trace %s already registered\n",
374 type->name);
375 ret = -1;
376 goto out;
377 }
378 }
379
Steven Rostedt60a11772008-05-12 21:20:44 +0200380#ifdef CONFIG_FTRACE_STARTUP_TEST
381 if (type->selftest) {
382 struct tracer *saved_tracer = current_trace;
383 struct trace_array_cpu *data;
384 struct trace_array *tr = &global_trace;
385 int saved_ctrl = tr->ctrl;
386 int i;
387 /*
388 * Run a selftest on this tracer.
389 * Here we reset the trace buffer, and set the current
390 * tracer to be this tracer. The tracer can then run some
391 * internal tracing to verify that everything is in order.
392 * If we fail, we do not register this tracer.
393 */
394 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200395 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200396 if (!head_page(data))
397 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200398 tracing_reset(data);
399 }
400 current_trace = type;
401 tr->ctrl = 0;
402 /* the test is responsible for initializing and enabling */
403 pr_info("Testing tracer %s: ", type->name);
404 ret = type->selftest(type, tr);
405 /* the test is responsible for resetting too */
406 current_trace = saved_tracer;
407 tr->ctrl = saved_ctrl;
408 if (ret) {
409 printk(KERN_CONT "FAILED!\n");
410 goto out;
411 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200412 /* Only reset on passing, to avoid touching corrupted buffers */
413 for_each_possible_cpu(i) {
414 data = tr->data[i];
415 if (!head_page(data))
416 continue;
417 tracing_reset(data);
418 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200419 printk(KERN_CONT "PASSED\n");
420 }
421#endif
422
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200423 type->next = trace_types;
424 trace_types = type;
425 len = strlen(type->name);
426 if (len > max_tracer_type_len)
427 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200428
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200429 out:
430 mutex_unlock(&trace_types_lock);
431
432 return ret;
433}
434
435void unregister_tracer(struct tracer *type)
436{
437 struct tracer **t;
438 int len;
439
440 mutex_lock(&trace_types_lock);
441 for (t = &trace_types; *t; t = &(*t)->next) {
442 if (*t == type)
443 goto found;
444 }
445 pr_info("Trace %s not registered\n", type->name);
446 goto out;
447
448 found:
449 *t = (*t)->next;
450 if (strlen(type->name) != max_tracer_type_len)
451 goto out;
452
453 max_tracer_type_len = 0;
454 for (t = &trace_types; *t; t = &(*t)->next) {
455 len = strlen((*t)->name);
456 if (len > max_tracer_type_len)
457 max_tracer_type_len = len;
458 }
459 out:
460 mutex_unlock(&trace_types_lock);
461}
462
Ingo Molnare309b412008-05-12 21:20:51 +0200463void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200464{
465 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200466 data->trace_head = data->trace_tail = head_page(data);
467 data->trace_head_idx = 0;
468 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200469}
470
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200471#define SAVED_CMDLINES 128
472static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
473static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
474static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
475static int cmdline_idx;
476static DEFINE_SPINLOCK(trace_cmdline_lock);
477atomic_t trace_record_cmdline_disabled;
478
479static void trace_init_cmdlines(void)
480{
481 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
482 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
483 cmdline_idx = 0;
484}
485
Ingo Molnare309b412008-05-12 21:20:51 +0200486void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200487
Ingo Molnare309b412008-05-12 21:20:51 +0200488static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200489{
490 unsigned map;
491 unsigned idx;
492
493 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
494 return;
495
496 /*
497 * It's not the end of the world if we don't get
498 * the lock, but we also don't want to spin
499 * nor do we want to disable interrupts,
500 * so if we miss here, then better luck next time.
501 */
502 if (!spin_trylock(&trace_cmdline_lock))
503 return;
504
505 idx = map_pid_to_cmdline[tsk->pid];
506 if (idx >= SAVED_CMDLINES) {
507 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
508
509 map = map_cmdline_to_pid[idx];
510 if (map <= PID_MAX_DEFAULT)
511 map_pid_to_cmdline[map] = (unsigned)-1;
512
513 map_pid_to_cmdline[tsk->pid] = idx;
514
515 cmdline_idx = idx;
516 }
517
518 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
519
520 spin_unlock(&trace_cmdline_lock);
521}
522
Ingo Molnare309b412008-05-12 21:20:51 +0200523static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200524{
525 char *cmdline = "<...>";
526 unsigned map;
527
528 if (!pid)
529 return "<idle>";
530
531 if (pid > PID_MAX_DEFAULT)
532 goto out;
533
534 map = map_pid_to_cmdline[pid];
535 if (map >= SAVED_CMDLINES)
536 goto out;
537
538 cmdline = saved_cmdlines[map];
539
540 out:
541 return cmdline;
542}
543
Ingo Molnare309b412008-05-12 21:20:51 +0200544void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200545{
546 if (atomic_read(&trace_record_cmdline_disabled))
547 return;
548
549 trace_save_cmdline(tsk);
550}
551
Ingo Molnare309b412008-05-12 21:20:51 +0200552static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200553trace_next_list(struct trace_array_cpu *data, struct list_head *next)
554{
555 /*
556 * Roundrobin - but skip the head (which is not a real page):
557 */
558 next = next->next;
559 if (unlikely(next == &data->trace_pages))
560 next = next->next;
561 BUG_ON(next == &data->trace_pages);
562
563 return next;
564}
565
Ingo Molnare309b412008-05-12 21:20:51 +0200566static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200567trace_next_page(struct trace_array_cpu *data, void *addr)
568{
569 struct list_head *next;
570 struct page *page;
571
572 page = virt_to_page(addr);
573
574 next = trace_next_list(data, &page->lru);
575 page = list_entry(next, struct page, lru);
576
577 return page_address(page);
578}
579
Ingo Molnare309b412008-05-12 21:20:51 +0200580static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200581tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200582{
583 unsigned long idx, idx_next;
584 struct trace_entry *entry;
585
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200586 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200587 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200588 idx_next = idx + 1;
589
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200590 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
591
Steven Rostedt93a588f2008-05-12 21:20:45 +0200592 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200593
594 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200595 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200596 idx_next = 0;
597 }
598
Steven Rostedt93a588f2008-05-12 21:20:45 +0200599 if (data->trace_head == data->trace_tail &&
600 idx_next == data->trace_tail_idx) {
601 /* overrun */
602 data->trace_tail_idx++;
603 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
604 data->trace_tail =
605 trace_next_page(data, data->trace_tail);
606 data->trace_tail_idx = 0;
607 }
608 }
609
610 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200611
612 return entry;
613}
614
Ingo Molnare309b412008-05-12 21:20:51 +0200615static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200616tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200617{
618 struct task_struct *tsk = current;
619 unsigned long pc;
620
621 pc = preempt_count();
622
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200623 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200624 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200625 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200626 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
627 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
628 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
629 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
630}
631
Ingo Molnare309b412008-05-12 21:20:51 +0200632void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200633trace_function(struct trace_array *tr, struct trace_array_cpu *data,
634 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200635{
636 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200637 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200638
Steven Rostedt92205c22008-05-12 21:20:55 +0200639 raw_local_irq_save(irq_flags);
640 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200641 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200642 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200643 entry->type = TRACE_FN;
644 entry->fn.ip = ip;
645 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200646 __raw_spin_unlock(&data->lock);
647 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200648}
649
Ingo Molnare309b412008-05-12 21:20:51 +0200650void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200651ftrace(struct trace_array *tr, struct trace_array_cpu *data,
652 unsigned long ip, unsigned long parent_ip, unsigned long flags)
653{
654 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200655 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200656}
657
Ingo Molnare309b412008-05-12 21:20:51 +0200658void
Ingo Molnar4e655512008-05-12 21:20:52 +0200659__trace_special(void *__tr, void *__data,
660 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200661{
Ingo Molnar4e655512008-05-12 21:20:52 +0200662 struct trace_array_cpu *data = __data;
663 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200664 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200665 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200666
Steven Rostedt92205c22008-05-12 21:20:55 +0200667 raw_local_irq_save(irq_flags);
668 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200669 entry = tracing_get_trace_entry(tr, data);
670 tracing_generic_entry_update(entry, 0);
671 entry->type = TRACE_SPECIAL;
672 entry->special.arg1 = arg1;
673 entry->special.arg2 = arg2;
674 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200675 __raw_spin_unlock(&data->lock);
676 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200677
678 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200679}
680
681void __trace_stack(struct trace_array *tr,
682 struct trace_array_cpu *data,
683 unsigned long flags,
684 int skip)
685{
686 struct trace_entry *entry;
687 struct stack_trace trace;
688
689 if (!(trace_flags & TRACE_ITER_STACKTRACE))
690 return;
691
692 entry = tracing_get_trace_entry(tr, data);
693 tracing_generic_entry_update(entry, flags);
694 entry->type = TRACE_STACK;
695
696 memset(&entry->stack, 0, sizeof(entry->stack));
697
698 trace.nr_entries = 0;
699 trace.max_entries = FTRACE_STACK_ENTRIES;
700 trace.skip = skip;
701 trace.entries = entry->stack.caller;
702
703 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200704}
705
Ingo Molnare309b412008-05-12 21:20:51 +0200706void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200707tracing_sched_switch_trace(struct trace_array *tr,
708 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200709 struct task_struct *prev,
710 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200711 unsigned long flags)
712{
713 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200714 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200715
Steven Rostedt92205c22008-05-12 21:20:55 +0200716 raw_local_irq_save(irq_flags);
717 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200718 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200719 tracing_generic_entry_update(entry, flags);
720 entry->type = TRACE_CTX;
721 entry->ctx.prev_pid = prev->pid;
722 entry->ctx.prev_prio = prev->prio;
723 entry->ctx.prev_state = prev->state;
724 entry->ctx.next_pid = next->pid;
725 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200726 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200727 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200728 __raw_spin_unlock(&data->lock);
729 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200730}
731
Ingo Molnar57422792008-05-12 21:20:51 +0200732void
733tracing_sched_wakeup_trace(struct trace_array *tr,
734 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200735 struct task_struct *wakee,
736 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200737 unsigned long flags)
738{
739 struct trace_entry *entry;
740 unsigned long irq_flags;
741
Steven Rostedt92205c22008-05-12 21:20:55 +0200742 raw_local_irq_save(irq_flags);
743 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200744 entry = tracing_get_trace_entry(tr, data);
745 tracing_generic_entry_update(entry, flags);
746 entry->type = TRACE_WAKE;
747 entry->ctx.prev_pid = curr->pid;
748 entry->ctx.prev_prio = curr->prio;
749 entry->ctx.prev_state = curr->state;
750 entry->ctx.next_pid = wakee->pid;
751 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200752 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200753 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200754 __raw_spin_unlock(&data->lock);
755 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200756
757 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200758}
759
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200760#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200761static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200762function_trace_call(unsigned long ip, unsigned long parent_ip)
763{
764 struct trace_array *tr = &global_trace;
765 struct trace_array_cpu *data;
766 unsigned long flags;
767 long disabled;
768 int cpu;
769
770 if (unlikely(!tracer_enabled))
771 return;
772
773 local_irq_save(flags);
774 cpu = raw_smp_processor_id();
775 data = tr->data[cpu];
776 disabled = atomic_inc_return(&data->disabled);
777
778 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200779 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200780
781 atomic_dec(&data->disabled);
782 local_irq_restore(flags);
783}
784
785static struct ftrace_ops trace_ops __read_mostly =
786{
787 .func = function_trace_call,
788};
789
Ingo Molnare309b412008-05-12 21:20:51 +0200790void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200791{
792 register_ftrace_function(&trace_ops);
793}
794
Ingo Molnare309b412008-05-12 21:20:51 +0200795void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200796{
797 unregister_ftrace_function(&trace_ops);
798}
799#endif
800
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200801enum trace_file_type {
802 TRACE_FILE_LAT_FMT = 1,
803};
804
805static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200806trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
807 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200808{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200809 struct page *page;
810 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200811
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200812 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200813 iter->next_idx[cpu] >= data->trace_idx ||
814 (data->trace_head == data->trace_tail &&
815 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200816 return NULL;
817
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200818 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200819 /* Initialize the iterator for this cpu trace buffer */
820 WARN_ON(!data->trace_tail);
821 page = virt_to_page(data->trace_tail);
822 iter->next_page[cpu] = &page->lru;
823 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200824 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200825
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200826 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200827 BUG_ON(&data->trace_pages == &page->lru);
828
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200829 array = page_address(page);
830
Steven Rostedt93a588f2008-05-12 21:20:45 +0200831 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200832 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200833}
834
Ingo Molnare309b412008-05-12 21:20:51 +0200835static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200836find_next_entry(struct trace_iterator *iter, int *ent_cpu)
837{
838 struct trace_array *tr = iter->tr;
839 struct trace_entry *ent, *next = NULL;
840 int next_cpu = -1;
841 int cpu;
842
843 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200844 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200845 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200846 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200847 /*
848 * Pick the entry with the smallest timestamp:
849 */
850 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200851 next = ent;
852 next_cpu = cpu;
853 }
854 }
855
856 if (ent_cpu)
857 *ent_cpu = next_cpu;
858
859 return next;
860}
861
Ingo Molnare309b412008-05-12 21:20:51 +0200862static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200863{
864 iter->idx++;
865 iter->next_idx[iter->cpu]++;
866 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200867
Steven Rostedtb3806b42008-05-12 21:20:46 +0200868 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
869 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
870
871 iter->next_page_idx[iter->cpu] = 0;
872 iter->next_page[iter->cpu] =
873 trace_next_list(data, iter->next_page[iter->cpu]);
874 }
875}
876
Ingo Molnare309b412008-05-12 21:20:51 +0200877static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200878{
879 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
880
881 data->trace_tail_idx++;
882 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
883 data->trace_tail = trace_next_page(data, data->trace_tail);
884 data->trace_tail_idx = 0;
885 }
886
887 /* Check if we empty it, then reset the index */
888 if (data->trace_head == data->trace_tail &&
889 data->trace_head_idx == data->trace_tail_idx)
890 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200891}
892
Ingo Molnare309b412008-05-12 21:20:51 +0200893static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200894{
895 struct trace_entry *next;
896 int next_cpu = -1;
897
898 next = find_next_entry(iter, &next_cpu);
899
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200900 iter->prev_ent = iter->ent;
901 iter->prev_cpu = iter->cpu;
902
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200903 iter->ent = next;
904 iter->cpu = next_cpu;
905
Steven Rostedtb3806b42008-05-12 21:20:46 +0200906 if (next)
907 trace_iterator_increment(iter);
908
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200909 return next ? iter : NULL;
910}
911
Ingo Molnare309b412008-05-12 21:20:51 +0200912static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200913{
914 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200915 void *last_ent = iter->ent;
916 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200917 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200918
919 (*pos)++;
920
921 /* can't go backwards */
922 if (iter->idx > i)
923 return NULL;
924
925 if (iter->idx < 0)
926 ent = find_next_entry_inc(iter);
927 else
928 ent = iter;
929
930 while (ent && iter->idx < i)
931 ent = find_next_entry_inc(iter);
932
933 iter->pos = *pos;
934
935 if (last_ent && !ent)
936 seq_puts(m, "\n\nvim:ft=help\n");
937
938 return ent;
939}
940
941static void *s_start(struct seq_file *m, loff_t *pos)
942{
943 struct trace_iterator *iter = m->private;
944 void *p = NULL;
945 loff_t l = 0;
946 int i;
947
948 mutex_lock(&trace_types_lock);
949
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200950 if (!current_trace || current_trace != iter->trace) {
951 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200952 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200953 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200954
955 atomic_inc(&trace_record_cmdline_disabled);
956
957 /* let the tracer grab locks here if needed */
958 if (current_trace->start)
959 current_trace->start(iter);
960
961 if (*pos != iter->pos) {
962 iter->ent = NULL;
963 iter->cpu = 0;
964 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200965 iter->prev_ent = NULL;
966 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200967
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200968 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200969 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200970 iter->next_page[i] = NULL;
971 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200972
973 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
974 ;
975
976 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200977 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200978 p = s_next(m, p, &l);
979 }
980
981 return p;
982}
983
984static void s_stop(struct seq_file *m, void *p)
985{
986 struct trace_iterator *iter = m->private;
987
988 atomic_dec(&trace_record_cmdline_disabled);
989
990 /* let the tracer release locks here if needed */
991 if (current_trace && current_trace == iter->trace && iter->trace->stop)
992 iter->trace->stop(iter);
993
994 mutex_unlock(&trace_types_lock);
995}
996
Steven Rostedtb3806b42008-05-12 21:20:46 +0200997static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200998seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200999{
1000#ifdef CONFIG_KALLSYMS
1001 char str[KSYM_SYMBOL_LEN];
1002
1003 kallsyms_lookup(address, NULL, NULL, NULL, str);
1004
Steven Rostedtb3806b42008-05-12 21:20:46 +02001005 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001006#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001007 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001008}
1009
Steven Rostedtb3806b42008-05-12 21:20:46 +02001010static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001011seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1012 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001013{
1014#ifdef CONFIG_KALLSYMS
1015 char str[KSYM_SYMBOL_LEN];
1016
1017 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001018 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001019#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001020 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001021}
1022
1023#ifndef CONFIG_64BIT
1024# define IP_FMT "%08lx"
1025#else
1026# define IP_FMT "%016lx"
1027#endif
1028
Ingo Molnare309b412008-05-12 21:20:51 +02001029static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001030seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001031{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001032 int ret;
1033
1034 if (!ip)
1035 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001036
1037 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001038 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001039 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001040 ret = seq_print_sym_short(s, "%s", ip);
1041
1042 if (!ret)
1043 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044
1045 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001046 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1047 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001048}
1049
Ingo Molnare309b412008-05-12 21:20:51 +02001050static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001051{
1052 seq_puts(m, "# _------=> CPU# \n");
1053 seq_puts(m, "# / _-----=> irqs-off \n");
1054 seq_puts(m, "# | / _----=> need-resched \n");
1055 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1056 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1057 seq_puts(m, "# |||| / \n");
1058 seq_puts(m, "# ||||| delay \n");
1059 seq_puts(m, "# cmd pid ||||| time | caller \n");
1060 seq_puts(m, "# \\ / ||||| \\ | / \n");
1061}
1062
Ingo Molnare309b412008-05-12 21:20:51 +02001063static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001064{
1065 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1066 seq_puts(m, "# | | | | |\n");
1067}
1068
1069
Ingo Molnare309b412008-05-12 21:20:51 +02001070static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001071print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1072{
1073 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1074 struct trace_array *tr = iter->tr;
1075 struct trace_array_cpu *data = tr->data[tr->cpu];
1076 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001077 unsigned long total = 0;
1078 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001079 int cpu;
1080 const char *name = "preemption";
1081
1082 if (type)
1083 name = type->name;
1084
1085 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001086 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001087 total += tr->data[cpu]->trace_idx;
1088 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001089 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001090 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001091 entries += tr->data[cpu]->trace_idx;
1092 }
1093 }
1094
1095 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1096 name, UTS_RELEASE);
1097 seq_puts(m, "-----------------------------------"
1098 "---------------------------------\n");
1099 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1100 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001101 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001102 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001103 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001104 tr->cpu,
1105#if defined(CONFIG_PREEMPT_NONE)
1106 "server",
1107#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1108 "desktop",
1109#elif defined(CONFIG_PREEMPT_DESKTOP)
1110 "preempt",
1111#else
1112 "unknown",
1113#endif
1114 /* These are reserved for later use */
1115 0, 0, 0, 0);
1116#ifdef CONFIG_SMP
1117 seq_printf(m, " #P:%d)\n", num_online_cpus());
1118#else
1119 seq_puts(m, ")\n");
1120#endif
1121 seq_puts(m, " -----------------\n");
1122 seq_printf(m, " | task: %.16s-%d "
1123 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1124 data->comm, data->pid, data->uid, data->nice,
1125 data->policy, data->rt_priority);
1126 seq_puts(m, " -----------------\n");
1127
1128 if (data->critical_start) {
1129 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001130 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1131 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001132 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001133 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1134 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001135 seq_puts(m, "\n");
1136 }
1137
1138 seq_puts(m, "\n");
1139}
1140
Ingo Molnare309b412008-05-12 21:20:51 +02001141static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001142lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001143{
1144 int hardirq, softirq;
1145 char *comm;
1146
1147 comm = trace_find_cmdline(entry->pid);
1148
Steven Rostedt214023c2008-05-12 21:20:46 +02001149 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1150 trace_seq_printf(s, "%d", cpu);
1151 trace_seq_printf(s, "%c%c",
1152 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1153 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001154
1155 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1156 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001157 if (hardirq && softirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001158 trace_seq_putc(s, 'H');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001159 } else {
1160 if (hardirq) {
Steven Rostedt214023c2008-05-12 21:20:46 +02001161 trace_seq_putc(s, 'h');
Ingo Molnarafc2abc2008-05-12 21:21:00 +02001162 } else {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001163 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001164 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001165 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001166 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167 }
1168 }
1169
1170 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001171 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001173 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174}
1175
1176unsigned long preempt_mark_thresh = 100;
1177
Ingo Molnare309b412008-05-12 21:20:51 +02001178static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001179lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001180 unsigned long rel_usecs)
1181{
Steven Rostedt214023c2008-05-12 21:20:46 +02001182 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001183 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001184 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001186 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001187 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001188 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001189}
1190
1191static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1192
Ingo Molnare309b412008-05-12 21:20:51 +02001193static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001194print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001195{
Steven Rostedt214023c2008-05-12 21:20:46 +02001196 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1198 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1199 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1200 struct trace_entry *entry = iter->ent;
1201 unsigned long abs_usecs;
1202 unsigned long rel_usecs;
1203 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001204 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001205 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001206 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001207
1208 if (!next_entry)
1209 next_entry = entry;
1210 rel_usecs = ns2usecs(next_entry->t - entry->t);
1211 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1212
1213 if (verbose) {
1214 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001215 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1216 " %ld.%03ldms (+%ld.%03ldms): ",
1217 comm,
1218 entry->pid, cpu, entry->flags,
1219 entry->preempt_count, trace_idx,
1220 ns2usecs(entry->t),
1221 abs_usecs/1000,
1222 abs_usecs % 1000, rel_usecs/1000,
1223 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001224 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001225 lat_print_generic(s, entry, cpu);
1226 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001227 }
1228 switch (entry->type) {
1229 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001230 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1231 trace_seq_puts(s, " (");
1232 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1233 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001234 break;
1235 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001236 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001237 T = entry->ctx.next_state < sizeof(state_to_char) ?
1238 state_to_char[entry->ctx.next_state] : 'X';
1239
Ankita Gargd17d9692008-05-12 21:20:58 +02001240 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1241 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001242 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001243 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001244 entry->ctx.prev_pid,
1245 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001246 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001247 entry->ctx.next_pid,
1248 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001249 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001250 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001251 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001252 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001253 entry->special.arg1,
1254 entry->special.arg2,
1255 entry->special.arg3);
1256 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001257 case TRACE_STACK:
1258 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1259 if (i)
1260 trace_seq_puts(s, " <= ");
1261 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1262 }
1263 trace_seq_puts(s, "\n");
1264 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001265 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001266 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001267 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001268 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001269}
1270
Ingo Molnare309b412008-05-12 21:20:51 +02001271static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001272{
Steven Rostedt214023c2008-05-12 21:20:46 +02001273 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001274 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001275 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276 unsigned long usec_rem;
1277 unsigned long long t;
1278 unsigned long secs;
1279 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001280 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001281 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001282 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001284 entry = iter->ent;
1285
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001286 comm = trace_find_cmdline(iter->ent->pid);
1287
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001288 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001289 usec_rem = do_div(t, 1000000ULL);
1290 secs = (unsigned long)t;
1291
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001292 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1293 if (!ret)
1294 return 0;
1295 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1296 if (!ret)
1297 return 0;
1298 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1299 if (!ret)
1300 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001301
1302 switch (entry->type) {
1303 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001304 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1305 if (!ret)
1306 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001307 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1308 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001309 ret = trace_seq_printf(s, " <-");
1310 if (!ret)
1311 return 0;
1312 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1313 sym_flags);
1314 if (!ret)
1315 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001316 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001317 ret = trace_seq_printf(s, "\n");
1318 if (!ret)
1319 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001320 break;
1321 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001322 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1324 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001325 T = entry->ctx.next_state < sizeof(state_to_char) ?
1326 state_to_char[entry->ctx.next_state] : 'X';
1327 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001328 entry->ctx.prev_pid,
1329 entry->ctx.prev_prio,
1330 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001331 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001332 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001333 entry->ctx.next_prio,
1334 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001335 if (!ret)
1336 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001337 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001338 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001339 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001340 entry->special.arg1,
1341 entry->special.arg2,
1342 entry->special.arg3);
1343 if (!ret)
1344 return 0;
1345 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001346 case TRACE_STACK:
1347 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1348 if (i) {
1349 ret = trace_seq_puts(s, " <= ");
1350 if (!ret)
1351 return 0;
1352 }
1353 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1354 sym_flags);
1355 if (!ret)
1356 return 0;
1357 }
1358 ret = trace_seq_puts(s, "\n");
1359 if (!ret)
1360 return 0;
1361 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001363 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001364}
1365
Ingo Molnare309b412008-05-12 21:20:51 +02001366static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001367{
1368 struct trace_seq *s = &iter->seq;
1369 struct trace_entry *entry;
1370 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001371 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001372
1373 entry = iter->ent;
1374
1375 ret = trace_seq_printf(s, "%d %d %llu ",
1376 entry->pid, iter->cpu, entry->t);
1377 if (!ret)
1378 return 0;
1379
1380 switch (entry->type) {
1381 case TRACE_FN:
1382 ret = trace_seq_printf(s, "%x %x\n",
1383 entry->fn.ip, entry->fn.parent_ip);
1384 if (!ret)
1385 return 0;
1386 break;
1387 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001388 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001389 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1390 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001391 T = entry->ctx.next_state < sizeof(state_to_char) ?
1392 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001393 if (entry->type == TRACE_WAKE)
1394 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001395 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001396 entry->ctx.prev_pid,
1397 entry->ctx.prev_prio,
1398 S,
1399 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001400 entry->ctx.next_prio,
1401 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001402 if (!ret)
1403 return 0;
1404 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001405 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001406 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001407 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001408 entry->special.arg1,
1409 entry->special.arg2,
1410 entry->special.arg3);
1411 if (!ret)
1412 return 0;
1413 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001414 }
1415 return 1;
1416}
1417
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001418#define SEQ_PUT_FIELD_RET(s, x) \
1419do { \
1420 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1421 return 0; \
1422} while (0)
1423
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001424#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1425do { \
1426 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1427 return 0; \
1428} while (0)
1429
Ingo Molnare309b412008-05-12 21:20:51 +02001430static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001431{
1432 struct trace_seq *s = &iter->seq;
1433 unsigned char newline = '\n';
1434 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001435 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001436
1437 entry = iter->ent;
1438
1439 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1440 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1441 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1442
1443 switch (entry->type) {
1444 case TRACE_FN:
1445 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1446 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1447 break;
1448 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001449 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001450 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1451 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001452 T = entry->ctx.next_state < sizeof(state_to_char) ?
1453 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001454 if (entry->type == TRACE_WAKE)
1455 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001456 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1457 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1458 SEQ_PUT_HEX_FIELD_RET(s, S);
1459 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001462 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001463 break;
1464 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001465 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001466 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1467 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1468 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1469 break;
1470 }
1471 SEQ_PUT_FIELD_RET(s, newline);
1472
1473 return 1;
1474}
1475
Ingo Molnare309b412008-05-12 21:20:51 +02001476static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001477{
1478 struct trace_seq *s = &iter->seq;
1479 struct trace_entry *entry;
1480
1481 entry = iter->ent;
1482
1483 SEQ_PUT_FIELD_RET(s, entry->pid);
1484 SEQ_PUT_FIELD_RET(s, entry->cpu);
1485 SEQ_PUT_FIELD_RET(s, entry->t);
1486
1487 switch (entry->type) {
1488 case TRACE_FN:
1489 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1490 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1491 break;
1492 case TRACE_CTX:
1493 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1494 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1495 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1496 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1497 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001498 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001499 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001500 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001501 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001502 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1503 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1504 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1505 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001506 }
1507 return 1;
1508}
1509
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001510static int trace_empty(struct trace_iterator *iter)
1511{
1512 struct trace_array_cpu *data;
1513 int cpu;
1514
1515 for_each_possible_cpu(cpu) {
1516 data = iter->tr->data[cpu];
1517
Steven Rostedtb3806b42008-05-12 21:20:46 +02001518 if (head_page(data) && data->trace_idx &&
1519 (data->trace_tail != data->trace_head ||
1520 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001521 return 0;
1522 }
1523 return 1;
1524}
1525
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001526static int print_trace_line(struct trace_iterator *iter)
1527{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001528 if (iter->trace && iter->trace->print_line)
1529 return iter->trace->print_line(iter);
1530
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001531 if (trace_flags & TRACE_ITER_BIN)
1532 return print_bin_fmt(iter);
1533
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001534 if (trace_flags & TRACE_ITER_HEX)
1535 return print_hex_fmt(iter);
1536
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001537 if (trace_flags & TRACE_ITER_RAW)
1538 return print_raw_fmt(iter);
1539
1540 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1541 return print_lat_fmt(iter, iter->idx, iter->cpu);
1542
1543 return print_trace_fmt(iter);
1544}
1545
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001546static int s_show(struct seq_file *m, void *v)
1547{
1548 struct trace_iterator *iter = v;
1549
1550 if (iter->ent == NULL) {
1551 if (iter->tr) {
1552 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1553 seq_puts(m, "#\n");
1554 }
1555 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1556 /* print nothing if the buffers are empty */
1557 if (trace_empty(iter))
1558 return 0;
1559 print_trace_header(m, iter);
1560 if (!(trace_flags & TRACE_ITER_VERBOSE))
1561 print_lat_help_header(m);
1562 } else {
1563 if (!(trace_flags & TRACE_ITER_VERBOSE))
1564 print_func_help_header(m);
1565 }
1566 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001567 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001568 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001569 }
1570
1571 return 0;
1572}
1573
1574static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001575 .start = s_start,
1576 .next = s_next,
1577 .stop = s_stop,
1578 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001579};
1580
Ingo Molnare309b412008-05-12 21:20:51 +02001581static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001582__tracing_open(struct inode *inode, struct file *file, int *ret)
1583{
1584 struct trace_iterator *iter;
1585
Steven Rostedt60a11772008-05-12 21:20:44 +02001586 if (tracing_disabled) {
1587 *ret = -ENODEV;
1588 return NULL;
1589 }
1590
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001591 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1592 if (!iter) {
1593 *ret = -ENOMEM;
1594 goto out;
1595 }
1596
1597 mutex_lock(&trace_types_lock);
1598 if (current_trace && current_trace->print_max)
1599 iter->tr = &max_tr;
1600 else
1601 iter->tr = inode->i_private;
1602 iter->trace = current_trace;
1603 iter->pos = -1;
1604
1605 /* TODO stop tracer */
1606 *ret = seq_open(file, &tracer_seq_ops);
1607 if (!*ret) {
1608 struct seq_file *m = file->private_data;
1609 m->private = iter;
1610
1611 /* stop the trace while dumping */
1612 if (iter->tr->ctrl)
1613 tracer_enabled = 0;
1614
1615 if (iter->trace && iter->trace->open)
1616 iter->trace->open(iter);
1617 } else {
1618 kfree(iter);
1619 iter = NULL;
1620 }
1621 mutex_unlock(&trace_types_lock);
1622
1623 out:
1624 return iter;
1625}
1626
1627int tracing_open_generic(struct inode *inode, struct file *filp)
1628{
Steven Rostedt60a11772008-05-12 21:20:44 +02001629 if (tracing_disabled)
1630 return -ENODEV;
1631
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001632 filp->private_data = inode->i_private;
1633 return 0;
1634}
1635
1636int tracing_release(struct inode *inode, struct file *file)
1637{
1638 struct seq_file *m = (struct seq_file *)file->private_data;
1639 struct trace_iterator *iter = m->private;
1640
1641 mutex_lock(&trace_types_lock);
1642 if (iter->trace && iter->trace->close)
1643 iter->trace->close(iter);
1644
1645 /* reenable tracing if it was previously enabled */
1646 if (iter->tr->ctrl)
1647 tracer_enabled = 1;
1648 mutex_unlock(&trace_types_lock);
1649
1650 seq_release(inode, file);
1651 kfree(iter);
1652 return 0;
1653}
1654
1655static int tracing_open(struct inode *inode, struct file *file)
1656{
1657 int ret;
1658
1659 __tracing_open(inode, file, &ret);
1660
1661 return ret;
1662}
1663
1664static int tracing_lt_open(struct inode *inode, struct file *file)
1665{
1666 struct trace_iterator *iter;
1667 int ret;
1668
1669 iter = __tracing_open(inode, file, &ret);
1670
1671 if (!ret)
1672 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1673
1674 return ret;
1675}
1676
1677
Ingo Molnare309b412008-05-12 21:20:51 +02001678static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001679t_next(struct seq_file *m, void *v, loff_t *pos)
1680{
1681 struct tracer *t = m->private;
1682
1683 (*pos)++;
1684
1685 if (t)
1686 t = t->next;
1687
1688 m->private = t;
1689
1690 return t;
1691}
1692
1693static void *t_start(struct seq_file *m, loff_t *pos)
1694{
1695 struct tracer *t = m->private;
1696 loff_t l = 0;
1697
1698 mutex_lock(&trace_types_lock);
1699 for (; t && l < *pos; t = t_next(m, t, &l))
1700 ;
1701
1702 return t;
1703}
1704
1705static void t_stop(struct seq_file *m, void *p)
1706{
1707 mutex_unlock(&trace_types_lock);
1708}
1709
1710static int t_show(struct seq_file *m, void *v)
1711{
1712 struct tracer *t = v;
1713
1714 if (!t)
1715 return 0;
1716
1717 seq_printf(m, "%s", t->name);
1718 if (t->next)
1719 seq_putc(m, ' ');
1720 else
1721 seq_putc(m, '\n');
1722
1723 return 0;
1724}
1725
1726static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001727 .start = t_start,
1728 .next = t_next,
1729 .stop = t_stop,
1730 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001731};
1732
1733static int show_traces_open(struct inode *inode, struct file *file)
1734{
1735 int ret;
1736
Steven Rostedt60a11772008-05-12 21:20:44 +02001737 if (tracing_disabled)
1738 return -ENODEV;
1739
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001740 ret = seq_open(file, &show_traces_seq_ops);
1741 if (!ret) {
1742 struct seq_file *m = file->private_data;
1743 m->private = trace_types;
1744 }
1745
1746 return ret;
1747}
1748
1749static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001750 .open = tracing_open,
1751 .read = seq_read,
1752 .llseek = seq_lseek,
1753 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001754};
1755
1756static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001757 .open = tracing_lt_open,
1758 .read = seq_read,
1759 .llseek = seq_lseek,
1760 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001761};
1762
1763static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001764 .open = show_traces_open,
1765 .read = seq_read,
1766 .release = seq_release,
1767};
1768
Ingo Molnar36dfe922008-05-12 21:20:52 +02001769/*
1770 * Only trace on a CPU if the bitmask is set:
1771 */
1772static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1773
1774/*
1775 * When tracing/tracing_cpu_mask is modified then this holds
1776 * the new bitmask we are about to install:
1777 */
1778static cpumask_t tracing_cpumask_new;
1779
1780/*
1781 * The tracer itself will not take this lock, but still we want
1782 * to provide a consistent cpumask to user-space:
1783 */
1784static DEFINE_MUTEX(tracing_cpumask_update_lock);
1785
1786/*
1787 * Temporary storage for the character representation of the
1788 * CPU bitmask (and one more byte for the newline):
1789 */
1790static char mask_str[NR_CPUS + 1];
1791
Ingo Molnarc7078de2008-05-12 21:20:52 +02001792static ssize_t
1793tracing_cpumask_read(struct file *filp, char __user *ubuf,
1794 size_t count, loff_t *ppos)
1795{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001796 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001797
1798 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001799
1800 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1801 if (count - len < 2) {
1802 count = -EINVAL;
1803 goto out_err;
1804 }
1805 len += sprintf(mask_str + len, "\n");
1806 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1807
1808out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001809 mutex_unlock(&tracing_cpumask_update_lock);
1810
1811 return count;
1812}
1813
1814static ssize_t
1815tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1816 size_t count, loff_t *ppos)
1817{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001818 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001819
1820 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001821 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1822 if (err)
1823 goto err_unlock;
1824
Steven Rostedt92205c22008-05-12 21:20:55 +02001825 raw_local_irq_disable();
1826 __raw_spin_lock(&ftrace_max_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001827 for_each_possible_cpu(cpu) {
1828 /*
1829 * Increase/decrease the disabled counter if we are
1830 * about to flip a bit in the cpumask:
1831 */
1832 if (cpu_isset(cpu, tracing_cpumask) &&
1833 !cpu_isset(cpu, tracing_cpumask_new)) {
1834 atomic_inc(&global_trace.data[cpu]->disabled);
1835 }
1836 if (!cpu_isset(cpu, tracing_cpumask) &&
1837 cpu_isset(cpu, tracing_cpumask_new)) {
1838 atomic_dec(&global_trace.data[cpu]->disabled);
1839 }
1840 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001841 __raw_spin_unlock(&ftrace_max_lock);
1842 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001843
1844 tracing_cpumask = tracing_cpumask_new;
1845
Ingo Molnarc7078de2008-05-12 21:20:52 +02001846 mutex_unlock(&tracing_cpumask_update_lock);
1847
Ingo Molnarc7078de2008-05-12 21:20:52 +02001848 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001849
1850err_unlock:
1851 mutex_unlock(&tracing_cpumask_update_lock);
1852
1853 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001854}
1855
1856static struct file_operations tracing_cpumask_fops = {
1857 .open = tracing_open_generic,
1858 .read = tracing_cpumask_read,
1859 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001860};
1861
1862static ssize_t
1863tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1864 size_t cnt, loff_t *ppos)
1865{
1866 char *buf;
1867 int r = 0;
1868 int len = 0;
1869 int i;
1870
1871 /* calulate max size */
1872 for (i = 0; trace_options[i]; i++) {
1873 len += strlen(trace_options[i]);
1874 len += 3; /* "no" and space */
1875 }
1876
1877 /* +2 for \n and \0 */
1878 buf = kmalloc(len + 2, GFP_KERNEL);
1879 if (!buf)
1880 return -ENOMEM;
1881
1882 for (i = 0; trace_options[i]; i++) {
1883 if (trace_flags & (1 << i))
1884 r += sprintf(buf + r, "%s ", trace_options[i]);
1885 else
1886 r += sprintf(buf + r, "no%s ", trace_options[i]);
1887 }
1888
1889 r += sprintf(buf + r, "\n");
1890 WARN_ON(r >= len + 2);
1891
Ingo Molnar36dfe922008-05-12 21:20:52 +02001892 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001893
1894 kfree(buf);
1895
1896 return r;
1897}
1898
1899static ssize_t
1900tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1901 size_t cnt, loff_t *ppos)
1902{
1903 char buf[64];
1904 char *cmp = buf;
1905 int neg = 0;
1906 int i;
1907
Steven Rostedtcffae432008-05-12 21:21:00 +02001908 if (cnt >= sizeof(buf))
1909 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001910
1911 if (copy_from_user(&buf, ubuf, cnt))
1912 return -EFAULT;
1913
1914 buf[cnt] = 0;
1915
1916 if (strncmp(buf, "no", 2) == 0) {
1917 neg = 1;
1918 cmp += 2;
1919 }
1920
1921 for (i = 0; trace_options[i]; i++) {
1922 int len = strlen(trace_options[i]);
1923
1924 if (strncmp(cmp, trace_options[i], len) == 0) {
1925 if (neg)
1926 trace_flags &= ~(1 << i);
1927 else
1928 trace_flags |= (1 << i);
1929 break;
1930 }
1931 }
Ingo Molnar442e5442008-05-12 21:20:53 +02001932 /*
1933 * If no option could be set, return an error:
1934 */
1935 if (!trace_options[i])
1936 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001937
1938 filp->f_pos += cnt;
1939
1940 return cnt;
1941}
1942
1943static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001944 .open = tracing_open_generic,
1945 .read = tracing_iter_ctrl_read,
1946 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001947};
1948
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001949static const char readme_msg[] =
1950 "tracing mini-HOWTO:\n\n"
1951 "# mkdir /debug\n"
1952 "# mount -t debugfs nodev /debug\n\n"
1953 "# cat /debug/tracing/available_tracers\n"
1954 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1955 "# cat /debug/tracing/current_tracer\n"
1956 "none\n"
1957 "# echo sched_switch > /debug/tracing/current_tracer\n"
1958 "# cat /debug/tracing/current_tracer\n"
1959 "sched_switch\n"
1960 "# cat /debug/tracing/iter_ctrl\n"
1961 "noprint-parent nosym-offset nosym-addr noverbose\n"
1962 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1963 "# echo 1 > /debug/tracing/tracing_enabled\n"
1964 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1965 "echo 0 > /debug/tracing/tracing_enabled\n"
1966;
1967
1968static ssize_t
1969tracing_readme_read(struct file *filp, char __user *ubuf,
1970 size_t cnt, loff_t *ppos)
1971{
1972 return simple_read_from_buffer(ubuf, cnt, ppos,
1973 readme_msg, strlen(readme_msg));
1974}
1975
1976static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001977 .open = tracing_open_generic,
1978 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001979};
1980
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001981static ssize_t
1982tracing_ctrl_read(struct file *filp, char __user *ubuf,
1983 size_t cnt, loff_t *ppos)
1984{
1985 struct trace_array *tr = filp->private_data;
1986 char buf[64];
1987 int r;
1988
1989 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001990 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001991}
1992
1993static ssize_t
1994tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1995 size_t cnt, loff_t *ppos)
1996{
1997 struct trace_array *tr = filp->private_data;
1998 long val;
1999 char buf[64];
2000
Steven Rostedtcffae432008-05-12 21:21:00 +02002001 if (cnt >= sizeof(buf))
2002 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002003
2004 if (copy_from_user(&buf, ubuf, cnt))
2005 return -EFAULT;
2006
2007 buf[cnt] = 0;
2008
2009 val = simple_strtoul(buf, NULL, 10);
2010
2011 val = !!val;
2012
2013 mutex_lock(&trace_types_lock);
2014 if (tr->ctrl ^ val) {
2015 if (val)
2016 tracer_enabled = 1;
2017 else
2018 tracer_enabled = 0;
2019
2020 tr->ctrl = val;
2021
2022 if (current_trace && current_trace->ctrl_update)
2023 current_trace->ctrl_update(tr);
2024 }
2025 mutex_unlock(&trace_types_lock);
2026
2027 filp->f_pos += cnt;
2028
2029 return cnt;
2030}
2031
2032static ssize_t
2033tracing_set_trace_read(struct file *filp, char __user *ubuf,
2034 size_t cnt, loff_t *ppos)
2035{
2036 char buf[max_tracer_type_len+2];
2037 int r;
2038
2039 mutex_lock(&trace_types_lock);
2040 if (current_trace)
2041 r = sprintf(buf, "%s\n", current_trace->name);
2042 else
2043 r = sprintf(buf, "\n");
2044 mutex_unlock(&trace_types_lock);
2045
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002046 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002047}
2048
2049static ssize_t
2050tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2051 size_t cnt, loff_t *ppos)
2052{
2053 struct trace_array *tr = &global_trace;
2054 struct tracer *t;
2055 char buf[max_tracer_type_len+1];
2056 int i;
2057
2058 if (cnt > max_tracer_type_len)
2059 cnt = max_tracer_type_len;
2060
2061 if (copy_from_user(&buf, ubuf, cnt))
2062 return -EFAULT;
2063
2064 buf[cnt] = 0;
2065
2066 /* strip ending whitespace. */
2067 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2068 buf[i] = 0;
2069
2070 mutex_lock(&trace_types_lock);
2071 for (t = trace_types; t; t = t->next) {
2072 if (strcmp(t->name, buf) == 0)
2073 break;
2074 }
2075 if (!t || t == current_trace)
2076 goto out;
2077
2078 if (current_trace && current_trace->reset)
2079 current_trace->reset(tr);
2080
2081 current_trace = t;
2082 if (t->init)
2083 t->init(tr);
2084
2085 out:
2086 mutex_unlock(&trace_types_lock);
2087
2088 filp->f_pos += cnt;
2089
2090 return cnt;
2091}
2092
2093static ssize_t
2094tracing_max_lat_read(struct file *filp, char __user *ubuf,
2095 size_t cnt, loff_t *ppos)
2096{
2097 unsigned long *ptr = filp->private_data;
2098 char buf[64];
2099 int r;
2100
Steven Rostedtcffae432008-05-12 21:21:00 +02002101 r = snprintf(buf, sizeof(buf), "%ld\n",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002102 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
Steven Rostedtcffae432008-05-12 21:21:00 +02002103 if (r > sizeof(buf))
2104 r = sizeof(buf);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002105 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002106}
2107
2108static ssize_t
2109tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2110 size_t cnt, loff_t *ppos)
2111{
2112 long *ptr = filp->private_data;
2113 long val;
2114 char buf[64];
2115
Steven Rostedtcffae432008-05-12 21:21:00 +02002116 if (cnt >= sizeof(buf))
2117 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002118
2119 if (copy_from_user(&buf, ubuf, cnt))
2120 return -EFAULT;
2121
2122 buf[cnt] = 0;
2123
2124 val = simple_strtoul(buf, NULL, 10);
2125
2126 *ptr = val * 1000;
2127
2128 return cnt;
2129}
2130
Steven Rostedtb3806b42008-05-12 21:20:46 +02002131static atomic_t tracing_reader;
2132
2133static int tracing_open_pipe(struct inode *inode, struct file *filp)
2134{
2135 struct trace_iterator *iter;
2136
2137 if (tracing_disabled)
2138 return -ENODEV;
2139
2140 /* We only allow for reader of the pipe */
2141 if (atomic_inc_return(&tracing_reader) != 1) {
2142 atomic_dec(&tracing_reader);
2143 return -EBUSY;
2144 }
2145
2146 /* create a buffer to store the information to pass to userspace */
2147 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2148 if (!iter)
2149 return -ENOMEM;
2150
2151 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002152 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002153
2154 filp->private_data = iter;
2155
2156 return 0;
2157}
2158
2159static int tracing_release_pipe(struct inode *inode, struct file *file)
2160{
2161 struct trace_iterator *iter = file->private_data;
2162
2163 kfree(iter);
2164 atomic_dec(&tracing_reader);
2165
2166 return 0;
2167}
2168
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002169static unsigned int
2170tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2171{
2172 struct trace_iterator *iter = filp->private_data;
2173
2174 if (trace_flags & TRACE_ITER_BLOCK) {
2175 /*
2176 * Always select as readable when in blocking mode
2177 */
2178 return POLLIN | POLLRDNORM;
Ingo Molnarafc2abc2008-05-12 21:21:00 +02002179 } else {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002180 if (!trace_empty(iter))
2181 return POLLIN | POLLRDNORM;
2182 poll_wait(filp, &trace_wait, poll_table);
2183 if (!trace_empty(iter))
2184 return POLLIN | POLLRDNORM;
2185
2186 return 0;
2187 }
2188}
2189
Steven Rostedtb3806b42008-05-12 21:20:46 +02002190/*
2191 * Consumer reader.
2192 */
2193static ssize_t
2194tracing_read_pipe(struct file *filp, char __user *ubuf,
2195 size_t cnt, loff_t *ppos)
2196{
2197 struct trace_iterator *iter = filp->private_data;
2198 struct trace_array_cpu *data;
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002199 struct trace_array *tr = iter->tr;
2200 struct tracer *tracer = iter->trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002201 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002202 static int start;
2203 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002204#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002205 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002206#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002207 int read = 0;
2208 int cpu;
2209 int len;
2210 int ret;
2211
2212 /* return any leftover data */
2213 if (iter->seq.len > start) {
2214 len = iter->seq.len - start;
2215 if (cnt > len)
2216 cnt = len;
2217 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2218 if (ret)
2219 cnt = -EFAULT;
2220
2221 start += len;
2222
2223 return cnt;
2224 }
2225
2226 trace_seq_reset(&iter->seq);
2227 start = 0;
2228
2229 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002230
2231 if ((filp->f_flags & O_NONBLOCK))
2232 return -EAGAIN;
2233
Steven Rostedtb3806b42008-05-12 21:20:46 +02002234 /*
2235 * This is a make-shift waitqueue. The reason we don't use
2236 * an actual wait queue is because:
2237 * 1) we only ever have one waiter
2238 * 2) the tracing, traces all functions, we don't want
2239 * the overhead of calling wake_up and friends
2240 * (and tracing them too)
2241 * Anyway, this is really very primitive wakeup.
2242 */
2243 set_current_state(TASK_INTERRUPTIBLE);
2244 iter->tr->waiter = current;
2245
2246 /* sleep for one second, and try again. */
2247 schedule_timeout(HZ);
2248
2249 iter->tr->waiter = NULL;
2250
2251 if (signal_pending(current))
2252 return -EINTR;
2253
Steven Rostedt84527992008-05-12 21:20:58 +02002254 if (iter->trace != current_trace)
2255 return 0;
2256
Steven Rostedtb3806b42008-05-12 21:20:46 +02002257 /*
2258 * We block until we read something and tracing is disabled.
2259 * We still block if tracing is disabled, but we have never
2260 * read anything. This allows a user to cat this file, and
2261 * then enable tracing. But after we have read something,
2262 * we give an EOF when tracing is again disabled.
2263 *
2264 * iter->pos will be 0 if we haven't read anything.
2265 */
2266 if (!tracer_enabled && iter->pos)
2267 break;
2268
2269 continue;
2270 }
2271
2272 /* stop when tracing is finished */
2273 if (trace_empty(iter))
2274 return 0;
2275
2276 if (cnt >= PAGE_SIZE)
2277 cnt = PAGE_SIZE - 1;
2278
2279 memset(iter, 0, sizeof(*iter));
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002280 iter->tr = tr;
2281 iter->trace = tracer;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002282 iter->pos = -1;
2283
2284 /*
2285 * We need to stop all tracing on all CPUS to read the
2286 * the next buffer. This is a bit expensive, but is
2287 * not done often. We fill all what we can read,
2288 * and then release the locks again.
2289 */
2290
2291 cpus_clear(mask);
2292 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002293#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002294 ftrace_save = ftrace_enabled;
2295 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002296#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002297 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002298 for_each_possible_cpu(cpu) {
2299 data = iter->tr->data[cpu];
2300
2301 if (!head_page(data) || !data->trace_idx)
2302 continue;
2303
2304 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002305 cpu_set(cpu, mask);
2306 }
2307
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002308 for_each_cpu_mask(cpu, mask) {
2309 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002310 __raw_spin_lock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002311 }
2312
Steven Rostedt088b1e422008-05-12 21:20:48 +02002313 while (find_next_entry_inc(iter) != NULL) {
2314 int len = iter->seq.len;
2315
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002316 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002317 if (!ret) {
2318 /* don't print partial lines */
2319 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002320 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002321 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002322
2323 trace_consume(iter);
2324
2325 if (iter->seq.len >= cnt)
2326 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002327 }
2328
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002329 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002330 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002331 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002332 }
2333
2334 for_each_cpu_mask(cpu, mask) {
2335 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002336 atomic_dec(&data->disabled);
2337 }
Ingo Molnar25770462008-05-12 21:20:49 +02002338#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002339 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002340#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002341 local_irq_restore(flags);
2342
2343 /* Now copy what we have to the user */
2344 read = iter->seq.len;
2345 if (read > cnt)
2346 read = cnt;
2347
2348 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2349
2350 if (read < iter->seq.len)
2351 start = read;
2352 else
2353 trace_seq_reset(&iter->seq);
2354
2355 if (ret)
2356 read = -EFAULT;
2357
2358 return read;
2359}
2360
Steven Rostedta98a3c32008-05-12 21:20:59 +02002361static ssize_t
2362tracing_entries_read(struct file *filp, char __user *ubuf,
2363 size_t cnt, loff_t *ppos)
2364{
2365 struct trace_array *tr = filp->private_data;
2366 char buf[64];
2367 int r;
2368
2369 r = sprintf(buf, "%lu\n", tr->entries);
2370 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2371}
2372
2373static ssize_t
2374tracing_entries_write(struct file *filp, const char __user *ubuf,
2375 size_t cnt, loff_t *ppos)
2376{
2377 unsigned long val;
2378 char buf[64];
2379
Steven Rostedtcffae432008-05-12 21:21:00 +02002380 if (cnt >= sizeof(buf))
2381 return -EINVAL;
Steven Rostedta98a3c32008-05-12 21:20:59 +02002382
2383 if (copy_from_user(&buf, ubuf, cnt))
2384 return -EFAULT;
2385
2386 buf[cnt] = 0;
2387
2388 val = simple_strtoul(buf, NULL, 10);
2389
2390 /* must have at least 1 entry */
2391 if (!val)
2392 return -EINVAL;
2393
2394 mutex_lock(&trace_types_lock);
2395
2396 if (current_trace != &no_tracer) {
2397 cnt = -EBUSY;
2398 pr_info("ftrace: set current_tracer to none"
2399 " before modifying buffer size\n");
2400 goto out;
2401 }
2402
2403 if (val > global_trace.entries) {
2404 while (global_trace.entries < val) {
2405 if (trace_alloc_page()) {
2406 cnt = -ENOMEM;
2407 goto out;
2408 }
2409 }
2410 } else {
2411 /* include the number of entries in val (inc of page entries) */
2412 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2413 trace_free_page();
2414 }
2415
2416 filp->f_pos += cnt;
2417
2418 out:
2419 max_tr.entries = global_trace.entries;
2420 mutex_unlock(&trace_types_lock);
2421
2422 return cnt;
2423}
2424
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002425static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002426 .open = tracing_open_generic,
2427 .read = tracing_max_lat_read,
2428 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002429};
2430
2431static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002432 .open = tracing_open_generic,
2433 .read = tracing_ctrl_read,
2434 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002435};
2436
2437static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002438 .open = tracing_open_generic,
2439 .read = tracing_set_trace_read,
2440 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002441};
2442
Steven Rostedtb3806b42008-05-12 21:20:46 +02002443static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002444 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002445 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002446 .read = tracing_read_pipe,
2447 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002448};
2449
Steven Rostedta98a3c32008-05-12 21:20:59 +02002450static struct file_operations tracing_entries_fops = {
2451 .open = tracing_open_generic,
2452 .read = tracing_entries_read,
2453 .write = tracing_entries_write,
2454};
2455
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002456#ifdef CONFIG_DYNAMIC_FTRACE
2457
2458static ssize_t
2459tracing_read_long(struct file *filp, char __user *ubuf,
2460 size_t cnt, loff_t *ppos)
2461{
2462 unsigned long *p = filp->private_data;
2463 char buf[64];
2464 int r;
2465
2466 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002467
2468 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002469}
2470
2471static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002472 .open = tracing_open_generic,
2473 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002474};
2475#endif
2476
2477static struct dentry *d_tracer;
2478
2479struct dentry *tracing_init_dentry(void)
2480{
2481 static int once;
2482
2483 if (d_tracer)
2484 return d_tracer;
2485
2486 d_tracer = debugfs_create_dir("tracing", NULL);
2487
2488 if (!d_tracer && !once) {
2489 once = 1;
2490 pr_warning("Could not create debugfs directory 'tracing'\n");
2491 return NULL;
2492 }
2493
2494 return d_tracer;
2495}
2496
Steven Rostedt60a11772008-05-12 21:20:44 +02002497#ifdef CONFIG_FTRACE_SELFTEST
2498/* Let selftest have access to static functions in this file */
2499#include "trace_selftest.c"
2500#endif
2501
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002502static __init void tracer_init_debugfs(void)
2503{
2504 struct dentry *d_tracer;
2505 struct dentry *entry;
2506
2507 d_tracer = tracing_init_dentry();
2508
2509 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2510 &global_trace, &tracing_ctrl_fops);
2511 if (!entry)
2512 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2513
2514 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2515 NULL, &tracing_iter_fops);
2516 if (!entry)
2517 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2518
Ingo Molnarc7078de2008-05-12 21:20:52 +02002519 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2520 NULL, &tracing_cpumask_fops);
2521 if (!entry)
2522 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2523
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002524 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2525 &global_trace, &tracing_lt_fops);
2526 if (!entry)
2527 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2528
2529 entry = debugfs_create_file("trace", 0444, d_tracer,
2530 &global_trace, &tracing_fops);
2531 if (!entry)
2532 pr_warning("Could not create debugfs 'trace' entry\n");
2533
2534 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2535 &global_trace, &show_traces_fops);
2536 if (!entry)
2537 pr_warning("Could not create debugfs 'trace' entry\n");
2538
2539 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2540 &global_trace, &set_tracer_fops);
2541 if (!entry)
2542 pr_warning("Could not create debugfs 'trace' entry\n");
2543
2544 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2545 &tracing_max_latency,
2546 &tracing_max_lat_fops);
2547 if (!entry)
2548 pr_warning("Could not create debugfs "
2549 "'tracing_max_latency' entry\n");
2550
2551 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2552 &tracing_thresh, &tracing_max_lat_fops);
2553 if (!entry)
2554 pr_warning("Could not create debugfs "
2555 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002556 entry = debugfs_create_file("README", 0644, d_tracer,
2557 NULL, &tracing_readme_fops);
2558 if (!entry)
2559 pr_warning("Could not create debugfs 'README' entry\n");
2560
Steven Rostedtb3806b42008-05-12 21:20:46 +02002561 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2562 NULL, &tracing_pipe_fops);
2563 if (!entry)
2564 pr_warning("Could not create debugfs "
2565 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002566
Steven Rostedta98a3c32008-05-12 21:20:59 +02002567 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2568 &global_trace, &tracing_entries_fops);
2569 if (!entry)
2570 pr_warning("Could not create debugfs "
2571 "'tracing_threash' entry\n");
2572
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002573#ifdef CONFIG_DYNAMIC_FTRACE
2574 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2575 &ftrace_update_tot_cnt,
2576 &tracing_read_long_fops);
2577 if (!entry)
2578 pr_warning("Could not create debugfs "
2579 "'dyn_ftrace_total_info' entry\n");
2580#endif
2581}
2582
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002583static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002584{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002585 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002586 struct page *page, *tmp;
2587 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002588 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002589 int i;
2590
2591 /* first allocate a page for each CPU */
2592 for_each_possible_cpu(i) {
2593 array = (void *)__get_free_page(GFP_KERNEL);
2594 if (array == NULL) {
2595 printk(KERN_ERR "tracer: failed to allocate page"
2596 "for trace buffer!\n");
2597 goto free_pages;
2598 }
2599
2600 page = virt_to_page(array);
2601 list_add(&page->lru, &pages);
2602
2603/* Only allocate if we are actually using the max trace */
2604#ifdef CONFIG_TRACER_MAX_TRACE
2605 array = (void *)__get_free_page(GFP_KERNEL);
2606 if (array == NULL) {
2607 printk(KERN_ERR "tracer: failed to allocate page"
2608 "for trace buffer!\n");
2609 goto free_pages;
2610 }
2611 page = virt_to_page(array);
2612 list_add(&page->lru, &pages);
2613#endif
2614 }
2615
2616 /* Now that we successfully allocate a page per CPU, add them */
2617 for_each_possible_cpu(i) {
2618 data = global_trace.data[i];
2619 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002620 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002621 list_add_tail(&page->lru, &data->trace_pages);
2622 ClearPageLRU(page);
2623
2624#ifdef CONFIG_TRACER_MAX_TRACE
2625 data = max_tr.data[i];
2626 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002627 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002628 list_add_tail(&page->lru, &data->trace_pages);
2629 SetPageLRU(page);
2630#endif
2631 }
2632 global_trace.entries += ENTRIES_PER_PAGE;
2633
2634 return 0;
2635
2636 free_pages:
2637 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002638 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002639 __free_page(page);
2640 }
2641 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002642}
2643
Steven Rostedta98a3c32008-05-12 21:20:59 +02002644static int trace_free_page(void)
2645{
2646 struct trace_array_cpu *data;
2647 struct page *page;
2648 struct list_head *p;
2649 int i;
2650 int ret = 0;
2651
2652 /* free one page from each buffer */
2653 for_each_possible_cpu(i) {
2654 data = global_trace.data[i];
2655 p = data->trace_pages.next;
2656 if (p == &data->trace_pages) {
2657 /* should never happen */
2658 WARN_ON(1);
2659 tracing_disabled = 1;
2660 ret = -1;
2661 break;
2662 }
2663 page = list_entry(p, struct page, lru);
2664 ClearPageLRU(page);
2665 list_del(&page->lru);
2666 __free_page(page);
2667
2668 tracing_reset(data);
2669
2670#ifdef CONFIG_TRACER_MAX_TRACE
2671 data = max_tr.data[i];
2672 p = data->trace_pages.next;
2673 if (p == &data->trace_pages) {
2674 /* should never happen */
2675 WARN_ON(1);
2676 tracing_disabled = 1;
2677 ret = -1;
2678 break;
2679 }
2680 page = list_entry(p, struct page, lru);
2681 ClearPageLRU(page);
2682 list_del(&page->lru);
2683 __free_page(page);
2684
2685 tracing_reset(data);
2686#endif
2687 }
2688 global_trace.entries -= ENTRIES_PER_PAGE;
2689
2690 return ret;
2691}
2692
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002693__init static int tracer_alloc_buffers(void)
2694{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002695 struct trace_array_cpu *data;
2696 void *array;
2697 struct page *page;
2698 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002699 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002700 int i;
2701
Steven Rostedt26994ea2008-05-12 21:20:48 +02002702 global_trace.ctrl = tracer_enabled;
2703
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002704 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002705 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002706 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002707 max_tr.data[i] = &per_cpu(max_data, i);
2708
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002709 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002710 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002711 printk(KERN_ERR "tracer: failed to allocate page"
2712 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002713 goto free_buffers;
2714 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002715
2716 /* set the array to the list */
2717 INIT_LIST_HEAD(&data->trace_pages);
2718 page = virt_to_page(array);
2719 list_add(&page->lru, &data->trace_pages);
2720 /* use the LRU flag to differentiate the two buffers */
2721 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002722
Steven Rostedta98a3c32008-05-12 21:20:59 +02002723 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2724 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2725
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002726/* Only allocate if we are actually using the max trace */
2727#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002728 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002729 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002730 printk(KERN_ERR "tracer: failed to allocate page"
2731 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002732 goto free_buffers;
2733 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002734
2735 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2736 page = virt_to_page(array);
2737 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2738 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002739#endif
2740 }
2741
2742 /*
2743 * Since we allocate by orders of pages, we may be able to
2744 * round up a bit.
2745 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002746 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002747 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002748
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002749 while (global_trace.entries < trace_nr_entries) {
2750 if (trace_alloc_page())
2751 break;
2752 pages++;
2753 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002754 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002755
2756 pr_info("tracer: %d pages allocated for %ld",
2757 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002758 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2759 pr_info(" actual entries %ld\n", global_trace.entries);
2760
2761 tracer_init_debugfs();
2762
2763 trace_init_cmdlines();
2764
2765 register_tracer(&no_tracer);
2766 current_trace = &no_tracer;
2767
Steven Rostedt60a11772008-05-12 21:20:44 +02002768 /* All seems OK, enable tracing */
2769 tracing_disabled = 0;
2770
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002771 return 0;
2772
2773 free_buffers:
2774 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002775 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002776 struct trace_array_cpu *data = global_trace.data[i];
2777
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002778 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002779 list_for_each_entry_safe(page, tmp,
2780 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002781 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002782 __free_page(page);
2783 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002784 }
2785
2786#ifdef CONFIG_TRACER_MAX_TRACE
2787 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002788 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002789 list_for_each_entry_safe(page, tmp,
2790 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002791 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002792 __free_page(page);
2793 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002794 }
2795#endif
2796 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002797 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002798}
Steven Rostedt60a11772008-05-12 21:20:44 +02002799fs_initcall(tracer_alloc_buffers);