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