blob: ea11f4ebfae1c8e6c9e3b34f4b9d4feecda27fef [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
Ingo Molnare309b412008-05-12 21:20:51 +020040static long
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_type {
100 __TRACE_FIRST_TYPE = 0,
101
102 TRACE_FN,
103 TRACE_CTX,
Ingo Molnar57422792008-05-12 21:20:51 +0200104 TRACE_WAKE,
Ingo Molnar86387f72008-05-12 21:20:51 +0200105 TRACE_STACK,
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200106 TRACE_SPECIAL,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200107
108 __TRACE_LAST_TYPE
109};
110
111enum trace_flag_type {
112 TRACE_FLAG_IRQS_OFF = 0x01,
113 TRACE_FLAG_NEED_RESCHED = 0x02,
114 TRACE_FLAG_HARDIRQ = 0x04,
115 TRACE_FLAG_SOFTIRQ = 0x08,
116};
117
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200118#define TRACE_ITER_SYM_MASK \
119 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
120
121/* These must match the bit postions above */
122static const char *trace_options[] = {
123 "print-parent",
124 "sym-offset",
125 "sym-addr",
126 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200127 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200128 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200129 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200130 "block",
Ingo Molnar86387f72008-05-12 21:20:51 +0200131 "stacktrace",
Ingo Molnar4ac3ba42008-05-12 21:20:52 +0200132 "sched-tree",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200133 NULL
134};
135
Steven Rostedt92205c22008-05-12 21:20:55 +0200136static raw_spinlock_t ftrace_max_lock =
137 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200138
139/*
140 * Copy the new maximum trace into the separate maximum-trace
141 * structure. (this way the maximum trace is permanently saved,
142 * for later retrieval via /debugfs/tracing/latency_trace)
143 */
Ingo Molnare309b412008-05-12 21:20:51 +0200144static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200145__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
146{
147 struct trace_array_cpu *data = tr->data[cpu];
148
149 max_tr.cpu = cpu;
150 max_tr.time_start = data->preempt_timestamp;
151
152 data = max_tr.data[cpu];
153 data->saved_latency = tracing_max_latency;
154
155 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
156 data->pid = tsk->pid;
157 data->uid = tsk->uid;
158 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
159 data->policy = tsk->policy;
160 data->rt_priority = tsk->rt_priority;
161
162 /* record this tasks comm */
163 tracing_record_cmdline(current);
164}
165
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200166void check_pages(struct trace_array_cpu *data)
167{
168 struct page *page, *tmp;
169
170 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
171 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
172
173 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
174 BUG_ON(page->lru.next->prev != &page->lru);
175 BUG_ON(page->lru.prev->next != &page->lru);
176 }
177}
178
179void *head_page(struct trace_array_cpu *data)
180{
181 struct page *page;
182
183 check_pages(data);
184 if (list_empty(&data->trace_pages))
185 return NULL;
186
187 page = list_entry(data->trace_pages.next, struct page, lru);
188 BUG_ON(&page->lru == &data->trace_pages);
189
190 return page_address(page);
191}
192
Ingo Molnare309b412008-05-12 21:20:51 +0200193static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200194trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
195{
196 int len = (PAGE_SIZE - 1) - s->len;
197 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200198 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200199
200 if (!len)
201 return 0;
202
203 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200204 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200205 va_end(ap);
206
Steven Rostedtb3806b42008-05-12 21:20:46 +0200207 /* If we can't write it all, don't bother writing anything */
208 if (ret > len)
209 return 0;
210
211 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200212
213 return len;
214}
215
Ingo Molnare309b412008-05-12 21:20:51 +0200216static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200217trace_seq_puts(struct trace_seq *s, const char *str)
218{
219 int len = strlen(str);
220
221 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200222 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200223
224 memcpy(s->buffer + s->len, str, len);
225 s->len += len;
226
227 return len;
228}
229
Ingo Molnare309b412008-05-12 21:20:51 +0200230static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200231trace_seq_putc(struct trace_seq *s, unsigned char c)
232{
233 if (s->len >= (PAGE_SIZE - 1))
234 return 0;
235
236 s->buffer[s->len++] = c;
237
238 return 1;
239}
240
Ingo Molnare309b412008-05-12 21:20:51 +0200241static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200242trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
243{
244 if (len > ((PAGE_SIZE - 1) - s->len))
245 return 0;
246
247 memcpy(s->buffer + s->len, mem, len);
248 s->len += len;
249
250 return len;
251}
252
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200253#define HEX_CHARS 17
254
Ingo Molnare309b412008-05-12 21:20:51 +0200255static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200256trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
257{
258 unsigned char hex[HEX_CHARS];
259 unsigned char *data;
260 unsigned char byte;
261 int i, j;
262
263 BUG_ON(len >= HEX_CHARS);
264
265 data = mem;
266
267#ifdef __BIG_ENDIAN
268 for (i = 0, j = 0; i < len; i++) {
269#else
270 for (i = len-1, j = 0; i >= 0; i--) {
271#endif
272 byte = data[i];
273
274 hex[j] = byte & 0x0f;
275 if (hex[j] >= 10)
276 hex[j] += 'a' - 10;
277 else
278 hex[j] += '0';
279 j++;
280
281 hex[j] = byte >> 4;
282 if (hex[j] >= 10)
283 hex[j] += 'a' - 10;
284 else
285 hex[j] += '0';
286 j++;
287 }
288 hex[j] = ' ';
289 j++;
290
291 return trace_seq_putmem(s, hex, j);
292}
293
Ingo Molnare309b412008-05-12 21:20:51 +0200294static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200295trace_seq_reset(struct trace_seq *s)
296{
297 s->len = 0;
298}
299
Ingo Molnare309b412008-05-12 21:20:51 +0200300static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200301trace_print_seq(struct seq_file *m, struct trace_seq *s)
302{
303 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
304
305 s->buffer[len] = 0;
306 seq_puts(m, s->buffer);
307
308 trace_seq_reset(s);
309}
310
Ingo Molnare309b412008-05-12 21:20:51 +0200311static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200312flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
313{
314 struct list_head flip_pages;
315
316 INIT_LIST_HEAD(&flip_pages);
317
Steven Rostedt93a588f2008-05-12 21:20:45 +0200318 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200319 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200320 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200321
322 check_pages(tr1);
323 check_pages(tr2);
324 list_splice_init(&tr1->trace_pages, &flip_pages);
325 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
326 list_splice_init(&flip_pages, &tr2->trace_pages);
327 BUG_ON(!list_empty(&flip_pages));
328 check_pages(tr1);
329 check_pages(tr2);
330}
331
Ingo Molnare309b412008-05-12 21:20:51 +0200332void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200333update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334{
335 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200336 int i;
337
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200338 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200339 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200340 /* clear out all the previous traces */
341 for_each_possible_cpu(i) {
342 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200343 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200344 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200345 }
346
347 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200348 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200349}
350
351/**
352 * update_max_tr_single - only copy one trace over, and reset the rest
353 * @tr - tracer
354 * @tsk - task with the latency
355 * @cpu - the cpu of the buffer to copy.
356 */
Ingo Molnare309b412008-05-12 21:20:51 +0200357void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200358update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
359{
360 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200361 int i;
362
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200363 WARN_ON_ONCE(!irqs_disabled());
Steven Rostedt92205c22008-05-12 21:20:55 +0200364 __raw_spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200365 for_each_possible_cpu(i)
366 tracing_reset(max_tr.data[i]);
367
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200368 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200369 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200370
371 __update_max_tr(tr, tsk, cpu);
Steven Rostedt92205c22008-05-12 21:20:55 +0200372 __raw_spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200373}
374
375int register_tracer(struct tracer *type)
376{
377 struct tracer *t;
378 int len;
379 int ret = 0;
380
381 if (!type->name) {
382 pr_info("Tracer must have a name\n");
383 return -1;
384 }
385
386 mutex_lock(&trace_types_lock);
387 for (t = trace_types; t; t = t->next) {
388 if (strcmp(type->name, t->name) == 0) {
389 /* already found */
390 pr_info("Trace %s already registered\n",
391 type->name);
392 ret = -1;
393 goto out;
394 }
395 }
396
Steven Rostedt60a11772008-05-12 21:20:44 +0200397#ifdef CONFIG_FTRACE_STARTUP_TEST
398 if (type->selftest) {
399 struct tracer *saved_tracer = current_trace;
400 struct trace_array_cpu *data;
401 struct trace_array *tr = &global_trace;
402 int saved_ctrl = tr->ctrl;
403 int i;
404 /*
405 * Run a selftest on this tracer.
406 * Here we reset the trace buffer, and set the current
407 * tracer to be this tracer. The tracer can then run some
408 * internal tracing to verify that everything is in order.
409 * If we fail, we do not register this tracer.
410 */
411 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200412 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200413 if (!head_page(data))
414 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200415 tracing_reset(data);
416 }
417 current_trace = type;
418 tr->ctrl = 0;
419 /* the test is responsible for initializing and enabling */
420 pr_info("Testing tracer %s: ", type->name);
421 ret = type->selftest(type, tr);
422 /* the test is responsible for resetting too */
423 current_trace = saved_tracer;
424 tr->ctrl = saved_ctrl;
425 if (ret) {
426 printk(KERN_CONT "FAILED!\n");
427 goto out;
428 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200429 /* Only reset on passing, to avoid touching corrupted buffers */
430 for_each_possible_cpu(i) {
431 data = tr->data[i];
432 if (!head_page(data))
433 continue;
434 tracing_reset(data);
435 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200436 printk(KERN_CONT "PASSED\n");
437 }
438#endif
439
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200440 type->next = trace_types;
441 trace_types = type;
442 len = strlen(type->name);
443 if (len > max_tracer_type_len)
444 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200445
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200446 out:
447 mutex_unlock(&trace_types_lock);
448
449 return ret;
450}
451
452void unregister_tracer(struct tracer *type)
453{
454 struct tracer **t;
455 int len;
456
457 mutex_lock(&trace_types_lock);
458 for (t = &trace_types; *t; t = &(*t)->next) {
459 if (*t == type)
460 goto found;
461 }
462 pr_info("Trace %s not registered\n", type->name);
463 goto out;
464
465 found:
466 *t = (*t)->next;
467 if (strlen(type->name) != max_tracer_type_len)
468 goto out;
469
470 max_tracer_type_len = 0;
471 for (t = &trace_types; *t; t = &(*t)->next) {
472 len = strlen((*t)->name);
473 if (len > max_tracer_type_len)
474 max_tracer_type_len = len;
475 }
476 out:
477 mutex_unlock(&trace_types_lock);
478}
479
Ingo Molnare309b412008-05-12 21:20:51 +0200480void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200481{
482 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200483 data->trace_head = data->trace_tail = head_page(data);
484 data->trace_head_idx = 0;
485 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200486}
487
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200488#define SAVED_CMDLINES 128
489static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
490static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
491static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
492static int cmdline_idx;
493static DEFINE_SPINLOCK(trace_cmdline_lock);
494atomic_t trace_record_cmdline_disabled;
495
496static void trace_init_cmdlines(void)
497{
498 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
499 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
500 cmdline_idx = 0;
501}
502
Ingo Molnare309b412008-05-12 21:20:51 +0200503void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200504
Ingo Molnare309b412008-05-12 21:20:51 +0200505static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200506{
507 unsigned map;
508 unsigned idx;
509
510 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
511 return;
512
513 /*
514 * It's not the end of the world if we don't get
515 * the lock, but we also don't want to spin
516 * nor do we want to disable interrupts,
517 * so if we miss here, then better luck next time.
518 */
519 if (!spin_trylock(&trace_cmdline_lock))
520 return;
521
522 idx = map_pid_to_cmdline[tsk->pid];
523 if (idx >= SAVED_CMDLINES) {
524 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
525
526 map = map_cmdline_to_pid[idx];
527 if (map <= PID_MAX_DEFAULT)
528 map_pid_to_cmdline[map] = (unsigned)-1;
529
530 map_pid_to_cmdline[tsk->pid] = idx;
531
532 cmdline_idx = idx;
533 }
534
535 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
536
537 spin_unlock(&trace_cmdline_lock);
538}
539
Ingo Molnare309b412008-05-12 21:20:51 +0200540static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200541{
542 char *cmdline = "<...>";
543 unsigned map;
544
545 if (!pid)
546 return "<idle>";
547
548 if (pid > PID_MAX_DEFAULT)
549 goto out;
550
551 map = map_pid_to_cmdline[pid];
552 if (map >= SAVED_CMDLINES)
553 goto out;
554
555 cmdline = saved_cmdlines[map];
556
557 out:
558 return cmdline;
559}
560
Ingo Molnare309b412008-05-12 21:20:51 +0200561void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200562{
563 if (atomic_read(&trace_record_cmdline_disabled))
564 return;
565
566 trace_save_cmdline(tsk);
567}
568
Ingo Molnare309b412008-05-12 21:20:51 +0200569static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200570trace_next_list(struct trace_array_cpu *data, struct list_head *next)
571{
572 /*
573 * Roundrobin - but skip the head (which is not a real page):
574 */
575 next = next->next;
576 if (unlikely(next == &data->trace_pages))
577 next = next->next;
578 BUG_ON(next == &data->trace_pages);
579
580 return next;
581}
582
Ingo Molnare309b412008-05-12 21:20:51 +0200583static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200584trace_next_page(struct trace_array_cpu *data, void *addr)
585{
586 struct list_head *next;
587 struct page *page;
588
589 page = virt_to_page(addr);
590
591 next = trace_next_list(data, &page->lru);
592 page = list_entry(next, struct page, lru);
593
594 return page_address(page);
595}
596
Ingo Molnare309b412008-05-12 21:20:51 +0200597static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200598tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200599{
600 unsigned long idx, idx_next;
601 struct trace_entry *entry;
602
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200603 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200604 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200605 idx_next = idx + 1;
606
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200607 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
608
Steven Rostedt93a588f2008-05-12 21:20:45 +0200609 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200610
611 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200612 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200613 idx_next = 0;
614 }
615
Steven Rostedt93a588f2008-05-12 21:20:45 +0200616 if (data->trace_head == data->trace_tail &&
617 idx_next == data->trace_tail_idx) {
618 /* overrun */
619 data->trace_tail_idx++;
620 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
621 data->trace_tail =
622 trace_next_page(data, data->trace_tail);
623 data->trace_tail_idx = 0;
624 }
625 }
626
627 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200628
629 return entry;
630}
631
Ingo Molnare309b412008-05-12 21:20:51 +0200632static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200633tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200634{
635 struct task_struct *tsk = current;
636 unsigned long pc;
637
638 pc = preempt_count();
639
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200640 entry->preempt_count = pc & 0xff;
641 entry->pid = tsk->pid;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200642 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200643 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
644 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
645 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
646 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
647}
648
Ingo Molnare309b412008-05-12 21:20:51 +0200649void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200650trace_function(struct trace_array *tr, struct trace_array_cpu *data,
651 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652{
653 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200654 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200655
Steven Rostedt92205c22008-05-12 21:20:55 +0200656 raw_local_irq_save(irq_flags);
657 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200658 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200659 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200660 entry->type = TRACE_FN;
661 entry->fn.ip = ip;
662 entry->fn.parent_ip = parent_ip;
Steven Rostedt92205c22008-05-12 21:20:55 +0200663 __raw_spin_unlock(&data->lock);
664 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200665}
666
Ingo Molnare309b412008-05-12 21:20:51 +0200667void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200668ftrace(struct trace_array *tr, struct trace_array_cpu *data,
669 unsigned long ip, unsigned long parent_ip, unsigned long flags)
670{
671 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200672 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200673}
674
Ingo Molnare309b412008-05-12 21:20:51 +0200675void
Ingo Molnar4e655512008-05-12 21:20:52 +0200676__trace_special(void *__tr, void *__data,
677 unsigned long arg1, unsigned long arg2, unsigned long arg3)
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200678{
Ingo Molnar4e655512008-05-12 21:20:52 +0200679 struct trace_array_cpu *data = __data;
680 struct trace_array *tr = __tr;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200681 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200682 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200683
Steven Rostedt92205c22008-05-12 21:20:55 +0200684 raw_local_irq_save(irq_flags);
685 __raw_spin_lock(&data->lock);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200686 entry = tracing_get_trace_entry(tr, data);
687 tracing_generic_entry_update(entry, 0);
688 entry->type = TRACE_SPECIAL;
689 entry->special.arg1 = arg1;
690 entry->special.arg2 = arg2;
691 entry->special.arg3 = arg3;
Steven Rostedt92205c22008-05-12 21:20:55 +0200692 __raw_spin_unlock(&data->lock);
693 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200694
695 trace_wake_up();
Ingo Molnar86387f72008-05-12 21:20:51 +0200696}
697
698void __trace_stack(struct trace_array *tr,
699 struct trace_array_cpu *data,
700 unsigned long flags,
701 int skip)
702{
703 struct trace_entry *entry;
704 struct stack_trace trace;
705
706 if (!(trace_flags & TRACE_ITER_STACKTRACE))
707 return;
708
709 entry = tracing_get_trace_entry(tr, data);
710 tracing_generic_entry_update(entry, flags);
711 entry->type = TRACE_STACK;
712
713 memset(&entry->stack, 0, sizeof(entry->stack));
714
715 trace.nr_entries = 0;
716 trace.max_entries = FTRACE_STACK_ENTRIES;
717 trace.skip = skip;
718 trace.entries = entry->stack.caller;
719
720 save_stack_trace(&trace);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200721}
722
Ingo Molnare309b412008-05-12 21:20:51 +0200723void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200724tracing_sched_switch_trace(struct trace_array *tr,
725 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200726 struct task_struct *prev,
727 struct task_struct *next,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200728 unsigned long flags)
729{
730 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200731 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200732
Steven Rostedt92205c22008-05-12 21:20:55 +0200733 raw_local_irq_save(irq_flags);
734 __raw_spin_lock(&data->lock);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200735 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200736 tracing_generic_entry_update(entry, flags);
737 entry->type = TRACE_CTX;
738 entry->ctx.prev_pid = prev->pid;
739 entry->ctx.prev_prio = prev->prio;
740 entry->ctx.prev_state = prev->state;
741 entry->ctx.next_pid = next->pid;
742 entry->ctx.next_prio = next->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200743 entry->ctx.next_state = next->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200744 __trace_stack(tr, data, flags, 4);
Steven Rostedt92205c22008-05-12 21:20:55 +0200745 __raw_spin_unlock(&data->lock);
746 raw_local_irq_restore(irq_flags);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200747}
748
Ingo Molnar57422792008-05-12 21:20:51 +0200749void
750tracing_sched_wakeup_trace(struct trace_array *tr,
751 struct trace_array_cpu *data,
Ingo Molnar86387f72008-05-12 21:20:51 +0200752 struct task_struct *wakee,
753 struct task_struct *curr,
Ingo Molnar57422792008-05-12 21:20:51 +0200754 unsigned long flags)
755{
756 struct trace_entry *entry;
757 unsigned long irq_flags;
758
Steven Rostedt92205c22008-05-12 21:20:55 +0200759 raw_local_irq_save(irq_flags);
760 __raw_spin_lock(&data->lock);
Ingo Molnar57422792008-05-12 21:20:51 +0200761 entry = tracing_get_trace_entry(tr, data);
762 tracing_generic_entry_update(entry, flags);
763 entry->type = TRACE_WAKE;
764 entry->ctx.prev_pid = curr->pid;
765 entry->ctx.prev_prio = curr->prio;
766 entry->ctx.prev_state = curr->state;
767 entry->ctx.next_pid = wakee->pid;
768 entry->ctx.next_prio = wakee->prio;
Peter Zijlstrabac524d2008-05-12 21:20:53 +0200769 entry->ctx.next_state = wakee->state;
Ingo Molnar86387f72008-05-12 21:20:51 +0200770 __trace_stack(tr, data, flags, 5);
Steven Rostedt92205c22008-05-12 21:20:55 +0200771 __raw_spin_unlock(&data->lock);
772 raw_local_irq_restore(irq_flags);
Ingo Molnar017730c2008-05-12 21:20:52 +0200773
774 trace_wake_up();
Ingo Molnar57422792008-05-12 21:20:51 +0200775}
776
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200777#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200778static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200779function_trace_call(unsigned long ip, unsigned long parent_ip)
780{
781 struct trace_array *tr = &global_trace;
782 struct trace_array_cpu *data;
783 unsigned long flags;
784 long disabled;
785 int cpu;
786
787 if (unlikely(!tracer_enabled))
788 return;
789
790 local_irq_save(flags);
791 cpu = raw_smp_processor_id();
792 data = tr->data[cpu];
793 disabled = atomic_inc_return(&data->disabled);
794
795 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200796 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200797
798 atomic_dec(&data->disabled);
799 local_irq_restore(flags);
800}
801
802static struct ftrace_ops trace_ops __read_mostly =
803{
804 .func = function_trace_call,
805};
806
Ingo Molnare309b412008-05-12 21:20:51 +0200807void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200808{
809 register_ftrace_function(&trace_ops);
810}
811
Ingo Molnare309b412008-05-12 21:20:51 +0200812void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200813{
814 unregister_ftrace_function(&trace_ops);
815}
816#endif
817
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200818enum trace_file_type {
819 TRACE_FILE_LAT_FMT = 1,
820};
821
822static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200823trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
824 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200825{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200826 struct page *page;
827 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200828
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200829 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200830 iter->next_idx[cpu] >= data->trace_idx ||
831 (data->trace_head == data->trace_tail &&
832 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200833 return NULL;
834
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200835 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200836 /* Initialize the iterator for this cpu trace buffer */
837 WARN_ON(!data->trace_tail);
838 page = virt_to_page(data->trace_tail);
839 iter->next_page[cpu] = &page->lru;
840 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200841 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200842
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200843 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200844 BUG_ON(&data->trace_pages == &page->lru);
845
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200846 array = page_address(page);
847
Steven Rostedt93a588f2008-05-12 21:20:45 +0200848 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200849 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200850}
851
Ingo Molnare309b412008-05-12 21:20:51 +0200852static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200853find_next_entry(struct trace_iterator *iter, int *ent_cpu)
854{
855 struct trace_array *tr = iter->tr;
856 struct trace_entry *ent, *next = NULL;
857 int next_cpu = -1;
858 int cpu;
859
860 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200861 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200862 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200863 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200864 /*
865 * Pick the entry with the smallest timestamp:
866 */
867 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200868 next = ent;
869 next_cpu = cpu;
870 }
871 }
872
873 if (ent_cpu)
874 *ent_cpu = next_cpu;
875
876 return next;
877}
878
Ingo Molnare309b412008-05-12 21:20:51 +0200879static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200880{
881 iter->idx++;
882 iter->next_idx[iter->cpu]++;
883 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200884
Steven Rostedtb3806b42008-05-12 21:20:46 +0200885 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
886 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
887
888 iter->next_page_idx[iter->cpu] = 0;
889 iter->next_page[iter->cpu] =
890 trace_next_list(data, iter->next_page[iter->cpu]);
891 }
892}
893
Ingo Molnare309b412008-05-12 21:20:51 +0200894static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200895{
896 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
897
898 data->trace_tail_idx++;
899 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
900 data->trace_tail = trace_next_page(data, data->trace_tail);
901 data->trace_tail_idx = 0;
902 }
903
904 /* Check if we empty it, then reset the index */
905 if (data->trace_head == data->trace_tail &&
906 data->trace_head_idx == data->trace_tail_idx)
907 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200908}
909
Ingo Molnare309b412008-05-12 21:20:51 +0200910static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200911{
912 struct trace_entry *next;
913 int next_cpu = -1;
914
915 next = find_next_entry(iter, &next_cpu);
916
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200917 iter->prev_ent = iter->ent;
918 iter->prev_cpu = iter->cpu;
919
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200920 iter->ent = next;
921 iter->cpu = next_cpu;
922
Steven Rostedtb3806b42008-05-12 21:20:46 +0200923 if (next)
924 trace_iterator_increment(iter);
925
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200926 return next ? iter : NULL;
927}
928
Ingo Molnare309b412008-05-12 21:20:51 +0200929static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200930{
931 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200932 void *last_ent = iter->ent;
933 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200934 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200935
936 (*pos)++;
937
938 /* can't go backwards */
939 if (iter->idx > i)
940 return NULL;
941
942 if (iter->idx < 0)
943 ent = find_next_entry_inc(iter);
944 else
945 ent = iter;
946
947 while (ent && iter->idx < i)
948 ent = find_next_entry_inc(iter);
949
950 iter->pos = *pos;
951
952 if (last_ent && !ent)
953 seq_puts(m, "\n\nvim:ft=help\n");
954
955 return ent;
956}
957
958static void *s_start(struct seq_file *m, loff_t *pos)
959{
960 struct trace_iterator *iter = m->private;
961 void *p = NULL;
962 loff_t l = 0;
963 int i;
964
965 mutex_lock(&trace_types_lock);
966
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200967 if (!current_trace || current_trace != iter->trace) {
968 mutex_unlock(&trace_types_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200969 return NULL;
Steven Rostedtd15f57f2008-05-12 21:20:56 +0200970 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200971
972 atomic_inc(&trace_record_cmdline_disabled);
973
974 /* let the tracer grab locks here if needed */
975 if (current_trace->start)
976 current_trace->start(iter);
977
978 if (*pos != iter->pos) {
979 iter->ent = NULL;
980 iter->cpu = 0;
981 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200982 iter->prev_ent = NULL;
983 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200984
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200985 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200986 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200987 iter->next_page[i] = NULL;
988 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200989
990 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
991 ;
992
993 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200994 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200995 p = s_next(m, p, &l);
996 }
997
998 return p;
999}
1000
1001static void s_stop(struct seq_file *m, void *p)
1002{
1003 struct trace_iterator *iter = m->private;
1004
1005 atomic_dec(&trace_record_cmdline_disabled);
1006
1007 /* let the tracer release locks here if needed */
1008 if (current_trace && current_trace == iter->trace && iter->trace->stop)
1009 iter->trace->stop(iter);
1010
1011 mutex_unlock(&trace_types_lock);
1012}
1013
Steven Rostedtb3806b42008-05-12 21:20:46 +02001014static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001015seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016{
1017#ifdef CONFIG_KALLSYMS
1018 char str[KSYM_SYMBOL_LEN];
1019
1020 kallsyms_lookup(address, NULL, NULL, NULL, str);
1021
Steven Rostedtb3806b42008-05-12 21:20:46 +02001022 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001023#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001024 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001025}
1026
Steven Rostedtb3806b42008-05-12 21:20:46 +02001027static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001028seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1029 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001030{
1031#ifdef CONFIG_KALLSYMS
1032 char str[KSYM_SYMBOL_LEN];
1033
1034 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001035 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001036#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02001037 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001038}
1039
1040#ifndef CONFIG_64BIT
1041# define IP_FMT "%08lx"
1042#else
1043# define IP_FMT "%016lx"
1044#endif
1045
Ingo Molnare309b412008-05-12 21:20:51 +02001046static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001047seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001048{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001049 int ret;
1050
1051 if (!ip)
1052 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001053
1054 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001055 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001057 ret = seq_print_sym_short(s, "%s", ip);
1058
1059 if (!ret)
1060 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001061
1062 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001063 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1064 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001065}
1066
Ingo Molnare309b412008-05-12 21:20:51 +02001067static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068{
1069 seq_puts(m, "# _------=> CPU# \n");
1070 seq_puts(m, "# / _-----=> irqs-off \n");
1071 seq_puts(m, "# | / _----=> need-resched \n");
1072 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1073 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1074 seq_puts(m, "# |||| / \n");
1075 seq_puts(m, "# ||||| delay \n");
1076 seq_puts(m, "# cmd pid ||||| time | caller \n");
1077 seq_puts(m, "# \\ / ||||| \\ | / \n");
1078}
1079
Ingo Molnare309b412008-05-12 21:20:51 +02001080static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001081{
1082 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1083 seq_puts(m, "# | | | | |\n");
1084}
1085
1086
Ingo Molnare309b412008-05-12 21:20:51 +02001087static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001088print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1089{
1090 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1091 struct trace_array *tr = iter->tr;
1092 struct trace_array_cpu *data = tr->data[tr->cpu];
1093 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001094 unsigned long total = 0;
1095 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001096 int cpu;
1097 const char *name = "preemption";
1098
1099 if (type)
1100 name = type->name;
1101
1102 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001103 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001104 total += tr->data[cpu]->trace_idx;
1105 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001106 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001107 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001108 entries += tr->data[cpu]->trace_idx;
1109 }
1110 }
1111
1112 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1113 name, UTS_RELEASE);
1114 seq_puts(m, "-----------------------------------"
1115 "---------------------------------\n");
1116 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1117 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001118 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001119 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001120 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001121 tr->cpu,
1122#if defined(CONFIG_PREEMPT_NONE)
1123 "server",
1124#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1125 "desktop",
1126#elif defined(CONFIG_PREEMPT_DESKTOP)
1127 "preempt",
1128#else
1129 "unknown",
1130#endif
1131 /* These are reserved for later use */
1132 0, 0, 0, 0);
1133#ifdef CONFIG_SMP
1134 seq_printf(m, " #P:%d)\n", num_online_cpus());
1135#else
1136 seq_puts(m, ")\n");
1137#endif
1138 seq_puts(m, " -----------------\n");
1139 seq_printf(m, " | task: %.16s-%d "
1140 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1141 data->comm, data->pid, data->uid, data->nice,
1142 data->policy, data->rt_priority);
1143 seq_puts(m, " -----------------\n");
1144
1145 if (data->critical_start) {
1146 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001147 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1148 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001149 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001150 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1151 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001152 seq_puts(m, "\n");
1153 }
1154
1155 seq_puts(m, "\n");
1156}
1157
Ingo Molnare309b412008-05-12 21:20:51 +02001158static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001159lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001160{
1161 int hardirq, softirq;
1162 char *comm;
1163
1164 comm = trace_find_cmdline(entry->pid);
1165
Steven Rostedt214023c2008-05-12 21:20:46 +02001166 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1167 trace_seq_printf(s, "%d", cpu);
1168 trace_seq_printf(s, "%c%c",
1169 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1170 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001171
1172 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1173 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1174 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001175 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001176 else {
1177 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001178 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001179 else {
1180 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001181 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001182 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001183 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001184 }
1185 }
1186
1187 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001188 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001189 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001190 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001191}
1192
1193unsigned long preempt_mark_thresh = 100;
1194
Ingo Molnare309b412008-05-12 21:20:51 +02001195static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001196lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001197 unsigned long rel_usecs)
1198{
Steven Rostedt214023c2008-05-12 21:20:46 +02001199 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001200 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001201 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001202 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001203 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001204 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001205 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001206}
1207
1208static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1209
Ingo Molnare309b412008-05-12 21:20:51 +02001210static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001211print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001212{
Steven Rostedt214023c2008-05-12 21:20:46 +02001213 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001214 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1215 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1216 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1217 struct trace_entry *entry = iter->ent;
1218 unsigned long abs_usecs;
1219 unsigned long rel_usecs;
1220 char *comm;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001221 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001222 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001223
1224 if (!next_entry)
1225 next_entry = entry;
1226 rel_usecs = ns2usecs(next_entry->t - entry->t);
1227 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1228
1229 if (verbose) {
1230 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001231 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1232 " %ld.%03ldms (+%ld.%03ldms): ",
1233 comm,
1234 entry->pid, cpu, entry->flags,
1235 entry->preempt_count, trace_idx,
1236 ns2usecs(entry->t),
1237 abs_usecs/1000,
1238 abs_usecs % 1000, rel_usecs/1000,
1239 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240 } else {
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001241 lat_print_generic(s, entry, cpu);
1242 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001243 }
1244 switch (entry->type) {
1245 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001246 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1247 trace_seq_puts(s, " (");
1248 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1249 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001250 break;
1251 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001252 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001253 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1254 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001255 T = entry->ctx.next_state < sizeof(state_to_char) ?
1256 state_to_char[entry->ctx.next_state] : 'X';
1257
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001258 comm = trace_find_cmdline(entry->ctx.next_pid);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001259 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001260 entry->ctx.prev_pid,
1261 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001262 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001263 entry->ctx.next_pid,
1264 entry->ctx.next_prio,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001265 T, comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001266 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001267 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001268 trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001269 entry->special.arg1,
1270 entry->special.arg2,
1271 entry->special.arg3);
1272 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001273 case TRACE_STACK:
1274 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1275 if (i)
1276 trace_seq_puts(s, " <= ");
1277 seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1278 }
1279 trace_seq_puts(s, "\n");
1280 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001281 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001282 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001284 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001285}
1286
Ingo Molnare309b412008-05-12 21:20:51 +02001287static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001288{
Steven Rostedt214023c2008-05-12 21:20:46 +02001289 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001290 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001291 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001292 unsigned long usec_rem;
1293 unsigned long long t;
1294 unsigned long secs;
1295 char *comm;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001296 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001297 int S, T;
Ingo Molnar86387f72008-05-12 21:20:51 +02001298 int i;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001299
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001300 entry = iter->ent;
1301
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001302 comm = trace_find_cmdline(iter->ent->pid);
1303
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001304 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001305 usec_rem = do_div(t, 1000000ULL);
1306 secs = (unsigned long)t;
1307
Ingo Molnarf29c73f2008-05-12 21:20:53 +02001308 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1309 if (!ret)
1310 return 0;
1311 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1312 if (!ret)
1313 return 0;
1314 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1315 if (!ret)
1316 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001317
1318 switch (entry->type) {
1319 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001320 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1321 if (!ret)
1322 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001323 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1324 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001325 ret = trace_seq_printf(s, " <-");
1326 if (!ret)
1327 return 0;
1328 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1329 sym_flags);
1330 if (!ret)
1331 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001332 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001333 ret = trace_seq_printf(s, "\n");
1334 if (!ret)
1335 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001336 break;
1337 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001338 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001339 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1340 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001341 T = entry->ctx.next_state < sizeof(state_to_char) ?
1342 state_to_char[entry->ctx.next_state] : 'X';
1343 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d:%c\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001344 entry->ctx.prev_pid,
1345 entry->ctx.prev_prio,
1346 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001347 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001348 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001349 entry->ctx.next_prio,
1350 T);
Steven Rostedtb3806b42008-05-12 21:20:46 +02001351 if (!ret)
1352 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001353 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001354 case TRACE_SPECIAL:
Ingo Molnar88a42162008-05-12 21:20:53 +02001355 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001356 entry->special.arg1,
1357 entry->special.arg2,
1358 entry->special.arg3);
1359 if (!ret)
1360 return 0;
1361 break;
Ingo Molnar86387f72008-05-12 21:20:51 +02001362 case TRACE_STACK:
1363 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1364 if (i) {
1365 ret = trace_seq_puts(s, " <= ");
1366 if (!ret)
1367 return 0;
1368 }
1369 ret = seq_print_ip_sym(s, entry->stack.caller[i],
1370 sym_flags);
1371 if (!ret)
1372 return 0;
1373 }
1374 ret = trace_seq_puts(s, "\n");
1375 if (!ret)
1376 return 0;
1377 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001378 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001379 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001380}
1381
Ingo Molnare309b412008-05-12 21:20:51 +02001382static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001383{
1384 struct trace_seq *s = &iter->seq;
1385 struct trace_entry *entry;
1386 int ret;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001387 int S, T;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001388
1389 entry = iter->ent;
1390
1391 ret = trace_seq_printf(s, "%d %d %llu ",
1392 entry->pid, iter->cpu, entry->t);
1393 if (!ret)
1394 return 0;
1395
1396 switch (entry->type) {
1397 case TRACE_FN:
1398 ret = trace_seq_printf(s, "%x %x\n",
1399 entry->fn.ip, entry->fn.parent_ip);
1400 if (!ret)
1401 return 0;
1402 break;
1403 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001404 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001405 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1406 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001407 T = entry->ctx.next_state < sizeof(state_to_char) ?
1408 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001409 if (entry->type == TRACE_WAKE)
1410 S = '+';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001411 ret = trace_seq_printf(s, "%d %d %c %d %d %c\n",
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001412 entry->ctx.prev_pid,
1413 entry->ctx.prev_prio,
1414 S,
1415 entry->ctx.next_pid,
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001416 entry->ctx.next_prio,
1417 T);
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001418 if (!ret)
1419 return 0;
1420 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001421 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001422 case TRACE_STACK:
Ingo Molnar88a42162008-05-12 21:20:53 +02001423 ret = trace_seq_printf(s, "# %ld %ld %ld\n",
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001424 entry->special.arg1,
1425 entry->special.arg2,
1426 entry->special.arg3);
1427 if (!ret)
1428 return 0;
1429 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001430 }
1431 return 1;
1432}
1433
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001434#define SEQ_PUT_FIELD_RET(s, x) \
1435do { \
1436 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1437 return 0; \
1438} while (0)
1439
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001440#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1441do { \
1442 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1443 return 0; \
1444} while (0)
1445
Ingo Molnare309b412008-05-12 21:20:51 +02001446static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001447{
1448 struct trace_seq *s = &iter->seq;
1449 unsigned char newline = '\n';
1450 struct trace_entry *entry;
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001451 int S, T;
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001452
1453 entry = iter->ent;
1454
1455 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1456 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1457 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1458
1459 switch (entry->type) {
1460 case TRACE_FN:
1461 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1462 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1463 break;
1464 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001465 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001466 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1467 state_to_char[entry->ctx.prev_state] : 'X';
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001468 T = entry->ctx.next_state < sizeof(state_to_char) ?
1469 state_to_char[entry->ctx.next_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001470 if (entry->type == TRACE_WAKE)
1471 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001472 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1473 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1474 SEQ_PUT_HEX_FIELD_RET(s, S);
1475 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1476 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1477 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001478 SEQ_PUT_HEX_FIELD_RET(s, T);
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001479 break;
1480 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001481 case TRACE_STACK:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001482 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1483 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1484 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1485 break;
1486 }
1487 SEQ_PUT_FIELD_RET(s, newline);
1488
1489 return 1;
1490}
1491
Ingo Molnare309b412008-05-12 21:20:51 +02001492static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001493{
1494 struct trace_seq *s = &iter->seq;
1495 struct trace_entry *entry;
1496
1497 entry = iter->ent;
1498
1499 SEQ_PUT_FIELD_RET(s, entry->pid);
1500 SEQ_PUT_FIELD_RET(s, entry->cpu);
1501 SEQ_PUT_FIELD_RET(s, entry->t);
1502
1503 switch (entry->type) {
1504 case TRACE_FN:
1505 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1506 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1507 break;
1508 case TRACE_CTX:
1509 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1510 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1511 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1512 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1513 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
Peter Zijlstrabac524d2008-05-12 21:20:53 +02001514 SEQ_PUT_FIELD_RET(s, entry->ctx.next_state);
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001515 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001516 case TRACE_SPECIAL:
Ingo Molnar86387f72008-05-12 21:20:51 +02001517 case TRACE_STACK:
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001518 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1519 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1520 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1521 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001522 }
1523 return 1;
1524}
1525
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001526static int trace_empty(struct trace_iterator *iter)
1527{
1528 struct trace_array_cpu *data;
1529 int cpu;
1530
1531 for_each_possible_cpu(cpu) {
1532 data = iter->tr->data[cpu];
1533
Steven Rostedtb3806b42008-05-12 21:20:46 +02001534 if (head_page(data) && data->trace_idx &&
1535 (data->trace_tail != data->trace_head ||
1536 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001537 return 0;
1538 }
1539 return 1;
1540}
1541
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001542static int print_trace_line(struct trace_iterator *iter)
1543{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001544 if (trace_flags & TRACE_ITER_BIN)
1545 return print_bin_fmt(iter);
1546
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001547 if (trace_flags & TRACE_ITER_HEX)
1548 return print_hex_fmt(iter);
1549
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001550 if (trace_flags & TRACE_ITER_RAW)
1551 return print_raw_fmt(iter);
1552
1553 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1554 return print_lat_fmt(iter, iter->idx, iter->cpu);
1555
1556 return print_trace_fmt(iter);
1557}
1558
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001559static int s_show(struct seq_file *m, void *v)
1560{
1561 struct trace_iterator *iter = v;
1562
1563 if (iter->ent == NULL) {
1564 if (iter->tr) {
1565 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1566 seq_puts(m, "#\n");
1567 }
1568 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1569 /* print nothing if the buffers are empty */
1570 if (trace_empty(iter))
1571 return 0;
1572 print_trace_header(m, iter);
1573 if (!(trace_flags & TRACE_ITER_VERBOSE))
1574 print_lat_help_header(m);
1575 } else {
1576 if (!(trace_flags & TRACE_ITER_VERBOSE))
1577 print_func_help_header(m);
1578 }
1579 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001580 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001581 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001582 }
1583
1584 return 0;
1585}
1586
1587static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001588 .start = s_start,
1589 .next = s_next,
1590 .stop = s_stop,
1591 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001592};
1593
Ingo Molnare309b412008-05-12 21:20:51 +02001594static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001595__tracing_open(struct inode *inode, struct file *file, int *ret)
1596{
1597 struct trace_iterator *iter;
1598
Steven Rostedt60a11772008-05-12 21:20:44 +02001599 if (tracing_disabled) {
1600 *ret = -ENODEV;
1601 return NULL;
1602 }
1603
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001604 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1605 if (!iter) {
1606 *ret = -ENOMEM;
1607 goto out;
1608 }
1609
1610 mutex_lock(&trace_types_lock);
1611 if (current_trace && current_trace->print_max)
1612 iter->tr = &max_tr;
1613 else
1614 iter->tr = inode->i_private;
1615 iter->trace = current_trace;
1616 iter->pos = -1;
1617
1618 /* TODO stop tracer */
1619 *ret = seq_open(file, &tracer_seq_ops);
1620 if (!*ret) {
1621 struct seq_file *m = file->private_data;
1622 m->private = iter;
1623
1624 /* stop the trace while dumping */
1625 if (iter->tr->ctrl)
1626 tracer_enabled = 0;
1627
1628 if (iter->trace && iter->trace->open)
1629 iter->trace->open(iter);
1630 } else {
1631 kfree(iter);
1632 iter = NULL;
1633 }
1634 mutex_unlock(&trace_types_lock);
1635
1636 out:
1637 return iter;
1638}
1639
1640int tracing_open_generic(struct inode *inode, struct file *filp)
1641{
Steven Rostedt60a11772008-05-12 21:20:44 +02001642 if (tracing_disabled)
1643 return -ENODEV;
1644
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001645 filp->private_data = inode->i_private;
1646 return 0;
1647}
1648
1649int tracing_release(struct inode *inode, struct file *file)
1650{
1651 struct seq_file *m = (struct seq_file *)file->private_data;
1652 struct trace_iterator *iter = m->private;
1653
1654 mutex_lock(&trace_types_lock);
1655 if (iter->trace && iter->trace->close)
1656 iter->trace->close(iter);
1657
1658 /* reenable tracing if it was previously enabled */
1659 if (iter->tr->ctrl)
1660 tracer_enabled = 1;
1661 mutex_unlock(&trace_types_lock);
1662
1663 seq_release(inode, file);
1664 kfree(iter);
1665 return 0;
1666}
1667
1668static int tracing_open(struct inode *inode, struct file *file)
1669{
1670 int ret;
1671
1672 __tracing_open(inode, file, &ret);
1673
1674 return ret;
1675}
1676
1677static int tracing_lt_open(struct inode *inode, struct file *file)
1678{
1679 struct trace_iterator *iter;
1680 int ret;
1681
1682 iter = __tracing_open(inode, file, &ret);
1683
1684 if (!ret)
1685 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1686
1687 return ret;
1688}
1689
1690
Ingo Molnare309b412008-05-12 21:20:51 +02001691static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001692t_next(struct seq_file *m, void *v, loff_t *pos)
1693{
1694 struct tracer *t = m->private;
1695
1696 (*pos)++;
1697
1698 if (t)
1699 t = t->next;
1700
1701 m->private = t;
1702
1703 return t;
1704}
1705
1706static void *t_start(struct seq_file *m, loff_t *pos)
1707{
1708 struct tracer *t = m->private;
1709 loff_t l = 0;
1710
1711 mutex_lock(&trace_types_lock);
1712 for (; t && l < *pos; t = t_next(m, t, &l))
1713 ;
1714
1715 return t;
1716}
1717
1718static void t_stop(struct seq_file *m, void *p)
1719{
1720 mutex_unlock(&trace_types_lock);
1721}
1722
1723static int t_show(struct seq_file *m, void *v)
1724{
1725 struct tracer *t = v;
1726
1727 if (!t)
1728 return 0;
1729
1730 seq_printf(m, "%s", t->name);
1731 if (t->next)
1732 seq_putc(m, ' ');
1733 else
1734 seq_putc(m, '\n');
1735
1736 return 0;
1737}
1738
1739static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001740 .start = t_start,
1741 .next = t_next,
1742 .stop = t_stop,
1743 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001744};
1745
1746static int show_traces_open(struct inode *inode, struct file *file)
1747{
1748 int ret;
1749
Steven Rostedt60a11772008-05-12 21:20:44 +02001750 if (tracing_disabled)
1751 return -ENODEV;
1752
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001753 ret = seq_open(file, &show_traces_seq_ops);
1754 if (!ret) {
1755 struct seq_file *m = file->private_data;
1756 m->private = trace_types;
1757 }
1758
1759 return ret;
1760}
1761
1762static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001763 .open = tracing_open,
1764 .read = seq_read,
1765 .llseek = seq_lseek,
1766 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001767};
1768
1769static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001770 .open = tracing_lt_open,
1771 .read = seq_read,
1772 .llseek = seq_lseek,
1773 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001774};
1775
1776static struct file_operations show_traces_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001777 .open = show_traces_open,
1778 .read = seq_read,
1779 .release = seq_release,
1780};
1781
Ingo Molnar36dfe922008-05-12 21:20:52 +02001782/*
1783 * Only trace on a CPU if the bitmask is set:
1784 */
1785static cpumask_t tracing_cpumask = CPU_MASK_ALL;
1786
1787/*
1788 * When tracing/tracing_cpu_mask is modified then this holds
1789 * the new bitmask we are about to install:
1790 */
1791static cpumask_t tracing_cpumask_new;
1792
1793/*
1794 * The tracer itself will not take this lock, but still we want
1795 * to provide a consistent cpumask to user-space:
1796 */
1797static DEFINE_MUTEX(tracing_cpumask_update_lock);
1798
1799/*
1800 * Temporary storage for the character representation of the
1801 * CPU bitmask (and one more byte for the newline):
1802 */
1803static char mask_str[NR_CPUS + 1];
1804
Ingo Molnarc7078de2008-05-12 21:20:52 +02001805static ssize_t
1806tracing_cpumask_read(struct file *filp, char __user *ubuf,
1807 size_t count, loff_t *ppos)
1808{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001809 int len;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001810
1811 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001812
1813 len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
1814 if (count - len < 2) {
1815 count = -EINVAL;
1816 goto out_err;
1817 }
1818 len += sprintf(mask_str + len, "\n");
1819 count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
1820
1821out_err:
Ingo Molnarc7078de2008-05-12 21:20:52 +02001822 mutex_unlock(&tracing_cpumask_update_lock);
1823
1824 return count;
1825}
1826
1827static ssize_t
1828tracing_cpumask_write(struct file *filp, const char __user *ubuf,
1829 size_t count, loff_t *ppos)
1830{
Ingo Molnar36dfe922008-05-12 21:20:52 +02001831 int err, cpu;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001832
1833 mutex_lock(&tracing_cpumask_update_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001834 err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
1835 if (err)
1836 goto err_unlock;
1837
Steven Rostedt92205c22008-05-12 21:20:55 +02001838 raw_local_irq_disable();
1839 __raw_spin_lock(&ftrace_max_lock);
Ingo Molnar36dfe922008-05-12 21:20:52 +02001840 for_each_possible_cpu(cpu) {
1841 /*
1842 * Increase/decrease the disabled counter if we are
1843 * about to flip a bit in the cpumask:
1844 */
1845 if (cpu_isset(cpu, tracing_cpumask) &&
1846 !cpu_isset(cpu, tracing_cpumask_new)) {
1847 atomic_inc(&global_trace.data[cpu]->disabled);
1848 }
1849 if (!cpu_isset(cpu, tracing_cpumask) &&
1850 cpu_isset(cpu, tracing_cpumask_new)) {
1851 atomic_dec(&global_trace.data[cpu]->disabled);
1852 }
1853 }
Steven Rostedt92205c22008-05-12 21:20:55 +02001854 __raw_spin_unlock(&ftrace_max_lock);
1855 raw_local_irq_enable();
Ingo Molnar36dfe922008-05-12 21:20:52 +02001856
1857 tracing_cpumask = tracing_cpumask_new;
1858
Ingo Molnarc7078de2008-05-12 21:20:52 +02001859 mutex_unlock(&tracing_cpumask_update_lock);
1860
Ingo Molnarc7078de2008-05-12 21:20:52 +02001861 return count;
Ingo Molnar36dfe922008-05-12 21:20:52 +02001862
1863err_unlock:
1864 mutex_unlock(&tracing_cpumask_update_lock);
1865
1866 return err;
Ingo Molnarc7078de2008-05-12 21:20:52 +02001867}
1868
1869static struct file_operations tracing_cpumask_fops = {
1870 .open = tracing_open_generic,
1871 .read = tracing_cpumask_read,
1872 .write = tracing_cpumask_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001873};
1874
1875static ssize_t
1876tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1877 size_t cnt, loff_t *ppos)
1878{
1879 char *buf;
1880 int r = 0;
1881 int len = 0;
1882 int i;
1883
1884 /* calulate max size */
1885 for (i = 0; trace_options[i]; i++) {
1886 len += strlen(trace_options[i]);
1887 len += 3; /* "no" and space */
1888 }
1889
1890 /* +2 for \n and \0 */
1891 buf = kmalloc(len + 2, GFP_KERNEL);
1892 if (!buf)
1893 return -ENOMEM;
1894
1895 for (i = 0; trace_options[i]; i++) {
1896 if (trace_flags & (1 << i))
1897 r += sprintf(buf + r, "%s ", trace_options[i]);
1898 else
1899 r += sprintf(buf + r, "no%s ", trace_options[i]);
1900 }
1901
1902 r += sprintf(buf + r, "\n");
1903 WARN_ON(r >= len + 2);
1904
Ingo Molnar36dfe922008-05-12 21:20:52 +02001905 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001906
1907 kfree(buf);
1908
1909 return r;
1910}
1911
1912static ssize_t
1913tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1914 size_t cnt, loff_t *ppos)
1915{
1916 char buf[64];
1917 char *cmp = buf;
1918 int neg = 0;
1919 int i;
1920
1921 if (cnt > 63)
1922 cnt = 63;
1923
1924 if (copy_from_user(&buf, ubuf, cnt))
1925 return -EFAULT;
1926
1927 buf[cnt] = 0;
1928
1929 if (strncmp(buf, "no", 2) == 0) {
1930 neg = 1;
1931 cmp += 2;
1932 }
1933
1934 for (i = 0; trace_options[i]; i++) {
1935 int len = strlen(trace_options[i]);
1936
1937 if (strncmp(cmp, trace_options[i], len) == 0) {
1938 if (neg)
1939 trace_flags &= ~(1 << i);
1940 else
1941 trace_flags |= (1 << i);
1942 break;
1943 }
1944 }
Ingo Molnar442e5442008-05-12 21:20:53 +02001945 /*
1946 * If no option could be set, return an error:
1947 */
1948 if (!trace_options[i])
1949 return -EINVAL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001950
1951 filp->f_pos += cnt;
1952
1953 return cnt;
1954}
1955
1956static struct file_operations tracing_iter_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001957 .open = tracing_open_generic,
1958 .read = tracing_iter_ctrl_read,
1959 .write = tracing_iter_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001960};
1961
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001962static const char readme_msg[] =
1963 "tracing mini-HOWTO:\n\n"
1964 "# mkdir /debug\n"
1965 "# mount -t debugfs nodev /debug\n\n"
1966 "# cat /debug/tracing/available_tracers\n"
1967 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1968 "# cat /debug/tracing/current_tracer\n"
1969 "none\n"
1970 "# echo sched_switch > /debug/tracing/current_tracer\n"
1971 "# cat /debug/tracing/current_tracer\n"
1972 "sched_switch\n"
1973 "# cat /debug/tracing/iter_ctrl\n"
1974 "noprint-parent nosym-offset nosym-addr noverbose\n"
1975 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1976 "# echo 1 > /debug/tracing/tracing_enabled\n"
1977 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1978 "echo 0 > /debug/tracing/tracing_enabled\n"
1979;
1980
1981static ssize_t
1982tracing_readme_read(struct file *filp, char __user *ubuf,
1983 size_t cnt, loff_t *ppos)
1984{
1985 return simple_read_from_buffer(ubuf, cnt, ppos,
1986 readme_msg, strlen(readme_msg));
1987}
1988
1989static struct file_operations tracing_readme_fops = {
Ingo Molnarc7078de2008-05-12 21:20:52 +02001990 .open = tracing_open_generic,
1991 .read = tracing_readme_read,
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001992};
1993
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001994static ssize_t
1995tracing_ctrl_read(struct file *filp, char __user *ubuf,
1996 size_t cnt, loff_t *ppos)
1997{
1998 struct trace_array *tr = filp->private_data;
1999 char buf[64];
2000 int r;
2001
2002 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02002003 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002004}
2005
2006static ssize_t
2007tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2008 size_t cnt, loff_t *ppos)
2009{
2010 struct trace_array *tr = filp->private_data;
2011 long val;
2012 char buf[64];
2013
2014 if (cnt > 63)
2015 cnt = 63;
2016
2017 if (copy_from_user(&buf, ubuf, cnt))
2018 return -EFAULT;
2019
2020 buf[cnt] = 0;
2021
2022 val = simple_strtoul(buf, NULL, 10);
2023
2024 val = !!val;
2025
2026 mutex_lock(&trace_types_lock);
2027 if (tr->ctrl ^ val) {
2028 if (val)
2029 tracer_enabled = 1;
2030 else
2031 tracer_enabled = 0;
2032
2033 tr->ctrl = val;
2034
2035 if (current_trace && current_trace->ctrl_update)
2036 current_trace->ctrl_update(tr);
2037 }
2038 mutex_unlock(&trace_types_lock);
2039
2040 filp->f_pos += cnt;
2041
2042 return cnt;
2043}
2044
2045static ssize_t
2046tracing_set_trace_read(struct file *filp, char __user *ubuf,
2047 size_t cnt, loff_t *ppos)
2048{
2049 char buf[max_tracer_type_len+2];
2050 int r;
2051
2052 mutex_lock(&trace_types_lock);
2053 if (current_trace)
2054 r = sprintf(buf, "%s\n", current_trace->name);
2055 else
2056 r = sprintf(buf, "\n");
2057 mutex_unlock(&trace_types_lock);
2058
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002059 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002060}
2061
2062static ssize_t
2063tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2064 size_t cnt, loff_t *ppos)
2065{
2066 struct trace_array *tr = &global_trace;
2067 struct tracer *t;
2068 char buf[max_tracer_type_len+1];
2069 int i;
2070
2071 if (cnt > max_tracer_type_len)
2072 cnt = max_tracer_type_len;
2073
2074 if (copy_from_user(&buf, ubuf, cnt))
2075 return -EFAULT;
2076
2077 buf[cnt] = 0;
2078
2079 /* strip ending whitespace. */
2080 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2081 buf[i] = 0;
2082
2083 mutex_lock(&trace_types_lock);
2084 for (t = trace_types; t; t = t->next) {
2085 if (strcmp(t->name, buf) == 0)
2086 break;
2087 }
2088 if (!t || t == current_trace)
2089 goto out;
2090
2091 if (current_trace && current_trace->reset)
2092 current_trace->reset(tr);
2093
2094 current_trace = t;
2095 if (t->init)
2096 t->init(tr);
2097
2098 out:
2099 mutex_unlock(&trace_types_lock);
2100
2101 filp->f_pos += cnt;
2102
2103 return cnt;
2104}
2105
2106static ssize_t
2107tracing_max_lat_read(struct file *filp, char __user *ubuf,
2108 size_t cnt, loff_t *ppos)
2109{
2110 unsigned long *ptr = filp->private_data;
2111 char buf[64];
2112 int r;
2113
2114 r = snprintf(buf, 64, "%ld\n",
2115 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2116 if (r > 64)
2117 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002118 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002119}
2120
2121static ssize_t
2122tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2123 size_t cnt, loff_t *ppos)
2124{
2125 long *ptr = filp->private_data;
2126 long val;
2127 char buf[64];
2128
2129 if (cnt > 63)
2130 cnt = 63;
2131
2132 if (copy_from_user(&buf, ubuf, cnt))
2133 return -EFAULT;
2134
2135 buf[cnt] = 0;
2136
2137 val = simple_strtoul(buf, NULL, 10);
2138
2139 *ptr = val * 1000;
2140
2141 return cnt;
2142}
2143
Steven Rostedtb3806b42008-05-12 21:20:46 +02002144static atomic_t tracing_reader;
2145
2146static int tracing_open_pipe(struct inode *inode, struct file *filp)
2147{
2148 struct trace_iterator *iter;
2149
2150 if (tracing_disabled)
2151 return -ENODEV;
2152
2153 /* We only allow for reader of the pipe */
2154 if (atomic_inc_return(&tracing_reader) != 1) {
2155 atomic_dec(&tracing_reader);
2156 return -EBUSY;
2157 }
2158
2159 /* create a buffer to store the information to pass to userspace */
2160 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2161 if (!iter)
2162 return -ENOMEM;
2163
2164 iter->tr = &global_trace;
2165
2166 filp->private_data = iter;
2167
2168 return 0;
2169}
2170
2171static int tracing_release_pipe(struct inode *inode, struct file *file)
2172{
2173 struct trace_iterator *iter = file->private_data;
2174
2175 kfree(iter);
2176 atomic_dec(&tracing_reader);
2177
2178 return 0;
2179}
2180
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002181static unsigned int
2182tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2183{
2184 struct trace_iterator *iter = filp->private_data;
2185
2186 if (trace_flags & TRACE_ITER_BLOCK) {
2187 /*
2188 * Always select as readable when in blocking mode
2189 */
2190 return POLLIN | POLLRDNORM;
2191 }
2192 else {
2193 if (!trace_empty(iter))
2194 return POLLIN | POLLRDNORM;
2195 poll_wait(filp, &trace_wait, poll_table);
2196 if (!trace_empty(iter))
2197 return POLLIN | POLLRDNORM;
2198
2199 return 0;
2200 }
2201}
2202
Steven Rostedtb3806b42008-05-12 21:20:46 +02002203/*
2204 * Consumer reader.
2205 */
2206static ssize_t
2207tracing_read_pipe(struct file *filp, char __user *ubuf,
2208 size_t cnt, loff_t *ppos)
2209{
2210 struct trace_iterator *iter = filp->private_data;
2211 struct trace_array_cpu *data;
2212 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002213 static int start;
2214 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002215#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002216 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002217#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002218 int read = 0;
2219 int cpu;
2220 int len;
2221 int ret;
2222
2223 /* return any leftover data */
2224 if (iter->seq.len > start) {
2225 len = iter->seq.len - start;
2226 if (cnt > len)
2227 cnt = len;
2228 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2229 if (ret)
2230 cnt = -EFAULT;
2231
2232 start += len;
2233
2234 return cnt;
2235 }
2236
2237 trace_seq_reset(&iter->seq);
2238 start = 0;
2239
2240 while (trace_empty(iter)) {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002241 if (!(trace_flags & TRACE_ITER_BLOCK))
2242 return -EWOULDBLOCK;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002243 /*
2244 * This is a make-shift waitqueue. The reason we don't use
2245 * an actual wait queue is because:
2246 * 1) we only ever have one waiter
2247 * 2) the tracing, traces all functions, we don't want
2248 * the overhead of calling wake_up and friends
2249 * (and tracing them too)
2250 * Anyway, this is really very primitive wakeup.
2251 */
2252 set_current_state(TASK_INTERRUPTIBLE);
2253 iter->tr->waiter = current;
2254
2255 /* sleep for one second, and try again. */
2256 schedule_timeout(HZ);
2257
2258 iter->tr->waiter = NULL;
2259
2260 if (signal_pending(current))
2261 return -EINTR;
2262
2263 /*
2264 * We block until we read something and tracing is disabled.
2265 * We still block if tracing is disabled, but we have never
2266 * read anything. This allows a user to cat this file, and
2267 * then enable tracing. But after we have read something,
2268 * we give an EOF when tracing is again disabled.
2269 *
2270 * iter->pos will be 0 if we haven't read anything.
2271 */
2272 if (!tracer_enabled && iter->pos)
2273 break;
2274
2275 continue;
2276 }
2277
2278 /* stop when tracing is finished */
2279 if (trace_empty(iter))
2280 return 0;
2281
2282 if (cnt >= PAGE_SIZE)
2283 cnt = PAGE_SIZE - 1;
2284
2285 memset(iter, 0, sizeof(*iter));
2286 iter->tr = &global_trace;
2287 iter->pos = -1;
2288
2289 /*
2290 * We need to stop all tracing on all CPUS to read the
2291 * the next buffer. This is a bit expensive, but is
2292 * not done often. We fill all what we can read,
2293 * and then release the locks again.
2294 */
2295
2296 cpus_clear(mask);
2297 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002298#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002299 ftrace_save = ftrace_enabled;
2300 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002301#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002302 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002303 for_each_possible_cpu(cpu) {
2304 data = iter->tr->data[cpu];
2305
2306 if (!head_page(data) || !data->trace_idx)
2307 continue;
2308
2309 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002310 cpu_set(cpu, mask);
2311 }
2312
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002313 for_each_cpu_mask(cpu, mask) {
2314 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002315 __raw_spin_lock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002316 }
2317
Steven Rostedt088b1e422008-05-12 21:20:48 +02002318 while (find_next_entry_inc(iter) != NULL) {
2319 int len = iter->seq.len;
2320
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002321 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002322 if (!ret) {
2323 /* don't print partial lines */
2324 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002325 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002326 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002327
2328 trace_consume(iter);
2329
2330 if (iter->seq.len >= cnt)
2331 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002332 }
2333
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002334 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002335 data = iter->tr->data[cpu];
Steven Rostedt92205c22008-05-12 21:20:55 +02002336 __raw_spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002337 }
2338
2339 for_each_cpu_mask(cpu, mask) {
2340 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002341 atomic_dec(&data->disabled);
2342 }
Ingo Molnar25770462008-05-12 21:20:49 +02002343#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002344 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002345#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002346 local_irq_restore(flags);
2347
2348 /* Now copy what we have to the user */
2349 read = iter->seq.len;
2350 if (read > cnt)
2351 read = cnt;
2352
2353 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2354
2355 if (read < iter->seq.len)
2356 start = read;
2357 else
2358 trace_seq_reset(&iter->seq);
2359
2360 if (ret)
2361 read = -EFAULT;
2362
2363 return read;
2364}
2365
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002366static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002367 .open = tracing_open_generic,
2368 .read = tracing_max_lat_read,
2369 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002370};
2371
2372static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002373 .open = tracing_open_generic,
2374 .read = tracing_ctrl_read,
2375 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002376};
2377
2378static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002379 .open = tracing_open_generic,
2380 .read = tracing_set_trace_read,
2381 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002382};
2383
Steven Rostedtb3806b42008-05-12 21:20:46 +02002384static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002385 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002386 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002387 .read = tracing_read_pipe,
2388 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002389};
2390
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002391#ifdef CONFIG_DYNAMIC_FTRACE
2392
2393static ssize_t
2394tracing_read_long(struct file *filp, char __user *ubuf,
2395 size_t cnt, loff_t *ppos)
2396{
2397 unsigned long *p = filp->private_data;
2398 char buf[64];
2399 int r;
2400
2401 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002402
2403 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002404}
2405
2406static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002407 .open = tracing_open_generic,
2408 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002409};
2410#endif
2411
2412static struct dentry *d_tracer;
2413
2414struct dentry *tracing_init_dentry(void)
2415{
2416 static int once;
2417
2418 if (d_tracer)
2419 return d_tracer;
2420
2421 d_tracer = debugfs_create_dir("tracing", NULL);
2422
2423 if (!d_tracer && !once) {
2424 once = 1;
2425 pr_warning("Could not create debugfs directory 'tracing'\n");
2426 return NULL;
2427 }
2428
2429 return d_tracer;
2430}
2431
Steven Rostedt60a11772008-05-12 21:20:44 +02002432#ifdef CONFIG_FTRACE_SELFTEST
2433/* Let selftest have access to static functions in this file */
2434#include "trace_selftest.c"
2435#endif
2436
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002437static __init void tracer_init_debugfs(void)
2438{
2439 struct dentry *d_tracer;
2440 struct dentry *entry;
2441
2442 d_tracer = tracing_init_dentry();
2443
2444 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2445 &global_trace, &tracing_ctrl_fops);
2446 if (!entry)
2447 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2448
2449 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2450 NULL, &tracing_iter_fops);
2451 if (!entry)
2452 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2453
Ingo Molnarc7078de2008-05-12 21:20:52 +02002454 entry = debugfs_create_file("tracing_cpumask", 0644, d_tracer,
2455 NULL, &tracing_cpumask_fops);
2456 if (!entry)
2457 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2458
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002459 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2460 &global_trace, &tracing_lt_fops);
2461 if (!entry)
2462 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2463
2464 entry = debugfs_create_file("trace", 0444, d_tracer,
2465 &global_trace, &tracing_fops);
2466 if (!entry)
2467 pr_warning("Could not create debugfs 'trace' entry\n");
2468
2469 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2470 &global_trace, &show_traces_fops);
2471 if (!entry)
2472 pr_warning("Could not create debugfs 'trace' entry\n");
2473
2474 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2475 &global_trace, &set_tracer_fops);
2476 if (!entry)
2477 pr_warning("Could not create debugfs 'trace' entry\n");
2478
2479 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2480 &tracing_max_latency,
2481 &tracing_max_lat_fops);
2482 if (!entry)
2483 pr_warning("Could not create debugfs "
2484 "'tracing_max_latency' entry\n");
2485
2486 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2487 &tracing_thresh, &tracing_max_lat_fops);
2488 if (!entry)
2489 pr_warning("Could not create debugfs "
2490 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002491 entry = debugfs_create_file("README", 0644, d_tracer,
2492 NULL, &tracing_readme_fops);
2493 if (!entry)
2494 pr_warning("Could not create debugfs 'README' entry\n");
2495
Steven Rostedtb3806b42008-05-12 21:20:46 +02002496 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2497 NULL, &tracing_pipe_fops);
2498 if (!entry)
2499 pr_warning("Could not create debugfs "
2500 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002501
2502#ifdef CONFIG_DYNAMIC_FTRACE
2503 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2504 &ftrace_update_tot_cnt,
2505 &tracing_read_long_fops);
2506 if (!entry)
2507 pr_warning("Could not create debugfs "
2508 "'dyn_ftrace_total_info' entry\n");
2509#endif
2510}
2511
2512/* dummy trace to disable tracing */
2513static struct tracer no_tracer __read_mostly =
2514{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002515 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002516};
2517
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002518static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002519{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002520 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002521 struct page *page, *tmp;
2522 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002523 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002524 int i;
2525
2526 /* first allocate a page for each CPU */
2527 for_each_possible_cpu(i) {
2528 array = (void *)__get_free_page(GFP_KERNEL);
2529 if (array == NULL) {
2530 printk(KERN_ERR "tracer: failed to allocate page"
2531 "for trace buffer!\n");
2532 goto free_pages;
2533 }
2534
2535 page = virt_to_page(array);
2536 list_add(&page->lru, &pages);
2537
2538/* Only allocate if we are actually using the max trace */
2539#ifdef CONFIG_TRACER_MAX_TRACE
2540 array = (void *)__get_free_page(GFP_KERNEL);
2541 if (array == NULL) {
2542 printk(KERN_ERR "tracer: failed to allocate page"
2543 "for trace buffer!\n");
2544 goto free_pages;
2545 }
2546 page = virt_to_page(array);
2547 list_add(&page->lru, &pages);
2548#endif
2549 }
2550
2551 /* Now that we successfully allocate a page per CPU, add them */
2552 for_each_possible_cpu(i) {
2553 data = global_trace.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 ClearPageLRU(page);
2559
2560#ifdef CONFIG_TRACER_MAX_TRACE
2561 data = max_tr.data[i];
Steven Rostedt92205c22008-05-12 21:20:55 +02002562 data->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002563 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002564 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002565 list_add_tail(&page->lru, &data->trace_pages);
2566 SetPageLRU(page);
2567#endif
2568 }
2569 global_trace.entries += ENTRIES_PER_PAGE;
2570
2571 return 0;
2572
2573 free_pages:
2574 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002575 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002576 __free_page(page);
2577 }
2578 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002579}
2580
2581__init static int tracer_alloc_buffers(void)
2582{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002583 struct trace_array_cpu *data;
2584 void *array;
2585 struct page *page;
2586 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002587 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002588 int i;
2589
Steven Rostedt26994ea2008-05-12 21:20:48 +02002590 global_trace.ctrl = tracer_enabled;
2591
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002592 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002593 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002594 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002595 max_tr.data[i] = &per_cpu(max_data, i);
2596
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002597 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002598 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002599 printk(KERN_ERR "tracer: failed to allocate page"
2600 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002601 goto free_buffers;
2602 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002603
2604 /* set the array to the list */
2605 INIT_LIST_HEAD(&data->trace_pages);
2606 page = virt_to_page(array);
2607 list_add(&page->lru, &data->trace_pages);
2608 /* use the LRU flag to differentiate the two buffers */
2609 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002610
2611/* Only allocate if we are actually using the max trace */
2612#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002613 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002614 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002615 printk(KERN_ERR "tracer: failed to allocate page"
2616 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002617 goto free_buffers;
2618 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002619
2620 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2621 page = virt_to_page(array);
2622 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2623 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002624#endif
2625 }
2626
2627 /*
2628 * Since we allocate by orders of pages, we may be able to
2629 * round up a bit.
2630 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002631 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002632 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002633
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002634 while (global_trace.entries < trace_nr_entries) {
2635 if (trace_alloc_page())
2636 break;
2637 pages++;
2638 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002639 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002640
2641 pr_info("tracer: %d pages allocated for %ld",
2642 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002643 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2644 pr_info(" actual entries %ld\n", global_trace.entries);
2645
2646 tracer_init_debugfs();
2647
2648 trace_init_cmdlines();
2649
2650 register_tracer(&no_tracer);
2651 current_trace = &no_tracer;
2652
Steven Rostedt60a11772008-05-12 21:20:44 +02002653 /* All seems OK, enable tracing */
2654 tracing_disabled = 0;
2655
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002656 return 0;
2657
2658 free_buffers:
2659 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002660 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002661 struct trace_array_cpu *data = global_trace.data[i];
2662
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002663 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002664 list_for_each_entry_safe(page, tmp,
2665 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002666 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002667 __free_page(page);
2668 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002669 }
2670
2671#ifdef CONFIG_TRACER_MAX_TRACE
2672 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002673 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002674 list_for_each_entry_safe(page, tmp,
2675 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002676 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002677 __free_page(page);
2678 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002679 }
2680#endif
2681 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002682 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002683}
Steven Rostedt60a11772008-05-12 21:20:44 +02002684fs_initcall(tracer_alloc_buffers);