blob: 192c1354a7e0a35572b45830d5a129674456b018 [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;
Ingo Molnar57422792008-05-12 21:20:51 +020060static unsigned long trace_nr_entries = 65536UL;
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 Molnar57422792008-05-12 21:20:51 +020090 TRACE_WAKE,
Ingo Molnarf0a920d2008-05-12 21:20:47 +020091 TRACE_SPECIAL,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +020092
93 __TRACE_LAST_TYPE
94};
95
96enum trace_flag_type {
97 TRACE_FLAG_IRQS_OFF = 0x01,
98 TRACE_FLAG_NEED_RESCHED = 0x02,
99 TRACE_FLAG_HARDIRQ = 0x04,
100 TRACE_FLAG_SOFTIRQ = 0x08,
101};
102
103enum trace_iterator_flags {
104 TRACE_ITER_PRINT_PARENT = 0x01,
105 TRACE_ITER_SYM_OFFSET = 0x02,
106 TRACE_ITER_SYM_ADDR = 0x04,
107 TRACE_ITER_VERBOSE = 0x08,
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200108 TRACE_ITER_RAW = 0x10,
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200109 TRACE_ITER_HEX = 0x20,
110 TRACE_ITER_BIN = 0x40,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200111 TRACE_ITER_BLOCK = 0x80,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200112};
113
114#define TRACE_ITER_SYM_MASK \
115 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
116
117/* These must match the bit postions above */
118static const char *trace_options[] = {
119 "print-parent",
120 "sym-offset",
121 "sym-addr",
122 "verbose",
Ingo Molnarf9896bf2008-05-12 21:20:47 +0200123 "raw",
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200124 "hex",
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200125 "bin",
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200126 "block",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200127 NULL
128};
129
130static unsigned trace_flags;
131
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200132static DEFINE_SPINLOCK(ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200133
134/*
135 * Copy the new maximum trace into the separate maximum-trace
136 * structure. (this way the maximum trace is permanently saved,
137 * for later retrieval via /debugfs/tracing/latency_trace)
138 */
Ingo Molnare309b412008-05-12 21:20:51 +0200139static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200140__update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
141{
142 struct trace_array_cpu *data = tr->data[cpu];
143
144 max_tr.cpu = cpu;
145 max_tr.time_start = data->preempt_timestamp;
146
147 data = max_tr.data[cpu];
148 data->saved_latency = tracing_max_latency;
149
150 memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
151 data->pid = tsk->pid;
152 data->uid = tsk->uid;
153 data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
154 data->policy = tsk->policy;
155 data->rt_priority = tsk->rt_priority;
156
157 /* record this tasks comm */
158 tracing_record_cmdline(current);
159}
160
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200161void check_pages(struct trace_array_cpu *data)
162{
163 struct page *page, *tmp;
164
165 BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
166 BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
167
168 list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
169 BUG_ON(page->lru.next->prev != &page->lru);
170 BUG_ON(page->lru.prev->next != &page->lru);
171 }
172}
173
174void *head_page(struct trace_array_cpu *data)
175{
176 struct page *page;
177
178 check_pages(data);
179 if (list_empty(&data->trace_pages))
180 return NULL;
181
182 page = list_entry(data->trace_pages.next, struct page, lru);
183 BUG_ON(&page->lru == &data->trace_pages);
184
185 return page_address(page);
186}
187
Ingo Molnare309b412008-05-12 21:20:51 +0200188static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200189trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
190{
191 int len = (PAGE_SIZE - 1) - s->len;
192 va_list ap;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200193 int ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200194
195 if (!len)
196 return 0;
197
198 va_start(ap, fmt);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200199 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
Steven Rostedt214023c2008-05-12 21:20:46 +0200200 va_end(ap);
201
Steven Rostedtb3806b42008-05-12 21:20:46 +0200202 /* If we can't write it all, don't bother writing anything */
203 if (ret > len)
204 return 0;
205
206 s->len += ret;
Steven Rostedt214023c2008-05-12 21:20:46 +0200207
208 return len;
209}
210
Ingo Molnare309b412008-05-12 21:20:51 +0200211static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200212trace_seq_puts(struct trace_seq *s, const char *str)
213{
214 int len = strlen(str);
215
216 if (len > ((PAGE_SIZE - 1) - s->len))
Steven Rostedtb3806b42008-05-12 21:20:46 +0200217 return 0;
Steven Rostedt214023c2008-05-12 21:20:46 +0200218
219 memcpy(s->buffer + s->len, str, len);
220 s->len += len;
221
222 return len;
223}
224
Ingo Molnare309b412008-05-12 21:20:51 +0200225static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200226trace_seq_putc(struct trace_seq *s, unsigned char c)
227{
228 if (s->len >= (PAGE_SIZE - 1))
229 return 0;
230
231 s->buffer[s->len++] = c;
232
233 return 1;
234}
235
Ingo Molnare309b412008-05-12 21:20:51 +0200236static int
Ingo Molnarcb0f12a2008-05-12 21:20:47 +0200237trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
238{
239 if (len > ((PAGE_SIZE - 1) - s->len))
240 return 0;
241
242 memcpy(s->buffer + s->len, mem, len);
243 s->len += len;
244
245 return len;
246}
247
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200248#define HEX_CHARS 17
249
Ingo Molnare309b412008-05-12 21:20:51 +0200250static int
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +0200251trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
252{
253 unsigned char hex[HEX_CHARS];
254 unsigned char *data;
255 unsigned char byte;
256 int i, j;
257
258 BUG_ON(len >= HEX_CHARS);
259
260 data = mem;
261
262#ifdef __BIG_ENDIAN
263 for (i = 0, j = 0; i < len; i++) {
264#else
265 for (i = len-1, j = 0; i >= 0; i--) {
266#endif
267 byte = data[i];
268
269 hex[j] = byte & 0x0f;
270 if (hex[j] >= 10)
271 hex[j] += 'a' - 10;
272 else
273 hex[j] += '0';
274 j++;
275
276 hex[j] = byte >> 4;
277 if (hex[j] >= 10)
278 hex[j] += 'a' - 10;
279 else
280 hex[j] += '0';
281 j++;
282 }
283 hex[j] = ' ';
284 j++;
285
286 return trace_seq_putmem(s, hex, j);
287}
288
Ingo Molnare309b412008-05-12 21:20:51 +0200289static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200290trace_seq_reset(struct trace_seq *s)
291{
292 s->len = 0;
293}
294
Ingo Molnare309b412008-05-12 21:20:51 +0200295static void
Steven Rostedt214023c2008-05-12 21:20:46 +0200296trace_print_seq(struct seq_file *m, struct trace_seq *s)
297{
298 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
299
300 s->buffer[len] = 0;
301 seq_puts(m, s->buffer);
302
303 trace_seq_reset(s);
304}
305
Ingo Molnare309b412008-05-12 21:20:51 +0200306static void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200307flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
308{
309 struct list_head flip_pages;
310
311 INIT_LIST_HEAD(&flip_pages);
312
Steven Rostedt93a588f2008-05-12 21:20:45 +0200313 memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200314 sizeof(struct trace_array_cpu) -
Steven Rostedt93a588f2008-05-12 21:20:45 +0200315 offsetof(struct trace_array_cpu, trace_head_idx));
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200316
317 check_pages(tr1);
318 check_pages(tr2);
319 list_splice_init(&tr1->trace_pages, &flip_pages);
320 list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
321 list_splice_init(&flip_pages, &tr2->trace_pages);
322 BUG_ON(!list_empty(&flip_pages));
323 check_pages(tr1);
324 check_pages(tr2);
325}
326
Ingo Molnare309b412008-05-12 21:20:51 +0200327void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200328update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
329{
330 struct trace_array_cpu *data;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200331 int i;
332
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200333 WARN_ON_ONCE(!irqs_disabled());
334 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200335 /* clear out all the previous traces */
336 for_each_possible_cpu(i) {
337 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200338 flip_trace(max_tr.data[i], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200339 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200340 }
341
342 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200343 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200344}
345
346/**
347 * update_max_tr_single - only copy one trace over, and reset the rest
348 * @tr - tracer
349 * @tsk - task with the latency
350 * @cpu - the cpu of the buffer to copy.
351 */
Ingo Molnare309b412008-05-12 21:20:51 +0200352void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200353update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
354{
355 struct trace_array_cpu *data = tr->data[cpu];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200356 int i;
357
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200358 WARN_ON_ONCE(!irqs_disabled());
359 spin_lock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200360 for_each_possible_cpu(i)
361 tracing_reset(max_tr.data[i]);
362
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200363 flip_trace(max_tr.data[cpu], data);
Steven Rostedt89b2f972008-05-12 21:20:44 +0200364 tracing_reset(data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200365
366 __update_max_tr(tr, tsk, cpu);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200367 spin_unlock(&ftrace_max_lock);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200368}
369
370int register_tracer(struct tracer *type)
371{
372 struct tracer *t;
373 int len;
374 int ret = 0;
375
376 if (!type->name) {
377 pr_info("Tracer must have a name\n");
378 return -1;
379 }
380
381 mutex_lock(&trace_types_lock);
382 for (t = trace_types; t; t = t->next) {
383 if (strcmp(type->name, t->name) == 0) {
384 /* already found */
385 pr_info("Trace %s already registered\n",
386 type->name);
387 ret = -1;
388 goto out;
389 }
390 }
391
Steven Rostedt60a11772008-05-12 21:20:44 +0200392#ifdef CONFIG_FTRACE_STARTUP_TEST
393 if (type->selftest) {
394 struct tracer *saved_tracer = current_trace;
395 struct trace_array_cpu *data;
396 struct trace_array *tr = &global_trace;
397 int saved_ctrl = tr->ctrl;
398 int i;
399 /*
400 * Run a selftest on this tracer.
401 * Here we reset the trace buffer, and set the current
402 * tracer to be this tracer. The tracer can then run some
403 * internal tracing to verify that everything is in order.
404 * If we fail, we do not register this tracer.
405 */
406 for_each_possible_cpu(i) {
Steven Rostedt60a11772008-05-12 21:20:44 +0200407 data = tr->data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200408 if (!head_page(data))
409 continue;
Steven Rostedt60a11772008-05-12 21:20:44 +0200410 tracing_reset(data);
411 }
412 current_trace = type;
413 tr->ctrl = 0;
414 /* the test is responsible for initializing and enabling */
415 pr_info("Testing tracer %s: ", type->name);
416 ret = type->selftest(type, tr);
417 /* the test is responsible for resetting too */
418 current_trace = saved_tracer;
419 tr->ctrl = saved_ctrl;
420 if (ret) {
421 printk(KERN_CONT "FAILED!\n");
422 goto out;
423 }
Steven Rostedt1d4db002008-05-12 21:20:45 +0200424 /* Only reset on passing, to avoid touching corrupted buffers */
425 for_each_possible_cpu(i) {
426 data = tr->data[i];
427 if (!head_page(data))
428 continue;
429 tracing_reset(data);
430 }
Steven Rostedt60a11772008-05-12 21:20:44 +0200431 printk(KERN_CONT "PASSED\n");
432 }
433#endif
434
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200435 type->next = trace_types;
436 trace_types = type;
437 len = strlen(type->name);
438 if (len > max_tracer_type_len)
439 max_tracer_type_len = len;
Steven Rostedt60a11772008-05-12 21:20:44 +0200440
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200441 out:
442 mutex_unlock(&trace_types_lock);
443
444 return ret;
445}
446
447void unregister_tracer(struct tracer *type)
448{
449 struct tracer **t;
450 int len;
451
452 mutex_lock(&trace_types_lock);
453 for (t = &trace_types; *t; t = &(*t)->next) {
454 if (*t == type)
455 goto found;
456 }
457 pr_info("Trace %s not registered\n", type->name);
458 goto out;
459
460 found:
461 *t = (*t)->next;
462 if (strlen(type->name) != max_tracer_type_len)
463 goto out;
464
465 max_tracer_type_len = 0;
466 for (t = &trace_types; *t; t = &(*t)->next) {
467 len = strlen((*t)->name);
468 if (len > max_tracer_type_len)
469 max_tracer_type_len = len;
470 }
471 out:
472 mutex_unlock(&trace_types_lock);
473}
474
Ingo Molnare309b412008-05-12 21:20:51 +0200475void tracing_reset(struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200476{
477 data->trace_idx = 0;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200478 data->trace_head = data->trace_tail = head_page(data);
479 data->trace_head_idx = 0;
480 data->trace_tail_idx = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200481}
482
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200483#define SAVED_CMDLINES 128
484static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
485static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
486static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
487static int cmdline_idx;
488static DEFINE_SPINLOCK(trace_cmdline_lock);
489atomic_t trace_record_cmdline_disabled;
490
491static void trace_init_cmdlines(void)
492{
493 memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
494 memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
495 cmdline_idx = 0;
496}
497
Ingo Molnare309b412008-05-12 21:20:51 +0200498void trace_stop_cmdline_recording(void);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200499
Ingo Molnare309b412008-05-12 21:20:51 +0200500static void trace_save_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200501{
502 unsigned map;
503 unsigned idx;
504
505 if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
506 return;
507
508 /*
509 * It's not the end of the world if we don't get
510 * the lock, but we also don't want to spin
511 * nor do we want to disable interrupts,
512 * so if we miss here, then better luck next time.
513 */
514 if (!spin_trylock(&trace_cmdline_lock))
515 return;
516
517 idx = map_pid_to_cmdline[tsk->pid];
518 if (idx >= SAVED_CMDLINES) {
519 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
520
521 map = map_cmdline_to_pid[idx];
522 if (map <= PID_MAX_DEFAULT)
523 map_pid_to_cmdline[map] = (unsigned)-1;
524
525 map_pid_to_cmdline[tsk->pid] = idx;
526
527 cmdline_idx = idx;
528 }
529
530 memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
531
532 spin_unlock(&trace_cmdline_lock);
533}
534
Ingo Molnare309b412008-05-12 21:20:51 +0200535static char *trace_find_cmdline(int pid)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200536{
537 char *cmdline = "<...>";
538 unsigned map;
539
540 if (!pid)
541 return "<idle>";
542
543 if (pid > PID_MAX_DEFAULT)
544 goto out;
545
546 map = map_pid_to_cmdline[pid];
547 if (map >= SAVED_CMDLINES)
548 goto out;
549
550 cmdline = saved_cmdlines[map];
551
552 out:
553 return cmdline;
554}
555
Ingo Molnare309b412008-05-12 21:20:51 +0200556void tracing_record_cmdline(struct task_struct *tsk)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200557{
558 if (atomic_read(&trace_record_cmdline_disabled))
559 return;
560
561 trace_save_cmdline(tsk);
562}
563
Ingo Molnare309b412008-05-12 21:20:51 +0200564static inline struct list_head *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200565trace_next_list(struct trace_array_cpu *data, struct list_head *next)
566{
567 /*
568 * Roundrobin - but skip the head (which is not a real page):
569 */
570 next = next->next;
571 if (unlikely(next == &data->trace_pages))
572 next = next->next;
573 BUG_ON(next == &data->trace_pages);
574
575 return next;
576}
577
Ingo Molnare309b412008-05-12 21:20:51 +0200578static inline void *
Steven Rostedt93a588f2008-05-12 21:20:45 +0200579trace_next_page(struct trace_array_cpu *data, void *addr)
580{
581 struct list_head *next;
582 struct page *page;
583
584 page = virt_to_page(addr);
585
586 next = trace_next_list(data, &page->lru);
587 page = list_entry(next, struct page, lru);
588
589 return page_address(page);
590}
591
Ingo Molnare309b412008-05-12 21:20:51 +0200592static inline struct trace_entry *
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200593tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200594{
595 unsigned long idx, idx_next;
596 struct trace_entry *entry;
597
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200598 data->trace_idx++;
Steven Rostedt93a588f2008-05-12 21:20:45 +0200599 idx = data->trace_head_idx;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200600 idx_next = idx + 1;
601
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200602 BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
603
Steven Rostedt93a588f2008-05-12 21:20:45 +0200604 entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200605
606 if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200607 data->trace_head = trace_next_page(data, data->trace_head);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200608 idx_next = 0;
609 }
610
Steven Rostedt93a588f2008-05-12 21:20:45 +0200611 if (data->trace_head == data->trace_tail &&
612 idx_next == data->trace_tail_idx) {
613 /* overrun */
614 data->trace_tail_idx++;
615 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
616 data->trace_tail =
617 trace_next_page(data, data->trace_tail);
618 data->trace_tail_idx = 0;
619 }
620 }
621
622 data->trace_head_idx = idx_next;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200623
624 return entry;
625}
626
Ingo Molnare309b412008-05-12 21:20:51 +0200627static inline void
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200628tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200629{
630 struct task_struct *tsk = current;
631 unsigned long pc;
632
633 pc = preempt_count();
634
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200635 entry->preempt_count = pc & 0xff;
636 entry->pid = tsk->pid;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200637 entry->t = ftrace_now(raw_smp_processor_id());
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200638 entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
639 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
640 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
641 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
642}
643
Ingo Molnare309b412008-05-12 21:20:51 +0200644void
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200645trace_function(struct trace_array *tr, struct trace_array_cpu *data,
646 unsigned long ip, unsigned long parent_ip, unsigned long flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200647{
648 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200649 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200650
Ingo Molnardcb63082008-05-12 21:20:48 +0200651 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200652 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200653 tracing_generic_entry_update(entry, flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200654 entry->type = TRACE_FN;
655 entry->fn.ip = ip;
656 entry->fn.parent_ip = parent_ip;
Ingo Molnardcb63082008-05-12 21:20:48 +0200657 spin_unlock_irqrestore(&data->lock, irq_flags);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200658
659 if (!(trace_flags & TRACE_ITER_BLOCK))
660 wake_up (&trace_wait);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200661}
662
Ingo Molnare309b412008-05-12 21:20:51 +0200663void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200664ftrace(struct trace_array *tr, struct trace_array_cpu *data,
665 unsigned long ip, unsigned long parent_ip, unsigned long flags)
666{
667 if (likely(!atomic_read(&data->disabled)))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200668 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200669}
670
Ingo Molnare309b412008-05-12 21:20:51 +0200671void
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200672trace_special(struct trace_array *tr, struct trace_array_cpu *data,
673 unsigned long arg1, unsigned long arg2, unsigned long arg3)
674{
675 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200676 unsigned long irq_flags;
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200677
Ingo Molnardcb63082008-05-12 21:20:48 +0200678 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200679 entry = tracing_get_trace_entry(tr, data);
680 tracing_generic_entry_update(entry, 0);
681 entry->type = TRACE_SPECIAL;
682 entry->special.arg1 = arg1;
683 entry->special.arg2 = arg2;
684 entry->special.arg3 = arg3;
Ingo Molnardcb63082008-05-12 21:20:48 +0200685 spin_unlock_irqrestore(&data->lock, irq_flags);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200686
687 if (!(trace_flags & TRACE_ITER_BLOCK))
688 wake_up (&trace_wait);
Ingo Molnarf0a920d2008-05-12 21:20:47 +0200689}
690
Ingo Molnare309b412008-05-12 21:20:51 +0200691void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200692tracing_sched_switch_trace(struct trace_array *tr,
693 struct trace_array_cpu *data,
694 struct task_struct *prev, struct task_struct *next,
695 unsigned long flags)
696{
697 struct trace_entry *entry;
Ingo Molnardcb63082008-05-12 21:20:48 +0200698 unsigned long irq_flags;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200699
Ingo Molnardcb63082008-05-12 21:20:48 +0200700 spin_lock_irqsave(&data->lock, irq_flags);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200701 entry = tracing_get_trace_entry(tr, data);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200702 tracing_generic_entry_update(entry, flags);
703 entry->type = TRACE_CTX;
704 entry->ctx.prev_pid = prev->pid;
705 entry->ctx.prev_prio = prev->prio;
706 entry->ctx.prev_state = prev->state;
707 entry->ctx.next_pid = next->pid;
708 entry->ctx.next_prio = next->prio;
Ingo Molnardcb63082008-05-12 21:20:48 +0200709 spin_unlock_irqrestore(&data->lock, irq_flags);
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +0200710
711 if (!(trace_flags & TRACE_ITER_BLOCK))
712 wake_up (&trace_wait);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200713}
714
Ingo Molnar57422792008-05-12 21:20:51 +0200715void
716tracing_sched_wakeup_trace(struct trace_array *tr,
717 struct trace_array_cpu *data,
718 struct task_struct *wakee, struct task_struct *curr,
719 unsigned long flags)
720{
721 struct trace_entry *entry;
722 unsigned long irq_flags;
723
724 spin_lock_irqsave(&data->lock, irq_flags);
725 entry = tracing_get_trace_entry(tr, data);
726 tracing_generic_entry_update(entry, flags);
727 entry->type = TRACE_WAKE;
728 entry->ctx.prev_pid = curr->pid;
729 entry->ctx.prev_prio = curr->prio;
730 entry->ctx.prev_state = curr->state;
731 entry->ctx.next_pid = wakee->pid;
732 entry->ctx.next_prio = wakee->prio;
733 spin_unlock_irqrestore(&data->lock, irq_flags);
734
735 if (!(trace_flags & TRACE_ITER_BLOCK))
736 wake_up(&trace_wait);
737}
738
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200739#ifdef CONFIG_FTRACE
Ingo Molnare309b412008-05-12 21:20:51 +0200740static void
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200741function_trace_call(unsigned long ip, unsigned long parent_ip)
742{
743 struct trace_array *tr = &global_trace;
744 struct trace_array_cpu *data;
745 unsigned long flags;
746 long disabled;
747 int cpu;
748
749 if (unlikely(!tracer_enabled))
750 return;
751
752 local_irq_save(flags);
753 cpu = raw_smp_processor_id();
754 data = tr->data[cpu];
755 disabled = atomic_inc_return(&data->disabled);
756
757 if (likely(disabled == 1))
Steven Rostedt6fb44b72008-05-12 21:20:49 +0200758 trace_function(tr, data, ip, parent_ip, flags);
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200759
760 atomic_dec(&data->disabled);
761 local_irq_restore(flags);
762}
763
764static struct ftrace_ops trace_ops __read_mostly =
765{
766 .func = function_trace_call,
767};
768
Ingo Molnare309b412008-05-12 21:20:51 +0200769void tracing_start_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200770{
771 register_ftrace_function(&trace_ops);
772}
773
Ingo Molnare309b412008-05-12 21:20:51 +0200774void tracing_stop_function_trace(void)
Ingo Molnar2e0f5762008-05-12 21:20:49 +0200775{
776 unregister_ftrace_function(&trace_ops);
777}
778#endif
779
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200780enum trace_file_type {
781 TRACE_FILE_LAT_FMT = 1,
782};
783
784static struct trace_entry *
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200785trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
786 struct trace_iterator *iter, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200787{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200788 struct page *page;
789 struct trace_entry *array;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200790
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200791 if (iter->next_idx[cpu] >= tr->entries ||
Steven Rostedtb3806b42008-05-12 21:20:46 +0200792 iter->next_idx[cpu] >= data->trace_idx ||
793 (data->trace_head == data->trace_tail &&
794 data->trace_head_idx == data->trace_tail_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200795 return NULL;
796
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200797 if (!iter->next_page[cpu]) {
Steven Rostedt93a588f2008-05-12 21:20:45 +0200798 /* Initialize the iterator for this cpu trace buffer */
799 WARN_ON(!data->trace_tail);
800 page = virt_to_page(data->trace_tail);
801 iter->next_page[cpu] = &page->lru;
802 iter->next_page_idx[cpu] = data->trace_tail_idx;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200803 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200804
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200805 page = list_entry(iter->next_page[cpu], struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200806 BUG_ON(&data->trace_pages == &page->lru);
807
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200808 array = page_address(page);
809
Steven Rostedt93a588f2008-05-12 21:20:45 +0200810 WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200811 return &array[iter->next_page_idx[cpu]];
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200812}
813
Ingo Molnare309b412008-05-12 21:20:51 +0200814static struct trace_entry *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200815find_next_entry(struct trace_iterator *iter, int *ent_cpu)
816{
817 struct trace_array *tr = iter->tr;
818 struct trace_entry *ent, *next = NULL;
819 int next_cpu = -1;
820 int cpu;
821
822 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +0200823 if (!head_page(tr->data[cpu]))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200824 continue;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200825 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
Ingo Molnarcdd31cd2008-05-12 21:20:46 +0200826 /*
827 * Pick the entry with the smallest timestamp:
828 */
829 if (ent && (!next || ent->t < next->t)) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200830 next = ent;
831 next_cpu = cpu;
832 }
833 }
834
835 if (ent_cpu)
836 *ent_cpu = next_cpu;
837
838 return next;
839}
840
Ingo Molnare309b412008-05-12 21:20:51 +0200841static void trace_iterator_increment(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200842{
843 iter->idx++;
844 iter->next_idx[iter->cpu]++;
845 iter->next_page_idx[iter->cpu]++;
Ingo Molnar8c523a92008-05-12 21:20:46 +0200846
Steven Rostedtb3806b42008-05-12 21:20:46 +0200847 if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
848 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
849
850 iter->next_page_idx[iter->cpu] = 0;
851 iter->next_page[iter->cpu] =
852 trace_next_list(data, iter->next_page[iter->cpu]);
853 }
854}
855
Ingo Molnare309b412008-05-12 21:20:51 +0200856static void trace_consume(struct trace_iterator *iter)
Steven Rostedtb3806b42008-05-12 21:20:46 +0200857{
858 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
859
860 data->trace_tail_idx++;
861 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
862 data->trace_tail = trace_next_page(data, data->trace_tail);
863 data->trace_tail_idx = 0;
864 }
865
866 /* Check if we empty it, then reset the index */
867 if (data->trace_head == data->trace_tail &&
868 data->trace_head_idx == data->trace_tail_idx)
869 data->trace_idx = 0;
Steven Rostedtb3806b42008-05-12 21:20:46 +0200870}
871
Ingo Molnare309b412008-05-12 21:20:51 +0200872static void *find_next_entry_inc(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200873{
874 struct trace_entry *next;
875 int next_cpu = -1;
876
877 next = find_next_entry(iter, &next_cpu);
878
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200879 iter->prev_ent = iter->ent;
880 iter->prev_cpu = iter->cpu;
881
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200882 iter->ent = next;
883 iter->cpu = next_cpu;
884
Steven Rostedtb3806b42008-05-12 21:20:46 +0200885 if (next)
886 trace_iterator_increment(iter);
887
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200888 return next ? iter : NULL;
889}
890
Ingo Molnare309b412008-05-12 21:20:51 +0200891static void *s_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200892{
893 struct trace_iterator *iter = m->private;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200894 void *last_ent = iter->ent;
895 int i = (int)*pos;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200896 void *ent;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200897
898 (*pos)++;
899
900 /* can't go backwards */
901 if (iter->idx > i)
902 return NULL;
903
904 if (iter->idx < 0)
905 ent = find_next_entry_inc(iter);
906 else
907 ent = iter;
908
909 while (ent && iter->idx < i)
910 ent = find_next_entry_inc(iter);
911
912 iter->pos = *pos;
913
914 if (last_ent && !ent)
915 seq_puts(m, "\n\nvim:ft=help\n");
916
917 return ent;
918}
919
920static void *s_start(struct seq_file *m, loff_t *pos)
921{
922 struct trace_iterator *iter = m->private;
923 void *p = NULL;
924 loff_t l = 0;
925 int i;
926
927 mutex_lock(&trace_types_lock);
928
929 if (!current_trace || current_trace != iter->trace)
930 return NULL;
931
932 atomic_inc(&trace_record_cmdline_disabled);
933
934 /* let the tracer grab locks here if needed */
935 if (current_trace->start)
936 current_trace->start(iter);
937
938 if (*pos != iter->pos) {
939 iter->ent = NULL;
940 iter->cpu = 0;
941 iter->idx = -1;
Ingo Molnar4e3c3332008-05-12 21:20:45 +0200942 iter->prev_ent = NULL;
943 iter->prev_cpu = -1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200944
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200945 for_each_possible_cpu(i) {
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200946 iter->next_idx[i] = 0;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200947 iter->next_page[i] = NULL;
948 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200949
950 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
951 ;
952
953 } else {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +0200954 l = *pos - 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200955 p = s_next(m, p, &l);
956 }
957
958 return p;
959}
960
961static void s_stop(struct seq_file *m, void *p)
962{
963 struct trace_iterator *iter = m->private;
964
965 atomic_dec(&trace_record_cmdline_disabled);
966
967 /* let the tracer release locks here if needed */
968 if (current_trace && current_trace == iter->trace && iter->trace->stop)
969 iter->trace->stop(iter);
970
971 mutex_unlock(&trace_types_lock);
972}
973
Steven Rostedtb3806b42008-05-12 21:20:46 +0200974static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200975seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200976{
977#ifdef CONFIG_KALLSYMS
978 char str[KSYM_SYMBOL_LEN];
979
980 kallsyms_lookup(address, NULL, NULL, NULL, str);
981
Steven Rostedtb3806b42008-05-12 21:20:46 +0200982 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200983#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200984 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200985}
986
Steven Rostedtb3806b42008-05-12 21:20:46 +0200987static int
Steven Rostedt214023c2008-05-12 21:20:46 +0200988seq_print_sym_offset(struct trace_seq *s, const char *fmt,
989 unsigned long address)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200990{
991#ifdef CONFIG_KALLSYMS
992 char str[KSYM_SYMBOL_LEN];
993
994 sprint_symbol(str, address);
Steven Rostedtb3806b42008-05-12 21:20:46 +0200995 return trace_seq_printf(s, fmt, str);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200996#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +0200997 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +0200998}
999
1000#ifndef CONFIG_64BIT
1001# define IP_FMT "%08lx"
1002#else
1003# define IP_FMT "%016lx"
1004#endif
1005
Ingo Molnare309b412008-05-12 21:20:51 +02001006static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001007seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001008{
Steven Rostedtb3806b42008-05-12 21:20:46 +02001009 int ret;
1010
1011 if (!ip)
1012 return trace_seq_printf(s, "0");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001013
1014 if (sym_flags & TRACE_ITER_SYM_OFFSET)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001015 ret = seq_print_sym_offset(s, "%s", ip);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001016 else
Steven Rostedtb3806b42008-05-12 21:20:46 +02001017 ret = seq_print_sym_short(s, "%s", ip);
1018
1019 if (!ret)
1020 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001021
1022 if (sym_flags & TRACE_ITER_SYM_ADDR)
Steven Rostedtb3806b42008-05-12 21:20:46 +02001023 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1024 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001025}
1026
Ingo Molnare309b412008-05-12 21:20:51 +02001027static void print_lat_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001028{
1029 seq_puts(m, "# _------=> CPU# \n");
1030 seq_puts(m, "# / _-----=> irqs-off \n");
1031 seq_puts(m, "# | / _----=> need-resched \n");
1032 seq_puts(m, "# || / _---=> hardirq/softirq \n");
1033 seq_puts(m, "# ||| / _--=> preempt-depth \n");
1034 seq_puts(m, "# |||| / \n");
1035 seq_puts(m, "# ||||| delay \n");
1036 seq_puts(m, "# cmd pid ||||| time | caller \n");
1037 seq_puts(m, "# \\ / ||||| \\ | / \n");
1038}
1039
Ingo Molnare309b412008-05-12 21:20:51 +02001040static void print_func_help_header(struct seq_file *m)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001041{
1042 seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1043 seq_puts(m, "# | | | | |\n");
1044}
1045
1046
Ingo Molnare309b412008-05-12 21:20:51 +02001047static void
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001048print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1049{
1050 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1051 struct trace_array *tr = iter->tr;
1052 struct trace_array_cpu *data = tr->data[tr->cpu];
1053 struct tracer *type = current_trace;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001054 unsigned long total = 0;
1055 unsigned long entries = 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001056 int cpu;
1057 const char *name = "preemption";
1058
1059 if (type)
1060 name = type->name;
1061
1062 for_each_possible_cpu(cpu) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02001063 if (head_page(tr->data[cpu])) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001064 total += tr->data[cpu]->trace_idx;
1065 if (tr->data[cpu]->trace_idx > tr->entries)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001066 entries += tr->entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001067 else
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001068 entries += tr->data[cpu]->trace_idx;
1069 }
1070 }
1071
1072 seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1073 name, UTS_RELEASE);
1074 seq_puts(m, "-----------------------------------"
1075 "---------------------------------\n");
1076 seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1077 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
Steven Rostedt57f50be2008-05-12 21:20:44 +02001078 nsecs_to_usecs(data->saved_latency),
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001079 entries,
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02001080 total,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001081 tr->cpu,
1082#if defined(CONFIG_PREEMPT_NONE)
1083 "server",
1084#elif defined(CONFIG_PREEMPT_VOLUNTARY)
1085 "desktop",
1086#elif defined(CONFIG_PREEMPT_DESKTOP)
1087 "preempt",
1088#else
1089 "unknown",
1090#endif
1091 /* These are reserved for later use */
1092 0, 0, 0, 0);
1093#ifdef CONFIG_SMP
1094 seq_printf(m, " #P:%d)\n", num_online_cpus());
1095#else
1096 seq_puts(m, ")\n");
1097#endif
1098 seq_puts(m, " -----------------\n");
1099 seq_printf(m, " | task: %.16s-%d "
1100 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1101 data->comm, data->pid, data->uid, data->nice,
1102 data->policy, data->rt_priority);
1103 seq_puts(m, " -----------------\n");
1104
1105 if (data->critical_start) {
1106 seq_puts(m, " => started at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001107 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1108 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001109 seq_puts(m, "\n => ended at: ");
Steven Rostedt214023c2008-05-12 21:20:46 +02001110 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1111 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001112 seq_puts(m, "\n");
1113 }
1114
1115 seq_puts(m, "\n");
1116}
1117
Ingo Molnare309b412008-05-12 21:20:51 +02001118static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001119lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001120{
1121 int hardirq, softirq;
1122 char *comm;
1123
1124 comm = trace_find_cmdline(entry->pid);
1125
Steven Rostedt214023c2008-05-12 21:20:46 +02001126 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1127 trace_seq_printf(s, "%d", cpu);
1128 trace_seq_printf(s, "%c%c",
1129 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1130 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001131
1132 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1133 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1134 if (hardirq && softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001135 trace_seq_putc(s, 'H');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001136 else {
1137 if (hardirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001138 trace_seq_putc(s, 'h');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001139 else {
1140 if (softirq)
Steven Rostedt214023c2008-05-12 21:20:46 +02001141 trace_seq_putc(s, 's');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001142 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001143 trace_seq_putc(s, '.');
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001144 }
1145 }
1146
1147 if (entry->preempt_count)
Steven Rostedt214023c2008-05-12 21:20:46 +02001148 trace_seq_printf(s, "%x", entry->preempt_count);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001149 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001150 trace_seq_puts(s, ".");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001151}
1152
1153unsigned long preempt_mark_thresh = 100;
1154
Ingo Molnare309b412008-05-12 21:20:51 +02001155static void
Steven Rostedt214023c2008-05-12 21:20:46 +02001156lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001157 unsigned long rel_usecs)
1158{
Steven Rostedt214023c2008-05-12 21:20:46 +02001159 trace_seq_printf(s, " %4lldus", abs_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001160 if (rel_usecs > preempt_mark_thresh)
Steven Rostedt214023c2008-05-12 21:20:46 +02001161 trace_seq_puts(s, "!: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001162 else if (rel_usecs > 1)
Steven Rostedt214023c2008-05-12 21:20:46 +02001163 trace_seq_puts(s, "+: ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001164 else
Steven Rostedt214023c2008-05-12 21:20:46 +02001165 trace_seq_puts(s, " : ");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001166}
1167
1168static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1169
Ingo Molnare309b412008-05-12 21:20:51 +02001170static int
Steven Rostedt214023c2008-05-12 21:20:46 +02001171print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001172{
Steven Rostedt214023c2008-05-12 21:20:46 +02001173 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001174 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1175 struct trace_entry *next_entry = find_next_entry(iter, NULL);
1176 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1177 struct trace_entry *entry = iter->ent;
1178 unsigned long abs_usecs;
1179 unsigned long rel_usecs;
1180 char *comm;
1181 int S;
1182
1183 if (!next_entry)
1184 next_entry = entry;
1185 rel_usecs = ns2usecs(next_entry->t - entry->t);
1186 abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1187
1188 if (verbose) {
1189 comm = trace_find_cmdline(entry->pid);
Steven Rostedt214023c2008-05-12 21:20:46 +02001190 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1191 " %ld.%03ldms (+%ld.%03ldms): ",
1192 comm,
1193 entry->pid, cpu, entry->flags,
1194 entry->preempt_count, trace_idx,
1195 ns2usecs(entry->t),
1196 abs_usecs/1000,
1197 abs_usecs % 1000, rel_usecs/1000,
1198 rel_usecs % 1000);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001199 } else {
Steven Rostedt214023c2008-05-12 21:20:46 +02001200 lat_print_generic(s, entry, cpu);
1201 lat_print_timestamp(s, abs_usecs, rel_usecs);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001202 }
1203 switch (entry->type) {
1204 case TRACE_FN:
Steven Rostedt214023c2008-05-12 21:20:46 +02001205 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1206 trace_seq_puts(s, " (");
1207 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1208 trace_seq_puts(s, ")\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001209 break;
1210 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001211 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001212 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1213 state_to_char[entry->ctx.prev_state] : 'X';
1214 comm = trace_find_cmdline(entry->ctx.next_pid);
Ingo Molnar57422792008-05-12 21:20:51 +02001215 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
Steven Rostedt214023c2008-05-12 21:20:46 +02001216 entry->ctx.prev_pid,
1217 entry->ctx.prev_prio,
Ingo Molnar57422792008-05-12 21:20:51 +02001218 S, entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedt214023c2008-05-12 21:20:46 +02001219 entry->ctx.next_pid,
1220 entry->ctx.next_prio,
1221 comm);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001222 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001223 case TRACE_SPECIAL:
1224 trace_seq_printf(s, " %lx %lx %lx\n",
1225 entry->special.arg1,
1226 entry->special.arg2,
1227 entry->special.arg3);
1228 break;
Steven Rostedt89b2f972008-05-12 21:20:44 +02001229 default:
Steven Rostedt214023c2008-05-12 21:20:46 +02001230 trace_seq_printf(s, "Unknown type %d\n", entry->type);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001231 }
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001232 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001233}
1234
Ingo Molnare309b412008-05-12 21:20:51 +02001235static int print_trace_fmt(struct trace_iterator *iter)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001236{
Steven Rostedt214023c2008-05-12 21:20:46 +02001237 struct trace_seq *s = &iter->seq;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001238 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001239 struct trace_entry *entry;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001240 unsigned long usec_rem;
1241 unsigned long long t;
1242 unsigned long secs;
1243 char *comm;
1244 int S;
Steven Rostedtb3806b42008-05-12 21:20:46 +02001245 int ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001246
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001247 entry = iter->ent;
1248
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001249 comm = trace_find_cmdline(iter->ent->pid);
1250
Ingo Molnarcdd31cd2008-05-12 21:20:46 +02001251 t = ns2usecs(entry->t);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001252 usec_rem = do_div(t, 1000000ULL);
1253 secs = (unsigned long)t;
1254
Steven Rostedtb3806b42008-05-12 21:20:46 +02001255 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1256 if (!ret)
1257 return 0;
1258 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1259 if (!ret)
1260 return 0;
1261 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1262 if (!ret)
1263 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001264
1265 switch (entry->type) {
1266 case TRACE_FN:
Steven Rostedtb3806b42008-05-12 21:20:46 +02001267 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1268 if (!ret)
1269 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001270 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1271 entry->fn.parent_ip) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02001272 ret = trace_seq_printf(s, " <-");
1273 if (!ret)
1274 return 0;
1275 ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1276 sym_flags);
1277 if (!ret)
1278 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001279 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001280 ret = trace_seq_printf(s, "\n");
1281 if (!ret)
1282 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001283 break;
1284 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001285 case TRACE_WAKE:
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001286 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1287 state_to_char[entry->ctx.prev_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001288 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001289 entry->ctx.prev_pid,
1290 entry->ctx.prev_prio,
1291 S,
Ingo Molnar57422792008-05-12 21:20:51 +02001292 entry->type == TRACE_CTX ? "==>" : " +",
Steven Rostedtb3806b42008-05-12 21:20:46 +02001293 entry->ctx.next_pid,
1294 entry->ctx.next_prio);
1295 if (!ret)
1296 return 0;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001297 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001298 case TRACE_SPECIAL:
1299 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1300 entry->special.arg1,
1301 entry->special.arg2,
1302 entry->special.arg3);
1303 if (!ret)
1304 return 0;
1305 break;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001306 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02001307 return 1;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001308}
1309
Ingo Molnare309b412008-05-12 21:20:51 +02001310static int print_raw_fmt(struct trace_iterator *iter)
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001311{
1312 struct trace_seq *s = &iter->seq;
1313 struct trace_entry *entry;
1314 int ret;
1315 int S;
1316
1317 entry = iter->ent;
1318
1319 ret = trace_seq_printf(s, "%d %d %llu ",
1320 entry->pid, iter->cpu, entry->t);
1321 if (!ret)
1322 return 0;
1323
1324 switch (entry->type) {
1325 case TRACE_FN:
1326 ret = trace_seq_printf(s, "%x %x\n",
1327 entry->fn.ip, entry->fn.parent_ip);
1328 if (!ret)
1329 return 0;
1330 break;
1331 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001332 case TRACE_WAKE:
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001333 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1334 state_to_char[entry->ctx.prev_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001335 if (entry->type == TRACE_WAKE)
1336 S = '+';
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001337 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1338 entry->ctx.prev_pid,
1339 entry->ctx.prev_prio,
1340 S,
1341 entry->ctx.next_pid,
1342 entry->ctx.next_prio);
1343 if (!ret)
1344 return 0;
1345 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001346 case TRACE_SPECIAL:
1347 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1348 entry->special.arg1,
1349 entry->special.arg2,
1350 entry->special.arg3);
1351 if (!ret)
1352 return 0;
1353 break;
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001354 }
1355 return 1;
1356}
1357
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001358#define SEQ_PUT_FIELD_RET(s, x) \
1359do { \
1360 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1361 return 0; \
1362} while (0)
1363
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001364#define SEQ_PUT_HEX_FIELD_RET(s, x) \
1365do { \
1366 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1367 return 0; \
1368} while (0)
1369
Ingo Molnare309b412008-05-12 21:20:51 +02001370static int print_hex_fmt(struct trace_iterator *iter)
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001371{
1372 struct trace_seq *s = &iter->seq;
1373 unsigned char newline = '\n';
1374 struct trace_entry *entry;
1375 int S;
1376
1377 entry = iter->ent;
1378
1379 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1380 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1381 SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1382
1383 switch (entry->type) {
1384 case TRACE_FN:
1385 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1386 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1387 break;
1388 case TRACE_CTX:
Ingo Molnar57422792008-05-12 21:20:51 +02001389 case TRACE_WAKE:
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001390 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1391 state_to_char[entry->ctx.prev_state] : 'X';
Ingo Molnar57422792008-05-12 21:20:51 +02001392 if (entry->type == TRACE_WAKE)
1393 S = '+';
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001394 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1395 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1396 SEQ_PUT_HEX_FIELD_RET(s, S);
1397 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1398 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1399 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1400 break;
1401 case TRACE_SPECIAL:
1402 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1403 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1404 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1405 break;
1406 }
1407 SEQ_PUT_FIELD_RET(s, newline);
1408
1409 return 1;
1410}
1411
Ingo Molnare309b412008-05-12 21:20:51 +02001412static int print_bin_fmt(struct trace_iterator *iter)
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001413{
1414 struct trace_seq *s = &iter->seq;
1415 struct trace_entry *entry;
1416
1417 entry = iter->ent;
1418
1419 SEQ_PUT_FIELD_RET(s, entry->pid);
1420 SEQ_PUT_FIELD_RET(s, entry->cpu);
1421 SEQ_PUT_FIELD_RET(s, entry->t);
1422
1423 switch (entry->type) {
1424 case TRACE_FN:
1425 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1426 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1427 break;
1428 case TRACE_CTX:
1429 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1430 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1431 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1432 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1433 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1434 break;
Ingo Molnarf0a920d2008-05-12 21:20:47 +02001435 case TRACE_SPECIAL:
1436 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1437 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1438 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1439 break;
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001440 }
1441 return 1;
1442}
1443
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001444static int trace_empty(struct trace_iterator *iter)
1445{
1446 struct trace_array_cpu *data;
1447 int cpu;
1448
1449 for_each_possible_cpu(cpu) {
1450 data = iter->tr->data[cpu];
1451
Steven Rostedtb3806b42008-05-12 21:20:46 +02001452 if (head_page(data) && data->trace_idx &&
1453 (data->trace_tail != data->trace_head ||
1454 data->trace_tail_idx != data->trace_head_idx))
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001455 return 0;
1456 }
1457 return 1;
1458}
1459
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001460static int print_trace_line(struct trace_iterator *iter)
1461{
Ingo Molnarcb0f12a2008-05-12 21:20:47 +02001462 if (trace_flags & TRACE_ITER_BIN)
1463 return print_bin_fmt(iter);
1464
Ingo Molnar5e3ca0e2008-05-12 21:20:49 +02001465 if (trace_flags & TRACE_ITER_HEX)
1466 return print_hex_fmt(iter);
1467
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001468 if (trace_flags & TRACE_ITER_RAW)
1469 return print_raw_fmt(iter);
1470
1471 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1472 return print_lat_fmt(iter, iter->idx, iter->cpu);
1473
1474 return print_trace_fmt(iter);
1475}
1476
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001477static int s_show(struct seq_file *m, void *v)
1478{
1479 struct trace_iterator *iter = v;
1480
1481 if (iter->ent == NULL) {
1482 if (iter->tr) {
1483 seq_printf(m, "# tracer: %s\n", iter->trace->name);
1484 seq_puts(m, "#\n");
1485 }
1486 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1487 /* print nothing if the buffers are empty */
1488 if (trace_empty(iter))
1489 return 0;
1490 print_trace_header(m, iter);
1491 if (!(trace_flags & TRACE_ITER_VERBOSE))
1492 print_lat_help_header(m);
1493 } else {
1494 if (!(trace_flags & TRACE_ITER_VERBOSE))
1495 print_func_help_header(m);
1496 }
1497 } else {
Ingo Molnarf9896bf2008-05-12 21:20:47 +02001498 print_trace_line(iter);
Steven Rostedt214023c2008-05-12 21:20:46 +02001499 trace_print_seq(m, &iter->seq);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001500 }
1501
1502 return 0;
1503}
1504
1505static struct seq_operations tracer_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001506 .start = s_start,
1507 .next = s_next,
1508 .stop = s_stop,
1509 .show = s_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001510};
1511
Ingo Molnare309b412008-05-12 21:20:51 +02001512static struct trace_iterator *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001513__tracing_open(struct inode *inode, struct file *file, int *ret)
1514{
1515 struct trace_iterator *iter;
1516
Steven Rostedt60a11772008-05-12 21:20:44 +02001517 if (tracing_disabled) {
1518 *ret = -ENODEV;
1519 return NULL;
1520 }
1521
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001522 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1523 if (!iter) {
1524 *ret = -ENOMEM;
1525 goto out;
1526 }
1527
1528 mutex_lock(&trace_types_lock);
1529 if (current_trace && current_trace->print_max)
1530 iter->tr = &max_tr;
1531 else
1532 iter->tr = inode->i_private;
1533 iter->trace = current_trace;
1534 iter->pos = -1;
1535
1536 /* TODO stop tracer */
1537 *ret = seq_open(file, &tracer_seq_ops);
1538 if (!*ret) {
1539 struct seq_file *m = file->private_data;
1540 m->private = iter;
1541
1542 /* stop the trace while dumping */
1543 if (iter->tr->ctrl)
1544 tracer_enabled = 0;
1545
1546 if (iter->trace && iter->trace->open)
1547 iter->trace->open(iter);
1548 } else {
1549 kfree(iter);
1550 iter = NULL;
1551 }
1552 mutex_unlock(&trace_types_lock);
1553
1554 out:
1555 return iter;
1556}
1557
1558int tracing_open_generic(struct inode *inode, struct file *filp)
1559{
Steven Rostedt60a11772008-05-12 21:20:44 +02001560 if (tracing_disabled)
1561 return -ENODEV;
1562
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001563 filp->private_data = inode->i_private;
1564 return 0;
1565}
1566
1567int tracing_release(struct inode *inode, struct file *file)
1568{
1569 struct seq_file *m = (struct seq_file *)file->private_data;
1570 struct trace_iterator *iter = m->private;
1571
1572 mutex_lock(&trace_types_lock);
1573 if (iter->trace && iter->trace->close)
1574 iter->trace->close(iter);
1575
1576 /* reenable tracing if it was previously enabled */
1577 if (iter->tr->ctrl)
1578 tracer_enabled = 1;
1579 mutex_unlock(&trace_types_lock);
1580
1581 seq_release(inode, file);
1582 kfree(iter);
1583 return 0;
1584}
1585
1586static int tracing_open(struct inode *inode, struct file *file)
1587{
1588 int ret;
1589
1590 __tracing_open(inode, file, &ret);
1591
1592 return ret;
1593}
1594
1595static int tracing_lt_open(struct inode *inode, struct file *file)
1596{
1597 struct trace_iterator *iter;
1598 int ret;
1599
1600 iter = __tracing_open(inode, file, &ret);
1601
1602 if (!ret)
1603 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1604
1605 return ret;
1606}
1607
1608
Ingo Molnare309b412008-05-12 21:20:51 +02001609static void *
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001610t_next(struct seq_file *m, void *v, loff_t *pos)
1611{
1612 struct tracer *t = m->private;
1613
1614 (*pos)++;
1615
1616 if (t)
1617 t = t->next;
1618
1619 m->private = t;
1620
1621 return t;
1622}
1623
1624static void *t_start(struct seq_file *m, loff_t *pos)
1625{
1626 struct tracer *t = m->private;
1627 loff_t l = 0;
1628
1629 mutex_lock(&trace_types_lock);
1630 for (; t && l < *pos; t = t_next(m, t, &l))
1631 ;
1632
1633 return t;
1634}
1635
1636static void t_stop(struct seq_file *m, void *p)
1637{
1638 mutex_unlock(&trace_types_lock);
1639}
1640
1641static int t_show(struct seq_file *m, void *v)
1642{
1643 struct tracer *t = v;
1644
1645 if (!t)
1646 return 0;
1647
1648 seq_printf(m, "%s", t->name);
1649 if (t->next)
1650 seq_putc(m, ' ');
1651 else
1652 seq_putc(m, '\n');
1653
1654 return 0;
1655}
1656
1657static struct seq_operations show_traces_seq_ops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001658 .start = t_start,
1659 .next = t_next,
1660 .stop = t_stop,
1661 .show = t_show,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001662};
1663
1664static int show_traces_open(struct inode *inode, struct file *file)
1665{
1666 int ret;
1667
Steven Rostedt60a11772008-05-12 21:20:44 +02001668 if (tracing_disabled)
1669 return -ENODEV;
1670
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001671 ret = seq_open(file, &show_traces_seq_ops);
1672 if (!ret) {
1673 struct seq_file *m = file->private_data;
1674 m->private = trace_types;
1675 }
1676
1677 return ret;
1678}
1679
1680static struct file_operations tracing_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001681 .open = tracing_open,
1682 .read = seq_read,
1683 .llseek = seq_lseek,
1684 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001685};
1686
1687static struct file_operations tracing_lt_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001688 .open = tracing_lt_open,
1689 .read = seq_read,
1690 .llseek = seq_lseek,
1691 .release = tracing_release,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001692};
1693
1694static struct file_operations show_traces_fops = {
1695 .open = show_traces_open,
1696 .read = seq_read,
1697 .release = seq_release,
1698};
1699
1700static ssize_t
1701tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1702 size_t cnt, loff_t *ppos)
1703{
1704 char *buf;
1705 int r = 0;
1706 int len = 0;
1707 int i;
1708
1709 /* calulate max size */
1710 for (i = 0; trace_options[i]; i++) {
1711 len += strlen(trace_options[i]);
1712 len += 3; /* "no" and space */
1713 }
1714
1715 /* +2 for \n and \0 */
1716 buf = kmalloc(len + 2, GFP_KERNEL);
1717 if (!buf)
1718 return -ENOMEM;
1719
1720 for (i = 0; trace_options[i]; i++) {
1721 if (trace_flags & (1 << i))
1722 r += sprintf(buf + r, "%s ", trace_options[i]);
1723 else
1724 r += sprintf(buf + r, "no%s ", trace_options[i]);
1725 }
1726
1727 r += sprintf(buf + r, "\n");
1728 WARN_ON(r >= len + 2);
1729
1730 r = simple_read_from_buffer(ubuf, cnt, ppos,
1731 buf, r);
1732
1733 kfree(buf);
1734
1735 return r;
1736}
1737
1738static ssize_t
1739tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1740 size_t cnt, loff_t *ppos)
1741{
1742 char buf[64];
1743 char *cmp = buf;
1744 int neg = 0;
1745 int i;
1746
1747 if (cnt > 63)
1748 cnt = 63;
1749
1750 if (copy_from_user(&buf, ubuf, cnt))
1751 return -EFAULT;
1752
1753 buf[cnt] = 0;
1754
1755 if (strncmp(buf, "no", 2) == 0) {
1756 neg = 1;
1757 cmp += 2;
1758 }
1759
1760 for (i = 0; trace_options[i]; i++) {
1761 int len = strlen(trace_options[i]);
1762
1763 if (strncmp(cmp, trace_options[i], len) == 0) {
1764 if (neg)
1765 trace_flags &= ~(1 << i);
1766 else
1767 trace_flags |= (1 << i);
1768 break;
1769 }
1770 }
1771
1772 filp->f_pos += cnt;
1773
1774 return cnt;
1775}
1776
1777static struct file_operations tracing_iter_fops = {
1778 .open = tracing_open_generic,
1779 .read = tracing_iter_ctrl_read,
1780 .write = tracing_iter_ctrl_write,
1781};
1782
Ingo Molnar7bd2f242008-05-12 21:20:45 +02001783static const char readme_msg[] =
1784 "tracing mini-HOWTO:\n\n"
1785 "# mkdir /debug\n"
1786 "# mount -t debugfs nodev /debug\n\n"
1787 "# cat /debug/tracing/available_tracers\n"
1788 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1789 "# cat /debug/tracing/current_tracer\n"
1790 "none\n"
1791 "# echo sched_switch > /debug/tracing/current_tracer\n"
1792 "# cat /debug/tracing/current_tracer\n"
1793 "sched_switch\n"
1794 "# cat /debug/tracing/iter_ctrl\n"
1795 "noprint-parent nosym-offset nosym-addr noverbose\n"
1796 "# echo print-parent > /debug/tracing/iter_ctrl\n"
1797 "# echo 1 > /debug/tracing/tracing_enabled\n"
1798 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1799 "echo 0 > /debug/tracing/tracing_enabled\n"
1800;
1801
1802static ssize_t
1803tracing_readme_read(struct file *filp, char __user *ubuf,
1804 size_t cnt, loff_t *ppos)
1805{
1806 return simple_read_from_buffer(ubuf, cnt, ppos,
1807 readme_msg, strlen(readme_msg));
1808}
1809
1810static struct file_operations tracing_readme_fops = {
1811 .open = tracing_open_generic,
1812 .read = tracing_readme_read,
1813};
1814
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001815static ssize_t
1816tracing_ctrl_read(struct file *filp, char __user *ubuf,
1817 size_t cnt, loff_t *ppos)
1818{
1819 struct trace_array *tr = filp->private_data;
1820 char buf[64];
1821 int r;
1822
1823 r = sprintf(buf, "%ld\n", tr->ctrl);
Ingo Molnar4e3c3332008-05-12 21:20:45 +02001824 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001825}
1826
1827static ssize_t
1828tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1829 size_t cnt, loff_t *ppos)
1830{
1831 struct trace_array *tr = filp->private_data;
1832 long val;
1833 char buf[64];
1834
1835 if (cnt > 63)
1836 cnt = 63;
1837
1838 if (copy_from_user(&buf, ubuf, cnt))
1839 return -EFAULT;
1840
1841 buf[cnt] = 0;
1842
1843 val = simple_strtoul(buf, NULL, 10);
1844
1845 val = !!val;
1846
1847 mutex_lock(&trace_types_lock);
1848 if (tr->ctrl ^ val) {
1849 if (val)
1850 tracer_enabled = 1;
1851 else
1852 tracer_enabled = 0;
1853
1854 tr->ctrl = val;
1855
1856 if (current_trace && current_trace->ctrl_update)
1857 current_trace->ctrl_update(tr);
1858 }
1859 mutex_unlock(&trace_types_lock);
1860
1861 filp->f_pos += cnt;
1862
1863 return cnt;
1864}
1865
1866static ssize_t
1867tracing_set_trace_read(struct file *filp, char __user *ubuf,
1868 size_t cnt, loff_t *ppos)
1869{
1870 char buf[max_tracer_type_len+2];
1871 int r;
1872
1873 mutex_lock(&trace_types_lock);
1874 if (current_trace)
1875 r = sprintf(buf, "%s\n", current_trace->name);
1876 else
1877 r = sprintf(buf, "\n");
1878 mutex_unlock(&trace_types_lock);
1879
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001880 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001881}
1882
1883static ssize_t
1884tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1885 size_t cnt, loff_t *ppos)
1886{
1887 struct trace_array *tr = &global_trace;
1888 struct tracer *t;
1889 char buf[max_tracer_type_len+1];
1890 int i;
1891
1892 if (cnt > max_tracer_type_len)
1893 cnt = max_tracer_type_len;
1894
1895 if (copy_from_user(&buf, ubuf, cnt))
1896 return -EFAULT;
1897
1898 buf[cnt] = 0;
1899
1900 /* strip ending whitespace. */
1901 for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1902 buf[i] = 0;
1903
1904 mutex_lock(&trace_types_lock);
1905 for (t = trace_types; t; t = t->next) {
1906 if (strcmp(t->name, buf) == 0)
1907 break;
1908 }
1909 if (!t || t == current_trace)
1910 goto out;
1911
1912 if (current_trace && current_trace->reset)
1913 current_trace->reset(tr);
1914
1915 current_trace = t;
1916 if (t->init)
1917 t->init(tr);
1918
1919 out:
1920 mutex_unlock(&trace_types_lock);
1921
1922 filp->f_pos += cnt;
1923
1924 return cnt;
1925}
1926
1927static ssize_t
1928tracing_max_lat_read(struct file *filp, char __user *ubuf,
1929 size_t cnt, loff_t *ppos)
1930{
1931 unsigned long *ptr = filp->private_data;
1932 char buf[64];
1933 int r;
1934
1935 r = snprintf(buf, 64, "%ld\n",
1936 *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
1937 if (r > 64)
1938 r = 64;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02001939 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02001940}
1941
1942static ssize_t
1943tracing_max_lat_write(struct file *filp, const char __user *ubuf,
1944 size_t cnt, loff_t *ppos)
1945{
1946 long *ptr = filp->private_data;
1947 long val;
1948 char buf[64];
1949
1950 if (cnt > 63)
1951 cnt = 63;
1952
1953 if (copy_from_user(&buf, ubuf, cnt))
1954 return -EFAULT;
1955
1956 buf[cnt] = 0;
1957
1958 val = simple_strtoul(buf, NULL, 10);
1959
1960 *ptr = val * 1000;
1961
1962 return cnt;
1963}
1964
Steven Rostedtb3806b42008-05-12 21:20:46 +02001965static atomic_t tracing_reader;
1966
1967static int tracing_open_pipe(struct inode *inode, struct file *filp)
1968{
1969 struct trace_iterator *iter;
1970
1971 if (tracing_disabled)
1972 return -ENODEV;
1973
1974 /* We only allow for reader of the pipe */
1975 if (atomic_inc_return(&tracing_reader) != 1) {
1976 atomic_dec(&tracing_reader);
1977 return -EBUSY;
1978 }
1979
1980 /* create a buffer to store the information to pass to userspace */
1981 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1982 if (!iter)
1983 return -ENOMEM;
1984
1985 iter->tr = &global_trace;
1986
1987 filp->private_data = iter;
1988
1989 return 0;
1990}
1991
1992static int tracing_release_pipe(struct inode *inode, struct file *file)
1993{
1994 struct trace_iterator *iter = file->private_data;
1995
1996 kfree(iter);
1997 atomic_dec(&tracing_reader);
1998
1999 return 0;
2000}
2001
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002002static unsigned int
2003tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2004{
2005 struct trace_iterator *iter = filp->private_data;
2006
2007 if (trace_flags & TRACE_ITER_BLOCK) {
2008 /*
2009 * Always select as readable when in blocking mode
2010 */
2011 return POLLIN | POLLRDNORM;
2012 }
2013 else {
2014 if (!trace_empty(iter))
2015 return POLLIN | POLLRDNORM;
2016 poll_wait(filp, &trace_wait, poll_table);
2017 if (!trace_empty(iter))
2018 return POLLIN | POLLRDNORM;
2019
2020 return 0;
2021 }
2022}
2023
Steven Rostedtb3806b42008-05-12 21:20:46 +02002024/*
2025 * Consumer reader.
2026 */
2027static ssize_t
2028tracing_read_pipe(struct file *filp, char __user *ubuf,
2029 size_t cnt, loff_t *ppos)
2030{
2031 struct trace_iterator *iter = filp->private_data;
2032 struct trace_array_cpu *data;
2033 static cpumask_t mask;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002034 static int start;
2035 unsigned long flags;
Ingo Molnar25770462008-05-12 21:20:49 +02002036#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002037 int ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002038#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002039 int read = 0;
2040 int cpu;
2041 int len;
2042 int ret;
2043
2044 /* return any leftover data */
2045 if (iter->seq.len > start) {
2046 len = iter->seq.len - start;
2047 if (cnt > len)
2048 cnt = len;
2049 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2050 if (ret)
2051 cnt = -EFAULT;
2052
2053 start += len;
2054
2055 return cnt;
2056 }
2057
2058 trace_seq_reset(&iter->seq);
2059 start = 0;
2060
2061 while (trace_empty(iter)) {
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002062 if (!(trace_flags & TRACE_ITER_BLOCK))
2063 return -EWOULDBLOCK;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002064 /*
2065 * This is a make-shift waitqueue. The reason we don't use
2066 * an actual wait queue is because:
2067 * 1) we only ever have one waiter
2068 * 2) the tracing, traces all functions, we don't want
2069 * the overhead of calling wake_up and friends
2070 * (and tracing them too)
2071 * Anyway, this is really very primitive wakeup.
2072 */
2073 set_current_state(TASK_INTERRUPTIBLE);
2074 iter->tr->waiter = current;
2075
2076 /* sleep for one second, and try again. */
2077 schedule_timeout(HZ);
2078
2079 iter->tr->waiter = NULL;
2080
2081 if (signal_pending(current))
2082 return -EINTR;
2083
2084 /*
2085 * We block until we read something and tracing is disabled.
2086 * We still block if tracing is disabled, but we have never
2087 * read anything. This allows a user to cat this file, and
2088 * then enable tracing. But after we have read something,
2089 * we give an EOF when tracing is again disabled.
2090 *
2091 * iter->pos will be 0 if we haven't read anything.
2092 */
2093 if (!tracer_enabled && iter->pos)
2094 break;
2095
2096 continue;
2097 }
2098
2099 /* stop when tracing is finished */
2100 if (trace_empty(iter))
2101 return 0;
2102
2103 if (cnt >= PAGE_SIZE)
2104 cnt = PAGE_SIZE - 1;
2105
2106 memset(iter, 0, sizeof(*iter));
2107 iter->tr = &global_trace;
2108 iter->pos = -1;
2109
2110 /*
2111 * We need to stop all tracing on all CPUS to read the
2112 * the next buffer. This is a bit expensive, but is
2113 * not done often. We fill all what we can read,
2114 * and then release the locks again.
2115 */
2116
2117 cpus_clear(mask);
2118 local_irq_save(flags);
Ingo Molnar25770462008-05-12 21:20:49 +02002119#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002120 ftrace_save = ftrace_enabled;
2121 ftrace_enabled = 0;
Ingo Molnar25770462008-05-12 21:20:49 +02002122#endif
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002123 smp_wmb();
Steven Rostedtb3806b42008-05-12 21:20:46 +02002124 for_each_possible_cpu(cpu) {
2125 data = iter->tr->data[cpu];
2126
2127 if (!head_page(data) || !data->trace_idx)
2128 continue;
2129
2130 atomic_inc(&data->disabled);
Steven Rostedtb3806b42008-05-12 21:20:46 +02002131 cpu_set(cpu, mask);
2132 }
2133
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002134 for_each_cpu_mask(cpu, mask) {
2135 data = iter->tr->data[cpu];
2136 spin_lock(&data->lock);
2137 }
2138
Steven Rostedt088b1e422008-05-12 21:20:48 +02002139 while (find_next_entry_inc(iter) != NULL) {
2140 int len = iter->seq.len;
2141
Ingo Molnarf9896bf2008-05-12 21:20:47 +02002142 ret = print_trace_line(iter);
Steven Rostedt088b1e422008-05-12 21:20:48 +02002143 if (!ret) {
2144 /* don't print partial lines */
2145 iter->seq.len = len;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002146 break;
Steven Rostedt088b1e422008-05-12 21:20:48 +02002147 }
Steven Rostedtb3806b42008-05-12 21:20:46 +02002148
2149 trace_consume(iter);
2150
2151 if (iter->seq.len >= cnt)
2152 break;
Steven Rostedtb3806b42008-05-12 21:20:46 +02002153 }
2154
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002155 for_each_cpu_mask(cpu, mask) {
Steven Rostedtb3806b42008-05-12 21:20:46 +02002156 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002157 spin_unlock(&data->lock);
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002158 }
2159
2160 for_each_cpu_mask(cpu, mask) {
2161 data = iter->tr->data[cpu];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002162 atomic_dec(&data->disabled);
2163 }
Ingo Molnar25770462008-05-12 21:20:49 +02002164#ifdef CONFIG_FTRACE
Ingo Molnar2e0f5762008-05-12 21:20:49 +02002165 ftrace_enabled = ftrace_save;
Ingo Molnar25770462008-05-12 21:20:49 +02002166#endif
Steven Rostedtb3806b42008-05-12 21:20:46 +02002167 local_irq_restore(flags);
2168
2169 /* Now copy what we have to the user */
2170 read = iter->seq.len;
2171 if (read > cnt)
2172 read = cnt;
2173
2174 ret = copy_to_user(ubuf, iter->seq.buffer, read);
2175
2176 if (read < iter->seq.len)
2177 start = read;
2178 else
2179 trace_seq_reset(&iter->seq);
2180
2181 if (ret)
2182 read = -EFAULT;
2183
2184 return read;
2185}
2186
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002187static struct file_operations tracing_max_lat_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002188 .open = tracing_open_generic,
2189 .read = tracing_max_lat_read,
2190 .write = tracing_max_lat_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002191};
2192
2193static struct file_operations tracing_ctrl_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002194 .open = tracing_open_generic,
2195 .read = tracing_ctrl_read,
2196 .write = tracing_ctrl_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002197};
2198
2199static struct file_operations set_tracer_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002200 .open = tracing_open_generic,
2201 .read = tracing_set_trace_read,
2202 .write = tracing_set_trace_write,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002203};
2204
Steven Rostedtb3806b42008-05-12 21:20:46 +02002205static struct file_operations tracing_pipe_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002206 .open = tracing_open_pipe,
Soeren Sandmann Pedersen2a2cc8f2008-05-12 21:20:49 +02002207 .poll = tracing_poll_pipe,
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002208 .read = tracing_read_pipe,
2209 .release = tracing_release_pipe,
Steven Rostedtb3806b42008-05-12 21:20:46 +02002210};
2211
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002212#ifdef CONFIG_DYNAMIC_FTRACE
2213
2214static ssize_t
2215tracing_read_long(struct file *filp, char __user *ubuf,
2216 size_t cnt, loff_t *ppos)
2217{
2218 unsigned long *p = filp->private_data;
2219 char buf[64];
2220 int r;
2221
2222 r = sprintf(buf, "%ld\n", *p);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002223
2224 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002225}
2226
2227static struct file_operations tracing_read_long_fops = {
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002228 .open = tracing_open_generic,
2229 .read = tracing_read_long,
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002230};
2231#endif
2232
2233static struct dentry *d_tracer;
2234
2235struct dentry *tracing_init_dentry(void)
2236{
2237 static int once;
2238
2239 if (d_tracer)
2240 return d_tracer;
2241
2242 d_tracer = debugfs_create_dir("tracing", NULL);
2243
2244 if (!d_tracer && !once) {
2245 once = 1;
2246 pr_warning("Could not create debugfs directory 'tracing'\n");
2247 return NULL;
2248 }
2249
2250 return d_tracer;
2251}
2252
Steven Rostedt60a11772008-05-12 21:20:44 +02002253#ifdef CONFIG_FTRACE_SELFTEST
2254/* Let selftest have access to static functions in this file */
2255#include "trace_selftest.c"
2256#endif
2257
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002258static __init void tracer_init_debugfs(void)
2259{
2260 struct dentry *d_tracer;
2261 struct dentry *entry;
2262
2263 d_tracer = tracing_init_dentry();
2264
2265 entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2266 &global_trace, &tracing_ctrl_fops);
2267 if (!entry)
2268 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2269
2270 entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2271 NULL, &tracing_iter_fops);
2272 if (!entry)
2273 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2274
2275 entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2276 &global_trace, &tracing_lt_fops);
2277 if (!entry)
2278 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2279
2280 entry = debugfs_create_file("trace", 0444, d_tracer,
2281 &global_trace, &tracing_fops);
2282 if (!entry)
2283 pr_warning("Could not create debugfs 'trace' entry\n");
2284
2285 entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2286 &global_trace, &show_traces_fops);
2287 if (!entry)
2288 pr_warning("Could not create debugfs 'trace' entry\n");
2289
2290 entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2291 &global_trace, &set_tracer_fops);
2292 if (!entry)
2293 pr_warning("Could not create debugfs 'trace' entry\n");
2294
2295 entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2296 &tracing_max_latency,
2297 &tracing_max_lat_fops);
2298 if (!entry)
2299 pr_warning("Could not create debugfs "
2300 "'tracing_max_latency' entry\n");
2301
2302 entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2303 &tracing_thresh, &tracing_max_lat_fops);
2304 if (!entry)
2305 pr_warning("Could not create debugfs "
2306 "'tracing_threash' entry\n");
Ingo Molnar7bd2f242008-05-12 21:20:45 +02002307 entry = debugfs_create_file("README", 0644, d_tracer,
2308 NULL, &tracing_readme_fops);
2309 if (!entry)
2310 pr_warning("Could not create debugfs 'README' entry\n");
2311
Steven Rostedtb3806b42008-05-12 21:20:46 +02002312 entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2313 NULL, &tracing_pipe_fops);
2314 if (!entry)
2315 pr_warning("Could not create debugfs "
2316 "'tracing_threash' entry\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002317
2318#ifdef CONFIG_DYNAMIC_FTRACE
2319 entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2320 &ftrace_update_tot_cnt,
2321 &tracing_read_long_fops);
2322 if (!entry)
2323 pr_warning("Could not create debugfs "
2324 "'dyn_ftrace_total_info' entry\n");
2325#endif
2326}
2327
2328/* dummy trace to disable tracing */
2329static struct tracer no_tracer __read_mostly =
2330{
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002331 .name = "none",
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002332};
2333
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002334static int trace_alloc_page(void)
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002335{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002336 struct trace_array_cpu *data;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002337 struct page *page, *tmp;
2338 LIST_HEAD(pages);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002339 void *array;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002340 int i;
2341
2342 /* first allocate a page for each CPU */
2343 for_each_possible_cpu(i) {
2344 array = (void *)__get_free_page(GFP_KERNEL);
2345 if (array == NULL) {
2346 printk(KERN_ERR "tracer: failed to allocate page"
2347 "for trace buffer!\n");
2348 goto free_pages;
2349 }
2350
2351 page = virt_to_page(array);
2352 list_add(&page->lru, &pages);
2353
2354/* Only allocate if we are actually using the max trace */
2355#ifdef CONFIG_TRACER_MAX_TRACE
2356 array = (void *)__get_free_page(GFP_KERNEL);
2357 if (array == NULL) {
2358 printk(KERN_ERR "tracer: failed to allocate page"
2359 "for trace buffer!\n");
2360 goto free_pages;
2361 }
2362 page = virt_to_page(array);
2363 list_add(&page->lru, &pages);
2364#endif
2365 }
2366
2367 /* Now that we successfully allocate a page per CPU, add them */
2368 for_each_possible_cpu(i) {
2369 data = global_trace.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002370 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002371 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002372 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002373 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002374 list_add_tail(&page->lru, &data->trace_pages);
2375 ClearPageLRU(page);
2376
2377#ifdef CONFIG_TRACER_MAX_TRACE
2378 data = max_tr.data[i];
Steven Rostedtb3806b42008-05-12 21:20:46 +02002379 spin_lock_init(&data->lock);
Ingo Molnard4c5a2f2008-05-12 21:20:46 +02002380 lockdep_set_class(&data->lock, &data->lock_key);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002381 page = list_entry(pages.next, struct page, lru);
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002382 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002383 list_add_tail(&page->lru, &data->trace_pages);
2384 SetPageLRU(page);
2385#endif
2386 }
2387 global_trace.entries += ENTRIES_PER_PAGE;
2388
2389 return 0;
2390
2391 free_pages:
2392 list_for_each_entry_safe(page, tmp, &pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002393 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002394 __free_page(page);
2395 }
2396 return -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002397}
2398
2399__init static int tracer_alloc_buffers(void)
2400{
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002401 struct trace_array_cpu *data;
2402 void *array;
2403 struct page *page;
2404 int pages = 0;
Steven Rostedt60a11772008-05-12 21:20:44 +02002405 int ret = -ENOMEM;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002406 int i;
2407
Steven Rostedt26994ea2008-05-12 21:20:48 +02002408 global_trace.ctrl = tracer_enabled;
2409
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002410 /* Allocate the first page for all buffers */
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002411 for_each_possible_cpu(i) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002412 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002413 max_tr.data[i] = &per_cpu(max_data, i);
2414
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002415 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002416 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002417 printk(KERN_ERR "tracer: failed to allocate page"
2418 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002419 goto free_buffers;
2420 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002421
2422 /* set the array to the list */
2423 INIT_LIST_HEAD(&data->trace_pages);
2424 page = virt_to_page(array);
2425 list_add(&page->lru, &data->trace_pages);
2426 /* use the LRU flag to differentiate the two buffers */
2427 ClearPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002428
2429/* Only allocate if we are actually using the max trace */
2430#ifdef CONFIG_TRACER_MAX_TRACE
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002431 array = (void *)__get_free_page(GFP_KERNEL);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002432 if (array == NULL) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002433 printk(KERN_ERR "tracer: failed to allocate page"
2434 "for trace buffer!\n");
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002435 goto free_buffers;
2436 }
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002437
2438 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2439 page = virt_to_page(array);
2440 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2441 SetPageLRU(page);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002442#endif
2443 }
2444
2445 /*
2446 * Since we allocate by orders of pages, we may be able to
2447 * round up a bit.
2448 */
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002449 global_trace.entries = ENTRIES_PER_PAGE;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002450 pages++;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002451
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002452 while (global_trace.entries < trace_nr_entries) {
2453 if (trace_alloc_page())
2454 break;
2455 pages++;
2456 }
Steven Rostedt89b2f972008-05-12 21:20:44 +02002457 max_tr.entries = global_trace.entries;
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002458
2459 pr_info("tracer: %d pages allocated for %ld",
2460 pages, trace_nr_entries);
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002461 pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2462 pr_info(" actual entries %ld\n", global_trace.entries);
2463
2464 tracer_init_debugfs();
2465
2466 trace_init_cmdlines();
2467
2468 register_tracer(&no_tracer);
2469 current_trace = &no_tracer;
2470
Steven Rostedt60a11772008-05-12 21:20:44 +02002471 /* All seems OK, enable tracing */
2472 tracing_disabled = 0;
2473
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002474 return 0;
2475
2476 free_buffers:
2477 for (i-- ; i >= 0; i--) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002478 struct page *page, *tmp;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002479 struct trace_array_cpu *data = global_trace.data[i];
2480
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002481 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002482 list_for_each_entry_safe(page, tmp,
2483 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002484 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002485 __free_page(page);
2486 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002487 }
2488
2489#ifdef CONFIG_TRACER_MAX_TRACE
2490 data = max_tr.data[i];
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002491 if (data) {
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002492 list_for_each_entry_safe(page, tmp,
2493 &data->trace_pages, lru) {
Ingo Molnarc7aafc52008-05-12 21:20:45 +02002494 list_del_init(&page->lru);
Steven Rostedt4c11d7a2008-05-12 21:20:43 +02002495 __free_page(page);
2496 }
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002497 }
2498#endif
2499 }
Steven Rostedt60a11772008-05-12 21:20:44 +02002500 return ret;
Steven Rostedtbc0c38d2008-05-12 21:20:42 +02002501}
Steven Rostedt60a11772008-05-12 21:20:44 +02002502fs_initcall(tracer_alloc_buffers);