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