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