]> Pileus Git - ~andy/linux/blob - kernel/trace/trace_functions_graph.c
Merge branch 'perf/urgent' into perf/core
[~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                 snprintf(nsecs_str, min(sizeof(nsecs_str), 8UL - len), "%03lu",
669                          nsecs_rem);
670                 ret = trace_seq_printf(s, ".%s", nsecs_str);
671                 if (!ret)
672                         return TRACE_TYPE_PARTIAL_LINE;
673                 len += strlen(nsecs_str);
674         }
675
676         ret = trace_seq_printf(s, " us ");
677         if (!ret)
678                 return TRACE_TYPE_PARTIAL_LINE;
679
680         /* Print remaining spaces to fit the row's width */
681         for (i = len; i < 7; i++) {
682                 ret = trace_seq_printf(s, " ");
683                 if (!ret)
684                         return TRACE_TYPE_PARTIAL_LINE;
685         }
686         return TRACE_TYPE_HANDLED;
687 }
688
689 static enum print_line_t
690 print_graph_duration(unsigned long long duration, struct trace_seq *s)
691 {
692         int ret;
693
694         ret = trace_print_graph_duration(duration, s);
695         if (ret != TRACE_TYPE_HANDLED)
696                 return ret;
697
698         ret = trace_seq_printf(s, "|  ");
699         if (!ret)
700                 return TRACE_TYPE_PARTIAL_LINE;
701
702         return TRACE_TYPE_HANDLED;
703 }
704
705 /* Case of a leaf function on its call entry */
706 static enum print_line_t
707 print_graph_entry_leaf(struct trace_iterator *iter,
708                 struct ftrace_graph_ent_entry *entry,
709                 struct ftrace_graph_ret_entry *ret_entry,
710                 struct trace_seq *s, u32 flags)
711 {
712         struct fgraph_data *data = iter->private;
713         struct ftrace_graph_ret *graph_ret;
714         struct ftrace_graph_ent *call;
715         unsigned long long duration;
716         int ret;
717         int i;
718
719         graph_ret = &ret_entry->ret;
720         call = &entry->graph_ent;
721         duration = graph_ret->rettime - graph_ret->calltime;
722
723         if (data) {
724                 struct fgraph_cpu_data *cpu_data;
725                 int cpu = iter->cpu;
726
727                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
728
729                 /*
730                  * Comments display at + 1 to depth. Since
731                  * this is a leaf function, keep the comments
732                  * equal to this depth.
733                  */
734                 cpu_data->depth = call->depth - 1;
735
736                 /* No need to keep this function around for this depth */
737                 if (call->depth < FTRACE_RETFUNC_DEPTH)
738                         cpu_data->enter_funcs[call->depth] = 0;
739         }
740
741         /* Overhead */
742         ret = print_graph_overhead(duration, s, flags);
743         if (!ret)
744                 return TRACE_TYPE_PARTIAL_LINE;
745
746         /* Duration */
747         if (flags & TRACE_GRAPH_PRINT_DURATION) {
748                 ret = print_graph_duration(duration, s);
749                 if (ret == TRACE_TYPE_PARTIAL_LINE)
750                         return TRACE_TYPE_PARTIAL_LINE;
751         }
752
753         /* Function */
754         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
755                 ret = trace_seq_printf(s, " ");
756                 if (!ret)
757                         return TRACE_TYPE_PARTIAL_LINE;
758         }
759
760         ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
761         if (!ret)
762                 return TRACE_TYPE_PARTIAL_LINE;
763
764         return TRACE_TYPE_HANDLED;
765 }
766
767 static enum print_line_t
768 print_graph_entry_nested(struct trace_iterator *iter,
769                          struct ftrace_graph_ent_entry *entry,
770                          struct trace_seq *s, int cpu, u32 flags)
771 {
772         struct ftrace_graph_ent *call = &entry->graph_ent;
773         struct fgraph_data *data = iter->private;
774         int ret;
775         int i;
776
777         if (data) {
778                 struct fgraph_cpu_data *cpu_data;
779                 int cpu = iter->cpu;
780
781                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
782                 cpu_data->depth = call->depth;
783
784                 /* Save this function pointer to see if the exit matches */
785                 if (call->depth < FTRACE_RETFUNC_DEPTH)
786                         cpu_data->enter_funcs[call->depth] = call->func;
787         }
788
789         /* No overhead */
790         ret = print_graph_overhead(-1, s, flags);
791         if (!ret)
792                 return TRACE_TYPE_PARTIAL_LINE;
793
794         /* No time */
795         if (flags & TRACE_GRAPH_PRINT_DURATION) {
796                 ret = trace_seq_printf(s, "            |  ");
797                 if (!ret)
798                         return TRACE_TYPE_PARTIAL_LINE;
799         }
800
801         /* Function */
802         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
803                 ret = trace_seq_printf(s, " ");
804                 if (!ret)
805                         return TRACE_TYPE_PARTIAL_LINE;
806         }
807
808         ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
809         if (!ret)
810                 return TRACE_TYPE_PARTIAL_LINE;
811
812         /*
813          * we already consumed the current entry to check the next one
814          * and see if this is a leaf.
815          */
816         return TRACE_TYPE_NO_CONSUME;
817 }
818
819 static enum print_line_t
820 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
821                      int type, unsigned long addr, u32 flags)
822 {
823         struct fgraph_data *data = iter->private;
824         struct trace_entry *ent = iter->ent;
825         int cpu = iter->cpu;
826         int ret;
827
828         /* Pid */
829         if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
830                 return TRACE_TYPE_PARTIAL_LINE;
831
832         if (type) {
833                 /* Interrupt */
834                 ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
835                 if (ret == TRACE_TYPE_PARTIAL_LINE)
836                         return TRACE_TYPE_PARTIAL_LINE;
837         }
838
839         /* Absolute time */
840         if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
841                 ret = print_graph_abs_time(iter->ts, s);
842                 if (!ret)
843                         return TRACE_TYPE_PARTIAL_LINE;
844         }
845
846         /* Cpu */
847         if (flags & TRACE_GRAPH_PRINT_CPU) {
848                 ret = print_graph_cpu(s, cpu);
849                 if (ret == TRACE_TYPE_PARTIAL_LINE)
850                         return TRACE_TYPE_PARTIAL_LINE;
851         }
852
853         /* Proc */
854         if (flags & TRACE_GRAPH_PRINT_PROC) {
855                 ret = print_graph_proc(s, ent->pid);
856                 if (ret == TRACE_TYPE_PARTIAL_LINE)
857                         return TRACE_TYPE_PARTIAL_LINE;
858
859                 ret = trace_seq_printf(s, " | ");
860                 if (!ret)
861                         return TRACE_TYPE_PARTIAL_LINE;
862         }
863
864         /* Latency format */
865         if (trace_flags & TRACE_ITER_LATENCY_FMT) {
866                 ret = print_graph_lat_fmt(s, ent);
867                 if (ret == TRACE_TYPE_PARTIAL_LINE)
868                         return TRACE_TYPE_PARTIAL_LINE;
869         }
870
871         return 0;
872 }
873
874 /*
875  * Entry check for irq code
876  *
877  * returns 1 if
878  *  - we are inside irq code
879  *  - we just extered irq code
880  *
881  * retunns 0 if
882  *  - funcgraph-interrupts option is set
883  *  - we are not inside irq code
884  */
885 static int
886 check_irq_entry(struct trace_iterator *iter, u32 flags,
887                 unsigned long addr, int depth)
888 {
889         int cpu = iter->cpu;
890         struct fgraph_data *data = iter->private;
891         int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
892
893         if (flags & TRACE_GRAPH_PRINT_IRQS)
894                 return 0;
895
896         /*
897          * We are inside the irq code
898          */
899         if (*depth_irq >= 0)
900                 return 1;
901
902         if ((addr < (unsigned long)__irqentry_text_start) ||
903             (addr >= (unsigned long)__irqentry_text_end))
904                 return 0;
905
906         /*
907          * We are entering irq code.
908          */
909         *depth_irq = depth;
910         return 1;
911 }
912
913 /*
914  * Return check for irq code
915  *
916  * returns 1 if
917  *  - we are inside irq code
918  *  - we just left irq code
919  *
920  * returns 0 if
921  *  - funcgraph-interrupts option is set
922  *  - we are not inside irq code
923  */
924 static int
925 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
926 {
927         int cpu = iter->cpu;
928         struct fgraph_data *data = iter->private;
929         int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
930
931         if (flags & TRACE_GRAPH_PRINT_IRQS)
932                 return 0;
933
934         /*
935          * We are not inside the irq code.
936          */
937         if (*depth_irq == -1)
938                 return 0;
939
940         /*
941          * We are inside the irq code, and this is returning entry.
942          * Let's not trace it and clear the entry depth, since
943          * we are out of irq code.
944          *
945          * This condition ensures that we 'leave the irq code' once
946          * we are out of the entry depth. Thus protecting us from
947          * the RETURN entry loss.
948          */
949         if (*depth_irq >= depth) {
950                 *depth_irq = -1;
951                 return 1;
952         }
953
954         /*
955          * We are inside the irq code, and this is not the entry.
956          */
957         return 1;
958 }
959
960 static enum print_line_t
961 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
962                         struct trace_iterator *iter, u32 flags)
963 {
964         struct fgraph_data *data = iter->private;
965         struct ftrace_graph_ent *call = &field->graph_ent;
966         struct ftrace_graph_ret_entry *leaf_ret;
967         static enum print_line_t ret;
968         int cpu = iter->cpu;
969
970         if (check_irq_entry(iter, flags, call->func, call->depth))
971                 return TRACE_TYPE_HANDLED;
972
973         if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
974                 return TRACE_TYPE_PARTIAL_LINE;
975
976         leaf_ret = get_return_for_leaf(iter, field);
977         if (leaf_ret)
978                 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
979         else
980                 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
981
982         if (data) {
983                 /*
984                  * If we failed to write our output, then we need to make
985                  * note of it. Because we already consumed our entry.
986                  */
987                 if (s->full) {
988                         data->failed = 1;
989                         data->cpu = cpu;
990                 } else
991                         data->failed = 0;
992         }
993
994         return ret;
995 }
996
997 static enum print_line_t
998 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
999                    struct trace_entry *ent, struct trace_iterator *iter,
1000                    u32 flags)
1001 {
1002         unsigned long long duration = trace->rettime - trace->calltime;
1003         struct fgraph_data *data = iter->private;
1004         pid_t pid = ent->pid;
1005         int cpu = iter->cpu;
1006         int func_match = 1;
1007         int ret;
1008         int i;
1009
1010         if (check_irq_return(iter, flags, trace->depth))
1011                 return TRACE_TYPE_HANDLED;
1012
1013         if (data) {
1014                 struct fgraph_cpu_data *cpu_data;
1015                 int cpu = iter->cpu;
1016
1017                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1018
1019                 /*
1020                  * Comments display at + 1 to depth. This is the
1021                  * return from a function, we now want the comments
1022                  * to display at the same level of the bracket.
1023                  */
1024                 cpu_data->depth = trace->depth - 1;
1025
1026                 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1027                         if (cpu_data->enter_funcs[trace->depth] != trace->func)
1028                                 func_match = 0;
1029                         cpu_data->enter_funcs[trace->depth] = 0;
1030                 }
1031         }
1032
1033         if (print_graph_prologue(iter, s, 0, 0, flags))
1034                 return TRACE_TYPE_PARTIAL_LINE;
1035
1036         /* Overhead */
1037         ret = print_graph_overhead(duration, s, flags);
1038         if (!ret)
1039                 return TRACE_TYPE_PARTIAL_LINE;
1040
1041         /* Duration */
1042         if (flags & TRACE_GRAPH_PRINT_DURATION) {
1043                 ret = print_graph_duration(duration, s);
1044                 if (ret == TRACE_TYPE_PARTIAL_LINE)
1045                         return TRACE_TYPE_PARTIAL_LINE;
1046         }
1047
1048         /* Closing brace */
1049         for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
1050                 ret = trace_seq_printf(s, " ");
1051                 if (!ret)
1052                         return TRACE_TYPE_PARTIAL_LINE;
1053         }
1054
1055         /*
1056          * If the return function does not have a matching entry,
1057          * then the entry was lost. Instead of just printing
1058          * the '}' and letting the user guess what function this
1059          * belongs to, write out the function name.
1060          */
1061         if (func_match) {
1062                 ret = trace_seq_printf(s, "}\n");
1063                 if (!ret)
1064                         return TRACE_TYPE_PARTIAL_LINE;
1065         } else {
1066                 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1067                 if (!ret)
1068                         return TRACE_TYPE_PARTIAL_LINE;
1069         }
1070
1071         /* Overrun */
1072         if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
1073                 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
1074                                         trace->overrun);
1075                 if (!ret)
1076                         return TRACE_TYPE_PARTIAL_LINE;
1077         }
1078
1079         ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1080                               cpu, pid, flags);
1081         if (ret == TRACE_TYPE_PARTIAL_LINE)
1082                 return TRACE_TYPE_PARTIAL_LINE;
1083
1084         return TRACE_TYPE_HANDLED;
1085 }
1086
1087 static enum print_line_t
1088 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1089                     struct trace_iterator *iter, u32 flags)
1090 {
1091         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1092         struct fgraph_data *data = iter->private;
1093         struct trace_event *event;
1094         int depth = 0;
1095         int ret;
1096         int i;
1097
1098         if (data)
1099                 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1100
1101         if (print_graph_prologue(iter, s, 0, 0, flags))
1102                 return TRACE_TYPE_PARTIAL_LINE;
1103
1104         /* No overhead */
1105         ret = print_graph_overhead(-1, s, flags);
1106         if (!ret)
1107                 return TRACE_TYPE_PARTIAL_LINE;
1108
1109         /* No time */
1110         if (flags & TRACE_GRAPH_PRINT_DURATION) {
1111                 ret = trace_seq_printf(s, "            |  ");
1112                 if (!ret)
1113                         return TRACE_TYPE_PARTIAL_LINE;
1114         }
1115
1116         /* Indentation */
1117         if (depth > 0)
1118                 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1119                         ret = trace_seq_printf(s, " ");
1120                         if (!ret)
1121                                 return TRACE_TYPE_PARTIAL_LINE;
1122                 }
1123
1124         /* The comment */
1125         ret = trace_seq_printf(s, "/* ");
1126         if (!ret)
1127                 return TRACE_TYPE_PARTIAL_LINE;
1128
1129         switch (iter->ent->type) {
1130         case TRACE_BPRINT:
1131                 ret = trace_print_bprintk_msg_only(iter);
1132                 if (ret != TRACE_TYPE_HANDLED)
1133                         return ret;
1134                 break;
1135         case TRACE_PRINT:
1136                 ret = trace_print_printk_msg_only(iter);
1137                 if (ret != TRACE_TYPE_HANDLED)
1138                         return ret;
1139                 break;
1140         default:
1141                 event = ftrace_find_event(ent->type);
1142                 if (!event)
1143                         return TRACE_TYPE_UNHANDLED;
1144
1145                 ret = event->funcs->trace(iter, sym_flags, event);
1146                 if (ret != TRACE_TYPE_HANDLED)
1147                         return ret;
1148         }
1149
1150         /* Strip ending newline */
1151         if (s->buffer[s->len - 1] == '\n') {
1152                 s->buffer[s->len - 1] = '\0';
1153                 s->len--;
1154         }
1155
1156         ret = trace_seq_printf(s, " */\n");
1157         if (!ret)
1158                 return TRACE_TYPE_PARTIAL_LINE;
1159
1160         return TRACE_TYPE_HANDLED;
1161 }
1162
1163
1164 enum print_line_t
1165 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1166 {
1167         struct ftrace_graph_ent_entry *field;
1168         struct fgraph_data *data = iter->private;
1169         struct trace_entry *entry = iter->ent;
1170         struct trace_seq *s = &iter->seq;
1171         int cpu = iter->cpu;
1172         int ret;
1173
1174         if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1175                 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1176                 return TRACE_TYPE_HANDLED;
1177         }
1178
1179         /*
1180          * If the last output failed, there's a possibility we need
1181          * to print out the missing entry which would never go out.
1182          */
1183         if (data && data->failed) {
1184                 field = &data->ent;
1185                 iter->cpu = data->cpu;
1186                 ret = print_graph_entry(field, s, iter, flags);
1187                 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1188                         per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1189                         ret = TRACE_TYPE_NO_CONSUME;
1190                 }
1191                 iter->cpu = cpu;
1192                 return ret;
1193         }
1194
1195         switch (entry->type) {
1196         case TRACE_GRAPH_ENT: {
1197                 /*
1198                  * print_graph_entry() may consume the current event,
1199                  * thus @field may become invalid, so we need to save it.
1200                  * sizeof(struct ftrace_graph_ent_entry) is very small,
1201                  * it can be safely saved at the stack.
1202                  */
1203                 struct ftrace_graph_ent_entry saved;
1204                 trace_assign_type(field, entry);
1205                 saved = *field;
1206                 return print_graph_entry(&saved, s, iter, flags);
1207         }
1208         case TRACE_GRAPH_RET: {
1209                 struct ftrace_graph_ret_entry *field;
1210                 trace_assign_type(field, entry);
1211                 return print_graph_return(&field->ret, s, entry, iter, flags);
1212         }
1213         case TRACE_STACK:
1214         case TRACE_FN:
1215                 /* dont trace stack and functions as comments */
1216                 return TRACE_TYPE_UNHANDLED;
1217
1218         default:
1219                 return print_graph_comment(s, entry, iter, flags);
1220         }
1221
1222         return TRACE_TYPE_HANDLED;
1223 }
1224
1225 static enum print_line_t
1226 print_graph_function(struct trace_iterator *iter)
1227 {
1228         return print_graph_function_flags(iter, tracer_flags.val);
1229 }
1230
1231 static enum print_line_t
1232 print_graph_function_event(struct trace_iterator *iter, int flags,
1233                            struct trace_event *event)
1234 {
1235         return print_graph_function(iter);
1236 }
1237
1238 static void print_lat_header(struct seq_file *s, u32 flags)
1239 {
1240         static const char spaces[] = "                " /* 16 spaces */
1241                 "    "                                  /* 4 spaces */
1242                 "                 ";                    /* 17 spaces */
1243         int size = 0;
1244
1245         if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1246                 size += 16;
1247         if (flags & TRACE_GRAPH_PRINT_CPU)
1248                 size += 4;
1249         if (flags & TRACE_GRAPH_PRINT_PROC)
1250                 size += 17;
1251
1252         seq_printf(s, "#%.*s  _-----=> irqs-off        \n", size, spaces);
1253         seq_printf(s, "#%.*s / _----=> need-resched    \n", size, spaces);
1254         seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1255         seq_printf(s, "#%.*s|| / _--=> preempt-depth   \n", size, spaces);
1256         seq_printf(s, "#%.*s||| / _-=> lock-depth      \n", size, spaces);
1257         seq_printf(s, "#%.*s|||| /                     \n", size, spaces);
1258 }
1259
1260 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1261 {
1262         int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1263
1264         if (lat)
1265                 print_lat_header(s, flags);
1266
1267         /* 1st line */
1268         seq_printf(s, "#");
1269         if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1270                 seq_printf(s, "     TIME       ");
1271         if (flags & TRACE_GRAPH_PRINT_CPU)
1272                 seq_printf(s, " CPU");
1273         if (flags & TRACE_GRAPH_PRINT_PROC)
1274                 seq_printf(s, "  TASK/PID       ");
1275         if (lat)
1276                 seq_printf(s, "|||||");
1277         if (flags & TRACE_GRAPH_PRINT_DURATION)
1278                 seq_printf(s, "  DURATION   ");
1279         seq_printf(s, "               FUNCTION CALLS\n");
1280
1281         /* 2nd line */
1282         seq_printf(s, "#");
1283         if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1284                 seq_printf(s, "      |         ");
1285         if (flags & TRACE_GRAPH_PRINT_CPU)
1286                 seq_printf(s, " |  ");
1287         if (flags & TRACE_GRAPH_PRINT_PROC)
1288                 seq_printf(s, "   |    |        ");
1289         if (lat)
1290                 seq_printf(s, "|||||");
1291         if (flags & TRACE_GRAPH_PRINT_DURATION)
1292                 seq_printf(s, "   |   |      ");
1293         seq_printf(s, "               |   |   |   |\n");
1294 }
1295
1296 void print_graph_headers(struct seq_file *s)
1297 {
1298         print_graph_headers_flags(s, tracer_flags.val);
1299 }
1300
1301 void graph_trace_open(struct trace_iterator *iter)
1302 {
1303         /* pid and depth on the last trace processed */
1304         struct fgraph_data *data;
1305         int cpu;
1306
1307         iter->private = NULL;
1308
1309         data = kzalloc(sizeof(*data), GFP_KERNEL);
1310         if (!data)
1311                 goto out_err;
1312
1313         data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1314         if (!data->cpu_data)
1315                 goto out_err_free;
1316
1317         for_each_possible_cpu(cpu) {
1318                 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1319                 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1320                 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1321                 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1322
1323                 *pid = -1;
1324                 *depth = 0;
1325                 *ignore = 0;
1326                 *depth_irq = -1;
1327         }
1328
1329         iter->private = data;
1330
1331         return;
1332
1333  out_err_free:
1334         kfree(data);
1335  out_err:
1336         pr_warning("function graph tracer: not enough memory\n");
1337 }
1338
1339 void graph_trace_close(struct trace_iterator *iter)
1340 {
1341         struct fgraph_data *data = iter->private;
1342
1343         if (data) {
1344                 free_percpu(data->cpu_data);
1345                 kfree(data);
1346         }
1347 }
1348
1349 static int func_graph_set_flag(u32 old_flags, u32 bit, int set)
1350 {
1351         if (bit == TRACE_GRAPH_PRINT_IRQS)
1352                 ftrace_graph_skip_irqs = !set;
1353
1354         return 0;
1355 }
1356
1357 static struct trace_event_functions graph_functions = {
1358         .trace          = print_graph_function_event,
1359 };
1360
1361 static struct trace_event graph_trace_entry_event = {
1362         .type           = TRACE_GRAPH_ENT,
1363         .funcs          = &graph_functions,
1364 };
1365
1366 static struct trace_event graph_trace_ret_event = {
1367         .type           = TRACE_GRAPH_RET,
1368         .funcs          = &graph_functions
1369 };
1370
1371 static struct tracer graph_trace __read_mostly = {
1372         .name           = "function_graph",
1373         .open           = graph_trace_open,
1374         .pipe_open      = graph_trace_open,
1375         .close          = graph_trace_close,
1376         .pipe_close     = graph_trace_close,
1377         .wait_pipe      = poll_wait_pipe,
1378         .init           = graph_trace_init,
1379         .reset          = graph_trace_reset,
1380         .print_line     = print_graph_function,
1381         .print_header   = print_graph_headers,
1382         .flags          = &tracer_flags,
1383         .set_flag       = func_graph_set_flag,
1384 #ifdef CONFIG_FTRACE_SELFTEST
1385         .selftest       = trace_selftest_startup_function_graph,
1386 #endif
1387 };
1388
1389 static __init int init_graph_trace(void)
1390 {
1391         max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1392
1393         if (!register_ftrace_event(&graph_trace_entry_event)) {
1394                 pr_warning("Warning: could not register graph trace events\n");
1395                 return 1;
1396         }
1397
1398         if (!register_ftrace_event(&graph_trace_ret_event)) {
1399                 pr_warning("Warning: could not register graph trace events\n");
1400                 return 1;
1401         }
1402
1403         return register_tracer(&graph_trace);
1404 }
1405
1406 device_initcall(init_graph_trace);