blob: 4723e012151d99e558c416c3656351f7c8ef9a61 [file] [log] [blame]
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001/*
2 * ring buffer based function tracer
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
13 */
14#include <linux/utsrelease.h>
15#include <linux/kallsyms.h>
16#include <linux/seq_file.h>
17#include <linux/debugfs.h>
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020018#include <linux/pagemap.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020019#include <linux/hardirq.h>
20#include <linux/linkage.h>
21#include <linux/uaccess.h>
22#include <linux/ftrace.h>
23#include <linux/module.h>
24#include <linux/percpu.h>
25#include <linux/ctype.h>
26#include <linux/init.h>
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020027#include <linux/poll.h>
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020028#include <linux/gfp.h>
29#include <linux/fs.h>
30
Ingo Molnar86387f72008-05-12 21:20:51 +020031#include <linux/stacktrace.h>
32
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020033#include "trace.h"
34
35unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
36unsigned long __read_mostly tracing_thresh;
37
Steven Rostedta98a3c32008-05-12 21:20:59 +020038/* dummy trace to disable tracing */
39static struct tracer no_tracer __read_mostly =
40{
41 .name = "none",
42};
43
44static int trace_alloc_page(void);
45static int trace_free_page(void);
46
Steven Rostedt60a11772008-05-12 21:20:44 +020047static int tracing_disabled = 1;
48
Thomas Gleixner72829bc2008-05-23 21:37:28 +020049long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020050ns2usecs(cycle_t nsec)
51{
52 nsec += 500;
53 do_div(nsec, 1000);
54 return nsec;
55}
56
Ingo Molnare309b412008-05-12 21:20:51 +020057cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020058{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020059 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020060}
61
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020062static struct trace_array global_trace;
63
64static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
65
66static struct trace_array max_tr;
67
68static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
69
Steven Rostedt26994ea2008-05-12 21:20:48 +020070static int tracer_enabled = 1;
Ingo Molnar57422792008-05-12 21:20:51 +020071static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020072
73static struct tracer *trace_types __read_mostly;
74static struct tracer *current_trace __read_mostly;
75static int max_tracer_type_len;
76
77static DEFINE_MUTEX(trace_types_lock);
Ingo Molnar4e655512008-05-12 21:20:52 +020078static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
79
80unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
81
Ingo Molnar4e655512008-05-12 21:20:52 +020082void trace_wake_up(void)
83{
Ingo Molnar017730c2008-05-12 21:20:52 +020084 /*
85 * The runqueue_is_locked() can fail, but this is the best we
86 * have for now:
87 */
88 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +020089 wake_up(&trace_wait);
90}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020091
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020092#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
93
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020094static int __init set_nr_entries(char *str)
95{
96 if (!str)
97 return 0;
98 trace_nr_entries = simple_strtoul(str, &str, 0);
99 return 1;
100}
101__setup("trace_entries=", set_nr_entries);
102
Steven Rostedt57f50be2008-05-12 21:20:44 +0200103unsigned long nsecs_to_usecs(unsigned long nsecs)
104{
105 return nsecs / 1000;
106}
107
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200108enum trace_flag_type {
109 TRACE_FLAG_IRQS_OFF = 0x01,
110 TRACE_FLAG_NEED_RESCHED = 0x02,
111 TRACE_FLAG_HARDIRQ = 0x04,
112 TRACE_FLAG_SOFTIRQ = 0x08,
113};
114
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200115#define TRACE_ITER_SYM_MASK \
116 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
117
118/* These must match the bit postions above */
119static const char *trace_options[] = {
120 "print-parent",
121 "sym-offset",
122 "sym-addr",
123 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200124 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200125 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200126 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200127 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200128 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200129 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200130 NULL
131};
132
Steven Rostedt92205c22008-05-12 21:20:55 +0200133static raw_spinlock_t ftrace_max_lock =
134 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200135
136/*
137 * Copy the new maximum trace into the separate maximum-trace
138 * structure. (this way the maximum trace is permanently saved,
139 * for later retrieval via /debugfs/tracing/latency_trace)
140 */
Ingo Molnare309b412008-05-12 21:20:51 +0200141static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200142__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
143{
144 struct trace_array_cpu *data = tr->data[cpu];
145
146 max_tr.cpu = cpu;
147 max_tr.time_start = data->preempt_timestamp;
148
149 data = max_tr.data[cpu];
150 data->saved_latency = tracing_max_latency;
151
152 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
153 data->pid = tsk->pid;
154 data->uid = tsk->uid;
155 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
156 data->policy = tsk->policy;
157 data->rt_priority = tsk->rt_priority;
158
159 /* record this tasks comm */
160 tracing_record_cmdline(current);
161}
162
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200163void check_pages(struct trace_array_cpu *data)
164{
165 struct page *page, *tmp;
166
167 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
168 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
169
170 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
171 BUG_ON(page->lru.next->prev != &page->lru);
172 BUG_ON(page->lru.prev->next != &page->lru);
173 }
174}
175
176void *head_page(struct trace_array_cpu *data)
177{
178 struct page *page;
179
180 check_pages(data);
181 if (list_empty(&data->trace_pages))
182 return NULL;
183
184 page = list_entry(data->trace_pages.next, struct page, lru);
185 BUG_ON(&page->lru == &data->trace_pages);
186
187 return page_address(page);
188}
189
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200190int
Steven Rostedt214023c2008-05-12 21:20:46 +0200191trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
192{
193 int len = (PAGE_SIZE - 1) - s->len;
194 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200195 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200196
197 if (!len)
198 return 0;
199
200 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200201 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200202 va_end(ap);
203
Steven Rostedtb3806b42008-05-12 21:20:46 +0200204 /* If we can't write it all, don't bother writing anything */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200205 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200206 return 0;
207
208 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200209
210 return len;
211}
212
Ingo Molnare309b412008-05-12 21:20:51 +0200213static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200214trace_seq_puts(struct trace_seq *s, const char *str)
215{
216 int len = strlen(str);
217
218 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200219 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200220
221 memcpy(s->buffer + s->len, str, len);
222 s->len += len;
223
224 return len;
225}
226
Ingo Molnare309b412008-05-12 21:20:51 +0200227static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200228trace_seq_putc(struct trace_seq *s, unsigned char c)
229{
230 if (s->len >= (PAGE_SIZE - 1))
231 return 0;
232
233 s->buffer[s->len++] = c;
234
235 return 1;
236}
237
Ingo Molnare309b412008-05-12 21:20:51 +0200238static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200239trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
240{
241 if (len > ((PAGE_SIZE - 1) - s->len))
242 return 0;
243
244 memcpy(s->buffer + s->len, mem, len);
245 s->len += len;
246
247 return len;
248}
249
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200250#define HEX_CHARS 17
251
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];
256 unsigned char *data;
257 unsigned char byte;
258 int i, j;
259
260 BUG_ON(len >= HEX_CHARS);
261
262 data = mem;
263
264#ifdef __BIG_ENDIAN
265 for (i = 0, j = 0; i < len; i++) {
266#else
267 for (i = len-1, j = 0; i >= 0; i--) {
268#endif
269 byte = data[i];
270
271 hex[j] = byte & 0x0f;
272 if (hex[j] >= 10)
273 hex[j] += 'a' - 10;
274 else
275 hex[j] += '0';
276 j++;
277
278 hex[j] = byte >> 4;
279 if (hex[j] >= 10)
280 hex[j] += 'a' - 10;
281 else
282 hex[j] += '0';
283 j++;
284 }
285 hex[j] = ' ';
286 j++;
287
288 return trace_seq_putmem(s, hex, j);
289}
290
Ingo Molnare309b412008-05-12 21:20:51 +0200291static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200292trace_seq_reset(struct trace_seq *s)
293{
294 s->len = 0;
295}
296
Ingo Molnare309b412008-05-12 21:20:51 +0200297static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200298trace_print_seq(struct seq_file *m, struct trace_seq *s)
299{
300 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
301
302 s->buffer[len] = 0;
303 seq_puts(m, s->buffer);
304
305 trace_seq_reset(s);
306}
307
Ingo Molnare309b412008-05-12 21:20:51 +0200308static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200309flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
310{
311 struct list_head flip_pages;
312
313 INIT_LIST_HEAD(&flip_pages);
314
Steven Rostedt93a588f2008-05-12 21:20:45 +0200315 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200316 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200317 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200318
319 check_pages(tr1);
320 check_pages(tr2);
321 list_splice_init(&tr1->trace_pages, &flip_pages);
322 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
323 list_splice_init(&flip_pages, &tr2->trace_pages);
324 BUG_ON(!list_empty(&flip_pages));
325 check_pages(tr1);
326 check_pages(tr2);
327}
328
Ingo Molnare309b412008-05-12 21:20:51 +0200329void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200330update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
331{
332 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200333 int i;
334
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200335 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200336 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200337 /* clear out all the previous traces */
338 for_each_possible_cpu(i) {
339 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200340 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200341 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200342 }
343
344 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200345 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200346}
347
348/**
349 * update_max_tr_single - only copy one trace over, and reset the rest
350 * @tr - tracer
351 * @tsk - task with the latency
352 * @cpu - the cpu of the buffer to copy.
353 */
Ingo Molnare309b412008-05-12 21:20:51 +0200354void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200355update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
356{
357 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200358 int i;
359
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200360 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200361 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200362 for_each_possible_cpu(i)
363 tracing_reset(max_tr.data[i]);
364
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200365 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200366 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200367
368 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200369 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200370}
371
372int register_tracer(struct tracer *type)
373{
374 struct tracer *t;
375 int len;
376 int ret = 0;
377
378 if (!type->name) {
379 pr_info("Tracer must have a name\n");
380 return -1;
381 }
382
383 mutex_lock(&trace_types_lock);
384 for (t = trace_types; t; t = t->next) {
385 if (strcmp(type->name, t->name) == 0) {
386 /* already found */
387 pr_info("Trace %s already registered\n",
388 type->name);
389 ret = -1;
390 goto out;
391 }
392 }
393
Steven Rostedt60a11772008-05-12 21:20:44 +0200394#ifdef CONFIG_FTRACE_STARTUP_TEST
395 if (type->selftest) {
396 struct tracer *saved_tracer = current_trace;
397 struct trace_array_cpu *data;
398 struct trace_array *tr = &global_trace;
399 int saved_ctrl = tr->ctrl;
400 int i;
401 /*
402 * Run a selftest on this tracer.
403 * Here we reset the trace buffer, and set the current
404 * tracer to be this tracer. The tracer can then run some
405 * internal tracing to verify that everything is in order.
406 * If we fail, we do not register this tracer.
407 */
408 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200409 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200410 if (!head_page(data))
411 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200412 tracing_reset(data);
413 }
414 current_trace = type;
415 tr->ctrl = 0;
416 /* the test is responsible for initializing and enabling */
417 pr_info("Testing tracer %s: ", type->name);
418 ret = type->selftest(type, tr);
419 /* the test is responsible for resetting too */
420 current_trace = saved_tracer;
421 tr->ctrl = saved_ctrl;
422 if (ret) {
423 printk(KERN_CONT "FAILED!\n");
424 goto out;
425 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200426 /* Only reset on passing, to avoid touching corrupted buffers */
427 for_each_possible_cpu(i) {
428 data = tr->data[i];
429 if (!head_page(data))
430 continue;
431 tracing_reset(data);
432 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200433 printk(KERN_CONT "PASSED\n");
434 }
435#endif
436
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200437 type->next = trace_types;
438 trace_types = type;
439 len = strlen(type->name);
440 if (len > max_tracer_type_len)
441 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200442
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200443 out:
444 mutex_unlock(&trace_types_lock);
445
446 return ret;
447}
448
449void unregister_tracer(struct tracer *type)
450{
451 struct tracer **t;
452 int len;
453
454 mutex_lock(&trace_types_lock);
455 for (t = &trace_types; *t; t = &(*t)->next) {
456 if (*t == type)
457 goto found;
458 }
459 pr_info("Trace %s not registered\n", type->name);
460 goto out;
461
462 found:
463 *t = (*t)->next;
464 if (strlen(type->name) != max_tracer_type_len)
465 goto out;
466
467 max_tracer_type_len = 0;
468 for (t = &trace_types; *t; t = &(*t)->next) {
469 len = strlen((*t)->name);
470 if (len > max_tracer_type_len)
471 max_tracer_type_len = len;
472 }
473 out:
474 mutex_unlock(&trace_types_lock);
475}
476
Ingo Molnare309b412008-05-12 21:20:51 +0200477void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200478{
479 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200480 data->trace_head = data->trace_tail = head_page(data);
481 data->trace_head_idx = 0;
482 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200483}
484
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200485#define SAVED_CMDLINES 128
486static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
487static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
488static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
489static int cmdline_idx;
490static DEFINE_SPINLOCK(trace_cmdline_lock);
491atomic_t trace_record_cmdline_disabled;
492
493static void trace_init_cmdlines(void)
494{
495 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
496 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
497 cmdline_idx = 0;
498}
499
Ingo Molnare309b412008-05-12 21:20:51 +0200500void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501
Ingo Molnare309b412008-05-12 21:20:51 +0200502static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200503{
504 unsigned map;
505 unsigned idx;
506
507 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
508 return;
509
510 /*
511 * It's not the end of the world if we don't get
512 * the lock, but we also don't want to spin
513 * nor do we want to disable interrupts,
514 * so if we miss here, then better luck next time.
515 */
516 if (!spin_trylock(&trace_cmdline_lock))
517 return;
518
519 idx = map_pid_to_cmdline[tsk->pid];
520 if (idx >= SAVED_CMDLINES) {
521 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
522
523 map = map_cmdline_to_pid[idx];
524 if (map <= PID_MAX_DEFAULT)
525 map_pid_to_cmdline[map] = (unsigned)-1;
526
527 map_pid_to_cmdline[tsk->pid] = idx;
528
529 cmdline_idx = idx;
530 }
531
532 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
533
534 spin_unlock(&trace_cmdline_lock);
535}
536
Ingo Molnare309b412008-05-12 21:20:51 +0200537static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200538{
539 char *cmdline = "<...>";
540 unsigned map;
541
542 if (!pid)
543 return "<idle>";
544
545 if (pid > PID_MAX_DEFAULT)
546 goto out;
547
548 map = map_pid_to_cmdline[pid];
549 if (map >= SAVED_CMDLINES)
550 goto out;
551
552 cmdline = saved_cmdlines[map];
553
554 out:
555 return cmdline;
556}
557
Ingo Molnare309b412008-05-12 21:20:51 +0200558void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200559{
560 if (atomic_read(&trace_record_cmdline_disabled))
561 return;
562
563 trace_save_cmdline(tsk);
564}
565
Ingo Molnare309b412008-05-12 21:20:51 +0200566static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200567trace_next_list(struct trace_array_cpu *data, struct list_head *next)
568{
569 /*
570 * Roundrobin - but skip the head (which is not a real page):
571 */
572 next = next->next;
573 if (unlikely(next == &data->trace_pages))
574 next = next->next;
575 BUG_ON(next == &data->trace_pages);
576
577 return next;
578}
579
Ingo Molnare309b412008-05-12 21:20:51 +0200580static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200581trace_next_page(struct trace_array_cpu *data, void *addr)
582{
583 struct list_head *next;
584 struct page *page;
585
586 page = virt_to_page(addr);
587
588 next = trace_next_list(data, &page->lru);
589 page = list_entry(next, struct page, lru);
590
591 return page_address(page);
592}
593
Ingo Molnare309b412008-05-12 21:20:51 +0200594static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200595tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200596{
597 unsigned long idx, idx_next;
598 struct trace_entry *entry;
599
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200600 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200601 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200602 idx_next = idx + 1;
603
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200604 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
605
Steven Rostedt93a588f2008-05-12 21:20:45 +0200606 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200607
608 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200609 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200610 idx_next = 0;
611 }
612
Steven Rostedt93a588f2008-05-12 21:20:45 +0200613 if (data->trace_head == data->trace_tail &&
614 idx_next == data->trace_tail_idx) {
615 /* overrun */
616 data->trace_tail_idx++;
617 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
618 data->trace_tail =
619 trace_next_page(data, data->trace_tail);
620 data->trace_tail_idx = 0;
621 }
622 }
623
624 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200625
626 return entry;
627}
628
Ingo Molnare309b412008-05-12 21:20:51 +0200629static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200630tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200631{
632 struct task_struct *tsk = current;
633 unsigned long pc;
634
635 pc = preempt_count();
636
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200637 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200638 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200639 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200640 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
641 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
642 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
643 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
644}
645
Ingo Molnare309b412008-05-12 21:20:51 +0200646void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200647trace_function(struct trace_array *tr, struct trace_array_cpu *data,
648 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200649{
650 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200651 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652
Steven Rostedt92205c22008-05-12 21:20:55 +0200653 raw_local_irq_save(irq_flags);
654 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200655 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200656 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200657 entry->type = TRACE_FN;
658 entry->fn.ip = ip;
659 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200660 __raw_spin_unlock(&data->lock);
661 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200662}
663
Ingo Molnare309b412008-05-12 21:20:51 +0200664void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200665ftrace(struct trace_array *tr, struct trace_array_cpu *data,
666 unsigned long ip, unsigned long parent_ip, unsigned long flags)
667{
668 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200669 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200670}
671
Ingo Molnare309b412008-05-12 21:20:51 +0200672void
Ingo Molnar4e655512008-05-12 21:20:52 +0200673__trace_special(void *__tr, void *__data,
674 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200675{
Ingo Molnar4e655512008-05-12 21:20:52 +0200676 struct trace_array_cpu *data = __data;
677 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200678 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200679 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200680
Steven Rostedt92205c22008-05-12 21:20:55 +0200681 raw_local_irq_save(irq_flags);
682 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200683 entry = tracing_get_trace_entry(tr, data);
684 tracing_generic_entry_update(entry, 0);
685 entry->type = TRACE_SPECIAL;
686 entry->special.arg1 = arg1;
687 entry->special.arg2 = arg2;
688 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200689 __raw_spin_unlock(&data->lock);
690 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200691
692 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200693}
694
695void __trace_stack(struct trace_array *tr,
696 struct trace_array_cpu *data,
697 unsigned long flags,
698 int skip)
699{
700 struct trace_entry *entry;
701 struct stack_trace trace;
702
703 if (!(trace_flags & TRACE_ITER_STACKTRACE))
704 return;
705
706 entry = tracing_get_trace_entry(tr, data);
707 tracing_generic_entry_update(entry, flags);
708 entry->type = TRACE_STACK;
709
710 memset(&entry->stack, 0, sizeof(entry->stack));
711
712 trace.nr_entries = 0;
713 trace.max_entries = FTRACE_STACK_ENTRIES;
714 trace.skip = skip;
715 trace.entries = entry->stack.caller;
716
717 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200718}
719
Ingo Molnare309b412008-05-12 21:20:51 +0200720void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200721tracing_sched_switch_trace(struct trace_array *tr,
722 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200723 struct task_struct *prev,
724 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200725 unsigned long flags)
726{
727 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200728 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200729
Steven Rostedt92205c22008-05-12 21:20:55 +0200730 raw_local_irq_save(irq_flags);
731 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200732 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200733 tracing_generic_entry_update(entry, flags);
734 entry->type = TRACE_CTX;
735 entry->ctx.prev_pid = prev->pid;
736 entry->ctx.prev_prio = prev->prio;
737 entry->ctx.prev_state = prev->state;
738 entry->ctx.next_pid = next->pid;
739 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200740 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200741 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200742 __raw_spin_unlock(&data->lock);
743 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200744}
745
Ingo Molnar57422792008-05-12 21:20:51 +0200746void
747tracing_sched_wakeup_trace(struct trace_array *tr,
748 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200749 struct task_struct *wakee,
750 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200751 unsigned long flags)
752{
753 struct trace_entry *entry;
754 unsigned long irq_flags;
755
Steven Rostedt92205c22008-05-12 21:20:55 +0200756 raw_local_irq_save(irq_flags);
757 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200758 entry = tracing_get_trace_entry(tr, data);
759 tracing_generic_entry_update(entry, flags);
760 entry->type = TRACE_WAKE;
761 entry->ctx.prev_pid = curr->pid;
762 entry->ctx.prev_prio = curr->prio;
763 entry->ctx.prev_state = curr->state;
764 entry->ctx.next_pid = wakee->pid;
765 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200766 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200767 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200768 __raw_spin_unlock(&data->lock);
769 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200770
771 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200772}
773
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200774#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200775static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200776function_trace_call(unsigned long ip, unsigned long parent_ip)
777{
778 struct trace_array *tr = &global_trace;
779 struct trace_array_cpu *data;
780 unsigned long flags;
781 long disabled;
782 int cpu;
783
784 if (unlikely(!tracer_enabled))
785 return;
786
787 local_irq_save(flags);
788 cpu = raw_smp_processor_id();
789 data = tr->data[cpu];
790 disabled = atomic_inc_return(&data->disabled);
791
792 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200793 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200794
795 atomic_dec(&data->disabled);
796 local_irq_restore(flags);
797}
798
799static struct ftrace_ops trace_ops __read_mostly =
800{
801 .func = function_trace_call,
802};
803
Ingo Molnare309b412008-05-12 21:20:51 +0200804void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200805{
806 register_ftrace_function(&trace_ops);
807}
808
Ingo Molnare309b412008-05-12 21:20:51 +0200809void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200810{
811 unregister_ftrace_function(&trace_ops);
812}
813#endif
814
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200815enum trace_file_type {
816 TRACE_FILE_LAT_FMT = 1,
817};
818
819static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200820trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
821 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200822{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200823 struct page *page;
824 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200825
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200826 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200827 iter->next_idx[cpu] >= data->trace_idx ||
828 (data->trace_head == data->trace_tail &&
829 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200830 return NULL;
831
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200832 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200833 /* Initialize the iterator for this cpu trace buffer */
834 WARN_ON(!data->trace_tail);
835 page = virt_to_page(data->trace_tail);
836 iter->next_page[cpu] = &page->lru;
837 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200838 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200839
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200840 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200841 BUG_ON(&data->trace_pages == &page->lru);
842
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200843 array = page_address(page);
844
Steven Rostedt93a588f2008-05-12 21:20:45 +0200845 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200846 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200847}
848
Ingo Molnare309b412008-05-12 21:20:51 +0200849static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200850find_next_entry(struct trace_iterator *iter, int *ent_cpu)
851{
852 struct trace_array *tr = iter->tr;
853 struct trace_entry *ent, *next = NULL;
854 int next_cpu = -1;
855 int cpu;
856
857 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200858 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200859 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200860 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200861 /*
862 * Pick the entry with the smallest timestamp:
863 */
864 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200865 next = ent;
866 next_cpu = cpu;
867 }
868 }
869
870 if (ent_cpu)
871 *ent_cpu = next_cpu;
872
873 return next;
874}
875
Ingo Molnare309b412008-05-12 21:20:51 +0200876static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200877{
878 iter->idx++;
879 iter->next_idx[iter->cpu]++;
880 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200881
Steven Rostedtb3806b42008-05-12 21:20:46 +0200882 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
883 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
884
885 iter->next_page_idx[iter->cpu] = 0;
886 iter->next_page[iter->cpu] =
887 trace_next_list(data, iter->next_page[iter->cpu]);
888 }
889}
890
Ingo Molnare309b412008-05-12 21:20:51 +0200891static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200892{
893 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
894
895 data->trace_tail_idx++;
896 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
897 data->trace_tail = trace_next_page(data, data->trace_tail);
898 data->trace_tail_idx = 0;
899 }
900
901 /* Check if we empty it, then reset the index */
902 if (data->trace_head == data->trace_tail &&
903 data->trace_head_idx == data->trace_tail_idx)
904 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200905}
906
Ingo Molnare309b412008-05-12 21:20:51 +0200907static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200908{
909 struct trace_entry *next;
910 int next_cpu = -1;
911
912 next = find_next_entry(iter, &next_cpu);
913
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200914 iter->prev_ent = iter->ent;
915 iter->prev_cpu = iter->cpu;
916
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200917 iter->ent = next;
918 iter->cpu = next_cpu;
919
Steven Rostedtb3806b42008-05-12 21:20:46 +0200920 if (next)
921 trace_iterator_increment(iter);
922
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200923 return next ? iter : NULL;
924}
925
Ingo Molnare309b412008-05-12 21:20:51 +0200926static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200927{
928 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200929 void *last_ent = iter->ent;
930 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200931 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200932
933 (*pos)++;
934
935 /* can't go backwards */
936 if (iter->idx > i)
937 return NULL;
938
939 if (iter->idx < 0)
940 ent = find_next_entry_inc(iter);
941 else
942 ent = iter;
943
944 while (ent && iter->idx < i)
945 ent = find_next_entry_inc(iter);
946
947 iter->pos = *pos;
948
949 if (last_ent && !ent)
950 seq_puts(m, "\n\nvim:ft=help\n");
951
952 return ent;
953}
954
955static void *s_start(struct seq_file *m, loff_t *pos)
956{
957 struct trace_iterator *iter = m->private;
958 void *p = NULL;
959 loff_t l = 0;
960 int i;
961
962 mutex_lock(&trace_types_lock);
963
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200964 if (!current_trace || current_trace != iter->trace) {
965 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200966 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200967 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200968
969 atomic_inc(&trace_record_cmdline_disabled);
970
971 /* let the tracer grab locks here if needed */
972 if (current_trace->start)
973 current_trace->start(iter);
974
975 if (*pos != iter->pos) {
976 iter->ent = NULL;
977 iter->cpu = 0;
978 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200979 iter->prev_ent = NULL;
980 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200981
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200982 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200983 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200984 iter->next_page[i] = NULL;
985 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200986
987 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
988 ;
989
990 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200991 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200992 p = s_next(m, p, &l);
993 }
994
995 return p;
996}
997
998static void s_stop(struct seq_file *m, void *p)
999{
1000 struct trace_iterator *iter = m->private;
1001
1002 atomic_dec(&trace_record_cmdline_disabled);
1003
1004 /* let the tracer release locks here if needed */
1005 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1006 iter->trace->stop(iter);
1007
1008 mutex_unlock(&trace_types_lock);
1009}
1010
Steven Rostedtb3806b42008-05-12 21:20:46 +02001011static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001012seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001013{
1014#ifdef CONFIG_KALLSYMS
1015 char str[KSYM_SYMBOL_LEN];
1016
1017 kallsyms_lookup(address, NULL, NULL, NULL, str);
1018
Steven Rostedtb3806b42008-05-12 21:20:46 +02001019 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001020#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001021 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001022}
1023
Steven Rostedtb3806b42008-05-12 21:20:46 +02001024static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001025seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1026 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001027{
1028#ifdef CONFIG_KALLSYMS
1029 char str[KSYM_SYMBOL_LEN];
1030
1031 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001032 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001033#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001034 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001035}
1036
1037#ifndef CONFIG_64BIT
1038# define IP_FMT "%08lx"
1039#else
1040# define IP_FMT "%016lx"
1041#endif
1042
Ingo Molnare309b412008-05-12 21:20:51 +02001043static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001044seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001045{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001046 int ret;
1047
1048 if (!ip)
1049 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001050
1051 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001052 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001053 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001054 ret = seq_print_sym_short(s, "%s", ip);
1055
1056 if (!ret)
1057 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001058
1059 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001060 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1061 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001062}
1063
Ingo Molnare309b412008-05-12 21:20:51 +02001064static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001065{
1066 seq_puts(m, "# _------=> CPU# \n");
1067 seq_puts(m, "# / _-----=> irqs-off \n");
1068 seq_puts(m, "# | / _----=> need-resched \n");
1069 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1070 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1071 seq_puts(m, "# |||| / \n");
1072 seq_puts(m, "# ||||| delay \n");
1073 seq_puts(m, "# cmd pid ||||| time | caller \n");
1074 seq_puts(m, "# \\ / ||||| \\ | / \n");
1075}
1076
Ingo Molnare309b412008-05-12 21:20:51 +02001077static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001078{
1079 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1080 seq_puts(m, "# | | | | |\n");
1081}
1082
1083
Ingo Molnare309b412008-05-12 21:20:51 +02001084static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001085print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1086{
1087 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1088 struct trace_array *tr = iter->tr;
1089 struct trace_array_cpu *data = tr->data[tr->cpu];
1090 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001091 unsigned long total = 0;
1092 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001093 int cpu;
1094 const char *name = "preemption";
1095
1096 if (type)
1097 name = type->name;
1098
1099 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001100 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001101 total += tr->data[cpu]->trace_idx;
1102 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001103 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001104 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001105 entries += tr->data[cpu]->trace_idx;
1106 }
1107 }
1108
1109 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1110 name, UTS_RELEASE);
1111 seq_puts(m, "-----------------------------------"
1112 "---------------------------------\n");
1113 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1114 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001115 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001116 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001117 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118 tr->cpu,
1119#if defined(CONFIG_PREEMPT_NONE)
1120 "server",
1121#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1122 "desktop",
1123#elif defined(CONFIG_PREEMPT_DESKTOP)
1124 "preempt",
1125#else
1126 "unknown",
1127#endif
1128 /* These are reserved for later use */
1129 0, 0, 0, 0);
1130#ifdef CONFIG_SMP
1131 seq_printf(m, " #P:%d)\n", num_online_cpus());
1132#else
1133 seq_puts(m, ")\n");
1134#endif
1135 seq_puts(m, " -----------------\n");
1136 seq_printf(m, " | task: %.16s-%d "
1137 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1138 data->comm, data->pid, data->uid, data->nice,
1139 data->policy, data->rt_priority);
1140 seq_puts(m, " -----------------\n");
1141
1142 if (data->critical_start) {
1143 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001144 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1145 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001146 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001147 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1148 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001149 seq_puts(m, "\n");
1150 }
1151
1152 seq_puts(m, "\n");
1153}
1154
Ingo Molnare309b412008-05-12 21:20:51 +02001155static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001156lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001157{
1158 int hardirq, softirq;
1159 char *comm;
1160
1161 comm = trace_find_cmdline(entry->pid);
1162
Steven Rostedt214023c2008-05-12 21:20:46 +02001163 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1164 trace_seq_printf(s, "%d", cpu);
1165 trace_seq_printf(s, "%c%c",
1166 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1167 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001168
1169 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1170 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1171 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001172 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001173 else {
1174 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001175 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001176 else {
1177 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001178 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001179 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001180 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001181 }
1182 }
1183
1184 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001185 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001186 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001187 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001188}
1189
1190unsigned long preempt_mark_thresh = 100;
1191
Ingo Molnare309b412008-05-12 21:20:51 +02001192static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001193lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001194 unsigned long rel_usecs)
1195{
Steven Rostedt214023c2008-05-12 21:20:46 +02001196 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001198 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001199 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001200 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001201 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001202 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001203}
1204
1205static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1206
Ingo Molnare309b412008-05-12 21:20:51 +02001207static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001208print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001209{
Steven Rostedt214023c2008-05-12 21:20:46 +02001210 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001211 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1212 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1213 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1214 struct trace_entry *entry = iter->ent;
1215 unsigned long abs_usecs;
1216 unsigned long rel_usecs;
1217 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001218 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001219 int i;
Ankita Gargd17d9692008-05-12 21:20:58 +02001220 unsigned state;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001221
1222 if (!next_entry)
1223 next_entry = entry;
1224 rel_usecs = ns2usecs(next_entry->t - entry->t);
1225 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1226
1227 if (verbose) {
1228 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001229 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1230 " %ld.%03ldms (+%ld.%03ldms): ",
1231 comm,
1232 entry->pid, cpu, entry->flags,
1233 entry->preempt_count, trace_idx,
1234 ns2usecs(entry->t),
1235 abs_usecs/1000,
1236 abs_usecs % 1000, rel_usecs/1000,
1237 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001238 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001239 lat_print_generic(s, entry, cpu);
1240 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001241 }
1242 switch (entry->type) {
1243 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001244 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1245 trace_seq_puts(s, " (");
1246 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1247 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001248 break;
1249 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001250 case TRACE_WAKE:
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001251 T = entry->ctx.next_state < sizeof(state_to_char) ?
1252 state_to_char[entry->ctx.next_state] : 'X';
1253
Ankita Gargd17d9692008-05-12 21:20:58 +02001254 state = entry->ctx.prev_state ? __ffs(entry->ctx.prev_state) + 1 : 0;
1255 S = state < sizeof(state_to_char) - 1 ? state_to_char[state] : 'X';
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001256 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001257 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001258 entry->ctx.prev_pid,
1259 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001260 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001261 entry->ctx.next_pid,
1262 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001263 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001264 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001265 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001266 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001267 entry->special.arg1,
1268 entry->special.arg2,
1269 entry->special.arg3);
1270 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001271 case TRACE_STACK:
1272 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1273 if (i)
1274 trace_seq_puts(s, " <= ");
1275 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1276 }
1277 trace_seq_puts(s, "\n");
1278 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001279 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001280 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001281 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001282 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283}
1284
Ingo Molnare309b412008-05-12 21:20:51 +02001285static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001286{
Steven Rostedt214023c2008-05-12 21:20:46 +02001287 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001289 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290 unsigned long usec_rem;
1291 unsigned long long t;
1292 unsigned long secs;
1293 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001294 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001295 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001296 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001297
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001298 entry = iter->ent;
1299
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001300 comm = trace_find_cmdline(iter->ent->pid);
1301
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001302 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001303 usec_rem = do_div(t, 1000000ULL);
1304 secs = (unsigned long)t;
1305
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001306 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1307 if (!ret)
1308 return 0;
1309 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1310 if (!ret)
1311 return 0;
1312 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1313 if (!ret)
1314 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001315
1316 switch (entry->type) {
1317 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001318 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1319 if (!ret)
1320 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001321 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1322 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001323 ret = trace_seq_printf(s, " <-");
1324 if (!ret)
1325 return 0;
1326 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1327 sym_flags);
1328 if (!ret)
1329 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001330 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001331 ret = trace_seq_printf(s, "\n");
1332 if (!ret)
1333 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001334 break;
1335 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001336 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001337 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1338 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001339 T = entry->ctx.next_state < sizeof(state_to_char) ?
1340 state_to_char[entry->ctx.next_state] : 'X';
1341 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001342 entry->ctx.prev_pid,
1343 entry->ctx.prev_prio,
1344 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001345 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001346 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001347 entry->ctx.next_prio,
1348 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001349 if (!ret)
1350 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001351 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001352 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001353 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001354 entry->special.arg1,
1355 entry->special.arg2,
1356 entry->special.arg3);
1357 if (!ret)
1358 return 0;
1359 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001360 case TRACE_STACK:
1361 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1362 if (i) {
1363 ret = trace_seq_puts(s, " <= ");
1364 if (!ret)
1365 return 0;
1366 }
1367 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1368 sym_flags);
1369 if (!ret)
1370 return 0;
1371 }
1372 ret = trace_seq_puts(s, "\n");
1373 if (!ret)
1374 return 0;
1375 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001376 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001377 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001378}
1379
Ingo Molnare309b412008-05-12 21:20:51 +02001380static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001381{
1382 struct trace_seq *s = &iter->seq;
1383 struct trace_entry *entry;
1384 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001385 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001386
1387 entry = iter->ent;
1388
1389 ret = trace_seq_printf(s, "%d %d %llu ",
1390 entry->pid, iter->cpu, entry->t);
1391 if (!ret)
1392 return 0;
1393
1394 switch (entry->type) {
1395 case TRACE_FN:
1396 ret = trace_seq_printf(s, "%x %x\n",
1397 entry->fn.ip, entry->fn.parent_ip);
1398 if (!ret)
1399 return 0;
1400 break;
1401 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001402 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001403 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1404 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001405 T = entry->ctx.next_state < sizeof(state_to_char) ?
1406 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001407 if (entry->type == TRACE_WAKE)
1408 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001409 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001410 entry->ctx.prev_pid,
1411 entry->ctx.prev_prio,
1412 S,
1413 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001414 entry->ctx.next_prio,
1415 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001416 if (!ret)
1417 return 0;
1418 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001419 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001420 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001421 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001422 entry->special.arg1,
1423 entry->special.arg2,
1424 entry->special.arg3);
1425 if (!ret)
1426 return 0;
1427 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001428 }
1429 return 1;
1430}
1431
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001432#define SEQ_PUT_FIELD_RET(s, x) \
1433do { \
1434 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1435 return 0; \
1436} while (0)
1437
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001438#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1439do { \
1440 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1441 return 0; \
1442} while (0)
1443
Ingo Molnare309b412008-05-12 21:20:51 +02001444static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001445{
1446 struct trace_seq *s = &iter->seq;
1447 unsigned char newline = '\n';
1448 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001449 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001450
1451 entry = iter->ent;
1452
1453 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1454 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1455 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1456
1457 switch (entry->type) {
1458 case TRACE_FN:
1459 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1460 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1461 break;
1462 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001463 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001464 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1465 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001466 T = entry->ctx.next_state < sizeof(state_to_char) ?
1467 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001468 if (entry->type == TRACE_WAKE)
1469 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001470 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1471 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1472 SEQ_PUT_HEX_FIELD_RET(s, S);
1473 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1474 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1475 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001476 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001477 break;
1478 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001479 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001480 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1481 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1482 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1483 break;
1484 }
1485 SEQ_PUT_FIELD_RET(s, newline);
1486
1487 return 1;
1488}
1489
Ingo Molnare309b412008-05-12 21:20:51 +02001490static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001491{
1492 struct trace_seq *s = &iter->seq;
1493 struct trace_entry *entry;
1494
1495 entry = iter->ent;
1496
1497 SEQ_PUT_FIELD_RET(s, entry->pid);
1498 SEQ_PUT_FIELD_RET(s, entry->cpu);
1499 SEQ_PUT_FIELD_RET(s, entry->t);
1500
1501 switch (entry->type) {
1502 case TRACE_FN:
1503 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1504 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1505 break;
1506 case TRACE_CTX:
1507 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1508 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1509 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1510 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1511 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001512 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001513 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001514 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001515 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001516 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1517 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1518 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1519 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001520 }
1521 return 1;
1522}
1523
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001524static int trace_empty(struct trace_iterator *iter)
1525{
1526 struct trace_array_cpu *data;
1527 int cpu;
1528
1529 for_each_possible_cpu(cpu) {
1530 data = iter->tr->data[cpu];
1531
Steven Rostedtb3806b42008-05-12 21:20:46 +02001532 if (head_page(data) && data->trace_idx &&
1533 (data->trace_tail != data->trace_head ||
1534 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001535 return 0;
1536 }
1537 return 1;
1538}
1539
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001540static int print_trace_line(struct trace_iterator *iter)
1541{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001542 if (iter->trace && iter->trace->print_line)
1543 return iter->trace->print_line(iter);
1544
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001545 if (trace_flags & TRACE_ITER_BIN)
1546 return print_bin_fmt(iter);
1547
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001548 if (trace_flags & TRACE_ITER_HEX)
1549 return print_hex_fmt(iter);
1550
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001551 if (trace_flags & TRACE_ITER_RAW)
1552 return print_raw_fmt(iter);
1553
1554 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1555 return print_lat_fmt(iter, iter->idx, iter->cpu);
1556
1557 return print_trace_fmt(iter);
1558}
1559
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001560static int s_show(struct seq_file *m, void *v)
1561{
1562 struct trace_iterator *iter = v;
1563
1564 if (iter->ent == NULL) {
1565 if (iter->tr) {
1566 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1567 seq_puts(m, "#\n");
1568 }
1569 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1570 /* print nothing if the buffers are empty */
1571 if (trace_empty(iter))
1572 return 0;
1573 print_trace_header(m, iter);
1574 if (!(trace_flags & TRACE_ITER_VERBOSE))
1575 print_lat_help_header(m);
1576 } else {
1577 if (!(trace_flags & TRACE_ITER_VERBOSE))
1578 print_func_help_header(m);
1579 }
1580 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001581 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001582 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001583 }
1584
1585 return 0;
1586}
1587
1588static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001589 .start = s_start,
1590 .next = s_next,
1591 .stop = s_stop,
1592 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001593};
1594
Ingo Molnare309b412008-05-12 21:20:51 +02001595static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001596__tracing_open(struct inode *inode, struct file *file, int *ret)
1597{
1598 struct trace_iterator *iter;
1599
Steven Rostedt60a11772008-05-12 21:20:44 +02001600 if (tracing_disabled) {
1601 *ret = -ENODEV;
1602 return NULL;
1603 }
1604
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001605 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1606 if (!iter) {
1607 *ret = -ENOMEM;
1608 goto out;
1609 }
1610
1611 mutex_lock(&trace_types_lock);
1612 if (current_trace && current_trace->print_max)
1613 iter->tr = &max_tr;
1614 else
1615 iter->tr = inode->i_private;
1616 iter->trace = current_trace;
1617 iter->pos = -1;
1618
1619 /* TODO stop tracer */
1620 *ret = seq_open(file, &tracer_seq_ops);
1621 if (!*ret) {
1622 struct seq_file *m = file->private_data;
1623 m->private = iter;
1624
1625 /* stop the trace while dumping */
1626 if (iter->tr->ctrl)
1627 tracer_enabled = 0;
1628
1629 if (iter->trace && iter->trace->open)
1630 iter->trace->open(iter);
1631 } else {
1632 kfree(iter);
1633 iter = NULL;
1634 }
1635 mutex_unlock(&trace_types_lock);
1636
1637 out:
1638 return iter;
1639}
1640
1641int tracing_open_generic(struct inode *inode, struct file *filp)
1642{
Steven Rostedt60a11772008-05-12 21:20:44 +02001643 if (tracing_disabled)
1644 return -ENODEV;
1645
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001646 filp->private_data = inode->i_private;
1647 return 0;
1648}
1649
1650int tracing_release(struct inode *inode, struct file *file)
1651{
1652 struct seq_file *m = (struct seq_file *)file->private_data;
1653 struct trace_iterator *iter = m->private;
1654
1655 mutex_lock(&trace_types_lock);
1656 if (iter->trace && iter->trace->close)
1657 iter->trace->close(iter);
1658
1659 /* reenable tracing if it was previously enabled */
1660 if (iter->tr->ctrl)
1661 tracer_enabled = 1;
1662 mutex_unlock(&trace_types_lock);
1663
1664 seq_release(inode, file);
1665 kfree(iter);
1666 return 0;
1667}
1668
1669static int tracing_open(struct inode *inode, struct file *file)
1670{
1671 int ret;
1672
1673 __tracing_open(inode, file, &ret);
1674
1675 return ret;
1676}
1677
1678static int tracing_lt_open(struct inode *inode, struct file *file)
1679{
1680 struct trace_iterator *iter;
1681 int ret;
1682
1683 iter = __tracing_open(inode, file, &ret);
1684
1685 if (!ret)
1686 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1687
1688 return ret;
1689}
1690
1691
Ingo Molnare309b412008-05-12 21:20:51 +02001692static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001693t_next(struct seq_file *m, void *v, loff_t *pos)
1694{
1695 struct tracer *t = m->private;
1696
1697 (*pos)++;
1698
1699 if (t)
1700 t = t->next;
1701
1702 m->private = t;
1703
1704 return t;
1705}
1706
1707static void *t_start(struct seq_file *m, loff_t *pos)
1708{
1709 struct tracer *t = m->private;
1710 loff_t l = 0;
1711
1712 mutex_lock(&trace_types_lock);
1713 for (; t && l < *pos; t = t_next(m, t, &l))
1714 ;
1715
1716 return t;
1717}
1718
1719static void t_stop(struct seq_file *m, void *p)
1720{
1721 mutex_unlock(&trace_types_lock);
1722}
1723
1724static int t_show(struct seq_file *m, void *v)
1725{
1726 struct tracer *t = v;
1727
1728 if (!t)
1729 return 0;
1730
1731 seq_printf(m, "%s", t->name);
1732 if (t->next)
1733 seq_putc(m, ' ');
1734 else
1735 seq_putc(m, '\n');
1736
1737 return 0;
1738}
1739
1740static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001741 .start = t_start,
1742 .next = t_next,
1743 .stop = t_stop,
1744 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001745};
1746
1747static int show_traces_open(struct inode *inode, struct file *file)
1748{
1749 int ret;
1750
Steven Rostedt60a11772008-05-12 21:20:44 +02001751 if (tracing_disabled)
1752 return -ENODEV;
1753
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001754 ret = seq_open(file, &show_traces_seq_ops);
1755 if (!ret) {
1756 struct seq_file *m = file->private_data;
1757 m->private = trace_types;
1758 }
1759
1760 return ret;
1761}
1762
1763static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001764 .open = tracing_open,
1765 .read = seq_read,
1766 .llseek = seq_lseek,
1767 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001768};
1769
1770static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001771 .open = tracing_lt_open,
1772 .read = seq_read,
1773 .llseek = seq_lseek,
1774 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001775};
1776
1777static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001778 .open = show_traces_open,
1779 .read = seq_read,
1780 .release = seq_release,
1781};
1782
Ingo Molnar36dfe922008-05-12 21:20:52 +02001783/*
1784 * Only trace on a CPU if the bitmask is set:
1785 */
1786static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1787
1788/*
1789 * When tracing/tracing_cpu_mask is modified then this holds
1790 * the new bitmask we are about to install:
1791 */
1792static cpumask_t tracing_cpumask_new;
1793
1794/*
1795 * The tracer itself will not take this lock, but still we want
1796 * to provide a consistent cpumask to user-space:
1797 */
1798static DEFINE_MUTEX(tracing_cpumask_update_lock);
1799
1800/*
1801 * Temporary storage for the character representation of the
1802 * CPU bitmask (and one more byte for the newline):
1803 */
1804static char mask_str[NR_CPUS + 1];
1805
Ingo Molnarc7078de2008-05-12 21:20:52 +02001806static ssize_t
1807tracing_cpumask_read(struct file *filp, char __user *ubuf,
1808 size_t count, loff_t *ppos)
1809{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001810 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001811
1812 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001813
1814 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1815 if (count - len < 2) {
1816 count = -EINVAL;
1817 goto out_err;
1818 }
1819 len += sprintf(mask_str + len, "\n");
1820 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1821
1822out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001823 mutex_unlock(&tracing_cpumask_update_lock);
1824
1825 return count;
1826}
1827
1828static ssize_t
1829tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1830 size_t count, loff_t *ppos)
1831{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001832 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001833
1834 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001835 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1836 if (err)
1837 goto err_unlock;
1838
Steven Rostedt92205c22008-05-12 21:20:55 +02001839 raw_local_irq_disable();
1840 __raw_spin_lock(&ftrace_max_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001841 for_each_possible_cpu(cpu) {
1842 /*
1843 * Increase/decrease the disabled counter if we are
1844 * about to flip a bit in the cpumask:
1845 */
1846 if (cpu_isset(cpu, tracing_cpumask) &&
1847 !cpu_isset(cpu, tracing_cpumask_new)) {
1848 atomic_inc(&global_trace.data[cpu]->disabled);
1849 }
1850 if (!cpu_isset(cpu, tracing_cpumask) &&
1851 cpu_isset(cpu, tracing_cpumask_new)) {
1852 atomic_dec(&global_trace.data[cpu]->disabled);
1853 }
1854 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001855 __raw_spin_unlock(&ftrace_max_lock);
1856 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001857
1858 tracing_cpumask = tracing_cpumask_new;
1859
Ingo Molnarc7078de2008-05-12 21:20:52 +02001860 mutex_unlock(&tracing_cpumask_update_lock);
1861
Ingo Molnarc7078de2008-05-12 21:20:52 +02001862 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001863
1864err_unlock:
1865 mutex_unlock(&tracing_cpumask_update_lock);
1866
1867 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001868}
1869
1870static struct file_operations tracing_cpumask_fops = {
1871 .open = tracing_open_generic,
1872 .read = tracing_cpumask_read,
1873 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001874};
1875
1876static ssize_t
1877tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1878 size_t cnt, loff_t *ppos)
1879{
1880 char *buf;
1881 int r = 0;
1882 int len = 0;
1883 int i;
1884
1885 /* calulate max size */
1886 for (i = 0; trace_options[i]; i++) {
1887 len += strlen(trace_options[i]);
1888 len += 3; /* "no" and space */
1889 }
1890
1891 /* +2 for \n and \0 */
1892 buf = kmalloc(len + 2, GFP_KERNEL);
1893 if (!buf)
1894 return -ENOMEM;
1895
1896 for (i = 0; trace_options[i]; i++) {
1897 if (trace_flags & (1 << i))
1898 r += sprintf(buf + r, "%s ", trace_options[i]);
1899 else
1900 r += sprintf(buf + r, "no%s ", trace_options[i]);
1901 }
1902
1903 r += sprintf(buf + r, "\n");
1904 WARN_ON(r >= len + 2);
1905
Ingo Molnar36dfe922008-05-12 21:20:52 +02001906 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001907
1908 kfree(buf);
1909
1910 return r;
1911}
1912
1913static ssize_t
1914tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1915 size_t cnt, loff_t *ppos)
1916{
1917 char buf[64];
1918 char *cmp = buf;
1919 int neg = 0;
1920 int i;
1921
1922 if (cnt > 63)
1923 cnt = 63;
1924
1925 if (copy_from_user(&buf, ubuf, cnt))
1926 return -EFAULT;
1927
1928 buf[cnt] = 0;
1929
1930 if (strncmp(buf, "no", 2) == 0) {
1931 neg = 1;
1932 cmp += 2;
1933 }
1934
1935 for (i = 0; trace_options[i]; i++) {
1936 int len = strlen(trace_options[i]);
1937
1938 if (strncmp(cmp, trace_options[i], len) == 0) {
1939 if (neg)
1940 trace_flags &= ~(1 << i);
1941 else
1942 trace_flags |= (1 << i);
1943 break;
1944 }
1945 }
Ingo Molnar442e5442008-05-12 21:20:53 +02001946 /*
1947 * If no option could be set, return an error:
1948 */
1949 if (!trace_options[i])
1950 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001951
1952 filp->f_pos += cnt;
1953
1954 return cnt;
1955}
1956
1957static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001958 .open = tracing_open_generic,
1959 .read = tracing_iter_ctrl_read,
1960 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001961};
1962
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001963static const char readme_msg[] =
1964 "tracing mini-HOWTO:\n\n"
1965 "# mkdir /debug\n"
1966 "# mount -t debugfs nodev /debug\n\n"
1967 "# cat /debug/tracing/available_tracers\n"
1968 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1969 "# cat /debug/tracing/current_tracer\n"
1970 "none\n"
1971 "# echo sched_switch > /debug/tracing/current_tracer\n"
1972 "# cat /debug/tracing/current_tracer\n"
1973 "sched_switch\n"
1974 "# cat /debug/tracing/iter_ctrl\n"
1975 "noprint-parent nosym-offset nosym-addr noverbose\n"
1976 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1977 "# echo 1 > /debug/tracing/tracing_enabled\n"
1978 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1979 "echo 0 > /debug/tracing/tracing_enabled\n"
1980;
1981
1982static ssize_t
1983tracing_readme_read(struct file *filp, char __user *ubuf,
1984 size_t cnt, loff_t *ppos)
1985{
1986 return simple_read_from_buffer(ubuf, cnt, ppos,
1987 readme_msg, strlen(readme_msg));
1988}
1989
1990static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001991 .open = tracing_open_generic,
1992 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001993};
1994
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001995static ssize_t
1996tracing_ctrl_read(struct file *filp, char __user *ubuf,
1997 size_t cnt, loff_t *ppos)
1998{
1999 struct trace_array *tr = filp->private_data;
2000 char buf[64];
2001 int r;
2002
2003 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002004 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002005}
2006
2007static ssize_t
2008tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2009 size_t cnt, loff_t *ppos)
2010{
2011 struct trace_array *tr = filp->private_data;
2012 long val;
2013 char buf[64];
2014
2015 if (cnt > 63)
2016 cnt = 63;
2017
2018 if (copy_from_user(&buf, ubuf, cnt))
2019 return -EFAULT;
2020
2021 buf[cnt] = 0;
2022
2023 val = simple_strtoul(buf, NULL, 10);
2024
2025 val = !!val;
2026
2027 mutex_lock(&trace_types_lock);
2028 if (tr->ctrl ^ val) {
2029 if (val)
2030 tracer_enabled = 1;
2031 else
2032 tracer_enabled = 0;
2033
2034 tr->ctrl = val;
2035
2036 if (current_trace && current_trace->ctrl_update)
2037 current_trace->ctrl_update(tr);
2038 }
2039 mutex_unlock(&trace_types_lock);
2040
2041 filp->f_pos += cnt;
2042
2043 return cnt;
2044}
2045
2046static ssize_t
2047tracing_set_trace_read(struct file *filp, char __user *ubuf,
2048 size_t cnt, loff_t *ppos)
2049{
2050 char buf[max_tracer_type_len+2];
2051 int r;
2052
2053 mutex_lock(&trace_types_lock);
2054 if (current_trace)
2055 r = sprintf(buf, "%s\n", current_trace->name);
2056 else
2057 r = sprintf(buf, "\n");
2058 mutex_unlock(&trace_types_lock);
2059
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002060 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002061}
2062
2063static ssize_t
2064tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2065 size_t cnt, loff_t *ppos)
2066{
2067 struct trace_array *tr = &global_trace;
2068 struct tracer *t;
2069 char buf[max_tracer_type_len+1];
2070 int i;
2071
2072 if (cnt > max_tracer_type_len)
2073 cnt = max_tracer_type_len;
2074
2075 if (copy_from_user(&buf, ubuf, cnt))
2076 return -EFAULT;
2077
2078 buf[cnt] = 0;
2079
2080 /* strip ending whitespace. */
2081 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2082 buf[i] = 0;
2083
2084 mutex_lock(&trace_types_lock);
2085 for (t = trace_types; t; t = t->next) {
2086 if (strcmp(t->name, buf) == 0)
2087 break;
2088 }
2089 if (!t || t == current_trace)
2090 goto out;
2091
2092 if (current_trace && current_trace->reset)
2093 current_trace->reset(tr);
2094
2095 current_trace = t;
2096 if (t->init)
2097 t->init(tr);
2098
2099 out:
2100 mutex_unlock(&trace_types_lock);
2101
2102 filp->f_pos += cnt;
2103
2104 return cnt;
2105}
2106
2107static ssize_t
2108tracing_max_lat_read(struct file *filp, char __user *ubuf,
2109 size_t cnt, loff_t *ppos)
2110{
2111 unsigned long *ptr = filp->private_data;
2112 char buf[64];
2113 int r;
2114
2115 r = snprintf(buf, 64, "%ld\n",
2116 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2117 if (r > 64)
2118 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002119 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002120}
2121
2122static ssize_t
2123tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2124 size_t cnt, loff_t *ppos)
2125{
2126 long *ptr = filp->private_data;
2127 long val;
2128 char buf[64];
2129
2130 if (cnt > 63)
2131 cnt = 63;
2132
2133 if (copy_from_user(&buf, ubuf, cnt))
2134 return -EFAULT;
2135
2136 buf[cnt] = 0;
2137
2138 val = simple_strtoul(buf, NULL, 10);
2139
2140 *ptr = val * 1000;
2141
2142 return cnt;
2143}
2144
Steven Rostedtb3806b42008-05-12 21:20:46 +02002145static atomic_t tracing_reader;
2146
2147static int tracing_open_pipe(struct inode *inode, struct file *filp)
2148{
2149 struct trace_iterator *iter;
2150
2151 if (tracing_disabled)
2152 return -ENODEV;
2153
2154 /* We only allow for reader of the pipe */
2155 if (atomic_inc_return(&tracing_reader) != 1) {
2156 atomic_dec(&tracing_reader);
2157 return -EBUSY;
2158 }
2159
2160 /* create a buffer to store the information to pass to userspace */
2161 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2162 if (!iter)
2163 return -ENOMEM;
2164
2165 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002166 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002167
2168 filp->private_data = iter;
2169
2170 return 0;
2171}
2172
2173static int tracing_release_pipe(struct inode *inode, struct file *file)
2174{
2175 struct trace_iterator *iter = file->private_data;
2176
2177 kfree(iter);
2178 atomic_dec(&tracing_reader);
2179
2180 return 0;
2181}
2182
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002183static unsigned int
2184tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2185{
2186 struct trace_iterator *iter = filp->private_data;
2187
2188 if (trace_flags & TRACE_ITER_BLOCK) {
2189 /*
2190 * Always select as readable when in blocking mode
2191 */
2192 return POLLIN | POLLRDNORM;
2193 }
2194 else {
2195 if (!trace_empty(iter))
2196 return POLLIN | POLLRDNORM;
2197 poll_wait(filp, &trace_wait, poll_table);
2198 if (!trace_empty(iter))
2199 return POLLIN | POLLRDNORM;
2200
2201 return 0;
2202 }
2203}
2204
Steven Rostedtb3806b42008-05-12 21:20:46 +02002205/*
2206 * Consumer reader.
2207 */
2208static ssize_t
2209tracing_read_pipe(struct file *filp, char __user *ubuf,
2210 size_t cnt, loff_t *ppos)
2211{
2212 struct trace_iterator *iter = filp->private_data;
2213 struct trace_array_cpu *data;
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002214 struct trace_array *tr = iter->tr;
2215 struct tracer *tracer = iter->trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002216 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002217 static int start;
2218 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002219#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002220 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002221#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002222 int read = 0;
2223 int cpu;
2224 int len;
2225 int ret;
2226
2227 /* return any leftover data */
2228 if (iter->seq.len > start) {
2229 len = iter->seq.len - start;
2230 if (cnt > len)
2231 cnt = len;
2232 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2233 if (ret)
2234 cnt = -EFAULT;
2235
2236 start += len;
2237
2238 return cnt;
2239 }
2240
2241 trace_seq_reset(&iter->seq);
2242 start = 0;
2243
2244 while (trace_empty(iter)) {
Steven Rostedt2dc8f092008-05-12 21:20:58 +02002245
2246 if ((filp->f_flags & O_NONBLOCK))
2247 return -EAGAIN;
2248
Steven Rostedtb3806b42008-05-12 21:20:46 +02002249 /*
2250 * This is a make-shift waitqueue. The reason we don't use
2251 * an actual wait queue is because:
2252 * 1) we only ever have one waiter
2253 * 2) the tracing, traces all functions, we don't want
2254 * the overhead of calling wake_up and friends
2255 * (and tracing them too)
2256 * Anyway, this is really very primitive wakeup.
2257 */
2258 set_current_state(TASK_INTERRUPTIBLE);
2259 iter->tr->waiter = current;
2260
2261 /* sleep for one second, and try again. */
2262 schedule_timeout(HZ);
2263
2264 iter->tr->waiter = NULL;
2265
2266 if (signal_pending(current))
2267 return -EINTR;
2268
Steven Rostedt84527992008-05-12 21:20:58 +02002269 if (iter->trace != current_trace)
2270 return 0;
2271
Steven Rostedtb3806b42008-05-12 21:20:46 +02002272 /*
2273 * We block until we read something and tracing is disabled.
2274 * We still block if tracing is disabled, but we have never
2275 * read anything. This allows a user to cat this file, and
2276 * then enable tracing. But after we have read something,
2277 * we give an EOF when tracing is again disabled.
2278 *
2279 * iter->pos will be 0 if we haven't read anything.
2280 */
2281 if (!tracer_enabled && iter->pos)
2282 break;
2283
2284 continue;
2285 }
2286
2287 /* stop when tracing is finished */
2288 if (trace_empty(iter))
2289 return 0;
2290
2291 if (cnt >= PAGE_SIZE)
2292 cnt = PAGE_SIZE - 1;
2293
2294 memset(iter, 0, sizeof(*iter));
Steven Rostedtb5685ae2008-05-12 21:20:58 +02002295 iter->tr = tr;
2296 iter->trace = tracer;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002297 iter->pos = -1;
2298
2299 /*
2300 * We need to stop all tracing on all CPUS to read the
2301 * the next buffer. This is a bit expensive, but is
2302 * not done often. We fill all what we can read,
2303 * and then release the locks again.
2304 */
2305
2306 cpus_clear(mask);
2307 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002308#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002309 ftrace_save = ftrace_enabled;
2310 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002311#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002312 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002313 for_each_possible_cpu(cpu) {
2314 data = iter->tr->data[cpu];
2315
2316 if (!head_page(data) || !data->trace_idx)
2317 continue;
2318
2319 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002320 cpu_set(cpu, mask);
2321 }
2322
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002323 for_each_cpu_mask(cpu, mask) {
2324 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002325 __raw_spin_lock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002326 }
2327
Steven Rostedt088b1e422008-05-12 21:20:48 +02002328 while (find_next_entry_inc(iter) != NULL) {
2329 int len = iter->seq.len;
2330
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002331 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002332 if (!ret) {
2333 /* don't print partial lines */
2334 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002335 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002336 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002337
2338 trace_consume(iter);
2339
2340 if (iter->seq.len >= cnt)
2341 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002342 }
2343
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002344 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002345 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002346 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002347 }
2348
2349 for_each_cpu_mask(cpu, mask) {
2350 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002351 atomic_dec(&data->disabled);
2352 }
Ingo Molnar25770462008-05-12 21:20:49 +02002353#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002354 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002355#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002356 local_irq_restore(flags);
2357
2358 /* Now copy what we have to the user */
2359 read = iter->seq.len;
2360 if (read > cnt)
2361 read = cnt;
2362
2363 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2364
2365 if (read < iter->seq.len)
2366 start = read;
2367 else
2368 trace_seq_reset(&iter->seq);
2369
2370 if (ret)
2371 read = -EFAULT;
2372
2373 return read;
2374}
2375
Steven Rostedta98a3c32008-05-12 21:20:59 +02002376static ssize_t
2377tracing_entries_read(struct file *filp, char __user *ubuf,
2378 size_t cnt, loff_t *ppos)
2379{
2380 struct trace_array *tr = filp->private_data;
2381 char buf[64];
2382 int r;
2383
2384 r = sprintf(buf, "%lu\n", tr->entries);
2385 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2386}
2387
2388static ssize_t
2389tracing_entries_write(struct file *filp, const char __user *ubuf,
2390 size_t cnt, loff_t *ppos)
2391{
2392 unsigned long val;
2393 char buf[64];
2394
2395 if (cnt > 63)
2396 cnt = 63;
2397
2398 if (copy_from_user(&buf, ubuf, cnt))
2399 return -EFAULT;
2400
2401 buf[cnt] = 0;
2402
2403 val = simple_strtoul(buf, NULL, 10);
2404
2405 /* must have at least 1 entry */
2406 if (!val)
2407 return -EINVAL;
2408
2409 mutex_lock(&trace_types_lock);
2410
2411 if (current_trace != &no_tracer) {
2412 cnt = -EBUSY;
2413 pr_info("ftrace: set current_tracer to none"
2414 " before modifying buffer size\n");
2415 goto out;
2416 }
2417
2418 if (val > global_trace.entries) {
2419 while (global_trace.entries < val) {
2420 if (trace_alloc_page()) {
2421 cnt = -ENOMEM;
2422 goto out;
2423 }
2424 }
2425 } else {
2426 /* include the number of entries in val (inc of page entries) */
2427 while (global_trace.entries > val + (ENTRIES_PER_PAGE - 1))
2428 trace_free_page();
2429 }
2430
2431 filp->f_pos += cnt;
2432
2433 out:
2434 max_tr.entries = global_trace.entries;
2435 mutex_unlock(&trace_types_lock);
2436
2437 return cnt;
2438}
2439
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002440static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002441 .open = tracing_open_generic,
2442 .read = tracing_max_lat_read,
2443 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002444};
2445
2446static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002447 .open = tracing_open_generic,
2448 .read = tracing_ctrl_read,
2449 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002450};
2451
2452static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002453 .open = tracing_open_generic,
2454 .read = tracing_set_trace_read,
2455 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002456};
2457
Steven Rostedtb3806b42008-05-12 21:20:46 +02002458static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002459 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002460 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002461 .read = tracing_read_pipe,
2462 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002463};
2464
Steven Rostedta98a3c32008-05-12 21:20:59 +02002465static struct file_operations tracing_entries_fops = {
2466 .open = tracing_open_generic,
2467 .read = tracing_entries_read,
2468 .write = tracing_entries_write,
2469};
2470
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002471#ifdef CONFIG_DYNAMIC_FTRACE
2472
2473static ssize_t
2474tracing_read_long(struct file *filp, char __user *ubuf,
2475 size_t cnt, loff_t *ppos)
2476{
2477 unsigned long *p = filp->private_data;
2478 char buf[64];
2479 int r;
2480
2481 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002482
2483 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002484}
2485
2486static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002487 .open = tracing_open_generic,
2488 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002489};
2490#endif
2491
2492static struct dentry *d_tracer;
2493
2494struct dentry *tracing_init_dentry(void)
2495{
2496 static int once;
2497
2498 if (d_tracer)
2499 return d_tracer;
2500
2501 d_tracer = debugfs_create_dir("tracing", NULL);
2502
2503 if (!d_tracer && !once) {
2504 once = 1;
2505 pr_warning("Could not create debugfs directory 'tracing'\n");
2506 return NULL;
2507 }
2508
2509 return d_tracer;
2510}
2511
Steven Rostedt60a11772008-05-12 21:20:44 +02002512#ifdef CONFIG_FTRACE_SELFTEST
2513/* Let selftest have access to static functions in this file */
2514#include "trace_selftest.c"
2515#endif
2516
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002517static __init void tracer_init_debugfs(void)
2518{
2519 struct dentry *d_tracer;
2520 struct dentry *entry;
2521
2522 d_tracer = tracing_init_dentry();
2523
2524 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2525 &global_trace, &tracing_ctrl_fops);
2526 if (!entry)
2527 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2528
2529 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2530 NULL, &tracing_iter_fops);
2531 if (!entry)
2532 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2533
Ingo Molnarc7078de2008-05-12 21:20:52 +02002534 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2535 NULL, &tracing_cpumask_fops);
2536 if (!entry)
2537 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2538
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002539 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2540 &global_trace, &tracing_lt_fops);
2541 if (!entry)
2542 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2543
2544 entry = debugfs_create_file("trace", 0444, d_tracer,
2545 &global_trace, &tracing_fops);
2546 if (!entry)
2547 pr_warning("Could not create debugfs 'trace' entry\n");
2548
2549 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2550 &global_trace, &show_traces_fops);
2551 if (!entry)
2552 pr_warning("Could not create debugfs 'trace' entry\n");
2553
2554 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2555 &global_trace, &set_tracer_fops);
2556 if (!entry)
2557 pr_warning("Could not create debugfs 'trace' entry\n");
2558
2559 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2560 &tracing_max_latency,
2561 &tracing_max_lat_fops);
2562 if (!entry)
2563 pr_warning("Could not create debugfs "
2564 "'tracing_max_latency' entry\n");
2565
2566 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2567 &tracing_thresh, &tracing_max_lat_fops);
2568 if (!entry)
2569 pr_warning("Could not create debugfs "
2570 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002571 entry = debugfs_create_file("README", 0644, d_tracer,
2572 NULL, &tracing_readme_fops);
2573 if (!entry)
2574 pr_warning("Could not create debugfs 'README' entry\n");
2575
Steven Rostedtb3806b42008-05-12 21:20:46 +02002576 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2577 NULL, &tracing_pipe_fops);
2578 if (!entry)
2579 pr_warning("Could not create debugfs "
2580 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002581
Steven Rostedta98a3c32008-05-12 21:20:59 +02002582 entry = debugfs_create_file("trace_entries", 0644, d_tracer,
2583 &global_trace, &tracing_entries_fops);
2584 if (!entry)
2585 pr_warning("Could not create debugfs "
2586 "'tracing_threash' entry\n");
2587
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002588#ifdef CONFIG_DYNAMIC_FTRACE
2589 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2590 &ftrace_update_tot_cnt,
2591 &tracing_read_long_fops);
2592 if (!entry)
2593 pr_warning("Could not create debugfs "
2594 "'dyn_ftrace_total_info' entry\n");
2595#endif
2596}
2597
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002598static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002599{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002600 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002601 struct page *page, *tmp;
2602 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002603 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002604 int i;
2605
2606 /* first allocate a page for each CPU */
2607 for_each_possible_cpu(i) {
2608 array = (void *)__get_free_page(GFP_KERNEL);
2609 if (array == NULL) {
2610 printk(KERN_ERR "tracer: failed to allocate page"
2611 "for trace buffer!\n");
2612 goto free_pages;
2613 }
2614
2615 page = virt_to_page(array);
2616 list_add(&page->lru, &pages);
2617
2618/* Only allocate if we are actually using the max trace */
2619#ifdef CONFIG_TRACER_MAX_TRACE
2620 array = (void *)__get_free_page(GFP_KERNEL);
2621 if (array == NULL) {
2622 printk(KERN_ERR "tracer: failed to allocate page"
2623 "for trace buffer!\n");
2624 goto free_pages;
2625 }
2626 page = virt_to_page(array);
2627 list_add(&page->lru, &pages);
2628#endif
2629 }
2630
2631 /* Now that we successfully allocate a page per CPU, add them */
2632 for_each_possible_cpu(i) {
2633 data = global_trace.data[i];
2634 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002635 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002636 list_add_tail(&page->lru, &data->trace_pages);
2637 ClearPageLRU(page);
2638
2639#ifdef CONFIG_TRACER_MAX_TRACE
2640 data = max_tr.data[i];
2641 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002642 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002643 list_add_tail(&page->lru, &data->trace_pages);
2644 SetPageLRU(page);
2645#endif
2646 }
2647 global_trace.entries += ENTRIES_PER_PAGE;
2648
2649 return 0;
2650
2651 free_pages:
2652 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002653 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002654 __free_page(page);
2655 }
2656 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002657}
2658
Steven Rostedta98a3c32008-05-12 21:20:59 +02002659static int trace_free_page(void)
2660{
2661 struct trace_array_cpu *data;
2662 struct page *page;
2663 struct list_head *p;
2664 int i;
2665 int ret = 0;
2666
2667 /* free one page from each buffer */
2668 for_each_possible_cpu(i) {
2669 data = global_trace.data[i];
2670 p = data->trace_pages.next;
2671 if (p == &data->trace_pages) {
2672 /* should never happen */
2673 WARN_ON(1);
2674 tracing_disabled = 1;
2675 ret = -1;
2676 break;
2677 }
2678 page = list_entry(p, struct page, lru);
2679 ClearPageLRU(page);
2680 list_del(&page->lru);
2681 __free_page(page);
2682
2683 tracing_reset(data);
2684
2685#ifdef CONFIG_TRACER_MAX_TRACE
2686 data = max_tr.data[i];
2687 p = data->trace_pages.next;
2688 if (p == &data->trace_pages) {
2689 /* should never happen */
2690 WARN_ON(1);
2691 tracing_disabled = 1;
2692 ret = -1;
2693 break;
2694 }
2695 page = list_entry(p, struct page, lru);
2696 ClearPageLRU(page);
2697 list_del(&page->lru);
2698 __free_page(page);
2699
2700 tracing_reset(data);
2701#endif
2702 }
2703 global_trace.entries -= ENTRIES_PER_PAGE;
2704
2705 return ret;
2706}
2707
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002708__init static int tracer_alloc_buffers(void)
2709{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002710 struct trace_array_cpu *data;
2711 void *array;
2712 struct page *page;
2713 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002714 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002715 int i;
2716
Steven Rostedt26994ea2008-05-12 21:20:48 +02002717 global_trace.ctrl = tracer_enabled;
2718
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002719 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002720 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002721 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002722 max_tr.data[i] = &per_cpu(max_data, i);
2723
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002724 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002725 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002726 printk(KERN_ERR "tracer: failed to allocate page"
2727 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002728 goto free_buffers;
2729 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002730
2731 /* set the array to the list */
2732 INIT_LIST_HEAD(&data->trace_pages);
2733 page = virt_to_page(array);
2734 list_add(&page->lru, &data->trace_pages);
2735 /* use the LRU flag to differentiate the two buffers */
2736 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002737
Steven Rostedta98a3c32008-05-12 21:20:59 +02002738 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2739 max_tr.data[i]->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
2740
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002741/* Only allocate if we are actually using the max trace */
2742#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002743 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002744 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002745 printk(KERN_ERR "tracer: failed to allocate page"
2746 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002747 goto free_buffers;
2748 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002749
2750 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2751 page = virt_to_page(array);
2752 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2753 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002754#endif
2755 }
2756
2757 /*
2758 * Since we allocate by orders of pages, we may be able to
2759 * round up a bit.
2760 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002761 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002762 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002763
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002764 while (global_trace.entries < trace_nr_entries) {
2765 if (trace_alloc_page())
2766 break;
2767 pages++;
2768 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002769 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002770
2771 pr_info("tracer: %d pages allocated for %ld",
2772 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002773 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2774 pr_info(" actual entries %ld\n", global_trace.entries);
2775
2776 tracer_init_debugfs();
2777
2778 trace_init_cmdlines();
2779
2780 register_tracer(&no_tracer);
2781 current_trace = &no_tracer;
2782
Steven Rostedt60a11772008-05-12 21:20:44 +02002783 /* All seems OK, enable tracing */
2784 tracing_disabled = 0;
2785
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002786 return 0;
2787
2788 free_buffers:
2789 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002790 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002791 struct trace_array_cpu *data = global_trace.data[i];
2792
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002793 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002794 list_for_each_entry_safe(page, tmp,
2795 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002796 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002797 __free_page(page);
2798 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002799 }
2800
2801#ifdef CONFIG_TRACER_MAX_TRACE
2802 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002803 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002804 list_for_each_entry_safe(page, tmp,
2805 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002806 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002807 __free_page(page);
2808 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002809 }
2810#endif
2811 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002812 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002813}
Steven Rostedt60a11772008-05-12 21:20:44 +02002814fs_initcall(tracer_alloc_buffers);