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 struct tracer_opt trace_opts[] = {
51 /* Display overruns? (for self-debug purpose) */
52 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
54 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
55 /* Display Overhead ? */
56 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
57 /* Display proc name/pid */
58 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
59 /* Display duration of execution */
60 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
61 /* Display absolute time of an entry */
62 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
63 /* Display interrupts */
64 { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
68 static struct tracer_flags tracer_flags = {
69 /* Don't display overruns and proc by default */
70 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
71 TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS,
75 static struct trace_array *graph_array;
78 /* Add a function return address to the trace stack on thread info.*/
80 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
81 unsigned long frame_pointer)
83 unsigned long long calltime;
86 if (!current->ret_stack)
90 * We must make sure the ret_stack is tested before we read
95 /* The return trace stack is full */
96 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
97 atomic_inc(¤t->trace_overrun);
101 calltime = trace_clock_local();
103 index = ++current->curr_ret_stack;
105 current->ret_stack[index].ret = ret;
106 current->ret_stack[index].func = func;
107 current->ret_stack[index].calltime = calltime;
108 current->ret_stack[index].subtime = 0;
109 current->ret_stack[index].fp = frame_pointer;
115 /* Retrieve a function return address to the trace stack on thread info.*/
117 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
118 unsigned long frame_pointer)
122 index = current->curr_ret_stack;
124 if (unlikely(index < 0)) {
127 /* Might as well panic, otherwise we have no where to go */
128 *ret = (unsigned long)panic;
132 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
134 * The arch may choose to record the frame pointer used
135 * and check it here to make sure that it is what we expect it
136 * to be. If gcc does not set the place holder of the return
137 * address in the frame pointer, and does a copy instead, then
138 * the function graph trace will fail. This test detects this
141 * Currently, x86_32 with optimize for size (-Os) makes the latest
144 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
146 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
147 " from func %ps return to %lx\n",
148 current->ret_stack[index].fp,
150 (void *)current->ret_stack[index].func,
151 current->ret_stack[index].ret);
152 *ret = (unsigned long)panic;
157 *ret = current->ret_stack[index].ret;
158 trace->func = current->ret_stack[index].func;
159 trace->calltime = current->ret_stack[index].calltime;
160 trace->overrun = atomic_read(¤t->trace_overrun);
161 trace->depth = index;
165 * Send the trace to the ring-buffer.
166 * @return the original return address.
168 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
170 struct ftrace_graph_ret trace;
173 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
174 trace.rettime = trace_clock_local();
175 ftrace_graph_return(&trace);
177 current->curr_ret_stack--;
179 if (unlikely(!ret)) {
182 /* Might as well panic. What else to do? */
183 ret = (unsigned long)panic;
189 int __trace_graph_entry(struct trace_array *tr,
190 struct ftrace_graph_ent *trace,
194 struct ftrace_event_call *call = &event_funcgraph_entry;
195 struct ring_buffer_event *event;
196 struct ring_buffer *buffer = tr->buffer;
197 struct ftrace_graph_ent_entry *entry;
199 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
202 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
203 sizeof(*entry), flags, pc);
206 entry = ring_buffer_event_data(event);
207 entry->graph_ent = *trace;
208 if (!filter_current_check_discard(buffer, call, entry, event))
209 ring_buffer_unlock_commit(buffer, event);
214 static inline int ftrace_graph_ignore_irqs(void)
216 if (!ftrace_graph_skip_irqs)
222 int trace_graph_entry(struct ftrace_graph_ent *trace)
224 struct trace_array *tr = graph_array;
225 struct trace_array_cpu *data;
232 if (!ftrace_trace_task(current))
235 /* trace it when it is-nested-in or is a function enabled. */
236 if (!(trace->depth || ftrace_graph_addr(trace->func)) ||
237 ftrace_graph_ignore_irqs())
240 local_irq_save(flags);
241 cpu = raw_smp_processor_id();
242 data = tr->data[cpu];
243 disabled = atomic_inc_return(&data->disabled);
244 if (likely(disabled == 1)) {
245 pc = preempt_count();
246 ret = __trace_graph_entry(tr, trace, flags, pc);
251 atomic_dec(&data->disabled);
252 local_irq_restore(flags);
257 int trace_graph_thresh_entry(struct ftrace_graph_ent *trace)
262 return trace_graph_entry(trace);
265 void __trace_graph_return(struct trace_array *tr,
266 struct ftrace_graph_ret *trace,
270 struct ftrace_event_call *call = &event_funcgraph_exit;
271 struct ring_buffer_event *event;
272 struct ring_buffer *buffer = tr->buffer;
273 struct ftrace_graph_ret_entry *entry;
275 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
278 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
279 sizeof(*entry), flags, pc);
282 entry = ring_buffer_event_data(event);
284 if (!filter_current_check_discard(buffer, call, entry, event))
285 ring_buffer_unlock_commit(buffer, event);
288 void trace_graph_return(struct ftrace_graph_ret *trace)
290 struct trace_array *tr = graph_array;
291 struct trace_array_cpu *data;
297 local_irq_save(flags);
298 cpu = raw_smp_processor_id();
299 data = tr->data[cpu];
300 disabled = atomic_inc_return(&data->disabled);
301 if (likely(disabled == 1)) {
302 pc = preempt_count();
303 __trace_graph_return(tr, trace, flags, pc);
305 atomic_dec(&data->disabled);
306 local_irq_restore(flags);
309 void set_graph_array(struct trace_array *tr)
313 /* Make graph_array visible before we start tracing */
318 void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
320 if (tracing_thresh &&
321 (trace->rettime - trace->calltime < tracing_thresh))
324 trace_graph_return(trace);
327 static int graph_trace_init(struct trace_array *tr)
333 ret = register_ftrace_graph(&trace_graph_thresh_return,
334 &trace_graph_thresh_entry);
336 ret = register_ftrace_graph(&trace_graph_return,
340 tracing_start_cmdline_record();
345 static void graph_trace_reset(struct trace_array *tr)
347 tracing_stop_cmdline_record();
348 unregister_ftrace_graph();
351 static int max_bytes_for_cpu;
353 static enum print_line_t
354 print_graph_cpu(struct trace_seq *s, int cpu)
359 * Start with a space character - to make it stand out
360 * to the right a bit when trace output is pasted into
363 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
365 return TRACE_TYPE_PARTIAL_LINE;
367 return TRACE_TYPE_HANDLED;
370 #define TRACE_GRAPH_PROCINFO_LENGTH 14
372 static enum print_line_t
373 print_graph_proc(struct trace_seq *s, pid_t pid)
375 char comm[TASK_COMM_LEN];
376 /* sign + log10(MAX_INT) + '\0' */
383 trace_find_cmdline(pid, comm);
385 sprintf(pid_str, "%d", pid);
387 /* 1 stands for the "-" character */
388 len = strlen(comm) + strlen(pid_str) + 1;
390 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
391 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
393 /* First spaces to align center */
394 for (i = 0; i < spaces / 2; i++) {
395 ret = trace_seq_printf(s, " ");
397 return TRACE_TYPE_PARTIAL_LINE;
400 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
402 return TRACE_TYPE_PARTIAL_LINE;
404 /* Last spaces to align center */
405 for (i = 0; i < spaces - (spaces / 2); i++) {
406 ret = trace_seq_printf(s, " ");
408 return TRACE_TYPE_PARTIAL_LINE;
410 return TRACE_TYPE_HANDLED;
414 static enum print_line_t
415 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
417 if (!trace_seq_putc(s, ' '))
420 return trace_print_lat_fmt(s, entry);
423 /* If the pid changed since the last trace, output this event */
424 static enum print_line_t
425 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
432 return TRACE_TYPE_HANDLED;
434 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
436 if (*last_pid == pid)
437 return TRACE_TYPE_HANDLED;
439 prev_pid = *last_pid;
443 return TRACE_TYPE_HANDLED;
445 * Context-switch trace line:
447 ------------------------------------------
448 | 1) migration/0--1 => sshd-1755
449 ------------------------------------------
452 ret = trace_seq_printf(s,
453 " ------------------------------------------\n");
455 return TRACE_TYPE_PARTIAL_LINE;
457 ret = print_graph_cpu(s, cpu);
458 if (ret == TRACE_TYPE_PARTIAL_LINE)
459 return TRACE_TYPE_PARTIAL_LINE;
461 ret = print_graph_proc(s, prev_pid);
462 if (ret == TRACE_TYPE_PARTIAL_LINE)
463 return TRACE_TYPE_PARTIAL_LINE;
465 ret = trace_seq_printf(s, " => ");
467 return TRACE_TYPE_PARTIAL_LINE;
469 ret = print_graph_proc(s, pid);
470 if (ret == TRACE_TYPE_PARTIAL_LINE)
471 return TRACE_TYPE_PARTIAL_LINE;
473 ret = trace_seq_printf(s,
474 "\n ------------------------------------------\n\n");
476 return TRACE_TYPE_PARTIAL_LINE;
478 return TRACE_TYPE_HANDLED;
481 static struct ftrace_graph_ret_entry *
482 get_return_for_leaf(struct trace_iterator *iter,
483 struct ftrace_graph_ent_entry *curr)
485 struct fgraph_data *data = iter->private;
486 struct ring_buffer_iter *ring_iter = NULL;
487 struct ring_buffer_event *event;
488 struct ftrace_graph_ret_entry *next;
491 * If the previous output failed to write to the seq buffer,
492 * then we just reuse the data from before.
494 if (data && data->failed) {
499 ring_iter = iter->buffer_iter[iter->cpu];
501 /* First peek to compare current entry and the next one */
503 event = ring_buffer_iter_peek(ring_iter, NULL);
506 * We need to consume the current entry to see
509 ring_buffer_consume(iter->tr->buffer, iter->cpu,
511 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
518 next = ring_buffer_event_data(event);
522 * Save current and next entries for later reference
523 * if the output fails.
527 * If the next event is not a return type, then
528 * we only care about what type it is. Otherwise we can
529 * safely copy the entire event.
531 if (next->ent.type == TRACE_GRAPH_RET)
534 data->ret.ent.type = next->ent.type;
538 if (next->ent.type != TRACE_GRAPH_RET)
541 if (curr->ent.pid != next->ent.pid ||
542 curr->graph_ent.func != next->ret.func)
545 /* this is a leaf, now advance the iterator */
547 ring_buffer_read(ring_iter, NULL);
552 /* Signal a overhead of time execution to the output */
554 print_graph_overhead(unsigned long long duration, struct trace_seq *s,
557 /* If duration disappear, we don't need anything */
558 if (!(flags & TRACE_GRAPH_PRINT_DURATION))
561 /* Non nested entry or return */
563 return trace_seq_printf(s, " ");
565 if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
566 /* Duration exceeded 100 msecs */
567 if (duration > 100000ULL)
568 return trace_seq_printf(s, "! ");
570 /* Duration exceeded 10 msecs */
571 if (duration > 10000ULL)
572 return trace_seq_printf(s, "+ ");
575 return trace_seq_printf(s, " ");
578 static int print_graph_abs_time(u64 t, struct trace_seq *s)
580 unsigned long usecs_rem;
582 usecs_rem = do_div(t, NSEC_PER_SEC);
585 return trace_seq_printf(s, "%5lu.%06lu | ",
586 (unsigned long)t, usecs_rem);
589 static enum print_line_t
590 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
591 enum trace_type type, int cpu, pid_t pid, u32 flags)
594 struct trace_seq *s = &iter->seq;
596 if (addr < (unsigned long)__irqentry_text_start ||
597 addr >= (unsigned long)__irqentry_text_end)
598 return TRACE_TYPE_UNHANDLED;
601 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
602 ret = print_graph_abs_time(iter->ts, s);
604 return TRACE_TYPE_PARTIAL_LINE;
608 if (flags & TRACE_GRAPH_PRINT_CPU) {
609 ret = print_graph_cpu(s, cpu);
610 if (ret == TRACE_TYPE_PARTIAL_LINE)
611 return TRACE_TYPE_PARTIAL_LINE;
615 if (flags & TRACE_GRAPH_PRINT_PROC) {
616 ret = print_graph_proc(s, pid);
617 if (ret == TRACE_TYPE_PARTIAL_LINE)
618 return TRACE_TYPE_PARTIAL_LINE;
619 ret = trace_seq_printf(s, " | ");
621 return TRACE_TYPE_PARTIAL_LINE;
625 ret = print_graph_overhead(-1, s, flags);
627 return TRACE_TYPE_PARTIAL_LINE;
629 if (type == TRACE_GRAPH_ENT)
630 ret = trace_seq_printf(s, "==========>");
632 ret = trace_seq_printf(s, "<==========");
635 return TRACE_TYPE_PARTIAL_LINE;
637 /* Don't close the duration column if haven't one */
638 if (flags & TRACE_GRAPH_PRINT_DURATION)
639 trace_seq_printf(s, " |");
640 ret = trace_seq_printf(s, "\n");
643 return TRACE_TYPE_PARTIAL_LINE;
644 return TRACE_TYPE_HANDLED;
648 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
650 unsigned long nsecs_rem = do_div(duration, 1000);
651 /* log10(ULONG_MAX) + '\0' */
657 sprintf(msecs_str, "%lu", (unsigned long) duration);
660 ret = trace_seq_printf(s, "%s", msecs_str);
662 return TRACE_TYPE_PARTIAL_LINE;
664 len = strlen(msecs_str);
666 /* Print nsecs (we don't want to exceed 7 numbers) */
668 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
670 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
671 ret = trace_seq_printf(s, ".%s", nsecs_str);
673 return TRACE_TYPE_PARTIAL_LINE;
674 len += strlen(nsecs_str);
677 ret = trace_seq_printf(s, " us ");
679 return TRACE_TYPE_PARTIAL_LINE;
681 /* Print remaining spaces to fit the row's width */
682 for (i = len; i < 7; i++) {
683 ret = trace_seq_printf(s, " ");
685 return TRACE_TYPE_PARTIAL_LINE;
687 return TRACE_TYPE_HANDLED;
690 static enum print_line_t
691 print_graph_duration(unsigned long long duration, struct trace_seq *s)
695 ret = trace_print_graph_duration(duration, s);
696 if (ret != TRACE_TYPE_HANDLED)
699 ret = trace_seq_printf(s, "| ");
701 return TRACE_TYPE_PARTIAL_LINE;
703 return TRACE_TYPE_HANDLED;
706 /* Case of a leaf function on its call entry */
707 static enum print_line_t
708 print_graph_entry_leaf(struct trace_iterator *iter,
709 struct ftrace_graph_ent_entry *entry,
710 struct ftrace_graph_ret_entry *ret_entry,
711 struct trace_seq *s, u32 flags)
713 struct fgraph_data *data = iter->private;
714 struct ftrace_graph_ret *graph_ret;
715 struct ftrace_graph_ent *call;
716 unsigned long long duration;
720 graph_ret = &ret_entry->ret;
721 call = &entry->graph_ent;
722 duration = graph_ret->rettime - graph_ret->calltime;
725 struct fgraph_cpu_data *cpu_data;
728 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
731 * Comments display at + 1 to depth. Since
732 * this is a leaf function, keep the comments
733 * equal to this depth.
735 cpu_data->depth = call->depth - 1;
737 /* No need to keep this function around for this depth */
738 if (call->depth < FTRACE_RETFUNC_DEPTH)
739 cpu_data->enter_funcs[call->depth] = 0;
743 ret = print_graph_overhead(duration, s, flags);
745 return TRACE_TYPE_PARTIAL_LINE;
748 if (flags & TRACE_GRAPH_PRINT_DURATION) {
749 ret = print_graph_duration(duration, s);
750 if (ret == TRACE_TYPE_PARTIAL_LINE)
751 return TRACE_TYPE_PARTIAL_LINE;
755 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
756 ret = trace_seq_printf(s, " ");
758 return TRACE_TYPE_PARTIAL_LINE;
761 ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
763 return TRACE_TYPE_PARTIAL_LINE;
765 return TRACE_TYPE_HANDLED;
768 static enum print_line_t
769 print_graph_entry_nested(struct trace_iterator *iter,
770 struct ftrace_graph_ent_entry *entry,
771 struct trace_seq *s, int cpu, u32 flags)
773 struct ftrace_graph_ent *call = &entry->graph_ent;
774 struct fgraph_data *data = iter->private;
779 struct fgraph_cpu_data *cpu_data;
782 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
783 cpu_data->depth = call->depth;
785 /* Save this function pointer to see if the exit matches */
786 if (call->depth < FTRACE_RETFUNC_DEPTH)
787 cpu_data->enter_funcs[call->depth] = call->func;
791 ret = print_graph_overhead(-1, s, flags);
793 return TRACE_TYPE_PARTIAL_LINE;
796 if (flags & TRACE_GRAPH_PRINT_DURATION) {
797 ret = trace_seq_printf(s, " | ");
799 return TRACE_TYPE_PARTIAL_LINE;
803 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
804 ret = trace_seq_printf(s, " ");
806 return TRACE_TYPE_PARTIAL_LINE;
809 ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
811 return TRACE_TYPE_PARTIAL_LINE;
814 * we already consumed the current entry to check the next one
815 * and see if this is a leaf.
817 return TRACE_TYPE_NO_CONSUME;
820 static enum print_line_t
821 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
822 int type, unsigned long addr, u32 flags)
824 struct fgraph_data *data = iter->private;
825 struct trace_entry *ent = iter->ent;
830 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
831 return TRACE_TYPE_PARTIAL_LINE;
835 ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
836 if (ret == TRACE_TYPE_PARTIAL_LINE)
837 return TRACE_TYPE_PARTIAL_LINE;
841 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
842 ret = print_graph_abs_time(iter->ts, s);
844 return TRACE_TYPE_PARTIAL_LINE;
848 if (flags & TRACE_GRAPH_PRINT_CPU) {
849 ret = print_graph_cpu(s, cpu);
850 if (ret == TRACE_TYPE_PARTIAL_LINE)
851 return TRACE_TYPE_PARTIAL_LINE;
855 if (flags & TRACE_GRAPH_PRINT_PROC) {
856 ret = print_graph_proc(s, ent->pid);
857 if (ret == TRACE_TYPE_PARTIAL_LINE)
858 return TRACE_TYPE_PARTIAL_LINE;
860 ret = trace_seq_printf(s, " | ");
862 return TRACE_TYPE_PARTIAL_LINE;
866 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
867 ret = print_graph_lat_fmt(s, ent);
868 if (ret == TRACE_TYPE_PARTIAL_LINE)
869 return TRACE_TYPE_PARTIAL_LINE;
876 * Entry check for irq code
879 * - we are inside irq code
880 * - we just extered irq code
883 * - funcgraph-interrupts option is set
884 * - we are not inside irq code
887 check_irq_entry(struct trace_iterator *iter, u32 flags,
888 unsigned long addr, int depth)
891 struct fgraph_data *data = iter->private;
892 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
894 if (flags & TRACE_GRAPH_PRINT_IRQS)
898 * We are inside the irq code
903 if ((addr < (unsigned long)__irqentry_text_start) ||
904 (addr >= (unsigned long)__irqentry_text_end))
908 * We are entering irq code.
915 * Return check for irq code
918 * - we are inside irq code
919 * - we just left irq code
922 * - funcgraph-interrupts option is set
923 * - we are not inside irq code
926 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
929 struct fgraph_data *data = iter->private;
930 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
932 if (flags & TRACE_GRAPH_PRINT_IRQS)
936 * We are not inside the irq code.
938 if (*depth_irq == -1)
942 * We are inside the irq code, and this is returning entry.
943 * Let's not trace it and clear the entry depth, since
944 * we are out of irq code.
946 * This condition ensures that we 'leave the irq code' once
947 * we are out of the entry depth. Thus protecting us from
948 * the RETURN entry loss.
950 if (*depth_irq >= depth) {
956 * We are inside the irq code, and this is not the entry.
961 static enum print_line_t
962 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
963 struct trace_iterator *iter, u32 flags)
965 struct fgraph_data *data = iter->private;
966 struct ftrace_graph_ent *call = &field->graph_ent;
967 struct ftrace_graph_ret_entry *leaf_ret;
968 static enum print_line_t ret;
971 if (check_irq_entry(iter, flags, call->func, call->depth))
972 return TRACE_TYPE_HANDLED;
974 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
975 return TRACE_TYPE_PARTIAL_LINE;
977 leaf_ret = get_return_for_leaf(iter, field);
979 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
981 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
985 * If we failed to write our output, then we need to make
986 * note of it. Because we already consumed our entry.
998 static enum print_line_t
999 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
1000 struct trace_entry *ent, struct trace_iterator *iter,
1003 unsigned long long duration = trace->rettime - trace->calltime;
1004 struct fgraph_data *data = iter->private;
1005 pid_t pid = ent->pid;
1006 int cpu = iter->cpu;
1011 if (check_irq_return(iter, flags, trace->depth))
1012 return TRACE_TYPE_HANDLED;
1015 struct fgraph_cpu_data *cpu_data;
1016 int cpu = iter->cpu;
1018 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1021 * Comments display at + 1 to depth. This is the
1022 * return from a function, we now want the comments
1023 * to display at the same level of the bracket.
1025 cpu_data->depth = trace->depth - 1;
1027 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1028 if (cpu_data->enter_funcs[trace->depth] != trace->func)
1030 cpu_data->enter_funcs[trace->depth] = 0;
1034 if (print_graph_prologue(iter, s, 0, 0, flags))
1035 return TRACE_TYPE_PARTIAL_LINE;
1038 ret = print_graph_overhead(duration, s, flags);
1040 return TRACE_TYPE_PARTIAL_LINE;
1043 if (flags & TRACE_GRAPH_PRINT_DURATION) {
1044 ret = print_graph_duration(duration, s);
1045 if (ret == TRACE_TYPE_PARTIAL_LINE)
1046 return TRACE_TYPE_PARTIAL_LINE;
1050 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
1051 ret = trace_seq_printf(s, " ");
1053 return TRACE_TYPE_PARTIAL_LINE;
1057 * If the return function does not have a matching entry,
1058 * then the entry was lost. Instead of just printing
1059 * the '}' and letting the user guess what function this
1060 * belongs to, write out the function name.
1063 ret = trace_seq_printf(s, "}\n");
1065 return TRACE_TYPE_PARTIAL_LINE;
1067 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1069 return TRACE_TYPE_PARTIAL_LINE;
1073 if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
1074 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
1077 return TRACE_TYPE_PARTIAL_LINE;
1080 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1082 if (ret == TRACE_TYPE_PARTIAL_LINE)
1083 return TRACE_TYPE_PARTIAL_LINE;
1085 return TRACE_TYPE_HANDLED;
1088 static enum print_line_t
1089 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1090 struct trace_iterator *iter, u32 flags)
1092 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1093 struct fgraph_data *data = iter->private;
1094 struct trace_event *event;
1100 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1102 if (print_graph_prologue(iter, s, 0, 0, flags))
1103 return TRACE_TYPE_PARTIAL_LINE;
1106 ret = print_graph_overhead(-1, s, flags);
1108 return TRACE_TYPE_PARTIAL_LINE;
1111 if (flags & TRACE_GRAPH_PRINT_DURATION) {
1112 ret = trace_seq_printf(s, " | ");
1114 return TRACE_TYPE_PARTIAL_LINE;
1119 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1120 ret = trace_seq_printf(s, " ");
1122 return TRACE_TYPE_PARTIAL_LINE;
1126 ret = trace_seq_printf(s, "/* ");
1128 return TRACE_TYPE_PARTIAL_LINE;
1130 switch (iter->ent->type) {
1132 ret = trace_print_bprintk_msg_only(iter);
1133 if (ret != TRACE_TYPE_HANDLED)
1137 ret = trace_print_printk_msg_only(iter);
1138 if (ret != TRACE_TYPE_HANDLED)
1142 event = ftrace_find_event(ent->type);
1144 return TRACE_TYPE_UNHANDLED;
1146 ret = event->funcs->trace(iter, sym_flags, event);
1147 if (ret != TRACE_TYPE_HANDLED)
1151 /* Strip ending newline */
1152 if (s->buffer[s->len - 1] == '\n') {
1153 s->buffer[s->len - 1] = '\0';
1157 ret = trace_seq_printf(s, " */\n");
1159 return TRACE_TYPE_PARTIAL_LINE;
1161 return TRACE_TYPE_HANDLED;
1166 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1168 struct ftrace_graph_ent_entry *field;
1169 struct fgraph_data *data = iter->private;
1170 struct trace_entry *entry = iter->ent;
1171 struct trace_seq *s = &iter->seq;
1172 int cpu = iter->cpu;
1175 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1176 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1177 return TRACE_TYPE_HANDLED;
1181 * If the last output failed, there's a possibility we need
1182 * to print out the missing entry which would never go out.
1184 if (data && data->failed) {
1186 iter->cpu = data->cpu;
1187 ret = print_graph_entry(field, s, iter, flags);
1188 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1189 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1190 ret = TRACE_TYPE_NO_CONSUME;
1196 switch (entry->type) {
1197 case TRACE_GRAPH_ENT: {
1199 * print_graph_entry() may consume the current event,
1200 * thus @field may become invalid, so we need to save it.
1201 * sizeof(struct ftrace_graph_ent_entry) is very small,
1202 * it can be safely saved at the stack.
1204 struct ftrace_graph_ent_entry saved;
1205 trace_assign_type(field, entry);
1207 return print_graph_entry(&saved, s, iter, flags);
1209 case TRACE_GRAPH_RET: {
1210 struct ftrace_graph_ret_entry *field;
1211 trace_assign_type(field, entry);
1212 return print_graph_return(&field->ret, s, entry, iter, flags);
1216 /* dont trace stack and functions as comments */
1217 return TRACE_TYPE_UNHANDLED;
1220 return print_graph_comment(s, entry, iter, flags);
1223 return TRACE_TYPE_HANDLED;
1226 static enum print_line_t
1227 print_graph_function(struct trace_iterator *iter)
1229 return print_graph_function_flags(iter, tracer_flags.val);
1232 static enum print_line_t
1233 print_graph_function_event(struct trace_iterator *iter, int flags,
1234 struct trace_event *event)
1236 return print_graph_function(iter);
1239 static void print_lat_header(struct seq_file *s, u32 flags)
1241 static const char spaces[] = " " /* 16 spaces */
1243 " "; /* 17 spaces */
1246 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1248 if (flags & TRACE_GRAPH_PRINT_CPU)
1250 if (flags & TRACE_GRAPH_PRINT_PROC)
1253 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1254 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1255 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1256 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
1257 seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces);
1258 seq_printf(s, "#%.*s|||| / \n", size, spaces);
1261 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1263 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1266 print_lat_header(s, flags);
1270 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1271 seq_printf(s, " TIME ");
1272 if (flags & TRACE_GRAPH_PRINT_CPU)
1273 seq_printf(s, " CPU");
1274 if (flags & TRACE_GRAPH_PRINT_PROC)
1275 seq_printf(s, " TASK/PID ");
1277 seq_printf(s, "|||||");
1278 if (flags & TRACE_GRAPH_PRINT_DURATION)
1279 seq_printf(s, " DURATION ");
1280 seq_printf(s, " FUNCTION CALLS\n");
1284 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1285 seq_printf(s, " | ");
1286 if (flags & TRACE_GRAPH_PRINT_CPU)
1287 seq_printf(s, " | ");
1288 if (flags & TRACE_GRAPH_PRINT_PROC)
1289 seq_printf(s, " | | ");
1291 seq_printf(s, "|||||");
1292 if (flags & TRACE_GRAPH_PRINT_DURATION)
1293 seq_printf(s, " | | ");
1294 seq_printf(s, " | | | |\n");
1297 void print_graph_headers(struct seq_file *s)
1299 print_graph_headers_flags(s, tracer_flags.val);
1302 void graph_trace_open(struct trace_iterator *iter)
1304 /* pid and depth on the last trace processed */
1305 struct fgraph_data *data;
1308 iter->private = NULL;
1310 data = kzalloc(sizeof(*data), GFP_KERNEL);
1314 data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1315 if (!data->cpu_data)
1318 for_each_possible_cpu(cpu) {
1319 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1320 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1321 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1322 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1330 iter->private = data;
1337 pr_warning("function graph tracer: not enough memory\n");
1340 void graph_trace_close(struct trace_iterator *iter)
1342 struct fgraph_data *data = iter->private;
1345 free_percpu(data->cpu_data);
1350 static int func_graph_set_flag(u32 old_flags, u32 bit, int set)
1352 if (bit == TRACE_GRAPH_PRINT_IRQS)
1353 ftrace_graph_skip_irqs = !set;
1358 static struct trace_event_functions graph_functions = {
1359 .trace = print_graph_function_event,
1362 static struct trace_event graph_trace_entry_event = {
1363 .type = TRACE_GRAPH_ENT,
1364 .funcs = &graph_functions,
1367 static struct trace_event graph_trace_ret_event = {
1368 .type = TRACE_GRAPH_RET,
1369 .funcs = &graph_functions
1372 static struct tracer graph_trace __read_mostly = {
1373 .name = "function_graph",
1374 .open = graph_trace_open,
1375 .pipe_open = graph_trace_open,
1376 .close = graph_trace_close,
1377 .pipe_close = graph_trace_close,
1378 .wait_pipe = poll_wait_pipe,
1379 .init = graph_trace_init,
1380 .reset = graph_trace_reset,
1381 .print_line = print_graph_function,
1382 .print_header = print_graph_headers,
1383 .flags = &tracer_flags,
1384 .set_flag = func_graph_set_flag,
1385 #ifdef CONFIG_FTRACE_SELFTEST
1386 .selftest = trace_selftest_startup_function_graph,
1390 static __init int init_graph_trace(void)
1392 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1394 if (!register_ftrace_event(&graph_trace_entry_event)) {
1395 pr_warning("Warning: could not register graph trace events\n");
1399 if (!register_ftrace_event(&graph_trace_ret_event)) {
1400 pr_warning("Warning: could not register graph trace events\n");
1404 return register_tracer(&graph_trace);
1407 device_initcall(init_graph_trace);