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