Merge branch 'linus' into tracing/core
[safe/jmp/linux-2.6] / kernel / trace / trace_functions_graph.c
1 /*
2  *
3  * Function graph tracer.
4  * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
5  * Mostly borrowed from function tracer which
6  * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
7  *
8  */
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <linux/slab.h>
13 #include <linux/fs.h>
14
15 #include "trace.h"
16 #include "trace_output.h"
17
18 struct fgraph_cpu_data {
19         pid_t           last_pid;
20         int             depth;
21         int             ignore;
22         unsigned long   enter_funcs[FTRACE_RETFUNC_DEPTH];
23 };
24
25 struct fgraph_data {
26         struct fgraph_cpu_data          *cpu_data;
27
28         /* Place to preserve last processed entry. */
29         struct ftrace_graph_ent_entry   ent;
30         struct ftrace_graph_ret_entry   ret;
31         int                             failed;
32         int                             cpu;
33 };
34
35 #define TRACE_GRAPH_INDENT      2
36
37 /* Flag options */
38 #define TRACE_GRAPH_PRINT_OVERRUN       0x1
39 #define TRACE_GRAPH_PRINT_CPU           0x2
40 #define TRACE_GRAPH_PRINT_OVERHEAD      0x4
41 #define TRACE_GRAPH_PRINT_PROC          0x8
42 #define TRACE_GRAPH_PRINT_DURATION      0x10
43 #define TRACE_GRAPH_PRINT_ABS_TIME      0X20
44
45 static struct tracer_opt trace_opts[] = {
46         /* Display overruns? (for self-debug purpose) */
47         { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
48         /* Display CPU ? */
49         { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
50         /* Display Overhead ? */
51         { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
52         /* Display proc name/pid */
53         { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
54         /* Display duration of execution */
55         { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
56         /* Display absolute time of an entry */
57         { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
58         { } /* Empty entry */
59 };
60
61 static struct tracer_flags tracer_flags = {
62         /* Don't display overruns and proc by default */
63         .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
64                TRACE_GRAPH_PRINT_DURATION,
65         .opts = trace_opts
66 };
67
68 static struct trace_array *graph_array;
69
70
71 /* Add a function return address to the trace stack on thread info.*/
72 int
73 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
74                          unsigned long frame_pointer)
75 {
76         unsigned long long calltime;
77         int index;
78
79         if (!current->ret_stack)
80                 return -EBUSY;
81
82         /*
83          * We must make sure the ret_stack is tested before we read
84          * anything else.
85          */
86         smp_rmb();
87
88         /* The return trace stack is full */
89         if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
90                 atomic_inc(&current->trace_overrun);
91                 return -EBUSY;
92         }
93
94         calltime = trace_clock_local();
95
96         index = ++current->curr_ret_stack;
97         barrier();
98         current->ret_stack[index].ret = ret;
99         current->ret_stack[index].func = func;
100         current->ret_stack[index].calltime = calltime;
101         current->ret_stack[index].subtime = 0;
102         current->ret_stack[index].fp = frame_pointer;
103         *depth = index;
104
105         return 0;
106 }
107
108 /* Retrieve a function return address to the trace stack on thread info.*/
109 static void
110 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
111                         unsigned long frame_pointer)
112 {
113         int index;
114
115         index = current->curr_ret_stack;
116
117         if (unlikely(index < 0)) {
118                 ftrace_graph_stop();
119                 WARN_ON(1);
120                 /* Might as well panic, otherwise we have no where to go */
121                 *ret = (unsigned long)panic;
122                 return;
123         }
124
125 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
126         /*
127          * The arch may choose to record the frame pointer used
128          * and check it here to make sure that it is what we expect it
129          * to be. If gcc does not set the place holder of the return
130          * address in the frame pointer, and does a copy instead, then
131          * the function graph trace will fail. This test detects this
132          * case.
133          *
134          * Currently, x86_32 with optimize for size (-Os) makes the latest
135          * gcc do the above.
136          */
137         if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
138                 ftrace_graph_stop();
139                 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
140                      "  from func %ps return to %lx\n",
141                      current->ret_stack[index].fp,
142                      frame_pointer,
143                      (void *)current->ret_stack[index].func,
144                      current->ret_stack[index].ret);
145                 *ret = (unsigned long)panic;
146                 return;
147         }
148 #endif
149
150         *ret = current->ret_stack[index].ret;
151         trace->func = current->ret_stack[index].func;
152         trace->calltime = current->ret_stack[index].calltime;
153         trace->overrun = atomic_read(&current->trace_overrun);
154         trace->depth = index;
155 }
156
157 /*
158  * Send the trace to the ring-buffer.
159  * @return the original return address.
160  */
161 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
162 {
163         struct ftrace_graph_ret trace;
164         unsigned long ret;
165
166         ftrace_pop_return_trace(&trace, &ret, frame_pointer);
167         trace.rettime = trace_clock_local();
168         ftrace_graph_return(&trace);
169         barrier();
170         current->curr_ret_stack--;
171
172         if (unlikely(!ret)) {
173                 ftrace_graph_stop();
174                 WARN_ON(1);
175                 /* Might as well panic. What else to do? */
176                 ret = (unsigned long)panic;
177         }
178
179         return ret;
180 }
181
182 static int __trace_graph_entry(struct trace_array *tr,
183                                 struct ftrace_graph_ent *trace,
184                                 unsigned long flags,
185                                 int pc)
186 {
187         struct ftrace_event_call *call = &event_funcgraph_entry;
188         struct ring_buffer_event *event;
189         struct ring_buffer *buffer = tr->buffer;
190         struct ftrace_graph_ent_entry *entry;
191
192         if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
193                 return 0;
194
195         event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
196                                           sizeof(*entry), flags, pc);
197         if (!event)
198                 return 0;
199         entry   = ring_buffer_event_data(event);
200         entry->graph_ent                        = *trace;
201         if (!filter_current_check_discard(buffer, call, entry, event))
202                 ring_buffer_unlock_commit(buffer, event);
203
204         return 1;
205 }
206
207 int trace_graph_entry(struct ftrace_graph_ent *trace)
208 {
209         struct trace_array *tr = graph_array;
210         struct trace_array_cpu *data;
211         unsigned long flags;
212         long disabled;
213         int ret;
214         int cpu;
215         int pc;
216
217         if (!ftrace_trace_task(current))
218                 return 0;
219
220         /* trace it when it is-nested-in or is a function enabled. */
221         if (!(trace->depth || ftrace_graph_addr(trace->func)))
222                 return 0;
223
224         local_irq_save(flags);
225         cpu = raw_smp_processor_id();
226         data = tr->data[cpu];
227         disabled = atomic_inc_return(&data->disabled);
228         if (likely(disabled == 1)) {
229                 pc = preempt_count();
230                 ret = __trace_graph_entry(tr, trace, flags, pc);
231         } else {
232                 ret = 0;
233         }
234
235         atomic_dec(&data->disabled);
236         local_irq_restore(flags);
237
238         return ret;
239 }
240
241 int trace_graph_thresh_entry(struct ftrace_graph_ent *trace)
242 {
243         if (tracing_thresh)
244                 return 1;
245         else
246                 return trace_graph_entry(trace);
247 }
248
249 static void __trace_graph_return(struct trace_array *tr,
250                                 struct ftrace_graph_ret *trace,
251                                 unsigned long flags,
252                                 int pc)
253 {
254         struct ftrace_event_call *call = &event_funcgraph_exit;
255         struct ring_buffer_event *event;
256         struct ring_buffer *buffer = tr->buffer;
257         struct ftrace_graph_ret_entry *entry;
258
259         if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
260                 return;
261
262         event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
263                                           sizeof(*entry), flags, pc);
264         if (!event)
265                 return;
266         entry   = ring_buffer_event_data(event);
267         entry->ret                              = *trace;
268         if (!filter_current_check_discard(buffer, call, entry, event))
269                 ring_buffer_unlock_commit(buffer, event);
270 }
271
272 void trace_graph_return(struct ftrace_graph_ret *trace)
273 {
274         struct trace_array *tr = graph_array;
275         struct trace_array_cpu *data;
276         unsigned long flags;
277         long disabled;
278         int cpu;
279         int pc;
280
281         local_irq_save(flags);
282         cpu = raw_smp_processor_id();
283         data = tr->data[cpu];
284         disabled = atomic_inc_return(&data->disabled);
285         if (likely(disabled == 1)) {
286                 pc = preempt_count();
287                 __trace_graph_return(tr, trace, flags, pc);
288         }
289         atomic_dec(&data->disabled);
290         local_irq_restore(flags);
291 }
292
293 void set_graph_array(struct trace_array *tr)
294 {
295         graph_array = tr;
296
297         /* Make graph_array visible before we start tracing */
298
299         smp_mb();
300 }
301
302 void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
303 {
304         if (tracing_thresh &&
305             (trace->rettime - trace->calltime < tracing_thresh))
306                 return;
307         else
308                 trace_graph_return(trace);
309 }
310
311 static int graph_trace_init(struct trace_array *tr)
312 {
313         int ret;
314
315         set_graph_array(tr);
316         if (tracing_thresh)
317                 ret = register_ftrace_graph(&trace_graph_thresh_return,
318                                             &trace_graph_thresh_entry);
319         else
320                 ret = register_ftrace_graph(&trace_graph_return,
321                                             &trace_graph_entry);
322         if (ret)
323                 return ret;
324         tracing_start_cmdline_record();
325
326         return 0;
327 }
328
329 static void graph_trace_reset(struct trace_array *tr)
330 {
331         tracing_stop_cmdline_record();
332         unregister_ftrace_graph();
333 }
334
335 static int max_bytes_for_cpu;
336
337 static enum print_line_t
338 print_graph_cpu(struct trace_seq *s, int cpu)
339 {
340         int ret;
341
342         /*
343          * Start with a space character - to make it stand out
344          * to the right a bit when trace output is pasted into
345          * email:
346          */
347         ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
348         if (!ret)
349                 return TRACE_TYPE_PARTIAL_LINE;
350
351         return TRACE_TYPE_HANDLED;
352 }
353
354 #define TRACE_GRAPH_PROCINFO_LENGTH     14
355
356 static enum print_line_t
357 print_graph_proc(struct trace_seq *s, pid_t pid)
358 {
359         char comm[TASK_COMM_LEN];
360         /* sign + log10(MAX_INT) + '\0' */
361         char pid_str[11];
362         int spaces = 0;
363         int ret;
364         int len;
365         int i;
366
367         trace_find_cmdline(pid, comm);
368         comm[7] = '\0';
369         sprintf(pid_str, "%d", pid);
370
371         /* 1 stands for the "-" character */
372         len = strlen(comm) + strlen(pid_str) + 1;
373
374         if (len < TRACE_GRAPH_PROCINFO_LENGTH)
375                 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
376
377         /* First spaces to align center */
378         for (i = 0; i < spaces / 2; i++) {
379                 ret = trace_seq_printf(s, " ");
380                 if (!ret)
381                         return TRACE_TYPE_PARTIAL_LINE;
382         }
383
384         ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
385         if (!ret)
386                 return TRACE_TYPE_PARTIAL_LINE;
387
388         /* Last spaces to align center */
389         for (i = 0; i < spaces - (spaces / 2); i++) {
390                 ret = trace_seq_printf(s, " ");
391                 if (!ret)
392                         return TRACE_TYPE_PARTIAL_LINE;
393         }
394         return TRACE_TYPE_HANDLED;
395 }
396
397
398 static enum print_line_t
399 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
400 {
401         if (!trace_seq_putc(s, ' '))
402                 return 0;
403
404         return trace_print_lat_fmt(s, entry);
405 }
406
407 /* If the pid changed since the last trace, output this event */
408 static enum print_line_t
409 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
410 {
411         pid_t prev_pid;
412         pid_t *last_pid;
413         int ret;
414
415         if (!data)
416                 return TRACE_TYPE_HANDLED;
417
418         last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
419
420         if (*last_pid == pid)
421                 return TRACE_TYPE_HANDLED;
422
423         prev_pid = *last_pid;
424         *last_pid = pid;
425
426         if (prev_pid == -1)
427                 return TRACE_TYPE_HANDLED;
428 /*
429  * Context-switch trace line:
430
431  ------------------------------------------
432  | 1)  migration/0--1  =>  sshd-1755
433  ------------------------------------------
434
435  */
436         ret = trace_seq_printf(s,
437                 " ------------------------------------------\n");
438         if (!ret)
439                 return TRACE_TYPE_PARTIAL_LINE;
440
441         ret = print_graph_cpu(s, cpu);
442         if (ret == TRACE_TYPE_PARTIAL_LINE)
443                 return TRACE_TYPE_PARTIAL_LINE;
444
445         ret = print_graph_proc(s, prev_pid);
446         if (ret == TRACE_TYPE_PARTIAL_LINE)
447                 return TRACE_TYPE_PARTIAL_LINE;
448
449         ret = trace_seq_printf(s, " => ");
450         if (!ret)
451                 return TRACE_TYPE_PARTIAL_LINE;
452
453         ret = print_graph_proc(s, pid);
454         if (ret == TRACE_TYPE_PARTIAL_LINE)
455                 return TRACE_TYPE_PARTIAL_LINE;
456
457         ret = trace_seq_printf(s,
458                 "\n ------------------------------------------\n\n");
459         if (!ret)
460                 return TRACE_TYPE_PARTIAL_LINE;
461
462         return TRACE_TYPE_HANDLED;
463 }
464
465 static struct ftrace_graph_ret_entry *
466 get_return_for_leaf(struct trace_iterator *iter,
467                 struct ftrace_graph_ent_entry *curr)
468 {
469         struct fgraph_data *data = iter->private;
470         struct ring_buffer_iter *ring_iter = NULL;
471         struct ring_buffer_event *event;
472         struct ftrace_graph_ret_entry *next;
473
474         /*
475          * If the previous output failed to write to the seq buffer,
476          * then we just reuse the data from before.
477          */
478         if (data && data->failed) {
479                 curr = &data->ent;
480                 next = &data->ret;
481         } else {
482
483                 ring_iter = iter->buffer_iter[iter->cpu];
484
485                 /* First peek to compare current entry and the next one */
486                 if (ring_iter)
487                         event = ring_buffer_iter_peek(ring_iter, NULL);
488                 else {
489                         /*
490                          * We need to consume the current entry to see
491                          * the next one.
492                          */
493                         ring_buffer_consume(iter->tr->buffer, iter->cpu,
494                                             NULL, NULL);
495                         event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
496                                                  NULL, NULL);
497                 }
498
499                 if (!event)
500                         return NULL;
501
502                 next = ring_buffer_event_data(event);
503
504                 if (data) {
505                         /*
506                          * Save current and next entries for later reference
507                          * if the output fails.
508                          */
509                         data->ent = *curr;
510                         data->ret = *next;
511                 }
512         }
513
514         if (next->ent.type != TRACE_GRAPH_RET)
515                 return NULL;
516
517         if (curr->ent.pid != next->ent.pid ||
518                         curr->graph_ent.func != next->ret.func)
519                 return NULL;
520
521         /* this is a leaf, now advance the iterator */
522         if (ring_iter)
523                 ring_buffer_read(ring_iter, NULL);
524
525         return next;
526 }
527
528 /* Signal a overhead of time execution to the output */
529 static int
530 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
531 {
532         /* If duration disappear, we don't need anything */
533         if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
534                 return 1;
535
536         /* Non nested entry or return */
537         if (duration == -1)
538                 return trace_seq_printf(s, "  ");
539
540         if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
541                 /* Duration exceeded 100 msecs */
542                 if (duration > 100000ULL)
543                         return trace_seq_printf(s, "! ");
544
545                 /* Duration exceeded 10 msecs */
546                 if (duration > 10000ULL)
547                         return trace_seq_printf(s, "+ ");
548         }
549
550         return trace_seq_printf(s, "  ");
551 }
552
553 static int print_graph_abs_time(u64 t, struct trace_seq *s)
554 {
555         unsigned long usecs_rem;
556
557         usecs_rem = do_div(t, NSEC_PER_SEC);
558         usecs_rem /= 1000;
559
560         return trace_seq_printf(s, "%5lu.%06lu |  ",
561                         (unsigned long)t, usecs_rem);
562 }
563
564 static enum print_line_t
565 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
566                 enum trace_type type, int cpu, pid_t pid)
567 {
568         int ret;
569         struct trace_seq *s = &iter->seq;
570
571         if (addr < (unsigned long)__irqentry_text_start ||
572                 addr >= (unsigned long)__irqentry_text_end)
573                 return TRACE_TYPE_UNHANDLED;
574
575         /* Absolute time */
576         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
577                 ret = print_graph_abs_time(iter->ts, s);
578                 if (!ret)
579                         return TRACE_TYPE_PARTIAL_LINE;
580         }
581
582         /* Cpu */
583         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
584                 ret = print_graph_cpu(s, cpu);
585                 if (ret == TRACE_TYPE_PARTIAL_LINE)
586                         return TRACE_TYPE_PARTIAL_LINE;
587         }
588
589         /* Proc */
590         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
591                 ret = print_graph_proc(s, pid);
592                 if (ret == TRACE_TYPE_PARTIAL_LINE)
593                         return TRACE_TYPE_PARTIAL_LINE;
594                 ret = trace_seq_printf(s, " | ");
595                 if (!ret)
596                         return TRACE_TYPE_PARTIAL_LINE;
597         }
598
599         /* No overhead */
600         ret = print_graph_overhead(-1, s);
601         if (!ret)
602                 return TRACE_TYPE_PARTIAL_LINE;
603
604         if (type == TRACE_GRAPH_ENT)
605                 ret = trace_seq_printf(s, "==========>");
606         else
607                 ret = trace_seq_printf(s, "<==========");
608
609         if (!ret)
610                 return TRACE_TYPE_PARTIAL_LINE;
611
612         /* Don't close the duration column if haven't one */
613         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
614                 trace_seq_printf(s, " |");
615         ret = trace_seq_printf(s, "\n");
616
617         if (!ret)
618                 return TRACE_TYPE_PARTIAL_LINE;
619         return TRACE_TYPE_HANDLED;
620 }
621
622 enum print_line_t
623 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
624 {
625         unsigned long nsecs_rem = do_div(duration, 1000);
626         /* log10(ULONG_MAX) + '\0' */
627         char msecs_str[21];
628         char nsecs_str[5];
629         int ret, len;
630         int i;
631
632         sprintf(msecs_str, "%lu", (unsigned long) duration);
633
634         /* Print msecs */
635         ret = trace_seq_printf(s, "%s", msecs_str);
636         if (!ret)
637                 return TRACE_TYPE_PARTIAL_LINE;
638
639         len = strlen(msecs_str);
640
641         /* Print nsecs (we don't want to exceed 7 numbers) */
642         if (len < 7) {
643                 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
644                 ret = trace_seq_printf(s, ".%s", nsecs_str);
645                 if (!ret)
646                         return TRACE_TYPE_PARTIAL_LINE;
647                 len += strlen(nsecs_str);
648         }
649
650         ret = trace_seq_printf(s, " us ");
651         if (!ret)
652                 return TRACE_TYPE_PARTIAL_LINE;
653
654         /* Print remaining spaces to fit the row's width */
655         for (i = len; i < 7; i++) {
656                 ret = trace_seq_printf(s, " ");
657                 if (!ret)
658                         return TRACE_TYPE_PARTIAL_LINE;
659         }
660         return TRACE_TYPE_HANDLED;
661 }
662
663 static enum print_line_t
664 print_graph_duration(unsigned long long duration, struct trace_seq *s)
665 {
666         int ret;
667
668         ret = trace_print_graph_duration(duration, s);
669         if (ret != TRACE_TYPE_HANDLED)
670                 return ret;
671
672         ret = trace_seq_printf(s, "|  ");
673         if (!ret)
674                 return TRACE_TYPE_PARTIAL_LINE;
675
676         return TRACE_TYPE_HANDLED;
677 }
678
679 /* Case of a leaf function on its call entry */
680 static enum print_line_t
681 print_graph_entry_leaf(struct trace_iterator *iter,
682                 struct ftrace_graph_ent_entry *entry,
683                 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
684 {
685         struct fgraph_data *data = iter->private;
686         struct ftrace_graph_ret *graph_ret;
687         struct ftrace_graph_ent *call;
688         unsigned long long duration;
689         int ret;
690         int i;
691
692         graph_ret = &ret_entry->ret;
693         call = &entry->graph_ent;
694         duration = graph_ret->rettime - graph_ret->calltime;
695
696         if (data) {
697                 struct fgraph_cpu_data *cpu_data;
698                 int cpu = iter->cpu;
699
700                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
701
702                 /*
703                  * Comments display at + 1 to depth. Since
704                  * this is a leaf function, keep the comments
705                  * equal to this depth.
706                  */
707                 cpu_data->depth = call->depth - 1;
708
709                 /* No need to keep this function around for this depth */
710                 if (call->depth < FTRACE_RETFUNC_DEPTH)
711                         cpu_data->enter_funcs[call->depth] = 0;
712         }
713
714         /* Overhead */
715         ret = print_graph_overhead(duration, s);
716         if (!ret)
717                 return TRACE_TYPE_PARTIAL_LINE;
718
719         /* Duration */
720         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
721                 ret = print_graph_duration(duration, s);
722                 if (ret == TRACE_TYPE_PARTIAL_LINE)
723                         return TRACE_TYPE_PARTIAL_LINE;
724         }
725
726         /* Function */
727         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
728                 ret = trace_seq_printf(s, " ");
729                 if (!ret)
730                         return TRACE_TYPE_PARTIAL_LINE;
731         }
732
733         ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
734         if (!ret)
735                 return TRACE_TYPE_PARTIAL_LINE;
736
737         return TRACE_TYPE_HANDLED;
738 }
739
740 static enum print_line_t
741 print_graph_entry_nested(struct trace_iterator *iter,
742                          struct ftrace_graph_ent_entry *entry,
743                          struct trace_seq *s, int cpu)
744 {
745         struct ftrace_graph_ent *call = &entry->graph_ent;
746         struct fgraph_data *data = iter->private;
747         int ret;
748         int i;
749
750         if (data) {
751                 struct fgraph_cpu_data *cpu_data;
752                 int cpu = iter->cpu;
753
754                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
755                 cpu_data->depth = call->depth;
756
757                 /* Save this function pointer to see if the exit matches */
758                 if (call->depth < FTRACE_RETFUNC_DEPTH)
759                         cpu_data->enter_funcs[call->depth] = call->func;
760         }
761
762         /* No overhead */
763         ret = print_graph_overhead(-1, s);
764         if (!ret)
765                 return TRACE_TYPE_PARTIAL_LINE;
766
767         /* No time */
768         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
769                 ret = trace_seq_printf(s, "            |  ");
770                 if (!ret)
771                         return TRACE_TYPE_PARTIAL_LINE;
772         }
773
774         /* Function */
775         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
776                 ret = trace_seq_printf(s, " ");
777                 if (!ret)
778                         return TRACE_TYPE_PARTIAL_LINE;
779         }
780
781         ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
782         if (!ret)
783                 return TRACE_TYPE_PARTIAL_LINE;
784
785         /*
786          * we already consumed the current entry to check the next one
787          * and see if this is a leaf.
788          */
789         return TRACE_TYPE_NO_CONSUME;
790 }
791
792 static enum print_line_t
793 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
794                      int type, unsigned long addr)
795 {
796         struct fgraph_data *data = iter->private;
797         struct trace_entry *ent = iter->ent;
798         int cpu = iter->cpu;
799         int ret;
800
801         /* Pid */
802         if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
803                 return TRACE_TYPE_PARTIAL_LINE;
804
805         if (type) {
806                 /* Interrupt */
807                 ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
808                 if (ret == TRACE_TYPE_PARTIAL_LINE)
809                         return TRACE_TYPE_PARTIAL_LINE;
810         }
811
812         /* Absolute time */
813         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
814                 ret = print_graph_abs_time(iter->ts, s);
815                 if (!ret)
816                         return TRACE_TYPE_PARTIAL_LINE;
817         }
818
819         /* Cpu */
820         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
821                 ret = print_graph_cpu(s, cpu);
822                 if (ret == TRACE_TYPE_PARTIAL_LINE)
823                         return TRACE_TYPE_PARTIAL_LINE;
824         }
825
826         /* Proc */
827         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
828                 ret = print_graph_proc(s, ent->pid);
829                 if (ret == TRACE_TYPE_PARTIAL_LINE)
830                         return TRACE_TYPE_PARTIAL_LINE;
831
832                 ret = trace_seq_printf(s, " | ");
833                 if (!ret)
834                         return TRACE_TYPE_PARTIAL_LINE;
835         }
836
837         /* Latency format */
838         if (trace_flags & TRACE_ITER_LATENCY_FMT) {
839                 ret = print_graph_lat_fmt(s, ent);
840                 if (ret == TRACE_TYPE_PARTIAL_LINE)
841                         return TRACE_TYPE_PARTIAL_LINE;
842         }
843
844         return 0;
845 }
846
847 static enum print_line_t
848 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
849                         struct trace_iterator *iter)
850 {
851         struct fgraph_data *data = iter->private;
852         struct ftrace_graph_ent *call = &field->graph_ent;
853         struct ftrace_graph_ret_entry *leaf_ret;
854         static enum print_line_t ret;
855         int cpu = iter->cpu;
856
857         if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
858                 return TRACE_TYPE_PARTIAL_LINE;
859
860         leaf_ret = get_return_for_leaf(iter, field);
861         if (leaf_ret)
862                 ret = print_graph_entry_leaf(iter, field, leaf_ret, s);
863         else
864                 ret = print_graph_entry_nested(iter, field, s, cpu);
865
866         if (data) {
867                 /*
868                  * If we failed to write our output, then we need to make
869                  * note of it. Because we already consumed our entry.
870                  */
871                 if (s->full) {
872                         data->failed = 1;
873                         data->cpu = cpu;
874                 } else
875                         data->failed = 0;
876         }
877
878         return ret;
879 }
880
881 static enum print_line_t
882 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
883                    struct trace_entry *ent, struct trace_iterator *iter)
884 {
885         unsigned long long duration = trace->rettime - trace->calltime;
886         struct fgraph_data *data = iter->private;
887         pid_t pid = ent->pid;
888         int cpu = iter->cpu;
889         int func_match = 1;
890         int ret;
891         int i;
892
893         if (data) {
894                 struct fgraph_cpu_data *cpu_data;
895                 int cpu = iter->cpu;
896
897                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
898
899                 /*
900                  * Comments display at + 1 to depth. This is the
901                  * return from a function, we now want the comments
902                  * to display at the same level of the bracket.
903                  */
904                 cpu_data->depth = trace->depth - 1;
905
906                 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
907                         if (cpu_data->enter_funcs[trace->depth] != trace->func)
908                                 func_match = 0;
909                         cpu_data->enter_funcs[trace->depth] = 0;
910                 }
911         }
912
913         if (print_graph_prologue(iter, s, 0, 0))
914                 return TRACE_TYPE_PARTIAL_LINE;
915
916         /* Overhead */
917         ret = print_graph_overhead(duration, s);
918         if (!ret)
919                 return TRACE_TYPE_PARTIAL_LINE;
920
921         /* Duration */
922         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
923                 ret = print_graph_duration(duration, s);
924                 if (ret == TRACE_TYPE_PARTIAL_LINE)
925                         return TRACE_TYPE_PARTIAL_LINE;
926         }
927
928         /* Closing brace */
929         for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
930                 ret = trace_seq_printf(s, " ");
931                 if (!ret)
932                         return TRACE_TYPE_PARTIAL_LINE;
933         }
934
935         /*
936          * If the return function does not have a matching entry,
937          * then the entry was lost. Instead of just printing
938          * the '}' and letting the user guess what function this
939          * belongs to, write out the function name.
940          */
941         if (func_match) {
942                 ret = trace_seq_printf(s, "}\n");
943                 if (!ret)
944                         return TRACE_TYPE_PARTIAL_LINE;
945         } else {
946                 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
947                 if (!ret)
948                         return TRACE_TYPE_PARTIAL_LINE;
949         }
950
951         /* Overrun */
952         if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
953                 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
954                                         trace->overrun);
955                 if (!ret)
956                         return TRACE_TYPE_PARTIAL_LINE;
957         }
958
959         ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
960         if (ret == TRACE_TYPE_PARTIAL_LINE)
961                 return TRACE_TYPE_PARTIAL_LINE;
962
963         return TRACE_TYPE_HANDLED;
964 }
965
966 static enum print_line_t
967 print_graph_comment(struct trace_seq *s,  struct trace_entry *ent,
968                     struct trace_iterator *iter)
969 {
970         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
971         struct fgraph_data *data = iter->private;
972         struct trace_event *event;
973         int depth = 0;
974         int ret;
975         int i;
976
977         if (data)
978                 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
979
980         if (print_graph_prologue(iter, s, 0, 0))
981                 return TRACE_TYPE_PARTIAL_LINE;
982
983         /* No overhead */
984         ret = print_graph_overhead(-1, s);
985         if (!ret)
986                 return TRACE_TYPE_PARTIAL_LINE;
987
988         /* No time */
989         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
990                 ret = trace_seq_printf(s, "            |  ");
991                 if (!ret)
992                         return TRACE_TYPE_PARTIAL_LINE;
993         }
994
995         /* Indentation */
996         if (depth > 0)
997                 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
998                         ret = trace_seq_printf(s, " ");
999                         if (!ret)
1000                                 return TRACE_TYPE_PARTIAL_LINE;
1001                 }
1002
1003         /* The comment */
1004         ret = trace_seq_printf(s, "/* ");
1005         if (!ret)
1006                 return TRACE_TYPE_PARTIAL_LINE;
1007
1008         switch (iter->ent->type) {
1009         case TRACE_BPRINT:
1010                 ret = trace_print_bprintk_msg_only(iter);
1011                 if (ret != TRACE_TYPE_HANDLED)
1012                         return ret;
1013                 break;
1014         case TRACE_PRINT:
1015                 ret = trace_print_printk_msg_only(iter);
1016                 if (ret != TRACE_TYPE_HANDLED)
1017                         return ret;
1018                 break;
1019         default:
1020                 event = ftrace_find_event(ent->type);
1021                 if (!event)
1022                         return TRACE_TYPE_UNHANDLED;
1023
1024                 ret = event->trace(iter, sym_flags);
1025                 if (ret != TRACE_TYPE_HANDLED)
1026                         return ret;
1027         }
1028
1029         /* Strip ending newline */
1030         if (s->buffer[s->len - 1] == '\n') {
1031                 s->buffer[s->len - 1] = '\0';
1032                 s->len--;
1033         }
1034
1035         ret = trace_seq_printf(s, " */\n");
1036         if (!ret)
1037                 return TRACE_TYPE_PARTIAL_LINE;
1038
1039         return TRACE_TYPE_HANDLED;
1040 }
1041
1042
1043 enum print_line_t
1044 print_graph_function(struct trace_iterator *iter)
1045 {
1046         struct ftrace_graph_ent_entry *field;
1047         struct fgraph_data *data = iter->private;
1048         struct trace_entry *entry = iter->ent;
1049         struct trace_seq *s = &iter->seq;
1050         int cpu = iter->cpu;
1051         int ret;
1052
1053         if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1054                 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1055                 return TRACE_TYPE_HANDLED;
1056         }
1057
1058         /*
1059          * If the last output failed, there's a possibility we need
1060          * to print out the missing entry which would never go out.
1061          */
1062         if (data && data->failed) {
1063                 field = &data->ent;
1064                 iter->cpu = data->cpu;
1065                 ret = print_graph_entry(field, s, iter);
1066                 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1067                         per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1068                         ret = TRACE_TYPE_NO_CONSUME;
1069                 }
1070                 iter->cpu = cpu;
1071                 return ret;
1072         }
1073
1074         switch (entry->type) {
1075         case TRACE_GRAPH_ENT: {
1076                 /*
1077                  * print_graph_entry() may consume the current event,
1078                  * thus @field may become invalid, so we need to save it.
1079                  * sizeof(struct ftrace_graph_ent_entry) is very small,
1080                  * it can be safely saved at the stack.
1081                  */
1082                 struct ftrace_graph_ent_entry saved;
1083                 trace_assign_type(field, entry);
1084                 saved = *field;
1085                 return print_graph_entry(&saved, s, iter);
1086         }
1087         case TRACE_GRAPH_RET: {
1088                 struct ftrace_graph_ret_entry *field;
1089                 trace_assign_type(field, entry);
1090                 return print_graph_return(&field->ret, s, entry, iter);
1091         }
1092         default:
1093                 return print_graph_comment(s, entry, iter);
1094         }
1095
1096         return TRACE_TYPE_HANDLED;
1097 }
1098
1099 static void print_lat_header(struct seq_file *s)
1100 {
1101         static const char spaces[] = "                " /* 16 spaces */
1102                 "    "                                  /* 4 spaces */
1103                 "                 ";                    /* 17 spaces */
1104         int size = 0;
1105
1106         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1107                 size += 16;
1108         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1109                 size += 4;
1110         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1111                 size += 17;
1112
1113         seq_printf(s, "#%.*s  _-----=> irqs-off        \n", size, spaces);
1114         seq_printf(s, "#%.*s / _----=> need-resched    \n", size, spaces);
1115         seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1116         seq_printf(s, "#%.*s|| / _--=> preempt-depth   \n", size, spaces);
1117         seq_printf(s, "#%.*s||| / _-=> lock-depth      \n", size, spaces);
1118         seq_printf(s, "#%.*s|||| /                     \n", size, spaces);
1119 }
1120
1121 static void print_graph_headers(struct seq_file *s)
1122 {
1123         int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1124
1125         if (lat)
1126                 print_lat_header(s);
1127
1128         /* 1st line */
1129         seq_printf(s, "#");
1130         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1131                 seq_printf(s, "     TIME       ");
1132         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1133                 seq_printf(s, " CPU");
1134         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1135                 seq_printf(s, "  TASK/PID       ");
1136         if (lat)
1137                 seq_printf(s, "|||||");
1138         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1139                 seq_printf(s, "  DURATION   ");
1140         seq_printf(s, "               FUNCTION CALLS\n");
1141
1142         /* 2nd line */
1143         seq_printf(s, "#");
1144         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1145                 seq_printf(s, "      |         ");
1146         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1147                 seq_printf(s, " |  ");
1148         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1149                 seq_printf(s, "   |    |        ");
1150         if (lat)
1151                 seq_printf(s, "|||||");
1152         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1153                 seq_printf(s, "   |   |      ");
1154         seq_printf(s, "               |   |   |   |\n");
1155 }
1156
1157 static void graph_trace_open(struct trace_iterator *iter)
1158 {
1159         /* pid and depth on the last trace processed */
1160         struct fgraph_data *data;
1161         int cpu;
1162
1163         iter->private = NULL;
1164
1165         data = kzalloc(sizeof(*data), GFP_KERNEL);
1166         if (!data)
1167                 goto out_err;
1168
1169         data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1170         if (!data->cpu_data)
1171                 goto out_err_free;
1172
1173         for_each_possible_cpu(cpu) {
1174                 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1175                 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1176                 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1177                 *pid = -1;
1178                 *depth = 0;
1179                 *ignore = 0;
1180         }
1181
1182         iter->private = data;
1183
1184         return;
1185
1186  out_err_free:
1187         kfree(data);
1188  out_err:
1189         pr_warning("function graph tracer: not enough memory\n");
1190 }
1191
1192 static void graph_trace_close(struct trace_iterator *iter)
1193 {
1194         struct fgraph_data *data = iter->private;
1195
1196         if (data) {
1197                 free_percpu(data->cpu_data);
1198                 kfree(data);
1199         }
1200 }
1201
1202 static struct tracer graph_trace __read_mostly = {
1203         .name           = "function_graph",
1204         .open           = graph_trace_open,
1205         .pipe_open      = graph_trace_open,
1206         .close          = graph_trace_close,
1207         .pipe_close     = graph_trace_close,
1208         .wait_pipe      = poll_wait_pipe,
1209         .init           = graph_trace_init,
1210         .reset          = graph_trace_reset,
1211         .print_line     = print_graph_function,
1212         .print_header   = print_graph_headers,
1213         .flags          = &tracer_flags,
1214 #ifdef CONFIG_FTRACE_SELFTEST
1215         .selftest       = trace_selftest_startup_function_graph,
1216 #endif
1217 };
1218
1219 static __init int init_graph_trace(void)
1220 {
1221         max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1222
1223         return register_tracer(&graph_trace);
1224 }
1225
1226 device_initcall(init_graph_trace);