]> Pileus Git - ~andy/linux/blob - kernel/trace/trace_functions_graph.c
tracing: Make graph related irqs/preemptsoff functions global
[~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 static void
266 __trace_graph_function(struct trace_array *tr,
267                 unsigned long ip, unsigned long flags, int pc)
268 {
269         u64 time = trace_clock_local();
270         struct ftrace_graph_ent ent = {
271                 .func  = ip,
272                 .depth = 0,
273         };
274         struct ftrace_graph_ret ret = {
275                 .func     = ip,
276                 .depth    = 0,
277                 .calltime = time,
278                 .rettime  = time,
279         };
280
281         __trace_graph_entry(tr, &ent, flags, pc);
282         __trace_graph_return(tr, &ret, flags, pc);
283 }
284
285 void
286 trace_graph_function(struct trace_array *tr,
287                 unsigned long ip, unsigned long parent_ip,
288                 unsigned long flags, int pc)
289 {
290         __trace_graph_function(tr, parent_ip, flags, pc);
291         __trace_graph_function(tr, ip, flags, pc);
292 }
293
294 void __trace_graph_return(struct trace_array *tr,
295                                 struct ftrace_graph_ret *trace,
296                                 unsigned long flags,
297                                 int pc)
298 {
299         struct ftrace_event_call *call = &event_funcgraph_exit;
300         struct ring_buffer_event *event;
301         struct ring_buffer *buffer = tr->buffer;
302         struct ftrace_graph_ret_entry *entry;
303
304         if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
305                 return;
306
307         event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
308                                           sizeof(*entry), flags, pc);
309         if (!event)
310                 return;
311         entry   = ring_buffer_event_data(event);
312         entry->ret                              = *trace;
313         if (!filter_current_check_discard(buffer, call, entry, event))
314                 ring_buffer_unlock_commit(buffer, event);
315 }
316
317 void trace_graph_return(struct ftrace_graph_ret *trace)
318 {
319         struct trace_array *tr = graph_array;
320         struct trace_array_cpu *data;
321         unsigned long flags;
322         long disabled;
323         int cpu;
324         int pc;
325
326         local_irq_save(flags);
327         cpu = raw_smp_processor_id();
328         data = tr->data[cpu];
329         disabled = atomic_inc_return(&data->disabled);
330         if (likely(disabled == 1)) {
331                 pc = preempt_count();
332                 __trace_graph_return(tr, trace, flags, pc);
333         }
334         atomic_dec(&data->disabled);
335         local_irq_restore(flags);
336 }
337
338 void set_graph_array(struct trace_array *tr)
339 {
340         graph_array = tr;
341
342         /* Make graph_array visible before we start tracing */
343
344         smp_mb();
345 }
346
347 void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
348 {
349         if (tracing_thresh &&
350             (trace->rettime - trace->calltime < tracing_thresh))
351                 return;
352         else
353                 trace_graph_return(trace);
354 }
355
356 static int graph_trace_init(struct trace_array *tr)
357 {
358         int ret;
359
360         set_graph_array(tr);
361         if (tracing_thresh)
362                 ret = register_ftrace_graph(&trace_graph_thresh_return,
363                                             &trace_graph_thresh_entry);
364         else
365                 ret = register_ftrace_graph(&trace_graph_return,
366                                             &trace_graph_entry);
367         if (ret)
368                 return ret;
369         tracing_start_cmdline_record();
370
371         return 0;
372 }
373
374 static void graph_trace_reset(struct trace_array *tr)
375 {
376         tracing_stop_cmdline_record();
377         unregister_ftrace_graph();
378 }
379
380 static int max_bytes_for_cpu;
381
382 static enum print_line_t
383 print_graph_cpu(struct trace_seq *s, int cpu)
384 {
385         int ret;
386
387         /*
388          * Start with a space character - to make it stand out
389          * to the right a bit when trace output is pasted into
390          * email:
391          */
392         ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
393         if (!ret)
394                 return TRACE_TYPE_PARTIAL_LINE;
395
396         return TRACE_TYPE_HANDLED;
397 }
398
399 #define TRACE_GRAPH_PROCINFO_LENGTH     14
400
401 static enum print_line_t
402 print_graph_proc(struct trace_seq *s, pid_t pid)
403 {
404         char comm[TASK_COMM_LEN];
405         /* sign + log10(MAX_INT) + '\0' */
406         char pid_str[11];
407         int spaces = 0;
408         int ret;
409         int len;
410         int i;
411
412         trace_find_cmdline(pid, comm);
413         comm[7] = '\0';
414         sprintf(pid_str, "%d", pid);
415
416         /* 1 stands for the "-" character */
417         len = strlen(comm) + strlen(pid_str) + 1;
418
419         if (len < TRACE_GRAPH_PROCINFO_LENGTH)
420                 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
421
422         /* First spaces to align center */
423         for (i = 0; i < spaces / 2; i++) {
424                 ret = trace_seq_printf(s, " ");
425                 if (!ret)
426                         return TRACE_TYPE_PARTIAL_LINE;
427         }
428
429         ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
430         if (!ret)
431                 return TRACE_TYPE_PARTIAL_LINE;
432
433         /* Last spaces to align center */
434         for (i = 0; i < spaces - (spaces / 2); i++) {
435                 ret = trace_seq_printf(s, " ");
436                 if (!ret)
437                         return TRACE_TYPE_PARTIAL_LINE;
438         }
439         return TRACE_TYPE_HANDLED;
440 }
441
442
443 static enum print_line_t
444 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
445 {
446         if (!trace_seq_putc(s, ' '))
447                 return 0;
448
449         return trace_print_lat_fmt(s, entry);
450 }
451
452 /* If the pid changed since the last trace, output this event */
453 static enum print_line_t
454 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
455 {
456         pid_t prev_pid;
457         pid_t *last_pid;
458         int ret;
459
460         if (!data)
461                 return TRACE_TYPE_HANDLED;
462
463         last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
464
465         if (*last_pid == pid)
466                 return TRACE_TYPE_HANDLED;
467
468         prev_pid = *last_pid;
469         *last_pid = pid;
470
471         if (prev_pid == -1)
472                 return TRACE_TYPE_HANDLED;
473 /*
474  * Context-switch trace line:
475
476  ------------------------------------------
477  | 1)  migration/0--1  =>  sshd-1755
478  ------------------------------------------
479
480  */
481         ret = trace_seq_printf(s,
482                 " ------------------------------------------\n");
483         if (!ret)
484                 return TRACE_TYPE_PARTIAL_LINE;
485
486         ret = print_graph_cpu(s, cpu);
487         if (ret == TRACE_TYPE_PARTIAL_LINE)
488                 return TRACE_TYPE_PARTIAL_LINE;
489
490         ret = print_graph_proc(s, prev_pid);
491         if (ret == TRACE_TYPE_PARTIAL_LINE)
492                 return TRACE_TYPE_PARTIAL_LINE;
493
494         ret = trace_seq_printf(s, " => ");
495         if (!ret)
496                 return TRACE_TYPE_PARTIAL_LINE;
497
498         ret = print_graph_proc(s, pid);
499         if (ret == TRACE_TYPE_PARTIAL_LINE)
500                 return TRACE_TYPE_PARTIAL_LINE;
501
502         ret = trace_seq_printf(s,
503                 "\n ------------------------------------------\n\n");
504         if (!ret)
505                 return TRACE_TYPE_PARTIAL_LINE;
506
507         return TRACE_TYPE_HANDLED;
508 }
509
510 static struct ftrace_graph_ret_entry *
511 get_return_for_leaf(struct trace_iterator *iter,
512                 struct ftrace_graph_ent_entry *curr)
513 {
514         struct fgraph_data *data = iter->private;
515         struct ring_buffer_iter *ring_iter = NULL;
516         struct ring_buffer_event *event;
517         struct ftrace_graph_ret_entry *next;
518
519         /*
520          * If the previous output failed to write to the seq buffer,
521          * then we just reuse the data from before.
522          */
523         if (data && data->failed) {
524                 curr = &data->ent;
525                 next = &data->ret;
526         } else {
527
528                 ring_iter = iter->buffer_iter[iter->cpu];
529
530                 /* First peek to compare current entry and the next one */
531                 if (ring_iter)
532                         event = ring_buffer_iter_peek(ring_iter, NULL);
533                 else {
534                         /*
535                          * We need to consume the current entry to see
536                          * the next one.
537                          */
538                         ring_buffer_consume(iter->tr->buffer, iter->cpu,
539                                             NULL, NULL);
540                         event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
541                                                  NULL, NULL);
542                 }
543
544                 if (!event)
545                         return NULL;
546
547                 next = ring_buffer_event_data(event);
548
549                 if (data) {
550                         /*
551                          * Save current and next entries for later reference
552                          * if the output fails.
553                          */
554                         data->ent = *curr;
555                         /*
556                          * If the next event is not a return type, then
557                          * we only care about what type it is. Otherwise we can
558                          * safely copy the entire event.
559                          */
560                         if (next->ent.type == TRACE_GRAPH_RET)
561                                 data->ret = *next;
562                         else
563                                 data->ret.ent.type = next->ent.type;
564                 }
565         }
566
567         if (next->ent.type != TRACE_GRAPH_RET)
568                 return NULL;
569
570         if (curr->ent.pid != next->ent.pid ||
571                         curr->graph_ent.func != next->ret.func)
572                 return NULL;
573
574         /* this is a leaf, now advance the iterator */
575         if (ring_iter)
576                 ring_buffer_read(ring_iter, NULL);
577
578         return next;
579 }
580
581 /* Signal a overhead of time execution to the output */
582 static int
583 print_graph_overhead(unsigned long long duration, struct trace_seq *s,
584                      u32 flags)
585 {
586         /* If duration disappear, we don't need anything */
587         if (!(flags & TRACE_GRAPH_PRINT_DURATION))
588                 return 1;
589
590         /* Non nested entry or return */
591         if (duration == -1)
592                 return trace_seq_printf(s, "  ");
593
594         if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
595                 /* Duration exceeded 100 msecs */
596                 if (duration > 100000ULL)
597                         return trace_seq_printf(s, "! ");
598
599                 /* Duration exceeded 10 msecs */
600                 if (duration > 10000ULL)
601                         return trace_seq_printf(s, "+ ");
602         }
603
604         return trace_seq_printf(s, "  ");
605 }
606
607 static int print_graph_abs_time(u64 t, struct trace_seq *s)
608 {
609         unsigned long usecs_rem;
610
611         usecs_rem = do_div(t, NSEC_PER_SEC);
612         usecs_rem /= 1000;
613
614         return trace_seq_printf(s, "%5lu.%06lu |  ",
615                         (unsigned long)t, usecs_rem);
616 }
617
618 static enum print_line_t
619 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
620                 enum trace_type type, int cpu, pid_t pid, u32 flags)
621 {
622         int ret;
623         struct trace_seq *s = &iter->seq;
624
625         if (addr < (unsigned long)__irqentry_text_start ||
626                 addr >= (unsigned long)__irqentry_text_end)
627                 return TRACE_TYPE_UNHANDLED;
628
629         /* Absolute time */
630         if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
631                 ret = print_graph_abs_time(iter->ts, s);
632                 if (!ret)
633                         return TRACE_TYPE_PARTIAL_LINE;
634         }
635
636         /* Cpu */
637         if (flags & TRACE_GRAPH_PRINT_CPU) {
638                 ret = print_graph_cpu(s, cpu);
639                 if (ret == TRACE_TYPE_PARTIAL_LINE)
640                         return TRACE_TYPE_PARTIAL_LINE;
641         }
642
643         /* Proc */
644         if (flags & TRACE_GRAPH_PRINT_PROC) {
645                 ret = print_graph_proc(s, pid);
646                 if (ret == TRACE_TYPE_PARTIAL_LINE)
647                         return TRACE_TYPE_PARTIAL_LINE;
648                 ret = trace_seq_printf(s, " | ");
649                 if (!ret)
650                         return TRACE_TYPE_PARTIAL_LINE;
651         }
652
653         /* No overhead */
654         ret = print_graph_overhead(-1, s, flags);
655         if (!ret)
656                 return TRACE_TYPE_PARTIAL_LINE;
657
658         if (type == TRACE_GRAPH_ENT)
659                 ret = trace_seq_printf(s, "==========>");
660         else
661                 ret = trace_seq_printf(s, "<==========");
662
663         if (!ret)
664                 return TRACE_TYPE_PARTIAL_LINE;
665
666         /* Don't close the duration column if haven't one */
667         if (flags & TRACE_GRAPH_PRINT_DURATION)
668                 trace_seq_printf(s, " |");
669         ret = trace_seq_printf(s, "\n");
670
671         if (!ret)
672                 return TRACE_TYPE_PARTIAL_LINE;
673         return TRACE_TYPE_HANDLED;
674 }
675
676 enum print_line_t
677 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
678 {
679         unsigned long nsecs_rem = do_div(duration, 1000);
680         /* log10(ULONG_MAX) + '\0' */
681         char msecs_str[21];
682         char nsecs_str[5];
683         int ret, len;
684         int i;
685
686         sprintf(msecs_str, "%lu", (unsigned long) duration);
687
688         /* Print msecs */
689         ret = trace_seq_printf(s, "%s", msecs_str);
690         if (!ret)
691                 return TRACE_TYPE_PARTIAL_LINE;
692
693         len = strlen(msecs_str);
694
695         /* Print nsecs (we don't want to exceed 7 numbers) */
696         if (len < 7) {
697                 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
698
699                 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
700                 ret = trace_seq_printf(s, ".%s", nsecs_str);
701                 if (!ret)
702                         return TRACE_TYPE_PARTIAL_LINE;
703                 len += strlen(nsecs_str);
704         }
705
706         ret = trace_seq_printf(s, " us ");
707         if (!ret)
708                 return TRACE_TYPE_PARTIAL_LINE;
709
710         /* Print remaining spaces to fit the row's width */
711         for (i = len; i < 7; i++) {
712                 ret = trace_seq_printf(s, " ");
713                 if (!ret)
714                         return TRACE_TYPE_PARTIAL_LINE;
715         }
716         return TRACE_TYPE_HANDLED;
717 }
718
719 static enum print_line_t
720 print_graph_duration(unsigned long long duration, struct trace_seq *s)
721 {
722         int ret;
723
724         ret = trace_print_graph_duration(duration, s);
725         if (ret != TRACE_TYPE_HANDLED)
726                 return ret;
727
728         ret = trace_seq_printf(s, "|  ");
729         if (!ret)
730                 return TRACE_TYPE_PARTIAL_LINE;
731
732         return TRACE_TYPE_HANDLED;
733 }
734
735 /* Case of a leaf function on its call entry */
736 static enum print_line_t
737 print_graph_entry_leaf(struct trace_iterator *iter,
738                 struct ftrace_graph_ent_entry *entry,
739                 struct ftrace_graph_ret_entry *ret_entry,
740                 struct trace_seq *s, u32 flags)
741 {
742         struct fgraph_data *data = iter->private;
743         struct ftrace_graph_ret *graph_ret;
744         struct ftrace_graph_ent *call;
745         unsigned long long duration;
746         int ret;
747         int i;
748
749         graph_ret = &ret_entry->ret;
750         call = &entry->graph_ent;
751         duration = graph_ret->rettime - graph_ret->calltime;
752
753         if (data) {
754                 struct fgraph_cpu_data *cpu_data;
755                 int cpu = iter->cpu;
756
757                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
758
759                 /*
760                  * Comments display at + 1 to depth. Since
761                  * this is a leaf function, keep the comments
762                  * equal to this depth.
763                  */
764                 cpu_data->depth = call->depth - 1;
765
766                 /* No need to keep this function around for this depth */
767                 if (call->depth < FTRACE_RETFUNC_DEPTH)
768                         cpu_data->enter_funcs[call->depth] = 0;
769         }
770
771         /* Overhead */
772         ret = print_graph_overhead(duration, s, flags);
773         if (!ret)
774                 return TRACE_TYPE_PARTIAL_LINE;
775
776         /* Duration */
777         if (flags & TRACE_GRAPH_PRINT_DURATION) {
778                 ret = print_graph_duration(duration, s);
779                 if (ret == TRACE_TYPE_PARTIAL_LINE)
780                         return TRACE_TYPE_PARTIAL_LINE;
781         }
782
783         /* Function */
784         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
785                 ret = trace_seq_printf(s, " ");
786                 if (!ret)
787                         return TRACE_TYPE_PARTIAL_LINE;
788         }
789
790         ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
791         if (!ret)
792                 return TRACE_TYPE_PARTIAL_LINE;
793
794         return TRACE_TYPE_HANDLED;
795 }
796
797 static enum print_line_t
798 print_graph_entry_nested(struct trace_iterator *iter,
799                          struct ftrace_graph_ent_entry *entry,
800                          struct trace_seq *s, int cpu, u32 flags)
801 {
802         struct ftrace_graph_ent *call = &entry->graph_ent;
803         struct fgraph_data *data = iter->private;
804         int ret;
805         int i;
806
807         if (data) {
808                 struct fgraph_cpu_data *cpu_data;
809                 int cpu = iter->cpu;
810
811                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
812                 cpu_data->depth = call->depth;
813
814                 /* Save this function pointer to see if the exit matches */
815                 if (call->depth < FTRACE_RETFUNC_DEPTH)
816                         cpu_data->enter_funcs[call->depth] = call->func;
817         }
818
819         /* No overhead */
820         ret = print_graph_overhead(-1, s, flags);
821         if (!ret)
822                 return TRACE_TYPE_PARTIAL_LINE;
823
824         /* No time */
825         if (flags & TRACE_GRAPH_PRINT_DURATION) {
826                 ret = trace_seq_printf(s, "            |  ");
827                 if (!ret)
828                         return TRACE_TYPE_PARTIAL_LINE;
829         }
830
831         /* Function */
832         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
833                 ret = trace_seq_printf(s, " ");
834                 if (!ret)
835                         return TRACE_TYPE_PARTIAL_LINE;
836         }
837
838         ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
839         if (!ret)
840                 return TRACE_TYPE_PARTIAL_LINE;
841
842         /*
843          * we already consumed the current entry to check the next one
844          * and see if this is a leaf.
845          */
846         return TRACE_TYPE_NO_CONSUME;
847 }
848
849 static enum print_line_t
850 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
851                      int type, unsigned long addr, u32 flags)
852 {
853         struct fgraph_data *data = iter->private;
854         struct trace_entry *ent = iter->ent;
855         int cpu = iter->cpu;
856         int ret;
857
858         /* Pid */
859         if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
860                 return TRACE_TYPE_PARTIAL_LINE;
861
862         if (type) {
863                 /* Interrupt */
864                 ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
865                 if (ret == TRACE_TYPE_PARTIAL_LINE)
866                         return TRACE_TYPE_PARTIAL_LINE;
867         }
868
869         /* Absolute time */
870         if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
871                 ret = print_graph_abs_time(iter->ts, s);
872                 if (!ret)
873                         return TRACE_TYPE_PARTIAL_LINE;
874         }
875
876         /* Cpu */
877         if (flags & TRACE_GRAPH_PRINT_CPU) {
878                 ret = print_graph_cpu(s, cpu);
879                 if (ret == TRACE_TYPE_PARTIAL_LINE)
880                         return TRACE_TYPE_PARTIAL_LINE;
881         }
882
883         /* Proc */
884         if (flags & TRACE_GRAPH_PRINT_PROC) {
885                 ret = print_graph_proc(s, ent->pid);
886                 if (ret == TRACE_TYPE_PARTIAL_LINE)
887                         return TRACE_TYPE_PARTIAL_LINE;
888
889                 ret = trace_seq_printf(s, " | ");
890                 if (!ret)
891                         return TRACE_TYPE_PARTIAL_LINE;
892         }
893
894         /* Latency format */
895         if (trace_flags & TRACE_ITER_LATENCY_FMT) {
896                 ret = print_graph_lat_fmt(s, ent);
897                 if (ret == TRACE_TYPE_PARTIAL_LINE)
898                         return TRACE_TYPE_PARTIAL_LINE;
899         }
900
901         return 0;
902 }
903
904 /*
905  * Entry check for irq code
906  *
907  * returns 1 if
908  *  - we are inside irq code
909  *  - we just extered irq code
910  *
911  * retunns 0 if
912  *  - funcgraph-interrupts option is set
913  *  - we are not inside irq code
914  */
915 static int
916 check_irq_entry(struct trace_iterator *iter, u32 flags,
917                 unsigned long addr, int depth)
918 {
919         int cpu = iter->cpu;
920         int *depth_irq;
921         struct fgraph_data *data = iter->private;
922
923         /*
924          * If we are either displaying irqs, or we got called as
925          * a graph event and private data does not exist,
926          * then we bypass the irq check.
927          */
928         if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
929             (!data))
930                 return 0;
931
932         depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
933
934         /*
935          * We are inside the irq code
936          */
937         if (*depth_irq >= 0)
938                 return 1;
939
940         if ((addr < (unsigned long)__irqentry_text_start) ||
941             (addr >= (unsigned long)__irqentry_text_end))
942                 return 0;
943
944         /*
945          * We are entering irq code.
946          */
947         *depth_irq = depth;
948         return 1;
949 }
950
951 /*
952  * Return check for irq code
953  *
954  * returns 1 if
955  *  - we are inside irq code
956  *  - we just left irq code
957  *
958  * returns 0 if
959  *  - funcgraph-interrupts option is set
960  *  - we are not inside irq code
961  */
962 static int
963 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
964 {
965         int cpu = iter->cpu;
966         int *depth_irq;
967         struct fgraph_data *data = iter->private;
968
969         /*
970          * If we are either displaying irqs, or we got called as
971          * a graph event and private data does not exist,
972          * then we bypass the irq check.
973          */
974         if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
975             (!data))
976                 return 0;
977
978         depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
979
980         /*
981          * We are not inside the irq code.
982          */
983         if (*depth_irq == -1)
984                 return 0;
985
986         /*
987          * We are inside the irq code, and this is returning entry.
988          * Let's not trace it and clear the entry depth, since
989          * we are out of irq code.
990          *
991          * This condition ensures that we 'leave the irq code' once
992          * we are out of the entry depth. Thus protecting us from
993          * the RETURN entry loss.
994          */
995         if (*depth_irq >= depth) {
996                 *depth_irq = -1;
997                 return 1;
998         }
999
1000         /*
1001          * We are inside the irq code, and this is not the entry.
1002          */
1003         return 1;
1004 }
1005
1006 static enum print_line_t
1007 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
1008                         struct trace_iterator *iter, u32 flags)
1009 {
1010         struct fgraph_data *data = iter->private;
1011         struct ftrace_graph_ent *call = &field->graph_ent;
1012         struct ftrace_graph_ret_entry *leaf_ret;
1013         static enum print_line_t ret;
1014         int cpu = iter->cpu;
1015
1016         if (check_irq_entry(iter, flags, call->func, call->depth))
1017                 return TRACE_TYPE_HANDLED;
1018
1019         if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
1020                 return TRACE_TYPE_PARTIAL_LINE;
1021
1022         leaf_ret = get_return_for_leaf(iter, field);
1023         if (leaf_ret)
1024                 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
1025         else
1026                 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
1027
1028         if (data) {
1029                 /*
1030                  * If we failed to write our output, then we need to make
1031                  * note of it. Because we already consumed our entry.
1032                  */
1033                 if (s->full) {
1034                         data->failed = 1;
1035                         data->cpu = cpu;
1036                 } else
1037                         data->failed = 0;
1038         }
1039
1040         return ret;
1041 }
1042
1043 static enum print_line_t
1044 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
1045                    struct trace_entry *ent, struct trace_iterator *iter,
1046                    u32 flags)
1047 {
1048         unsigned long long duration = trace->rettime - trace->calltime;
1049         struct fgraph_data *data = iter->private;
1050         pid_t pid = ent->pid;
1051         int cpu = iter->cpu;
1052         int func_match = 1;
1053         int ret;
1054         int i;
1055
1056         if (check_irq_return(iter, flags, trace->depth))
1057                 return TRACE_TYPE_HANDLED;
1058
1059         if (data) {
1060                 struct fgraph_cpu_data *cpu_data;
1061                 int cpu = iter->cpu;
1062
1063                 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1064
1065                 /*
1066                  * Comments display at + 1 to depth. This is the
1067                  * return from a function, we now want the comments
1068                  * to display at the same level of the bracket.
1069                  */
1070                 cpu_data->depth = trace->depth - 1;
1071
1072                 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1073                         if (cpu_data->enter_funcs[trace->depth] != trace->func)
1074                                 func_match = 0;
1075                         cpu_data->enter_funcs[trace->depth] = 0;
1076                 }
1077         }
1078
1079         if (print_graph_prologue(iter, s, 0, 0, flags))
1080                 return TRACE_TYPE_PARTIAL_LINE;
1081
1082         /* Overhead */
1083         ret = print_graph_overhead(duration, s, flags);
1084         if (!ret)
1085                 return TRACE_TYPE_PARTIAL_LINE;
1086
1087         /* Duration */
1088         if (flags & TRACE_GRAPH_PRINT_DURATION) {
1089                 ret = print_graph_duration(duration, s);
1090                 if (ret == TRACE_TYPE_PARTIAL_LINE)
1091                         return TRACE_TYPE_PARTIAL_LINE;
1092         }
1093
1094         /* Closing brace */
1095         for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
1096                 ret = trace_seq_printf(s, " ");
1097                 if (!ret)
1098                         return TRACE_TYPE_PARTIAL_LINE;
1099         }
1100
1101         /*
1102          * If the return function does not have a matching entry,
1103          * then the entry was lost. Instead of just printing
1104          * the '}' and letting the user guess what function this
1105          * belongs to, write out the function name.
1106          */
1107         if (func_match) {
1108                 ret = trace_seq_printf(s, "}\n");
1109                 if (!ret)
1110                         return TRACE_TYPE_PARTIAL_LINE;
1111         } else {
1112                 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1113                 if (!ret)
1114                         return TRACE_TYPE_PARTIAL_LINE;
1115         }
1116
1117         /* Overrun */
1118         if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
1119                 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
1120                                         trace->overrun);
1121                 if (!ret)
1122                         return TRACE_TYPE_PARTIAL_LINE;
1123         }
1124
1125         ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1126                               cpu, pid, flags);
1127         if (ret == TRACE_TYPE_PARTIAL_LINE)
1128                 return TRACE_TYPE_PARTIAL_LINE;
1129
1130         return TRACE_TYPE_HANDLED;
1131 }
1132
1133 static enum print_line_t
1134 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1135                     struct trace_iterator *iter, u32 flags)
1136 {
1137         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1138         struct fgraph_data *data = iter->private;
1139         struct trace_event *event;
1140         int depth = 0;
1141         int ret;
1142         int i;
1143
1144         if (data)
1145                 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1146
1147         if (print_graph_prologue(iter, s, 0, 0, flags))
1148                 return TRACE_TYPE_PARTIAL_LINE;
1149
1150         /* No overhead */
1151         ret = print_graph_overhead(-1, s, flags);
1152         if (!ret)
1153                 return TRACE_TYPE_PARTIAL_LINE;
1154
1155         /* No time */
1156         if (flags & TRACE_GRAPH_PRINT_DURATION) {
1157                 ret = trace_seq_printf(s, "            |  ");
1158                 if (!ret)
1159                         return TRACE_TYPE_PARTIAL_LINE;
1160         }
1161
1162         /* Indentation */
1163         if (depth > 0)
1164                 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1165                         ret = trace_seq_printf(s, " ");
1166                         if (!ret)
1167                                 return TRACE_TYPE_PARTIAL_LINE;
1168                 }
1169
1170         /* The comment */
1171         ret = trace_seq_printf(s, "/* ");
1172         if (!ret)
1173                 return TRACE_TYPE_PARTIAL_LINE;
1174
1175         switch (iter->ent->type) {
1176         case TRACE_BPRINT:
1177                 ret = trace_print_bprintk_msg_only(iter);
1178                 if (ret != TRACE_TYPE_HANDLED)
1179                         return ret;
1180                 break;
1181         case TRACE_PRINT:
1182                 ret = trace_print_printk_msg_only(iter);
1183                 if (ret != TRACE_TYPE_HANDLED)
1184                         return ret;
1185                 break;
1186         default:
1187                 event = ftrace_find_event(ent->type);
1188                 if (!event)
1189                         return TRACE_TYPE_UNHANDLED;
1190
1191                 ret = event->funcs->trace(iter, sym_flags, event);
1192                 if (ret != TRACE_TYPE_HANDLED)
1193                         return ret;
1194         }
1195
1196         /* Strip ending newline */
1197         if (s->buffer[s->len - 1] == '\n') {
1198                 s->buffer[s->len - 1] = '\0';
1199                 s->len--;
1200         }
1201
1202         ret = trace_seq_printf(s, " */\n");
1203         if (!ret)
1204                 return TRACE_TYPE_PARTIAL_LINE;
1205
1206         return TRACE_TYPE_HANDLED;
1207 }
1208
1209
1210 enum print_line_t
1211 __print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1212 {
1213         struct ftrace_graph_ent_entry *field;
1214         struct fgraph_data *data = iter->private;
1215         struct trace_entry *entry = iter->ent;
1216         struct trace_seq *s = &iter->seq;
1217         int cpu = iter->cpu;
1218         int ret;
1219
1220         if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1221                 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1222                 return TRACE_TYPE_HANDLED;
1223         }
1224
1225         /*
1226          * If the last output failed, there's a possibility we need
1227          * to print out the missing entry which would never go out.
1228          */
1229         if (data && data->failed) {
1230                 field = &data->ent;
1231                 iter->cpu = data->cpu;
1232                 ret = print_graph_entry(field, s, iter, flags);
1233                 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1234                         per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1235                         ret = TRACE_TYPE_NO_CONSUME;
1236                 }
1237                 iter->cpu = cpu;
1238                 return ret;
1239         }
1240
1241         switch (entry->type) {
1242         case TRACE_GRAPH_ENT: {
1243                 /*
1244                  * print_graph_entry() may consume the current event,
1245                  * thus @field may become invalid, so we need to save it.
1246                  * sizeof(struct ftrace_graph_ent_entry) is very small,
1247                  * it can be safely saved at the stack.
1248                  */
1249                 struct ftrace_graph_ent_entry saved;
1250                 trace_assign_type(field, entry);
1251                 saved = *field;
1252                 return print_graph_entry(&saved, s, iter, flags);
1253         }
1254         case TRACE_GRAPH_RET: {
1255                 struct ftrace_graph_ret_entry *field;
1256                 trace_assign_type(field, entry);
1257                 return print_graph_return(&field->ret, s, entry, iter, flags);
1258         }
1259         case TRACE_STACK:
1260         case TRACE_FN:
1261                 /* dont trace stack and functions as comments */
1262                 return TRACE_TYPE_UNHANDLED;
1263
1264         default:
1265                 return print_graph_comment(s, entry, iter, flags);
1266         }
1267
1268         return TRACE_TYPE_HANDLED;
1269 }
1270
1271 static enum print_line_t
1272 print_graph_function(struct trace_iterator *iter)
1273 {
1274         return __print_graph_function_flags(iter, tracer_flags.val);
1275 }
1276
1277 enum print_line_t print_graph_function_flags(struct trace_iterator *iter,
1278                                              u32 flags)
1279 {
1280         if (trace_flags & TRACE_ITER_LATENCY_FMT)
1281                 flags |= TRACE_GRAPH_PRINT_DURATION;
1282         else
1283                 flags |= TRACE_GRAPH_PRINT_ABS_TIME;
1284
1285         return __print_graph_function_flags(iter, flags);
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||| / _-=> lock-depth      \n", size, spaces);
1314         seq_printf(s, "#%.*s|||| /                     \n", size, spaces);
1315 }
1316
1317 static void __print_graph_headers_flags(struct seq_file *s, u32 flags)
1318 {
1319         int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1320
1321         if (lat)
1322                 print_lat_header(s, flags);
1323
1324         /* 1st line */
1325         seq_printf(s, "#");
1326         if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1327                 seq_printf(s, "     TIME       ");
1328         if (flags & TRACE_GRAPH_PRINT_CPU)
1329                 seq_printf(s, " CPU");
1330         if (flags & TRACE_GRAPH_PRINT_PROC)
1331                 seq_printf(s, "  TASK/PID       ");
1332         if (lat)
1333                 seq_printf(s, "|||||");
1334         if (flags & TRACE_GRAPH_PRINT_DURATION)
1335                 seq_printf(s, "  DURATION   ");
1336         seq_printf(s, "               FUNCTION CALLS\n");
1337
1338         /* 2nd line */
1339         seq_printf(s, "#");
1340         if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1341                 seq_printf(s, "      |         ");
1342         if (flags & TRACE_GRAPH_PRINT_CPU)
1343                 seq_printf(s, " |  ");
1344         if (flags & TRACE_GRAPH_PRINT_PROC)
1345                 seq_printf(s, "   |    |        ");
1346         if (lat)
1347                 seq_printf(s, "|||||");
1348         if (flags & TRACE_GRAPH_PRINT_DURATION)
1349                 seq_printf(s, "   |   |      ");
1350         seq_printf(s, "               |   |   |   |\n");
1351 }
1352
1353 void print_graph_headers(struct seq_file *s)
1354 {
1355         print_graph_headers_flags(s, tracer_flags.val);
1356 }
1357
1358 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1359 {
1360         struct trace_iterator *iter = s->private;
1361
1362         if (trace_flags & TRACE_ITER_LATENCY_FMT) {
1363                 /* print nothing if the buffers are empty */
1364                 if (trace_empty(iter))
1365                         return;
1366
1367                 print_trace_header(s, iter);
1368                 flags |= TRACE_GRAPH_PRINT_DURATION;
1369         } else
1370                 flags |= TRACE_GRAPH_PRINT_ABS_TIME;
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 static __init int init_graph_trace(void)
1464 {
1465         max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1466
1467         if (!register_ftrace_event(&graph_trace_entry_event)) {
1468                 pr_warning("Warning: could not register graph trace events\n");
1469                 return 1;
1470         }
1471
1472         if (!register_ftrace_event(&graph_trace_ret_event)) {
1473                 pr_warning("Warning: could not register graph trace events\n");
1474                 return 1;
1475         }
1476
1477         return register_tracer(&graph_trace);
1478 }
1479
1480 device_initcall(init_graph_trace);