blob: f5898051fdd965948a1f5d55054376170348156c [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
31#include "trace.h"
32
33unsigned long __read_mostly tracing_max_latency = (cycle_t)ULONG_MAX;
34unsigned long __read_mostly tracing_thresh;
35
Steven Rostedt60a11772008-05-12 21:20:44 +020036static int tracing_disabled = 1;
37
Ingo Molnare309b412008-05-12 21:20:51 +020038static long
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020039ns2usecs(cycle_t nsec)
40{
41 nsec += 500;
42 do_div(nsec, 1000);
43 return nsec;
44}
45
Ingo Molnare309b412008-05-12 21:20:51 +020046cycle_t ftrace_now(int cpu)
Ingo Molnar750ed1a2008-05-12 21:20:46 +020047{
Ingo Molnar0fd9e0d2008-05-12 21:20:48 +020048 return cpu_clock(cpu);
Ingo Molnar750ed1a2008-05-12 21:20:46 +020049}
50
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020051static struct trace_array global_trace;
52
53static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
54
55static struct trace_array max_tr;
56
57static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
58
Steven Rostedt26994ea2008-05-12 21:20:48 +020059static int tracer_enabled = 1;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020060static unsigned long trace_nr_entries = 16384UL;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020061
62static struct tracer *trace_types __read_mostly;
63static struct tracer *current_trace __read_mostly;
64static int max_tracer_type_len;
65
66static DEFINE_MUTEX(trace_types_lock);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +020067static DECLARE_WAIT_QUEUE_HEAD (trace_wait);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020068
Steven Rostedt4c11d7a2008-05-12 21:20:43 +020069#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
70
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020071static int __init set_nr_entries(char *str)
72{
73 if (!str)
74 return 0;
75 trace_nr_entries = simple_strtoul(str, &str, 0);
76 return 1;
77}
78__setup("trace_entries=", set_nr_entries);
79
Steven Rostedt57f50be2008-05-12 21:20:44 +020080unsigned long nsecs_to_usecs(unsigned long nsecs)
81{
82 return nsecs / 1000;
83}
84
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020085enum trace_type {
86 __TRACE_FIRST_TYPE = 0,
87
88 TRACE_FN,
89 TRACE_CTX,
Ingo Molnarf0a920d2008-05-12 21:20:47 +020090 TRACE_SPECIAL,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020091
92 __TRACE_LAST_TYPE
93};
94
95enum trace_flag_type {
96 TRACE_FLAG_IRQS_OFF = 0x01,
97 TRACE_FLAG_NEED_RESCHED = 0x02,
98 TRACE_FLAG_HARDIRQ = 0x04,
99 TRACE_FLAG_SOFTIRQ = 0x08,
100};
101
102enum trace_iterator_flags {
103 TRACE_ITER_PRINT_PARENT = 0x01,
104 TRACE_ITER_SYM_OFFSET = 0x02,
105 TRACE_ITER_SYM_ADDR = 0x04,
106 TRACE_ITER_VERBOSE = 0x08,
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200107 TRACE_ITER_RAW = 0x10,
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200108 TRACE_ITER_HEX = 0x20,
109 TRACE_ITER_BIN = 0x40,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200110 TRACE_ITER_BLOCK = 0x80,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200111};
112
113#define TRACE_ITER_SYM_MASK \
114 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
115
116/* These must match the bit postions above */
117static const char *trace_options[] = {
118 "print-parent",
119 "sym-offset",
120 "sym-addr",
121 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200122 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200123 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200124 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200125 "block",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200126 NULL
127};
128
129static unsigned trace_flags;
130
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200131static DEFINE_SPINLOCK(ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200132
133/*
134 * Copy the new maximum trace into the separate maximum-trace
135 * structure. (this way the maximum trace is permanently saved,
136 * for later retrieval via /debugfs/tracing/latency_trace)
137 */
Ingo Molnare309b412008-05-12 21:20:51 +0200138static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200139__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
140{
141 struct trace_array_cpu *data = tr->data[cpu];
142
143 max_tr.cpu = cpu;
144 max_tr.time_start = data->preempt_timestamp;
145
146 data = max_tr.data[cpu];
147 data->saved_latency = tracing_max_latency;
148
149 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
150 data->pid = tsk->pid;
151 data->uid = tsk->uid;
152 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
153 data->policy = tsk->policy;
154 data->rt_priority = tsk->rt_priority;
155
156 /* record this tasks comm */
157 tracing_record_cmdline(current);
158}
159
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200160void check_pages(struct trace_array_cpu *data)
161{
162 struct page *page, *tmp;
163
164 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
165 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
166
167 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
168 BUG_ON(page->lru.next->prev != &page->lru);
169 BUG_ON(page->lru.prev->next != &page->lru);
170 }
171}
172
173void *head_page(struct trace_array_cpu *data)
174{
175 struct page *page;
176
177 check_pages(data);
178 if (list_empty(&data->trace_pages))
179 return NULL;
180
181 page = list_entry(data->trace_pages.next, struct page, lru);
182 BUG_ON(&page->lru == &data->trace_pages);
183
184 return page_address(page);
185}
186
Ingo Molnare309b412008-05-12 21:20:51 +0200187static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200188trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
189{
190 int len = (PAGE_SIZE - 1) - s->len;
191 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200192 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200193
194 if (!len)
195 return 0;
196
197 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200198 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200199 va_end(ap);
200
Steven Rostedtb3806b42008-05-12 21:20:46 +0200201 /* If we can't write it all, don't bother writing anything */
202 if (ret > len)
203 return 0;
204
205 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200206
207 return len;
208}
209
Ingo Molnare309b412008-05-12 21:20:51 +0200210static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200211trace_seq_puts(struct trace_seq *s, const char *str)
212{
213 int len = strlen(str);
214
215 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200216 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200217
218 memcpy(s->buffer + s->len, str, len);
219 s->len += len;
220
221 return len;
222}
223
Ingo Molnare309b412008-05-12 21:20:51 +0200224static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200225trace_seq_putc(struct trace_seq *s, unsigned char c)
226{
227 if (s->len >= (PAGE_SIZE - 1))
228 return 0;
229
230 s->buffer[s->len++] = c;
231
232 return 1;
233}
234
Ingo Molnare309b412008-05-12 21:20:51 +0200235static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200236trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
237{
238 if (len > ((PAGE_SIZE - 1) - s->len))
239 return 0;
240
241 memcpy(s->buffer + s->len, mem, len);
242 s->len += len;
243
244 return len;
245}
246
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200247#define HEX_CHARS 17
248
Ingo Molnare309b412008-05-12 21:20:51 +0200249static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200250trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
251{
252 unsigned char hex[HEX_CHARS];
253 unsigned char *data;
254 unsigned char byte;
255 int i, j;
256
257 BUG_ON(len >= HEX_CHARS);
258
259 data = mem;
260
261#ifdef __BIG_ENDIAN
262 for (i = 0, j = 0; i < len; i++) {
263#else
264 for (i = len-1, j = 0; i >= 0; i--) {
265#endif
266 byte = data[i];
267
268 hex[j] = byte & 0x0f;
269 if (hex[j] >= 10)
270 hex[j] += 'a' - 10;
271 else
272 hex[j] += '0';
273 j++;
274
275 hex[j] = byte >> 4;
276 if (hex[j] >= 10)
277 hex[j] += 'a' - 10;
278 else
279 hex[j] += '0';
280 j++;
281 }
282 hex[j] = ' ';
283 j++;
284
285 return trace_seq_putmem(s, hex, j);
286}
287
Ingo Molnare309b412008-05-12 21:20:51 +0200288static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200289trace_seq_reset(struct trace_seq *s)
290{
291 s->len = 0;
292}
293
Ingo Molnare309b412008-05-12 21:20:51 +0200294static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200295trace_print_seq(struct seq_file *m, struct trace_seq *s)
296{
297 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
298
299 s->buffer[len] = 0;
300 seq_puts(m, s->buffer);
301
302 trace_seq_reset(s);
303}
304
Ingo Molnare309b412008-05-12 21:20:51 +0200305static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200306flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
307{
308 struct list_head flip_pages;
309
310 INIT_LIST_HEAD(&flip_pages);
311
Steven Rostedt93a588f2008-05-12 21:20:45 +0200312 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200313 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200314 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200315
316 check_pages(tr1);
317 check_pages(tr2);
318 list_splice_init(&tr1->trace_pages, &flip_pages);
319 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
320 list_splice_init(&flip_pages, &tr2->trace_pages);
321 BUG_ON(!list_empty(&flip_pages));
322 check_pages(tr1);
323 check_pages(tr2);
324}
325
Ingo Molnare309b412008-05-12 21:20:51 +0200326void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200327update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
328{
329 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200330 int i;
331
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200332 WARN_ON_ONCE(!irqs_disabled());
333 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200334 /* clear out all the previous traces */
335 for_each_possible_cpu(i) {
336 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200337 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200338 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200339 }
340
341 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200342 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200343}
344
345/**
346 * update_max_tr_single - only copy one trace over, and reset the rest
347 * @tr - tracer
348 * @tsk - task with the latency
349 * @cpu - the cpu of the buffer to copy.
350 */
Ingo Molnare309b412008-05-12 21:20:51 +0200351void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200352update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
353{
354 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200355 int i;
356
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200357 WARN_ON_ONCE(!irqs_disabled());
358 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200359 for_each_possible_cpu(i)
360 tracing_reset(max_tr.data[i]);
361
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200362 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200363 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200364
365 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200366 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200367}
368
369int register_tracer(struct tracer *type)
370{
371 struct tracer *t;
372 int len;
373 int ret = 0;
374
375 if (!type->name) {
376 pr_info("Tracer must have a name\n");
377 return -1;
378 }
379
380 mutex_lock(&trace_types_lock);
381 for (t = trace_types; t; t = t->next) {
382 if (strcmp(type->name, t->name) == 0) {
383 /* already found */
384 pr_info("Trace %s already registered\n",
385 type->name);
386 ret = -1;
387 goto out;
388 }
389 }
390
Steven Rostedt60a11772008-05-12 21:20:44 +0200391#ifdef CONFIG_FTRACE_STARTUP_TEST
392 if (type->selftest) {
393 struct tracer *saved_tracer = current_trace;
394 struct trace_array_cpu *data;
395 struct trace_array *tr = &global_trace;
396 int saved_ctrl = tr->ctrl;
397 int i;
398 /*
399 * Run a selftest on this tracer.
400 * Here we reset the trace buffer, and set the current
401 * tracer to be this tracer. The tracer can then run some
402 * internal tracing to verify that everything is in order.
403 * If we fail, we do not register this tracer.
404 */
405 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200406 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200407 if (!head_page(data))
408 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200409 tracing_reset(data);
410 }
411 current_trace = type;
412 tr->ctrl = 0;
413 /* the test is responsible for initializing and enabling */
414 pr_info("Testing tracer %s: ", type->name);
415 ret = type->selftest(type, tr);
416 /* the test is responsible for resetting too */
417 current_trace = saved_tracer;
418 tr->ctrl = saved_ctrl;
419 if (ret) {
420 printk(KERN_CONT "FAILED!\n");
421 goto out;
422 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200423 /* Only reset on passing, to avoid touching corrupted buffers */
424 for_each_possible_cpu(i) {
425 data = tr->data[i];
426 if (!head_page(data))
427 continue;
428 tracing_reset(data);
429 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200430 printk(KERN_CONT "PASSED\n");
431 }
432#endif
433
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200434 type->next = trace_types;
435 trace_types = type;
436 len = strlen(type->name);
437 if (len > max_tracer_type_len)
438 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200439
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200440 out:
441 mutex_unlock(&trace_types_lock);
442
443 return ret;
444}
445
446void unregister_tracer(struct tracer *type)
447{
448 struct tracer **t;
449 int len;
450
451 mutex_lock(&trace_types_lock);
452 for (t = &trace_types; *t; t = &(*t)->next) {
453 if (*t == type)
454 goto found;
455 }
456 pr_info("Trace %s not registered\n", type->name);
457 goto out;
458
459 found:
460 *t = (*t)->next;
461 if (strlen(type->name) != max_tracer_type_len)
462 goto out;
463
464 max_tracer_type_len = 0;
465 for (t = &trace_types; *t; t = &(*t)->next) {
466 len = strlen((*t)->name);
467 if (len > max_tracer_type_len)
468 max_tracer_type_len = len;
469 }
470 out:
471 mutex_unlock(&trace_types_lock);
472}
473
Ingo Molnare309b412008-05-12 21:20:51 +0200474void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200475{
476 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200477 data->trace_head = data->trace_tail = head_page(data);
478 data->trace_head_idx = 0;
479 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200480}
481
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200482#define SAVED_CMDLINES 128
483static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
484static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
485static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
486static int cmdline_idx;
487static DEFINE_SPINLOCK(trace_cmdline_lock);
488atomic_t trace_record_cmdline_disabled;
489
490static void trace_init_cmdlines(void)
491{
492 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
493 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
494 cmdline_idx = 0;
495}
496
Ingo Molnare309b412008-05-12 21:20:51 +0200497void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200498
Ingo Molnare309b412008-05-12 21:20:51 +0200499static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200500{
501 unsigned map;
502 unsigned idx;
503
504 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
505 return;
506
507 /*
508 * It's not the end of the world if we don't get
509 * the lock, but we also don't want to spin
510 * nor do we want to disable interrupts,
511 * so if we miss here, then better luck next time.
512 */
513 if (!spin_trylock(&trace_cmdline_lock))
514 return;
515
516 idx = map_pid_to_cmdline[tsk->pid];
517 if (idx >= SAVED_CMDLINES) {
518 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
519
520 map = map_cmdline_to_pid[idx];
521 if (map <= PID_MAX_DEFAULT)
522 map_pid_to_cmdline[map] = (unsigned)-1;
523
524 map_pid_to_cmdline[tsk->pid] = idx;
525
526 cmdline_idx = idx;
527 }
528
529 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
530
531 spin_unlock(&trace_cmdline_lock);
532}
533
Ingo Molnare309b412008-05-12 21:20:51 +0200534static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200535{
536 char *cmdline = "<...>";
537 unsigned map;
538
539 if (!pid)
540 return "<idle>";
541
542 if (pid > PID_MAX_DEFAULT)
543 goto out;
544
545 map = map_pid_to_cmdline[pid];
546 if (map >= SAVED_CMDLINES)
547 goto out;
548
549 cmdline = saved_cmdlines[map];
550
551 out:
552 return cmdline;
553}
554
Ingo Molnare309b412008-05-12 21:20:51 +0200555void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200556{
557 if (atomic_read(&trace_record_cmdline_disabled))
558 return;
559
560 trace_save_cmdline(tsk);
561}
562
Ingo Molnare309b412008-05-12 21:20:51 +0200563static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200564trace_next_list(struct trace_array_cpu *data, struct list_head *next)
565{
566 /*
567 * Roundrobin - but skip the head (which is not a real page):
568 */
569 next = next->next;
570 if (unlikely(next == &data->trace_pages))
571 next = next->next;
572 BUG_ON(next == &data->trace_pages);
573
574 return next;
575}
576
Ingo Molnare309b412008-05-12 21:20:51 +0200577static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200578trace_next_page(struct trace_array_cpu *data, void *addr)
579{
580 struct list_head *next;
581 struct page *page;
582
583 page = virt_to_page(addr);
584
585 next = trace_next_list(data, &page->lru);
586 page = list_entry(next, struct page, lru);
587
588 return page_address(page);
589}
590
Ingo Molnare309b412008-05-12 21:20:51 +0200591static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200592tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200593{
594 unsigned long idx, idx_next;
595 struct trace_entry *entry;
596
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200597 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200598 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200599 idx_next = idx + 1;
600
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200601 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
602
Steven Rostedt93a588f2008-05-12 21:20:45 +0200603 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200604
605 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200606 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200607 idx_next = 0;
608 }
609
Steven Rostedt93a588f2008-05-12 21:20:45 +0200610 if (data->trace_head == data->trace_tail &&
611 idx_next == data->trace_tail_idx) {
612 /* overrun */
613 data->trace_tail_idx++;
614 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
615 data->trace_tail =
616 trace_next_page(data, data->trace_tail);
617 data->trace_tail_idx = 0;
618 }
619 }
620
621 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200622
623 return entry;
624}
625
Ingo Molnare309b412008-05-12 21:20:51 +0200626static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200627tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200628{
629 struct task_struct *tsk = current;
630 unsigned long pc;
631
632 pc = preempt_count();
633
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200634 entry->preempt_count = pc & 0xff;
635 entry->pid = tsk->pid;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200636 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200637 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
638 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
639 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
640 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
641}
642
Ingo Molnare309b412008-05-12 21:20:51 +0200643void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200644trace_function(struct trace_array *tr, struct trace_array_cpu *data,
645 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200646{
647 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200648 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200649
Ingo Molnardcb63082008-05-12 21:20:48 +0200650 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200651 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200652 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200653 entry->type = TRACE_FN;
654 entry->fn.ip = ip;
655 entry->fn.parent_ip = parent_ip;
Ingo Molnardcb63082008-05-12 21:20:48 +0200656 spin_unlock_irqrestore(&data->lock, irq_flags);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200657
658 if (!(trace_flags & TRACE_ITER_BLOCK))
659 wake_up (&trace_wait);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200660}
661
Ingo Molnare309b412008-05-12 21:20:51 +0200662void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200663ftrace(struct trace_array *tr, struct trace_array_cpu *data,
664 unsigned long ip, unsigned long parent_ip, unsigned long flags)
665{
666 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200667 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200668}
669
Ingo Molnare309b412008-05-12 21:20:51 +0200670void
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200671trace_special(struct trace_array *tr, struct trace_array_cpu *data,
672 unsigned long arg1, unsigned long arg2, unsigned long arg3)
673{
674 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200675 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200676
Ingo Molnardcb63082008-05-12 21:20:48 +0200677 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200678 entry = tracing_get_trace_entry(tr, data);
679 tracing_generic_entry_update(entry, 0);
680 entry->type = TRACE_SPECIAL;
681 entry->special.arg1 = arg1;
682 entry->special.arg2 = arg2;
683 entry->special.arg3 = arg3;
Ingo Molnardcb63082008-05-12 21:20:48 +0200684 spin_unlock_irqrestore(&data->lock, irq_flags);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200685
686 if (!(trace_flags & TRACE_ITER_BLOCK))
687 wake_up (&trace_wait);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200688}
689
Ingo Molnare309b412008-05-12 21:20:51 +0200690void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200691tracing_sched_switch_trace(struct trace_array *tr,
692 struct trace_array_cpu *data,
693 struct task_struct *prev, struct task_struct *next,
694 unsigned long flags)
695{
696 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200697 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200698
Ingo Molnardcb63082008-05-12 21:20:48 +0200699 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200700 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200701 tracing_generic_entry_update(entry, flags);
702 entry->type = TRACE_CTX;
703 entry->ctx.prev_pid = prev->pid;
704 entry->ctx.prev_prio = prev->prio;
705 entry->ctx.prev_state = prev->state;
706 entry->ctx.next_pid = next->pid;
707 entry->ctx.next_prio = next->prio;
Ingo Molnardcb63082008-05-12 21:20:48 +0200708 spin_unlock_irqrestore(&data->lock, irq_flags);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200709
710 if (!(trace_flags & TRACE_ITER_BLOCK))
711 wake_up (&trace_wait);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200712}
713
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200714#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200715static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200716function_trace_call(unsigned long ip, unsigned long parent_ip)
717{
718 struct trace_array *tr = &global_trace;
719 struct trace_array_cpu *data;
720 unsigned long flags;
721 long disabled;
722 int cpu;
723
724 if (unlikely(!tracer_enabled))
725 return;
726
727 local_irq_save(flags);
728 cpu = raw_smp_processor_id();
729 data = tr->data[cpu];
730 disabled = atomic_inc_return(&data->disabled);
731
732 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200733 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200734
735 atomic_dec(&data->disabled);
736 local_irq_restore(flags);
737}
738
739static struct ftrace_ops trace_ops __read_mostly =
740{
741 .func = function_trace_call,
742};
743
Ingo Molnare309b412008-05-12 21:20:51 +0200744void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200745{
746 register_ftrace_function(&trace_ops);
747}
748
Ingo Molnare309b412008-05-12 21:20:51 +0200749void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200750{
751 unregister_ftrace_function(&trace_ops);
752}
753#endif
754
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200755enum trace_file_type {
756 TRACE_FILE_LAT_FMT = 1,
757};
758
759static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200760trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
761 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200762{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200763 struct page *page;
764 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200765
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200766 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200767 iter->next_idx[cpu] >= data->trace_idx ||
768 (data->trace_head == data->trace_tail &&
769 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200770 return NULL;
771
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200772 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200773 /* Initialize the iterator for this cpu trace buffer */
774 WARN_ON(!data->trace_tail);
775 page = virt_to_page(data->trace_tail);
776 iter->next_page[cpu] = &page->lru;
777 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200778 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200779
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200780 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200781 BUG_ON(&data->trace_pages == &page->lru);
782
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200783 array = page_address(page);
784
Steven Rostedt93a588f2008-05-12 21:20:45 +0200785 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200786 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200787}
788
Ingo Molnare309b412008-05-12 21:20:51 +0200789static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200790find_next_entry(struct trace_iterator *iter, int *ent_cpu)
791{
792 struct trace_array *tr = iter->tr;
793 struct trace_entry *ent, *next = NULL;
794 int next_cpu = -1;
795 int cpu;
796
797 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200798 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200799 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200800 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200801 /*
802 * Pick the entry with the smallest timestamp:
803 */
804 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200805 next = ent;
806 next_cpu = cpu;
807 }
808 }
809
810 if (ent_cpu)
811 *ent_cpu = next_cpu;
812
813 return next;
814}
815
Ingo Molnare309b412008-05-12 21:20:51 +0200816static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200817{
818 iter->idx++;
819 iter->next_idx[iter->cpu]++;
820 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200821
Steven Rostedtb3806b42008-05-12 21:20:46 +0200822 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
823 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
824
825 iter->next_page_idx[iter->cpu] = 0;
826 iter->next_page[iter->cpu] =
827 trace_next_list(data, iter->next_page[iter->cpu]);
828 }
829}
830
Ingo Molnare309b412008-05-12 21:20:51 +0200831static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200832{
833 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
834
835 data->trace_tail_idx++;
836 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
837 data->trace_tail = trace_next_page(data, data->trace_tail);
838 data->trace_tail_idx = 0;
839 }
840
841 /* Check if we empty it, then reset the index */
842 if (data->trace_head == data->trace_tail &&
843 data->trace_head_idx == data->trace_tail_idx)
844 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200845}
846
Ingo Molnare309b412008-05-12 21:20:51 +0200847static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200848{
849 struct trace_entry *next;
850 int next_cpu = -1;
851
852 next = find_next_entry(iter, &next_cpu);
853
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200854 iter->prev_ent = iter->ent;
855 iter->prev_cpu = iter->cpu;
856
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200857 iter->ent = next;
858 iter->cpu = next_cpu;
859
Steven Rostedtb3806b42008-05-12 21:20:46 +0200860 if (next)
861 trace_iterator_increment(iter);
862
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200863 return next ? iter : NULL;
864}
865
Ingo Molnare309b412008-05-12 21:20:51 +0200866static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200867{
868 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200869 void *last_ent = iter->ent;
870 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200871 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200872
873 (*pos)++;
874
875 /* can't go backwards */
876 if (iter->idx > i)
877 return NULL;
878
879 if (iter->idx < 0)
880 ent = find_next_entry_inc(iter);
881 else
882 ent = iter;
883
884 while (ent && iter->idx < i)
885 ent = find_next_entry_inc(iter);
886
887 iter->pos = *pos;
888
889 if (last_ent && !ent)
890 seq_puts(m, "\n\nvim:ft=help\n");
891
892 return ent;
893}
894
895static void *s_start(struct seq_file *m, loff_t *pos)
896{
897 struct trace_iterator *iter = m->private;
898 void *p = NULL;
899 loff_t l = 0;
900 int i;
901
902 mutex_lock(&trace_types_lock);
903
904 if (!current_trace || current_trace != iter->trace)
905 return NULL;
906
907 atomic_inc(&trace_record_cmdline_disabled);
908
909 /* let the tracer grab locks here if needed */
910 if (current_trace->start)
911 current_trace->start(iter);
912
913 if (*pos != iter->pos) {
914 iter->ent = NULL;
915 iter->cpu = 0;
916 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200917 iter->prev_ent = NULL;
918 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200919
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200920 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200921 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200922 iter->next_page[i] = NULL;
923 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200924
925 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
926 ;
927
928 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200929 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200930 p = s_next(m, p, &l);
931 }
932
933 return p;
934}
935
936static void s_stop(struct seq_file *m, void *p)
937{
938 struct trace_iterator *iter = m->private;
939
940 atomic_dec(&trace_record_cmdline_disabled);
941
942 /* let the tracer release locks here if needed */
943 if (current_trace && current_trace == iter->trace && iter->trace->stop)
944 iter->trace->stop(iter);
945
946 mutex_unlock(&trace_types_lock);
947}
948
Steven Rostedtb3806b42008-05-12 21:20:46 +0200949static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200950seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200951{
952#ifdef CONFIG_KALLSYMS
953 char str[KSYM_SYMBOL_LEN];
954
955 kallsyms_lookup(address, NULL, NULL, NULL, str);
956
Steven Rostedtb3806b42008-05-12 21:20:46 +0200957 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200958#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200959 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200960}
961
Steven Rostedtb3806b42008-05-12 21:20:46 +0200962static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200963seq_print_sym_offset(struct trace_seq *s, const char *fmt,
964 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200965{
966#ifdef CONFIG_KALLSYMS
967 char str[KSYM_SYMBOL_LEN];
968
969 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200970 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200971#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200972 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200973}
974
975#ifndef CONFIG_64BIT
976# define IP_FMT "%08lx"
977#else
978# define IP_FMT "%016lx"
979#endif
980
Ingo Molnare309b412008-05-12 21:20:51 +0200981static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200982seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200983{
Steven Rostedtb3806b42008-05-12 21:20:46 +0200984 int ret;
985
986 if (!ip)
987 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200988
989 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200990 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200991 else
Steven Rostedtb3806b42008-05-12 21:20:46 +0200992 ret = seq_print_sym_short(s, "%s", ip);
993
994 if (!ret)
995 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200996
997 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200998 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
999 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001000}
1001
Ingo Molnare309b412008-05-12 21:20:51 +02001002static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001003{
1004 seq_puts(m, "# _------=> CPU# \n");
1005 seq_puts(m, "# / _-----=> irqs-off \n");
1006 seq_puts(m, "# | / _----=> need-resched \n");
1007 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1008 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1009 seq_puts(m, "# |||| / \n");
1010 seq_puts(m, "# ||||| delay \n");
1011 seq_puts(m, "# cmd pid ||||| time | caller \n");
1012 seq_puts(m, "# \\ / ||||| \\ | / \n");
1013}
1014
Ingo Molnare309b412008-05-12 21:20:51 +02001015static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016{
1017 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1018 seq_puts(m, "# | | | | |\n");
1019}
1020
1021
Ingo Molnare309b412008-05-12 21:20:51 +02001022static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001023print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1024{
1025 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1026 struct trace_array *tr = iter->tr;
1027 struct trace_array_cpu *data = tr->data[tr->cpu];
1028 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001029 unsigned long total = 0;
1030 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001031 int cpu;
1032 const char *name = "preemption";
1033
1034 if (type)
1035 name = type->name;
1036
1037 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001038 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001039 total += tr->data[cpu]->trace_idx;
1040 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001041 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001042 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001043 entries += tr->data[cpu]->trace_idx;
1044 }
1045 }
1046
1047 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1048 name, UTS_RELEASE);
1049 seq_puts(m, "-----------------------------------"
1050 "---------------------------------\n");
1051 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1052 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001053 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001054 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001055 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056 tr->cpu,
1057#if defined(CONFIG_PREEMPT_NONE)
1058 "server",
1059#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1060 "desktop",
1061#elif defined(CONFIG_PREEMPT_DESKTOP)
1062 "preempt",
1063#else
1064 "unknown",
1065#endif
1066 /* These are reserved for later use */
1067 0, 0, 0, 0);
1068#ifdef CONFIG_SMP
1069 seq_printf(m, " #P:%d)\n", num_online_cpus());
1070#else
1071 seq_puts(m, ")\n");
1072#endif
1073 seq_puts(m, " -----------------\n");
1074 seq_printf(m, " | task: %.16s-%d "
1075 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1076 data->comm, data->pid, data->uid, data->nice,
1077 data->policy, data->rt_priority);
1078 seq_puts(m, " -----------------\n");
1079
1080 if (data->critical_start) {
1081 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001082 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1083 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001084 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001085 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1086 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001087 seq_puts(m, "\n");
1088 }
1089
1090 seq_puts(m, "\n");
1091}
1092
Ingo Molnare309b412008-05-12 21:20:51 +02001093static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001094lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001095{
1096 int hardirq, softirq;
1097 char *comm;
1098
1099 comm = trace_find_cmdline(entry->pid);
1100
Steven Rostedt214023c2008-05-12 21:20:46 +02001101 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1102 trace_seq_printf(s, "%d", cpu);
1103 trace_seq_printf(s, "%c%c",
1104 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1105 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001106
1107 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1108 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1109 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001110 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001111 else {
1112 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001113 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001114 else {
1115 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001116 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001117 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001118 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001119 }
1120 }
1121
1122 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001123 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001124 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001125 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001126}
1127
1128unsigned long preempt_mark_thresh = 100;
1129
Ingo Molnare309b412008-05-12 21:20:51 +02001130static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001131lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001132 unsigned long rel_usecs)
1133{
Steven Rostedt214023c2008-05-12 21:20:46 +02001134 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001135 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001136 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001137 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001138 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001139 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001140 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001141}
1142
1143static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1144
Ingo Molnare309b412008-05-12 21:20:51 +02001145static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001146print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001147{
Steven Rostedt214023c2008-05-12 21:20:46 +02001148 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001149 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1150 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1151 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1152 struct trace_entry *entry = iter->ent;
1153 unsigned long abs_usecs;
1154 unsigned long rel_usecs;
1155 char *comm;
1156 int S;
1157
1158 if (!next_entry)
1159 next_entry = entry;
1160 rel_usecs = ns2usecs(next_entry->t - entry->t);
1161 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1162
1163 if (verbose) {
1164 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001165 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1166 " %ld.%03ldms (+%ld.%03ldms): ",
1167 comm,
1168 entry->pid, cpu, entry->flags,
1169 entry->preempt_count, trace_idx,
1170 ns2usecs(entry->t),
1171 abs_usecs/1000,
1172 abs_usecs % 1000, rel_usecs/1000,
1173 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174 } else {
Steven Rostedt214023c2008-05-12 21:20:46 +02001175 lat_print_generic(s, entry, cpu);
1176 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001177 }
1178 switch (entry->type) {
1179 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001180 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1181 trace_seq_puts(s, " (");
1182 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1183 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001184 break;
1185 case TRACE_CTX:
1186 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1187 state_to_char[entry->ctx.prev_state] : 'X';
1188 comm = trace_find_cmdline(entry->ctx.next_pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001189 trace_seq_printf(s, " %d:%d:%c --> %d:%d %s\n",
1190 entry->ctx.prev_pid,
1191 entry->ctx.prev_prio,
1192 S,
1193 entry->ctx.next_pid,
1194 entry->ctx.next_prio,
1195 comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001196 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001197 case TRACE_SPECIAL:
1198 trace_seq_printf(s, " %lx %lx %lx\n",
1199 entry->special.arg1,
1200 entry->special.arg2,
1201 entry->special.arg3);
1202 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001203 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001204 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001205 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001206 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001207}
1208
Ingo Molnare309b412008-05-12 21:20:51 +02001209static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001210{
Steven Rostedt214023c2008-05-12 21:20:46 +02001211 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001212 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001213 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001214 unsigned long usec_rem;
1215 unsigned long long t;
1216 unsigned long secs;
1217 char *comm;
1218 int S;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001219 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001220
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001221 entry = iter->ent;
1222
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001223 comm = trace_find_cmdline(iter->ent->pid);
1224
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001225 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001226 usec_rem = do_div(t, 1000000ULL);
1227 secs = (unsigned long)t;
1228
Steven Rostedtb3806b42008-05-12 21:20:46 +02001229 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1230 if (!ret)
1231 return 0;
1232 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1233 if (!ret)
1234 return 0;
1235 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1236 if (!ret)
1237 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001238
1239 switch (entry->type) {
1240 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001241 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1242 if (!ret)
1243 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001244 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1245 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001246 ret = trace_seq_printf(s, " <-");
1247 if (!ret)
1248 return 0;
1249 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1250 sym_flags);
1251 if (!ret)
1252 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001253 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001254 ret = trace_seq_printf(s, "\n");
1255 if (!ret)
1256 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001257 break;
1258 case TRACE_CTX:
1259 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1260 state_to_char[entry->ctx.prev_state] : 'X';
Steven Rostedtb3806b42008-05-12 21:20:46 +02001261 ret = trace_seq_printf(s, " %d:%d:%c ==> %d:%d\n",
1262 entry->ctx.prev_pid,
1263 entry->ctx.prev_prio,
1264 S,
1265 entry->ctx.next_pid,
1266 entry->ctx.next_prio);
1267 if (!ret)
1268 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001269 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001270 case TRACE_SPECIAL:
1271 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1272 entry->special.arg1,
1273 entry->special.arg2,
1274 entry->special.arg3);
1275 if (!ret)
1276 return 0;
1277 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001278 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001279 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001280}
1281
Ingo Molnare309b412008-05-12 21:20:51 +02001282static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001283{
1284 struct trace_seq *s = &iter->seq;
1285 struct trace_entry *entry;
1286 int ret;
1287 int S;
1288
1289 entry = iter->ent;
1290
1291 ret = trace_seq_printf(s, "%d %d %llu ",
1292 entry->pid, iter->cpu, entry->t);
1293 if (!ret)
1294 return 0;
1295
1296 switch (entry->type) {
1297 case TRACE_FN:
1298 ret = trace_seq_printf(s, "%x %x\n",
1299 entry->fn.ip, entry->fn.parent_ip);
1300 if (!ret)
1301 return 0;
1302 break;
1303 case TRACE_CTX:
1304 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1305 state_to_char[entry->ctx.prev_state] : 'X';
1306 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1307 entry->ctx.prev_pid,
1308 entry->ctx.prev_prio,
1309 S,
1310 entry->ctx.next_pid,
1311 entry->ctx.next_prio);
1312 if (!ret)
1313 return 0;
1314 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001315 case TRACE_SPECIAL:
1316 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1317 entry->special.arg1,
1318 entry->special.arg2,
1319 entry->special.arg3);
1320 if (!ret)
1321 return 0;
1322 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001323 }
1324 return 1;
1325}
1326
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001327#define SEQ_PUT_FIELD_RET(s, x) \
1328do { \
1329 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1330 return 0; \
1331} while (0)
1332
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001333#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1334do { \
1335 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1336 return 0; \
1337} while (0)
1338
Ingo Molnare309b412008-05-12 21:20:51 +02001339static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001340{
1341 struct trace_seq *s = &iter->seq;
1342 unsigned char newline = '\n';
1343 struct trace_entry *entry;
1344 int S;
1345
1346 entry = iter->ent;
1347
1348 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1349 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1350 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1351
1352 switch (entry->type) {
1353 case TRACE_FN:
1354 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1355 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1356 break;
1357 case TRACE_CTX:
1358 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1359 state_to_char[entry->ctx.prev_state] : 'X';
1360 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1361 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1362 SEQ_PUT_HEX_FIELD_RET(s, S);
1363 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1364 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1365 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1366 break;
1367 case TRACE_SPECIAL:
1368 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1369 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1370 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1371 break;
1372 }
1373 SEQ_PUT_FIELD_RET(s, newline);
1374
1375 return 1;
1376}
1377
Ingo Molnare309b412008-05-12 21:20:51 +02001378static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001379{
1380 struct trace_seq *s = &iter->seq;
1381 struct trace_entry *entry;
1382
1383 entry = iter->ent;
1384
1385 SEQ_PUT_FIELD_RET(s, entry->pid);
1386 SEQ_PUT_FIELD_RET(s, entry->cpu);
1387 SEQ_PUT_FIELD_RET(s, entry->t);
1388
1389 switch (entry->type) {
1390 case TRACE_FN:
1391 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1392 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1393 break;
1394 case TRACE_CTX:
1395 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1396 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1397 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1398 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1399 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1400 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001401 case TRACE_SPECIAL:
1402 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1403 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1404 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1405 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001406 }
1407 return 1;
1408}
1409
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001410static int trace_empty(struct trace_iterator *iter)
1411{
1412 struct trace_array_cpu *data;
1413 int cpu;
1414
1415 for_each_possible_cpu(cpu) {
1416 data = iter->tr->data[cpu];
1417
Steven Rostedtb3806b42008-05-12 21:20:46 +02001418 if (head_page(data) && data->trace_idx &&
1419 (data->trace_tail != data->trace_head ||
1420 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001421 return 0;
1422 }
1423 return 1;
1424}
1425
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001426static int print_trace_line(struct trace_iterator *iter)
1427{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001428 if (trace_flags & TRACE_ITER_BIN)
1429 return print_bin_fmt(iter);
1430
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001431 if (trace_flags & TRACE_ITER_HEX)
1432 return print_hex_fmt(iter);
1433
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001434 if (trace_flags & TRACE_ITER_RAW)
1435 return print_raw_fmt(iter);
1436
1437 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1438 return print_lat_fmt(iter, iter->idx, iter->cpu);
1439
1440 return print_trace_fmt(iter);
1441}
1442
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001443static int s_show(struct seq_file *m, void *v)
1444{
1445 struct trace_iterator *iter = v;
1446
1447 if (iter->ent == NULL) {
1448 if (iter->tr) {
1449 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1450 seq_puts(m, "#\n");
1451 }
1452 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1453 /* print nothing if the buffers are empty */
1454 if (trace_empty(iter))
1455 return 0;
1456 print_trace_header(m, iter);
1457 if (!(trace_flags & TRACE_ITER_VERBOSE))
1458 print_lat_help_header(m);
1459 } else {
1460 if (!(trace_flags & TRACE_ITER_VERBOSE))
1461 print_func_help_header(m);
1462 }
1463 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001464 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001465 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001466 }
1467
1468 return 0;
1469}
1470
1471static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001472 .start = s_start,
1473 .next = s_next,
1474 .stop = s_stop,
1475 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001476};
1477
Ingo Molnare309b412008-05-12 21:20:51 +02001478static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001479__tracing_open(struct inode *inode, struct file *file, int *ret)
1480{
1481 struct trace_iterator *iter;
1482
Steven Rostedt60a11772008-05-12 21:20:44 +02001483 if (tracing_disabled) {
1484 *ret = -ENODEV;
1485 return NULL;
1486 }
1487
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001488 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1489 if (!iter) {
1490 *ret = -ENOMEM;
1491 goto out;
1492 }
1493
1494 mutex_lock(&trace_types_lock);
1495 if (current_trace && current_trace->print_max)
1496 iter->tr = &max_tr;
1497 else
1498 iter->tr = inode->i_private;
1499 iter->trace = current_trace;
1500 iter->pos = -1;
1501
1502 /* TODO stop tracer */
1503 *ret = seq_open(file, &tracer_seq_ops);
1504 if (!*ret) {
1505 struct seq_file *m = file->private_data;
1506 m->private = iter;
1507
1508 /* stop the trace while dumping */
1509 if (iter->tr->ctrl)
1510 tracer_enabled = 0;
1511
1512 if (iter->trace && iter->trace->open)
1513 iter->trace->open(iter);
1514 } else {
1515 kfree(iter);
1516 iter = NULL;
1517 }
1518 mutex_unlock(&trace_types_lock);
1519
1520 out:
1521 return iter;
1522}
1523
1524int tracing_open_generic(struct inode *inode, struct file *filp)
1525{
Steven Rostedt60a11772008-05-12 21:20:44 +02001526 if (tracing_disabled)
1527 return -ENODEV;
1528
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001529 filp->private_data = inode->i_private;
1530 return 0;
1531}
1532
1533int tracing_release(struct inode *inode, struct file *file)
1534{
1535 struct seq_file *m = (struct seq_file *)file->private_data;
1536 struct trace_iterator *iter = m->private;
1537
1538 mutex_lock(&trace_types_lock);
1539 if (iter->trace && iter->trace->close)
1540 iter->trace->close(iter);
1541
1542 /* reenable tracing if it was previously enabled */
1543 if (iter->tr->ctrl)
1544 tracer_enabled = 1;
1545 mutex_unlock(&trace_types_lock);
1546
1547 seq_release(inode, file);
1548 kfree(iter);
1549 return 0;
1550}
1551
1552static int tracing_open(struct inode *inode, struct file *file)
1553{
1554 int ret;
1555
1556 __tracing_open(inode, file, &ret);
1557
1558 return ret;
1559}
1560
1561static int tracing_lt_open(struct inode *inode, struct file *file)
1562{
1563 struct trace_iterator *iter;
1564 int ret;
1565
1566 iter = __tracing_open(inode, file, &ret);
1567
1568 if (!ret)
1569 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1570
1571 return ret;
1572}
1573
1574
Ingo Molnare309b412008-05-12 21:20:51 +02001575static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001576t_next(struct seq_file *m, void *v, loff_t *pos)
1577{
1578 struct tracer *t = m->private;
1579
1580 (*pos)++;
1581
1582 if (t)
1583 t = t->next;
1584
1585 m->private = t;
1586
1587 return t;
1588}
1589
1590static void *t_start(struct seq_file *m, loff_t *pos)
1591{
1592 struct tracer *t = m->private;
1593 loff_t l = 0;
1594
1595 mutex_lock(&trace_types_lock);
1596 for (; t && l < *pos; t = t_next(m, t, &l))
1597 ;
1598
1599 return t;
1600}
1601
1602static void t_stop(struct seq_file *m, void *p)
1603{
1604 mutex_unlock(&trace_types_lock);
1605}
1606
1607static int t_show(struct seq_file *m, void *v)
1608{
1609 struct tracer *t = v;
1610
1611 if (!t)
1612 return 0;
1613
1614 seq_printf(m, "%s", t->name);
1615 if (t->next)
1616 seq_putc(m, ' ');
1617 else
1618 seq_putc(m, '\n');
1619
1620 return 0;
1621}
1622
1623static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001624 .start = t_start,
1625 .next = t_next,
1626 .stop = t_stop,
1627 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001628};
1629
1630static int show_traces_open(struct inode *inode, struct file *file)
1631{
1632 int ret;
1633
Steven Rostedt60a11772008-05-12 21:20:44 +02001634 if (tracing_disabled)
1635 return -ENODEV;
1636
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001637 ret = seq_open(file, &show_traces_seq_ops);
1638 if (!ret) {
1639 struct seq_file *m = file->private_data;
1640 m->private = trace_types;
1641 }
1642
1643 return ret;
1644}
1645
1646static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001647 .open = tracing_open,
1648 .read = seq_read,
1649 .llseek = seq_lseek,
1650 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001651};
1652
1653static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001654 .open = tracing_lt_open,
1655 .read = seq_read,
1656 .llseek = seq_lseek,
1657 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001658};
1659
1660static struct file_operations show_traces_fops = {
1661 .open = show_traces_open,
1662 .read = seq_read,
1663 .release = seq_release,
1664};
1665
1666static ssize_t
1667tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1668 size_t cnt, loff_t *ppos)
1669{
1670 char *buf;
1671 int r = 0;
1672 int len = 0;
1673 int i;
1674
1675 /* calulate max size */
1676 for (i = 0; trace_options[i]; i++) {
1677 len += strlen(trace_options[i]);
1678 len += 3; /* "no" and space */
1679 }
1680
1681 /* +2 for \n and \0 */
1682 buf = kmalloc(len + 2, GFP_KERNEL);
1683 if (!buf)
1684 return -ENOMEM;
1685
1686 for (i = 0; trace_options[i]; i++) {
1687 if (trace_flags & (1 << i))
1688 r += sprintf(buf + r, "%s ", trace_options[i]);
1689 else
1690 r += sprintf(buf + r, "no%s ", trace_options[i]);
1691 }
1692
1693 r += sprintf(buf + r, "\n");
1694 WARN_ON(r >= len + 2);
1695
1696 r = simple_read_from_buffer(ubuf, cnt, ppos,
1697 buf, r);
1698
1699 kfree(buf);
1700
1701 return r;
1702}
1703
1704static ssize_t
1705tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1706 size_t cnt, loff_t *ppos)
1707{
1708 char buf[64];
1709 char *cmp = buf;
1710 int neg = 0;
1711 int i;
1712
1713 if (cnt > 63)
1714 cnt = 63;
1715
1716 if (copy_from_user(&buf, ubuf, cnt))
1717 return -EFAULT;
1718
1719 buf[cnt] = 0;
1720
1721 if (strncmp(buf, "no", 2) == 0) {
1722 neg = 1;
1723 cmp += 2;
1724 }
1725
1726 for (i = 0; trace_options[i]; i++) {
1727 int len = strlen(trace_options[i]);
1728
1729 if (strncmp(cmp, trace_options[i], len) == 0) {
1730 if (neg)
1731 trace_flags &= ~(1 << i);
1732 else
1733 trace_flags |= (1 << i);
1734 break;
1735 }
1736 }
1737
1738 filp->f_pos += cnt;
1739
1740 return cnt;
1741}
1742
1743static struct file_operations tracing_iter_fops = {
1744 .open = tracing_open_generic,
1745 .read = tracing_iter_ctrl_read,
1746 .write = tracing_iter_ctrl_write,
1747};
1748
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001749static const char readme_msg[] =
1750 "tracing mini-HOWTO:\n\n"
1751 "# mkdir /debug\n"
1752 "# mount -t debugfs nodev /debug\n\n"
1753 "# cat /debug/tracing/available_tracers\n"
1754 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1755 "# cat /debug/tracing/current_tracer\n"
1756 "none\n"
1757 "# echo sched_switch > /debug/tracing/current_tracer\n"
1758 "# cat /debug/tracing/current_tracer\n"
1759 "sched_switch\n"
1760 "# cat /debug/tracing/iter_ctrl\n"
1761 "noprint-parent nosym-offset nosym-addr noverbose\n"
1762 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1763 "# echo 1 > /debug/tracing/tracing_enabled\n"
1764 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1765 "echo 0 > /debug/tracing/tracing_enabled\n"
1766;
1767
1768static ssize_t
1769tracing_readme_read(struct file *filp, char __user *ubuf,
1770 size_t cnt, loff_t *ppos)
1771{
1772 return simple_read_from_buffer(ubuf, cnt, ppos,
1773 readme_msg, strlen(readme_msg));
1774}
1775
1776static struct file_operations tracing_readme_fops = {
1777 .open = tracing_open_generic,
1778 .read = tracing_readme_read,
1779};
1780
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001781static ssize_t
1782tracing_ctrl_read(struct file *filp, char __user *ubuf,
1783 size_t cnt, loff_t *ppos)
1784{
1785 struct trace_array *tr = filp->private_data;
1786 char buf[64];
1787 int r;
1788
1789 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001790 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001791}
1792
1793static ssize_t
1794tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1795 size_t cnt, loff_t *ppos)
1796{
1797 struct trace_array *tr = filp->private_data;
1798 long val;
1799 char buf[64];
1800
1801 if (cnt > 63)
1802 cnt = 63;
1803
1804 if (copy_from_user(&buf, ubuf, cnt))
1805 return -EFAULT;
1806
1807 buf[cnt] = 0;
1808
1809 val = simple_strtoul(buf, NULL, 10);
1810
1811 val = !!val;
1812
1813 mutex_lock(&trace_types_lock);
1814 if (tr->ctrl ^ val) {
1815 if (val)
1816 tracer_enabled = 1;
1817 else
1818 tracer_enabled = 0;
1819
1820 tr->ctrl = val;
1821
1822 if (current_trace && current_trace->ctrl_update)
1823 current_trace->ctrl_update(tr);
1824 }
1825 mutex_unlock(&trace_types_lock);
1826
1827 filp->f_pos += cnt;
1828
1829 return cnt;
1830}
1831
1832static ssize_t
1833tracing_set_trace_read(struct file *filp, char __user *ubuf,
1834 size_t cnt, loff_t *ppos)
1835{
1836 char buf[max_tracer_type_len+2];
1837 int r;
1838
1839 mutex_lock(&trace_types_lock);
1840 if (current_trace)
1841 r = sprintf(buf, "%s\n", current_trace->name);
1842 else
1843 r = sprintf(buf, "\n");
1844 mutex_unlock(&trace_types_lock);
1845
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001846 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001847}
1848
1849static ssize_t
1850tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1851 size_t cnt, loff_t *ppos)
1852{
1853 struct trace_array *tr = &global_trace;
1854 struct tracer *t;
1855 char buf[max_tracer_type_len+1];
1856 int i;
1857
1858 if (cnt > max_tracer_type_len)
1859 cnt = max_tracer_type_len;
1860
1861 if (copy_from_user(&buf, ubuf, cnt))
1862 return -EFAULT;
1863
1864 buf[cnt] = 0;
1865
1866 /* strip ending whitespace. */
1867 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1868 buf[i] = 0;
1869
1870 mutex_lock(&trace_types_lock);
1871 for (t = trace_types; t; t = t->next) {
1872 if (strcmp(t->name, buf) == 0)
1873 break;
1874 }
1875 if (!t || t == current_trace)
1876 goto out;
1877
1878 if (current_trace && current_trace->reset)
1879 current_trace->reset(tr);
1880
1881 current_trace = t;
1882 if (t->init)
1883 t->init(tr);
1884
1885 out:
1886 mutex_unlock(&trace_types_lock);
1887
1888 filp->f_pos += cnt;
1889
1890 return cnt;
1891}
1892
1893static ssize_t
1894tracing_max_lat_read(struct file *filp, char __user *ubuf,
1895 size_t cnt, loff_t *ppos)
1896{
1897 unsigned long *ptr = filp->private_data;
1898 char buf[64];
1899 int r;
1900
1901 r = snprintf(buf, 64, "%ld\n",
1902 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1903 if (r > 64)
1904 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001905 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001906}
1907
1908static ssize_t
1909tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1910 size_t cnt, loff_t *ppos)
1911{
1912 long *ptr = filp->private_data;
1913 long val;
1914 char buf[64];
1915
1916 if (cnt > 63)
1917 cnt = 63;
1918
1919 if (copy_from_user(&buf, ubuf, cnt))
1920 return -EFAULT;
1921
1922 buf[cnt] = 0;
1923
1924 val = simple_strtoul(buf, NULL, 10);
1925
1926 *ptr = val * 1000;
1927
1928 return cnt;
1929}
1930
Steven Rostedtb3806b42008-05-12 21:20:46 +02001931static atomic_t tracing_reader;
1932
1933static int tracing_open_pipe(struct inode *inode, struct file *filp)
1934{
1935 struct trace_iterator *iter;
1936
1937 if (tracing_disabled)
1938 return -ENODEV;
1939
1940 /* We only allow for reader of the pipe */
1941 if (atomic_inc_return(&tracing_reader) != 1) {
1942 atomic_dec(&tracing_reader);
1943 return -EBUSY;
1944 }
1945
1946 /* create a buffer to store the information to pass to userspace */
1947 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1948 if (!iter)
1949 return -ENOMEM;
1950
1951 iter->tr = &global_trace;
1952
1953 filp->private_data = iter;
1954
1955 return 0;
1956}
1957
1958static int tracing_release_pipe(struct inode *inode, struct file *file)
1959{
1960 struct trace_iterator *iter = file->private_data;
1961
1962 kfree(iter);
1963 atomic_dec(&tracing_reader);
1964
1965 return 0;
1966}
1967
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02001968static unsigned int
1969tracing_poll_pipe(struct file *filp, poll_table *poll_table)
1970{
1971 struct trace_iterator *iter = filp->private_data;
1972
1973 if (trace_flags & TRACE_ITER_BLOCK) {
1974 /*
1975 * Always select as readable when in blocking mode
1976 */
1977 return POLLIN | POLLRDNORM;
1978 }
1979 else {
1980 if (!trace_empty(iter))
1981 return POLLIN | POLLRDNORM;
1982 poll_wait(filp, &trace_wait, poll_table);
1983 if (!trace_empty(iter))
1984 return POLLIN | POLLRDNORM;
1985
1986 return 0;
1987 }
1988}
1989
Steven Rostedtb3806b42008-05-12 21:20:46 +02001990/*
1991 * Consumer reader.
1992 */
1993static ssize_t
1994tracing_read_pipe(struct file *filp, char __user *ubuf,
1995 size_t cnt, loff_t *ppos)
1996{
1997 struct trace_iterator *iter = filp->private_data;
1998 struct trace_array_cpu *data;
1999 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002000 static int start;
2001 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002002#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002003 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002004#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002005 int read = 0;
2006 int cpu;
2007 int len;
2008 int ret;
2009
2010 /* return any leftover data */
2011 if (iter->seq.len > start) {
2012 len = iter->seq.len - start;
2013 if (cnt > len)
2014 cnt = len;
2015 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2016 if (ret)
2017 cnt = -EFAULT;
2018
2019 start += len;
2020
2021 return cnt;
2022 }
2023
2024 trace_seq_reset(&iter->seq);
2025 start = 0;
2026
2027 while (trace_empty(iter)) {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002028 if (!(trace_flags & TRACE_ITER_BLOCK))
2029 return -EWOULDBLOCK;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002030 /*
2031 * This is a make-shift waitqueue. The reason we don't use
2032 * an actual wait queue is because:
2033 * 1) we only ever have one waiter
2034 * 2) the tracing, traces all functions, we don't want
2035 * the overhead of calling wake_up and friends
2036 * (and tracing them too)
2037 * Anyway, this is really very primitive wakeup.
2038 */
2039 set_current_state(TASK_INTERRUPTIBLE);
2040 iter->tr->waiter = current;
2041
2042 /* sleep for one second, and try again. */
2043 schedule_timeout(HZ);
2044
2045 iter->tr->waiter = NULL;
2046
2047 if (signal_pending(current))
2048 return -EINTR;
2049
2050 /*
2051 * We block until we read something and tracing is disabled.
2052 * We still block if tracing is disabled, but we have never
2053 * read anything. This allows a user to cat this file, and
2054 * then enable tracing. But after we have read something,
2055 * we give an EOF when tracing is again disabled.
2056 *
2057 * iter->pos will be 0 if we haven't read anything.
2058 */
2059 if (!tracer_enabled && iter->pos)
2060 break;
2061
2062 continue;
2063 }
2064
2065 /* stop when tracing is finished */
2066 if (trace_empty(iter))
2067 return 0;
2068
2069 if (cnt >= PAGE_SIZE)
2070 cnt = PAGE_SIZE - 1;
2071
2072 memset(iter, 0, sizeof(*iter));
2073 iter->tr = &global_trace;
2074 iter->pos = -1;
2075
2076 /*
2077 * We need to stop all tracing on all CPUS to read the
2078 * the next buffer. This is a bit expensive, but is
2079 * not done often. We fill all what we can read,
2080 * and then release the locks again.
2081 */
2082
2083 cpus_clear(mask);
2084 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002085#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002086 ftrace_save = ftrace_enabled;
2087 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002088#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002089 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002090 for_each_possible_cpu(cpu) {
2091 data = iter->tr->data[cpu];
2092
2093 if (!head_page(data) || !data->trace_idx)
2094 continue;
2095
2096 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002097 cpu_set(cpu, mask);
2098 }
2099
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002100 for_each_cpu_mask(cpu, mask) {
2101 data = iter->tr->data[cpu];
2102 spin_lock(&data->lock);
2103 }
2104
Steven Rostedt088b1e422008-05-12 21:20:48 +02002105 while (find_next_entry_inc(iter) != NULL) {
2106 int len = iter->seq.len;
2107
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002108 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002109 if (!ret) {
2110 /* don't print partial lines */
2111 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002112 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002113 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002114
2115 trace_consume(iter);
2116
2117 if (iter->seq.len >= cnt)
2118 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002119 }
2120
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002121 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002122 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002123 spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002124 }
2125
2126 for_each_cpu_mask(cpu, mask) {
2127 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002128 atomic_dec(&data->disabled);
2129 }
Ingo Molnar25770462008-05-12 21:20:49 +02002130#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002131 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002132#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002133 local_irq_restore(flags);
2134
2135 /* Now copy what we have to the user */
2136 read = iter->seq.len;
2137 if (read > cnt)
2138 read = cnt;
2139
2140 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2141
2142 if (read < iter->seq.len)
2143 start = read;
2144 else
2145 trace_seq_reset(&iter->seq);
2146
2147 if (ret)
2148 read = -EFAULT;
2149
2150 return read;
2151}
2152
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002153static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002154 .open = tracing_open_generic,
2155 .read = tracing_max_lat_read,
2156 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002157};
2158
2159static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002160 .open = tracing_open_generic,
2161 .read = tracing_ctrl_read,
2162 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002163};
2164
2165static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002166 .open = tracing_open_generic,
2167 .read = tracing_set_trace_read,
2168 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002169};
2170
Steven Rostedtb3806b42008-05-12 21:20:46 +02002171static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002172 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002173 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002174 .read = tracing_read_pipe,
2175 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002176};
2177
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002178#ifdef CONFIG_DYNAMIC_FTRACE
2179
2180static ssize_t
2181tracing_read_long(struct file *filp, char __user *ubuf,
2182 size_t cnt, loff_t *ppos)
2183{
2184 unsigned long *p = filp->private_data;
2185 char buf[64];
2186 int r;
2187
2188 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002189
2190 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002191}
2192
2193static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002194 .open = tracing_open_generic,
2195 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002196};
2197#endif
2198
2199static struct dentry *d_tracer;
2200
2201struct dentry *tracing_init_dentry(void)
2202{
2203 static int once;
2204
2205 if (d_tracer)
2206 return d_tracer;
2207
2208 d_tracer = debugfs_create_dir("tracing", NULL);
2209
2210 if (!d_tracer && !once) {
2211 once = 1;
2212 pr_warning("Could not create debugfs directory 'tracing'\n");
2213 return NULL;
2214 }
2215
2216 return d_tracer;
2217}
2218
Steven Rostedt60a11772008-05-12 21:20:44 +02002219#ifdef CONFIG_FTRACE_SELFTEST
2220/* Let selftest have access to static functions in this file */
2221#include "trace_selftest.c"
2222#endif
2223
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002224static __init void tracer_init_debugfs(void)
2225{
2226 struct dentry *d_tracer;
2227 struct dentry *entry;
2228
2229 d_tracer = tracing_init_dentry();
2230
2231 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2232 &global_trace, &tracing_ctrl_fops);
2233 if (!entry)
2234 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2235
2236 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2237 NULL, &tracing_iter_fops);
2238 if (!entry)
2239 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2240
2241 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2242 &global_trace, &tracing_lt_fops);
2243 if (!entry)
2244 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2245
2246 entry = debugfs_create_file("trace", 0444, d_tracer,
2247 &global_trace, &tracing_fops);
2248 if (!entry)
2249 pr_warning("Could not create debugfs 'trace' entry\n");
2250
2251 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2252 &global_trace, &show_traces_fops);
2253 if (!entry)
2254 pr_warning("Could not create debugfs 'trace' entry\n");
2255
2256 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2257 &global_trace, &set_tracer_fops);
2258 if (!entry)
2259 pr_warning("Could not create debugfs 'trace' entry\n");
2260
2261 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2262 &tracing_max_latency,
2263 &tracing_max_lat_fops);
2264 if (!entry)
2265 pr_warning("Could not create debugfs "
2266 "'tracing_max_latency' entry\n");
2267
2268 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2269 &tracing_thresh, &tracing_max_lat_fops);
2270 if (!entry)
2271 pr_warning("Could not create debugfs "
2272 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002273 entry = debugfs_create_file("README", 0644, d_tracer,
2274 NULL, &tracing_readme_fops);
2275 if (!entry)
2276 pr_warning("Could not create debugfs 'README' entry\n");
2277
Steven Rostedtb3806b42008-05-12 21:20:46 +02002278 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2279 NULL, &tracing_pipe_fops);
2280 if (!entry)
2281 pr_warning("Could not create debugfs "
2282 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002283
2284#ifdef CONFIG_DYNAMIC_FTRACE
2285 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2286 &ftrace_update_tot_cnt,
2287 &tracing_read_long_fops);
2288 if (!entry)
2289 pr_warning("Could not create debugfs "
2290 "'dyn_ftrace_total_info' entry\n");
2291#endif
2292}
2293
2294/* dummy trace to disable tracing */
2295static struct tracer no_tracer __read_mostly =
2296{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002297 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002298};
2299
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002300static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002301{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002302 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002303 struct page *page, *tmp;
2304 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002305 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002306 int i;
2307
2308 /* first allocate a page for each CPU */
2309 for_each_possible_cpu(i) {
2310 array = (void *)__get_free_page(GFP_KERNEL);
2311 if (array == NULL) {
2312 printk(KERN_ERR "tracer: failed to allocate page"
2313 "for trace buffer!\n");
2314 goto free_pages;
2315 }
2316
2317 page = virt_to_page(array);
2318 list_add(&page->lru, &pages);
2319
2320/* Only allocate if we are actually using the max trace */
2321#ifdef CONFIG_TRACER_MAX_TRACE
2322 array = (void *)__get_free_page(GFP_KERNEL);
2323 if (array == NULL) {
2324 printk(KERN_ERR "tracer: failed to allocate page"
2325 "for trace buffer!\n");
2326 goto free_pages;
2327 }
2328 page = virt_to_page(array);
2329 list_add(&page->lru, &pages);
2330#endif
2331 }
2332
2333 /* Now that we successfully allocate a page per CPU, add them */
2334 for_each_possible_cpu(i) {
2335 data = global_trace.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002336 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002337 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002338 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002339 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002340 list_add_tail(&page->lru, &data->trace_pages);
2341 ClearPageLRU(page);
2342
2343#ifdef CONFIG_TRACER_MAX_TRACE
2344 data = max_tr.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002345 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002346 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002347 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002348 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002349 list_add_tail(&page->lru, &data->trace_pages);
2350 SetPageLRU(page);
2351#endif
2352 }
2353 global_trace.entries += ENTRIES_PER_PAGE;
2354
2355 return 0;
2356
2357 free_pages:
2358 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002359 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002360 __free_page(page);
2361 }
2362 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002363}
2364
2365__init static int tracer_alloc_buffers(void)
2366{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002367 struct trace_array_cpu *data;
2368 void *array;
2369 struct page *page;
2370 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002371 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002372 int i;
2373
Steven Rostedt26994ea2008-05-12 21:20:48 +02002374 global_trace.ctrl = tracer_enabled;
2375
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002376 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002377 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002378 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002379 max_tr.data[i] = &per_cpu(max_data, i);
2380
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002381 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002382 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002383 printk(KERN_ERR "tracer: failed to allocate page"
2384 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002385 goto free_buffers;
2386 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002387
2388 /* set the array to the list */
2389 INIT_LIST_HEAD(&data->trace_pages);
2390 page = virt_to_page(array);
2391 list_add(&page->lru, &data->trace_pages);
2392 /* use the LRU flag to differentiate the two buffers */
2393 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002394
2395/* Only allocate if we are actually using the max trace */
2396#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002397 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002398 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002399 printk(KERN_ERR "tracer: failed to allocate page"
2400 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002401 goto free_buffers;
2402 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002403
2404 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2405 page = virt_to_page(array);
2406 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2407 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002408#endif
2409 }
2410
2411 /*
2412 * Since we allocate by orders of pages, we may be able to
2413 * round up a bit.
2414 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002415 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002416 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002417
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002418 while (global_trace.entries < trace_nr_entries) {
2419 if (trace_alloc_page())
2420 break;
2421 pages++;
2422 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002423 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002424
2425 pr_info("tracer: %d pages allocated for %ld",
2426 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002427 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2428 pr_info(" actual entries %ld\n", global_trace.entries);
2429
2430 tracer_init_debugfs();
2431
2432 trace_init_cmdlines();
2433
2434 register_tracer(&no_tracer);
2435 current_trace = &no_tracer;
2436
Steven Rostedt60a11772008-05-12 21:20:44 +02002437 /* All seems OK, enable tracing */
2438 tracing_disabled = 0;
2439
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002440 return 0;
2441
2442 free_buffers:
2443 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002444 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002445 struct trace_array_cpu *data = global_trace.data[i];
2446
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002447 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002448 list_for_each_entry_safe(page, tmp,
2449 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002450 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002451 __free_page(page);
2452 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002453 }
2454
2455#ifdef CONFIG_TRACER_MAX_TRACE
2456 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002457 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002458 list_for_each_entry_safe(page, tmp,
2459 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002460 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002461 __free_page(page);
2462 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002463 }
2464#endif
2465 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002466 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002467}
Steven Rostedt60a11772008-05-12 21:20:44 +02002468fs_initcall(tracer_alloc_buffers);