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