]> err.no Git - linux-2.6/blob - kernel/trace/trace.c
ftrace: make nostacktrace the default
[linux-2.6] / kernel / trace / trace.c
1 /*
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>
18 #include <linux/pagemap.h>
19 #include <linux/hardirq.h>
20 #include <linux/linkage.h>
21 #include <linux/uaccess.h>
22 #include <linux/ftrace.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25 #include <linux/ctype.h>
26 #include <linux/init.h>
27 #include <linux/poll.h>
28 #include <linux/gfp.h>
29 #include <linux/fs.h>
30
31 #include <linux/stacktrace.h>
32
33 #include "trace.h"
34
35 unsigned long __read_mostly     tracing_max_latency = (cycle_t)ULONG_MAX;
36 unsigned long __read_mostly     tracing_thresh;
37
38 static int tracing_disabled = 1;
39
40 static long
41 ns2usecs(cycle_t nsec)
42 {
43         nsec += 500;
44         do_div(nsec, 1000);
45         return nsec;
46 }
47
48 cycle_t ftrace_now(int cpu)
49 {
50         return cpu_clock(cpu);
51 }
52
53 static struct trace_array       global_trace;
54
55 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
56
57 static struct trace_array       max_tr;
58
59 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
60
61 static int                      tracer_enabled = 1;
62 static unsigned long            trace_nr_entries = 65536UL;
63
64 static struct tracer            *trace_types __read_mostly;
65 static struct tracer            *current_trace __read_mostly;
66 static int                      max_tracer_type_len;
67
68 static DEFINE_MUTEX(trace_types_lock);
69 static DECLARE_WAIT_QUEUE_HEAD (trace_wait);
70
71 #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(struct trace_entry))
72
73 static int __init set_nr_entries(char *str)
74 {
75         if (!str)
76                 return 0;
77         trace_nr_entries = simple_strtoul(str, &str, 0);
78         return 1;
79 }
80 __setup("trace_entries=", set_nr_entries);
81
82 unsigned long nsecs_to_usecs(unsigned long nsecs)
83 {
84         return nsecs / 1000;
85 }
86
87 enum trace_type {
88         __TRACE_FIRST_TYPE = 0,
89
90         TRACE_FN,
91         TRACE_CTX,
92         TRACE_WAKE,
93         TRACE_STACK,
94         TRACE_SPECIAL,
95
96         __TRACE_LAST_TYPE
97 };
98
99 enum trace_flag_type {
100         TRACE_FLAG_IRQS_OFF             = 0x01,
101         TRACE_FLAG_NEED_RESCHED         = 0x02,
102         TRACE_FLAG_HARDIRQ              = 0x04,
103         TRACE_FLAG_SOFTIRQ              = 0x08,
104 };
105
106 enum trace_iterator_flags {
107         TRACE_ITER_PRINT_PARENT         = 0x01,
108         TRACE_ITER_SYM_OFFSET           = 0x02,
109         TRACE_ITER_SYM_ADDR             = 0x04,
110         TRACE_ITER_VERBOSE              = 0x08,
111         TRACE_ITER_RAW                  = 0x10,
112         TRACE_ITER_HEX                  = 0x20,
113         TRACE_ITER_BIN                  = 0x40,
114         TRACE_ITER_BLOCK                = 0x80,
115         TRACE_ITER_STACKTRACE           = 0x100,
116 };
117
118 #define TRACE_ITER_SYM_MASK \
119         (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
120
121 /* These must match the bit postions above */
122 static const char *trace_options[] = {
123         "print-parent",
124         "sym-offset",
125         "sym-addr",
126         "verbose",
127         "raw",
128         "hex",
129         "bin",
130         "block",
131         "stacktrace",
132         NULL
133 };
134
135 static unsigned trace_flags = TRACE_ITER_PRINT_PARENT;
136
137 static DEFINE_SPINLOCK(ftrace_max_lock);
138
139 /*
140  * Copy the new maximum trace into the separate maximum-trace
141  * structure. (this way the maximum trace is permanently saved,
142  * for later retrieval via /debugfs/tracing/latency_trace)
143  */
144 static void
145 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
146 {
147         struct trace_array_cpu *data = tr->data[cpu];
148
149         max_tr.cpu = cpu;
150         max_tr.time_start = data->preempt_timestamp;
151
152         data = max_tr.data[cpu];
153         data->saved_latency = tracing_max_latency;
154
155         memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
156         data->pid = tsk->pid;
157         data->uid = tsk->uid;
158         data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
159         data->policy = tsk->policy;
160         data->rt_priority = tsk->rt_priority;
161
162         /* record this tasks comm */
163         tracing_record_cmdline(current);
164 }
165
166 void check_pages(struct trace_array_cpu *data)
167 {
168         struct page *page, *tmp;
169
170         BUG_ON(data->trace_pages.next->prev != &data->trace_pages);
171         BUG_ON(data->trace_pages.prev->next != &data->trace_pages);
172
173         list_for_each_entry_safe(page, tmp, &data->trace_pages, lru) {
174                 BUG_ON(page->lru.next->prev != &page->lru);
175                 BUG_ON(page->lru.prev->next != &page->lru);
176         }
177 }
178
179 void *head_page(struct trace_array_cpu *data)
180 {
181         struct page *page;
182
183         check_pages(data);
184         if (list_empty(&data->trace_pages))
185                 return NULL;
186
187         page = list_entry(data->trace_pages.next, struct page, lru);
188         BUG_ON(&page->lru == &data->trace_pages);
189
190         return page_address(page);
191 }
192
193 static int
194 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
195 {
196         int len = (PAGE_SIZE - 1) - s->len;
197         va_list ap;
198         int ret;
199
200         if (!len)
201                 return 0;
202
203         va_start(ap, fmt);
204         ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
205         va_end(ap);
206
207         /* If we can't write it all, don't bother writing anything */
208         if (ret > len)
209                 return 0;
210
211         s->len += ret;
212
213         return len;
214 }
215
216 static int
217 trace_seq_puts(struct trace_seq *s, const char *str)
218 {
219         int len = strlen(str);
220
221         if (len > ((PAGE_SIZE - 1) - s->len))
222                 return 0;
223
224         memcpy(s->buffer + s->len, str, len);
225         s->len += len;
226
227         return len;
228 }
229
230 static int
231 trace_seq_putc(struct trace_seq *s, unsigned char c)
232 {
233         if (s->len >= (PAGE_SIZE - 1))
234                 return 0;
235
236         s->buffer[s->len++] = c;
237
238         return 1;
239 }
240
241 static int
242 trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
243 {
244         if (len > ((PAGE_SIZE - 1) - s->len))
245                 return 0;
246
247         memcpy(s->buffer + s->len, mem, len);
248         s->len += len;
249
250         return len;
251 }
252
253 #define HEX_CHARS 17
254
255 static int
256 trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
257 {
258         unsigned char hex[HEX_CHARS];
259         unsigned char *data;
260         unsigned char byte;
261         int i, j;
262
263         BUG_ON(len >= HEX_CHARS);
264
265         data = mem;
266
267 #ifdef __BIG_ENDIAN
268         for (i = 0, j = 0; i < len; i++) {
269 #else
270         for (i = len-1, j = 0; i >= 0; i--) {
271 #endif
272                 byte = data[i];
273
274                 hex[j]   = byte & 0x0f;
275                 if (hex[j] >= 10)
276                         hex[j] += 'a' - 10;
277                 else
278                         hex[j] += '0';
279                 j++;
280
281                 hex[j] = byte >> 4;
282                 if (hex[j] >= 10)
283                         hex[j] += 'a' - 10;
284                 else
285                         hex[j] += '0';
286                 j++;
287         }
288         hex[j] = ' ';
289         j++;
290
291         return trace_seq_putmem(s, hex, j);
292 }
293
294 static void
295 trace_seq_reset(struct trace_seq *s)
296 {
297         s->len = 0;
298 }
299
300 static void
301 trace_print_seq(struct seq_file *m, struct trace_seq *s)
302 {
303         int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
304
305         s->buffer[len] = 0;
306         seq_puts(m, s->buffer);
307
308         trace_seq_reset(s);
309 }
310
311 static void
312 flip_trace(struct trace_array_cpu *tr1, struct trace_array_cpu *tr2)
313 {
314         struct list_head flip_pages;
315
316         INIT_LIST_HEAD(&flip_pages);
317
318         memcpy(&tr1->trace_head_idx, &tr2->trace_head_idx,
319                 sizeof(struct trace_array_cpu) -
320                 offsetof(struct trace_array_cpu, trace_head_idx));
321
322         check_pages(tr1);
323         check_pages(tr2);
324         list_splice_init(&tr1->trace_pages, &flip_pages);
325         list_splice_init(&tr2->trace_pages, &tr1->trace_pages);
326         list_splice_init(&flip_pages, &tr2->trace_pages);
327         BUG_ON(!list_empty(&flip_pages));
328         check_pages(tr1);
329         check_pages(tr2);
330 }
331
332 void
333 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
334 {
335         struct trace_array_cpu *data;
336         int i;
337
338         WARN_ON_ONCE(!irqs_disabled());
339         spin_lock(&ftrace_max_lock);
340         /* clear out all the previous traces */
341         for_each_possible_cpu(i) {
342                 data = tr->data[i];
343                 flip_trace(max_tr.data[i], data);
344                 tracing_reset(data);
345         }
346
347         __update_max_tr(tr, tsk, cpu);
348         spin_unlock(&ftrace_max_lock);
349 }
350
351 /**
352  * update_max_tr_single - only copy one trace over, and reset the rest
353  * @tr - tracer
354  * @tsk - task with the latency
355  * @cpu - the cpu of the buffer to copy.
356  */
357 void
358 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
359 {
360         struct trace_array_cpu *data = tr->data[cpu];
361         int i;
362
363         WARN_ON_ONCE(!irqs_disabled());
364         spin_lock(&ftrace_max_lock);
365         for_each_possible_cpu(i)
366                 tracing_reset(max_tr.data[i]);
367
368         flip_trace(max_tr.data[cpu], data);
369         tracing_reset(data);
370
371         __update_max_tr(tr, tsk, cpu);
372         spin_unlock(&ftrace_max_lock);
373 }
374
375 int register_tracer(struct tracer *type)
376 {
377         struct tracer *t;
378         int len;
379         int ret = 0;
380
381         if (!type->name) {
382                 pr_info("Tracer must have a name\n");
383                 return -1;
384         }
385
386         mutex_lock(&trace_types_lock);
387         for (t = trace_types; t; t = t->next) {
388                 if (strcmp(type->name, t->name) == 0) {
389                         /* already found */
390                         pr_info("Trace %s already registered\n",
391                                 type->name);
392                         ret = -1;
393                         goto out;
394                 }
395         }
396
397 #ifdef CONFIG_FTRACE_STARTUP_TEST
398         if (type->selftest) {
399                 struct tracer *saved_tracer = current_trace;
400                 struct trace_array_cpu *data;
401                 struct trace_array *tr = &global_trace;
402                 int saved_ctrl = tr->ctrl;
403                 int i;
404                 /*
405                  * Run a selftest on this tracer.
406                  * Here we reset the trace buffer, and set the current
407                  * tracer to be this tracer. The tracer can then run some
408                  * internal tracing to verify that everything is in order.
409                  * If we fail, we do not register this tracer.
410                  */
411                 for_each_possible_cpu(i) {
412                         data = tr->data[i];
413                         if (!head_page(data))
414                                 continue;
415                         tracing_reset(data);
416                 }
417                 current_trace = type;
418                 tr->ctrl = 0;
419                 /* the test is responsible for initializing and enabling */
420                 pr_info("Testing tracer %s: ", type->name);
421                 ret = type->selftest(type, tr);
422                 /* the test is responsible for resetting too */
423                 current_trace = saved_tracer;
424                 tr->ctrl = saved_ctrl;
425                 if (ret) {
426                         printk(KERN_CONT "FAILED!\n");
427                         goto out;
428                 }
429                 /* Only reset on passing, to avoid touching corrupted buffers */
430                 for_each_possible_cpu(i) {
431                         data = tr->data[i];
432                         if (!head_page(data))
433                                 continue;
434                         tracing_reset(data);
435                 }
436                 printk(KERN_CONT "PASSED\n");
437         }
438 #endif
439
440         type->next = trace_types;
441         trace_types = type;
442         len = strlen(type->name);
443         if (len > max_tracer_type_len)
444                 max_tracer_type_len = len;
445
446  out:
447         mutex_unlock(&trace_types_lock);
448
449         return ret;
450 }
451
452 void unregister_tracer(struct tracer *type)
453 {
454         struct tracer **t;
455         int len;
456
457         mutex_lock(&trace_types_lock);
458         for (t = &trace_types; *t; t = &(*t)->next) {
459                 if (*t == type)
460                         goto found;
461         }
462         pr_info("Trace %s not registered\n", type->name);
463         goto out;
464
465  found:
466         *t = (*t)->next;
467         if (strlen(type->name) != max_tracer_type_len)
468                 goto out;
469
470         max_tracer_type_len = 0;
471         for (t = &trace_types; *t; t = &(*t)->next) {
472                 len = strlen((*t)->name);
473                 if (len > max_tracer_type_len)
474                         max_tracer_type_len = len;
475         }
476  out:
477         mutex_unlock(&trace_types_lock);
478 }
479
480 void tracing_reset(struct trace_array_cpu *data)
481 {
482         data->trace_idx = 0;
483         data->trace_head = data->trace_tail = head_page(data);
484         data->trace_head_idx = 0;
485         data->trace_tail_idx = 0;
486 }
487
488 #define SAVED_CMDLINES 128
489 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
490 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
491 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
492 static int cmdline_idx;
493 static DEFINE_SPINLOCK(trace_cmdline_lock);
494 atomic_t trace_record_cmdline_disabled;
495
496 static void trace_init_cmdlines(void)
497 {
498         memset(&map_pid_to_cmdline, -1, sizeof(map_pid_to_cmdline));
499         memset(&map_cmdline_to_pid, -1, sizeof(map_cmdline_to_pid));
500         cmdline_idx = 0;
501 }
502
503 void trace_stop_cmdline_recording(void);
504
505 static void trace_save_cmdline(struct task_struct *tsk)
506 {
507         unsigned map;
508         unsigned idx;
509
510         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
511                 return;
512
513         /*
514          * It's not the end of the world if we don't get
515          * the lock, but we also don't want to spin
516          * nor do we want to disable interrupts,
517          * so if we miss here, then better luck next time.
518          */
519         if (!spin_trylock(&trace_cmdline_lock))
520                 return;
521
522         idx = map_pid_to_cmdline[tsk->pid];
523         if (idx >= SAVED_CMDLINES) {
524                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
525
526                 map = map_cmdline_to_pid[idx];
527                 if (map <= PID_MAX_DEFAULT)
528                         map_pid_to_cmdline[map] = (unsigned)-1;
529
530                 map_pid_to_cmdline[tsk->pid] = idx;
531
532                 cmdline_idx = idx;
533         }
534
535         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
536
537         spin_unlock(&trace_cmdline_lock);
538 }
539
540 static char *trace_find_cmdline(int pid)
541 {
542         char *cmdline = "<...>";
543         unsigned map;
544
545         if (!pid)
546                 return "<idle>";
547
548         if (pid > PID_MAX_DEFAULT)
549                 goto out;
550
551         map = map_pid_to_cmdline[pid];
552         if (map >= SAVED_CMDLINES)
553                 goto out;
554
555         cmdline = saved_cmdlines[map];
556
557  out:
558         return cmdline;
559 }
560
561 void tracing_record_cmdline(struct task_struct *tsk)
562 {
563         if (atomic_read(&trace_record_cmdline_disabled))
564                 return;
565
566         trace_save_cmdline(tsk);
567 }
568
569 static inline struct list_head *
570 trace_next_list(struct trace_array_cpu *data, struct list_head *next)
571 {
572         /*
573          * Roundrobin - but skip the head (which is not a real page):
574          */
575         next = next->next;
576         if (unlikely(next == &data->trace_pages))
577                 next = next->next;
578         BUG_ON(next == &data->trace_pages);
579
580         return next;
581 }
582
583 static inline void *
584 trace_next_page(struct trace_array_cpu *data, void *addr)
585 {
586         struct list_head *next;
587         struct page *page;
588
589         page = virt_to_page(addr);
590
591         next = trace_next_list(data, &page->lru);
592         page = list_entry(next, struct page, lru);
593
594         return page_address(page);
595 }
596
597 static inline struct trace_entry *
598 tracing_get_trace_entry(struct trace_array *tr, struct trace_array_cpu *data)
599 {
600         unsigned long idx, idx_next;
601         struct trace_entry *entry;
602
603         data->trace_idx++;
604         idx = data->trace_head_idx;
605         idx_next = idx + 1;
606
607         BUG_ON(idx * TRACE_ENTRY_SIZE >= PAGE_SIZE);
608
609         entry = data->trace_head + idx * TRACE_ENTRY_SIZE;
610
611         if (unlikely(idx_next >= ENTRIES_PER_PAGE)) {
612                 data->trace_head = trace_next_page(data, data->trace_head);
613                 idx_next = 0;
614         }
615
616         if (data->trace_head == data->trace_tail &&
617             idx_next == data->trace_tail_idx) {
618                 /* overrun */
619                 data->trace_tail_idx++;
620                 if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
621                         data->trace_tail =
622                                 trace_next_page(data, data->trace_tail);
623                         data->trace_tail_idx = 0;
624                 }
625         }
626
627         data->trace_head_idx = idx_next;
628
629         return entry;
630 }
631
632 static inline void
633 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags)
634 {
635         struct task_struct *tsk = current;
636         unsigned long pc;
637
638         pc = preempt_count();
639
640         entry->preempt_count    = pc & 0xff;
641         entry->pid              = tsk->pid;
642         entry->t                = ftrace_now(raw_smp_processor_id());
643         entry->flags = (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
644                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
645                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
646                 (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
647 }
648
649 void
650 trace_function(struct trace_array *tr, struct trace_array_cpu *data,
651                unsigned long ip, unsigned long parent_ip, unsigned long flags)
652 {
653         struct trace_entry *entry;
654         unsigned long irq_flags;
655
656         spin_lock_irqsave(&data->lock, irq_flags);
657         entry                   = tracing_get_trace_entry(tr, data);
658         tracing_generic_entry_update(entry, flags);
659         entry->type             = TRACE_FN;
660         entry->fn.ip            = ip;
661         entry->fn.parent_ip     = parent_ip;
662         spin_unlock_irqrestore(&data->lock, irq_flags);
663
664         if (!(trace_flags & TRACE_ITER_BLOCK))
665                 wake_up(&trace_wait);
666 }
667
668 void
669 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
670        unsigned long ip, unsigned long parent_ip, unsigned long flags)
671 {
672         if (likely(!atomic_read(&data->disabled)))
673                 trace_function(tr, data, ip, parent_ip, flags);
674 }
675
676 void
677 trace_special(struct trace_array *tr, struct trace_array_cpu *data,
678               unsigned long arg1, unsigned long arg2, unsigned long arg3)
679 {
680         struct trace_entry *entry;
681         unsigned long irq_flags;
682
683         spin_lock_irqsave(&data->lock, irq_flags);
684         entry                   = tracing_get_trace_entry(tr, data);
685         tracing_generic_entry_update(entry, 0);
686         entry->type             = TRACE_SPECIAL;
687         entry->special.arg1     = arg1;
688         entry->special.arg2     = arg2;
689         entry->special.arg3     = arg3;
690         spin_unlock_irqrestore(&data->lock, irq_flags);
691
692         if (!(trace_flags & TRACE_ITER_BLOCK))
693                 wake_up(&trace_wait);
694 }
695
696 void __trace_stack(struct trace_array *tr,
697                    struct trace_array_cpu *data,
698                    unsigned long flags,
699                    int skip)
700 {
701         struct trace_entry *entry;
702         struct stack_trace trace;
703
704         if (!(trace_flags & TRACE_ITER_STACKTRACE))
705                 return;
706
707         entry                   = tracing_get_trace_entry(tr, data);
708         tracing_generic_entry_update(entry, flags);
709         entry->type             = TRACE_STACK;
710
711         memset(&entry->stack, 0, sizeof(entry->stack));
712
713         trace.nr_entries        = 0;
714         trace.max_entries       = FTRACE_STACK_ENTRIES;
715         trace.skip              = skip;
716         trace.entries           = entry->stack.caller;
717
718         save_stack_trace(&trace);
719 }
720
721 void
722 tracing_sched_switch_trace(struct trace_array *tr,
723                            struct trace_array_cpu *data,
724                            struct task_struct *prev,
725                            struct task_struct *next,
726                            unsigned long flags)
727 {
728         struct trace_entry *entry;
729         unsigned long irq_flags;
730
731         spin_lock_irqsave(&data->lock, irq_flags);
732         entry                   = tracing_get_trace_entry(tr, data);
733         tracing_generic_entry_update(entry, flags);
734         entry->type             = TRACE_CTX;
735         entry->ctx.prev_pid     = prev->pid;
736         entry->ctx.prev_prio    = prev->prio;
737         entry->ctx.prev_state   = prev->state;
738         entry->ctx.next_pid     = next->pid;
739         entry->ctx.next_prio    = next->prio;
740         __trace_stack(tr, data, flags, 4);
741         spin_unlock_irqrestore(&data->lock, irq_flags);
742
743         if (!(trace_flags & TRACE_ITER_BLOCK))
744                 wake_up(&trace_wait);
745 }
746
747 void
748 tracing_sched_wakeup_trace(struct trace_array *tr,
749                            struct trace_array_cpu *data,
750                            struct task_struct *wakee,
751                            struct task_struct *curr,
752                            unsigned long flags)
753 {
754         struct trace_entry *entry;
755         unsigned long irq_flags;
756
757         spin_lock_irqsave(&data->lock, irq_flags);
758         entry                   = tracing_get_trace_entry(tr, data);
759         tracing_generic_entry_update(entry, flags);
760         entry->type             = TRACE_WAKE;
761         entry->ctx.prev_pid     = curr->pid;
762         entry->ctx.prev_prio    = curr->prio;
763         entry->ctx.prev_state   = curr->state;
764         entry->ctx.next_pid     = wakee->pid;
765         entry->ctx.next_prio    = wakee->prio;
766         __trace_stack(tr, data, flags, 5);
767         spin_unlock_irqrestore(&data->lock, irq_flags);
768
769         if (!(trace_flags & TRACE_ITER_BLOCK))
770                 wake_up(&trace_wait);
771 }
772
773 #ifdef CONFIG_FTRACE
774 static void
775 function_trace_call(unsigned long ip, unsigned long parent_ip)
776 {
777         struct trace_array *tr = &global_trace;
778         struct trace_array_cpu *data;
779         unsigned long flags;
780         long disabled;
781         int cpu;
782
783         if (unlikely(!tracer_enabled))
784                 return;
785
786         local_irq_save(flags);
787         cpu = raw_smp_processor_id();
788         data = tr->data[cpu];
789         disabled = atomic_inc_return(&data->disabled);
790
791         if (likely(disabled == 1))
792                 trace_function(tr, data, ip, parent_ip, flags);
793
794         atomic_dec(&data->disabled);
795         local_irq_restore(flags);
796 }
797
798 static struct ftrace_ops trace_ops __read_mostly =
799 {
800         .func = function_trace_call,
801 };
802
803 void tracing_start_function_trace(void)
804 {
805         register_ftrace_function(&trace_ops);
806 }
807
808 void tracing_stop_function_trace(void)
809 {
810         unregister_ftrace_function(&trace_ops);
811 }
812 #endif
813
814 enum trace_file_type {
815         TRACE_FILE_LAT_FMT      = 1,
816 };
817
818 static struct trace_entry *
819 trace_entry_idx(struct trace_array *tr, struct trace_array_cpu *data,
820                 struct trace_iterator *iter, int cpu)
821 {
822         struct page *page;
823         struct trace_entry *array;
824
825         if (iter->next_idx[cpu] >= tr->entries ||
826             iter->next_idx[cpu] >= data->trace_idx ||
827             (data->trace_head == data->trace_tail &&
828              data->trace_head_idx == data->trace_tail_idx))
829                 return NULL;
830
831         if (!iter->next_page[cpu]) {
832                 /* Initialize the iterator for this cpu trace buffer */
833                 WARN_ON(!data->trace_tail);
834                 page = virt_to_page(data->trace_tail);
835                 iter->next_page[cpu] = &page->lru;
836                 iter->next_page_idx[cpu] = data->trace_tail_idx;
837         }
838
839         page = list_entry(iter->next_page[cpu], struct page, lru);
840         BUG_ON(&data->trace_pages == &page->lru);
841
842         array = page_address(page);
843
844         WARN_ON(iter->next_page_idx[cpu] >= ENTRIES_PER_PAGE);
845         return &array[iter->next_page_idx[cpu]];
846 }
847
848 static struct trace_entry *
849 find_next_entry(struct trace_iterator *iter, int *ent_cpu)
850 {
851         struct trace_array *tr = iter->tr;
852         struct trace_entry *ent, *next = NULL;
853         int next_cpu = -1;
854         int cpu;
855
856         for_each_possible_cpu(cpu) {
857                 if (!head_page(tr->data[cpu]))
858                         continue;
859                 ent = trace_entry_idx(tr, tr->data[cpu], iter, cpu);
860                 /*
861                  * Pick the entry with the smallest timestamp:
862                  */
863                 if (ent && (!next || ent->t < next->t)) {
864                         next = ent;
865                         next_cpu = cpu;
866                 }
867         }
868
869         if (ent_cpu)
870                 *ent_cpu = next_cpu;
871
872         return next;
873 }
874
875 static void trace_iterator_increment(struct trace_iterator *iter)
876 {
877         iter->idx++;
878         iter->next_idx[iter->cpu]++;
879         iter->next_page_idx[iter->cpu]++;
880
881         if (iter->next_page_idx[iter->cpu] >= ENTRIES_PER_PAGE) {
882                 struct trace_array_cpu *data = iter->tr->data[iter->cpu];
883
884                 iter->next_page_idx[iter->cpu] = 0;
885                 iter->next_page[iter->cpu] =
886                         trace_next_list(data, iter->next_page[iter->cpu]);
887         }
888 }
889
890 static void trace_consume(struct trace_iterator *iter)
891 {
892         struct trace_array_cpu *data = iter->tr->data[iter->cpu];
893
894         data->trace_tail_idx++;
895         if (data->trace_tail_idx >= ENTRIES_PER_PAGE) {
896                 data->trace_tail = trace_next_page(data, data->trace_tail);
897                 data->trace_tail_idx = 0;
898         }
899
900         /* Check if we empty it, then reset the index */
901         if (data->trace_head == data->trace_tail &&
902             data->trace_head_idx == data->trace_tail_idx)
903                 data->trace_idx = 0;
904 }
905
906 static void *find_next_entry_inc(struct trace_iterator *iter)
907 {
908         struct trace_entry *next;
909         int next_cpu = -1;
910
911         next = find_next_entry(iter, &next_cpu);
912
913         iter->prev_ent = iter->ent;
914         iter->prev_cpu = iter->cpu;
915
916         iter->ent = next;
917         iter->cpu = next_cpu;
918
919         if (next)
920                 trace_iterator_increment(iter);
921
922         return next ? iter : NULL;
923 }
924
925 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
926 {
927         struct trace_iterator *iter = m->private;
928         void *last_ent = iter->ent;
929         int i = (int)*pos;
930         void *ent;
931
932         (*pos)++;
933
934         /* can't go backwards */
935         if (iter->idx > i)
936                 return NULL;
937
938         if (iter->idx < 0)
939                 ent = find_next_entry_inc(iter);
940         else
941                 ent = iter;
942
943         while (ent && iter->idx < i)
944                 ent = find_next_entry_inc(iter);
945
946         iter->pos = *pos;
947
948         if (last_ent && !ent)
949                 seq_puts(m, "\n\nvim:ft=help\n");
950
951         return ent;
952 }
953
954 static void *s_start(struct seq_file *m, loff_t *pos)
955 {
956         struct trace_iterator *iter = m->private;
957         void *p = NULL;
958         loff_t l = 0;
959         int i;
960
961         mutex_lock(&trace_types_lock);
962
963         if (!current_trace || current_trace != iter->trace)
964                 return NULL;
965
966         atomic_inc(&trace_record_cmdline_disabled);
967
968         /* let the tracer grab locks here if needed */
969         if (current_trace->start)
970                 current_trace->start(iter);
971
972         if (*pos != iter->pos) {
973                 iter->ent = NULL;
974                 iter->cpu = 0;
975                 iter->idx = -1;
976                 iter->prev_ent = NULL;
977                 iter->prev_cpu = -1;
978
979                 for_each_possible_cpu(i) {
980                         iter->next_idx[i] = 0;
981                         iter->next_page[i] = NULL;
982                 }
983
984                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
985                         ;
986
987         } else {
988                 l = *pos - 1;
989                 p = s_next(m, p, &l);
990         }
991
992         return p;
993 }
994
995 static void s_stop(struct seq_file *m, void *p)
996 {
997         struct trace_iterator *iter = m->private;
998
999         atomic_dec(&trace_record_cmdline_disabled);
1000
1001         /* let the tracer release locks here if needed */
1002         if (current_trace && current_trace == iter->trace && iter->trace->stop)
1003                 iter->trace->stop(iter);
1004
1005         mutex_unlock(&trace_types_lock);
1006 }
1007
1008 static int
1009 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
1010 {
1011 #ifdef CONFIG_KALLSYMS
1012         char str[KSYM_SYMBOL_LEN];
1013
1014         kallsyms_lookup(address, NULL, NULL, NULL, str);
1015
1016         return trace_seq_printf(s, fmt, str);
1017 #endif
1018         return 1;
1019 }
1020
1021 static int
1022 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
1023                      unsigned long address)
1024 {
1025 #ifdef CONFIG_KALLSYMS
1026         char str[KSYM_SYMBOL_LEN];
1027
1028         sprint_symbol(str, address);
1029         return trace_seq_printf(s, fmt, str);
1030 #endif
1031         return 1;
1032 }
1033
1034 #ifndef CONFIG_64BIT
1035 # define IP_FMT "%08lx"
1036 #else
1037 # define IP_FMT "%016lx"
1038 #endif
1039
1040 static int
1041 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
1042 {
1043         int ret;
1044
1045         if (!ip)
1046                 return trace_seq_printf(s, "0");
1047
1048         if (sym_flags & TRACE_ITER_SYM_OFFSET)
1049                 ret = seq_print_sym_offset(s, "%s", ip);
1050         else
1051                 ret = seq_print_sym_short(s, "%s", ip);
1052
1053         if (!ret)
1054                 return 0;
1055
1056         if (sym_flags & TRACE_ITER_SYM_ADDR)
1057                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
1058         return ret;
1059 }
1060
1061 static void print_lat_help_header(struct seq_file *m)
1062 {
1063         seq_puts(m, "#                _------=> CPU#            \n");
1064         seq_puts(m, "#               / _-----=> irqs-off        \n");
1065         seq_puts(m, "#              | / _----=> need-resched    \n");
1066         seq_puts(m, "#              || / _---=> hardirq/softirq \n");
1067         seq_puts(m, "#              ||| / _--=> preempt-depth   \n");
1068         seq_puts(m, "#              |||| /                      \n");
1069         seq_puts(m, "#              |||||     delay             \n");
1070         seq_puts(m, "#  cmd     pid ||||| time  |   caller      \n");
1071         seq_puts(m, "#     \\   /    |||||   \\   |   /           \n");
1072 }
1073
1074 static void print_func_help_header(struct seq_file *m)
1075 {
1076         seq_puts(m, "#           TASK-PID   CPU#    TIMESTAMP  FUNCTION\n");
1077         seq_puts(m, "#              | |      |          |         |\n");
1078 }
1079
1080
1081 static void
1082 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1083 {
1084         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1085         struct trace_array *tr = iter->tr;
1086         struct trace_array_cpu *data = tr->data[tr->cpu];
1087         struct tracer *type = current_trace;
1088         unsigned long total   = 0;
1089         unsigned long entries = 0;
1090         int cpu;
1091         const char *name = "preemption";
1092
1093         if (type)
1094                 name = type->name;
1095
1096         for_each_possible_cpu(cpu) {
1097                 if (head_page(tr->data[cpu])) {
1098                         total += tr->data[cpu]->trace_idx;
1099                         if (tr->data[cpu]->trace_idx > tr->entries)
1100                                 entries += tr->entries;
1101                         else
1102                                 entries += tr->data[cpu]->trace_idx;
1103                 }
1104         }
1105
1106         seq_printf(m, "%s latency trace v1.1.5 on %s\n",
1107                    name, UTS_RELEASE);
1108         seq_puts(m, "-----------------------------------"
1109                  "---------------------------------\n");
1110         seq_printf(m, " latency: %lu us, #%lu/%lu, CPU#%d |"
1111                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1112                    nsecs_to_usecs(data->saved_latency),
1113                    entries,
1114                    total,
1115                    tr->cpu,
1116 #if defined(CONFIG_PREEMPT_NONE)
1117                    "server",
1118 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1119                    "desktop",
1120 #elif defined(CONFIG_PREEMPT_DESKTOP)
1121                    "preempt",
1122 #else
1123                    "unknown",
1124 #endif
1125                    /* These are reserved for later use */
1126                    0, 0, 0, 0);
1127 #ifdef CONFIG_SMP
1128         seq_printf(m, " #P:%d)\n", num_online_cpus());
1129 #else
1130         seq_puts(m, ")\n");
1131 #endif
1132         seq_puts(m, "    -----------------\n");
1133         seq_printf(m, "    | task: %.16s-%d "
1134                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1135                    data->comm, data->pid, data->uid, data->nice,
1136                    data->policy, data->rt_priority);
1137         seq_puts(m, "    -----------------\n");
1138
1139         if (data->critical_start) {
1140                 seq_puts(m, " => started at: ");
1141                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1142                 trace_print_seq(m, &iter->seq);
1143                 seq_puts(m, "\n => ended at:   ");
1144                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1145                 trace_print_seq(m, &iter->seq);
1146                 seq_puts(m, "\n");
1147         }
1148
1149         seq_puts(m, "\n");
1150 }
1151
1152 static void
1153 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
1154 {
1155         int hardirq, softirq;
1156         char *comm;
1157
1158         comm = trace_find_cmdline(entry->pid);
1159
1160         trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
1161         trace_seq_printf(s, "%d", cpu);
1162         trace_seq_printf(s, "%c%c",
1163                         (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' : '.',
1164                         ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
1165
1166         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
1167         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
1168         if (hardirq && softirq)
1169                 trace_seq_putc(s, 'H');
1170         else {
1171                 if (hardirq)
1172                         trace_seq_putc(s, 'h');
1173                 else {
1174                         if (softirq)
1175                                 trace_seq_putc(s, 's');
1176                         else
1177                                 trace_seq_putc(s, '.');
1178                 }
1179         }
1180
1181         if (entry->preempt_count)
1182                 trace_seq_printf(s, "%x", entry->preempt_count);
1183         else
1184                 trace_seq_puts(s, ".");
1185 }
1186
1187 unsigned long preempt_mark_thresh = 100;
1188
1189 static void
1190 lat_print_timestamp(struct trace_seq *s, unsigned long long abs_usecs,
1191                     unsigned long rel_usecs)
1192 {
1193         trace_seq_printf(s, " %4lldus", abs_usecs);
1194         if (rel_usecs > preempt_mark_thresh)
1195                 trace_seq_puts(s, "!: ");
1196         else if (rel_usecs > 1)
1197                 trace_seq_puts(s, "+: ");
1198         else
1199                 trace_seq_puts(s, " : ");
1200 }
1201
1202 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
1203
1204 static int
1205 print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
1206 {
1207         struct trace_seq *s = &iter->seq;
1208         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1209         struct trace_entry *next_entry = find_next_entry(iter, NULL);
1210         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
1211         struct trace_entry *entry = iter->ent;
1212         unsigned long abs_usecs;
1213         unsigned long rel_usecs;
1214         char *comm;
1215         int S;
1216         int i;
1217
1218         if (!next_entry)
1219                 next_entry = entry;
1220         rel_usecs = ns2usecs(next_entry->t - entry->t);
1221         abs_usecs = ns2usecs(entry->t - iter->tr->time_start);
1222
1223         if (verbose) {
1224                 comm = trace_find_cmdline(entry->pid);
1225                 trace_seq_printf(s, "%16s %5d %d %d %08x %08x [%08lx]"
1226                                  " %ld.%03ldms (+%ld.%03ldms): ",
1227                                  comm,
1228                                  entry->pid, cpu, entry->flags,
1229                                  entry->preempt_count, trace_idx,
1230                                  ns2usecs(entry->t),
1231                                  abs_usecs/1000,
1232                                  abs_usecs % 1000, rel_usecs/1000,
1233                                  rel_usecs % 1000);
1234         } else {
1235                 if (entry->type != TRACE_STACK) {
1236                         lat_print_generic(s, entry, cpu);
1237                         lat_print_timestamp(s, abs_usecs, rel_usecs);
1238                 }
1239         }
1240         switch (entry->type) {
1241         case TRACE_FN:
1242                 seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1243                 trace_seq_puts(s, " (");
1244                 seq_print_ip_sym(s, entry->fn.parent_ip, sym_flags);
1245                 trace_seq_puts(s, ")\n");
1246                 break;
1247         case TRACE_CTX:
1248         case TRACE_WAKE:
1249                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1250                         state_to_char[entry->ctx.prev_state] : 'X';
1251                 comm = trace_find_cmdline(entry->ctx.next_pid);
1252                 trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d %s\n",
1253                                  entry->ctx.prev_pid,
1254                                  entry->ctx.prev_prio,
1255                                  S, entry->type == TRACE_CTX ? "==>" : "  +",
1256                                  entry->ctx.next_pid,
1257                                  entry->ctx.next_prio,
1258                                  comm);
1259                 break;
1260         case TRACE_SPECIAL:
1261                 trace_seq_printf(s, " %lx %lx %lx\n",
1262                                  entry->special.arg1,
1263                                  entry->special.arg2,
1264                                  entry->special.arg3);
1265                 break;
1266         case TRACE_STACK:
1267                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1268                         if (i)
1269                                 trace_seq_puts(s, " <= ");
1270                         seq_print_ip_sym(s, entry->stack.caller[i], sym_flags);
1271                 }
1272                 trace_seq_puts(s, "\n");
1273                 break;
1274         default:
1275                 trace_seq_printf(s, "Unknown type %d\n", entry->type);
1276         }
1277         return 1;
1278 }
1279
1280 static int print_trace_fmt(struct trace_iterator *iter)
1281 {
1282         struct trace_seq *s = &iter->seq;
1283         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1284         struct trace_entry *entry;
1285         unsigned long usec_rem;
1286         unsigned long long t;
1287         unsigned long secs;
1288         char *comm;
1289         int ret;
1290         int S;
1291         int i;
1292
1293         entry = iter->ent;
1294
1295         comm = trace_find_cmdline(iter->ent->pid);
1296
1297         t = ns2usecs(entry->t);
1298         usec_rem = do_div(t, 1000000ULL);
1299         secs = (unsigned long)t;
1300
1301         if (entry->type != TRACE_STACK) {
1302                 ret = trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid);
1303                 if (!ret)
1304                         return 0;
1305                 ret = trace_seq_printf(s, "[%02d] ", iter->cpu);
1306                 if (!ret)
1307                         return 0;
1308                 ret = trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem);
1309                 if (!ret)
1310                         return 0;
1311         }
1312
1313         switch (entry->type) {
1314         case TRACE_FN:
1315                 ret = seq_print_ip_sym(s, entry->fn.ip, sym_flags);
1316                 if (!ret)
1317                         return 0;
1318                 if ((sym_flags & TRACE_ITER_PRINT_PARENT) &&
1319                                                 entry->fn.parent_ip) {
1320                         ret = trace_seq_printf(s, " <-");
1321                         if (!ret)
1322                                 return 0;
1323                         ret = seq_print_ip_sym(s, entry->fn.parent_ip,
1324                                                sym_flags);
1325                         if (!ret)
1326                                 return 0;
1327                 }
1328                 ret = trace_seq_printf(s, "\n");
1329                 if (!ret)
1330                         return 0;
1331                 break;
1332         case TRACE_CTX:
1333         case TRACE_WAKE:
1334                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1335                         state_to_char[entry->ctx.prev_state] : 'X';
1336                 ret = trace_seq_printf(s, " %5d:%3d:%c %s %5d:%3d\n",
1337                                        entry->ctx.prev_pid,
1338                                        entry->ctx.prev_prio,
1339                                        S,
1340                                        entry->type == TRACE_CTX ? "==>" : "  +",
1341                                        entry->ctx.next_pid,
1342                                        entry->ctx.next_prio);
1343                 if (!ret)
1344                         return 0;
1345                 break;
1346         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;
1354         case TRACE_STACK:
1355                 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1356                         if (i) {
1357                                 ret = trace_seq_puts(s, " <= ");
1358                                 if (!ret)
1359                                         return 0;
1360                         }
1361                         ret = seq_print_ip_sym(s, entry->stack.caller[i],
1362                                                sym_flags);
1363                         if (!ret)
1364                                 return 0;
1365                 }
1366                 ret = trace_seq_puts(s, "\n");
1367                 if (!ret)
1368                         return 0;
1369                 break;
1370         }
1371         return 1;
1372 }
1373
1374 static int print_raw_fmt(struct trace_iterator *iter)
1375 {
1376         struct trace_seq *s = &iter->seq;
1377         struct trace_entry *entry;
1378         int ret;
1379         int S;
1380
1381         entry = iter->ent;
1382
1383         ret = trace_seq_printf(s, "%d %d %llu ",
1384                 entry->pid, iter->cpu, entry->t);
1385         if (!ret)
1386                 return 0;
1387
1388         switch (entry->type) {
1389         case TRACE_FN:
1390                 ret = trace_seq_printf(s, "%x %x\n",
1391                                         entry->fn.ip, entry->fn.parent_ip);
1392                 if (!ret)
1393                         return 0;
1394                 break;
1395         case TRACE_CTX:
1396         case TRACE_WAKE:
1397                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1398                         state_to_char[entry->ctx.prev_state] : 'X';
1399                 if (entry->type == TRACE_WAKE)
1400                         S = '+';
1401                 ret = trace_seq_printf(s, "%d %d %c %d %d\n",
1402                                        entry->ctx.prev_pid,
1403                                        entry->ctx.prev_prio,
1404                                        S,
1405                                        entry->ctx.next_pid,
1406                                        entry->ctx.next_prio);
1407                 if (!ret)
1408                         return 0;
1409                 break;
1410         case TRACE_SPECIAL:
1411         case TRACE_STACK:
1412                 ret = trace_seq_printf(s, " %lx %lx %lx\n",
1413                                  entry->special.arg1,
1414                                  entry->special.arg2,
1415                                  entry->special.arg3);
1416                 if (!ret)
1417                         return 0;
1418                 break;
1419         }
1420         return 1;
1421 }
1422
1423 #define SEQ_PUT_FIELD_RET(s, x)                         \
1424 do {                                                    \
1425         if (!trace_seq_putmem(s, &(x), sizeof(x)))      \
1426                 return 0;                               \
1427 } while (0)
1428
1429 #define SEQ_PUT_HEX_FIELD_RET(s, x)                     \
1430 do {                                                    \
1431         if (!trace_seq_putmem_hex(s, &(x), sizeof(x)))  \
1432                 return 0;                               \
1433 } while (0)
1434
1435 static int print_hex_fmt(struct trace_iterator *iter)
1436 {
1437         struct trace_seq *s = &iter->seq;
1438         unsigned char newline = '\n';
1439         struct trace_entry *entry;
1440         int S;
1441
1442         entry = iter->ent;
1443
1444         SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1445         SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1446         SEQ_PUT_HEX_FIELD_RET(s, entry->t);
1447
1448         switch (entry->type) {
1449         case TRACE_FN:
1450                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.ip);
1451                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1452                 break;
1453         case TRACE_CTX:
1454         case TRACE_WAKE:
1455                 S = entry->ctx.prev_state < sizeof(state_to_char) ?
1456                         state_to_char[entry->ctx.prev_state] : 'X';
1457                 if (entry->type == TRACE_WAKE)
1458                         S = '+';
1459                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_pid);
1460                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.prev_prio);
1461                 SEQ_PUT_HEX_FIELD_RET(s, S);
1462                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_pid);
1463                 SEQ_PUT_HEX_FIELD_RET(s, entry->ctx.next_prio);
1464                 SEQ_PUT_HEX_FIELD_RET(s, entry->fn.parent_ip);
1465                 break;
1466         case TRACE_SPECIAL:
1467         case TRACE_STACK:
1468                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg1);
1469                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg2);
1470                 SEQ_PUT_HEX_FIELD_RET(s, entry->special.arg3);
1471                 break;
1472         }
1473         SEQ_PUT_FIELD_RET(s, newline);
1474
1475         return 1;
1476 }
1477
1478 static int print_bin_fmt(struct trace_iterator *iter)
1479 {
1480         struct trace_seq *s = &iter->seq;
1481         struct trace_entry *entry;
1482
1483         entry = iter->ent;
1484
1485         SEQ_PUT_FIELD_RET(s, entry->pid);
1486         SEQ_PUT_FIELD_RET(s, entry->cpu);
1487         SEQ_PUT_FIELD_RET(s, entry->t);
1488
1489         switch (entry->type) {
1490         case TRACE_FN:
1491                 SEQ_PUT_FIELD_RET(s, entry->fn.ip);
1492                 SEQ_PUT_FIELD_RET(s, entry->fn.parent_ip);
1493                 break;
1494         case TRACE_CTX:
1495                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_pid);
1496                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_prio);
1497                 SEQ_PUT_FIELD_RET(s, entry->ctx.prev_state);
1498                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_pid);
1499                 SEQ_PUT_FIELD_RET(s, entry->ctx.next_prio);
1500                 break;
1501         case TRACE_SPECIAL:
1502         case TRACE_STACK:
1503                 SEQ_PUT_FIELD_RET(s, entry->special.arg1);
1504                 SEQ_PUT_FIELD_RET(s, entry->special.arg2);
1505                 SEQ_PUT_FIELD_RET(s, entry->special.arg3);
1506                 break;
1507         }
1508         return 1;
1509 }
1510
1511 static int trace_empty(struct trace_iterator *iter)
1512 {
1513         struct trace_array_cpu *data;
1514         int cpu;
1515
1516         for_each_possible_cpu(cpu) {
1517                 data = iter->tr->data[cpu];
1518
1519                 if (head_page(data) && data->trace_idx &&
1520                     (data->trace_tail != data->trace_head ||
1521                      data->trace_tail_idx != data->trace_head_idx))
1522                         return 0;
1523         }
1524         return 1;
1525 }
1526
1527 static int print_trace_line(struct trace_iterator *iter)
1528 {
1529         if (trace_flags & TRACE_ITER_BIN)
1530                 return print_bin_fmt(iter);
1531
1532         if (trace_flags & TRACE_ITER_HEX)
1533                 return print_hex_fmt(iter);
1534
1535         if (trace_flags & TRACE_ITER_RAW)
1536                 return print_raw_fmt(iter);
1537
1538         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
1539                 return print_lat_fmt(iter, iter->idx, iter->cpu);
1540
1541         return print_trace_fmt(iter);
1542 }
1543
1544 static int s_show(struct seq_file *m, void *v)
1545 {
1546         struct trace_iterator *iter = v;
1547
1548         if (iter->ent == NULL) {
1549                 if (iter->tr) {
1550                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
1551                         seq_puts(m, "#\n");
1552                 }
1553                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1554                         /* print nothing if the buffers are empty */
1555                         if (trace_empty(iter))
1556                                 return 0;
1557                         print_trace_header(m, iter);
1558                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1559                                 print_lat_help_header(m);
1560                 } else {
1561                         if (!(trace_flags & TRACE_ITER_VERBOSE))
1562                                 print_func_help_header(m);
1563                 }
1564         } else {
1565                 print_trace_line(iter);
1566                 trace_print_seq(m, &iter->seq);
1567         }
1568
1569         return 0;
1570 }
1571
1572 static struct seq_operations tracer_seq_ops = {
1573         .start          = s_start,
1574         .next           = s_next,
1575         .stop           = s_stop,
1576         .show           = s_show,
1577 };
1578
1579 static struct trace_iterator *
1580 __tracing_open(struct inode *inode, struct file *file, int *ret)
1581 {
1582         struct trace_iterator *iter;
1583
1584         if (tracing_disabled) {
1585                 *ret = -ENODEV;
1586                 return NULL;
1587         }
1588
1589         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1590         if (!iter) {
1591                 *ret = -ENOMEM;
1592                 goto out;
1593         }
1594
1595         mutex_lock(&trace_types_lock);
1596         if (current_trace && current_trace->print_max)
1597                 iter->tr = &max_tr;
1598         else
1599                 iter->tr = inode->i_private;
1600         iter->trace = current_trace;
1601         iter->pos = -1;
1602
1603         /* TODO stop tracer */
1604         *ret = seq_open(file, &tracer_seq_ops);
1605         if (!*ret) {
1606                 struct seq_file *m = file->private_data;
1607                 m->private = iter;
1608
1609                 /* stop the trace while dumping */
1610                 if (iter->tr->ctrl)
1611                         tracer_enabled = 0;
1612
1613                 if (iter->trace && iter->trace->open)
1614                         iter->trace->open(iter);
1615         } else {
1616                 kfree(iter);
1617                 iter = NULL;
1618         }
1619         mutex_unlock(&trace_types_lock);
1620
1621  out:
1622         return iter;
1623 }
1624
1625 int tracing_open_generic(struct inode *inode, struct file *filp)
1626 {
1627         if (tracing_disabled)
1628                 return -ENODEV;
1629
1630         filp->private_data = inode->i_private;
1631         return 0;
1632 }
1633
1634 int tracing_release(struct inode *inode, struct file *file)
1635 {
1636         struct seq_file *m = (struct seq_file *)file->private_data;
1637         struct trace_iterator *iter = m->private;
1638
1639         mutex_lock(&trace_types_lock);
1640         if (iter->trace && iter->trace->close)
1641                 iter->trace->close(iter);
1642
1643         /* reenable tracing if it was previously enabled */
1644         if (iter->tr->ctrl)
1645                 tracer_enabled = 1;
1646         mutex_unlock(&trace_types_lock);
1647
1648         seq_release(inode, file);
1649         kfree(iter);
1650         return 0;
1651 }
1652
1653 static int tracing_open(struct inode *inode, struct file *file)
1654 {
1655         int ret;
1656
1657         __tracing_open(inode, file, &ret);
1658
1659         return ret;
1660 }
1661
1662 static int tracing_lt_open(struct inode *inode, struct file *file)
1663 {
1664         struct trace_iterator *iter;
1665         int ret;
1666
1667         iter = __tracing_open(inode, file, &ret);
1668
1669         if (!ret)
1670                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
1671
1672         return ret;
1673 }
1674
1675
1676 static void *
1677 t_next(struct seq_file *m, void *v, loff_t *pos)
1678 {
1679         struct tracer *t = m->private;
1680
1681         (*pos)++;
1682
1683         if (t)
1684                 t = t->next;
1685
1686         m->private = t;
1687
1688         return t;
1689 }
1690
1691 static void *t_start(struct seq_file *m, loff_t *pos)
1692 {
1693         struct tracer *t = m->private;
1694         loff_t l = 0;
1695
1696         mutex_lock(&trace_types_lock);
1697         for (; t && l < *pos; t = t_next(m, t, &l))
1698                 ;
1699
1700         return t;
1701 }
1702
1703 static void t_stop(struct seq_file *m, void *p)
1704 {
1705         mutex_unlock(&trace_types_lock);
1706 }
1707
1708 static int t_show(struct seq_file *m, void *v)
1709 {
1710         struct tracer *t = v;
1711
1712         if (!t)
1713                 return 0;
1714
1715         seq_printf(m, "%s", t->name);
1716         if (t->next)
1717                 seq_putc(m, ' ');
1718         else
1719                 seq_putc(m, '\n');
1720
1721         return 0;
1722 }
1723
1724 static struct seq_operations show_traces_seq_ops = {
1725         .start          = t_start,
1726         .next           = t_next,
1727         .stop           = t_stop,
1728         .show           = t_show,
1729 };
1730
1731 static int show_traces_open(struct inode *inode, struct file *file)
1732 {
1733         int ret;
1734
1735         if (tracing_disabled)
1736                 return -ENODEV;
1737
1738         ret = seq_open(file, &show_traces_seq_ops);
1739         if (!ret) {
1740                 struct seq_file *m = file->private_data;
1741                 m->private = trace_types;
1742         }
1743
1744         return ret;
1745 }
1746
1747 static struct file_operations tracing_fops = {
1748         .open           = tracing_open,
1749         .read           = seq_read,
1750         .llseek         = seq_lseek,
1751         .release        = tracing_release,
1752 };
1753
1754 static struct file_operations tracing_lt_fops = {
1755         .open           = tracing_lt_open,
1756         .read           = seq_read,
1757         .llseek         = seq_lseek,
1758         .release        = tracing_release,
1759 };
1760
1761 static struct file_operations show_traces_fops = {
1762         .open = show_traces_open,
1763         .read = seq_read,
1764         .release = seq_release,
1765 };
1766
1767 static ssize_t
1768 tracing_iter_ctrl_read(struct file *filp, char __user *ubuf,
1769                        size_t cnt, loff_t *ppos)
1770 {
1771         char *buf;
1772         int r = 0;
1773         int len = 0;
1774         int i;
1775
1776         /* calulate max size */
1777         for (i = 0; trace_options[i]; i++) {
1778                 len += strlen(trace_options[i]);
1779                 len += 3; /* "no" and space */
1780         }
1781
1782         /* +2 for \n and \0 */
1783         buf = kmalloc(len + 2, GFP_KERNEL);
1784         if (!buf)
1785                 return -ENOMEM;
1786
1787         for (i = 0; trace_options[i]; i++) {
1788                 if (trace_flags & (1 << i))
1789                         r += sprintf(buf + r, "%s ", trace_options[i]);
1790                 else
1791                         r += sprintf(buf + r, "no%s ", trace_options[i]);
1792         }
1793
1794         r += sprintf(buf + r, "\n");
1795         WARN_ON(r >= len + 2);
1796
1797         r = simple_read_from_buffer(ubuf, cnt, ppos,
1798                                     buf, r);
1799
1800         kfree(buf);
1801
1802         return r;
1803 }
1804
1805 static ssize_t
1806 tracing_iter_ctrl_write(struct file *filp, const char __user *ubuf,
1807                         size_t cnt, loff_t *ppos)
1808 {
1809         char buf[64];
1810         char *cmp = buf;
1811         int neg = 0;
1812         int i;
1813
1814         if (cnt > 63)
1815                 cnt = 63;
1816
1817         if (copy_from_user(&buf, ubuf, cnt))
1818                 return -EFAULT;
1819
1820         buf[cnt] = 0;
1821
1822         if (strncmp(buf, "no", 2) == 0) {
1823                 neg = 1;
1824                 cmp += 2;
1825         }
1826
1827         for (i = 0; trace_options[i]; i++) {
1828                 int len = strlen(trace_options[i]);
1829
1830                 if (strncmp(cmp, trace_options[i], len) == 0) {
1831                         if (neg)
1832                                 trace_flags &= ~(1 << i);
1833                         else
1834                                 trace_flags |= (1 << i);
1835                         break;
1836                 }
1837         }
1838
1839         filp->f_pos += cnt;
1840
1841         return cnt;
1842 }
1843
1844 static struct file_operations tracing_iter_fops = {
1845         .open = tracing_open_generic,
1846         .read = tracing_iter_ctrl_read,
1847         .write = tracing_iter_ctrl_write,
1848 };
1849
1850 static const char readme_msg[] =
1851         "tracing mini-HOWTO:\n\n"
1852         "# mkdir /debug\n"
1853         "# mount -t debugfs nodev /debug\n\n"
1854         "# cat /debug/tracing/available_tracers\n"
1855         "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
1856         "# cat /debug/tracing/current_tracer\n"
1857         "none\n"
1858         "# echo sched_switch > /debug/tracing/current_tracer\n"
1859         "# cat /debug/tracing/current_tracer\n"
1860         "sched_switch\n"
1861         "# cat /debug/tracing/iter_ctrl\n"
1862         "noprint-parent nosym-offset nosym-addr noverbose\n"
1863         "# echo print-parent > /debug/tracing/iter_ctrl\n"
1864         "# echo 1 > /debug/tracing/tracing_enabled\n"
1865         "# cat /debug/tracing/trace > /tmp/trace.txt\n"
1866         "echo 0 > /debug/tracing/tracing_enabled\n"
1867 ;
1868
1869 static ssize_t
1870 tracing_readme_read(struct file *filp, char __user *ubuf,
1871                        size_t cnt, loff_t *ppos)
1872 {
1873         return simple_read_from_buffer(ubuf, cnt, ppos,
1874                                         readme_msg, strlen(readme_msg));
1875 }
1876
1877 static struct file_operations tracing_readme_fops = {
1878         .open = tracing_open_generic,
1879         .read = tracing_readme_read,
1880 };
1881
1882 static ssize_t
1883 tracing_ctrl_read(struct file *filp, char __user *ubuf,
1884                   size_t cnt, loff_t *ppos)
1885 {
1886         struct trace_array *tr = filp->private_data;
1887         char buf[64];
1888         int r;
1889
1890         r = sprintf(buf, "%ld\n", tr->ctrl);
1891         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1892 }
1893
1894 static ssize_t
1895 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
1896                    size_t cnt, loff_t *ppos)
1897 {
1898         struct trace_array *tr = filp->private_data;
1899         long val;
1900         char buf[64];
1901
1902         if (cnt > 63)
1903                 cnt = 63;
1904
1905         if (copy_from_user(&buf, ubuf, cnt))
1906                 return -EFAULT;
1907
1908         buf[cnt] = 0;
1909
1910         val = simple_strtoul(buf, NULL, 10);
1911
1912         val = !!val;
1913
1914         mutex_lock(&trace_types_lock);
1915         if (tr->ctrl ^ val) {
1916                 if (val)
1917                         tracer_enabled = 1;
1918                 else
1919                         tracer_enabled = 0;
1920
1921                 tr->ctrl = val;
1922
1923                 if (current_trace && current_trace->ctrl_update)
1924                         current_trace->ctrl_update(tr);
1925         }
1926         mutex_unlock(&trace_types_lock);
1927
1928         filp->f_pos += cnt;
1929
1930         return cnt;
1931 }
1932
1933 static ssize_t
1934 tracing_set_trace_read(struct file *filp, char __user *ubuf,
1935                        size_t cnt, loff_t *ppos)
1936 {
1937         char buf[max_tracer_type_len+2];
1938         int r;
1939
1940         mutex_lock(&trace_types_lock);
1941         if (current_trace)
1942                 r = sprintf(buf, "%s\n", current_trace->name);
1943         else
1944                 r = sprintf(buf, "\n");
1945         mutex_unlock(&trace_types_lock);
1946
1947         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1948 }
1949
1950 static ssize_t
1951 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
1952                         size_t cnt, loff_t *ppos)
1953 {
1954         struct trace_array *tr = &global_trace;
1955         struct tracer *t;
1956         char buf[max_tracer_type_len+1];
1957         int i;
1958
1959         if (cnt > max_tracer_type_len)
1960                 cnt = max_tracer_type_len;
1961
1962         if (copy_from_user(&buf, ubuf, cnt))
1963                 return -EFAULT;
1964
1965         buf[cnt] = 0;
1966
1967         /* strip ending whitespace. */
1968         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
1969                 buf[i] = 0;
1970
1971         mutex_lock(&trace_types_lock);
1972         for (t = trace_types; t; t = t->next) {
1973                 if (strcmp(t->name, buf) == 0)
1974                         break;
1975         }
1976         if (!t || t == current_trace)
1977                 goto out;
1978
1979         if (current_trace && current_trace->reset)
1980                 current_trace->reset(tr);
1981
1982         current_trace = t;
1983         if (t->init)
1984                 t->init(tr);
1985
1986  out:
1987         mutex_unlock(&trace_types_lock);
1988
1989         filp->f_pos += cnt;
1990
1991         return cnt;
1992 }
1993
1994 static ssize_t
1995 tracing_max_lat_read(struct file *filp, char __user *ubuf,
1996                      size_t cnt, loff_t *ppos)
1997 {
1998         unsigned long *ptr = filp->private_data;
1999         char buf[64];
2000         int r;
2001
2002         r = snprintf(buf, 64, "%ld\n",
2003                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2004         if (r > 64)
2005                 r = 64;
2006         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2007 }
2008
2009 static ssize_t
2010 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2011                       size_t cnt, loff_t *ppos)
2012 {
2013         long *ptr = filp->private_data;
2014         long val;
2015         char buf[64];
2016
2017         if (cnt > 63)
2018                 cnt = 63;
2019
2020         if (copy_from_user(&buf, ubuf, cnt))
2021                 return -EFAULT;
2022
2023         buf[cnt] = 0;
2024
2025         val = simple_strtoul(buf, NULL, 10);
2026
2027         *ptr = val * 1000;
2028
2029         return cnt;
2030 }
2031
2032 static atomic_t tracing_reader;
2033
2034 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2035 {
2036         struct trace_iterator *iter;
2037
2038         if (tracing_disabled)
2039                 return -ENODEV;
2040
2041         /* We only allow for reader of the pipe */
2042         if (atomic_inc_return(&tracing_reader) != 1) {
2043                 atomic_dec(&tracing_reader);
2044                 return -EBUSY;
2045         }
2046
2047         /* create a buffer to store the information to pass to userspace */
2048         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2049         if (!iter)
2050                 return -ENOMEM;
2051
2052         iter->tr = &global_trace;
2053
2054         filp->private_data = iter;
2055
2056         return 0;
2057 }
2058
2059 static int tracing_release_pipe(struct inode *inode, struct file *file)
2060 {
2061         struct trace_iterator *iter = file->private_data;
2062
2063         kfree(iter);
2064         atomic_dec(&tracing_reader);
2065
2066         return 0;
2067 }
2068
2069 static unsigned int
2070 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2071 {
2072         struct trace_iterator *iter = filp->private_data;
2073
2074         if (trace_flags & TRACE_ITER_BLOCK) {
2075                 /*
2076                  * Always select as readable when in blocking mode
2077                  */
2078                 return POLLIN | POLLRDNORM;
2079         }
2080         else {
2081                 if (!trace_empty(iter))
2082                         return POLLIN | POLLRDNORM;
2083                 poll_wait(filp, &trace_wait, poll_table);
2084                 if (!trace_empty(iter))
2085                         return POLLIN | POLLRDNORM;
2086
2087                 return 0;
2088         }
2089 }
2090
2091 /*
2092  * Consumer reader.
2093  */
2094 static ssize_t
2095 tracing_read_pipe(struct file *filp, char __user *ubuf,
2096                   size_t cnt, loff_t *ppos)
2097 {
2098         struct trace_iterator *iter = filp->private_data;
2099         struct trace_array_cpu *data;
2100         static cpumask_t mask;
2101         static int start;
2102         unsigned long flags;
2103 #ifdef CONFIG_FTRACE
2104         int ftrace_save;
2105 #endif
2106         int read = 0;
2107         int cpu;
2108         int len;
2109         int ret;
2110
2111         /* return any leftover data */
2112         if (iter->seq.len > start) {
2113                 len = iter->seq.len - start;
2114                 if (cnt > len)
2115                         cnt = len;
2116                 ret = copy_to_user(ubuf, iter->seq.buffer + start, cnt);
2117                 if (ret)
2118                         cnt = -EFAULT;
2119
2120                 start += len;
2121
2122                 return cnt;
2123         }
2124
2125         trace_seq_reset(&iter->seq);
2126         start = 0;
2127
2128         while (trace_empty(iter)) {
2129                 if (!(trace_flags & TRACE_ITER_BLOCK))
2130                         return -EWOULDBLOCK;
2131                 /*
2132                  * This is a make-shift waitqueue. The reason we don't use
2133                  * an actual wait queue is because:
2134                  *  1) we only ever have one waiter
2135                  *  2) the tracing, traces all functions, we don't want
2136                  *     the overhead of calling wake_up and friends
2137                  *     (and tracing them too)
2138                  *     Anyway, this is really very primitive wakeup.
2139                  */
2140                 set_current_state(TASK_INTERRUPTIBLE);
2141                 iter->tr->waiter = current;
2142
2143                 /* sleep for one second, and try again. */
2144                 schedule_timeout(HZ);
2145
2146                 iter->tr->waiter = NULL;
2147
2148                 if (signal_pending(current))
2149                         return -EINTR;
2150
2151                 /*
2152                  * We block until we read something and tracing is disabled.
2153                  * We still block if tracing is disabled, but we have never
2154                  * read anything. This allows a user to cat this file, and
2155                  * then enable tracing. But after we have read something,
2156                  * we give an EOF when tracing is again disabled.
2157                  *
2158                  * iter->pos will be 0 if we haven't read anything.
2159                  */
2160                 if (!tracer_enabled && iter->pos)
2161                         break;
2162
2163                 continue;
2164         }
2165
2166         /* stop when tracing is finished */
2167         if (trace_empty(iter))
2168                 return 0;
2169
2170         if (cnt >= PAGE_SIZE)
2171                 cnt = PAGE_SIZE - 1;
2172
2173         memset(iter, 0, sizeof(*iter));
2174         iter->tr = &global_trace;
2175         iter->pos = -1;
2176
2177         /*
2178          * We need to stop all tracing on all CPUS to read the
2179          * the next buffer. This is a bit expensive, but is
2180          * not done often. We fill all what we can read,
2181          * and then release the locks again.
2182          */
2183
2184         cpus_clear(mask);
2185         local_irq_save(flags);
2186 #ifdef CONFIG_FTRACE
2187         ftrace_save = ftrace_enabled;
2188         ftrace_enabled = 0;
2189 #endif
2190         smp_wmb();
2191         for_each_possible_cpu(cpu) {
2192                 data = iter->tr->data[cpu];
2193
2194                 if (!head_page(data) || !data->trace_idx)
2195                         continue;
2196
2197                 atomic_inc(&data->disabled);
2198                 cpu_set(cpu, mask);
2199         }
2200
2201         for_each_cpu_mask(cpu, mask) {
2202                 data = iter->tr->data[cpu];
2203                 spin_lock(&data->lock);
2204         }
2205
2206         while (find_next_entry_inc(iter) != NULL) {
2207                 int len = iter->seq.len;
2208
2209                 ret = print_trace_line(iter);
2210                 if (!ret) {
2211                         /* don't print partial lines */
2212                         iter->seq.len = len;
2213                         break;
2214                 }
2215
2216                 trace_consume(iter);
2217
2218                 if (iter->seq.len >= cnt)
2219                         break;
2220         }
2221
2222         for_each_cpu_mask(cpu, mask) {
2223                 data = iter->tr->data[cpu];
2224                 spin_unlock(&data->lock);
2225         }
2226
2227         for_each_cpu_mask(cpu, mask) {
2228                 data = iter->tr->data[cpu];
2229                 atomic_dec(&data->disabled);
2230         }
2231 #ifdef CONFIG_FTRACE
2232         ftrace_enabled = ftrace_save;
2233 #endif
2234         local_irq_restore(flags);
2235
2236         /* Now copy what we have to the user */
2237         read = iter->seq.len;
2238         if (read > cnt)
2239                 read = cnt;
2240
2241         ret = copy_to_user(ubuf, iter->seq.buffer, read);
2242
2243         if (read < iter->seq.len)
2244                 start = read;
2245         else
2246                 trace_seq_reset(&iter->seq);
2247
2248         if (ret)
2249                 read = -EFAULT;
2250
2251         return read;
2252 }
2253
2254 static struct file_operations tracing_max_lat_fops = {
2255         .open           = tracing_open_generic,
2256         .read           = tracing_max_lat_read,
2257         .write          = tracing_max_lat_write,
2258 };
2259
2260 static struct file_operations tracing_ctrl_fops = {
2261         .open           = tracing_open_generic,
2262         .read           = tracing_ctrl_read,
2263         .write          = tracing_ctrl_write,
2264 };
2265
2266 static struct file_operations set_tracer_fops = {
2267         .open           = tracing_open_generic,
2268         .read           = tracing_set_trace_read,
2269         .write          = tracing_set_trace_write,
2270 };
2271
2272 static struct file_operations tracing_pipe_fops = {
2273         .open           = tracing_open_pipe,
2274         .poll           = tracing_poll_pipe,
2275         .read           = tracing_read_pipe,
2276         .release        = tracing_release_pipe,
2277 };
2278
2279 #ifdef CONFIG_DYNAMIC_FTRACE
2280
2281 static ssize_t
2282 tracing_read_long(struct file *filp, char __user *ubuf,
2283                   size_t cnt, loff_t *ppos)
2284 {
2285         unsigned long *p = filp->private_data;
2286         char buf[64];
2287         int r;
2288
2289         r = sprintf(buf, "%ld\n", *p);
2290
2291         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2292 }
2293
2294 static struct file_operations tracing_read_long_fops = {
2295         .open           = tracing_open_generic,
2296         .read           = tracing_read_long,
2297 };
2298 #endif
2299
2300 static struct dentry *d_tracer;
2301
2302 struct dentry *tracing_init_dentry(void)
2303 {
2304         static int once;
2305
2306         if (d_tracer)
2307                 return d_tracer;
2308
2309         d_tracer = debugfs_create_dir("tracing", NULL);
2310
2311         if (!d_tracer && !once) {
2312                 once = 1;
2313                 pr_warning("Could not create debugfs directory 'tracing'\n");
2314                 return NULL;
2315         }
2316
2317         return d_tracer;
2318 }
2319
2320 #ifdef CONFIG_FTRACE_SELFTEST
2321 /* Let selftest have access to static functions in this file */
2322 #include "trace_selftest.c"
2323 #endif
2324
2325 static __init void tracer_init_debugfs(void)
2326 {
2327         struct dentry *d_tracer;
2328         struct dentry *entry;
2329
2330         d_tracer = tracing_init_dentry();
2331
2332         entry = debugfs_create_file("tracing_enabled", 0644, d_tracer,
2333                                     &global_trace, &tracing_ctrl_fops);
2334         if (!entry)
2335                 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2336
2337         entry = debugfs_create_file("iter_ctrl", 0644, d_tracer,
2338                                     NULL, &tracing_iter_fops);
2339         if (!entry)
2340                 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2341
2342         entry = debugfs_create_file("latency_trace", 0444, d_tracer,
2343                                     &global_trace, &tracing_lt_fops);
2344         if (!entry)
2345                 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2346
2347         entry = debugfs_create_file("trace", 0444, d_tracer,
2348                                     &global_trace, &tracing_fops);
2349         if (!entry)
2350                 pr_warning("Could not create debugfs 'trace' entry\n");
2351
2352         entry = debugfs_create_file("available_tracers", 0444, d_tracer,
2353                                     &global_trace, &show_traces_fops);
2354         if (!entry)
2355                 pr_warning("Could not create debugfs 'trace' entry\n");
2356
2357         entry = debugfs_create_file("current_tracer", 0444, d_tracer,
2358                                     &global_trace, &set_tracer_fops);
2359         if (!entry)
2360                 pr_warning("Could not create debugfs 'trace' entry\n");
2361
2362         entry = debugfs_create_file("tracing_max_latency", 0644, d_tracer,
2363                                     &tracing_max_latency,
2364                                     &tracing_max_lat_fops);
2365         if (!entry)
2366                 pr_warning("Could not create debugfs "
2367                            "'tracing_max_latency' entry\n");
2368
2369         entry = debugfs_create_file("tracing_thresh", 0644, d_tracer,
2370                                     &tracing_thresh, &tracing_max_lat_fops);
2371         if (!entry)
2372                 pr_warning("Could not create debugfs "
2373                            "'tracing_threash' entry\n");
2374         entry = debugfs_create_file("README", 0644, d_tracer,
2375                                     NULL, &tracing_readme_fops);
2376         if (!entry)
2377                 pr_warning("Could not create debugfs 'README' entry\n");
2378
2379         entry = debugfs_create_file("trace_pipe", 0644, d_tracer,
2380                                     NULL, &tracing_pipe_fops);
2381         if (!entry)
2382                 pr_warning("Could not create debugfs "
2383                            "'tracing_threash' entry\n");
2384
2385 #ifdef CONFIG_DYNAMIC_FTRACE
2386         entry = debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer,
2387                                     &ftrace_update_tot_cnt,
2388                                     &tracing_read_long_fops);
2389         if (!entry)
2390                 pr_warning("Could not create debugfs "
2391                            "'dyn_ftrace_total_info' entry\n");
2392 #endif
2393 }
2394
2395 /* dummy trace to disable tracing */
2396 static struct tracer no_tracer __read_mostly =
2397 {
2398         .name           = "none",
2399 };
2400
2401 static int trace_alloc_page(void)
2402 {
2403         struct trace_array_cpu *data;
2404         struct page *page, *tmp;
2405         LIST_HEAD(pages);
2406         void *array;
2407         int i;
2408
2409         /* first allocate a page for each CPU */
2410         for_each_possible_cpu(i) {
2411                 array = (void *)__get_free_page(GFP_KERNEL);
2412                 if (array == NULL) {
2413                         printk(KERN_ERR "tracer: failed to allocate page"
2414                                "for trace buffer!\n");
2415                         goto free_pages;
2416                 }
2417
2418                 page = virt_to_page(array);
2419                 list_add(&page->lru, &pages);
2420
2421 /* Only allocate if we are actually using the max trace */
2422 #ifdef CONFIG_TRACER_MAX_TRACE
2423                 array = (void *)__get_free_page(GFP_KERNEL);
2424                 if (array == NULL) {
2425                         printk(KERN_ERR "tracer: failed to allocate page"
2426                                "for trace buffer!\n");
2427                         goto free_pages;
2428                 }
2429                 page = virt_to_page(array);
2430                 list_add(&page->lru, &pages);
2431 #endif
2432         }
2433
2434         /* Now that we successfully allocate a page per CPU, add them */
2435         for_each_possible_cpu(i) {
2436                 data = global_trace.data[i];
2437                 spin_lock_init(&data->lock);
2438                 lockdep_set_class(&data->lock, &data->lock_key);
2439                 page = list_entry(pages.next, struct page, lru);
2440                 list_del_init(&page->lru);
2441                 list_add_tail(&page->lru, &data->trace_pages);
2442                 ClearPageLRU(page);
2443
2444 #ifdef CONFIG_TRACER_MAX_TRACE
2445                 data = max_tr.data[i];
2446                 spin_lock_init(&data->lock);
2447                 lockdep_set_class(&data->lock, &data->lock_key);
2448                 page = list_entry(pages.next, struct page, lru);
2449                 list_del_init(&page->lru);
2450                 list_add_tail(&page->lru, &data->trace_pages);
2451                 SetPageLRU(page);
2452 #endif
2453         }
2454         global_trace.entries += ENTRIES_PER_PAGE;
2455
2456         return 0;
2457
2458  free_pages:
2459         list_for_each_entry_safe(page, tmp, &pages, lru) {
2460                 list_del_init(&page->lru);
2461                 __free_page(page);
2462         }
2463         return -ENOMEM;
2464 }
2465
2466 __init static int tracer_alloc_buffers(void)
2467 {
2468         struct trace_array_cpu *data;
2469         void *array;
2470         struct page *page;
2471         int pages = 0;
2472         int ret = -ENOMEM;
2473         int i;
2474
2475         global_trace.ctrl = tracer_enabled;
2476
2477         /* Allocate the first page for all buffers */
2478         for_each_possible_cpu(i) {
2479                 data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
2480                 max_tr.data[i] = &per_cpu(max_data, i);
2481
2482                 array = (void *)__get_free_page(GFP_KERNEL);
2483                 if (array == NULL) {
2484                         printk(KERN_ERR "tracer: failed to allocate page"
2485                                "for trace buffer!\n");
2486                         goto free_buffers;
2487                 }
2488
2489                 /* set the array to the list */
2490                 INIT_LIST_HEAD(&data->trace_pages);
2491                 page = virt_to_page(array);
2492                 list_add(&page->lru, &data->trace_pages);
2493                 /* use the LRU flag to differentiate the two buffers */
2494                 ClearPageLRU(page);
2495
2496 /* Only allocate if we are actually using the max trace */
2497 #ifdef CONFIG_TRACER_MAX_TRACE
2498                 array = (void *)__get_free_page(GFP_KERNEL);
2499                 if (array == NULL) {
2500                         printk(KERN_ERR "tracer: failed to allocate page"
2501                                "for trace buffer!\n");
2502                         goto free_buffers;
2503                 }
2504
2505                 INIT_LIST_HEAD(&max_tr.data[i]->trace_pages);
2506                 page = virt_to_page(array);
2507                 list_add(&page->lru, &max_tr.data[i]->trace_pages);
2508                 SetPageLRU(page);
2509 #endif
2510         }
2511
2512         /*
2513          * Since we allocate by orders of pages, we may be able to
2514          * round up a bit.
2515          */
2516         global_trace.entries = ENTRIES_PER_PAGE;
2517         pages++;
2518
2519         while (global_trace.entries < trace_nr_entries) {
2520                 if (trace_alloc_page())
2521                         break;
2522                 pages++;
2523         }
2524         max_tr.entries = global_trace.entries;
2525
2526         pr_info("tracer: %d pages allocated for %ld",
2527                 pages, trace_nr_entries);
2528         pr_info(" entries of %ld bytes\n", (long)TRACE_ENTRY_SIZE);
2529         pr_info("   actual entries %ld\n", global_trace.entries);
2530
2531         tracer_init_debugfs();
2532
2533         trace_init_cmdlines();
2534
2535         register_tracer(&no_tracer);
2536         current_trace = &no_tracer;
2537
2538         /* All seems OK, enable tracing */
2539         tracing_disabled = 0;
2540
2541         return 0;
2542
2543  free_buffers:
2544         for (i-- ; i >= 0; i--) {
2545                 struct page *page, *tmp;
2546                 struct trace_array_cpu *data = global_trace.data[i];
2547
2548                 if (data) {
2549                         list_for_each_entry_safe(page, tmp,
2550                                                  &data->trace_pages, lru) {
2551                                 list_del_init(&page->lru);
2552                                 __free_page(page);
2553                         }
2554                 }
2555
2556 #ifdef CONFIG_TRACER_MAX_TRACE
2557                 data = max_tr.data[i];
2558                 if (data) {
2559                         list_for_each_entry_safe(page, tmp,
2560                                                  &data->trace_pages, lru) {
2561                                 list_del_init(&page->lru);
2562                                 __free_page(page);
2563                         }
2564                 }
2565 #endif
2566         }
2567         return ret;
2568 }
2569 fs_initcall(tracer_alloc_buffers);