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