blob: 71b25b79b3decce0ad1ccfe171a93b5ce2e07c51 [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>
27#include <linux/gfp.h>
28#include <linux/fs.h>
29
30#include "trace.h"
31
32unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
33unsigned long __read_mostly tracing_thresh;
34
Steven Rostedt60a11772008-05-12 21:20:44 +020035static int tracing_disabled = 1;
36
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020037static long notrace
38ns2usecs(cycle_t nsec)
39{
40 nsec += 500;
41 do_div(nsec, 1000);
42 return nsec;
43}
44
Ingo Molnar750ed1a2008-05-12 21:20:46 +020045notrace cycle_t ftrace_now(int cpu)
46{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020047 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020048}
49
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020050static struct trace_array global_trace;
51
52static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
53
54static struct trace_array max_tr;
55
56static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
57
Steven Rostedt26994ea2008-05-12 21:20:48 +020058static int tracer_enabled = 1;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020059static unsigned long trace_nr_entries = 16384UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020060
61static struct tracer *trace_types __read_mostly;
62static struct tracer *current_trace __read_mostly;
63static int max_tracer_type_len;
64
65static DEFINE_MUTEX(trace_types_lock);
66
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020067#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
68
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020069static int __init set_nr_entries(char *str)
70{
71 if (!str)
72 return 0;
73 trace_nr_entries = simple_strtoul(str, &str, 0);
74 return 1;
75}
76__setup("trace_entries=", set_nr_entries);
77
Steven Rostedt57f50be2008-05-12 21:20:44 +020078unsigned long nsecs_to_usecs(unsigned long nsecs)
79{
80 return nsecs / 1000;
81}
82
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020083enum trace_type {
84 __TRACE_FIRST_TYPE = 0,
85
86 TRACE_FN,
87 TRACE_CTX,
Ingo Molnarf0a920d2008-05-12 21:20:47 +020088 TRACE_SPECIAL,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020089
90 __TRACE_LAST_TYPE
91};
92
93enum trace_flag_type {
94 TRACE_FLAG_IRQS_OFF = 0x01,
95 TRACE_FLAG_NEED_RESCHED = 0x02,
96 TRACE_FLAG_HARDIRQ = 0x04,
97 TRACE_FLAG_SOFTIRQ = 0x08,
98};
99
100enum trace_iterator_flags {
101 TRACE_ITER_PRINT_PARENT = 0x01,
102 TRACE_ITER_SYM_OFFSET = 0x02,
103 TRACE_ITER_SYM_ADDR = 0x04,
104 TRACE_ITER_VERBOSE = 0x08,
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200105 TRACE_ITER_RAW = 0x10,
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200106 TRACE_ITER_BIN = 0x20,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200107};
108
109#define TRACE_ITER_SYM_MASK \
110 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
111
112/* These must match the bit postions above */
113static const char *trace_options[] = {
114 "print-parent",
115 "sym-offset",
116 "sym-addr",
117 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200118 "raw",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200119 "bin",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200120 NULL
121};
122
123static unsigned trace_flags;
124
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200125static DEFINE_SPINLOCK(ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200126
127/*
128 * Copy the new maximum trace into the separate maximum-trace
129 * structure. (this way the maximum trace is permanently saved,
130 * for later retrieval via /debugfs/tracing/latency_trace)
131 */
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200132static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200133__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
134{
135 struct trace_array_cpu *data = tr->data[cpu];
136
137 max_tr.cpu = cpu;
138 max_tr.time_start = data->preempt_timestamp;
139
140 data = max_tr.data[cpu];
141 data->saved_latency = tracing_max_latency;
142
143 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
144 data->pid = tsk->pid;
145 data->uid = tsk->uid;
146 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
147 data->policy = tsk->policy;
148 data->rt_priority = tsk->rt_priority;
149
150 /* record this tasks comm */
151 tracing_record_cmdline(current);
152}
153
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200154void check_pages(struct trace_array_cpu *data)
155{
156 struct page *page, *tmp;
157
158 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
159 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
160
161 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
162 BUG_ON(page->lru.next->prev != &page->lru);
163 BUG_ON(page->lru.prev->next != &page->lru);
164 }
165}
166
167void *head_page(struct trace_array_cpu *data)
168{
169 struct page *page;
170
171 check_pages(data);
172 if (list_empty(&data->trace_pages))
173 return NULL;
174
175 page = list_entry(data->trace_pages.next, struct page, lru);
176 BUG_ON(&page->lru == &data->trace_pages);
177
178 return page_address(page);
179}
180
Steven Rostedt214023c2008-05-12 21:20:46 +0200181static notrace int
182trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
183{
184 int len = (PAGE_SIZE - 1) - s->len;
185 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200186 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200187
188 if (!len)
189 return 0;
190
191 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200192 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200193 va_end(ap);
194
Steven Rostedtb3806b42008-05-12 21:20:46 +0200195 /* If we can't write it all, don't bother writing anything */
196 if (ret > len)
197 return 0;
198
199 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200200
201 return len;
202}
203
204static notrace int
205trace_seq_puts(struct trace_seq *s, const char *str)
206{
207 int len = strlen(str);
208
209 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200210 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200211
212 memcpy(s->buffer + s->len, str, len);
213 s->len += len;
214
215 return len;
216}
217
218static notrace int
219trace_seq_putc(struct trace_seq *s, unsigned char c)
220{
221 if (s->len >= (PAGE_SIZE - 1))
222 return 0;
223
224 s->buffer[s->len++] = c;
225
226 return 1;
227}
228
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200229static notrace int
230trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
231{
232 if (len > ((PAGE_SIZE - 1) - s->len))
233 return 0;
234
235 memcpy(s->buffer + s->len, mem, len);
236 s->len += len;
237
238 return len;
239}
240
Steven Rostedt214023c2008-05-12 21:20:46 +0200241static notrace void
242trace_seq_reset(struct trace_seq *s)
243{
244 s->len = 0;
245}
246
247static notrace void
248trace_print_seq(struct seq_file *m, struct trace_seq *s)
249{
250 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
251
252 s->buffer[len] = 0;
253 seq_puts(m, s->buffer);
254
255 trace_seq_reset(s);
256}
257
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200258notrace static void
259flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
260{
261 struct list_head flip_pages;
262
263 INIT_LIST_HEAD(&flip_pages);
264
Steven Rostedt93a588f2008-05-12 21:20:45 +0200265 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200266 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200267 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200268
269 check_pages(tr1);
270 check_pages(tr2);
271 list_splice_init(&tr1->trace_pages, &flip_pages);
272 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
273 list_splice_init(&flip_pages, &tr2->trace_pages);
274 BUG_ON(!list_empty(&flip_pages));
275 check_pages(tr1);
276 check_pages(tr2);
277}
278
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200279notrace void
280update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
281{
282 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200283 int i;
284
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200285 WARN_ON_ONCE(!irqs_disabled());
286 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200287 /* clear out all the previous traces */
288 for_each_possible_cpu(i) {
289 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200290 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200291 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200292 }
293
294 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200295 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200296}
297
298/**
299 * update_max_tr_single - only copy one trace over, and reset the rest
300 * @tr - tracer
301 * @tsk - task with the latency
302 * @cpu - the cpu of the buffer to copy.
303 */
304notrace void
305update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
306{
307 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200308 int i;
309
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200310 WARN_ON_ONCE(!irqs_disabled());
311 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200312 for_each_possible_cpu(i)
313 tracing_reset(max_tr.data[i]);
314
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200315 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200316 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200317
318 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200319 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200320}
321
322int register_tracer(struct tracer *type)
323{
324 struct tracer *t;
325 int len;
326 int ret = 0;
327
328 if (!type->name) {
329 pr_info("Tracer must have a name\n");
330 return -1;
331 }
332
333 mutex_lock(&trace_types_lock);
334 for (t = trace_types; t; t = t->next) {
335 if (strcmp(type->name, t->name) == 0) {
336 /* already found */
337 pr_info("Trace %s already registered\n",
338 type->name);
339 ret = -1;
340 goto out;
341 }
342 }
343
Steven Rostedt60a11772008-05-12 21:20:44 +0200344#ifdef CONFIG_FTRACE_STARTUP_TEST
345 if (type->selftest) {
346 struct tracer *saved_tracer = current_trace;
347 struct trace_array_cpu *data;
348 struct trace_array *tr = &global_trace;
349 int saved_ctrl = tr->ctrl;
350 int i;
351 /*
352 * Run a selftest on this tracer.
353 * Here we reset the trace buffer, and set the current
354 * tracer to be this tracer. The tracer can then run some
355 * internal tracing to verify that everything is in order.
356 * If we fail, we do not register this tracer.
357 */
358 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200359 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200360 if (!head_page(data))
361 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200362 tracing_reset(data);
363 }
364 current_trace = type;
365 tr->ctrl = 0;
366 /* the test is responsible for initializing and enabling */
367 pr_info("Testing tracer %s: ", type->name);
368 ret = type->selftest(type, tr);
369 /* the test is responsible for resetting too */
370 current_trace = saved_tracer;
371 tr->ctrl = saved_ctrl;
372 if (ret) {
373 printk(KERN_CONT "FAILED!\n");
374 goto out;
375 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200376 /* Only reset on passing, to avoid touching corrupted buffers */
377 for_each_possible_cpu(i) {
378 data = tr->data[i];
379 if (!head_page(data))
380 continue;
381 tracing_reset(data);
382 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200383 printk(KERN_CONT "PASSED\n");
384 }
385#endif
386
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200387 type->next = trace_types;
388 trace_types = type;
389 len = strlen(type->name);
390 if (len > max_tracer_type_len)
391 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200392
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200393 out:
394 mutex_unlock(&trace_types_lock);
395
396 return ret;
397}
398
399void unregister_tracer(struct tracer *type)
400{
401 struct tracer **t;
402 int len;
403
404 mutex_lock(&trace_types_lock);
405 for (t = &trace_types; *t; t = &(*t)->next) {
406 if (*t == type)
407 goto found;
408 }
409 pr_info("Trace %s not registered\n", type->name);
410 goto out;
411
412 found:
413 *t = (*t)->next;
414 if (strlen(type->name) != max_tracer_type_len)
415 goto out;
416
417 max_tracer_type_len = 0;
418 for (t = &trace_types; *t; t = &(*t)->next) {
419 len = strlen((*t)->name);
420 if (len > max_tracer_type_len)
421 max_tracer_type_len = len;
422 }
423 out:
424 mutex_unlock(&trace_types_lock);
425}
426
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200427notrace void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200428{
429 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200430 data->trace_head = data->trace_tail = head_page(data);
431 data->trace_head_idx = 0;
432 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200433}
434
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200435#define SAVED_CMDLINES 128
436static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
437static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
438static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
439static int cmdline_idx;
440static DEFINE_SPINLOCK(trace_cmdline_lock);
441atomic_t trace_record_cmdline_disabled;
442
443static void trace_init_cmdlines(void)
444{
445 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
446 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
447 cmdline_idx = 0;
448}
449
450notrace void trace_stop_cmdline_recording(void);
451
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200452static notrace void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200453{
454 unsigned map;
455 unsigned idx;
456
457 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
458 return;
459
460 /*
461 * It's not the end of the world if we don't get
462 * the lock, but we also don't want to spin
463 * nor do we want to disable interrupts,
464 * so if we miss here, then better luck next time.
465 */
466 if (!spin_trylock(&trace_cmdline_lock))
467 return;
468
469 idx = map_pid_to_cmdline[tsk->pid];
470 if (idx >= SAVED_CMDLINES) {
471 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
472
473 map = map_cmdline_to_pid[idx];
474 if (map <= PID_MAX_DEFAULT)
475 map_pid_to_cmdline[map] = (unsigned)-1;
476
477 map_pid_to_cmdline[tsk->pid] = idx;
478
479 cmdline_idx = idx;
480 }
481
482 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
483
484 spin_unlock(&trace_cmdline_lock);
485}
486
487static notrace char *trace_find_cmdline(int pid)
488{
489 char *cmdline = "<...>";
490 unsigned map;
491
492 if (!pid)
493 return "<idle>";
494
495 if (pid > PID_MAX_DEFAULT)
496 goto out;
497
498 map = map_pid_to_cmdline[pid];
499 if (map >= SAVED_CMDLINES)
500 goto out;
501
502 cmdline = saved_cmdlines[map];
503
504 out:
505 return cmdline;
506}
507
508notrace void tracing_record_cmdline(struct task_struct *tsk)
509{
510 if (atomic_read(&trace_record_cmdline_disabled))
511 return;
512
513 trace_save_cmdline(tsk);
514}
515
Steven Rostedt93a588f2008-05-12 21:20:45 +0200516static inline notrace struct list_head *
517trace_next_list(struct trace_array_cpu *data, struct list_head *next)
518{
519 /*
520 * Roundrobin - but skip the head (which is not a real page):
521 */
522 next = next->next;
523 if (unlikely(next == &data->trace_pages))
524 next = next->next;
525 BUG_ON(next == &data->trace_pages);
526
527 return next;
528}
529
530static inline notrace void *
531trace_next_page(struct trace_array_cpu *data, void *addr)
532{
533 struct list_head *next;
534 struct page *page;
535
536 page = virt_to_page(addr);
537
538 next = trace_next_list(data, &page->lru);
539 page = list_entry(next, struct page, lru);
540
541 return page_address(page);
542}
543
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200544static inline notrace struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200545tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200546{
547 unsigned long idx, idx_next;
548 struct trace_entry *entry;
549
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200550 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200551 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200552 idx_next = idx + 1;
553
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200554 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
555
Steven Rostedt93a588f2008-05-12 21:20:45 +0200556 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200557
558 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200559 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200560 idx_next = 0;
561 }
562
Steven Rostedt93a588f2008-05-12 21:20:45 +0200563 if (data->trace_head == data->trace_tail &&
564 idx_next == data->trace_tail_idx) {
565 /* overrun */
566 data->trace_tail_idx++;
567 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
568 data->trace_tail =
569 trace_next_page(data, data->trace_tail);
570 data->trace_tail_idx = 0;
571 }
572 }
573
574 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200575
576 return entry;
577}
578
579static inline notrace void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200580tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200581{
582 struct task_struct *tsk = current;
583 unsigned long pc;
584
585 pc = preempt_count();
586
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200587 entry->preempt_count = pc & 0xff;
588 entry->pid = tsk->pid;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200589 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200590 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
591 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
592 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
593 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
594}
595
596notrace void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200597__ftrace(struct trace_array *tr, struct trace_array_cpu *data,
598 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200599{
600 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200601 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200602
Ingo Molnardcb63082008-05-12 21:20:48 +0200603 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200604 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200605 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200606 entry->type = TRACE_FN;
607 entry->fn.ip = ip;
608 entry->fn.parent_ip = parent_ip;
Ingo Molnardcb63082008-05-12 21:20:48 +0200609 spin_unlock_irqrestore(&data->lock, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200610}
611
612notrace void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200613ftrace(struct trace_array *tr, struct trace_array_cpu *data,
614 unsigned long ip, unsigned long parent_ip, unsigned long flags)
615{
616 if (likely(!atomic_read(&data->disabled)))
617 __ftrace(tr, data, ip, parent_ip, flags);
618}
619
620notrace void
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200621trace_special(struct trace_array *tr, struct trace_array_cpu *data,
622 unsigned long arg1, unsigned long arg2, unsigned long arg3)
623{
624 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200625 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200626
Ingo Molnardcb63082008-05-12 21:20:48 +0200627 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200628 entry = tracing_get_trace_entry(tr, data);
629 tracing_generic_entry_update(entry, 0);
630 entry->type = TRACE_SPECIAL;
631 entry->special.arg1 = arg1;
632 entry->special.arg2 = arg2;
633 entry->special.arg3 = arg3;
Ingo Molnardcb63082008-05-12 21:20:48 +0200634 spin_unlock_irqrestore(&data->lock, irq_flags);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200635}
636
637notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200638tracing_sched_switch_trace(struct trace_array *tr,
639 struct trace_array_cpu *data,
640 struct task_struct *prev, struct task_struct *next,
641 unsigned long flags)
642{
643 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200644 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200645
Ingo Molnardcb63082008-05-12 21:20:48 +0200646 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200647 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200648 tracing_generic_entry_update(entry, flags);
649 entry->type = TRACE_CTX;
650 entry->ctx.prev_pid = prev->pid;
651 entry->ctx.prev_prio = prev->prio;
652 entry->ctx.prev_state = prev->state;
653 entry->ctx.next_pid = next->pid;
654 entry->ctx.next_prio = next->prio;
Ingo Molnardcb63082008-05-12 21:20:48 +0200655 spin_unlock_irqrestore(&data->lock, irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200656}
657
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200658#ifdef CONFIG_FTRACE
659static notrace void
660function_trace_call(unsigned long ip, unsigned long parent_ip)
661{
662 struct trace_array *tr = &global_trace;
663 struct trace_array_cpu *data;
664 unsigned long flags;
665 long disabled;
666 int cpu;
667
668 if (unlikely(!tracer_enabled))
669 return;
670
671 local_irq_save(flags);
672 cpu = raw_smp_processor_id();
673 data = tr->data[cpu];
674 disabled = atomic_inc_return(&data->disabled);
675
676 if (likely(disabled == 1))
677 __ftrace(tr, data, ip, parent_ip, flags);
678
679 atomic_dec(&data->disabled);
680 local_irq_restore(flags);
681}
682
683static struct ftrace_ops trace_ops __read_mostly =
684{
685 .func = function_trace_call,
686};
687
688notrace void tracing_start_function_trace(void)
689{
690 register_ftrace_function(&trace_ops);
691}
692
693notrace void tracing_stop_function_trace(void)
694{
695 unregister_ftrace_function(&trace_ops);
696}
697#endif
698
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200699enum trace_file_type {
700 TRACE_FILE_LAT_FMT = 1,
701};
702
703static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200704trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
705 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200706{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200707 struct page *page;
708 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200709
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200710 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200711 iter->next_idx[cpu] >= data->trace_idx ||
712 (data->trace_head == data->trace_tail &&
713 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200714 return NULL;
715
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200716 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200717 /* Initialize the iterator for this cpu trace buffer */
718 WARN_ON(!data->trace_tail);
719 page = virt_to_page(data->trace_tail);
720 iter->next_page[cpu] = &page->lru;
721 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200722 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200723
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200724 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200725 BUG_ON(&data->trace_pages == &page->lru);
726
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200727 array = page_address(page);
728
Steven Rostedt93a588f2008-05-12 21:20:45 +0200729 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200730 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200731}
732
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200733static struct trace_entry * notrace
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200734find_next_entry(struct trace_iterator *iter, int *ent_cpu)
735{
736 struct trace_array *tr = iter->tr;
737 struct trace_entry *ent, *next = NULL;
738 int next_cpu = -1;
739 int cpu;
740
741 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200742 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200743 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200744 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200745 /*
746 * Pick the entry with the smallest timestamp:
747 */
748 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200749 next = ent;
750 next_cpu = cpu;
751 }
752 }
753
754 if (ent_cpu)
755 *ent_cpu = next_cpu;
756
757 return next;
758}
759
Ingo Molnar8c523a92008-05-12 21:20:46 +0200760static notrace void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200761{
762 iter->idx++;
763 iter->next_idx[iter->cpu]++;
764 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200765
Steven Rostedtb3806b42008-05-12 21:20:46 +0200766 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
767 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
768
769 iter->next_page_idx[iter->cpu] = 0;
770 iter->next_page[iter->cpu] =
771 trace_next_list(data, iter->next_page[iter->cpu]);
772 }
773}
774
Ingo Molnar8c523a92008-05-12 21:20:46 +0200775static notrace void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200776{
777 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
778
779 data->trace_tail_idx++;
780 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
781 data->trace_tail = trace_next_page(data, data->trace_tail);
782 data->trace_tail_idx = 0;
783 }
784
785 /* Check if we empty it, then reset the index */
786 if (data->trace_head == data->trace_tail &&
787 data->trace_head_idx == data->trace_tail_idx)
788 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200789}
790
Ingo Molnar8c523a92008-05-12 21:20:46 +0200791static notrace void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200792{
793 struct trace_entry *next;
794 int next_cpu = -1;
795
796 next = find_next_entry(iter, &next_cpu);
797
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200798 iter->prev_ent = iter->ent;
799 iter->prev_cpu = iter->cpu;
800
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200801 iter->ent = next;
802 iter->cpu = next_cpu;
803
Steven Rostedtb3806b42008-05-12 21:20:46 +0200804 if (next)
805 trace_iterator_increment(iter);
806
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200807 return next ? iter : NULL;
808}
809
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200810static notrace void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200811{
812 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200813 void *last_ent = iter->ent;
814 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200815 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200816
817 (*pos)++;
818
819 /* can't go backwards */
820 if (iter->idx > i)
821 return NULL;
822
823 if (iter->idx < 0)
824 ent = find_next_entry_inc(iter);
825 else
826 ent = iter;
827
828 while (ent && iter->idx < i)
829 ent = find_next_entry_inc(iter);
830
831 iter->pos = *pos;
832
833 if (last_ent && !ent)
834 seq_puts(m, "\n\nvim:ft=help\n");
835
836 return ent;
837}
838
839static void *s_start(struct seq_file *m, loff_t *pos)
840{
841 struct trace_iterator *iter = m->private;
842 void *p = NULL;
843 loff_t l = 0;
844 int i;
845
846 mutex_lock(&trace_types_lock);
847
848 if (!current_trace || current_trace != iter->trace)
849 return NULL;
850
851 atomic_inc(&trace_record_cmdline_disabled);
852
853 /* let the tracer grab locks here if needed */
854 if (current_trace->start)
855 current_trace->start(iter);
856
857 if (*pos != iter->pos) {
858 iter->ent = NULL;
859 iter->cpu = 0;
860 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200861 iter->prev_ent = NULL;
862 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200863
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200864 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200865 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200866 iter->next_page[i] = NULL;
867 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200868
869 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
870 ;
871
872 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200873 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200874 p = s_next(m, p, &l);
875 }
876
877 return p;
878}
879
880static void s_stop(struct seq_file *m, void *p)
881{
882 struct trace_iterator *iter = m->private;
883
884 atomic_dec(&trace_record_cmdline_disabled);
885
886 /* let the tracer release locks here if needed */
887 if (current_trace && current_trace == iter->trace && iter->trace->stop)
888 iter->trace->stop(iter);
889
890 mutex_unlock(&trace_types_lock);
891}
892
Steven Rostedtb3806b42008-05-12 21:20:46 +0200893static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200894seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200895{
896#ifdef CONFIG_KALLSYMS
897 char str[KSYM_SYMBOL_LEN];
898
899 kallsyms_lookup(address, NULL, NULL, NULL, str);
900
Steven Rostedtb3806b42008-05-12 21:20:46 +0200901 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200902#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200903 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200904}
905
Steven Rostedtb3806b42008-05-12 21:20:46 +0200906static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200907seq_print_sym_offset(struct trace_seq *s, const char *fmt,
908 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200909{
910#ifdef CONFIG_KALLSYMS
911 char str[KSYM_SYMBOL_LEN];
912
913 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200914 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200915#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200916 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200917}
918
919#ifndef CONFIG_64BIT
920# define IP_FMT "%08lx"
921#else
922# define IP_FMT "%016lx"
923#endif
924
Steven Rostedtb3806b42008-05-12 21:20:46 +0200925static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +0200926seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200927{
Steven Rostedtb3806b42008-05-12 21:20:46 +0200928 int ret;
929
930 if (!ip)
931 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200932
933 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200934 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200935 else
Steven Rostedtb3806b42008-05-12 21:20:46 +0200936 ret = seq_print_sym_short(s, "%s", ip);
937
938 if (!ret)
939 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200940
941 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200942 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
943 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200944}
945
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200946static notrace void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200947{
948 seq_puts(m, "# _------=> CPU# \n");
949 seq_puts(m, "# / _-----=> irqs-off \n");
950 seq_puts(m, "# | / _----=> need-resched \n");
951 seq_puts(m, "# || / _---=> hardirq/softirq \n");
952 seq_puts(m, "# ||| / _--=> preempt-depth \n");
953 seq_puts(m, "# |||| / \n");
954 seq_puts(m, "# ||||| delay \n");
955 seq_puts(m, "# cmd pid ||||| time | caller \n");
956 seq_puts(m, "# \\ / ||||| \\ | / \n");
957}
958
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200959static notrace void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200960{
961 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
962 seq_puts(m, "# | | | | |\n");
963}
964
965
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200966static notrace void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200967print_trace_header(struct seq_file *m, struct trace_iterator *iter)
968{
969 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
970 struct trace_array *tr = iter->tr;
971 struct trace_array_cpu *data = tr->data[tr->cpu];
972 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200973 unsigned long total = 0;
974 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200975 int cpu;
976 const char *name = "preemption";
977
978 if (type)
979 name = type->name;
980
981 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200982 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200983 total += tr->data[cpu]->trace_idx;
984 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200985 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200986 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200987 entries += tr->data[cpu]->trace_idx;
988 }
989 }
990
991 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
992 name, UTS_RELEASE);
993 seq_puts(m, "-----------------------------------"
994 "---------------------------------\n");
995 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
996 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +0200997 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200998 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200999 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001000 tr->cpu,
1001#if defined(CONFIG_PREEMPT_NONE)
1002 "server",
1003#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1004 "desktop",
1005#elif defined(CONFIG_PREEMPT_DESKTOP)
1006 "preempt",
1007#else
1008 "unknown",
1009#endif
1010 /* These are reserved for later use */
1011 0, 0, 0, 0);
1012#ifdef CONFIG_SMP
1013 seq_printf(m, " #P:%d)\n", num_online_cpus());
1014#else
1015 seq_puts(m, ")\n");
1016#endif
1017 seq_puts(m, " -----------------\n");
1018 seq_printf(m, " | task: %.16s-%d "
1019 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1020 data->comm, data->pid, data->uid, data->nice,
1021 data->policy, data->rt_priority);
1022 seq_puts(m, " -----------------\n");
1023
1024 if (data->critical_start) {
1025 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001026 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1027 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001028 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001029 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1030 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001031 seq_puts(m, "\n");
1032 }
1033
1034 seq_puts(m, "\n");
1035}
1036
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001037static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001038lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001039{
1040 int hardirq, softirq;
1041 char *comm;
1042
1043 comm = trace_find_cmdline(entry->pid);
1044
Steven Rostedt214023c2008-05-12 21:20:46 +02001045 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1046 trace_seq_printf(s, "%d", cpu);
1047 trace_seq_printf(s, "%c%c",
1048 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1049 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001050
1051 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1052 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1053 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001054 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001055 else {
1056 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001057 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001058 else {
1059 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001060 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001061 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001062 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001063 }
1064 }
1065
1066 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001067 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001069 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001070}
1071
1072unsigned long preempt_mark_thresh = 100;
1073
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001074static notrace void
Steven Rostedt214023c2008-05-12 21:20:46 +02001075lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001076 unsigned long rel_usecs)
1077{
Steven Rostedt214023c2008-05-12 21:20:46 +02001078 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001079 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001080 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001081 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001082 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001083 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001084 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001085}
1086
1087static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1088
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001089static notrace int
Steven Rostedt214023c2008-05-12 21:20:46 +02001090print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001091{
Steven Rostedt214023c2008-05-12 21:20:46 +02001092 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001093 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1094 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1095 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1096 struct trace_entry *entry = iter->ent;
1097 unsigned long abs_usecs;
1098 unsigned long rel_usecs;
1099 char *comm;
1100 int S;
1101
1102 if (!next_entry)
1103 next_entry = entry;
1104 rel_usecs = ns2usecs(next_entry->t - entry->t);
1105 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1106
1107 if (verbose) {
1108 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001109 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1110 " %ld.%03ldms (+%ld.%03ldms): ",
1111 comm,
1112 entry->pid, cpu, entry->flags,
1113 entry->preempt_count, trace_idx,
1114 ns2usecs(entry->t),
1115 abs_usecs/1000,
1116 abs_usecs % 1000, rel_usecs/1000,
1117 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001118 } else {
Steven Rostedt214023c2008-05-12 21:20:46 +02001119 lat_print_generic(s, entry, cpu);
1120 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001121 }
1122 switch (entry->type) {
1123 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001124 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1125 trace_seq_puts(s, " (");
1126 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1127 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001128 break;
1129 case TRACE_CTX:
1130 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1131 state_to_char[entry->ctx.prev_state] : 'X';
1132 comm = trace_find_cmdline(entry->ctx.next_pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001133 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1134 entry->ctx.prev_pid,
1135 entry->ctx.prev_prio,
1136 S,
1137 entry->ctx.next_pid,
1138 entry->ctx.next_prio,
1139 comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001140 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001141 case TRACE_SPECIAL:
1142 trace_seq_printf(s, " %lx %lx %lx\n",
1143 entry->special.arg1,
1144 entry->special.arg2,
1145 entry->special.arg3);
1146 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001147 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001148 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001149 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001150 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001151}
1152
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001153static notrace int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001154{
Steven Rostedt214023c2008-05-12 21:20:46 +02001155 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001156 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001157 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001158 unsigned long usec_rem;
1159 unsigned long long t;
1160 unsigned long secs;
1161 char *comm;
1162 int S;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001163 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001164
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001165 entry = iter->ent;
1166
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167 comm = trace_find_cmdline(iter->ent->pid);
1168
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001169 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001170 usec_rem = do_div(t, 1000000ULL);
1171 secs = (unsigned long)t;
1172
Steven Rostedtb3806b42008-05-12 21:20:46 +02001173 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1174 if (!ret)
1175 return 0;
1176 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1177 if (!ret)
1178 return 0;
1179 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1180 if (!ret)
1181 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001182
1183 switch (entry->type) {
1184 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001185 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1186 if (!ret)
1187 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001188 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1189 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001190 ret = trace_seq_printf(s, " <-");
1191 if (!ret)
1192 return 0;
1193 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1194 sym_flags);
1195 if (!ret)
1196 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001198 ret = trace_seq_printf(s, "\n");
1199 if (!ret)
1200 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001201 break;
1202 case TRACE_CTX:
1203 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1204 state_to_char[entry->ctx.prev_state] : 'X';
Steven Rostedtb3806b42008-05-12 21:20:46 +02001205 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1206 entry->ctx.prev_pid,
1207 entry->ctx.prev_prio,
1208 S,
1209 entry->ctx.next_pid,
1210 entry->ctx.next_prio);
1211 if (!ret)
1212 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001213 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001214 case TRACE_SPECIAL:
1215 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1216 entry->special.arg1,
1217 entry->special.arg2,
1218 entry->special.arg3);
1219 if (!ret)
1220 return 0;
1221 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001222 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001223 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001224}
1225
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001226static notrace int print_raw_fmt(struct trace_iterator *iter)
1227{
1228 struct trace_seq *s = &iter->seq;
1229 struct trace_entry *entry;
1230 int ret;
1231 int S;
1232
1233 entry = iter->ent;
1234
1235 ret = trace_seq_printf(s, "%d %d %llu ",
1236 entry->pid, iter->cpu, entry->t);
1237 if (!ret)
1238 return 0;
1239
1240 switch (entry->type) {
1241 case TRACE_FN:
1242 ret = trace_seq_printf(s, "%x %x\n",
1243 entry->fn.ip, entry->fn.parent_ip);
1244 if (!ret)
1245 return 0;
1246 break;
1247 case TRACE_CTX:
1248 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1249 state_to_char[entry->ctx.prev_state] : 'X';
1250 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1251 entry->ctx.prev_pid,
1252 entry->ctx.prev_prio,
1253 S,
1254 entry->ctx.next_pid,
1255 entry->ctx.next_prio);
1256 if (!ret)
1257 return 0;
1258 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001259 case TRACE_SPECIAL:
1260 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1261 entry->special.arg1,
1262 entry->special.arg2,
1263 entry->special.arg3);
1264 if (!ret)
1265 return 0;
1266 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001267 }
1268 return 1;
1269}
1270
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001271#define SEQ_PUT_FIELD_RET(s, x) \
1272do { \
1273 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1274 return 0; \
1275} while (0)
1276
1277static notrace int print_bin_fmt(struct trace_iterator *iter)
1278{
1279 struct trace_seq *s = &iter->seq;
1280 struct trace_entry *entry;
1281
1282 entry = iter->ent;
1283
1284 SEQ_PUT_FIELD_RET(s, entry->pid);
1285 SEQ_PUT_FIELD_RET(s, entry->cpu);
1286 SEQ_PUT_FIELD_RET(s, entry->t);
1287
1288 switch (entry->type) {
1289 case TRACE_FN:
1290 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1291 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1292 break;
1293 case TRACE_CTX:
1294 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1295 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1296 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1297 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1298 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1299 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001300 case TRACE_SPECIAL:
1301 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1302 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1303 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1304 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001305 }
1306 return 1;
1307}
1308
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001309static int trace_empty(struct trace_iterator *iter)
1310{
1311 struct trace_array_cpu *data;
1312 int cpu;
1313
1314 for_each_possible_cpu(cpu) {
1315 data = iter->tr->data[cpu];
1316
Steven Rostedtb3806b42008-05-12 21:20:46 +02001317 if (head_page(data) && data->trace_idx &&
1318 (data->trace_tail != data->trace_head ||
1319 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001320 return 0;
1321 }
1322 return 1;
1323}
1324
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001325static int print_trace_line(struct trace_iterator *iter)
1326{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001327 if (trace_flags & TRACE_ITER_BIN)
1328 return print_bin_fmt(iter);
1329
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001330 if (trace_flags & TRACE_ITER_RAW)
1331 return print_raw_fmt(iter);
1332
1333 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1334 return print_lat_fmt(iter, iter->idx, iter->cpu);
1335
1336 return print_trace_fmt(iter);
1337}
1338
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001339static int s_show(struct seq_file *m, void *v)
1340{
1341 struct trace_iterator *iter = v;
1342
1343 if (iter->ent == NULL) {
1344 if (iter->tr) {
1345 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1346 seq_puts(m, "#\n");
1347 }
1348 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1349 /* print nothing if the buffers are empty */
1350 if (trace_empty(iter))
1351 return 0;
1352 print_trace_header(m, iter);
1353 if (!(trace_flags & TRACE_ITER_VERBOSE))
1354 print_lat_help_header(m);
1355 } else {
1356 if (!(trace_flags & TRACE_ITER_VERBOSE))
1357 print_func_help_header(m);
1358 }
1359 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001360 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001361 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001362 }
1363
1364 return 0;
1365}
1366
1367static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001368 .start = s_start,
1369 .next = s_next,
1370 .stop = s_stop,
1371 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001372};
1373
1374static struct trace_iterator notrace *
1375__tracing_open(struct inode *inode, struct file *file, int *ret)
1376{
1377 struct trace_iterator *iter;
1378
Steven Rostedt60a11772008-05-12 21:20:44 +02001379 if (tracing_disabled) {
1380 *ret = -ENODEV;
1381 return NULL;
1382 }
1383
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001384 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1385 if (!iter) {
1386 *ret = -ENOMEM;
1387 goto out;
1388 }
1389
1390 mutex_lock(&trace_types_lock);
1391 if (current_trace && current_trace->print_max)
1392 iter->tr = &max_tr;
1393 else
1394 iter->tr = inode->i_private;
1395 iter->trace = current_trace;
1396 iter->pos = -1;
1397
1398 /* TODO stop tracer */
1399 *ret = seq_open(file, &tracer_seq_ops);
1400 if (!*ret) {
1401 struct seq_file *m = file->private_data;
1402 m->private = iter;
1403
1404 /* stop the trace while dumping */
1405 if (iter->tr->ctrl)
1406 tracer_enabled = 0;
1407
1408 if (iter->trace && iter->trace->open)
1409 iter->trace->open(iter);
1410 } else {
1411 kfree(iter);
1412 iter = NULL;
1413 }
1414 mutex_unlock(&trace_types_lock);
1415
1416 out:
1417 return iter;
1418}
1419
1420int tracing_open_generic(struct inode *inode, struct file *filp)
1421{
Steven Rostedt60a11772008-05-12 21:20:44 +02001422 if (tracing_disabled)
1423 return -ENODEV;
1424
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001425 filp->private_data = inode->i_private;
1426 return 0;
1427}
1428
1429int tracing_release(struct inode *inode, struct file *file)
1430{
1431 struct seq_file *m = (struct seq_file *)file->private_data;
1432 struct trace_iterator *iter = m->private;
1433
1434 mutex_lock(&trace_types_lock);
1435 if (iter->trace && iter->trace->close)
1436 iter->trace->close(iter);
1437
1438 /* reenable tracing if it was previously enabled */
1439 if (iter->tr->ctrl)
1440 tracer_enabled = 1;
1441 mutex_unlock(&trace_types_lock);
1442
1443 seq_release(inode, file);
1444 kfree(iter);
1445 return 0;
1446}
1447
1448static int tracing_open(struct inode *inode, struct file *file)
1449{
1450 int ret;
1451
1452 __tracing_open(inode, file, &ret);
1453
1454 return ret;
1455}
1456
1457static int tracing_lt_open(struct inode *inode, struct file *file)
1458{
1459 struct trace_iterator *iter;
1460 int ret;
1461
1462 iter = __tracing_open(inode, file, &ret);
1463
1464 if (!ret)
1465 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1466
1467 return ret;
1468}
1469
1470
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001471static notrace void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001472t_next(struct seq_file *m, void *v, loff_t *pos)
1473{
1474 struct tracer *t = m->private;
1475
1476 (*pos)++;
1477
1478 if (t)
1479 t = t->next;
1480
1481 m->private = t;
1482
1483 return t;
1484}
1485
1486static void *t_start(struct seq_file *m, loff_t *pos)
1487{
1488 struct tracer *t = m->private;
1489 loff_t l = 0;
1490
1491 mutex_lock(&trace_types_lock);
1492 for (; t && l < *pos; t = t_next(m, t, &l))
1493 ;
1494
1495 return t;
1496}
1497
1498static void t_stop(struct seq_file *m, void *p)
1499{
1500 mutex_unlock(&trace_types_lock);
1501}
1502
1503static int t_show(struct seq_file *m, void *v)
1504{
1505 struct tracer *t = v;
1506
1507 if (!t)
1508 return 0;
1509
1510 seq_printf(m, "%s", t->name);
1511 if (t->next)
1512 seq_putc(m, ' ');
1513 else
1514 seq_putc(m, '\n');
1515
1516 return 0;
1517}
1518
1519static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001520 .start = t_start,
1521 .next = t_next,
1522 .stop = t_stop,
1523 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001524};
1525
1526static int show_traces_open(struct inode *inode, struct file *file)
1527{
1528 int ret;
1529
Steven Rostedt60a11772008-05-12 21:20:44 +02001530 if (tracing_disabled)
1531 return -ENODEV;
1532
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001533 ret = seq_open(file, &show_traces_seq_ops);
1534 if (!ret) {
1535 struct seq_file *m = file->private_data;
1536 m->private = trace_types;
1537 }
1538
1539 return ret;
1540}
1541
1542static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001543 .open = tracing_open,
1544 .read = seq_read,
1545 .llseek = seq_lseek,
1546 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001547};
1548
1549static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001550 .open = tracing_lt_open,
1551 .read = seq_read,
1552 .llseek = seq_lseek,
1553 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001554};
1555
1556static struct file_operations show_traces_fops = {
1557 .open = show_traces_open,
1558 .read = seq_read,
1559 .release = seq_release,
1560};
1561
1562static ssize_t
1563tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1564 size_t cnt, loff_t *ppos)
1565{
1566 char *buf;
1567 int r = 0;
1568 int len = 0;
1569 int i;
1570
1571 /* calulate max size */
1572 for (i = 0; trace_options[i]; i++) {
1573 len += strlen(trace_options[i]);
1574 len += 3; /* "no" and space */
1575 }
1576
1577 /* +2 for \n and \0 */
1578 buf = kmalloc(len + 2, GFP_KERNEL);
1579 if (!buf)
1580 return -ENOMEM;
1581
1582 for (i = 0; trace_options[i]; i++) {
1583 if (trace_flags & (1 << i))
1584 r += sprintf(buf + r, "%s ", trace_options[i]);
1585 else
1586 r += sprintf(buf + r, "no%s ", trace_options[i]);
1587 }
1588
1589 r += sprintf(buf + r, "\n");
1590 WARN_ON(r >= len + 2);
1591
1592 r = simple_read_from_buffer(ubuf, cnt, ppos,
1593 buf, r);
1594
1595 kfree(buf);
1596
1597 return r;
1598}
1599
1600static ssize_t
1601tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1602 size_t cnt, loff_t *ppos)
1603{
1604 char buf[64];
1605 char *cmp = buf;
1606 int neg = 0;
1607 int i;
1608
1609 if (cnt > 63)
1610 cnt = 63;
1611
1612 if (copy_from_user(&buf, ubuf, cnt))
1613 return -EFAULT;
1614
1615 buf[cnt] = 0;
1616
1617 if (strncmp(buf, "no", 2) == 0) {
1618 neg = 1;
1619 cmp += 2;
1620 }
1621
1622 for (i = 0; trace_options[i]; i++) {
1623 int len = strlen(trace_options[i]);
1624
1625 if (strncmp(cmp, trace_options[i], len) == 0) {
1626 if (neg)
1627 trace_flags &= ~(1 << i);
1628 else
1629 trace_flags |= (1 << i);
1630 break;
1631 }
1632 }
1633
1634 filp->f_pos += cnt;
1635
1636 return cnt;
1637}
1638
1639static struct file_operations tracing_iter_fops = {
1640 .open = tracing_open_generic,
1641 .read = tracing_iter_ctrl_read,
1642 .write = tracing_iter_ctrl_write,
1643};
1644
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001645static const char readme_msg[] =
1646 "tracing mini-HOWTO:\n\n"
1647 "# mkdir /debug\n"
1648 "# mount -t debugfs nodev /debug\n\n"
1649 "# cat /debug/tracing/available_tracers\n"
1650 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1651 "# cat /debug/tracing/current_tracer\n"
1652 "none\n"
1653 "# echo sched_switch > /debug/tracing/current_tracer\n"
1654 "# cat /debug/tracing/current_tracer\n"
1655 "sched_switch\n"
1656 "# cat /debug/tracing/iter_ctrl\n"
1657 "noprint-parent nosym-offset nosym-addr noverbose\n"
1658 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1659 "# echo 1 > /debug/tracing/tracing_enabled\n"
1660 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1661 "echo 0 > /debug/tracing/tracing_enabled\n"
1662;
1663
1664static ssize_t
1665tracing_readme_read(struct file *filp, char __user *ubuf,
1666 size_t cnt, loff_t *ppos)
1667{
1668 return simple_read_from_buffer(ubuf, cnt, ppos,
1669 readme_msg, strlen(readme_msg));
1670}
1671
1672static struct file_operations tracing_readme_fops = {
1673 .open = tracing_open_generic,
1674 .read = tracing_readme_read,
1675};
1676
1677
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001678static ssize_t
1679tracing_ctrl_read(struct file *filp, char __user *ubuf,
1680 size_t cnt, loff_t *ppos)
1681{
1682 struct trace_array *tr = filp->private_data;
1683 char buf[64];
1684 int r;
1685
1686 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001687 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001688}
1689
1690static ssize_t
1691tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1692 size_t cnt, loff_t *ppos)
1693{
1694 struct trace_array *tr = filp->private_data;
1695 long val;
1696 char buf[64];
1697
1698 if (cnt > 63)
1699 cnt = 63;
1700
1701 if (copy_from_user(&buf, ubuf, cnt))
1702 return -EFAULT;
1703
1704 buf[cnt] = 0;
1705
1706 val = simple_strtoul(buf, NULL, 10);
1707
1708 val = !!val;
1709
1710 mutex_lock(&trace_types_lock);
1711 if (tr->ctrl ^ val) {
1712 if (val)
1713 tracer_enabled = 1;
1714 else
1715 tracer_enabled = 0;
1716
1717 tr->ctrl = val;
1718
1719 if (current_trace && current_trace->ctrl_update)
1720 current_trace->ctrl_update(tr);
1721 }
1722 mutex_unlock(&trace_types_lock);
1723
1724 filp->f_pos += cnt;
1725
1726 return cnt;
1727}
1728
1729static ssize_t
1730tracing_set_trace_read(struct file *filp, char __user *ubuf,
1731 size_t cnt, loff_t *ppos)
1732{
1733 char buf[max_tracer_type_len+2];
1734 int r;
1735
1736 mutex_lock(&trace_types_lock);
1737 if (current_trace)
1738 r = sprintf(buf, "%s\n", current_trace->name);
1739 else
1740 r = sprintf(buf, "\n");
1741 mutex_unlock(&trace_types_lock);
1742
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001743 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001744}
1745
1746static ssize_t
1747tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1748 size_t cnt, loff_t *ppos)
1749{
1750 struct trace_array *tr = &global_trace;
1751 struct tracer *t;
1752 char buf[max_tracer_type_len+1];
1753 int i;
1754
1755 if (cnt > max_tracer_type_len)
1756 cnt = max_tracer_type_len;
1757
1758 if (copy_from_user(&buf, ubuf, cnt))
1759 return -EFAULT;
1760
1761 buf[cnt] = 0;
1762
1763 /* strip ending whitespace. */
1764 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1765 buf[i] = 0;
1766
1767 mutex_lock(&trace_types_lock);
1768 for (t = trace_types; t; t = t->next) {
1769 if (strcmp(t->name, buf) == 0)
1770 break;
1771 }
1772 if (!t || t == current_trace)
1773 goto out;
1774
1775 if (current_trace && current_trace->reset)
1776 current_trace->reset(tr);
1777
1778 current_trace = t;
1779 if (t->init)
1780 t->init(tr);
1781
1782 out:
1783 mutex_unlock(&trace_types_lock);
1784
1785 filp->f_pos += cnt;
1786
1787 return cnt;
1788}
1789
1790static ssize_t
1791tracing_max_lat_read(struct file *filp, char __user *ubuf,
1792 size_t cnt, loff_t *ppos)
1793{
1794 unsigned long *ptr = filp->private_data;
1795 char buf[64];
1796 int r;
1797
1798 r = snprintf(buf, 64, "%ld\n",
1799 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1800 if (r > 64)
1801 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001802 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001803}
1804
1805static ssize_t
1806tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1807 size_t cnt, loff_t *ppos)
1808{
1809 long *ptr = filp->private_data;
1810 long val;
1811 char buf[64];
1812
1813 if (cnt > 63)
1814 cnt = 63;
1815
1816 if (copy_from_user(&buf, ubuf, cnt))
1817 return -EFAULT;
1818
1819 buf[cnt] = 0;
1820
1821 val = simple_strtoul(buf, NULL, 10);
1822
1823 *ptr = val * 1000;
1824
1825 return cnt;
1826}
1827
Steven Rostedtb3806b42008-05-12 21:20:46 +02001828static atomic_t tracing_reader;
1829
1830static int tracing_open_pipe(struct inode *inode, struct file *filp)
1831{
1832 struct trace_iterator *iter;
1833
1834 if (tracing_disabled)
1835 return -ENODEV;
1836
1837 /* We only allow for reader of the pipe */
1838 if (atomic_inc_return(&tracing_reader) != 1) {
1839 atomic_dec(&tracing_reader);
1840 return -EBUSY;
1841 }
1842
1843 /* create a buffer to store the information to pass to userspace */
1844 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1845 if (!iter)
1846 return -ENOMEM;
1847
1848 iter->tr = &global_trace;
1849
1850 filp->private_data = iter;
1851
1852 return 0;
1853}
1854
1855static int tracing_release_pipe(struct inode *inode, struct file *file)
1856{
1857 struct trace_iterator *iter = file->private_data;
1858
1859 kfree(iter);
1860 atomic_dec(&tracing_reader);
1861
1862 return 0;
1863}
1864
1865/*
1866 * Consumer reader.
1867 */
1868static ssize_t
1869tracing_read_pipe(struct file *filp, char __user *ubuf,
1870 size_t cnt, loff_t *ppos)
1871{
1872 struct trace_iterator *iter = filp->private_data;
1873 struct trace_array_cpu *data;
1874 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001875 static int start;
1876 unsigned long flags;
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001877 int ftrace_save;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001878 int read = 0;
1879 int cpu;
1880 int len;
1881 int ret;
1882
1883 /* return any leftover data */
1884 if (iter->seq.len > start) {
1885 len = iter->seq.len - start;
1886 if (cnt > len)
1887 cnt = len;
1888 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
1889 if (ret)
1890 cnt = -EFAULT;
1891
1892 start += len;
1893
1894 return cnt;
1895 }
1896
1897 trace_seq_reset(&iter->seq);
1898 start = 0;
1899
1900 while (trace_empty(iter)) {
1901 /*
1902 * This is a make-shift waitqueue. The reason we don't use
1903 * an actual wait queue is because:
1904 * 1) we only ever have one waiter
1905 * 2) the tracing, traces all functions, we don't want
1906 * the overhead of calling wake_up and friends
1907 * (and tracing them too)
1908 * Anyway, this is really very primitive wakeup.
1909 */
1910 set_current_state(TASK_INTERRUPTIBLE);
1911 iter->tr->waiter = current;
1912
1913 /* sleep for one second, and try again. */
1914 schedule_timeout(HZ);
1915
1916 iter->tr->waiter = NULL;
1917
1918 if (signal_pending(current))
1919 return -EINTR;
1920
1921 /*
1922 * We block until we read something and tracing is disabled.
1923 * We still block if tracing is disabled, but we have never
1924 * read anything. This allows a user to cat this file, and
1925 * then enable tracing. But after we have read something,
1926 * we give an EOF when tracing is again disabled.
1927 *
1928 * iter->pos will be 0 if we haven't read anything.
1929 */
1930 if (!tracer_enabled && iter->pos)
1931 break;
1932
1933 continue;
1934 }
1935
1936 /* stop when tracing is finished */
1937 if (trace_empty(iter))
1938 return 0;
1939
1940 if (cnt >= PAGE_SIZE)
1941 cnt = PAGE_SIZE - 1;
1942
1943 memset(iter, 0, sizeof(*iter));
1944 iter->tr = &global_trace;
1945 iter->pos = -1;
1946
1947 /*
1948 * We need to stop all tracing on all CPUS to read the
1949 * the next buffer. This is a bit expensive, but is
1950 * not done often. We fill all what we can read,
1951 * and then release the locks again.
1952 */
1953
1954 cpus_clear(mask);
1955 local_irq_save(flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001956 ftrace_save = ftrace_enabled;
1957 ftrace_enabled = 0;
1958 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02001959 for_each_possible_cpu(cpu) {
1960 data = iter->tr->data[cpu];
1961
1962 if (!head_page(data) || !data->trace_idx)
1963 continue;
1964
1965 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001966 cpu_set(cpu, mask);
1967 }
1968
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001969 for_each_cpu_mask(cpu, mask) {
1970 data = iter->tr->data[cpu];
1971 spin_lock(&data->lock);
1972 }
1973
Steven Rostedt088b1e422008-05-12 21:20:48 +02001974 while (find_next_entry_inc(iter) != NULL) {
1975 int len = iter->seq.len;
1976
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001977 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02001978 if (!ret) {
1979 /* don't print partial lines */
1980 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001981 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02001982 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001983
1984 trace_consume(iter);
1985
1986 if (iter->seq.len >= cnt)
1987 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001988 }
1989
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02001990 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001991 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02001992 spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001993 }
1994
1995 for_each_cpu_mask(cpu, mask) {
1996 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02001997 atomic_dec(&data->disabled);
1998 }
Ingo Molnar2e0f5762008-05-12 21:20:49 +02001999 ftrace_enabled = ftrace_save;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002000 local_irq_restore(flags);
2001
2002 /* Now copy what we have to the user */
2003 read = iter->seq.len;
2004 if (read > cnt)
2005 read = cnt;
2006
2007 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2008
2009 if (read < iter->seq.len)
2010 start = read;
2011 else
2012 trace_seq_reset(&iter->seq);
2013
2014 if (ret)
2015 read = -EFAULT;
2016
2017 return read;
2018}
2019
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002020static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002021 .open = tracing_open_generic,
2022 .read = tracing_max_lat_read,
2023 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002024};
2025
2026static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002027 .open = tracing_open_generic,
2028 .read = tracing_ctrl_read,
2029 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002030};
2031
2032static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002033 .open = tracing_open_generic,
2034 .read = tracing_set_trace_read,
2035 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002036};
2037
Steven Rostedtb3806b42008-05-12 21:20:46 +02002038static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002039 .open = tracing_open_pipe,
2040 .read = tracing_read_pipe,
2041 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002042};
2043
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002044#ifdef CONFIG_DYNAMIC_FTRACE
2045
2046static ssize_t
2047tracing_read_long(struct file *filp, char __user *ubuf,
2048 size_t cnt, loff_t *ppos)
2049{
2050 unsigned long *p = filp->private_data;
2051 char buf[64];
2052 int r;
2053
2054 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002055
2056 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002057}
2058
2059static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002060 .open = tracing_open_generic,
2061 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002062};
2063#endif
2064
2065static struct dentry *d_tracer;
2066
2067struct dentry *tracing_init_dentry(void)
2068{
2069 static int once;
2070
2071 if (d_tracer)
2072 return d_tracer;
2073
2074 d_tracer = debugfs_create_dir("tracing", NULL);
2075
2076 if (!d_tracer && !once) {
2077 once = 1;
2078 pr_warning("Could not create debugfs directory 'tracing'\n");
2079 return NULL;
2080 }
2081
2082 return d_tracer;
2083}
2084
Steven Rostedt60a11772008-05-12 21:20:44 +02002085#ifdef CONFIG_FTRACE_SELFTEST
2086/* Let selftest have access to static functions in this file */
2087#include "trace_selftest.c"
2088#endif
2089
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002090static __init void tracer_init_debugfs(void)
2091{
2092 struct dentry *d_tracer;
2093 struct dentry *entry;
2094
2095 d_tracer = tracing_init_dentry();
2096
2097 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2098 &global_trace, &tracing_ctrl_fops);
2099 if (!entry)
2100 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2101
2102 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2103 NULL, &tracing_iter_fops);
2104 if (!entry)
2105 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2106
2107 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2108 &global_trace, &tracing_lt_fops);
2109 if (!entry)
2110 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2111
2112 entry = debugfs_create_file("trace", 0444, d_tracer,
2113 &global_trace, &tracing_fops);
2114 if (!entry)
2115 pr_warning("Could not create debugfs 'trace' entry\n");
2116
2117 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2118 &global_trace, &show_traces_fops);
2119 if (!entry)
2120 pr_warning("Could not create debugfs 'trace' entry\n");
2121
2122 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2123 &global_trace, &set_tracer_fops);
2124 if (!entry)
2125 pr_warning("Could not create debugfs 'trace' entry\n");
2126
2127 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2128 &tracing_max_latency,
2129 &tracing_max_lat_fops);
2130 if (!entry)
2131 pr_warning("Could not create debugfs "
2132 "'tracing_max_latency' entry\n");
2133
2134 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2135 &tracing_thresh, &tracing_max_lat_fops);
2136 if (!entry)
2137 pr_warning("Could not create debugfs "
2138 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002139 entry = debugfs_create_file("README", 0644, d_tracer,
2140 NULL, &tracing_readme_fops);
2141 if (!entry)
2142 pr_warning("Could not create debugfs 'README' entry\n");
2143
Steven Rostedtb3806b42008-05-12 21:20:46 +02002144 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2145 NULL, &tracing_pipe_fops);
2146 if (!entry)
2147 pr_warning("Could not create debugfs "
2148 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002149
2150#ifdef CONFIG_DYNAMIC_FTRACE
2151 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2152 &ftrace_update_tot_cnt,
2153 &tracing_read_long_fops);
2154 if (!entry)
2155 pr_warning("Could not create debugfs "
2156 "'dyn_ftrace_total_info' entry\n");
2157#endif
2158}
2159
2160/* dummy trace to disable tracing */
2161static struct tracer no_tracer __read_mostly =
2162{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002163 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002164};
2165
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002166static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002167{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002168 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002169 struct page *page, *tmp;
2170 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002171 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002172 int i;
2173
2174 /* first allocate a page for each CPU */
2175 for_each_possible_cpu(i) {
2176 array = (void *)__get_free_page(GFP_KERNEL);
2177 if (array == NULL) {
2178 printk(KERN_ERR "tracer: failed to allocate page"
2179 "for trace buffer!\n");
2180 goto free_pages;
2181 }
2182
2183 page = virt_to_page(array);
2184 list_add(&page->lru, &pages);
2185
2186/* Only allocate if we are actually using the max trace */
2187#ifdef CONFIG_TRACER_MAX_TRACE
2188 array = (void *)__get_free_page(GFP_KERNEL);
2189 if (array == NULL) {
2190 printk(KERN_ERR "tracer: failed to allocate page"
2191 "for trace buffer!\n");
2192 goto free_pages;
2193 }
2194 page = virt_to_page(array);
2195 list_add(&page->lru, &pages);
2196#endif
2197 }
2198
2199 /* Now that we successfully allocate a page per CPU, add them */
2200 for_each_possible_cpu(i) {
2201 data = global_trace.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002202 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002203 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002204 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002205 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002206 list_add_tail(&page->lru, &data->trace_pages);
2207 ClearPageLRU(page);
2208
2209#ifdef CONFIG_TRACER_MAX_TRACE
2210 data = max_tr.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002211 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002212 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002213 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002214 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002215 list_add_tail(&page->lru, &data->trace_pages);
2216 SetPageLRU(page);
2217#endif
2218 }
2219 global_trace.entries += ENTRIES_PER_PAGE;
2220
2221 return 0;
2222
2223 free_pages:
2224 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002225 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002226 __free_page(page);
2227 }
2228 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002229}
2230
2231__init static int tracer_alloc_buffers(void)
2232{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002233 struct trace_array_cpu *data;
2234 void *array;
2235 struct page *page;
2236 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002237 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002238 int i;
2239
Steven Rostedt26994ea2008-05-12 21:20:48 +02002240 global_trace.ctrl = tracer_enabled;
2241
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002242 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002243 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002244 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002245 max_tr.data[i] = &per_cpu(max_data, i);
2246
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002247 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002248 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002249 printk(KERN_ERR "tracer: failed to allocate page"
2250 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002251 goto free_buffers;
2252 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002253
2254 /* set the array to the list */
2255 INIT_LIST_HEAD(&data->trace_pages);
2256 page = virt_to_page(array);
2257 list_add(&page->lru, &data->trace_pages);
2258 /* use the LRU flag to differentiate the two buffers */
2259 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002260
2261/* Only allocate if we are actually using the max trace */
2262#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002263 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002264 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002265 printk(KERN_ERR "tracer: failed to allocate page"
2266 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002267 goto free_buffers;
2268 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002269
2270 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2271 page = virt_to_page(array);
2272 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2273 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002274#endif
2275 }
2276
2277 /*
2278 * Since we allocate by orders of pages, we may be able to
2279 * round up a bit.
2280 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002281 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002282 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002283
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002284 while (global_trace.entries < trace_nr_entries) {
2285 if (trace_alloc_page())
2286 break;
2287 pages++;
2288 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002289 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002290
2291 pr_info("tracer: %d pages allocated for %ld",
2292 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002293 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2294 pr_info(" actual entries %ld\n", global_trace.entries);
2295
2296 tracer_init_debugfs();
2297
2298 trace_init_cmdlines();
2299
2300 register_tracer(&no_tracer);
2301 current_trace = &no_tracer;
2302
Steven Rostedt60a11772008-05-12 21:20:44 +02002303 /* All seems OK, enable tracing */
2304 tracing_disabled = 0;
2305
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002306 return 0;
2307
2308 free_buffers:
2309 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002310 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002311 struct trace_array_cpu *data = global_trace.data[i];
2312
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002313 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002314 list_for_each_entry_safe(page, tmp,
2315 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002316 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002317 __free_page(page);
2318 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002319 }
2320
2321#ifdef CONFIG_TRACER_MAX_TRACE
2322 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002323 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002324 list_for_each_entry_safe(page, tmp,
2325 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002326 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002327 __free_page(page);
2328 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002329 }
2330#endif
2331 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002332 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002333}
Steven Rostedt60a11772008-05-12 21:20:44 +02002334fs_initcall(tracer_alloc_buffers);