blob: 0eef0503febd69095000016f6333dbbd326a949a [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 Rostedt60a11772008-05-12 21:20:44 +020038static int tracing_disabled = 1;
39
Thomas Gleixner72829bc2008-05-23 21:37:28 +020040long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020041ns2usecs(cycle_t nsec)
42{
43 nsec += 500;
44 do_div(nsec, 1000);
45 return nsec;
46}
47
Ingo Molnare309b412008-05-12 21:20:51 +020048cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020049{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020050 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020051}
52
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020053static struct trace_array global_trace;
54
55static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
56
57static struct trace_array max_tr;
58
59static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
60
Steven Rostedt26994ea2008-05-12 21:20:48 +020061static int tracer_enabled = 1;
Ingo Molnar57422792008-05-12 21:20:51 +020062static unsigned long trace_nr_entries = 65536UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020063
64static struct tracer *trace_types __read_mostly;
65static struct tracer *current_trace __read_mostly;
66static int max_tracer_type_len;
67
68static DEFINE_MUTEX(trace_types_lock);
Ingo Molnar4e655512008-05-12 21:20:52 +020069static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
70
71unsigned long trace_flags = TRACE_ITER_PRINT_PARENT;
72
Ingo Molnar4e655512008-05-12 21:20:52 +020073void trace_wake_up(void)
74{
Ingo Molnar017730c2008-05-12 21:20:52 +020075 /*
76 * The runqueue_is_locked() can fail, but this is the best we
77 * have for now:
78 */
79 if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
Ingo Molnar4e655512008-05-12 21:20:52 +020080 wake_up(&trace_wait);
81}
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020082
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020083#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
84
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020085static int __init set_nr_entries(char *str)
86{
87 if (!str)
88 return 0;
89 trace_nr_entries = simple_strtoul(str, &str, 0);
90 return 1;
91}
92__setup("trace_entries=", set_nr_entries);
93
Steven Rostedt57f50be2008-05-12 21:20:44 +020094unsigned long nsecs_to_usecs(unsigned long nsecs)
95{
96 return nsecs / 1000;
97}
98
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020099enum trace_flag_type {
100 TRACE_FLAG_IRQS_OFF = 0x01,
101 TRACE_FLAG_NEED_RESCHED = 0x02,
102 TRACE_FLAG_HARDIRQ = 0x04,
103 TRACE_FLAG_SOFTIRQ = 0x08,
104};
105
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200106#define TRACE_ITER_SYM_MASK \
107 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
108
109/* These must match the bit postions above */
110static const char *trace_options[] = {
111 "print-parent",
112 "sym-offset",
113 "sym-addr",
114 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200115 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200116 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200117 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200118 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200119 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200120 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200121 NULL
122};
123
Steven Rostedt92205c22008-05-12 21:20:55 +0200124static raw_spinlock_t ftrace_max_lock =
125 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
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 Molnare309b412008-05-12 21:20:51 +0200132static 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
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200181int
Steven Rostedt214023c2008-05-12 21:20:46 +0200182trace_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 */
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200196 if (ret >= len)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200197 return 0;
198
199 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200200
201 return len;
202}
203
Ingo Molnare309b412008-05-12 21:20:51 +0200204static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200205trace_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
Ingo Molnare309b412008-05-12 21:20:51 +0200218static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200219trace_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 Molnare309b412008-05-12 21:20:51 +0200229static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200230trace_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
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200241#define HEX_CHARS 17
242
Ingo Molnare309b412008-05-12 21:20:51 +0200243static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200244trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
245{
246 unsigned char hex[HEX_CHARS];
247 unsigned char *data;
248 unsigned char byte;
249 int i, j;
250
251 BUG_ON(len >= HEX_CHARS);
252
253 data = mem;
254
255#ifdef __BIG_ENDIAN
256 for (i = 0, j = 0; i < len; i++) {
257#else
258 for (i = len-1, j = 0; i >= 0; i--) {
259#endif
260 byte = data[i];
261
262 hex[j] = byte & 0x0f;
263 if (hex[j] >= 10)
264 hex[j] += 'a' - 10;
265 else
266 hex[j] += '0';
267 j++;
268
269 hex[j] = byte >> 4;
270 if (hex[j] >= 10)
271 hex[j] += 'a' - 10;
272 else
273 hex[j] += '0';
274 j++;
275 }
276 hex[j] = ' ';
277 j++;
278
279 return trace_seq_putmem(s, hex, j);
280}
281
Ingo Molnare309b412008-05-12 21:20:51 +0200282static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200283trace_seq_reset(struct trace_seq *s)
284{
285 s->len = 0;
286}
287
Ingo Molnare309b412008-05-12 21:20:51 +0200288static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200289trace_print_seq(struct seq_file *m, struct trace_seq *s)
290{
291 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
292
293 s->buffer[len] = 0;
294 seq_puts(m, s->buffer);
295
296 trace_seq_reset(s);
297}
298
Ingo Molnare309b412008-05-12 21:20:51 +0200299static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200300flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
301{
302 struct list_head flip_pages;
303
304 INIT_LIST_HEAD(&flip_pages);
305
Steven Rostedt93a588f2008-05-12 21:20:45 +0200306 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200307 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200308 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200309
310 check_pages(tr1);
311 check_pages(tr2);
312 list_splice_init(&tr1->trace_pages, &flip_pages);
313 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
314 list_splice_init(&flip_pages, &tr2->trace_pages);
315 BUG_ON(!list_empty(&flip_pages));
316 check_pages(tr1);
317 check_pages(tr2);
318}
319
Ingo Molnare309b412008-05-12 21:20:51 +0200320void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200321update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
322{
323 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200324 int i;
325
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200326 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200327 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200328 /* clear out all the previous traces */
329 for_each_possible_cpu(i) {
330 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200331 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200332 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200333 }
334
335 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200336 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200337}
338
339/**
340 * update_max_tr_single - only copy one trace over, and reset the rest
341 * @tr - tracer
342 * @tsk - task with the latency
343 * @cpu - the cpu of the buffer to copy.
344 */
Ingo Molnare309b412008-05-12 21:20:51 +0200345void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200346update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
347{
348 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200349 int i;
350
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200351 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200352 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200353 for_each_possible_cpu(i)
354 tracing_reset(max_tr.data[i]);
355
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200356 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200357 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200358
359 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200360 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200361}
362
363int register_tracer(struct tracer *type)
364{
365 struct tracer *t;
366 int len;
367 int ret = 0;
368
369 if (!type->name) {
370 pr_info("Tracer must have a name\n");
371 return -1;
372 }
373
374 mutex_lock(&trace_types_lock);
375 for (t = trace_types; t; t = t->next) {
376 if (strcmp(type->name, t->name) == 0) {
377 /* already found */
378 pr_info("Trace %s already registered\n",
379 type->name);
380 ret = -1;
381 goto out;
382 }
383 }
384
Steven Rostedt60a11772008-05-12 21:20:44 +0200385#ifdef CONFIG_FTRACE_STARTUP_TEST
386 if (type->selftest) {
387 struct tracer *saved_tracer = current_trace;
388 struct trace_array_cpu *data;
389 struct trace_array *tr = &global_trace;
390 int saved_ctrl = tr->ctrl;
391 int i;
392 /*
393 * Run a selftest on this tracer.
394 * Here we reset the trace buffer, and set the current
395 * tracer to be this tracer. The tracer can then run some
396 * internal tracing to verify that everything is in order.
397 * If we fail, we do not register this tracer.
398 */
399 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200400 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200401 if (!head_page(data))
402 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200403 tracing_reset(data);
404 }
405 current_trace = type;
406 tr->ctrl = 0;
407 /* the test is responsible for initializing and enabling */
408 pr_info("Testing tracer %s: ", type->name);
409 ret = type->selftest(type, tr);
410 /* the test is responsible for resetting too */
411 current_trace = saved_tracer;
412 tr->ctrl = saved_ctrl;
413 if (ret) {
414 printk(KERN_CONT "FAILED!\n");
415 goto out;
416 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200417 /* Only reset on passing, to avoid touching corrupted buffers */
418 for_each_possible_cpu(i) {
419 data = tr->data[i];
420 if (!head_page(data))
421 continue;
422 tracing_reset(data);
423 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200424 printk(KERN_CONT "PASSED\n");
425 }
426#endif
427
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200428 type->next = trace_types;
429 trace_types = type;
430 len = strlen(type->name);
431 if (len > max_tracer_type_len)
432 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200433
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200434 out:
435 mutex_unlock(&trace_types_lock);
436
437 return ret;
438}
439
440void unregister_tracer(struct tracer *type)
441{
442 struct tracer **t;
443 int len;
444
445 mutex_lock(&trace_types_lock);
446 for (t = &trace_types; *t; t = &(*t)->next) {
447 if (*t == type)
448 goto found;
449 }
450 pr_info("Trace %s not registered\n", type->name);
451 goto out;
452
453 found:
454 *t = (*t)->next;
455 if (strlen(type->name) != max_tracer_type_len)
456 goto out;
457
458 max_tracer_type_len = 0;
459 for (t = &trace_types; *t; t = &(*t)->next) {
460 len = strlen((*t)->name);
461 if (len > max_tracer_type_len)
462 max_tracer_type_len = len;
463 }
464 out:
465 mutex_unlock(&trace_types_lock);
466}
467
Ingo Molnare309b412008-05-12 21:20:51 +0200468void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200469{
470 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200471 data->trace_head = data->trace_tail = head_page(data);
472 data->trace_head_idx = 0;
473 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200474}
475
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200476#define SAVED_CMDLINES 128
477static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
478static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
479static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
480static int cmdline_idx;
481static DEFINE_SPINLOCK(trace_cmdline_lock);
482atomic_t trace_record_cmdline_disabled;
483
484static void trace_init_cmdlines(void)
485{
486 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
487 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
488 cmdline_idx = 0;
489}
490
Ingo Molnare309b412008-05-12 21:20:51 +0200491void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200492
Ingo Molnare309b412008-05-12 21:20:51 +0200493static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200494{
495 unsigned map;
496 unsigned idx;
497
498 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
499 return;
500
501 /*
502 * It's not the end of the world if we don't get
503 * the lock, but we also don't want to spin
504 * nor do we want to disable interrupts,
505 * so if we miss here, then better luck next time.
506 */
507 if (!spin_trylock(&trace_cmdline_lock))
508 return;
509
510 idx = map_pid_to_cmdline[tsk->pid];
511 if (idx >= SAVED_CMDLINES) {
512 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
513
514 map = map_cmdline_to_pid[idx];
515 if (map <= PID_MAX_DEFAULT)
516 map_pid_to_cmdline[map] = (unsigned)-1;
517
518 map_pid_to_cmdline[tsk->pid] = idx;
519
520 cmdline_idx = idx;
521 }
522
523 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
524
525 spin_unlock(&trace_cmdline_lock);
526}
527
Ingo Molnare309b412008-05-12 21:20:51 +0200528static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200529{
530 char *cmdline = "<...>";
531 unsigned map;
532
533 if (!pid)
534 return "<idle>";
535
536 if (pid > PID_MAX_DEFAULT)
537 goto out;
538
539 map = map_pid_to_cmdline[pid];
540 if (map >= SAVED_CMDLINES)
541 goto out;
542
543 cmdline = saved_cmdlines[map];
544
545 out:
546 return cmdline;
547}
548
Ingo Molnare309b412008-05-12 21:20:51 +0200549void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200550{
551 if (atomic_read(&trace_record_cmdline_disabled))
552 return;
553
554 trace_save_cmdline(tsk);
555}
556
Ingo Molnare309b412008-05-12 21:20:51 +0200557static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200558trace_next_list(struct trace_array_cpu *data, struct list_head *next)
559{
560 /*
561 * Roundrobin - but skip the head (which is not a real page):
562 */
563 next = next->next;
564 if (unlikely(next == &data->trace_pages))
565 next = next->next;
566 BUG_ON(next == &data->trace_pages);
567
568 return next;
569}
570
Ingo Molnare309b412008-05-12 21:20:51 +0200571static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200572trace_next_page(struct trace_array_cpu *data, void *addr)
573{
574 struct list_head *next;
575 struct page *page;
576
577 page = virt_to_page(addr);
578
579 next = trace_next_list(data, &page->lru);
580 page = list_entry(next, struct page, lru);
581
582 return page_address(page);
583}
584
Ingo Molnare309b412008-05-12 21:20:51 +0200585static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200586tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200587{
588 unsigned long idx, idx_next;
589 struct trace_entry *entry;
590
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200591 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200592 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200593 idx_next = idx + 1;
594
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200595 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
596
Steven Rostedt93a588f2008-05-12 21:20:45 +0200597 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200598
599 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200600 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200601 idx_next = 0;
602 }
603
Steven Rostedt93a588f2008-05-12 21:20:45 +0200604 if (data->trace_head == data->trace_tail &&
605 idx_next == data->trace_tail_idx) {
606 /* overrun */
607 data->trace_tail_idx++;
608 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
609 data->trace_tail =
610 trace_next_page(data, data->trace_tail);
611 data->trace_tail_idx = 0;
612 }
613 }
614
615 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200616
617 return entry;
618}
619
Ingo Molnare309b412008-05-12 21:20:51 +0200620static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200621tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200622{
623 struct task_struct *tsk = current;
624 unsigned long pc;
625
626 pc = preempt_count();
627
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200628 entry->preempt_count = pc & 0xff;
Thomas Gleixner72829bc2008-05-23 21:37:28 +0200629 entry->pid = (tsk) ? tsk->pid : 0;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200630 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200631 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
632 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
633 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
634 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
635}
636
Ingo Molnare309b412008-05-12 21:20:51 +0200637void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200638trace_function(struct trace_array *tr, struct trace_array_cpu *data,
639 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200640{
641 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200642 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200643
Steven Rostedt92205c22008-05-12 21:20:55 +0200644 raw_local_irq_save(irq_flags);
645 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200646 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200647 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200648 entry->type = TRACE_FN;
649 entry->fn.ip = ip;
650 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200651 __raw_spin_unlock(&data->lock);
652 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653}
654
Ingo Molnare309b412008-05-12 21:20:51 +0200655void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200656ftrace(struct trace_array *tr, struct trace_array_cpu *data,
657 unsigned long ip, unsigned long parent_ip, unsigned long flags)
658{
659 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200660 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200661}
662
Ingo Molnare309b412008-05-12 21:20:51 +0200663void
Ingo Molnar4e655512008-05-12 21:20:52 +0200664__trace_special(void *__tr, void *__data,
665 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200666{
Ingo Molnar4e655512008-05-12 21:20:52 +0200667 struct trace_array_cpu *data = __data;
668 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200669 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200670 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200671
Steven Rostedt92205c22008-05-12 21:20:55 +0200672 raw_local_irq_save(irq_flags);
673 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200674 entry = tracing_get_trace_entry(tr, data);
675 tracing_generic_entry_update(entry, 0);
676 entry->type = TRACE_SPECIAL;
677 entry->special.arg1 = arg1;
678 entry->special.arg2 = arg2;
679 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200680 __raw_spin_unlock(&data->lock);
681 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200682
683 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200684}
685
686void __trace_stack(struct trace_array *tr,
687 struct trace_array_cpu *data,
688 unsigned long flags,
689 int skip)
690{
691 struct trace_entry *entry;
692 struct stack_trace trace;
693
694 if (!(trace_flags & TRACE_ITER_STACKTRACE))
695 return;
696
697 entry = tracing_get_trace_entry(tr, data);
698 tracing_generic_entry_update(entry, flags);
699 entry->type = TRACE_STACK;
700
701 memset(&entry->stack, 0, sizeof(entry->stack));
702
703 trace.nr_entries = 0;
704 trace.max_entries = FTRACE_STACK_ENTRIES;
705 trace.skip = skip;
706 trace.entries = entry->stack.caller;
707
708 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200709}
710
Ingo Molnare309b412008-05-12 21:20:51 +0200711void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200712tracing_sched_switch_trace(struct trace_array *tr,
713 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200714 struct task_struct *prev,
715 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200716 unsigned long flags)
717{
718 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200719 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200720
Steven Rostedt92205c22008-05-12 21:20:55 +0200721 raw_local_irq_save(irq_flags);
722 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200723 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200724 tracing_generic_entry_update(entry, flags);
725 entry->type = TRACE_CTX;
726 entry->ctx.prev_pid = prev->pid;
727 entry->ctx.prev_prio = prev->prio;
728 entry->ctx.prev_state = prev->state;
729 entry->ctx.next_pid = next->pid;
730 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200731 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200732 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200733 __raw_spin_unlock(&data->lock);
734 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200735}
736
Ingo Molnar57422792008-05-12 21:20:51 +0200737void
738tracing_sched_wakeup_trace(struct trace_array *tr,
739 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200740 struct task_struct *wakee,
741 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200742 unsigned long flags)
743{
744 struct trace_entry *entry;
745 unsigned long irq_flags;
746
Steven Rostedt92205c22008-05-12 21:20:55 +0200747 raw_local_irq_save(irq_flags);
748 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200749 entry = tracing_get_trace_entry(tr, data);
750 tracing_generic_entry_update(entry, flags);
751 entry->type = TRACE_WAKE;
752 entry->ctx.prev_pid = curr->pid;
753 entry->ctx.prev_prio = curr->prio;
754 entry->ctx.prev_state = curr->state;
755 entry->ctx.next_pid = wakee->pid;
756 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200757 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200758 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200759 __raw_spin_unlock(&data->lock);
760 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200761
762 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200763}
764
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200765#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200766static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200767function_trace_call(unsigned long ip, unsigned long parent_ip)
768{
769 struct trace_array *tr = &global_trace;
770 struct trace_array_cpu *data;
771 unsigned long flags;
772 long disabled;
773 int cpu;
774
775 if (unlikely(!tracer_enabled))
776 return;
777
778 local_irq_save(flags);
779 cpu = raw_smp_processor_id();
780 data = tr->data[cpu];
781 disabled = atomic_inc_return(&data->disabled);
782
783 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200784 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200785
786 atomic_dec(&data->disabled);
787 local_irq_restore(flags);
788}
789
790static struct ftrace_ops trace_ops __read_mostly =
791{
792 .func = function_trace_call,
793};
794
Ingo Molnare309b412008-05-12 21:20:51 +0200795void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200796{
797 register_ftrace_function(&trace_ops);
798}
799
Ingo Molnare309b412008-05-12 21:20:51 +0200800void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200801{
802 unregister_ftrace_function(&trace_ops);
803}
804#endif
805
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200806enum trace_file_type {
807 TRACE_FILE_LAT_FMT = 1,
808};
809
810static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200811trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
812 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200813{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200814 struct page *page;
815 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200816
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200817 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200818 iter->next_idx[cpu] >= data->trace_idx ||
819 (data->trace_head == data->trace_tail &&
820 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200821 return NULL;
822
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200823 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200824 /* Initialize the iterator for this cpu trace buffer */
825 WARN_ON(!data->trace_tail);
826 page = virt_to_page(data->trace_tail);
827 iter->next_page[cpu] = &page->lru;
828 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200829 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200830
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200831 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200832 BUG_ON(&data->trace_pages == &page->lru);
833
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200834 array = page_address(page);
835
Steven Rostedt93a588f2008-05-12 21:20:45 +0200836 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200837 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200838}
839
Ingo Molnare309b412008-05-12 21:20:51 +0200840static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200841find_next_entry(struct trace_iterator *iter, int *ent_cpu)
842{
843 struct trace_array *tr = iter->tr;
844 struct trace_entry *ent, *next = NULL;
845 int next_cpu = -1;
846 int cpu;
847
848 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200849 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200850 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200851 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200852 /*
853 * Pick the entry with the smallest timestamp:
854 */
855 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200856 next = ent;
857 next_cpu = cpu;
858 }
859 }
860
861 if (ent_cpu)
862 *ent_cpu = next_cpu;
863
864 return next;
865}
866
Ingo Molnare309b412008-05-12 21:20:51 +0200867static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200868{
869 iter->idx++;
870 iter->next_idx[iter->cpu]++;
871 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200872
Steven Rostedtb3806b42008-05-12 21:20:46 +0200873 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
874 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
875
876 iter->next_page_idx[iter->cpu] = 0;
877 iter->next_page[iter->cpu] =
878 trace_next_list(data, iter->next_page[iter->cpu]);
879 }
880}
881
Ingo Molnare309b412008-05-12 21:20:51 +0200882static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200883{
884 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
885
886 data->trace_tail_idx++;
887 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
888 data->trace_tail = trace_next_page(data, data->trace_tail);
889 data->trace_tail_idx = 0;
890 }
891
892 /* Check if we empty it, then reset the index */
893 if (data->trace_head == data->trace_tail &&
894 data->trace_head_idx == data->trace_tail_idx)
895 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200896}
897
Ingo Molnare309b412008-05-12 21:20:51 +0200898static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200899{
900 struct trace_entry *next;
901 int next_cpu = -1;
902
903 next = find_next_entry(iter, &next_cpu);
904
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200905 iter->prev_ent = iter->ent;
906 iter->prev_cpu = iter->cpu;
907
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200908 iter->ent = next;
909 iter->cpu = next_cpu;
910
Steven Rostedtb3806b42008-05-12 21:20:46 +0200911 if (next)
912 trace_iterator_increment(iter);
913
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200914 return next ? iter : NULL;
915}
916
Ingo Molnare309b412008-05-12 21:20:51 +0200917static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200918{
919 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200920 void *last_ent = iter->ent;
921 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200922 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200923
924 (*pos)++;
925
926 /* can't go backwards */
927 if (iter->idx > i)
928 return NULL;
929
930 if (iter->idx < 0)
931 ent = find_next_entry_inc(iter);
932 else
933 ent = iter;
934
935 while (ent && iter->idx < i)
936 ent = find_next_entry_inc(iter);
937
938 iter->pos = *pos;
939
940 if (last_ent && !ent)
941 seq_puts(m, "\n\nvim:ft=help\n");
942
943 return ent;
944}
945
946static void *s_start(struct seq_file *m, loff_t *pos)
947{
948 struct trace_iterator *iter = m->private;
949 void *p = NULL;
950 loff_t l = 0;
951 int i;
952
953 mutex_lock(&trace_types_lock);
954
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200955 if (!current_trace || current_trace != iter->trace) {
956 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200957 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200958 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200959
960 atomic_inc(&trace_record_cmdline_disabled);
961
962 /* let the tracer grab locks here if needed */
963 if (current_trace->start)
964 current_trace->start(iter);
965
966 if (*pos != iter->pos) {
967 iter->ent = NULL;
968 iter->cpu = 0;
969 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200970 iter->prev_ent = NULL;
971 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200972
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200973 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200974 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200975 iter->next_page[i] = NULL;
976 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200977
978 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
979 ;
980
981 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200982 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200983 p = s_next(m, p, &l);
984 }
985
986 return p;
987}
988
989static void s_stop(struct seq_file *m, void *p)
990{
991 struct trace_iterator *iter = m->private;
992
993 atomic_dec(&trace_record_cmdline_disabled);
994
995 /* let the tracer release locks here if needed */
996 if (current_trace && current_trace == iter->trace && iter->trace->stop)
997 iter->trace->stop(iter);
998
999 mutex_unlock(&trace_types_lock);
1000}
1001
Steven Rostedtb3806b42008-05-12 21:20:46 +02001002static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001003seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001004{
1005#ifdef CONFIG_KALLSYMS
1006 char str[KSYM_SYMBOL_LEN];
1007
1008 kallsyms_lookup(address, NULL, NULL, NULL, str);
1009
Steven Rostedtb3806b42008-05-12 21:20:46 +02001010 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001011#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001012 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001013}
1014
Steven Rostedtb3806b42008-05-12 21:20:46 +02001015static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001016seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1017 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001018{
1019#ifdef CONFIG_KALLSYMS
1020 char str[KSYM_SYMBOL_LEN];
1021
1022 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001023 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001024#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001025 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001026}
1027
1028#ifndef CONFIG_64BIT
1029# define IP_FMT "%08lx"
1030#else
1031# define IP_FMT "%016lx"
1032#endif
1033
Ingo Molnare309b412008-05-12 21:20:51 +02001034static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001035seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001036{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001037 int ret;
1038
1039 if (!ip)
1040 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001041
1042 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001043 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001044 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001045 ret = seq_print_sym_short(s, "%s", ip);
1046
1047 if (!ret)
1048 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001049
1050 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001051 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1052 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001053}
1054
Ingo Molnare309b412008-05-12 21:20:51 +02001055static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056{
1057 seq_puts(m, "# _------=> CPU# \n");
1058 seq_puts(m, "# / _-----=> irqs-off \n");
1059 seq_puts(m, "# | / _----=> need-resched \n");
1060 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1061 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1062 seq_puts(m, "# |||| / \n");
1063 seq_puts(m, "# ||||| delay \n");
1064 seq_puts(m, "# cmd pid ||||| time | caller \n");
1065 seq_puts(m, "# \\ / ||||| \\ | / \n");
1066}
1067
Ingo Molnare309b412008-05-12 21:20:51 +02001068static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001069{
1070 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1071 seq_puts(m, "# | | | | |\n");
1072}
1073
1074
Ingo Molnare309b412008-05-12 21:20:51 +02001075static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001076print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1077{
1078 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1079 struct trace_array *tr = iter->tr;
1080 struct trace_array_cpu *data = tr->data[tr->cpu];
1081 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001082 unsigned long total = 0;
1083 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001084 int cpu;
1085 const char *name = "preemption";
1086
1087 if (type)
1088 name = type->name;
1089
1090 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001091 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001092 total += tr->data[cpu]->trace_idx;
1093 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001094 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001095 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001096 entries += tr->data[cpu]->trace_idx;
1097 }
1098 }
1099
1100 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1101 name, UTS_RELEASE);
1102 seq_puts(m, "-----------------------------------"
1103 "---------------------------------\n");
1104 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1105 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001106 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001107 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001108 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001109 tr->cpu,
1110#if defined(CONFIG_PREEMPT_NONE)
1111 "server",
1112#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1113 "desktop",
1114#elif defined(CONFIG_PREEMPT_DESKTOP)
1115 "preempt",
1116#else
1117 "unknown",
1118#endif
1119 /* These are reserved for later use */
1120 0, 0, 0, 0);
1121#ifdef CONFIG_SMP
1122 seq_printf(m, " #P:%d)\n", num_online_cpus());
1123#else
1124 seq_puts(m, ")\n");
1125#endif
1126 seq_puts(m, " -----------------\n");
1127 seq_printf(m, " | task: %.16s-%d "
1128 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1129 data->comm, data->pid, data->uid, data->nice,
1130 data->policy, data->rt_priority);
1131 seq_puts(m, " -----------------\n");
1132
1133 if (data->critical_start) {
1134 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001135 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1136 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001137 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001138 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1139 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001140 seq_puts(m, "\n");
1141 }
1142
1143 seq_puts(m, "\n");
1144}
1145
Ingo Molnare309b412008-05-12 21:20:51 +02001146static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001147lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001148{
1149 int hardirq, softirq;
1150 char *comm;
1151
1152 comm = trace_find_cmdline(entry->pid);
1153
Steven Rostedt214023c2008-05-12 21:20:46 +02001154 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1155 trace_seq_printf(s, "%d", cpu);
1156 trace_seq_printf(s, "%c%c",
1157 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1158 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001159
1160 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1161 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1162 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001163 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001164 else {
1165 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001166 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001167 else {
1168 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001169 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001170 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001171 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172 }
1173 }
1174
1175 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001176 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001178 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001179}
1180
1181unsigned long preempt_mark_thresh = 100;
1182
Ingo Molnare309b412008-05-12 21:20:51 +02001183static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001184lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001185 unsigned long rel_usecs)
1186{
Steven Rostedt214023c2008-05-12 21:20:46 +02001187 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001188 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001189 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001190 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001191 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001192 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001193 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001194}
1195
1196static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1197
Ingo Molnare309b412008-05-12 21:20:51 +02001198static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001199print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001200{
Steven Rostedt214023c2008-05-12 21:20:46 +02001201 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001202 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1203 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1204 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1205 struct trace_entry *entry = iter->ent;
1206 unsigned long abs_usecs;
1207 unsigned long rel_usecs;
1208 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001209 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001210 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001211
1212 if (!next_entry)
1213 next_entry = entry;
1214 rel_usecs = ns2usecs(next_entry->t - entry->t);
1215 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1216
1217 if (verbose) {
1218 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001219 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1220 " %ld.%03ldms (+%ld.%03ldms): ",
1221 comm,
1222 entry->pid, cpu, entry->flags,
1223 entry->preempt_count, trace_idx,
1224 ns2usecs(entry->t),
1225 abs_usecs/1000,
1226 abs_usecs % 1000, rel_usecs/1000,
1227 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001228 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001229 lat_print_generic(s, entry, cpu);
1230 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001231 }
1232 switch (entry->type) {
1233 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001234 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1235 trace_seq_puts(s, " (");
1236 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1237 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001238 break;
1239 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001240 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001241 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1242 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001243 T = entry->ctx.next_state < sizeof(state_to_char) ?
1244 state_to_char[entry->ctx.next_state] : 'X';
1245
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001246 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001247 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001248 entry->ctx.prev_pid,
1249 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001250 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001251 entry->ctx.next_pid,
1252 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001253 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001254 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001255 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001256 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001257 entry->special.arg1,
1258 entry->special.arg2,
1259 entry->special.arg3);
1260 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001261 case TRACE_STACK:
1262 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1263 if (i)
1264 trace_seq_puts(s, " <= ");
1265 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1266 }
1267 trace_seq_puts(s, "\n");
1268 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001269 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001270 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001271 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001272 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001273}
1274
Ingo Molnare309b412008-05-12 21:20:51 +02001275static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001276{
Steven Rostedt214023c2008-05-12 21:20:46 +02001277 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001278 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001279 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001280 unsigned long usec_rem;
1281 unsigned long long t;
1282 unsigned long secs;
1283 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001284 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001285 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001286 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001287
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001288 entry = iter->ent;
1289
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290 comm = trace_find_cmdline(iter->ent->pid);
1291
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001292 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001293 usec_rem = do_div(t, 1000000ULL);
1294 secs = (unsigned long)t;
1295
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001296 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1297 if (!ret)
1298 return 0;
1299 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1300 if (!ret)
1301 return 0;
1302 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1303 if (!ret)
1304 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001305
1306 switch (entry->type) {
1307 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001308 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1309 if (!ret)
1310 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001311 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1312 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001313 ret = trace_seq_printf(s, " <-");
1314 if (!ret)
1315 return 0;
1316 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1317 sym_flags);
1318 if (!ret)
1319 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001320 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001321 ret = trace_seq_printf(s, "\n");
1322 if (!ret)
1323 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001324 break;
1325 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001326 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001327 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1328 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001329 T = entry->ctx.next_state < sizeof(state_to_char) ?
1330 state_to_char[entry->ctx.next_state] : 'X';
1331 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001332 entry->ctx.prev_pid,
1333 entry->ctx.prev_prio,
1334 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001335 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001336 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001337 entry->ctx.next_prio,
1338 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001339 if (!ret)
1340 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001341 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001342 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001343 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001344 entry->special.arg1,
1345 entry->special.arg2,
1346 entry->special.arg3);
1347 if (!ret)
1348 return 0;
1349 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001350 case TRACE_STACK:
1351 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1352 if (i) {
1353 ret = trace_seq_puts(s, " <= ");
1354 if (!ret)
1355 return 0;
1356 }
1357 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1358 sym_flags);
1359 if (!ret)
1360 return 0;
1361 }
1362 ret = trace_seq_puts(s, "\n");
1363 if (!ret)
1364 return 0;
1365 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001366 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001367 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001368}
1369
Ingo Molnare309b412008-05-12 21:20:51 +02001370static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001371{
1372 struct trace_seq *s = &iter->seq;
1373 struct trace_entry *entry;
1374 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001375 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001376
1377 entry = iter->ent;
1378
1379 ret = trace_seq_printf(s, "%d %d %llu ",
1380 entry->pid, iter->cpu, entry->t);
1381 if (!ret)
1382 return 0;
1383
1384 switch (entry->type) {
1385 case TRACE_FN:
1386 ret = trace_seq_printf(s, "%x %x\n",
1387 entry->fn.ip, entry->fn.parent_ip);
1388 if (!ret)
1389 return 0;
1390 break;
1391 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001392 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001393 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1394 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001395 T = entry->ctx.next_state < sizeof(state_to_char) ?
1396 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001397 if (entry->type == TRACE_WAKE)
1398 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001399 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001400 entry->ctx.prev_pid,
1401 entry->ctx.prev_prio,
1402 S,
1403 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001404 entry->ctx.next_prio,
1405 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001406 if (!ret)
1407 return 0;
1408 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001409 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001410 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001411 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001412 entry->special.arg1,
1413 entry->special.arg2,
1414 entry->special.arg3);
1415 if (!ret)
1416 return 0;
1417 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001418 }
1419 return 1;
1420}
1421
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001422#define SEQ_PUT_FIELD_RET(s, x) \
1423do { \
1424 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1425 return 0; \
1426} while (0)
1427
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001428#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1429do { \
1430 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1431 return 0; \
1432} while (0)
1433
Ingo Molnare309b412008-05-12 21:20:51 +02001434static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001435{
1436 struct trace_seq *s = &iter->seq;
1437 unsigned char newline = '\n';
1438 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001439 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001440
1441 entry = iter->ent;
1442
1443 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1444 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1445 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1446
1447 switch (entry->type) {
1448 case TRACE_FN:
1449 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1450 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1451 break;
1452 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001453 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001454 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1455 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001456 T = entry->ctx.next_state < sizeof(state_to_char) ?
1457 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001458 if (entry->type == TRACE_WAKE)
1459 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001460 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1462 SEQ_PUT_HEX_FIELD_RET(s, S);
1463 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1464 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1465 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001466 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001467 break;
1468 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001469 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001470 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1471 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1472 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1473 break;
1474 }
1475 SEQ_PUT_FIELD_RET(s, newline);
1476
1477 return 1;
1478}
1479
Ingo Molnare309b412008-05-12 21:20:51 +02001480static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001481{
1482 struct trace_seq *s = &iter->seq;
1483 struct trace_entry *entry;
1484
1485 entry = iter->ent;
1486
1487 SEQ_PUT_FIELD_RET(s, entry->pid);
1488 SEQ_PUT_FIELD_RET(s, entry->cpu);
1489 SEQ_PUT_FIELD_RET(s, entry->t);
1490
1491 switch (entry->type) {
1492 case TRACE_FN:
1493 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1494 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1495 break;
1496 case TRACE_CTX:
1497 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1498 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1499 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1500 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1501 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001502 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001503 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001504 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001505 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001506 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1507 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1508 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1509 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001510 }
1511 return 1;
1512}
1513
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001514static int trace_empty(struct trace_iterator *iter)
1515{
1516 struct trace_array_cpu *data;
1517 int cpu;
1518
1519 for_each_possible_cpu(cpu) {
1520 data = iter->tr->data[cpu];
1521
Steven Rostedtb3806b42008-05-12 21:20:46 +02001522 if (head_page(data) && data->trace_idx &&
1523 (data->trace_tail != data->trace_head ||
1524 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001525 return 0;
1526 }
1527 return 1;
1528}
1529
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001530static int print_trace_line(struct trace_iterator *iter)
1531{
Thomas Gleixner72829bc2008-05-23 21:37:28 +02001532 if (iter->trace && iter->trace->print_line)
1533 return iter->trace->print_line(iter);
1534
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001535 if (trace_flags & TRACE_ITER_BIN)
1536 return print_bin_fmt(iter);
1537
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001538 if (trace_flags & TRACE_ITER_HEX)
1539 return print_hex_fmt(iter);
1540
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001541 if (trace_flags & TRACE_ITER_RAW)
1542 return print_raw_fmt(iter);
1543
1544 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1545 return print_lat_fmt(iter, iter->idx, iter->cpu);
1546
1547 return print_trace_fmt(iter);
1548}
1549
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001550static int s_show(struct seq_file *m, void *v)
1551{
1552 struct trace_iterator *iter = v;
1553
1554 if (iter->ent == NULL) {
1555 if (iter->tr) {
1556 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1557 seq_puts(m, "#\n");
1558 }
1559 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1560 /* print nothing if the buffers are empty */
1561 if (trace_empty(iter))
1562 return 0;
1563 print_trace_header(m, iter);
1564 if (!(trace_flags & TRACE_ITER_VERBOSE))
1565 print_lat_help_header(m);
1566 } else {
1567 if (!(trace_flags & TRACE_ITER_VERBOSE))
1568 print_func_help_header(m);
1569 }
1570 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001571 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001572 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001573 }
1574
1575 return 0;
1576}
1577
1578static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001579 .start = s_start,
1580 .next = s_next,
1581 .stop = s_stop,
1582 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001583};
1584
Ingo Molnare309b412008-05-12 21:20:51 +02001585static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001586__tracing_open(struct inode *inode, struct file *file, int *ret)
1587{
1588 struct trace_iterator *iter;
1589
Steven Rostedt60a11772008-05-12 21:20:44 +02001590 if (tracing_disabled) {
1591 *ret = -ENODEV;
1592 return NULL;
1593 }
1594
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001595 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1596 if (!iter) {
1597 *ret = -ENOMEM;
1598 goto out;
1599 }
1600
1601 mutex_lock(&trace_types_lock);
1602 if (current_trace && current_trace->print_max)
1603 iter->tr = &max_tr;
1604 else
1605 iter->tr = inode->i_private;
1606 iter->trace = current_trace;
1607 iter->pos = -1;
1608
1609 /* TODO stop tracer */
1610 *ret = seq_open(file, &tracer_seq_ops);
1611 if (!*ret) {
1612 struct seq_file *m = file->private_data;
1613 m->private = iter;
1614
1615 /* stop the trace while dumping */
1616 if (iter->tr->ctrl)
1617 tracer_enabled = 0;
1618
1619 if (iter->trace && iter->trace->open)
1620 iter->trace->open(iter);
1621 } else {
1622 kfree(iter);
1623 iter = NULL;
1624 }
1625 mutex_unlock(&trace_types_lock);
1626
1627 out:
1628 return iter;
1629}
1630
1631int tracing_open_generic(struct inode *inode, struct file *filp)
1632{
Steven Rostedt60a11772008-05-12 21:20:44 +02001633 if (tracing_disabled)
1634 return -ENODEV;
1635
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001636 filp->private_data = inode->i_private;
1637 return 0;
1638}
1639
1640int tracing_release(struct inode *inode, struct file *file)
1641{
1642 struct seq_file *m = (struct seq_file *)file->private_data;
1643 struct trace_iterator *iter = m->private;
1644
1645 mutex_lock(&trace_types_lock);
1646 if (iter->trace && iter->trace->close)
1647 iter->trace->close(iter);
1648
1649 /* reenable tracing if it was previously enabled */
1650 if (iter->tr->ctrl)
1651 tracer_enabled = 1;
1652 mutex_unlock(&trace_types_lock);
1653
1654 seq_release(inode, file);
1655 kfree(iter);
1656 return 0;
1657}
1658
1659static int tracing_open(struct inode *inode, struct file *file)
1660{
1661 int ret;
1662
1663 __tracing_open(inode, file, &ret);
1664
1665 return ret;
1666}
1667
1668static int tracing_lt_open(struct inode *inode, struct file *file)
1669{
1670 struct trace_iterator *iter;
1671 int ret;
1672
1673 iter = __tracing_open(inode, file, &ret);
1674
1675 if (!ret)
1676 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1677
1678 return ret;
1679}
1680
1681
Ingo Molnare309b412008-05-12 21:20:51 +02001682static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001683t_next(struct seq_file *m, void *v, loff_t *pos)
1684{
1685 struct tracer *t = m->private;
1686
1687 (*pos)++;
1688
1689 if (t)
1690 t = t->next;
1691
1692 m->private = t;
1693
1694 return t;
1695}
1696
1697static void *t_start(struct seq_file *m, loff_t *pos)
1698{
1699 struct tracer *t = m->private;
1700 loff_t l = 0;
1701
1702 mutex_lock(&trace_types_lock);
1703 for (; t && l < *pos; t = t_next(m, t, &l))
1704 ;
1705
1706 return t;
1707}
1708
1709static void t_stop(struct seq_file *m, void *p)
1710{
1711 mutex_unlock(&trace_types_lock);
1712}
1713
1714static int t_show(struct seq_file *m, void *v)
1715{
1716 struct tracer *t = v;
1717
1718 if (!t)
1719 return 0;
1720
1721 seq_printf(m, "%s", t->name);
1722 if (t->next)
1723 seq_putc(m, ' ');
1724 else
1725 seq_putc(m, '\n');
1726
1727 return 0;
1728}
1729
1730static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001731 .start = t_start,
1732 .next = t_next,
1733 .stop = t_stop,
1734 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001735};
1736
1737static int show_traces_open(struct inode *inode, struct file *file)
1738{
1739 int ret;
1740
Steven Rostedt60a11772008-05-12 21:20:44 +02001741 if (tracing_disabled)
1742 return -ENODEV;
1743
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001744 ret = seq_open(file, &show_traces_seq_ops);
1745 if (!ret) {
1746 struct seq_file *m = file->private_data;
1747 m->private = trace_types;
1748 }
1749
1750 return ret;
1751}
1752
1753static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001754 .open = tracing_open,
1755 .read = seq_read,
1756 .llseek = seq_lseek,
1757 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001758};
1759
1760static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001761 .open = tracing_lt_open,
1762 .read = seq_read,
1763 .llseek = seq_lseek,
1764 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001765};
1766
1767static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001768 .open = show_traces_open,
1769 .read = seq_read,
1770 .release = seq_release,
1771};
1772
Ingo Molnar36dfe922008-05-12 21:20:52 +02001773/*
1774 * Only trace on a CPU if the bitmask is set:
1775 */
1776static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1777
1778/*
1779 * When tracing/tracing_cpu_mask is modified then this holds
1780 * the new bitmask we are about to install:
1781 */
1782static cpumask_t tracing_cpumask_new;
1783
1784/*
1785 * The tracer itself will not take this lock, but still we want
1786 * to provide a consistent cpumask to user-space:
1787 */
1788static DEFINE_MUTEX(tracing_cpumask_update_lock);
1789
1790/*
1791 * Temporary storage for the character representation of the
1792 * CPU bitmask (and one more byte for the newline):
1793 */
1794static char mask_str[NR_CPUS + 1];
1795
Ingo Molnarc7078de2008-05-12 21:20:52 +02001796static ssize_t
1797tracing_cpumask_read(struct file *filp, char __user *ubuf,
1798 size_t count, loff_t *ppos)
1799{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001800 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001801
1802 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001803
1804 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1805 if (count - len < 2) {
1806 count = -EINVAL;
1807 goto out_err;
1808 }
1809 len += sprintf(mask_str + len, "\n");
1810 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1811
1812out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001813 mutex_unlock(&tracing_cpumask_update_lock);
1814
1815 return count;
1816}
1817
1818static ssize_t
1819tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1820 size_t count, loff_t *ppos)
1821{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001822 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001823
1824 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001825 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1826 if (err)
1827 goto err_unlock;
1828
Steven Rostedt92205c22008-05-12 21:20:55 +02001829 raw_local_irq_disable();
1830 __raw_spin_lock(&ftrace_max_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001831 for_each_possible_cpu(cpu) {
1832 /*
1833 * Increase/decrease the disabled counter if we are
1834 * about to flip a bit in the cpumask:
1835 */
1836 if (cpu_isset(cpu, tracing_cpumask) &&
1837 !cpu_isset(cpu, tracing_cpumask_new)) {
1838 atomic_inc(&global_trace.data[cpu]->disabled);
1839 }
1840 if (!cpu_isset(cpu, tracing_cpumask) &&
1841 cpu_isset(cpu, tracing_cpumask_new)) {
1842 atomic_dec(&global_trace.data[cpu]->disabled);
1843 }
1844 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001845 __raw_spin_unlock(&ftrace_max_lock);
1846 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001847
1848 tracing_cpumask = tracing_cpumask_new;
1849
Ingo Molnarc7078de2008-05-12 21:20:52 +02001850 mutex_unlock(&tracing_cpumask_update_lock);
1851
Ingo Molnarc7078de2008-05-12 21:20:52 +02001852 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001853
1854err_unlock:
1855 mutex_unlock(&tracing_cpumask_update_lock);
1856
1857 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001858}
1859
1860static struct file_operations tracing_cpumask_fops = {
1861 .open = tracing_open_generic,
1862 .read = tracing_cpumask_read,
1863 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001864};
1865
1866static ssize_t
1867tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1868 size_t cnt, loff_t *ppos)
1869{
1870 char *buf;
1871 int r = 0;
1872 int len = 0;
1873 int i;
1874
1875 /* calulate max size */
1876 for (i = 0; trace_options[i]; i++) {
1877 len += strlen(trace_options[i]);
1878 len += 3; /* "no" and space */
1879 }
1880
1881 /* +2 for \n and \0 */
1882 buf = kmalloc(len + 2, GFP_KERNEL);
1883 if (!buf)
1884 return -ENOMEM;
1885
1886 for (i = 0; trace_options[i]; i++) {
1887 if (trace_flags & (1 << i))
1888 r += sprintf(buf + r, "%s ", trace_options[i]);
1889 else
1890 r += sprintf(buf + r, "no%s ", trace_options[i]);
1891 }
1892
1893 r += sprintf(buf + r, "\n");
1894 WARN_ON(r >= len + 2);
1895
Ingo Molnar36dfe922008-05-12 21:20:52 +02001896 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001897
1898 kfree(buf);
1899
1900 return r;
1901}
1902
1903static ssize_t
1904tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1905 size_t cnt, loff_t *ppos)
1906{
1907 char buf[64];
1908 char *cmp = buf;
1909 int neg = 0;
1910 int i;
1911
1912 if (cnt > 63)
1913 cnt = 63;
1914
1915 if (copy_from_user(&buf, ubuf, cnt))
1916 return -EFAULT;
1917
1918 buf[cnt] = 0;
1919
1920 if (strncmp(buf, "no", 2) == 0) {
1921 neg = 1;
1922 cmp += 2;
1923 }
1924
1925 for (i = 0; trace_options[i]; i++) {
1926 int len = strlen(trace_options[i]);
1927
1928 if (strncmp(cmp, trace_options[i], len) == 0) {
1929 if (neg)
1930 trace_flags &= ~(1 << i);
1931 else
1932 trace_flags |= (1 << i);
1933 break;
1934 }
1935 }
Ingo Molnar442e5442008-05-12 21:20:53 +02001936 /*
1937 * If no option could be set, return an error:
1938 */
1939 if (!trace_options[i])
1940 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001941
1942 filp->f_pos += cnt;
1943
1944 return cnt;
1945}
1946
1947static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001948 .open = tracing_open_generic,
1949 .read = tracing_iter_ctrl_read,
1950 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001951};
1952
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001953static const char readme_msg[] =
1954 "tracing mini-HOWTO:\n\n"
1955 "# mkdir /debug\n"
1956 "# mount -t debugfs nodev /debug\n\n"
1957 "# cat /debug/tracing/available_tracers\n"
1958 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1959 "# cat /debug/tracing/current_tracer\n"
1960 "none\n"
1961 "# echo sched_switch > /debug/tracing/current_tracer\n"
1962 "# cat /debug/tracing/current_tracer\n"
1963 "sched_switch\n"
1964 "# cat /debug/tracing/iter_ctrl\n"
1965 "noprint-parent nosym-offset nosym-addr noverbose\n"
1966 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1967 "# echo 1 > /debug/tracing/tracing_enabled\n"
1968 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1969 "echo 0 > /debug/tracing/tracing_enabled\n"
1970;
1971
1972static ssize_t
1973tracing_readme_read(struct file *filp, char __user *ubuf,
1974 size_t cnt, loff_t *ppos)
1975{
1976 return simple_read_from_buffer(ubuf, cnt, ppos,
1977 readme_msg, strlen(readme_msg));
1978}
1979
1980static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001981 .open = tracing_open_generic,
1982 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001983};
1984
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001985static ssize_t
1986tracing_ctrl_read(struct file *filp, char __user *ubuf,
1987 size_t cnt, loff_t *ppos)
1988{
1989 struct trace_array *tr = filp->private_data;
1990 char buf[64];
1991 int r;
1992
1993 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001994 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001995}
1996
1997static ssize_t
1998tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1999 size_t cnt, loff_t *ppos)
2000{
2001 struct trace_array *tr = filp->private_data;
2002 long val;
2003 char buf[64];
2004
2005 if (cnt > 63)
2006 cnt = 63;
2007
2008 if (copy_from_user(&buf, ubuf, cnt))
2009 return -EFAULT;
2010
2011 buf[cnt] = 0;
2012
2013 val = simple_strtoul(buf, NULL, 10);
2014
2015 val = !!val;
2016
2017 mutex_lock(&trace_types_lock);
2018 if (tr->ctrl ^ val) {
2019 if (val)
2020 tracer_enabled = 1;
2021 else
2022 tracer_enabled = 0;
2023
2024 tr->ctrl = val;
2025
2026 if (current_trace && current_trace->ctrl_update)
2027 current_trace->ctrl_update(tr);
2028 }
2029 mutex_unlock(&trace_types_lock);
2030
2031 filp->f_pos += cnt;
2032
2033 return cnt;
2034}
2035
2036static ssize_t
2037tracing_set_trace_read(struct file *filp, char __user *ubuf,
2038 size_t cnt, loff_t *ppos)
2039{
2040 char buf[max_tracer_type_len+2];
2041 int r;
2042
2043 mutex_lock(&trace_types_lock);
2044 if (current_trace)
2045 r = sprintf(buf, "%s\n", current_trace->name);
2046 else
2047 r = sprintf(buf, "\n");
2048 mutex_unlock(&trace_types_lock);
2049
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002050 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002051}
2052
2053static ssize_t
2054tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2055 size_t cnt, loff_t *ppos)
2056{
2057 struct trace_array *tr = &global_trace;
2058 struct tracer *t;
2059 char buf[max_tracer_type_len+1];
2060 int i;
2061
2062 if (cnt > max_tracer_type_len)
2063 cnt = max_tracer_type_len;
2064
2065 if (copy_from_user(&buf, ubuf, cnt))
2066 return -EFAULT;
2067
2068 buf[cnt] = 0;
2069
2070 /* strip ending whitespace. */
2071 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2072 buf[i] = 0;
2073
2074 mutex_lock(&trace_types_lock);
2075 for (t = trace_types; t; t = t->next) {
2076 if (strcmp(t->name, buf) == 0)
2077 break;
2078 }
2079 if (!t || t == current_trace)
2080 goto out;
2081
2082 if (current_trace && current_trace->reset)
2083 current_trace->reset(tr);
2084
2085 current_trace = t;
2086 if (t->init)
2087 t->init(tr);
2088
2089 out:
2090 mutex_unlock(&trace_types_lock);
2091
2092 filp->f_pos += cnt;
2093
2094 return cnt;
2095}
2096
2097static ssize_t
2098tracing_max_lat_read(struct file *filp, char __user *ubuf,
2099 size_t cnt, loff_t *ppos)
2100{
2101 unsigned long *ptr = filp->private_data;
2102 char buf[64];
2103 int r;
2104
2105 r = snprintf(buf, 64, "%ld\n",
2106 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2107 if (r > 64)
2108 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002109 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002110}
2111
2112static ssize_t
2113tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2114 size_t cnt, loff_t *ppos)
2115{
2116 long *ptr = filp->private_data;
2117 long val;
2118 char buf[64];
2119
2120 if (cnt > 63)
2121 cnt = 63;
2122
2123 if (copy_from_user(&buf, ubuf, cnt))
2124 return -EFAULT;
2125
2126 buf[cnt] = 0;
2127
2128 val = simple_strtoul(buf, NULL, 10);
2129
2130 *ptr = val * 1000;
2131
2132 return cnt;
2133}
2134
Steven Rostedtb3806b42008-05-12 21:20:46 +02002135static atomic_t tracing_reader;
2136
2137static int tracing_open_pipe(struct inode *inode, struct file *filp)
2138{
2139 struct trace_iterator *iter;
2140
2141 if (tracing_disabled)
2142 return -ENODEV;
2143
2144 /* We only allow for reader of the pipe */
2145 if (atomic_inc_return(&tracing_reader) != 1) {
2146 atomic_dec(&tracing_reader);
2147 return -EBUSY;
2148 }
2149
2150 /* create a buffer to store the information to pass to userspace */
2151 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2152 if (!iter)
2153 return -ENOMEM;
2154
2155 iter->tr = &global_trace;
Thomas Gleixner72829bc2008-05-23 21:37:28 +02002156 iter->trace = current_trace;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002157
2158 filp->private_data = iter;
2159
2160 return 0;
2161}
2162
2163static int tracing_release_pipe(struct inode *inode, struct file *file)
2164{
2165 struct trace_iterator *iter = file->private_data;
2166
2167 kfree(iter);
2168 atomic_dec(&tracing_reader);
2169
2170 return 0;
2171}
2172
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002173static unsigned int
2174tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2175{
2176 struct trace_iterator *iter = filp->private_data;
2177
2178 if (trace_flags & TRACE_ITER_BLOCK) {
2179 /*
2180 * Always select as readable when in blocking mode
2181 */
2182 return POLLIN | POLLRDNORM;
2183 }
2184 else {
2185 if (!trace_empty(iter))
2186 return POLLIN | POLLRDNORM;
2187 poll_wait(filp, &trace_wait, poll_table);
2188 if (!trace_empty(iter))
2189 return POLLIN | POLLRDNORM;
2190
2191 return 0;
2192 }
2193}
2194
Steven Rostedtb3806b42008-05-12 21:20:46 +02002195/*
2196 * Consumer reader.
2197 */
2198static ssize_t
2199tracing_read_pipe(struct file *filp, char __user *ubuf,
2200 size_t cnt, loff_t *ppos)
2201{
2202 struct trace_iterator *iter = filp->private_data;
2203 struct trace_array_cpu *data;
2204 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002205 static int start;
2206 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002207#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002208 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002209#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002210 int read = 0;
2211 int cpu;
2212 int len;
2213 int ret;
2214
2215 /* return any leftover data */
2216 if (iter->seq.len > start) {
2217 len = iter->seq.len - start;
2218 if (cnt > len)
2219 cnt = len;
2220 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2221 if (ret)
2222 cnt = -EFAULT;
2223
2224 start += len;
2225
2226 return cnt;
2227 }
2228
2229 trace_seq_reset(&iter->seq);
2230 start = 0;
2231
2232 while (trace_empty(iter)) {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002233 if (!(trace_flags & TRACE_ITER_BLOCK))
2234 return -EWOULDBLOCK;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002235 /*
2236 * This is a make-shift waitqueue. The reason we don't use
2237 * an actual wait queue is because:
2238 * 1) we only ever have one waiter
2239 * 2) the tracing, traces all functions, we don't want
2240 * the overhead of calling wake_up and friends
2241 * (and tracing them too)
2242 * Anyway, this is really very primitive wakeup.
2243 */
2244 set_current_state(TASK_INTERRUPTIBLE);
2245 iter->tr->waiter = current;
2246
2247 /* sleep for one second, and try again. */
2248 schedule_timeout(HZ);
2249
2250 iter->tr->waiter = NULL;
2251
2252 if (signal_pending(current))
2253 return -EINTR;
2254
2255 /*
2256 * We block until we read something and tracing is disabled.
2257 * We still block if tracing is disabled, but we have never
2258 * read anything. This allows a user to cat this file, and
2259 * then enable tracing. But after we have read something,
2260 * we give an EOF when tracing is again disabled.
2261 *
2262 * iter->pos will be 0 if we haven't read anything.
2263 */
2264 if (!tracer_enabled && iter->pos)
2265 break;
2266
2267 continue;
2268 }
2269
2270 /* stop when tracing is finished */
2271 if (trace_empty(iter))
2272 return 0;
2273
2274 if (cnt >= PAGE_SIZE)
2275 cnt = PAGE_SIZE - 1;
2276
2277 memset(iter, 0, sizeof(*iter));
2278 iter->tr = &global_trace;
2279 iter->pos = -1;
2280
2281 /*
2282 * We need to stop all tracing on all CPUS to read the
2283 * the next buffer. This is a bit expensive, but is
2284 * not done often. We fill all what we can read,
2285 * and then release the locks again.
2286 */
2287
2288 cpus_clear(mask);
2289 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002290#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002291 ftrace_save = ftrace_enabled;
2292 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002293#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002294 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002295 for_each_possible_cpu(cpu) {
2296 data = iter->tr->data[cpu];
2297
2298 if (!head_page(data) || !data->trace_idx)
2299 continue;
2300
2301 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002302 cpu_set(cpu, mask);
2303 }
2304
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002305 for_each_cpu_mask(cpu, mask) {
2306 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002307 __raw_spin_lock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002308 }
2309
Steven Rostedt088b1e42008-05-12 21:20:48 +02002310 while (find_next_entry_inc(iter) != NULL) {
2311 int len = iter->seq.len;
2312
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002313 ret = print_trace_line(iter);
Steven Rostedt088b1e42008-05-12 21:20:48 +02002314 if (!ret) {
2315 /* don't print partial lines */
2316 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002317 break;
Steven Rostedt088b1e42008-05-12 21:20:48 +02002318 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002319
2320 trace_consume(iter);
2321
2322 if (iter->seq.len >= cnt)
2323 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002324 }
2325
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002326 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002327 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002328 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002329 }
2330
2331 for_each_cpu_mask(cpu, mask) {
2332 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002333 atomic_dec(&data->disabled);
2334 }
Ingo Molnar25770462008-05-12 21:20:49 +02002335#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002336 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002337#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002338 local_irq_restore(flags);
2339
2340 /* Now copy what we have to the user */
2341 read = iter->seq.len;
2342 if (read > cnt)
2343 read = cnt;
2344
2345 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2346
2347 if (read < iter->seq.len)
2348 start = read;
2349 else
2350 trace_seq_reset(&iter->seq);
2351
2352 if (ret)
2353 read = -EFAULT;
2354
2355 return read;
2356}
2357
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002358static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002359 .open = tracing_open_generic,
2360 .read = tracing_max_lat_read,
2361 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002362};
2363
2364static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002365 .open = tracing_open_generic,
2366 .read = tracing_ctrl_read,
2367 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002368};
2369
2370static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002371 .open = tracing_open_generic,
2372 .read = tracing_set_trace_read,
2373 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002374};
2375
Steven Rostedtb3806b42008-05-12 21:20:46 +02002376static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002377 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002378 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002379 .read = tracing_read_pipe,
2380 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002381};
2382
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002383#ifdef CONFIG_DYNAMIC_FTRACE
2384
2385static ssize_t
2386tracing_read_long(struct file *filp, char __user *ubuf,
2387 size_t cnt, loff_t *ppos)
2388{
2389 unsigned long *p = filp->private_data;
2390 char buf[64];
2391 int r;
2392
2393 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002394
2395 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002396}
2397
2398static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002399 .open = tracing_open_generic,
2400 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002401};
2402#endif
2403
2404static struct dentry *d_tracer;
2405
2406struct dentry *tracing_init_dentry(void)
2407{
2408 static int once;
2409
2410 if (d_tracer)
2411 return d_tracer;
2412
2413 d_tracer = debugfs_create_dir("tracing", NULL);
2414
2415 if (!d_tracer && !once) {
2416 once = 1;
2417 pr_warning("Could not create debugfs directory 'tracing'\n");
2418 return NULL;
2419 }
2420
2421 return d_tracer;
2422}
2423
Steven Rostedt60a11772008-05-12 21:20:44 +02002424#ifdef CONFIG_FTRACE_SELFTEST
2425/* Let selftest have access to static functions in this file */
2426#include "trace_selftest.c"
2427#endif
2428
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002429static __init void tracer_init_debugfs(void)
2430{
2431 struct dentry *d_tracer;
2432 struct dentry *entry;
2433
2434 d_tracer = tracing_init_dentry();
2435
2436 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2437 &global_trace, &tracing_ctrl_fops);
2438 if (!entry)
2439 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2440
2441 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2442 NULL, &tracing_iter_fops);
2443 if (!entry)
2444 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2445
Ingo Molnarc7078de2008-05-12 21:20:52 +02002446 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2447 NULL, &tracing_cpumask_fops);
2448 if (!entry)
2449 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2450
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002451 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2452 &global_trace, &tracing_lt_fops);
2453 if (!entry)
2454 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2455
2456 entry = debugfs_create_file("trace", 0444, d_tracer,
2457 &global_trace, &tracing_fops);
2458 if (!entry)
2459 pr_warning("Could not create debugfs 'trace' entry\n");
2460
2461 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2462 &global_trace, &show_traces_fops);
2463 if (!entry)
2464 pr_warning("Could not create debugfs 'trace' entry\n");
2465
2466 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2467 &global_trace, &set_tracer_fops);
2468 if (!entry)
2469 pr_warning("Could not create debugfs 'trace' entry\n");
2470
2471 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2472 &tracing_max_latency,
2473 &tracing_max_lat_fops);
2474 if (!entry)
2475 pr_warning("Could not create debugfs "
2476 "'tracing_max_latency' entry\n");
2477
2478 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2479 &tracing_thresh, &tracing_max_lat_fops);
2480 if (!entry)
2481 pr_warning("Could not create debugfs "
2482 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002483 entry = debugfs_create_file("README", 0644, d_tracer,
2484 NULL, &tracing_readme_fops);
2485 if (!entry)
2486 pr_warning("Could not create debugfs 'README' entry\n");
2487
Steven Rostedtb3806b42008-05-12 21:20:46 +02002488 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2489 NULL, &tracing_pipe_fops);
2490 if (!entry)
2491 pr_warning("Could not create debugfs "
2492 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002493
2494#ifdef CONFIG_DYNAMIC_FTRACE
2495 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2496 &ftrace_update_tot_cnt,
2497 &tracing_read_long_fops);
2498 if (!entry)
2499 pr_warning("Could not create debugfs "
2500 "'dyn_ftrace_total_info' entry\n");
2501#endif
2502}
2503
2504/* dummy trace to disable tracing */
2505static struct tracer no_tracer __read_mostly =
2506{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002507 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002508};
2509
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002510static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002511{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002512 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002513 struct page *page, *tmp;
2514 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002515 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002516 int i;
2517
2518 /* first allocate a page for each CPU */
2519 for_each_possible_cpu(i) {
2520 array = (void *)__get_free_page(GFP_KERNEL);
2521 if (array == NULL) {
2522 printk(KERN_ERR "tracer: failed to allocate page"
2523 "for trace buffer!\n");
2524 goto free_pages;
2525 }
2526
2527 page = virt_to_page(array);
2528 list_add(&page->lru, &pages);
2529
2530/* Only allocate if we are actually using the max trace */
2531#ifdef CONFIG_TRACER_MAX_TRACE
2532 array = (void *)__get_free_page(GFP_KERNEL);
2533 if (array == NULL) {
2534 printk(KERN_ERR "tracer: failed to allocate page"
2535 "for trace buffer!\n");
2536 goto free_pages;
2537 }
2538 page = virt_to_page(array);
2539 list_add(&page->lru, &pages);
2540#endif
2541 }
2542
2543 /* Now that we successfully allocate a page per CPU, add them */
2544 for_each_possible_cpu(i) {
2545 data = global_trace.data[i];
Steven Rostedt92205c22008-05-12 21:20:55 +02002546 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002547 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002548 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002549 list_add_tail(&page->lru, &data->trace_pages);
2550 ClearPageLRU(page);
2551
2552#ifdef CONFIG_TRACER_MAX_TRACE
2553 data = max_tr.data[i];
Steven Rostedt92205c22008-05-12 21:20:55 +02002554 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002555 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002556 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002557 list_add_tail(&page->lru, &data->trace_pages);
2558 SetPageLRU(page);
2559#endif
2560 }
2561 global_trace.entries += ENTRIES_PER_PAGE;
2562
2563 return 0;
2564
2565 free_pages:
2566 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002567 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002568 __free_page(page);
2569 }
2570 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002571}
2572
2573__init static int tracer_alloc_buffers(void)
2574{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002575 struct trace_array_cpu *data;
2576 void *array;
2577 struct page *page;
2578 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002579 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002580 int i;
2581
Steven Rostedt26994ea2008-05-12 21:20:48 +02002582 global_trace.ctrl = tracer_enabled;
2583
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002584 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002585 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002586 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002587 max_tr.data[i] = &per_cpu(max_data, i);
2588
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002589 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002590 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002591 printk(KERN_ERR "tracer: failed to allocate page"
2592 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002593 goto free_buffers;
2594 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002595
2596 /* set the array to the list */
2597 INIT_LIST_HEAD(&data->trace_pages);
2598 page = virt_to_page(array);
2599 list_add(&page->lru, &data->trace_pages);
2600 /* use the LRU flag to differentiate the two buffers */
2601 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002602
2603/* Only allocate if we are actually using the max trace */
2604#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002605 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002606 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002607 printk(KERN_ERR "tracer: failed to allocate page"
2608 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002609 goto free_buffers;
2610 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002611
2612 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2613 page = virt_to_page(array);
2614 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2615 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002616#endif
2617 }
2618
2619 /*
2620 * Since we allocate by orders of pages, we may be able to
2621 * round up a bit.
2622 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002623 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002624 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002625
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002626 while (global_trace.entries < trace_nr_entries) {
2627 if (trace_alloc_page())
2628 break;
2629 pages++;
2630 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002631 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002632
2633 pr_info("tracer: %d pages allocated for %ld",
2634 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002635 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2636 pr_info(" actual entries %ld\n", global_trace.entries);
2637
2638 tracer_init_debugfs();
2639
2640 trace_init_cmdlines();
2641
2642 register_tracer(&no_tracer);
2643 current_trace = &no_tracer;
2644
Steven Rostedt60a11772008-05-12 21:20:44 +02002645 /* All seems OK, enable tracing */
2646 tracing_disabled = 0;
2647
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002648 return 0;
2649
2650 free_buffers:
2651 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002652 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002653 struct trace_array_cpu *data = global_trace.data[i];
2654
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002655 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002656 list_for_each_entry_safe(page, tmp,
2657 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002658 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002659 __free_page(page);
2660 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002661 }
2662
2663#ifdef CONFIG_TRACER_MAX_TRACE
2664 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002665 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002666 list_for_each_entry_safe(page, tmp,
2667 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002668 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002669 __free_page(page);
2670 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002671 }
2672#endif
2673 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002674 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002675}
Steven Rostedt60a11772008-05-12 21:20:44 +02002676fs_initcall(tracer_alloc_buffers);