3 * Function graph tracer.
5 * Mostly borrowed from function tracer which
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 static bool kill_ftrace_graph;
21 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
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.
27 bool ftrace_graph_is_dead(void)
29 return kill_ftrace_graph;
33 * ftrace_graph_stop - set to permanently disable function graph tracincg
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.
40 void ftrace_graph_stop(void)
42 kill_ftrace_graph = true;
45 /* When set, irq functions will be ignored */
46 static int ftrace_graph_skip_irqs;
48 struct fgraph_cpu_data {
53 unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH];
57 struct fgraph_cpu_data __percpu *cpu_data;
59 /* Place to preserve last processed entry. */
60 struct ftrace_graph_ent_entry ent;
61 struct ftrace_graph_ret_entry ret;
66 #define TRACE_GRAPH_INDENT 2
68 static unsigned int max_depth;
70 static struct tracer_opt trace_opts[] = {
71 /* Display overruns? (for self-debug purpose) */
72 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
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) },
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,
97 static struct trace_array *graph_array;
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.
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,
111 print_graph_duration(unsigned long long duration, struct trace_seq *s,
114 /* Add a function return address to the trace stack on thread info.*/
116 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
117 unsigned long frame_pointer)
119 unsigned long long calltime;
122 if (unlikely(ftrace_graph_is_dead()))
125 if (!current->ret_stack)
129 * We must make sure the ret_stack is tested before we read
134 /* The return trace stack is full */
135 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
136 atomic_inc(¤t->trace_overrun);
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.
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.
156 if (current->curr_ret_stack < -1)
159 calltime = trace_clock_local();
161 index = ++current->curr_ret_stack;
162 if (ftrace_graph_notrace_addr(func))
163 current->curr_ret_stack -= FTRACE_NOTRACE_DEPTH;
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;
175 /* Retrieve a function return address to the trace stack on thread info.*/
177 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
178 unsigned long frame_pointer)
182 index = current->curr_ret_stack;
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().
189 * TODO: Need to check whether the stack gets corrupted.
192 index += FTRACE_NOTRACE_DEPTH;
194 if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
197 /* Might as well panic, otherwise we have no where to go */
198 *ret = (unsigned long)panic;
202 #if defined(CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST) && !defined(CC_USING_FENTRY)
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
211 * Currently, x86_32 with optimize for size (-Os) makes the latest
214 * Note, -mfentry does not use frame pointers, and this test
215 * is not needed if CC_USING_FENTRY is set.
217 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
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,
223 (void *)current->ret_stack[index].func,
224 current->ret_stack[index].ret);
225 *ret = (unsigned long)panic;
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(¤t->trace_overrun);
234 trace->depth = index;
238 * Send the trace to the ring-buffer.
239 * @return the original return address.
241 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
243 struct ftrace_graph_ret trace;
246 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
247 trace.rettime = trace_clock_local();
249 current->curr_ret_stack--;
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.
255 if (current->curr_ret_stack < -1) {
256 current->curr_ret_stack += FTRACE_NOTRACE_DEPTH;
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.
265 ftrace_graph_return(&trace);
267 if (unlikely(!ret)) {
270 /* Might as well panic. What else to do? */
271 ret = (unsigned long)panic;
277 int __trace_graph_entry(struct trace_array *tr,
278 struct ftrace_graph_ent *trace,
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;
287 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
290 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
291 sizeof(*entry), flags, pc);
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);
302 static inline int ftrace_graph_ignore_irqs(void)
304 if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
310 int trace_graph_entry(struct ftrace_graph_ent *trace)
312 struct trace_array *tr = graph_array;
313 struct trace_array_cpu *data;
320 if (!ftrace_trace_task(current))
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))
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.
336 if (ftrace_graph_notrace_addr(trace->func))
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);
350 atomic_dec(&data->disabled);
351 local_irq_restore(flags);
356 static int trace_graph_thresh_entry(struct ftrace_graph_ent *trace)
361 return trace_graph_entry(trace);
365 __trace_graph_function(struct trace_array *tr,
366 unsigned long ip, unsigned long flags, int pc)
368 u64 time = trace_clock_local();
369 struct ftrace_graph_ent ent = {
373 struct ftrace_graph_ret ret = {
380 __trace_graph_entry(tr, &ent, flags, pc);
381 __trace_graph_return(tr, &ret, flags, pc);
385 trace_graph_function(struct trace_array *tr,
386 unsigned long ip, unsigned long parent_ip,
387 unsigned long flags, int pc)
389 __trace_graph_function(tr, ip, flags, pc);
392 void __trace_graph_return(struct trace_array *tr,
393 struct ftrace_graph_ret *trace,
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;
402 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
405 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
406 sizeof(*entry), flags, pc);
409 entry = ring_buffer_event_data(event);
411 if (!call_filter_check_discard(call, entry, buffer, event))
412 __buffer_unlock_commit(buffer, event);
415 void trace_graph_return(struct ftrace_graph_ret *trace)
417 struct trace_array *tr = graph_array;
418 struct trace_array_cpu *data;
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);
432 atomic_dec(&data->disabled);
433 local_irq_restore(flags);
436 void set_graph_array(struct trace_array *tr)
440 /* Make graph_array visible before we start tracing */
445 static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
447 if (tracing_thresh &&
448 (trace->rettime - trace->calltime < tracing_thresh))
451 trace_graph_return(trace);
454 static int graph_trace_init(struct trace_array *tr)
460 ret = register_ftrace_graph(&trace_graph_thresh_return,
461 &trace_graph_thresh_entry);
463 ret = register_ftrace_graph(&trace_graph_return,
467 tracing_start_cmdline_record();
472 static void graph_trace_reset(struct trace_array *tr)
474 tracing_stop_cmdline_record();
475 unregister_ftrace_graph();
478 static int graph_trace_update_thresh(struct trace_array *tr)
480 graph_trace_reset(tr);
481 return graph_trace_init(tr);
484 static int max_bytes_for_cpu;
486 static void print_graph_cpu(struct trace_seq *s, int cpu)
489 * Start with a space character - to make it stand out
490 * to the right a bit when trace output is pasted into
493 trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
496 #define TRACE_GRAPH_PROCINFO_LENGTH 14
498 static void print_graph_proc(struct trace_seq *s, pid_t pid)
500 char comm[TASK_COMM_LEN];
501 /* sign + log10(MAX_INT) + '\0' */
507 trace_find_cmdline(pid, comm);
509 sprintf(pid_str, "%d", pid);
511 /* 1 stands for the "-" character */
512 len = strlen(comm) + strlen(pid_str) + 1;
514 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
515 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
517 /* First spaces to align center */
518 for (i = 0; i < spaces / 2; i++)
519 trace_seq_putc(s, ' ');
521 trace_seq_printf(s, "%s-%s", comm, pid_str);
523 /* Last spaces to align center */
524 for (i = 0; i < spaces - (spaces / 2); i++)
525 trace_seq_putc(s, ' ');
529 static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
531 trace_seq_putc(s, ' ');
532 trace_print_lat_fmt(s, entry);
535 /* If the pid changed since the last trace, output this event */
537 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
545 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
547 if (*last_pid == pid)
550 prev_pid = *last_pid;
556 * Context-switch trace line:
558 ------------------------------------------
559 | 1) migration/0--1 => sshd-1755
560 ------------------------------------------
563 trace_seq_puts(s, " ------------------------------------------\n");
564 print_graph_cpu(s, cpu);
565 print_graph_proc(s, prev_pid);
566 trace_seq_puts(s, " => ");
567 print_graph_proc(s, pid);
568 trace_seq_puts(s, "\n ------------------------------------------\n\n");
571 static struct ftrace_graph_ret_entry *
572 get_return_for_leaf(struct trace_iterator *iter,
573 struct ftrace_graph_ent_entry *curr)
575 struct fgraph_data *data = iter->private;
576 struct ring_buffer_iter *ring_iter = NULL;
577 struct ring_buffer_event *event;
578 struct ftrace_graph_ret_entry *next;
581 * If the previous output failed to write to the seq buffer,
582 * then we just reuse the data from before.
584 if (data && data->failed) {
589 ring_iter = trace_buffer_iter(iter, iter->cpu);
591 /* First peek to compare current entry and the next one */
593 event = ring_buffer_iter_peek(ring_iter, NULL);
596 * We need to consume the current entry to see
599 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
601 event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
608 next = ring_buffer_event_data(event);
612 * Save current and next entries for later reference
613 * if the output fails.
617 * If the next event is not a return type, then
618 * we only care about what type it is. Otherwise we can
619 * safely copy the entire event.
621 if (next->ent.type == TRACE_GRAPH_RET)
624 data->ret.ent.type = next->ent.type;
628 if (next->ent.type != TRACE_GRAPH_RET)
631 if (curr->ent.pid != next->ent.pid ||
632 curr->graph_ent.func != next->ret.func)
635 /* this is a leaf, now advance the iterator */
637 ring_buffer_read(ring_iter, NULL);
642 static void print_graph_abs_time(u64 t, struct trace_seq *s)
644 unsigned long usecs_rem;
646 usecs_rem = do_div(t, NSEC_PER_SEC);
649 trace_seq_printf(s, "%5lu.%06lu | ",
650 (unsigned long)t, usecs_rem);
654 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
655 enum trace_type type, int cpu, pid_t pid, u32 flags)
657 struct trace_seq *s = &iter->seq;
658 struct trace_entry *ent = iter->ent;
660 if (addr < (unsigned long)__irqentry_text_start ||
661 addr >= (unsigned long)__irqentry_text_end)
664 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
666 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
667 print_graph_abs_time(iter->ts, s);
670 if (flags & TRACE_GRAPH_PRINT_CPU)
671 print_graph_cpu(s, cpu);
674 if (flags & TRACE_GRAPH_PRINT_PROC) {
675 print_graph_proc(s, pid);
676 trace_seq_puts(s, " | ");
680 if (trace_flags & TRACE_ITER_LATENCY_FMT)
681 print_graph_lat_fmt(s, ent);
685 print_graph_duration(0, s, flags | FLAGS_FILL_START);
687 if (type == TRACE_GRAPH_ENT)
688 trace_seq_puts(s, "==========>");
690 trace_seq_puts(s, "<==========");
692 print_graph_duration(0, s, flags | FLAGS_FILL_END);
693 trace_seq_putc(s, '\n');
697 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
699 unsigned long nsecs_rem = do_div(duration, 1000);
700 /* log10(ULONG_MAX) + '\0' */
706 sprintf(usecs_str, "%lu", (unsigned long) duration);
709 trace_seq_printf(s, "%s", usecs_str);
711 len = strlen(usecs_str);
713 /* Print nsecs (we don't want to exceed 7 numbers) */
715 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
717 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
718 trace_seq_printf(s, ".%s", nsecs_str);
719 len += strlen(nsecs_str);
722 trace_seq_puts(s, " us ");
724 /* Print remaining spaces to fit the row's width */
725 for (i = len; i < 7; i++)
726 trace_seq_putc(s, ' ');
730 print_graph_duration(unsigned long long duration, struct trace_seq *s,
733 bool duration_printed = false;
735 if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
736 !(trace_flags & TRACE_ITER_CONTEXT_INFO))
739 /* No real adata, just filling the column with spaces */
740 switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
741 case FLAGS_FILL_FULL:
742 trace_seq_puts(s, " | ");
744 case FLAGS_FILL_START:
745 trace_seq_puts(s, " ");
748 trace_seq_puts(s, " |");
752 /* Signal a overhead of time execution to the output */
753 if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
754 /* Duration exceeded 100 usecs */
755 if (duration > 100000ULL) {
756 trace_seq_puts(s, "! ");
757 duration_printed = true;
759 /* Duration exceeded 10 usecs */
760 } else if (duration > 10000ULL) {
761 trace_seq_puts(s, "+ ");
762 duration_printed = true;
767 * If we did not exceed the duration tresholds or we dont want
768 * to print out the overhead. Either way we need to fill out the space.
770 if (!duration_printed)
771 trace_seq_puts(s, " ");
773 trace_print_graph_duration(duration, s);
774 trace_seq_puts(s, "| ");
777 /* Case of a leaf function on its call entry */
778 static enum print_line_t
779 print_graph_entry_leaf(struct trace_iterator *iter,
780 struct ftrace_graph_ent_entry *entry,
781 struct ftrace_graph_ret_entry *ret_entry,
782 struct trace_seq *s, u32 flags)
784 struct fgraph_data *data = iter->private;
785 struct ftrace_graph_ret *graph_ret;
786 struct ftrace_graph_ent *call;
787 unsigned long long duration;
790 graph_ret = &ret_entry->ret;
791 call = &entry->graph_ent;
792 duration = graph_ret->rettime - graph_ret->calltime;
795 struct fgraph_cpu_data *cpu_data;
798 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
801 * Comments display at + 1 to depth. Since
802 * this is a leaf function, keep the comments
803 * equal to this depth.
805 cpu_data->depth = call->depth - 1;
807 /* No need to keep this function around for this depth */
808 if (call->depth < FTRACE_RETFUNC_DEPTH)
809 cpu_data->enter_funcs[call->depth] = 0;
812 /* Overhead and duration */
813 print_graph_duration(duration, s, flags);
816 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
817 trace_seq_putc(s, ' ');
819 trace_seq_printf(s, "%ps();\n", (void *)call->func);
821 return trace_handle_return(s);
824 static enum print_line_t
825 print_graph_entry_nested(struct trace_iterator *iter,
826 struct ftrace_graph_ent_entry *entry,
827 struct trace_seq *s, int cpu, u32 flags)
829 struct ftrace_graph_ent *call = &entry->graph_ent;
830 struct fgraph_data *data = iter->private;
834 struct fgraph_cpu_data *cpu_data;
837 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
838 cpu_data->depth = call->depth;
840 /* Save this function pointer to see if the exit matches */
841 if (call->depth < FTRACE_RETFUNC_DEPTH)
842 cpu_data->enter_funcs[call->depth] = call->func;
846 print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
849 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
850 trace_seq_putc(s, ' ');
852 trace_seq_printf(s, "%ps() {\n", (void *)call->func);
854 if (trace_seq_has_overflowed(s))
855 return TRACE_TYPE_PARTIAL_LINE;
858 * we already consumed the current entry to check the next one
859 * and see if this is a leaf.
861 return TRACE_TYPE_NO_CONSUME;
865 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
866 int type, unsigned long addr, u32 flags)
868 struct fgraph_data *data = iter->private;
869 struct trace_entry *ent = iter->ent;
873 verif_pid(s, ent->pid, cpu, data);
877 print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
879 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
883 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
884 print_graph_abs_time(iter->ts, s);
887 if (flags & TRACE_GRAPH_PRINT_CPU)
888 print_graph_cpu(s, cpu);
891 if (flags & TRACE_GRAPH_PRINT_PROC) {
892 print_graph_proc(s, ent->pid);
893 trace_seq_puts(s, " | ");
897 if (trace_flags & TRACE_ITER_LATENCY_FMT)
898 print_graph_lat_fmt(s, ent);
904 * Entry check for irq code
907 * - we are inside irq code
908 * - we just entered irq code
911 * - funcgraph-interrupts option is set
912 * - we are not inside irq code
915 check_irq_entry(struct trace_iterator *iter, u32 flags,
916 unsigned long addr, int depth)
920 struct fgraph_data *data = iter->private;
923 * If we are either displaying irqs, or we got called as
924 * a graph event and private data does not exist,
925 * then we bypass the irq check.
927 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
931 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
934 * We are inside the irq code
939 if ((addr < (unsigned long)__irqentry_text_start) ||
940 (addr >= (unsigned long)__irqentry_text_end))
944 * We are entering irq code.
951 * Return check for irq code
954 * - we are inside irq code
955 * - we just left irq code
958 * - funcgraph-interrupts option is set
959 * - we are not inside irq code
962 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
966 struct fgraph_data *data = iter->private;
969 * If we are either displaying irqs, or we got called as
970 * a graph event and private data does not exist,
971 * then we bypass the irq check.
973 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
977 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
980 * We are not inside the irq code.
982 if (*depth_irq == -1)
986 * We are inside the irq code, and this is returning entry.
987 * Let's not trace it and clear the entry depth, since
988 * we are out of irq code.
990 * This condition ensures that we 'leave the irq code' once
991 * we are out of the entry depth. Thus protecting us from
992 * the RETURN entry loss.
994 if (*depth_irq >= depth) {
1000 * We are inside the irq code, and this is not the entry.
1005 static enum print_line_t
1006 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
1007 struct trace_iterator *iter, u32 flags)
1009 struct fgraph_data *data = iter->private;
1010 struct ftrace_graph_ent *call = &field->graph_ent;
1011 struct ftrace_graph_ret_entry *leaf_ret;
1012 static enum print_line_t ret;
1013 int cpu = iter->cpu;
1015 if (check_irq_entry(iter, flags, call->func, call->depth))
1016 return TRACE_TYPE_HANDLED;
1018 print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
1020 leaf_ret = get_return_for_leaf(iter, field);
1022 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
1024 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
1028 * If we failed to write our output, then we need to make
1029 * note of it. Because we already consumed our entry.
1041 static enum print_line_t
1042 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
1043 struct trace_entry *ent, struct trace_iterator *iter,
1046 unsigned long long duration = trace->rettime - trace->calltime;
1047 struct fgraph_data *data = iter->private;
1048 pid_t pid = ent->pid;
1049 int cpu = iter->cpu;
1053 if (check_irq_return(iter, flags, trace->depth))
1054 return TRACE_TYPE_HANDLED;
1057 struct fgraph_cpu_data *cpu_data;
1058 int cpu = iter->cpu;
1060 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1063 * Comments display at + 1 to depth. This is the
1064 * return from a function, we now want the comments
1065 * to display at the same level of the bracket.
1067 cpu_data->depth = trace->depth - 1;
1069 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1070 if (cpu_data->enter_funcs[trace->depth] != trace->func)
1072 cpu_data->enter_funcs[trace->depth] = 0;
1076 print_graph_prologue(iter, s, 0, 0, flags);
1078 /* Overhead and duration */
1079 print_graph_duration(duration, s, flags);
1082 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
1083 trace_seq_putc(s, ' ');
1086 * If the return function does not have a matching entry,
1087 * then the entry was lost. Instead of just printing
1088 * the '}' and letting the user guess what function this
1089 * belongs to, write out the function name. Always do
1090 * that if the funcgraph-tail option is enabled.
1092 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
1093 trace_seq_puts(s, "}\n");
1095 trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1098 if (flags & TRACE_GRAPH_PRINT_OVERRUN)
1099 trace_seq_printf(s, " (Overruns: %lu)\n",
1102 print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1105 return trace_handle_return(s);
1108 static enum print_line_t
1109 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1110 struct trace_iterator *iter, u32 flags)
1112 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1113 struct fgraph_data *data = iter->private;
1114 struct trace_event *event;
1120 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1122 print_graph_prologue(iter, s, 0, 0, flags);
1125 print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
1129 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
1130 trace_seq_putc(s, ' ');
1133 trace_seq_puts(s, "/* ");
1135 switch (iter->ent->type) {
1137 ret = trace_print_bprintk_msg_only(iter);
1138 if (ret != TRACE_TYPE_HANDLED)
1142 ret = trace_print_printk_msg_only(iter);
1143 if (ret != TRACE_TYPE_HANDLED)
1147 event = ftrace_find_event(ent->type);
1149 return TRACE_TYPE_UNHANDLED;
1151 ret = event->funcs->trace(iter, sym_flags, event);
1152 if (ret != TRACE_TYPE_HANDLED)
1156 if (trace_seq_has_overflowed(s))
1159 /* Strip ending newline */
1160 if (s->buffer[s->seq.len - 1] == '\n') {
1161 s->buffer[s->seq.len - 1] = '\0';
1165 trace_seq_puts(s, " */\n");
1167 return trace_handle_return(s);
1172 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1174 struct ftrace_graph_ent_entry *field;
1175 struct fgraph_data *data = iter->private;
1176 struct trace_entry *entry = iter->ent;
1177 struct trace_seq *s = &iter->seq;
1178 int cpu = iter->cpu;
1181 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1182 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1183 return TRACE_TYPE_HANDLED;
1187 * If the last output failed, there's a possibility we need
1188 * to print out the missing entry which would never go out.
1190 if (data && data->failed) {
1192 iter->cpu = data->cpu;
1193 ret = print_graph_entry(field, s, iter, flags);
1194 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1195 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1196 ret = TRACE_TYPE_NO_CONSUME;
1202 switch (entry->type) {
1203 case TRACE_GRAPH_ENT: {
1205 * print_graph_entry() may consume the current event,
1206 * thus @field may become invalid, so we need to save it.
1207 * sizeof(struct ftrace_graph_ent_entry) is very small,
1208 * it can be safely saved at the stack.
1210 struct ftrace_graph_ent_entry saved;
1211 trace_assign_type(field, entry);
1213 return print_graph_entry(&saved, s, iter, flags);
1215 case TRACE_GRAPH_RET: {
1216 struct ftrace_graph_ret_entry *field;
1217 trace_assign_type(field, entry);
1218 return print_graph_return(&field->ret, s, entry, iter, flags);
1222 /* dont trace stack and functions as comments */
1223 return TRACE_TYPE_UNHANDLED;
1226 return print_graph_comment(s, entry, iter, flags);
1229 return TRACE_TYPE_HANDLED;
1232 static enum print_line_t
1233 print_graph_function(struct trace_iterator *iter)
1235 return print_graph_function_flags(iter, tracer_flags.val);
1238 static enum print_line_t
1239 print_graph_function_event(struct trace_iterator *iter, int flags,
1240 struct trace_event *event)
1242 return print_graph_function(iter);
1245 static void print_lat_header(struct seq_file *s, u32 flags)
1247 static const char spaces[] = " " /* 16 spaces */
1249 " "; /* 17 spaces */
1252 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1254 if (flags & TRACE_GRAPH_PRINT_CPU)
1256 if (flags & TRACE_GRAPH_PRINT_PROC)
1259 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1260 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1261 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1262 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
1263 seq_printf(s, "#%.*s||| / \n", size, spaces);
1266 static void __print_graph_headers_flags(struct seq_file *s, u32 flags)
1268 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1271 print_lat_header(s, flags);
1275 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1276 seq_puts(s, " TIME ");
1277 if (flags & TRACE_GRAPH_PRINT_CPU)
1278 seq_puts(s, " CPU");
1279 if (flags & TRACE_GRAPH_PRINT_PROC)
1280 seq_puts(s, " TASK/PID ");
1282 seq_puts(s, "||||");
1283 if (flags & TRACE_GRAPH_PRINT_DURATION)
1284 seq_puts(s, " DURATION ");
1285 seq_puts(s, " FUNCTION CALLS\n");
1289 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1291 if (flags & TRACE_GRAPH_PRINT_CPU)
1293 if (flags & TRACE_GRAPH_PRINT_PROC)
1294 seq_puts(s, " | | ");
1296 seq_puts(s, "||||");
1297 if (flags & TRACE_GRAPH_PRINT_DURATION)
1298 seq_puts(s, " | | ");
1299 seq_puts(s, " | | | |\n");
1302 static void print_graph_headers(struct seq_file *s)
1304 print_graph_headers_flags(s, tracer_flags.val);
1307 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1309 struct trace_iterator *iter = s->private;
1311 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
1314 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
1315 /* print nothing if the buffers are empty */
1316 if (trace_empty(iter))
1319 print_trace_header(s, iter);
1322 __print_graph_headers_flags(s, flags);
1325 void graph_trace_open(struct trace_iterator *iter)
1327 /* pid and depth on the last trace processed */
1328 struct fgraph_data *data;
1331 iter->private = NULL;
1333 data = kzalloc(sizeof(*data), GFP_KERNEL);
1337 data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1338 if (!data->cpu_data)
1341 for_each_possible_cpu(cpu) {
1342 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1343 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1344 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1345 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1353 iter->private = data;
1360 pr_warning("function graph tracer: not enough memory\n");
1363 void graph_trace_close(struct trace_iterator *iter)
1365 struct fgraph_data *data = iter->private;
1368 free_percpu(data->cpu_data);
1374 func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1376 if (bit == TRACE_GRAPH_PRINT_IRQS)
1377 ftrace_graph_skip_irqs = !set;
1382 static struct trace_event_functions graph_functions = {
1383 .trace = print_graph_function_event,
1386 static struct trace_event graph_trace_entry_event = {
1387 .type = TRACE_GRAPH_ENT,
1388 .funcs = &graph_functions,
1391 static struct trace_event graph_trace_ret_event = {
1392 .type = TRACE_GRAPH_RET,
1393 .funcs = &graph_functions
1396 static struct tracer graph_trace __tracer_data = {
1397 .name = "function_graph",
1398 .update_thresh = graph_trace_update_thresh,
1399 .open = graph_trace_open,
1400 .pipe_open = graph_trace_open,
1401 .close = graph_trace_close,
1402 .pipe_close = graph_trace_close,
1403 .init = graph_trace_init,
1404 .reset = graph_trace_reset,
1405 .print_line = print_graph_function,
1406 .print_header = print_graph_headers,
1407 .flags = &tracer_flags,
1408 .set_flag = func_graph_set_flag,
1409 #ifdef CONFIG_FTRACE_SELFTEST
1410 .selftest = trace_selftest_startup_function_graph,
1416 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1422 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1434 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1437 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1440 n = sprintf(buf, "%d\n", max_depth);
1442 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1445 static const struct file_operations graph_depth_fops = {
1446 .open = tracing_open_generic,
1447 .write = graph_depth_write,
1448 .read = graph_depth_read,
1449 .llseek = generic_file_llseek,
1452 static __init int init_graph_debugfs(void)
1454 struct dentry *d_tracer;
1456 d_tracer = tracing_init_dentry();
1460 trace_create_file("max_graph_depth", 0644, d_tracer,
1461 NULL, &graph_depth_fops);
1465 fs_initcall(init_graph_debugfs);
1467 static __init int init_graph_trace(void)
1469 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1471 if (!register_ftrace_event(&graph_trace_entry_event)) {
1472 pr_warning("Warning: could not register graph trace events\n");
1476 if (!register_ftrace_event(&graph_trace_ret_event)) {
1477 pr_warning("Warning: could not register graph trace events\n");
1481 return register_tracer(&graph_trace);
1484 core_initcall(init_graph_trace);