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>
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <linux/slab.h>
16 #include "trace_output.h"
18 /* When set, irq functions will be ignored */
19 static int ftrace_graph_skip_irqs;
21 struct fgraph_cpu_data {
26 unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH];
30 struct fgraph_cpu_data __percpu *cpu_data;
32 /* Place to preserve last processed entry. */
33 struct ftrace_graph_ent_entry ent;
34 struct ftrace_graph_ret_entry ret;
39 #define TRACE_GRAPH_INDENT 2
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
50 static unsigned int max_depth;
52 static struct tracer_opt trace_opts[] = {
53 /* Display overruns? (for self-debug purpose) */
54 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
56 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
57 /* Display Overhead ? */
58 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
59 /* Display proc name/pid */
60 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
61 /* Display duration of execution */
62 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
63 /* Display absolute time of an entry */
64 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
65 /* Display interrupts */
66 { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
70 static struct tracer_flags tracer_flags = {
71 /* Don't display overruns and proc by default */
72 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
73 TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS,
77 static struct trace_array *graph_array;
80 * DURATION column is being also used to display IRQ signs,
81 * following values are used by print_graph_irq and others
82 * to fill in space into DURATION column.
85 DURATION_FILL_FULL = -1,
86 DURATION_FILL_START = -2,
87 DURATION_FILL_END = -3,
90 static enum print_line_t
91 print_graph_duration(unsigned long long duration, struct trace_seq *s,
94 /* Add a function return address to the trace stack on thread info.*/
96 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
97 unsigned long frame_pointer)
99 unsigned long long calltime;
102 if (!current->ret_stack)
106 * We must make sure the ret_stack is tested before we read
111 /* The return trace stack is full */
112 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
113 atomic_inc(¤t->trace_overrun);
117 calltime = trace_clock_local();
119 index = ++current->curr_ret_stack;
121 current->ret_stack[index].ret = ret;
122 current->ret_stack[index].func = func;
123 current->ret_stack[index].calltime = calltime;
124 current->ret_stack[index].subtime = 0;
125 current->ret_stack[index].fp = frame_pointer;
131 /* Retrieve a function return address to the trace stack on thread info.*/
133 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
134 unsigned long frame_pointer)
138 index = current->curr_ret_stack;
140 if (unlikely(index < 0)) {
143 /* Might as well panic, otherwise we have no where to go */
144 *ret = (unsigned long)panic;
148 #if defined(CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST) && !defined(CC_USING_FENTRY)
150 * The arch may choose to record the frame pointer used
151 * and check it here to make sure that it is what we expect it
152 * to be. If gcc does not set the place holder of the return
153 * address in the frame pointer, and does a copy instead, then
154 * the function graph trace will fail. This test detects this
157 * Currently, x86_32 with optimize for size (-Os) makes the latest
160 * Note, -mfentry does not use frame pointers, and this test
161 * is not needed if CC_USING_FENTRY is set.
163 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
165 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
166 " from func %ps return to %lx\n",
167 current->ret_stack[index].fp,
169 (void *)current->ret_stack[index].func,
170 current->ret_stack[index].ret);
171 *ret = (unsigned long)panic;
176 *ret = current->ret_stack[index].ret;
177 trace->func = current->ret_stack[index].func;
178 trace->calltime = current->ret_stack[index].calltime;
179 trace->overrun = atomic_read(¤t->trace_overrun);
180 trace->depth = index;
184 * Send the trace to the ring-buffer.
185 * @return the original return address.
187 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
189 struct ftrace_graph_ret trace;
192 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
193 trace.rettime = trace_clock_local();
194 ftrace_graph_return(&trace);
196 current->curr_ret_stack--;
198 if (unlikely(!ret)) {
201 /* Might as well panic. What else to do? */
202 ret = (unsigned long)panic;
208 int __trace_graph_entry(struct trace_array *tr,
209 struct ftrace_graph_ent *trace,
213 struct ftrace_event_call *call = &event_funcgraph_entry;
214 struct ring_buffer_event *event;
215 struct ring_buffer *buffer = tr->buffer;
216 struct ftrace_graph_ent_entry *entry;
218 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
221 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
222 sizeof(*entry), flags, pc);
225 entry = ring_buffer_event_data(event);
226 entry->graph_ent = *trace;
227 if (!filter_current_check_discard(buffer, call, entry, event))
228 __buffer_unlock_commit(buffer, event);
233 static inline int ftrace_graph_ignore_irqs(void)
235 if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
241 int trace_graph_entry(struct ftrace_graph_ent *trace)
243 struct trace_array *tr = graph_array;
244 struct trace_array_cpu *data;
251 if (!ftrace_trace_task(current))
254 /* trace it when it is-nested-in or is a function enabled. */
255 if ((!(trace->depth || ftrace_graph_addr(trace->func)) ||
256 ftrace_graph_ignore_irqs()) ||
257 (max_depth && trace->depth >= max_depth))
260 local_irq_save(flags);
261 cpu = raw_smp_processor_id();
262 data = tr->data[cpu];
263 disabled = atomic_inc_return(&data->disabled);
264 if (likely(disabled == 1)) {
265 pc = preempt_count();
266 ret = __trace_graph_entry(tr, trace, flags, pc);
271 atomic_dec(&data->disabled);
272 local_irq_restore(flags);
277 int trace_graph_thresh_entry(struct ftrace_graph_ent *trace)
282 return trace_graph_entry(trace);
286 __trace_graph_function(struct trace_array *tr,
287 unsigned long ip, unsigned long flags, int pc)
289 u64 time = trace_clock_local();
290 struct ftrace_graph_ent ent = {
294 struct ftrace_graph_ret ret = {
301 __trace_graph_entry(tr, &ent, flags, pc);
302 __trace_graph_return(tr, &ret, flags, pc);
306 trace_graph_function(struct trace_array *tr,
307 unsigned long ip, unsigned long parent_ip,
308 unsigned long flags, int pc)
310 __trace_graph_function(tr, ip, flags, pc);
313 void __trace_graph_return(struct trace_array *tr,
314 struct ftrace_graph_ret *trace,
318 struct ftrace_event_call *call = &event_funcgraph_exit;
319 struct ring_buffer_event *event;
320 struct ring_buffer *buffer = tr->buffer;
321 struct ftrace_graph_ret_entry *entry;
323 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
326 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
327 sizeof(*entry), flags, pc);
330 entry = ring_buffer_event_data(event);
332 if (!filter_current_check_discard(buffer, call, entry, event))
333 __buffer_unlock_commit(buffer, event);
336 void trace_graph_return(struct ftrace_graph_ret *trace)
338 struct trace_array *tr = graph_array;
339 struct trace_array_cpu *data;
345 local_irq_save(flags);
346 cpu = raw_smp_processor_id();
347 data = tr->data[cpu];
348 disabled = atomic_inc_return(&data->disabled);
349 if (likely(disabled == 1)) {
350 pc = preempt_count();
351 __trace_graph_return(tr, trace, flags, pc);
353 atomic_dec(&data->disabled);
354 local_irq_restore(flags);
357 void set_graph_array(struct trace_array *tr)
361 /* Make graph_array visible before we start tracing */
366 void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
368 if (tracing_thresh &&
369 (trace->rettime - trace->calltime < tracing_thresh))
372 trace_graph_return(trace);
375 static int graph_trace_init(struct trace_array *tr)
381 ret = register_ftrace_graph(&trace_graph_thresh_return,
382 &trace_graph_thresh_entry);
384 ret = register_ftrace_graph(&trace_graph_return,
388 tracing_start_cmdline_record();
393 static void graph_trace_reset(struct trace_array *tr)
395 tracing_stop_cmdline_record();
396 unregister_ftrace_graph();
399 static int max_bytes_for_cpu;
401 static enum print_line_t
402 print_graph_cpu(struct trace_seq *s, int cpu)
407 * Start with a space character - to make it stand out
408 * to the right a bit when trace output is pasted into
411 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
413 return TRACE_TYPE_PARTIAL_LINE;
415 return TRACE_TYPE_HANDLED;
418 #define TRACE_GRAPH_PROCINFO_LENGTH 14
420 static enum print_line_t
421 print_graph_proc(struct trace_seq *s, pid_t pid)
423 char comm[TASK_COMM_LEN];
424 /* sign + log10(MAX_INT) + '\0' */
431 trace_find_cmdline(pid, comm);
433 sprintf(pid_str, "%d", pid);
435 /* 1 stands for the "-" character */
436 len = strlen(comm) + strlen(pid_str) + 1;
438 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
439 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
441 /* First spaces to align center */
442 for (i = 0; i < spaces / 2; i++) {
443 ret = trace_seq_printf(s, " ");
445 return TRACE_TYPE_PARTIAL_LINE;
448 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
450 return TRACE_TYPE_PARTIAL_LINE;
452 /* Last spaces to align center */
453 for (i = 0; i < spaces - (spaces / 2); i++) {
454 ret = trace_seq_printf(s, " ");
456 return TRACE_TYPE_PARTIAL_LINE;
458 return TRACE_TYPE_HANDLED;
462 static enum print_line_t
463 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
465 if (!trace_seq_putc(s, ' '))
468 return trace_print_lat_fmt(s, entry);
471 /* If the pid changed since the last trace, output this event */
472 static enum print_line_t
473 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
480 return TRACE_TYPE_HANDLED;
482 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
484 if (*last_pid == pid)
485 return TRACE_TYPE_HANDLED;
487 prev_pid = *last_pid;
491 return TRACE_TYPE_HANDLED;
493 * Context-switch trace line:
495 ------------------------------------------
496 | 1) migration/0--1 => sshd-1755
497 ------------------------------------------
500 ret = trace_seq_printf(s,
501 " ------------------------------------------\n");
503 return TRACE_TYPE_PARTIAL_LINE;
505 ret = print_graph_cpu(s, cpu);
506 if (ret == TRACE_TYPE_PARTIAL_LINE)
507 return TRACE_TYPE_PARTIAL_LINE;
509 ret = print_graph_proc(s, prev_pid);
510 if (ret == TRACE_TYPE_PARTIAL_LINE)
511 return TRACE_TYPE_PARTIAL_LINE;
513 ret = trace_seq_printf(s, " => ");
515 return TRACE_TYPE_PARTIAL_LINE;
517 ret = print_graph_proc(s, pid);
518 if (ret == TRACE_TYPE_PARTIAL_LINE)
519 return TRACE_TYPE_PARTIAL_LINE;
521 ret = trace_seq_printf(s,
522 "\n ------------------------------------------\n\n");
524 return TRACE_TYPE_PARTIAL_LINE;
526 return TRACE_TYPE_HANDLED;
529 static struct ftrace_graph_ret_entry *
530 get_return_for_leaf(struct trace_iterator *iter,
531 struct ftrace_graph_ent_entry *curr)
533 struct fgraph_data *data = iter->private;
534 struct ring_buffer_iter *ring_iter = NULL;
535 struct ring_buffer_event *event;
536 struct ftrace_graph_ret_entry *next;
539 * If the previous output failed to write to the seq buffer,
540 * then we just reuse the data from before.
542 if (data && data->failed) {
547 ring_iter = trace_buffer_iter(iter, iter->cpu);
549 /* First peek to compare current entry and the next one */
551 event = ring_buffer_iter_peek(ring_iter, NULL);
554 * We need to consume the current entry to see
557 ring_buffer_consume(iter->tr->buffer, iter->cpu,
559 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
566 next = ring_buffer_event_data(event);
570 * Save current and next entries for later reference
571 * if the output fails.
575 * If the next event is not a return type, then
576 * we only care about what type it is. Otherwise we can
577 * safely copy the entire event.
579 if (next->ent.type == TRACE_GRAPH_RET)
582 data->ret.ent.type = next->ent.type;
586 if (next->ent.type != TRACE_GRAPH_RET)
589 if (curr->ent.pid != next->ent.pid ||
590 curr->graph_ent.func != next->ret.func)
593 /* this is a leaf, now advance the iterator */
595 ring_buffer_read(ring_iter, NULL);
600 static int print_graph_abs_time(u64 t, struct trace_seq *s)
602 unsigned long usecs_rem;
604 usecs_rem = do_div(t, NSEC_PER_SEC);
607 return trace_seq_printf(s, "%5lu.%06lu | ",
608 (unsigned long)t, usecs_rem);
611 static enum print_line_t
612 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
613 enum trace_type type, int cpu, pid_t pid, u32 flags)
616 struct trace_seq *s = &iter->seq;
618 if (addr < (unsigned long)__irqentry_text_start ||
619 addr >= (unsigned long)__irqentry_text_end)
620 return TRACE_TYPE_UNHANDLED;
622 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
624 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
625 ret = print_graph_abs_time(iter->ts, s);
627 return TRACE_TYPE_PARTIAL_LINE;
631 if (flags & TRACE_GRAPH_PRINT_CPU) {
632 ret = print_graph_cpu(s, cpu);
633 if (ret == TRACE_TYPE_PARTIAL_LINE)
634 return TRACE_TYPE_PARTIAL_LINE;
638 if (flags & TRACE_GRAPH_PRINT_PROC) {
639 ret = print_graph_proc(s, pid);
640 if (ret == TRACE_TYPE_PARTIAL_LINE)
641 return TRACE_TYPE_PARTIAL_LINE;
642 ret = trace_seq_printf(s, " | ");
644 return TRACE_TYPE_PARTIAL_LINE;
649 ret = print_graph_duration(DURATION_FILL_START, s, flags);
650 if (ret != TRACE_TYPE_HANDLED)
653 if (type == TRACE_GRAPH_ENT)
654 ret = trace_seq_printf(s, "==========>");
656 ret = trace_seq_printf(s, "<==========");
659 return TRACE_TYPE_PARTIAL_LINE;
661 ret = print_graph_duration(DURATION_FILL_END, s, flags);
662 if (ret != TRACE_TYPE_HANDLED)
665 ret = trace_seq_printf(s, "\n");
668 return TRACE_TYPE_PARTIAL_LINE;
669 return TRACE_TYPE_HANDLED;
673 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
675 unsigned long nsecs_rem = do_div(duration, 1000);
676 /* log10(ULONG_MAX) + '\0' */
682 sprintf(msecs_str, "%lu", (unsigned long) duration);
685 ret = trace_seq_printf(s, "%s", msecs_str);
687 return TRACE_TYPE_PARTIAL_LINE;
689 len = strlen(msecs_str);
691 /* Print nsecs (we don't want to exceed 7 numbers) */
693 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
695 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
696 ret = trace_seq_printf(s, ".%s", nsecs_str);
698 return TRACE_TYPE_PARTIAL_LINE;
699 len += strlen(nsecs_str);
702 ret = trace_seq_printf(s, " us ");
704 return TRACE_TYPE_PARTIAL_LINE;
706 /* Print remaining spaces to fit the row's width */
707 for (i = len; i < 7; i++) {
708 ret = trace_seq_printf(s, " ");
710 return TRACE_TYPE_PARTIAL_LINE;
712 return TRACE_TYPE_HANDLED;
715 static enum print_line_t
716 print_graph_duration(unsigned long long duration, struct trace_seq *s,
721 if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
722 !(trace_flags & TRACE_ITER_CONTEXT_INFO))
723 return TRACE_TYPE_HANDLED;
725 /* No real adata, just filling the column with spaces */
727 case DURATION_FILL_FULL:
728 ret = trace_seq_printf(s, " | ");
729 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
730 case DURATION_FILL_START:
731 ret = trace_seq_printf(s, " ");
732 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
733 case DURATION_FILL_END:
734 ret = trace_seq_printf(s, " |");
735 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
738 /* Signal a overhead of time execution to the output */
739 if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
740 /* Duration exceeded 100 msecs */
741 if (duration > 100000ULL)
742 ret = trace_seq_printf(s, "! ");
743 /* Duration exceeded 10 msecs */
744 else if (duration > 10000ULL)
745 ret = trace_seq_printf(s, "+ ");
749 * The -1 means we either did not exceed the duration tresholds
750 * or we dont want to print out the overhead. Either way we need
751 * to fill out the space.
754 ret = trace_seq_printf(s, " ");
756 /* Catching here any failure happenned above */
758 return TRACE_TYPE_PARTIAL_LINE;
760 ret = trace_print_graph_duration(duration, s);
761 if (ret != TRACE_TYPE_HANDLED)
764 ret = trace_seq_printf(s, "| ");
766 return TRACE_TYPE_PARTIAL_LINE;
768 return TRACE_TYPE_HANDLED;
771 /* Case of a leaf function on its call entry */
772 static enum print_line_t
773 print_graph_entry_leaf(struct trace_iterator *iter,
774 struct ftrace_graph_ent_entry *entry,
775 struct ftrace_graph_ret_entry *ret_entry,
776 struct trace_seq *s, u32 flags)
778 struct fgraph_data *data = iter->private;
779 struct ftrace_graph_ret *graph_ret;
780 struct ftrace_graph_ent *call;
781 unsigned long long duration;
785 graph_ret = &ret_entry->ret;
786 call = &entry->graph_ent;
787 duration = graph_ret->rettime - graph_ret->calltime;
790 struct fgraph_cpu_data *cpu_data;
793 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
796 * Comments display at + 1 to depth. Since
797 * this is a leaf function, keep the comments
798 * equal to this depth.
800 cpu_data->depth = call->depth - 1;
802 /* No need to keep this function around for this depth */
803 if (call->depth < FTRACE_RETFUNC_DEPTH)
804 cpu_data->enter_funcs[call->depth] = 0;
807 /* Overhead and duration */
808 ret = print_graph_duration(duration, s, flags);
809 if (ret == TRACE_TYPE_PARTIAL_LINE)
810 return TRACE_TYPE_PARTIAL_LINE;
813 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
814 ret = trace_seq_printf(s, " ");
816 return TRACE_TYPE_PARTIAL_LINE;
819 ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
821 return TRACE_TYPE_PARTIAL_LINE;
823 return TRACE_TYPE_HANDLED;
826 static enum print_line_t
827 print_graph_entry_nested(struct trace_iterator *iter,
828 struct ftrace_graph_ent_entry *entry,
829 struct trace_seq *s, int cpu, u32 flags)
831 struct ftrace_graph_ent *call = &entry->graph_ent;
832 struct fgraph_data *data = iter->private;
837 struct fgraph_cpu_data *cpu_data;
840 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
841 cpu_data->depth = call->depth;
843 /* Save this function pointer to see if the exit matches */
844 if (call->depth < FTRACE_RETFUNC_DEPTH)
845 cpu_data->enter_funcs[call->depth] = call->func;
849 ret = print_graph_duration(DURATION_FILL_FULL, s, flags);
850 if (ret != TRACE_TYPE_HANDLED)
854 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
855 ret = trace_seq_printf(s, " ");
857 return TRACE_TYPE_PARTIAL_LINE;
860 ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
862 return TRACE_TYPE_PARTIAL_LINE;
865 * we already consumed the current entry to check the next one
866 * and see if this is a leaf.
868 return TRACE_TYPE_NO_CONSUME;
871 static enum print_line_t
872 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
873 int type, unsigned long addr, u32 flags)
875 struct fgraph_data *data = iter->private;
876 struct trace_entry *ent = iter->ent;
881 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
882 return TRACE_TYPE_PARTIAL_LINE;
886 ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
887 if (ret == TRACE_TYPE_PARTIAL_LINE)
888 return TRACE_TYPE_PARTIAL_LINE;
891 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
895 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
896 ret = print_graph_abs_time(iter->ts, s);
898 return TRACE_TYPE_PARTIAL_LINE;
902 if (flags & TRACE_GRAPH_PRINT_CPU) {
903 ret = print_graph_cpu(s, cpu);
904 if (ret == TRACE_TYPE_PARTIAL_LINE)
905 return TRACE_TYPE_PARTIAL_LINE;
909 if (flags & TRACE_GRAPH_PRINT_PROC) {
910 ret = print_graph_proc(s, ent->pid);
911 if (ret == TRACE_TYPE_PARTIAL_LINE)
912 return TRACE_TYPE_PARTIAL_LINE;
914 ret = trace_seq_printf(s, " | ");
916 return TRACE_TYPE_PARTIAL_LINE;
920 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
921 ret = print_graph_lat_fmt(s, ent);
922 if (ret == TRACE_TYPE_PARTIAL_LINE)
923 return TRACE_TYPE_PARTIAL_LINE;
930 * Entry check for irq code
933 * - we are inside irq code
934 * - we just entered irq code
937 * - funcgraph-interrupts option is set
938 * - we are not inside irq code
941 check_irq_entry(struct trace_iterator *iter, u32 flags,
942 unsigned long addr, int depth)
946 struct fgraph_data *data = iter->private;
949 * If we are either displaying irqs, or we got called as
950 * a graph event and private data does not exist,
951 * then we bypass the irq check.
953 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
957 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
960 * We are inside the irq code
965 if ((addr < (unsigned long)__irqentry_text_start) ||
966 (addr >= (unsigned long)__irqentry_text_end))
970 * We are entering irq code.
977 * Return check for irq code
980 * - we are inside irq code
981 * - we just left irq code
984 * - funcgraph-interrupts option is set
985 * - we are not inside irq code
988 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
992 struct fgraph_data *data = iter->private;
995 * If we are either displaying irqs, or we got called as
996 * a graph event and private data does not exist,
997 * then we bypass the irq check.
999 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
1003 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1006 * We are not inside the irq code.
1008 if (*depth_irq == -1)
1012 * We are inside the irq code, and this is returning entry.
1013 * Let's not trace it and clear the entry depth, since
1014 * we are out of irq code.
1016 * This condition ensures that we 'leave the irq code' once
1017 * we are out of the entry depth. Thus protecting us from
1018 * the RETURN entry loss.
1020 if (*depth_irq >= depth) {
1026 * We are inside the irq code, and this is not the entry.
1031 static enum print_line_t
1032 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
1033 struct trace_iterator *iter, u32 flags)
1035 struct fgraph_data *data = iter->private;
1036 struct ftrace_graph_ent *call = &field->graph_ent;
1037 struct ftrace_graph_ret_entry *leaf_ret;
1038 static enum print_line_t ret;
1039 int cpu = iter->cpu;
1041 if (check_irq_entry(iter, flags, call->func, call->depth))
1042 return TRACE_TYPE_HANDLED;
1044 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
1045 return TRACE_TYPE_PARTIAL_LINE;
1047 leaf_ret = get_return_for_leaf(iter, field);
1049 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
1051 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
1055 * If we failed to write our output, then we need to make
1056 * note of it. Because we already consumed our entry.
1068 static enum print_line_t
1069 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
1070 struct trace_entry *ent, struct trace_iterator *iter,
1073 unsigned long long duration = trace->rettime - trace->calltime;
1074 struct fgraph_data *data = iter->private;
1075 pid_t pid = ent->pid;
1076 int cpu = iter->cpu;
1081 if (check_irq_return(iter, flags, trace->depth))
1082 return TRACE_TYPE_HANDLED;
1085 struct fgraph_cpu_data *cpu_data;
1086 int cpu = iter->cpu;
1088 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1091 * Comments display at + 1 to depth. This is the
1092 * return from a function, we now want the comments
1093 * to display at the same level of the bracket.
1095 cpu_data->depth = trace->depth - 1;
1097 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1098 if (cpu_data->enter_funcs[trace->depth] != trace->func)
1100 cpu_data->enter_funcs[trace->depth] = 0;
1104 if (print_graph_prologue(iter, s, 0, 0, flags))
1105 return TRACE_TYPE_PARTIAL_LINE;
1107 /* Overhead and duration */
1108 ret = print_graph_duration(duration, s, flags);
1109 if (ret == TRACE_TYPE_PARTIAL_LINE)
1110 return TRACE_TYPE_PARTIAL_LINE;
1113 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
1114 ret = trace_seq_printf(s, " ");
1116 return TRACE_TYPE_PARTIAL_LINE;
1120 * If the return function does not have a matching entry,
1121 * then the entry was lost. Instead of just printing
1122 * the '}' and letting the user guess what function this
1123 * belongs to, write out the function name.
1126 ret = trace_seq_printf(s, "}\n");
1128 return TRACE_TYPE_PARTIAL_LINE;
1130 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1132 return TRACE_TYPE_PARTIAL_LINE;
1136 if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
1137 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
1140 return TRACE_TYPE_PARTIAL_LINE;
1143 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1145 if (ret == TRACE_TYPE_PARTIAL_LINE)
1146 return TRACE_TYPE_PARTIAL_LINE;
1148 return TRACE_TYPE_HANDLED;
1151 static enum print_line_t
1152 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1153 struct trace_iterator *iter, u32 flags)
1155 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1156 struct fgraph_data *data = iter->private;
1157 struct trace_event *event;
1163 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1165 if (print_graph_prologue(iter, s, 0, 0, flags))
1166 return TRACE_TYPE_PARTIAL_LINE;
1169 ret = print_graph_duration(DURATION_FILL_FULL, s, flags);
1170 if (ret != TRACE_TYPE_HANDLED)
1175 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1176 ret = trace_seq_printf(s, " ");
1178 return TRACE_TYPE_PARTIAL_LINE;
1182 ret = trace_seq_printf(s, "/* ");
1184 return TRACE_TYPE_PARTIAL_LINE;
1186 switch (iter->ent->type) {
1188 ret = trace_print_bprintk_msg_only(iter);
1189 if (ret != TRACE_TYPE_HANDLED)
1193 ret = trace_print_printk_msg_only(iter);
1194 if (ret != TRACE_TYPE_HANDLED)
1198 event = ftrace_find_event(ent->type);
1200 return TRACE_TYPE_UNHANDLED;
1202 ret = event->funcs->trace(iter, sym_flags, event);
1203 if (ret != TRACE_TYPE_HANDLED)
1207 /* Strip ending newline */
1208 if (s->buffer[s->len - 1] == '\n') {
1209 s->buffer[s->len - 1] = '\0';
1213 ret = trace_seq_printf(s, " */\n");
1215 return TRACE_TYPE_PARTIAL_LINE;
1217 return TRACE_TYPE_HANDLED;
1222 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1224 struct ftrace_graph_ent_entry *field;
1225 struct fgraph_data *data = iter->private;
1226 struct trace_entry *entry = iter->ent;
1227 struct trace_seq *s = &iter->seq;
1228 int cpu = iter->cpu;
1231 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1232 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1233 return TRACE_TYPE_HANDLED;
1237 * If the last output failed, there's a possibility we need
1238 * to print out the missing entry which would never go out.
1240 if (data && data->failed) {
1242 iter->cpu = data->cpu;
1243 ret = print_graph_entry(field, s, iter, flags);
1244 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1245 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1246 ret = TRACE_TYPE_NO_CONSUME;
1252 switch (entry->type) {
1253 case TRACE_GRAPH_ENT: {
1255 * print_graph_entry() may consume the current event,
1256 * thus @field may become invalid, so we need to save it.
1257 * sizeof(struct ftrace_graph_ent_entry) is very small,
1258 * it can be safely saved at the stack.
1260 struct ftrace_graph_ent_entry saved;
1261 trace_assign_type(field, entry);
1263 return print_graph_entry(&saved, s, iter, flags);
1265 case TRACE_GRAPH_RET: {
1266 struct ftrace_graph_ret_entry *field;
1267 trace_assign_type(field, entry);
1268 return print_graph_return(&field->ret, s, entry, iter, flags);
1272 /* dont trace stack and functions as comments */
1273 return TRACE_TYPE_UNHANDLED;
1276 return print_graph_comment(s, entry, iter, flags);
1279 return TRACE_TYPE_HANDLED;
1282 static enum print_line_t
1283 print_graph_function(struct trace_iterator *iter)
1285 return print_graph_function_flags(iter, tracer_flags.val);
1288 static enum print_line_t
1289 print_graph_function_event(struct trace_iterator *iter, int flags,
1290 struct trace_event *event)
1292 return print_graph_function(iter);
1295 static void print_lat_header(struct seq_file *s, u32 flags)
1297 static const char spaces[] = " " /* 16 spaces */
1299 " "; /* 17 spaces */
1302 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1304 if (flags & TRACE_GRAPH_PRINT_CPU)
1306 if (flags & TRACE_GRAPH_PRINT_PROC)
1309 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1310 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1311 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1312 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
1313 seq_printf(s, "#%.*s||| / \n", size, spaces);
1316 static void __print_graph_headers_flags(struct seq_file *s, u32 flags)
1318 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1321 print_lat_header(s, flags);
1325 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1326 seq_printf(s, " TIME ");
1327 if (flags & TRACE_GRAPH_PRINT_CPU)
1328 seq_printf(s, " CPU");
1329 if (flags & TRACE_GRAPH_PRINT_PROC)
1330 seq_printf(s, " TASK/PID ");
1332 seq_printf(s, "||||");
1333 if (flags & TRACE_GRAPH_PRINT_DURATION)
1334 seq_printf(s, " DURATION ");
1335 seq_printf(s, " FUNCTION CALLS\n");
1339 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1340 seq_printf(s, " | ");
1341 if (flags & TRACE_GRAPH_PRINT_CPU)
1342 seq_printf(s, " | ");
1343 if (flags & TRACE_GRAPH_PRINT_PROC)
1344 seq_printf(s, " | | ");
1346 seq_printf(s, "||||");
1347 if (flags & TRACE_GRAPH_PRINT_DURATION)
1348 seq_printf(s, " | | ");
1349 seq_printf(s, " | | | |\n");
1352 void print_graph_headers(struct seq_file *s)
1354 print_graph_headers_flags(s, tracer_flags.val);
1357 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1359 struct trace_iterator *iter = s->private;
1361 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
1364 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
1365 /* print nothing if the buffers are empty */
1366 if (trace_empty(iter))
1369 print_trace_header(s, iter);
1372 __print_graph_headers_flags(s, flags);
1375 void graph_trace_open(struct trace_iterator *iter)
1377 /* pid and depth on the last trace processed */
1378 struct fgraph_data *data;
1381 iter->private = NULL;
1383 data = kzalloc(sizeof(*data), GFP_KERNEL);
1387 data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1388 if (!data->cpu_data)
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);
1403 iter->private = data;
1410 pr_warning("function graph tracer: not enough memory\n");
1413 void graph_trace_close(struct trace_iterator *iter)
1415 struct fgraph_data *data = iter->private;
1418 free_percpu(data->cpu_data);
1423 static int func_graph_set_flag(u32 old_flags, u32 bit, int set)
1425 if (bit == TRACE_GRAPH_PRINT_IRQS)
1426 ftrace_graph_skip_irqs = !set;
1431 static struct trace_event_functions graph_functions = {
1432 .trace = print_graph_function_event,
1435 static struct trace_event graph_trace_entry_event = {
1436 .type = TRACE_GRAPH_ENT,
1437 .funcs = &graph_functions,
1440 static struct trace_event graph_trace_ret_event = {
1441 .type = TRACE_GRAPH_RET,
1442 .funcs = &graph_functions
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,
1465 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1471 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1483 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1486 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1489 n = sprintf(buf, "%d\n", max_depth);
1491 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1494 static const struct file_operations graph_depth_fops = {
1495 .open = tracing_open_generic,
1496 .write = graph_depth_write,
1497 .read = graph_depth_read,
1498 .llseek = generic_file_llseek,
1501 static __init int init_graph_debugfs(void)
1503 struct dentry *d_tracer;
1505 d_tracer = tracing_init_dentry();
1509 trace_create_file("max_graph_depth", 0644, d_tracer,
1510 NULL, &graph_depth_fops);
1514 fs_initcall(init_graph_debugfs);
1516 static __init int init_graph_trace(void)
1518 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1520 if (!register_ftrace_event(&graph_trace_entry_event)) {
1521 pr_warning("Warning: could not register graph trace events\n");
1525 if (!register_ftrace_event(&graph_trace_ret_event)) {
1526 pr_warning("Warning: could not register graph trace events\n");
1530 return register_tracer(&graph_trace);
1533 core_initcall(init_graph_trace);