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