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);
266 __trace_graph_function(struct trace_array *tr,
267 unsigned long ip, unsigned long flags, int pc)
269 u64 time = trace_clock_local();
270 struct ftrace_graph_ent ent = {
274 struct ftrace_graph_ret ret = {
281 __trace_graph_entry(tr, &ent, flags, pc);
282 __trace_graph_return(tr, &ret, flags, pc);
286 trace_graph_function(struct trace_array *tr,
287 unsigned long ip, unsigned long parent_ip,
288 unsigned long flags, int pc)
290 __trace_graph_function(tr, parent_ip, flags, pc);
291 __trace_graph_function(tr, ip, flags, pc);
294 void __trace_graph_return(struct trace_array *tr,
295 struct ftrace_graph_ret *trace,
299 struct ftrace_event_call *call = &event_funcgraph_exit;
300 struct ring_buffer_event *event;
301 struct ring_buffer *buffer = tr->buffer;
302 struct ftrace_graph_ret_entry *entry;
304 if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
307 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
308 sizeof(*entry), flags, pc);
311 entry = ring_buffer_event_data(event);
313 if (!filter_current_check_discard(buffer, call, entry, event))
314 ring_buffer_unlock_commit(buffer, event);
317 void trace_graph_return(struct ftrace_graph_ret *trace)
319 struct trace_array *tr = graph_array;
320 struct trace_array_cpu *data;
326 local_irq_save(flags);
327 cpu = raw_smp_processor_id();
328 data = tr->data[cpu];
329 disabled = atomic_inc_return(&data->disabled);
330 if (likely(disabled == 1)) {
331 pc = preempt_count();
332 __trace_graph_return(tr, trace, flags, pc);
334 atomic_dec(&data->disabled);
335 local_irq_restore(flags);
338 void set_graph_array(struct trace_array *tr)
342 /* Make graph_array visible before we start tracing */
347 void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
349 if (tracing_thresh &&
350 (trace->rettime - trace->calltime < tracing_thresh))
353 trace_graph_return(trace);
356 static int graph_trace_init(struct trace_array *tr)
362 ret = register_ftrace_graph(&trace_graph_thresh_return,
363 &trace_graph_thresh_entry);
365 ret = register_ftrace_graph(&trace_graph_return,
369 tracing_start_cmdline_record();
374 static void graph_trace_reset(struct trace_array *tr)
376 tracing_stop_cmdline_record();
377 unregister_ftrace_graph();
380 static int max_bytes_for_cpu;
382 static enum print_line_t
383 print_graph_cpu(struct trace_seq *s, int cpu)
388 * Start with a space character - to make it stand out
389 * to the right a bit when trace output is pasted into
392 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
394 return TRACE_TYPE_PARTIAL_LINE;
396 return TRACE_TYPE_HANDLED;
399 #define TRACE_GRAPH_PROCINFO_LENGTH 14
401 static enum print_line_t
402 print_graph_proc(struct trace_seq *s, pid_t pid)
404 char comm[TASK_COMM_LEN];
405 /* sign + log10(MAX_INT) + '\0' */
412 trace_find_cmdline(pid, comm);
414 sprintf(pid_str, "%d", pid);
416 /* 1 stands for the "-" character */
417 len = strlen(comm) + strlen(pid_str) + 1;
419 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
420 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
422 /* First spaces to align center */
423 for (i = 0; i < spaces / 2; i++) {
424 ret = trace_seq_printf(s, " ");
426 return TRACE_TYPE_PARTIAL_LINE;
429 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
431 return TRACE_TYPE_PARTIAL_LINE;
433 /* Last spaces to align center */
434 for (i = 0; i < spaces - (spaces / 2); i++) {
435 ret = trace_seq_printf(s, " ");
437 return TRACE_TYPE_PARTIAL_LINE;
439 return TRACE_TYPE_HANDLED;
443 static enum print_line_t
444 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
446 if (!trace_seq_putc(s, ' '))
449 return trace_print_lat_fmt(s, entry);
452 /* If the pid changed since the last trace, output this event */
453 static enum print_line_t
454 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
461 return TRACE_TYPE_HANDLED;
463 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
465 if (*last_pid == pid)
466 return TRACE_TYPE_HANDLED;
468 prev_pid = *last_pid;
472 return TRACE_TYPE_HANDLED;
474 * Context-switch trace line:
476 ------------------------------------------
477 | 1) migration/0--1 => sshd-1755
478 ------------------------------------------
481 ret = trace_seq_printf(s,
482 " ------------------------------------------\n");
484 return TRACE_TYPE_PARTIAL_LINE;
486 ret = print_graph_cpu(s, cpu);
487 if (ret == TRACE_TYPE_PARTIAL_LINE)
488 return TRACE_TYPE_PARTIAL_LINE;
490 ret = print_graph_proc(s, prev_pid);
491 if (ret == TRACE_TYPE_PARTIAL_LINE)
492 return TRACE_TYPE_PARTIAL_LINE;
494 ret = trace_seq_printf(s, " => ");
496 return TRACE_TYPE_PARTIAL_LINE;
498 ret = print_graph_proc(s, pid);
499 if (ret == TRACE_TYPE_PARTIAL_LINE)
500 return TRACE_TYPE_PARTIAL_LINE;
502 ret = trace_seq_printf(s,
503 "\n ------------------------------------------\n\n");
505 return TRACE_TYPE_PARTIAL_LINE;
507 return TRACE_TYPE_HANDLED;
510 static struct ftrace_graph_ret_entry *
511 get_return_for_leaf(struct trace_iterator *iter,
512 struct ftrace_graph_ent_entry *curr)
514 struct fgraph_data *data = iter->private;
515 struct ring_buffer_iter *ring_iter = NULL;
516 struct ring_buffer_event *event;
517 struct ftrace_graph_ret_entry *next;
520 * If the previous output failed to write to the seq buffer,
521 * then we just reuse the data from before.
523 if (data && data->failed) {
528 ring_iter = iter->buffer_iter[iter->cpu];
530 /* First peek to compare current entry and the next one */
532 event = ring_buffer_iter_peek(ring_iter, NULL);
535 * We need to consume the current entry to see
538 ring_buffer_consume(iter->tr->buffer, iter->cpu,
540 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
547 next = ring_buffer_event_data(event);
551 * Save current and next entries for later reference
552 * if the output fails.
556 * If the next event is not a return type, then
557 * we only care about what type it is. Otherwise we can
558 * safely copy the entire event.
560 if (next->ent.type == TRACE_GRAPH_RET)
563 data->ret.ent.type = next->ent.type;
567 if (next->ent.type != TRACE_GRAPH_RET)
570 if (curr->ent.pid != next->ent.pid ||
571 curr->graph_ent.func != next->ret.func)
574 /* this is a leaf, now advance the iterator */
576 ring_buffer_read(ring_iter, NULL);
581 /* Signal a overhead of time execution to the output */
583 print_graph_overhead(unsigned long long duration, struct trace_seq *s,
586 /* If duration disappear, we don't need anything */
587 if (!(flags & TRACE_GRAPH_PRINT_DURATION))
590 /* Non nested entry or return */
592 return trace_seq_printf(s, " ");
594 if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
595 /* Duration exceeded 100 msecs */
596 if (duration > 100000ULL)
597 return trace_seq_printf(s, "! ");
599 /* Duration exceeded 10 msecs */
600 if (duration > 10000ULL)
601 return trace_seq_printf(s, "+ ");
604 return trace_seq_printf(s, " ");
607 static int print_graph_abs_time(u64 t, struct trace_seq *s)
609 unsigned long usecs_rem;
611 usecs_rem = do_div(t, NSEC_PER_SEC);
614 return trace_seq_printf(s, "%5lu.%06lu | ",
615 (unsigned long)t, usecs_rem);
618 static enum print_line_t
619 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
620 enum trace_type type, int cpu, pid_t pid, u32 flags)
623 struct trace_seq *s = &iter->seq;
625 if (addr < (unsigned long)__irqentry_text_start ||
626 addr >= (unsigned long)__irqentry_text_end)
627 return TRACE_TYPE_UNHANDLED;
630 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
631 ret = print_graph_abs_time(iter->ts, s);
633 return TRACE_TYPE_PARTIAL_LINE;
637 if (flags & TRACE_GRAPH_PRINT_CPU) {
638 ret = print_graph_cpu(s, cpu);
639 if (ret == TRACE_TYPE_PARTIAL_LINE)
640 return TRACE_TYPE_PARTIAL_LINE;
644 if (flags & TRACE_GRAPH_PRINT_PROC) {
645 ret = print_graph_proc(s, pid);
646 if (ret == TRACE_TYPE_PARTIAL_LINE)
647 return TRACE_TYPE_PARTIAL_LINE;
648 ret = trace_seq_printf(s, " | ");
650 return TRACE_TYPE_PARTIAL_LINE;
654 ret = print_graph_overhead(-1, s, flags);
656 return TRACE_TYPE_PARTIAL_LINE;
658 if (type == TRACE_GRAPH_ENT)
659 ret = trace_seq_printf(s, "==========>");
661 ret = trace_seq_printf(s, "<==========");
664 return TRACE_TYPE_PARTIAL_LINE;
666 /* Don't close the duration column if haven't one */
667 if (flags & TRACE_GRAPH_PRINT_DURATION)
668 trace_seq_printf(s, " |");
669 ret = trace_seq_printf(s, "\n");
672 return TRACE_TYPE_PARTIAL_LINE;
673 return TRACE_TYPE_HANDLED;
677 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
679 unsigned long nsecs_rem = do_div(duration, 1000);
680 /* log10(ULONG_MAX) + '\0' */
686 sprintf(msecs_str, "%lu", (unsigned long) duration);
689 ret = trace_seq_printf(s, "%s", msecs_str);
691 return TRACE_TYPE_PARTIAL_LINE;
693 len = strlen(msecs_str);
695 /* Print nsecs (we don't want to exceed 7 numbers) */
697 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
699 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
700 ret = trace_seq_printf(s, ".%s", nsecs_str);
702 return TRACE_TYPE_PARTIAL_LINE;
703 len += strlen(nsecs_str);
706 ret = trace_seq_printf(s, " us ");
708 return TRACE_TYPE_PARTIAL_LINE;
710 /* Print remaining spaces to fit the row's width */
711 for (i = len; i < 7; i++) {
712 ret = trace_seq_printf(s, " ");
714 return TRACE_TYPE_PARTIAL_LINE;
716 return TRACE_TYPE_HANDLED;
719 static enum print_line_t
720 print_graph_duration(unsigned long long duration, struct trace_seq *s)
724 ret = trace_print_graph_duration(duration, s);
725 if (ret != TRACE_TYPE_HANDLED)
728 ret = trace_seq_printf(s, "| ");
730 return TRACE_TYPE_PARTIAL_LINE;
732 return TRACE_TYPE_HANDLED;
735 /* Case of a leaf function on its call entry */
736 static enum print_line_t
737 print_graph_entry_leaf(struct trace_iterator *iter,
738 struct ftrace_graph_ent_entry *entry,
739 struct ftrace_graph_ret_entry *ret_entry,
740 struct trace_seq *s, u32 flags)
742 struct fgraph_data *data = iter->private;
743 struct ftrace_graph_ret *graph_ret;
744 struct ftrace_graph_ent *call;
745 unsigned long long duration;
749 graph_ret = &ret_entry->ret;
750 call = &entry->graph_ent;
751 duration = graph_ret->rettime - graph_ret->calltime;
754 struct fgraph_cpu_data *cpu_data;
757 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
760 * Comments display at + 1 to depth. Since
761 * this is a leaf function, keep the comments
762 * equal to this depth.
764 cpu_data->depth = call->depth - 1;
766 /* No need to keep this function around for this depth */
767 if (call->depth < FTRACE_RETFUNC_DEPTH)
768 cpu_data->enter_funcs[call->depth] = 0;
772 ret = print_graph_overhead(duration, s, flags);
774 return TRACE_TYPE_PARTIAL_LINE;
777 if (flags & TRACE_GRAPH_PRINT_DURATION) {
778 ret = print_graph_duration(duration, s);
779 if (ret == TRACE_TYPE_PARTIAL_LINE)
780 return TRACE_TYPE_PARTIAL_LINE;
784 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
785 ret = trace_seq_printf(s, " ");
787 return TRACE_TYPE_PARTIAL_LINE;
790 ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
792 return TRACE_TYPE_PARTIAL_LINE;
794 return TRACE_TYPE_HANDLED;
797 static enum print_line_t
798 print_graph_entry_nested(struct trace_iterator *iter,
799 struct ftrace_graph_ent_entry *entry,
800 struct trace_seq *s, int cpu, u32 flags)
802 struct ftrace_graph_ent *call = &entry->graph_ent;
803 struct fgraph_data *data = iter->private;
808 struct fgraph_cpu_data *cpu_data;
811 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
812 cpu_data->depth = call->depth;
814 /* Save this function pointer to see if the exit matches */
815 if (call->depth < FTRACE_RETFUNC_DEPTH)
816 cpu_data->enter_funcs[call->depth] = call->func;
820 ret = print_graph_overhead(-1, s, flags);
822 return TRACE_TYPE_PARTIAL_LINE;
825 if (flags & TRACE_GRAPH_PRINT_DURATION) {
826 ret = trace_seq_printf(s, " | ");
828 return TRACE_TYPE_PARTIAL_LINE;
832 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
833 ret = trace_seq_printf(s, " ");
835 return TRACE_TYPE_PARTIAL_LINE;
838 ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
840 return TRACE_TYPE_PARTIAL_LINE;
843 * we already consumed the current entry to check the next one
844 * and see if this is a leaf.
846 return TRACE_TYPE_NO_CONSUME;
849 static enum print_line_t
850 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
851 int type, unsigned long addr, u32 flags)
853 struct fgraph_data *data = iter->private;
854 struct trace_entry *ent = iter->ent;
859 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
860 return TRACE_TYPE_PARTIAL_LINE;
864 ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
865 if (ret == TRACE_TYPE_PARTIAL_LINE)
866 return TRACE_TYPE_PARTIAL_LINE;
870 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
871 ret = print_graph_abs_time(iter->ts, s);
873 return TRACE_TYPE_PARTIAL_LINE;
877 if (flags & TRACE_GRAPH_PRINT_CPU) {
878 ret = print_graph_cpu(s, cpu);
879 if (ret == TRACE_TYPE_PARTIAL_LINE)
880 return TRACE_TYPE_PARTIAL_LINE;
884 if (flags & TRACE_GRAPH_PRINT_PROC) {
885 ret = print_graph_proc(s, ent->pid);
886 if (ret == TRACE_TYPE_PARTIAL_LINE)
887 return TRACE_TYPE_PARTIAL_LINE;
889 ret = trace_seq_printf(s, " | ");
891 return TRACE_TYPE_PARTIAL_LINE;
895 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
896 ret = print_graph_lat_fmt(s, ent);
897 if (ret == TRACE_TYPE_PARTIAL_LINE)
898 return TRACE_TYPE_PARTIAL_LINE;
905 * Entry check for irq code
908 * - we are inside irq code
909 * - we just extered irq code
912 * - funcgraph-interrupts option is set
913 * - we are not inside irq code
916 check_irq_entry(struct trace_iterator *iter, u32 flags,
917 unsigned long addr, int depth)
921 struct fgraph_data *data = iter->private;
924 * If we are either displaying irqs, or we got called as
925 * a graph event and private data does not exist,
926 * then we bypass the irq check.
928 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
932 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
935 * We are inside the irq code
940 if ((addr < (unsigned long)__irqentry_text_start) ||
941 (addr >= (unsigned long)__irqentry_text_end))
945 * We are entering irq code.
952 * Return check for irq code
955 * - we are inside irq code
956 * - we just left irq code
959 * - funcgraph-interrupts option is set
960 * - we are not inside irq code
963 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
967 struct fgraph_data *data = iter->private;
970 * If we are either displaying irqs, or we got called as
971 * a graph event and private data does not exist,
972 * then we bypass the irq check.
974 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
978 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
981 * We are not inside the irq code.
983 if (*depth_irq == -1)
987 * We are inside the irq code, and this is returning entry.
988 * Let's not trace it and clear the entry depth, since
989 * we are out of irq code.
991 * This condition ensures that we 'leave the irq code' once
992 * we are out of the entry depth. Thus protecting us from
993 * the RETURN entry loss.
995 if (*depth_irq >= depth) {
1001 * We are inside the irq code, and this is not the entry.
1006 static enum print_line_t
1007 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
1008 struct trace_iterator *iter, u32 flags)
1010 struct fgraph_data *data = iter->private;
1011 struct ftrace_graph_ent *call = &field->graph_ent;
1012 struct ftrace_graph_ret_entry *leaf_ret;
1013 static enum print_line_t ret;
1014 int cpu = iter->cpu;
1016 if (check_irq_entry(iter, flags, call->func, call->depth))
1017 return TRACE_TYPE_HANDLED;
1019 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
1020 return TRACE_TYPE_PARTIAL_LINE;
1022 leaf_ret = get_return_for_leaf(iter, field);
1024 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
1026 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
1030 * If we failed to write our output, then we need to make
1031 * note of it. Because we already consumed our entry.
1043 static enum print_line_t
1044 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
1045 struct trace_entry *ent, struct trace_iterator *iter,
1048 unsigned long long duration = trace->rettime - trace->calltime;
1049 struct fgraph_data *data = iter->private;
1050 pid_t pid = ent->pid;
1051 int cpu = iter->cpu;
1056 if (check_irq_return(iter, flags, trace->depth))
1057 return TRACE_TYPE_HANDLED;
1060 struct fgraph_cpu_data *cpu_data;
1061 int cpu = iter->cpu;
1063 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
1066 * Comments display at + 1 to depth. This is the
1067 * return from a function, we now want the comments
1068 * to display at the same level of the bracket.
1070 cpu_data->depth = trace->depth - 1;
1072 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1073 if (cpu_data->enter_funcs[trace->depth] != trace->func)
1075 cpu_data->enter_funcs[trace->depth] = 0;
1079 if (print_graph_prologue(iter, s, 0, 0, flags))
1080 return TRACE_TYPE_PARTIAL_LINE;
1083 ret = print_graph_overhead(duration, s, flags);
1085 return TRACE_TYPE_PARTIAL_LINE;
1088 if (flags & TRACE_GRAPH_PRINT_DURATION) {
1089 ret = print_graph_duration(duration, s);
1090 if (ret == TRACE_TYPE_PARTIAL_LINE)
1091 return TRACE_TYPE_PARTIAL_LINE;
1095 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
1096 ret = trace_seq_printf(s, " ");
1098 return TRACE_TYPE_PARTIAL_LINE;
1102 * If the return function does not have a matching entry,
1103 * then the entry was lost. Instead of just printing
1104 * the '}' and letting the user guess what function this
1105 * belongs to, write out the function name.
1108 ret = trace_seq_printf(s, "}\n");
1110 return TRACE_TYPE_PARTIAL_LINE;
1112 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
1114 return TRACE_TYPE_PARTIAL_LINE;
1118 if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
1119 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
1122 return TRACE_TYPE_PARTIAL_LINE;
1125 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1127 if (ret == TRACE_TYPE_PARTIAL_LINE)
1128 return TRACE_TYPE_PARTIAL_LINE;
1130 return TRACE_TYPE_HANDLED;
1133 static enum print_line_t
1134 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1135 struct trace_iterator *iter, u32 flags)
1137 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1138 struct fgraph_data *data = iter->private;
1139 struct trace_event *event;
1145 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
1147 if (print_graph_prologue(iter, s, 0, 0, flags))
1148 return TRACE_TYPE_PARTIAL_LINE;
1151 ret = print_graph_overhead(-1, s, flags);
1153 return TRACE_TYPE_PARTIAL_LINE;
1156 if (flags & TRACE_GRAPH_PRINT_DURATION) {
1157 ret = trace_seq_printf(s, " | ");
1159 return TRACE_TYPE_PARTIAL_LINE;
1164 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1165 ret = trace_seq_printf(s, " ");
1167 return TRACE_TYPE_PARTIAL_LINE;
1171 ret = trace_seq_printf(s, "/* ");
1173 return TRACE_TYPE_PARTIAL_LINE;
1175 switch (iter->ent->type) {
1177 ret = trace_print_bprintk_msg_only(iter);
1178 if (ret != TRACE_TYPE_HANDLED)
1182 ret = trace_print_printk_msg_only(iter);
1183 if (ret != TRACE_TYPE_HANDLED)
1187 event = ftrace_find_event(ent->type);
1189 return TRACE_TYPE_UNHANDLED;
1191 ret = event->funcs->trace(iter, sym_flags, event);
1192 if (ret != TRACE_TYPE_HANDLED)
1196 /* Strip ending newline */
1197 if (s->buffer[s->len - 1] == '\n') {
1198 s->buffer[s->len - 1] = '\0';
1202 ret = trace_seq_printf(s, " */\n");
1204 return TRACE_TYPE_PARTIAL_LINE;
1206 return TRACE_TYPE_HANDLED;
1211 __print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1213 struct ftrace_graph_ent_entry *field;
1214 struct fgraph_data *data = iter->private;
1215 struct trace_entry *entry = iter->ent;
1216 struct trace_seq *s = &iter->seq;
1217 int cpu = iter->cpu;
1220 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1221 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1222 return TRACE_TYPE_HANDLED;
1226 * If the last output failed, there's a possibility we need
1227 * to print out the missing entry which would never go out.
1229 if (data && data->failed) {
1231 iter->cpu = data->cpu;
1232 ret = print_graph_entry(field, s, iter, flags);
1233 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1234 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1235 ret = TRACE_TYPE_NO_CONSUME;
1241 switch (entry->type) {
1242 case TRACE_GRAPH_ENT: {
1244 * print_graph_entry() may consume the current event,
1245 * thus @field may become invalid, so we need to save it.
1246 * sizeof(struct ftrace_graph_ent_entry) is very small,
1247 * it can be safely saved at the stack.
1249 struct ftrace_graph_ent_entry saved;
1250 trace_assign_type(field, entry);
1252 return print_graph_entry(&saved, s, iter, flags);
1254 case TRACE_GRAPH_RET: {
1255 struct ftrace_graph_ret_entry *field;
1256 trace_assign_type(field, entry);
1257 return print_graph_return(&field->ret, s, entry, iter, flags);
1261 /* dont trace stack and functions as comments */
1262 return TRACE_TYPE_UNHANDLED;
1265 return print_graph_comment(s, entry, iter, flags);
1268 return TRACE_TYPE_HANDLED;
1271 static enum print_line_t
1272 print_graph_function(struct trace_iterator *iter)
1274 return __print_graph_function_flags(iter, tracer_flags.val);
1277 enum print_line_t print_graph_function_flags(struct trace_iterator *iter,
1280 if (trace_flags & TRACE_ITER_LATENCY_FMT)
1281 flags |= TRACE_GRAPH_PRINT_DURATION;
1283 flags |= TRACE_GRAPH_PRINT_ABS_TIME;
1285 return __print_graph_function_flags(iter, flags);
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||| / _-=> lock-depth \n", size, spaces);
1314 seq_printf(s, "#%.*s|||| / \n", size, spaces);
1317 static void __print_graph_headers_flags(struct seq_file *s, u32 flags)
1319 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1322 print_lat_header(s, flags);
1326 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1327 seq_printf(s, " TIME ");
1328 if (flags & TRACE_GRAPH_PRINT_CPU)
1329 seq_printf(s, " CPU");
1330 if (flags & TRACE_GRAPH_PRINT_PROC)
1331 seq_printf(s, " TASK/PID ");
1333 seq_printf(s, "|||||");
1334 if (flags & TRACE_GRAPH_PRINT_DURATION)
1335 seq_printf(s, " DURATION ");
1336 seq_printf(s, " FUNCTION CALLS\n");
1340 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1341 seq_printf(s, " | ");
1342 if (flags & TRACE_GRAPH_PRINT_CPU)
1343 seq_printf(s, " | ");
1344 if (flags & TRACE_GRAPH_PRINT_PROC)
1345 seq_printf(s, " | | ");
1347 seq_printf(s, "|||||");
1348 if (flags & TRACE_GRAPH_PRINT_DURATION)
1349 seq_printf(s, " | | ");
1350 seq_printf(s, " | | | |\n");
1353 void print_graph_headers(struct seq_file *s)
1355 print_graph_headers_flags(s, tracer_flags.val);
1358 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1360 struct trace_iterator *iter = s->private;
1362 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
1363 /* print nothing if the buffers are empty */
1364 if (trace_empty(iter))
1367 print_trace_header(s, iter);
1368 flags |= TRACE_GRAPH_PRINT_DURATION;
1370 flags |= TRACE_GRAPH_PRINT_ABS_TIME;
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,
1463 static __init int init_graph_trace(void)
1465 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1467 if (!register_ftrace_event(&graph_trace_entry_event)) {
1468 pr_warning("Warning: could not register graph trace events\n");
1472 if (!register_ftrace_event(&graph_trace_ret_event)) {
1473 pr_warning("Warning: could not register graph trace events\n");
1477 return register_tracer(&graph_trace);
1480 device_initcall(init_graph_trace);