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