tracing: Remove unnecessary variable in print_graph_return
[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_cpu_data {
18         pid_t           last_pid;
19         int             depth;
20         int             ignore;
21 };
22
23 struct fgraph_data {
24         struct fgraph_cpu_data          *cpu_data;
25
26         /* Place to preserve last processed entry. */
27         struct ftrace_graph_ent_entry   ent;
28         struct ftrace_graph_ret_entry   ret;
29         int                             failed;
30         int                             cpu;
31 };
32
33 #define TRACE_GRAPH_INDENT      2
34
35 /* Flag options */
36 #define TRACE_GRAPH_PRINT_OVERRUN       0x1
37 #define TRACE_GRAPH_PRINT_CPU           0x2
38 #define TRACE_GRAPH_PRINT_OVERHEAD      0x4
39 #define TRACE_GRAPH_PRINT_PROC          0x8
40 #define TRACE_GRAPH_PRINT_DURATION      0x10
41 #define TRACE_GRAPH_PRINT_ABS_TIME      0X20
42
43 static struct tracer_opt trace_opts[] = {
44         /* Display overruns? (for self-debug purpose) */
45         { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
46         /* Display CPU ? */
47         { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
48         /* Display Overhead ? */
49         { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
50         /* Display proc name/pid */
51         { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
52         /* Display duration of execution */
53         { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
54         /* Display absolute time of an entry */
55         { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
56         { } /* Empty entry */
57 };
58
59 static struct tracer_flags tracer_flags = {
60         /* Don't display overruns and proc by default */
61         .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
62                TRACE_GRAPH_PRINT_DURATION,
63         .opts = trace_opts
64 };
65
66 static struct trace_array *graph_array;
67
68
69 /* Add a function return address to the trace stack on thread info.*/
70 int
71 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
72                          unsigned long frame_pointer)
73 {
74         unsigned long long calltime;
75         int index;
76
77         if (!current->ret_stack)
78                 return -EBUSY;
79
80         /*
81          * We must make sure the ret_stack is tested before we read
82          * anything else.
83          */
84         smp_rmb();
85
86         /* The return trace stack is full */
87         if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
88                 atomic_inc(&current->trace_overrun);
89                 return -EBUSY;
90         }
91
92         calltime = trace_clock_local();
93
94         index = ++current->curr_ret_stack;
95         barrier();
96         current->ret_stack[index].ret = ret;
97         current->ret_stack[index].func = func;
98         current->ret_stack[index].calltime = calltime;
99         current->ret_stack[index].subtime = 0;
100         current->ret_stack[index].fp = frame_pointer;
101         *depth = index;
102
103         return 0;
104 }
105
106 /* Retrieve a function return address to the trace stack on thread info.*/
107 static void
108 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
109                         unsigned long frame_pointer)
110 {
111         int index;
112
113         index = current->curr_ret_stack;
114
115         if (unlikely(index < 0)) {
116                 ftrace_graph_stop();
117                 WARN_ON(1);
118                 /* Might as well panic, otherwise we have no where to go */
119                 *ret = (unsigned long)panic;
120                 return;
121         }
122
123 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
124         /*
125          * The arch may choose to record the frame pointer used
126          * and check it here to make sure that it is what we expect it
127          * to be. If gcc does not set the place holder of the return
128          * address in the frame pointer, and does a copy instead, then
129          * the function graph trace will fail. This test detects this
130          * case.
131          *
132          * Currently, x86_32 with optimize for size (-Os) makes the latest
133          * gcc do the above.
134          */
135         if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
136                 ftrace_graph_stop();
137                 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
138                      "  from func %ps return to %lx\n",
139                      current->ret_stack[index].fp,
140                      frame_pointer,
141                      (void *)current->ret_stack[index].func,
142                      current->ret_stack[index].ret);
143                 *ret = (unsigned long)panic;
144                 return;
145         }
146 #endif
147
148         *ret = current->ret_stack[index].ret;
149         trace->func = current->ret_stack[index].func;
150         trace->calltime = current->ret_stack[index].calltime;
151         trace->overrun = atomic_read(&current->trace_overrun);
152         trace->depth = index;
153 }
154
155 /*
156  * Send the trace to the ring-buffer.
157  * @return the original return address.
158  */
159 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
160 {
161         struct ftrace_graph_ret trace;
162         unsigned long ret;
163
164         ftrace_pop_return_trace(&trace, &ret, frame_pointer);
165         trace.rettime = trace_clock_local();
166         ftrace_graph_return(&trace);
167         barrier();
168         current->curr_ret_stack--;
169
170         if (unlikely(!ret)) {
171                 ftrace_graph_stop();
172                 WARN_ON(1);
173                 /* Might as well panic. What else to do? */
174                 ret = (unsigned long)panic;
175         }
176
177         return ret;
178 }
179
180 static int __trace_graph_entry(struct trace_array *tr,
181                                 struct ftrace_graph_ent *trace,
182                                 unsigned long flags,
183                                 int pc)
184 {
185         struct ftrace_event_call *call = &event_funcgraph_entry;
186         struct ring_buffer_event *event;
187         struct ring_buffer *buffer = tr->buffer;
188         struct ftrace_graph_ent_entry *entry;
189
190         if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
191                 return 0;
192
193         event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
194                                           sizeof(*entry), flags, pc);
195         if (!event)
196                 return 0;
197         entry   = ring_buffer_event_data(event);
198         entry->graph_ent                        = *trace;
199         if (!filter_current_check_discard(buffer, call, entry, event))
200                 ring_buffer_unlock_commit(buffer, event);
201
202         return 1;
203 }
204
205 int trace_graph_entry(struct ftrace_graph_ent *trace)
206 {
207         struct trace_array *tr = graph_array;
208         struct trace_array_cpu *data;
209         unsigned long flags;
210         long disabled;
211         int ret;
212         int cpu;
213         int pc;
214
215         if (!ftrace_trace_task(current))
216                 return 0;
217
218         /* trace it when it is-nested-in or is a function enabled. */
219         if (!(trace->depth || ftrace_graph_addr(trace->func)))
220                 return 0;
221
222         local_irq_save(flags);
223         cpu = raw_smp_processor_id();
224         data = tr->data[cpu];
225         disabled = atomic_inc_return(&data->disabled);
226         if (likely(disabled == 1)) {
227                 pc = preempt_count();
228                 ret = __trace_graph_entry(tr, trace, flags, pc);
229         } else {
230                 ret = 0;
231         }
232
233         atomic_dec(&data->disabled);
234         local_irq_restore(flags);
235
236         return ret;
237 }
238
239 static void __trace_graph_return(struct trace_array *tr,
240                                 struct ftrace_graph_ret *trace,
241                                 unsigned long flags,
242                                 int pc)
243 {
244         struct ftrace_event_call *call = &event_funcgraph_exit;
245         struct ring_buffer_event *event;
246         struct ring_buffer *buffer = tr->buffer;
247         struct ftrace_graph_ret_entry *entry;
248
249         if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
250                 return;
251
252         event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
253                                           sizeof(*entry), flags, pc);
254         if (!event)
255                 return;
256         entry   = ring_buffer_event_data(event);
257         entry->ret                              = *trace;
258         if (!filter_current_check_discard(buffer, call, entry, event))
259                 ring_buffer_unlock_commit(buffer, event);
260 }
261
262 void trace_graph_return(struct ftrace_graph_ret *trace)
263 {
264         struct trace_array *tr = graph_array;
265         struct trace_array_cpu *data;
266         unsigned long flags;
267         long disabled;
268         int cpu;
269         int pc;
270
271         local_irq_save(flags);
272         cpu = raw_smp_processor_id();
273         data = tr->data[cpu];
274         disabled = atomic_inc_return(&data->disabled);
275         if (likely(disabled == 1)) {
276                 pc = preempt_count();
277                 __trace_graph_return(tr, trace, flags, pc);
278         }
279         atomic_dec(&data->disabled);
280         local_irq_restore(flags);
281 }
282
283 void set_graph_array(struct trace_array *tr)
284 {
285         graph_array = tr;
286
287         /* Make graph_array visible before we start tracing */
288
289         smp_mb();
290 }
291
292 static int graph_trace_init(struct trace_array *tr)
293 {
294         int ret;
295
296         set_graph_array(tr);
297         ret = register_ftrace_graph(&trace_graph_return,
298                                     &trace_graph_entry);
299         if (ret)
300                 return ret;
301         tracing_start_cmdline_record();
302
303         return 0;
304 }
305
306 static void graph_trace_reset(struct trace_array *tr)
307 {
308         tracing_stop_cmdline_record();
309         unregister_ftrace_graph();
310 }
311
312 static int max_bytes_for_cpu;
313
314 static enum print_line_t
315 print_graph_cpu(struct trace_seq *s, int cpu)
316 {
317         int ret;
318
319         /*
320          * Start with a space character - to make it stand out
321          * to the right a bit when trace output is pasted into
322          * email:
323          */
324         ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
325         if (!ret)
326                 return TRACE_TYPE_PARTIAL_LINE;
327
328         return TRACE_TYPE_HANDLED;
329 }
330
331 #define TRACE_GRAPH_PROCINFO_LENGTH     14
332
333 static enum print_line_t
334 print_graph_proc(struct trace_seq *s, pid_t pid)
335 {
336         char comm[TASK_COMM_LEN];
337         /* sign + log10(MAX_INT) + '\0' */
338         char pid_str[11];
339         int spaces = 0;
340         int ret;
341         int len;
342         int i;
343
344         trace_find_cmdline(pid, comm);
345         comm[7] = '\0';
346         sprintf(pid_str, "%d", pid);
347
348         /* 1 stands for the "-" character */
349         len = strlen(comm) + strlen(pid_str) + 1;
350
351         if (len < TRACE_GRAPH_PROCINFO_LENGTH)
352                 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
353
354         /* First spaces to align center */
355         for (i = 0; i < spaces / 2; i++) {
356                 ret = trace_seq_printf(s, " ");
357                 if (!ret)
358                         return TRACE_TYPE_PARTIAL_LINE;
359         }
360
361         ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
362         if (!ret)
363                 return TRACE_TYPE_PARTIAL_LINE;
364
365         /* Last spaces to align center */
366         for (i = 0; i < spaces - (spaces / 2); i++) {
367                 ret = trace_seq_printf(s, " ");
368                 if (!ret)
369                         return TRACE_TYPE_PARTIAL_LINE;
370         }
371         return TRACE_TYPE_HANDLED;
372 }
373
374
375 static enum print_line_t
376 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
377 {
378         if (!trace_seq_putc(s, ' '))
379                 return 0;
380
381         return trace_print_lat_fmt(s, entry);
382 }
383
384 /* If the pid changed since the last trace, output this event */
385 static enum print_line_t
386 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
387 {
388         pid_t prev_pid;
389         pid_t *last_pid;
390         int ret;
391
392         if (!data)
393                 return TRACE_TYPE_HANDLED;
394
395         last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
396
397         if (*last_pid == pid)
398                 return TRACE_TYPE_HANDLED;
399
400         prev_pid = *last_pid;
401         *last_pid = pid;
402
403         if (prev_pid == -1)
404                 return TRACE_TYPE_HANDLED;
405 /*
406  * Context-switch trace line:
407
408  ------------------------------------------
409  | 1)  migration/0--1  =>  sshd-1755
410  ------------------------------------------
411
412  */
413         ret = trace_seq_printf(s,
414                 " ------------------------------------------\n");
415         if (!ret)
416                 return TRACE_TYPE_PARTIAL_LINE;
417
418         ret = print_graph_cpu(s, cpu);
419         if (ret == TRACE_TYPE_PARTIAL_LINE)
420                 return TRACE_TYPE_PARTIAL_LINE;
421
422         ret = print_graph_proc(s, prev_pid);
423         if (ret == TRACE_TYPE_PARTIAL_LINE)
424                 return TRACE_TYPE_PARTIAL_LINE;
425
426         ret = trace_seq_printf(s, " => ");
427         if (!ret)
428                 return TRACE_TYPE_PARTIAL_LINE;
429
430         ret = print_graph_proc(s, pid);
431         if (ret == TRACE_TYPE_PARTIAL_LINE)
432                 return TRACE_TYPE_PARTIAL_LINE;
433
434         ret = trace_seq_printf(s,
435                 "\n ------------------------------------------\n\n");
436         if (!ret)
437                 return TRACE_TYPE_PARTIAL_LINE;
438
439         return TRACE_TYPE_HANDLED;
440 }
441
442 static struct ftrace_graph_ret_entry *
443 get_return_for_leaf(struct trace_iterator *iter,
444                 struct ftrace_graph_ent_entry *curr)
445 {
446         struct fgraph_data *data = iter->private;
447         struct ring_buffer_iter *ring_iter = NULL;
448         struct ring_buffer_event *event;
449         struct ftrace_graph_ret_entry *next;
450
451         /*
452          * If the previous output failed to write to the seq buffer,
453          * then we just reuse the data from before.
454          */
455         if (data && data->failed) {
456                 curr = &data->ent;
457                 next = &data->ret;
458         } else {
459
460                 ring_iter = iter->buffer_iter[iter->cpu];
461
462                 /* First peek to compare current entry and the next one */
463                 if (ring_iter)
464                         event = ring_buffer_iter_peek(ring_iter, NULL);
465                 else {
466                         /*
467                          * We need to consume the current entry to see
468                          * the next one.
469                          */
470                         ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
471                         event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
472                                                  NULL);
473                 }
474
475                 if (!event)
476                         return NULL;
477
478                 next = ring_buffer_event_data(event);
479
480                 if (data) {
481                         /*
482                          * Save current and next entries for later reference
483                          * if the output fails.
484                          */
485                         data->ent = *curr;
486                         data->ret = *next;
487                 }
488         }
489
490         if (next->ent.type != TRACE_GRAPH_RET)
491                 return NULL;
492
493         if (curr->ent.pid != next->ent.pid ||
494                         curr->graph_ent.func != next->ret.func)
495                 return NULL;
496
497         /* this is a leaf, now advance the iterator */
498         if (ring_iter)
499                 ring_buffer_read(ring_iter, NULL);
500
501         return next;
502 }
503
504 /* Signal a overhead of time execution to the output */
505 static int
506 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
507 {
508         /* If duration disappear, we don't need anything */
509         if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
510                 return 1;
511
512         /* Non nested entry or return */
513         if (duration == -1)
514                 return trace_seq_printf(s, "  ");
515
516         if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
517                 /* Duration exceeded 100 msecs */
518                 if (duration > 100000ULL)
519                         return trace_seq_printf(s, "! ");
520
521                 /* Duration exceeded 10 msecs */
522                 if (duration > 10000ULL)
523                         return trace_seq_printf(s, "+ ");
524         }
525
526         return trace_seq_printf(s, "  ");
527 }
528
529 static int print_graph_abs_time(u64 t, struct trace_seq *s)
530 {
531         unsigned long usecs_rem;
532
533         usecs_rem = do_div(t, NSEC_PER_SEC);
534         usecs_rem /= 1000;
535
536         return trace_seq_printf(s, "%5lu.%06lu |  ",
537                         (unsigned long)t, usecs_rem);
538 }
539
540 static enum print_line_t
541 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
542                 enum trace_type type, int cpu, pid_t pid)
543 {
544         int ret;
545         struct trace_seq *s = &iter->seq;
546
547         if (addr < (unsigned long)__irqentry_text_start ||
548                 addr >= (unsigned long)__irqentry_text_end)
549                 return TRACE_TYPE_UNHANDLED;
550
551         /* Absolute time */
552         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
553                 ret = print_graph_abs_time(iter->ts, s);
554                 if (!ret)
555                         return TRACE_TYPE_PARTIAL_LINE;
556         }
557
558         /* Cpu */
559         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
560                 ret = print_graph_cpu(s, cpu);
561                 if (ret == TRACE_TYPE_PARTIAL_LINE)
562                         return TRACE_TYPE_PARTIAL_LINE;
563         }
564
565         /* Proc */
566         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
567                 ret = print_graph_proc(s, pid);
568                 if (ret == TRACE_TYPE_PARTIAL_LINE)
569                         return TRACE_TYPE_PARTIAL_LINE;
570                 ret = trace_seq_printf(s, " | ");
571                 if (!ret)
572                         return TRACE_TYPE_PARTIAL_LINE;
573         }
574
575         /* No overhead */
576         ret = print_graph_overhead(-1, s);
577         if (!ret)
578                 return TRACE_TYPE_PARTIAL_LINE;
579
580         if (type == TRACE_GRAPH_ENT)
581                 ret = trace_seq_printf(s, "==========>");
582         else
583                 ret = trace_seq_printf(s, "<==========");
584
585         if (!ret)
586                 return TRACE_TYPE_PARTIAL_LINE;
587
588         /* Don't close the duration column if haven't one */
589         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
590                 trace_seq_printf(s, " |");
591         ret = trace_seq_printf(s, "\n");
592
593         if (!ret)
594                 return TRACE_TYPE_PARTIAL_LINE;
595         return TRACE_TYPE_HANDLED;
596 }
597
598 enum print_line_t
599 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
600 {
601         unsigned long nsecs_rem = do_div(duration, 1000);
602         /* log10(ULONG_MAX) + '\0' */
603         char msecs_str[21];
604         char nsecs_str[5];
605         int ret, len;
606         int i;
607
608         sprintf(msecs_str, "%lu", (unsigned long) duration);
609
610         /* Print msecs */
611         ret = trace_seq_printf(s, "%s", msecs_str);
612         if (!ret)
613                 return TRACE_TYPE_PARTIAL_LINE;
614
615         len = strlen(msecs_str);
616
617         /* Print nsecs (we don't want to exceed 7 numbers) */
618         if (len < 7) {
619                 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
620                 ret = trace_seq_printf(s, ".%s", nsecs_str);
621                 if (!ret)
622                         return TRACE_TYPE_PARTIAL_LINE;
623                 len += strlen(nsecs_str);
624         }
625
626         ret = trace_seq_printf(s, " us ");
627         if (!ret)
628                 return TRACE_TYPE_PARTIAL_LINE;
629
630         /* Print remaining spaces to fit the row's width */
631         for (i = len; i < 7; i++) {
632                 ret = trace_seq_printf(s, " ");
633                 if (!ret)
634                         return TRACE_TYPE_PARTIAL_LINE;
635         }
636         return TRACE_TYPE_HANDLED;
637 }
638
639 static enum print_line_t
640 print_graph_duration(unsigned long long duration, struct trace_seq *s)
641 {
642         int ret;
643
644         ret = trace_print_graph_duration(duration, s);
645         if (ret != TRACE_TYPE_HANDLED)
646                 return ret;
647
648         ret = trace_seq_printf(s, "|  ");
649         if (!ret)
650                 return TRACE_TYPE_PARTIAL_LINE;
651
652         return TRACE_TYPE_HANDLED;
653 }
654
655 /* Case of a leaf function on its call entry */
656 static enum print_line_t
657 print_graph_entry_leaf(struct trace_iterator *iter,
658                 struct ftrace_graph_ent_entry *entry,
659                 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
660 {
661         struct fgraph_data *data = iter->private;
662         struct ftrace_graph_ret *graph_ret;
663         struct ftrace_graph_ent *call;
664         unsigned long long duration;
665         int ret;
666         int i;
667
668         graph_ret = &ret_entry->ret;
669         call = &entry->graph_ent;
670         duration = graph_ret->rettime - graph_ret->calltime;
671
672         if (data) {
673                 int cpu = iter->cpu;
674                 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
675
676                 /*
677                  * Comments display at + 1 to depth. Since
678                  * this is a leaf function, keep the comments
679                  * equal to this depth.
680                  */
681                 *depth = call->depth - 1;
682         }
683
684         /* Overhead */
685         ret = print_graph_overhead(duration, s);
686         if (!ret)
687                 return TRACE_TYPE_PARTIAL_LINE;
688
689         /* Duration */
690         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
691                 ret = print_graph_duration(duration, s);
692                 if (ret == TRACE_TYPE_PARTIAL_LINE)
693                         return TRACE_TYPE_PARTIAL_LINE;
694         }
695
696         /* Function */
697         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
698                 ret = trace_seq_printf(s, " ");
699                 if (!ret)
700                         return TRACE_TYPE_PARTIAL_LINE;
701         }
702
703         ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
704         if (!ret)
705                 return TRACE_TYPE_PARTIAL_LINE;
706
707         return TRACE_TYPE_HANDLED;
708 }
709
710 static enum print_line_t
711 print_graph_entry_nested(struct trace_iterator *iter,
712                          struct ftrace_graph_ent_entry *entry,
713                          struct trace_seq *s, int cpu)
714 {
715         struct ftrace_graph_ent *call = &entry->graph_ent;
716         struct fgraph_data *data = iter->private;
717         int ret;
718         int i;
719
720         if (data) {
721                 int cpu = iter->cpu;
722                 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
723
724                 *depth = call->depth;
725         }
726
727         /* No overhead */
728         ret = print_graph_overhead(-1, s);
729         if (!ret)
730                 return TRACE_TYPE_PARTIAL_LINE;
731
732         /* No time */
733         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
734                 ret = trace_seq_printf(s, "            |  ");
735                 if (!ret)
736                         return TRACE_TYPE_PARTIAL_LINE;
737         }
738
739         /* Function */
740         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
741                 ret = trace_seq_printf(s, " ");
742                 if (!ret)
743                         return TRACE_TYPE_PARTIAL_LINE;
744         }
745
746         ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
747         if (!ret)
748                 return TRACE_TYPE_PARTIAL_LINE;
749
750         /*
751          * we already consumed the current entry to check the next one
752          * and see if this is a leaf.
753          */
754         return TRACE_TYPE_NO_CONSUME;
755 }
756
757 static enum print_line_t
758 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
759                      int type, unsigned long addr)
760 {
761         struct fgraph_data *data = iter->private;
762         struct trace_entry *ent = iter->ent;
763         int cpu = iter->cpu;
764         int ret;
765
766         /* Pid */
767         if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
768                 return TRACE_TYPE_PARTIAL_LINE;
769
770         if (type) {
771                 /* Interrupt */
772                 ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
773                 if (ret == TRACE_TYPE_PARTIAL_LINE)
774                         return TRACE_TYPE_PARTIAL_LINE;
775         }
776
777         /* Absolute time */
778         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
779                 ret = print_graph_abs_time(iter->ts, s);
780                 if (!ret)
781                         return TRACE_TYPE_PARTIAL_LINE;
782         }
783
784         /* Cpu */
785         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
786                 ret = print_graph_cpu(s, cpu);
787                 if (ret == TRACE_TYPE_PARTIAL_LINE)
788                         return TRACE_TYPE_PARTIAL_LINE;
789         }
790
791         /* Proc */
792         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
793                 ret = print_graph_proc(s, ent->pid);
794                 if (ret == TRACE_TYPE_PARTIAL_LINE)
795                         return TRACE_TYPE_PARTIAL_LINE;
796
797                 ret = trace_seq_printf(s, " | ");
798                 if (!ret)
799                         return TRACE_TYPE_PARTIAL_LINE;
800         }
801
802         /* Latency format */
803         if (trace_flags & TRACE_ITER_LATENCY_FMT) {
804                 ret = print_graph_lat_fmt(s, ent);
805                 if (ret == TRACE_TYPE_PARTIAL_LINE)
806                         return TRACE_TYPE_PARTIAL_LINE;
807         }
808
809         return 0;
810 }
811
812 static enum print_line_t
813 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
814                         struct trace_iterator *iter)
815 {
816         struct fgraph_data *data = iter->private;
817         struct ftrace_graph_ent *call = &field->graph_ent;
818         struct ftrace_graph_ret_entry *leaf_ret;
819         static enum print_line_t ret;
820         int cpu = iter->cpu;
821
822         if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
823                 return TRACE_TYPE_PARTIAL_LINE;
824
825         leaf_ret = get_return_for_leaf(iter, field);
826         if (leaf_ret)
827                 ret = print_graph_entry_leaf(iter, field, leaf_ret, s);
828         else
829                 ret = print_graph_entry_nested(iter, field, s, cpu);
830
831         if (data) {
832                 /*
833                  * If we failed to write our output, then we need to make
834                  * note of it. Because we already consumed our entry.
835                  */
836                 if (s->full) {
837                         data->failed = 1;
838                         data->cpu = cpu;
839                 } else
840                         data->failed = 0;
841         }
842
843         return ret;
844 }
845
846 static enum print_line_t
847 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
848                    struct trace_entry *ent, struct trace_iterator *iter)
849 {
850         unsigned long long duration = trace->rettime - trace->calltime;
851         struct fgraph_data *data = iter->private;
852         pid_t pid = ent->pid;
853         int cpu = iter->cpu;
854         int ret;
855         int i;
856
857         if (data) {
858                 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
859
860                 /*
861                  * Comments display at + 1 to depth. This is the
862                  * return from a function, we now want the comments
863                  * to display at the same level of the bracket.
864                  */
865                 *depth = trace->depth - 1;
866         }
867
868         if (print_graph_prologue(iter, s, 0, 0))
869                 return TRACE_TYPE_PARTIAL_LINE;
870
871         /* Overhead */
872         ret = print_graph_overhead(duration, s);
873         if (!ret)
874                 return TRACE_TYPE_PARTIAL_LINE;
875
876         /* Duration */
877         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
878                 ret = print_graph_duration(duration, s);
879                 if (ret == TRACE_TYPE_PARTIAL_LINE)
880                         return TRACE_TYPE_PARTIAL_LINE;
881         }
882
883         /* Closing brace */
884         for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
885                 ret = trace_seq_printf(s, " ");
886                 if (!ret)
887                         return TRACE_TYPE_PARTIAL_LINE;
888         }
889
890         ret = trace_seq_printf(s, "}\n");
891         if (!ret)
892                 return TRACE_TYPE_PARTIAL_LINE;
893
894         /* Overrun */
895         if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
896                 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
897                                         trace->overrun);
898                 if (!ret)
899                         return TRACE_TYPE_PARTIAL_LINE;
900         }
901
902         ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
903         if (ret == TRACE_TYPE_PARTIAL_LINE)
904                 return TRACE_TYPE_PARTIAL_LINE;
905
906         return TRACE_TYPE_HANDLED;
907 }
908
909 static enum print_line_t
910 print_graph_comment(struct trace_seq *s,  struct trace_entry *ent,
911                     struct trace_iterator *iter)
912 {
913         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
914         struct fgraph_data *data = iter->private;
915         struct trace_event *event;
916         int depth = 0;
917         int ret;
918         int i;
919
920         if (data)
921                 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
922
923         if (print_graph_prologue(iter, s, 0, 0))
924                 return TRACE_TYPE_PARTIAL_LINE;
925
926         /* No overhead */
927         ret = print_graph_overhead(-1, s);
928         if (!ret)
929                 return TRACE_TYPE_PARTIAL_LINE;
930
931         /* No time */
932         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
933                 ret = trace_seq_printf(s, "            |  ");
934                 if (!ret)
935                         return TRACE_TYPE_PARTIAL_LINE;
936         }
937
938         /* Indentation */
939         if (depth > 0)
940                 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
941                         ret = trace_seq_printf(s, " ");
942                         if (!ret)
943                                 return TRACE_TYPE_PARTIAL_LINE;
944                 }
945
946         /* The comment */
947         ret = trace_seq_printf(s, "/* ");
948         if (!ret)
949                 return TRACE_TYPE_PARTIAL_LINE;
950
951         switch (iter->ent->type) {
952         case TRACE_BPRINT:
953                 ret = trace_print_bprintk_msg_only(iter);
954                 if (ret != TRACE_TYPE_HANDLED)
955                         return ret;
956                 break;
957         case TRACE_PRINT:
958                 ret = trace_print_printk_msg_only(iter);
959                 if (ret != TRACE_TYPE_HANDLED)
960                         return ret;
961                 break;
962         default:
963                 event = ftrace_find_event(ent->type);
964                 if (!event)
965                         return TRACE_TYPE_UNHANDLED;
966
967                 ret = event->trace(iter, sym_flags);
968                 if (ret != TRACE_TYPE_HANDLED)
969                         return ret;
970         }
971
972         /* Strip ending newline */
973         if (s->buffer[s->len - 1] == '\n') {
974                 s->buffer[s->len - 1] = '\0';
975                 s->len--;
976         }
977
978         ret = trace_seq_printf(s, " */\n");
979         if (!ret)
980                 return TRACE_TYPE_PARTIAL_LINE;
981
982         return TRACE_TYPE_HANDLED;
983 }
984
985
986 enum print_line_t
987 print_graph_function(struct trace_iterator *iter)
988 {
989         struct ftrace_graph_ent_entry *field;
990         struct fgraph_data *data = iter->private;
991         struct trace_entry *entry = iter->ent;
992         struct trace_seq *s = &iter->seq;
993         int cpu = iter->cpu;
994         int ret;
995
996         if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
997                 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
998                 return TRACE_TYPE_HANDLED;
999         }
1000
1001         /*
1002          * If the last output failed, there's a possibility we need
1003          * to print out the missing entry which would never go out.
1004          */
1005         if (data && data->failed) {
1006                 field = &data->ent;
1007                 iter->cpu = data->cpu;
1008                 ret = print_graph_entry(field, s, iter);
1009                 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1010                         per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1011                         ret = TRACE_TYPE_NO_CONSUME;
1012                 }
1013                 iter->cpu = cpu;
1014                 return ret;
1015         }
1016
1017         switch (entry->type) {
1018         case TRACE_GRAPH_ENT: {
1019                 /*
1020                  * print_graph_entry() may consume the current event,
1021                  * thus @field may become invalid, so we need to save it.
1022                  * sizeof(struct ftrace_graph_ent_entry) is very small,
1023                  * it can be safely saved at the stack.
1024                  */
1025                 struct ftrace_graph_ent_entry saved;
1026                 trace_assign_type(field, entry);
1027                 saved = *field;
1028                 return print_graph_entry(&saved, s, iter);
1029         }
1030         case TRACE_GRAPH_RET: {
1031                 struct ftrace_graph_ret_entry *field;
1032                 trace_assign_type(field, entry);
1033                 return print_graph_return(&field->ret, s, entry, iter);
1034         }
1035         default:
1036                 return print_graph_comment(s, entry, iter);
1037         }
1038
1039         return TRACE_TYPE_HANDLED;
1040 }
1041
1042 static void print_lat_header(struct seq_file *s)
1043 {
1044         static const char spaces[] = "                " /* 16 spaces */
1045                 "    "                                  /* 4 spaces */
1046                 "                 ";                    /* 17 spaces */
1047         int size = 0;
1048
1049         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1050                 size += 16;
1051         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1052                 size += 4;
1053         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1054                 size += 17;
1055
1056         seq_printf(s, "#%.*s  _-----=> irqs-off        \n", size, spaces);
1057         seq_printf(s, "#%.*s / _----=> need-resched    \n", size, spaces);
1058         seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1059         seq_printf(s, "#%.*s|| / _--=> preempt-depth   \n", size, spaces);
1060         seq_printf(s, "#%.*s||| / _-=> lock-depth      \n", size, spaces);
1061         seq_printf(s, "#%.*s|||| /                     \n", size, spaces);
1062 }
1063
1064 static void print_graph_headers(struct seq_file *s)
1065 {
1066         int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1067
1068         if (lat)
1069                 print_lat_header(s);
1070
1071         /* 1st line */
1072         seq_printf(s, "#");
1073         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1074                 seq_printf(s, "     TIME       ");
1075         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1076                 seq_printf(s, " CPU");
1077         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1078                 seq_printf(s, "  TASK/PID       ");
1079         if (lat)
1080                 seq_printf(s, "|||||");
1081         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1082                 seq_printf(s, "  DURATION   ");
1083         seq_printf(s, "               FUNCTION CALLS\n");
1084
1085         /* 2nd line */
1086         seq_printf(s, "#");
1087         if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1088                 seq_printf(s, "      |         ");
1089         if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1090                 seq_printf(s, " |  ");
1091         if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1092                 seq_printf(s, "   |    |        ");
1093         if (lat)
1094                 seq_printf(s, "|||||");
1095         if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1096                 seq_printf(s, "   |   |      ");
1097         seq_printf(s, "               |   |   |   |\n");
1098 }
1099
1100 static void graph_trace_open(struct trace_iterator *iter)
1101 {
1102         /* pid and depth on the last trace processed */
1103         struct fgraph_data *data;
1104         int cpu;
1105
1106         iter->private = NULL;
1107
1108         data = kzalloc(sizeof(*data), GFP_KERNEL);
1109         if (!data)
1110                 goto out_err;
1111
1112         data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1113         if (!data->cpu_data)
1114                 goto out_err_free;
1115
1116         for_each_possible_cpu(cpu) {
1117                 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1118                 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1119                 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1120                 *pid = -1;
1121                 *depth = 0;
1122                 *ignore = 0;
1123         }
1124
1125         iter->private = data;
1126
1127         return;
1128
1129  out_err_free:
1130         kfree(data);
1131  out_err:
1132         pr_warning("function graph tracer: not enough memory\n");
1133 }
1134
1135 static void graph_trace_close(struct trace_iterator *iter)
1136 {
1137         struct fgraph_data *data = iter->private;
1138
1139         if (data) {
1140                 free_percpu(data->cpu_data);
1141                 kfree(data);
1142         }
1143 }
1144
1145 static struct tracer graph_trace __read_mostly = {
1146         .name           = "function_graph",
1147         .open           = graph_trace_open,
1148         .pipe_open      = graph_trace_open,
1149         .close          = graph_trace_close,
1150         .pipe_close     = graph_trace_close,
1151         .wait_pipe      = poll_wait_pipe,
1152         .init           = graph_trace_init,
1153         .reset          = graph_trace_reset,
1154         .print_line     = print_graph_function,
1155         .print_header   = print_graph_headers,
1156         .flags          = &tracer_flags,
1157 #ifdef CONFIG_FTRACE_SELFTEST
1158         .selftest       = trace_selftest_startup_function_graph,
1159 #endif
1160 };
1161
1162 static __init int init_graph_trace(void)
1163 {
1164         max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1165
1166         return register_tracer(&graph_trace);
1167 }
1168
1169 device_initcall(init_graph_trace);