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