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