Merge branch 'mainline/function-graph' of git://git.kernel.org/pub/scm/linux/kernel...
[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/fs.h>
13
14 #include "trace.h"
15 #include "trace_output.h"
16
17 #define TRACE_GRAPH_INDENT      2
18
19 /* Flag options */
20 #define TRACE_GRAPH_PRINT_OVERRUN       0x1
21 #define TRACE_GRAPH_PRINT_CPU           0x2
22 #define TRACE_GRAPH_PRINT_OVERHEAD      0x4
23 #define TRACE_GRAPH_PRINT_PROC          0x8
24 #define TRACE_GRAPH_PRINT_DURATION      0x10
25 #define TRACE_GRAPH_PRINT_ABS_TIME      0X20
26
27 static struct tracer_opt trace_opts[] = {
28         /* Display overruns? (for self-debug purpose) */
29         { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
30         /* Display CPU ? */
31         { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
32         /* Display Overhead ? */
33         { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
34         /* Display proc name/pid */
35         { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
36         /* Display duration of execution */
37         { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
38         /* Display absolute time of an entry */
39         { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
40         { } /* Empty entry */
41 };
42
43 static struct tracer_flags tracer_flags = {
44         /* Don't display overruns and proc by default */
45         .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
46                TRACE_GRAPH_PRINT_DURATION,
47         .opts = trace_opts
48 };
49
50 /* pid on the last trace processed */
51
52
53 /* Add a function return address to the trace stack on thread info.*/
54 int
55 ftrace_push_return_trace(unsigned long ret, unsigned long long time,
56                          unsigned long func, int *depth)
57 {
58         int index;
59
60         if (!current->ret_stack)
61                 return -EBUSY;
62
63         /* The return trace stack is full */
64         if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
65                 atomic_inc(&current->trace_overrun);
66                 return -EBUSY;
67         }
68
69         index = ++current->curr_ret_stack;
70         barrier();
71         current->ret_stack[index].ret = ret;
72         current->ret_stack[index].func = func;
73         current->ret_stack[index].calltime = time;
74         *depth = index;
75
76         return 0;
77 }
78
79 /* Retrieve a function return address to the trace stack on thread info.*/
80 void
81 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret)
82 {
83         int index;
84
85         index = current->curr_ret_stack;
86
87         if (unlikely(index < 0)) {
88                 ftrace_graph_stop();
89                 WARN_ON(1);
90                 /* Might as well panic, otherwise we have no where to go */
91                 *ret = (unsigned long)panic;
92                 return;
93         }
94
95         *ret = current->ret_stack[index].ret;
96         trace->func = current->ret_stack[index].func;
97         trace->calltime = current->ret_stack[index].calltime;
98         trace->overrun = atomic_read(&current->trace_overrun);
99         trace->depth = index;
100         barrier();
101         current->curr_ret_stack--;
102
103 }
104
105 /*
106  * Send the trace to the ring-buffer.
107  * @return the original return address.
108  */
109 unsigned long ftrace_return_to_handler(void)
110 {
111         struct ftrace_graph_ret trace;
112         unsigned long ret;
113
114         ftrace_pop_return_trace(&trace, &ret);
115         trace.rettime = cpu_clock(raw_smp_processor_id());
116         ftrace_graph_return(&trace);
117
118         if (unlikely(!ret)) {
119                 ftrace_graph_stop();
120                 WARN_ON(1);
121                 /* Might as well panic. What else to do? */
122                 ret = (unsigned long)panic;
123         }
124
125         return ret;
126 }
127
128 static int graph_trace_init(struct trace_array *tr)
129 {
130         int ret = register_ftrace_graph(&trace_graph_return,
131                                         &trace_graph_entry);
132         if (ret)
133                 return ret;
134         tracing_start_cmdline_record();
135
136         return 0;
137 }
138
139 static void graph_trace_reset(struct trace_array *tr)
140 {
141         tracing_stop_cmdline_record();
142         unregister_ftrace_graph();
143 }
144
145 static inline int log10_cpu(int nb)
146 {
147         if (nb / 100)
148                 return 3;
149         if (nb / 10)
150                 return 2;
151         return 1;
152 }
153
154 static enum print_line_t
155 print_graph_cpu(struct trace_seq *s, int cpu)
156 {
157         int i;
158         int ret;
159         int log10_this = log10_cpu(cpu);
160         int log10_all = log10_cpu(cpumask_weight(cpu_online_mask));
161
162
163         /*
164          * Start with a space character - to make it stand out
165          * to the right a bit when trace output is pasted into
166          * email:
167          */
168         ret = trace_seq_printf(s, " ");
169
170         /*
171          * Tricky - we space the CPU field according to the max
172          * number of online CPUs. On a 2-cpu system it would take
173          * a maximum of 1 digit - on a 128 cpu system it would
174          * take up to 3 digits:
175          */
176         for (i = 0; i < log10_all - log10_this; i++) {
177                 ret = trace_seq_printf(s, " ");
178                 if (!ret)
179                         return TRACE_TYPE_PARTIAL_LINE;
180         }
181         ret = trace_seq_printf(s, "%d) ", cpu);
182         if (!ret)
183                 return TRACE_TYPE_PARTIAL_LINE;
184
185         return TRACE_TYPE_HANDLED;
186 }
187
188 #define TRACE_GRAPH_PROCINFO_LENGTH     14
189
190 static enum print_line_t
191 print_graph_proc(struct trace_seq *s, pid_t pid)
192 {
193         int i;
194         int ret;
195         int len;
196         char comm[8];
197         int spaces = 0;
198         /* sign + log10(MAX_INT) + '\0' */
199         char pid_str[11];
200
201         strncpy(comm, trace_find_cmdline(pid), 7);
202         comm[7] = '\0';
203         sprintf(pid_str, "%d", pid);
204
205         /* 1 stands for the "-" character */
206         len = strlen(comm) + strlen(pid_str) + 1;
207
208         if (len < TRACE_GRAPH_PROCINFO_LENGTH)
209                 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
210
211         /* First spaces to align center */
212         for (i = 0; i < spaces / 2; i++) {
213                 ret = trace_seq_printf(s, " ");
214                 if (!ret)
215                         return TRACE_TYPE_PARTIAL_LINE;
216         }
217
218         ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
219         if (!ret)
220                 return TRACE_TYPE_PARTIAL_LINE;
221
222         /* Last spaces to align center */
223         for (i = 0; i < spaces - (spaces / 2); i++) {
224                 ret = trace_seq_printf(s, " ");
225                 if (!ret)
226                         return TRACE_TYPE_PARTIAL_LINE;
227         }
228         return TRACE_TYPE_HANDLED;
229 }
230
231
232 /* If the pid changed since the last trace, output this event */
233 static enum print_line_t
234 verif_pid(struct trace_seq *s, pid_t pid, int cpu, pid_t *last_pids_cpu)
235 {
236         pid_t prev_pid;
237         pid_t *last_pid;
238         int ret;
239
240         if (!last_pids_cpu)
241                 return TRACE_TYPE_HANDLED;
242
243         last_pid = per_cpu_ptr(last_pids_cpu, cpu);
244
245         if (*last_pid == pid)
246                 return TRACE_TYPE_HANDLED;
247
248         prev_pid = *last_pid;
249         *last_pid = pid;
250
251         if (prev_pid == -1)
252                 return TRACE_TYPE_HANDLED;
253 /*
254  * Context-switch trace line:
255
256  ------------------------------------------
257  | 1)  migration/0--1  =>  sshd-1755
258  ------------------------------------------
259
260  */
261         ret = trace_seq_printf(s,
262                 " ------------------------------------------\n");
263         if (!ret)
264                 return TRACE_TYPE_PARTIAL_LINE;
265
266         ret = print_graph_cpu(s, cpu);
267         if (ret == TRACE_TYPE_PARTIAL_LINE)
268                 return TRACE_TYPE_PARTIAL_LINE;
269
270         ret = print_graph_proc(s, prev_pid);
271         if (ret == TRACE_TYPE_PARTIAL_LINE)
272                 return TRACE_TYPE_PARTIAL_LINE;
273
274         ret = trace_seq_printf(s, " => ");
275         if (!ret)
276                 return TRACE_TYPE_PARTIAL_LINE;
277
278         ret = print_graph_proc(s, pid);
279         if (ret == TRACE_TYPE_PARTIAL_LINE)
280                 return TRACE_TYPE_PARTIAL_LINE;
281
282         ret = trace_seq_printf(s,
283                 "\n ------------------------------------------\n\n");
284         if (!ret)
285                 return TRACE_TYPE_PARTIAL_LINE;
286
287         return TRACE_TYPE_HANDLED;
288 }
289
290 static struct ftrace_graph_ret_entry *
291 get_return_for_leaf(struct trace_iterator *iter,
292                 struct ftrace_graph_ent_entry *curr)
293 {
294         struct ring_buffer_iter *ring_iter;
295         struct ring_buffer_event *event;
296         struct ftrace_graph_ret_entry *next;
297
298         ring_iter = iter->buffer_iter[iter->cpu];
299
300         /* First peek to compare current entry and the next one */
301         if (ring_iter)
302                 event = ring_buffer_iter_peek(ring_iter, NULL);
303         else {
304         /* We need to consume the current entry to see the next one */
305                 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
306                 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
307                                         NULL);
308         }
309
310         if (!event)
311                 return NULL;
312
313         next = ring_buffer_event_data(event);
314
315         if (next->ent.type != TRACE_GRAPH_RET)
316                 return NULL;
317
318         if (curr->ent.pid != next->ent.pid ||
319                         curr->graph_ent.func != next->ret.func)
320                 return NULL;
321
322         /* this is a leaf, now advance the iterator */
323         if (ring_iter)
324                 ring_buffer_read(ring_iter, NULL);
325
326         return next;
327 }
328
329 /* Signal a overhead of time execution to the output */
330 static int
331 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
332 {
333         /* If duration disappear, we don't need anything */
334         if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
335                 return 1;
336
337         /* Non nested entry or return */
338         if (duration == -1)
339                 return trace_seq_printf(s, "  ");
340
341         if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
342                 /* Duration exceeded 100 msecs */
343                 if (duration > 100000ULL)
344                         return trace_seq_printf(s, "! ");
345
346                 /* Duration exceeded 10 msecs */
347                 if (duration > 10000ULL)
348                         return trace_seq_printf(s, "+ ");
349         }
350
351         return trace_seq_printf(s, "  ");
352 }
353
354 static enum print_line_t
355 print_graph_irq(struct trace_seq *s, unsigned long addr,
356                 enum trace_type type, int cpu, pid_t pid)
357 {
358         int ret;
359
360         if (addr < (unsigned long)__irqentry_text_start ||
361                 addr >= (unsigned long)__irqentry_text_end)
362                 return TRACE_TYPE_UNHANDLED;
363
364         /* Cpu */
365         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
366                 ret = print_graph_cpu(s, cpu);
367                 if (ret == TRACE_TYPE_PARTIAL_LINE)
368                         return TRACE_TYPE_PARTIAL_LINE;
369         }
370         /* Proc */
371         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
372                 ret = print_graph_proc(s, pid);
373                 if (ret == TRACE_TYPE_PARTIAL_LINE)
374                         return TRACE_TYPE_PARTIAL_LINE;
375                 ret = trace_seq_printf(s, " | ");
376                 if (!ret)
377                         return TRACE_TYPE_PARTIAL_LINE;
378         }
379
380         /* No overhead */
381         ret = print_graph_overhead(-1, s);
382         if (!ret)
383                 return TRACE_TYPE_PARTIAL_LINE;
384
385         if (type == TRACE_GRAPH_ENT)
386                 ret = trace_seq_printf(s, "==========>");
387         else
388                 ret = trace_seq_printf(s, "<==========");
389
390         if (!ret)
391                 return TRACE_TYPE_PARTIAL_LINE;
392
393         /* Don't close the duration column if haven't one */
394         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
395                 trace_seq_printf(s, " |");
396         ret = trace_seq_printf(s, "\n");
397
398         if (!ret)
399                 return TRACE_TYPE_PARTIAL_LINE;
400         return TRACE_TYPE_HANDLED;
401 }
402
403 static enum print_line_t
404 print_graph_duration(unsigned long long duration, struct trace_seq *s)
405 {
406         unsigned long nsecs_rem = do_div(duration, 1000);
407         /* log10(ULONG_MAX) + '\0' */
408         char msecs_str[21];
409         char nsecs_str[5];
410         int ret, len;
411         int i;
412
413         sprintf(msecs_str, "%lu", (unsigned long) duration);
414
415         /* Print msecs */
416         ret = trace_seq_printf(s, "%s", msecs_str);
417         if (!ret)
418                 return TRACE_TYPE_PARTIAL_LINE;
419
420         len = strlen(msecs_str);
421
422         /* Print nsecs (we don't want to exceed 7 numbers) */
423         if (len < 7) {
424                 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
425                 ret = trace_seq_printf(s, ".%s", nsecs_str);
426                 if (!ret)
427                         return TRACE_TYPE_PARTIAL_LINE;
428                 len += strlen(nsecs_str);
429         }
430
431         ret = trace_seq_printf(s, " us ");
432         if (!ret)
433                 return TRACE_TYPE_PARTIAL_LINE;
434
435         /* Print remaining spaces to fit the row's width */
436         for (i = len; i < 7; i++) {
437                 ret = trace_seq_printf(s, " ");
438                 if (!ret)
439                         return TRACE_TYPE_PARTIAL_LINE;
440         }
441
442         ret = trace_seq_printf(s, "|  ");
443         if (!ret)
444                 return TRACE_TYPE_PARTIAL_LINE;
445         return TRACE_TYPE_HANDLED;
446
447 }
448
449 static int print_graph_abs_time(u64 t, struct trace_seq *s)
450 {
451         unsigned long usecs_rem;
452
453         usecs_rem = do_div(t, 1000000000);
454         usecs_rem /= 1000;
455
456         return trace_seq_printf(s, "%5lu.%06lu |  ",
457                         (unsigned long)t, usecs_rem);
458 }
459
460 /* Case of a leaf function on its call entry */
461 static enum print_line_t
462 print_graph_entry_leaf(struct trace_iterator *iter,
463                 struct ftrace_graph_ent_entry *entry,
464                 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
465 {
466         struct ftrace_graph_ret *graph_ret;
467         struct ftrace_graph_ent *call;
468         unsigned long long duration;
469         int ret;
470         int i;
471
472         graph_ret = &ret_entry->ret;
473         call = &entry->graph_ent;
474         duration = graph_ret->rettime - graph_ret->calltime;
475
476         /* Overhead */
477         ret = print_graph_overhead(duration, s);
478         if (!ret)
479                 return TRACE_TYPE_PARTIAL_LINE;
480
481         /* Duration */
482         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
483                 ret = print_graph_duration(duration, s);
484                 if (ret == TRACE_TYPE_PARTIAL_LINE)
485                         return TRACE_TYPE_PARTIAL_LINE;
486         }
487
488         /* Function */
489         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
490                 ret = trace_seq_printf(s, " ");
491                 if (!ret)
492                         return TRACE_TYPE_PARTIAL_LINE;
493         }
494
495         ret = seq_print_ip_sym(s, call->func, 0);
496         if (!ret)
497                 return TRACE_TYPE_PARTIAL_LINE;
498
499         ret = trace_seq_printf(s, "();\n");
500         if (!ret)
501                 return TRACE_TYPE_PARTIAL_LINE;
502
503         return TRACE_TYPE_HANDLED;
504 }
505
506 static enum print_line_t
507 print_graph_entry_nested(struct ftrace_graph_ent_entry *entry,
508                         struct trace_seq *s, pid_t pid, int cpu)
509 {
510         int i;
511         int ret;
512         struct ftrace_graph_ent *call = &entry->graph_ent;
513
514         /* No overhead */
515         ret = print_graph_overhead(-1, s);
516         if (!ret)
517                 return TRACE_TYPE_PARTIAL_LINE;
518
519         /* No time */
520         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
521                 ret = trace_seq_printf(s, "            |  ");
522                 if (!ret)
523                         return TRACE_TYPE_PARTIAL_LINE;
524         }
525
526         /* Function */
527         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
528                 ret = trace_seq_printf(s, " ");
529                 if (!ret)
530                         return TRACE_TYPE_PARTIAL_LINE;
531         }
532
533         ret = seq_print_ip_sym(s, call->func, 0);
534         if (!ret)
535                 return TRACE_TYPE_PARTIAL_LINE;
536
537         ret = trace_seq_printf(s, "() {\n");
538         if (!ret)
539                 return TRACE_TYPE_PARTIAL_LINE;
540
541         /*
542          * we already consumed the current entry to check the next one
543          * and see if this is a leaf.
544          */
545         return TRACE_TYPE_NO_CONSUME;
546 }
547
548 static enum print_line_t
549 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
550                         struct trace_iterator *iter)
551 {
552         int ret;
553         int cpu = iter->cpu;
554         pid_t *last_entry = iter->private;
555         struct trace_entry *ent = iter->ent;
556         struct ftrace_graph_ent *call = &field->graph_ent;
557         struct ftrace_graph_ret_entry *leaf_ret;
558
559         /* Pid */
560         if (verif_pid(s, ent->pid, cpu, last_entry) == TRACE_TYPE_PARTIAL_LINE)
561                 return TRACE_TYPE_PARTIAL_LINE;
562
563         /* Interrupt */
564         ret = print_graph_irq(s, call->func, TRACE_GRAPH_ENT, cpu, ent->pid);
565         if (ret == TRACE_TYPE_PARTIAL_LINE)
566                 return TRACE_TYPE_PARTIAL_LINE;
567
568         /* Absolute time */
569         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
570                 ret = print_graph_abs_time(iter->ts, s);
571                 if (!ret)
572                         return TRACE_TYPE_PARTIAL_LINE;
573         }
574
575         /* Cpu */
576         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
577                 ret = print_graph_cpu(s, cpu);
578                 if (ret == TRACE_TYPE_PARTIAL_LINE)
579                         return TRACE_TYPE_PARTIAL_LINE;
580         }
581
582         /* Proc */
583         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
584                 ret = print_graph_proc(s, ent->pid);
585                 if (ret == TRACE_TYPE_PARTIAL_LINE)
586                         return TRACE_TYPE_PARTIAL_LINE;
587
588                 ret = trace_seq_printf(s, " | ");
589                 if (!ret)
590                         return TRACE_TYPE_PARTIAL_LINE;
591         }
592
593         leaf_ret = get_return_for_leaf(iter, field);
594         if (leaf_ret)
595                 return print_graph_entry_leaf(iter, field, leaf_ret, s);
596         else
597                 return print_graph_entry_nested(field, s, iter->ent->pid, cpu);
598
599 }
600
601 static enum print_line_t
602 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
603                    struct trace_entry *ent, struct trace_iterator *iter)
604 {
605         int i;
606         int ret;
607         int cpu = iter->cpu;
608         pid_t *last_pid = iter->private;
609         unsigned long long duration = trace->rettime - trace->calltime;
610
611         /* Pid */
612         if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
613                 return TRACE_TYPE_PARTIAL_LINE;
614
615         /* Absolute time */
616         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
617                 ret = print_graph_abs_time(iter->ts, s);
618                 if (!ret)
619                         return TRACE_TYPE_PARTIAL_LINE;
620         }
621
622         /* Cpu */
623         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
624                 ret = print_graph_cpu(s, cpu);
625                 if (ret == TRACE_TYPE_PARTIAL_LINE)
626                         return TRACE_TYPE_PARTIAL_LINE;
627         }
628
629         /* Proc */
630         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
631                 ret = print_graph_proc(s, ent->pid);
632                 if (ret == TRACE_TYPE_PARTIAL_LINE)
633                         return TRACE_TYPE_PARTIAL_LINE;
634
635                 ret = trace_seq_printf(s, " | ");
636                 if (!ret)
637                         return TRACE_TYPE_PARTIAL_LINE;
638         }
639
640         /* Overhead */
641         ret = print_graph_overhead(duration, s);
642         if (!ret)
643                 return TRACE_TYPE_PARTIAL_LINE;
644
645         /* Duration */
646         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
647                 ret = print_graph_duration(duration, s);
648                 if (ret == TRACE_TYPE_PARTIAL_LINE)
649                         return TRACE_TYPE_PARTIAL_LINE;
650         }
651
652         /* Closing brace */
653         for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
654                 ret = trace_seq_printf(s, " ");
655                 if (!ret)
656                         return TRACE_TYPE_PARTIAL_LINE;
657         }
658
659         ret = trace_seq_printf(s, "}\n");
660         if (!ret)
661                 return TRACE_TYPE_PARTIAL_LINE;
662
663         /* Overrun */
664         if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
665                 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
666                                         trace->overrun);
667                 if (!ret)
668                         return TRACE_TYPE_PARTIAL_LINE;
669         }
670
671         ret = print_graph_irq(s, trace->func, TRACE_GRAPH_RET, cpu, ent->pid);
672         if (ret == TRACE_TYPE_PARTIAL_LINE)
673                 return TRACE_TYPE_PARTIAL_LINE;
674
675         return TRACE_TYPE_HANDLED;
676 }
677
678 static enum print_line_t
679 print_graph_comment(struct print_entry *trace, struct trace_seq *s,
680                    struct trace_entry *ent, struct trace_iterator *iter)
681 {
682         int i;
683         int ret;
684         int cpu = iter->cpu;
685         pid_t *last_pid = iter->private;
686
687         /* Absolute time */
688         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
689                 ret = print_graph_abs_time(iter->ts, s);
690                 if (!ret)
691                         return TRACE_TYPE_PARTIAL_LINE;
692         }
693
694         /* Pid */
695         if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
696                 return TRACE_TYPE_PARTIAL_LINE;
697
698         /* Cpu */
699         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
700                 ret = print_graph_cpu(s, cpu);
701                 if (ret == TRACE_TYPE_PARTIAL_LINE)
702                         return TRACE_TYPE_PARTIAL_LINE;
703         }
704
705         /* Proc */
706         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
707                 ret = print_graph_proc(s, ent->pid);
708                 if (ret == TRACE_TYPE_PARTIAL_LINE)
709                         return TRACE_TYPE_PARTIAL_LINE;
710
711                 ret = trace_seq_printf(s, " | ");
712                 if (!ret)
713                         return TRACE_TYPE_PARTIAL_LINE;
714         }
715
716         /* No overhead */
717         ret = print_graph_overhead(-1, s);
718         if (!ret)
719                 return TRACE_TYPE_PARTIAL_LINE;
720
721         /* No time */
722         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
723                 ret = trace_seq_printf(s, "            |  ");
724                 if (!ret)
725                         return TRACE_TYPE_PARTIAL_LINE;
726         }
727
728         /* Indentation */
729         if (trace->depth > 0)
730                 for (i = 0; i < (trace->depth + 1) * TRACE_GRAPH_INDENT; i++) {
731                         ret = trace_seq_printf(s, " ");
732                         if (!ret)
733                                 return TRACE_TYPE_PARTIAL_LINE;
734                 }
735
736         /* The comment */
737         ret = trace_seq_printf(s, "/* %s", trace->buf);
738         if (!ret)
739                 return TRACE_TYPE_PARTIAL_LINE;
740
741         /* Strip ending newline */
742         if (s->buffer[s->len - 1] == '\n') {
743                 s->buffer[s->len - 1] = '\0';
744                 s->len--;
745         }
746
747         ret = trace_seq_printf(s, " */\n");
748         if (!ret)
749                 return TRACE_TYPE_PARTIAL_LINE;
750
751         return TRACE_TYPE_HANDLED;
752 }
753
754
755 enum print_line_t
756 print_graph_function(struct trace_iterator *iter)
757 {
758         struct trace_seq *s = &iter->seq;
759         struct trace_entry *entry = iter->ent;
760
761         switch (entry->type) {
762         case TRACE_GRAPH_ENT: {
763                 struct ftrace_graph_ent_entry *field;
764                 trace_assign_type(field, entry);
765                 return print_graph_entry(field, s, iter);
766         }
767         case TRACE_GRAPH_RET: {
768                 struct ftrace_graph_ret_entry *field;
769                 trace_assign_type(field, entry);
770                 return print_graph_return(&field->ret, s, entry, iter);
771         }
772         case TRACE_PRINT: {
773                 struct print_entry *field;
774                 trace_assign_type(field, entry);
775                 return print_graph_comment(field, s, entry, iter);
776         }
777         default:
778                 return TRACE_TYPE_UNHANDLED;
779         }
780 }
781
782 static void print_graph_headers(struct seq_file *s)
783 {
784         /* 1st line */
785         seq_printf(s, "# ");
786         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
787                 seq_printf(s, "     TIME       ");
788         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
789                 seq_printf(s, "CPU");
790         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
791                 seq_printf(s, "  TASK/PID      ");
792         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
793                 seq_printf(s, "  DURATION   ");
794         seq_printf(s, "               FUNCTION CALLS\n");
795
796         /* 2nd line */
797         seq_printf(s, "# ");
798         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
799                 seq_printf(s, "      |         ");
800         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
801                 seq_printf(s, "|  ");
802         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
803                 seq_printf(s, "  |    |        ");
804         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
805                 seq_printf(s, "   |   |      ");
806         seq_printf(s, "               |   |   |   |\n");
807 }
808
809 static void graph_trace_open(struct trace_iterator *iter)
810 {
811         /* pid on the last trace processed */
812         pid_t *last_pid = alloc_percpu(pid_t);
813         int cpu;
814
815         if (!last_pid)
816                 pr_warning("function graph tracer: not enough memory\n");
817         else
818                 for_each_possible_cpu(cpu) {
819                         pid_t *pid = per_cpu_ptr(last_pid, cpu);
820                         *pid = -1;
821                 }
822
823         iter->private = last_pid;
824 }
825
826 static void graph_trace_close(struct trace_iterator *iter)
827 {
828         percpu_free(iter->private);
829 }
830
831 static struct tracer graph_trace __read_mostly = {
832         .name           = "function_graph",
833         .open           = graph_trace_open,
834         .close          = graph_trace_close,
835         .wait_pipe      = poll_wait_pipe,
836         .init           = graph_trace_init,
837         .reset          = graph_trace_reset,
838         .print_line     = print_graph_function,
839         .print_header   = print_graph_headers,
840         .flags          = &tracer_flags,
841 #ifdef CONFIG_FTRACE_SELFTEST
842         .selftest       = trace_selftest_startup_function_graph,
843 #endif
844 };
845
846 static __init int init_graph_trace(void)
847 {
848         return register_tracer(&graph_trace);
849 }
850
851 device_initcall(init_graph_trace);