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 struct fgraph_cpu_data
{
22 unsigned long enter_funcs
[FTRACE_RETFUNC_DEPTH
];
26 struct fgraph_cpu_data
*cpu_data
;
28 /* Place to preserve last processed entry. */
29 struct ftrace_graph_ent_entry ent
;
30 struct ftrace_graph_ret_entry ret
;
35 #define TRACE_GRAPH_INDENT 2
38 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
39 #define TRACE_GRAPH_PRINT_CPU 0x2
40 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
41 #define TRACE_GRAPH_PRINT_PROC 0x8
42 #define TRACE_GRAPH_PRINT_DURATION 0x10
43 #define TRACE_GRAPH_PRINT_ABS_TIME 0X20
45 static struct tracer_opt trace_opts
[] = {
46 /* Display overruns? (for self-debug purpose) */
47 { TRACER_OPT(funcgraph
-overrun
, TRACE_GRAPH_PRINT_OVERRUN
) },
49 { TRACER_OPT(funcgraph
-cpu
, TRACE_GRAPH_PRINT_CPU
) },
50 /* Display Overhead ? */
51 { TRACER_OPT(funcgraph
-overhead
, TRACE_GRAPH_PRINT_OVERHEAD
) },
52 /* Display proc name/pid */
53 { TRACER_OPT(funcgraph
-proc
, TRACE_GRAPH_PRINT_PROC
) },
54 /* Display duration of execution */
55 { TRACER_OPT(funcgraph
-duration
, TRACE_GRAPH_PRINT_DURATION
) },
56 /* Display absolute time of an entry */
57 { TRACER_OPT(funcgraph
-abstime
, TRACE_GRAPH_PRINT_ABS_TIME
) },
61 static struct tracer_flags tracer_flags
= {
62 /* Don't display overruns and proc by default */
63 .val
= TRACE_GRAPH_PRINT_CPU
| TRACE_GRAPH_PRINT_OVERHEAD
|
64 TRACE_GRAPH_PRINT_DURATION
,
68 static struct trace_array
*graph_array
;
71 /* Add a function return address to the trace stack on thread info.*/
73 ftrace_push_return_trace(unsigned long ret
, unsigned long func
, int *depth
,
74 unsigned long frame_pointer
)
76 unsigned long long calltime
;
79 if (!current
->ret_stack
)
83 * We must make sure the ret_stack is tested before we read
88 /* The return trace stack is full */
89 if (current
->curr_ret_stack
== FTRACE_RETFUNC_DEPTH
- 1) {
90 atomic_inc(¤t
->trace_overrun
);
94 calltime
= trace_clock_local();
96 index
= ++current
->curr_ret_stack
;
98 current
->ret_stack
[index
].ret
= ret
;
99 current
->ret_stack
[index
].func
= func
;
100 current
->ret_stack
[index
].calltime
= calltime
;
101 current
->ret_stack
[index
].subtime
= 0;
102 current
->ret_stack
[index
].fp
= frame_pointer
;
108 /* Retrieve a function return address to the trace stack on thread info.*/
110 ftrace_pop_return_trace(struct ftrace_graph_ret
*trace
, unsigned long *ret
,
111 unsigned long frame_pointer
)
115 index
= current
->curr_ret_stack
;
117 if (unlikely(index
< 0)) {
120 /* Might as well panic, otherwise we have no where to go */
121 *ret
= (unsigned long)panic
;
125 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
127 * The arch may choose to record the frame pointer used
128 * and check it here to make sure that it is what we expect it
129 * to be. If gcc does not set the place holder of the return
130 * address in the frame pointer, and does a copy instead, then
131 * the function graph trace will fail. This test detects this
134 * Currently, x86_32 with optimize for size (-Os) makes the latest
137 if (unlikely(current
->ret_stack
[index
].fp
!= frame_pointer
)) {
139 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
140 " from func %ps return to %lx\n",
141 current
->ret_stack
[index
].fp
,
143 (void *)current
->ret_stack
[index
].func
,
144 current
->ret_stack
[index
].ret
);
145 *ret
= (unsigned long)panic
;
150 *ret
= current
->ret_stack
[index
].ret
;
151 trace
->func
= current
->ret_stack
[index
].func
;
152 trace
->calltime
= current
->ret_stack
[index
].calltime
;
153 trace
->overrun
= atomic_read(¤t
->trace_overrun
);
154 trace
->depth
= index
;
158 * Send the trace to the ring-buffer.
159 * @return the original return address.
161 unsigned long ftrace_return_to_handler(unsigned long frame_pointer
)
163 struct ftrace_graph_ret trace
;
166 ftrace_pop_return_trace(&trace
, &ret
, frame_pointer
);
167 trace
.rettime
= trace_clock_local();
168 ftrace_graph_return(&trace
);
170 current
->curr_ret_stack
--;
172 if (unlikely(!ret
)) {
175 /* Might as well panic. What else to do? */
176 ret
= (unsigned long)panic
;
182 static int __trace_graph_entry(struct trace_array
*tr
,
183 struct ftrace_graph_ent
*trace
,
187 struct ftrace_event_call
*call
= &event_funcgraph_entry
;
188 struct ring_buffer_event
*event
;
189 struct ring_buffer
*buffer
= tr
->buffer
;
190 struct ftrace_graph_ent_entry
*entry
;
192 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
195 event
= trace_buffer_lock_reserve(buffer
, TRACE_GRAPH_ENT
,
196 sizeof(*entry
), flags
, pc
);
199 entry
= ring_buffer_event_data(event
);
200 entry
->graph_ent
= *trace
;
201 if (!filter_current_check_discard(buffer
, call
, entry
, event
))
202 ring_buffer_unlock_commit(buffer
, event
);
207 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
209 struct trace_array
*tr
= graph_array
;
210 struct trace_array_cpu
*data
;
217 if (!ftrace_trace_task(current
))
220 /* trace it when it is-nested-in or is a function enabled. */
221 if (!(trace
->depth
|| ftrace_graph_addr(trace
->func
)))
224 local_irq_save(flags
);
225 cpu
= raw_smp_processor_id();
226 data
= tr
->data
[cpu
];
227 disabled
= atomic_inc_return(&data
->disabled
);
228 if (likely(disabled
== 1)) {
229 pc
= preempt_count();
230 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
235 atomic_dec(&data
->disabled
);
236 local_irq_restore(flags
);
241 int trace_graph_thresh_entry(struct ftrace_graph_ent
*trace
)
246 return trace_graph_entry(trace
);
249 static void __trace_graph_return(struct trace_array
*tr
,
250 struct ftrace_graph_ret
*trace
,
254 struct ftrace_event_call
*call
= &event_funcgraph_exit
;
255 struct ring_buffer_event
*event
;
256 struct ring_buffer
*buffer
= tr
->buffer
;
257 struct ftrace_graph_ret_entry
*entry
;
259 if (unlikely(__this_cpu_read(ftrace_cpu_disabled
)))
262 event
= trace_buffer_lock_reserve(buffer
, TRACE_GRAPH_RET
,
263 sizeof(*entry
), flags
, pc
);
266 entry
= ring_buffer_event_data(event
);
268 if (!filter_current_check_discard(buffer
, call
, entry
, event
))
269 ring_buffer_unlock_commit(buffer
, event
);
272 void trace_graph_return(struct ftrace_graph_ret
*trace
)
274 struct trace_array
*tr
= graph_array
;
275 struct trace_array_cpu
*data
;
281 local_irq_save(flags
);
282 cpu
= raw_smp_processor_id();
283 data
= tr
->data
[cpu
];
284 disabled
= atomic_inc_return(&data
->disabled
);
285 if (likely(disabled
== 1)) {
286 pc
= preempt_count();
287 __trace_graph_return(tr
, trace
, flags
, pc
);
289 atomic_dec(&data
->disabled
);
290 local_irq_restore(flags
);
293 void set_graph_array(struct trace_array
*tr
)
297 /* Make graph_array visible before we start tracing */
302 void trace_graph_thresh_return(struct ftrace_graph_ret
*trace
)
304 if (tracing_thresh
&&
305 (trace
->rettime
- trace
->calltime
< tracing_thresh
))
308 trace_graph_return(trace
);
311 static int graph_trace_init(struct trace_array
*tr
)
317 ret
= register_ftrace_graph(&trace_graph_thresh_return
,
318 &trace_graph_thresh_entry
);
320 ret
= register_ftrace_graph(&trace_graph_return
,
324 tracing_start_cmdline_record();
329 static void graph_trace_reset(struct trace_array
*tr
)
331 tracing_stop_cmdline_record();
332 unregister_ftrace_graph();
335 static int max_bytes_for_cpu
;
337 static enum print_line_t
338 print_graph_cpu(struct trace_seq
*s
, int cpu
)
343 * Start with a space character - to make it stand out
344 * to the right a bit when trace output is pasted into
347 ret
= trace_seq_printf(s
, " %*d) ", max_bytes_for_cpu
, cpu
);
349 return TRACE_TYPE_PARTIAL_LINE
;
351 return TRACE_TYPE_HANDLED
;
354 #define TRACE_GRAPH_PROCINFO_LENGTH 14
356 static enum print_line_t
357 print_graph_proc(struct trace_seq
*s
, pid_t pid
)
359 char comm
[TASK_COMM_LEN
];
360 /* sign + log10(MAX_INT) + '\0' */
367 trace_find_cmdline(pid
, comm
);
369 sprintf(pid_str
, "%d", pid
);
371 /* 1 stands for the "-" character */
372 len
= strlen(comm
) + strlen(pid_str
) + 1;
374 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
375 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
377 /* First spaces to align center */
378 for (i
= 0; i
< spaces
/ 2; i
++) {
379 ret
= trace_seq_printf(s
, " ");
381 return TRACE_TYPE_PARTIAL_LINE
;
384 ret
= trace_seq_printf(s
, "%s-%s", comm
, pid_str
);
386 return TRACE_TYPE_PARTIAL_LINE
;
388 /* Last spaces to align center */
389 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++) {
390 ret
= trace_seq_printf(s
, " ");
392 return TRACE_TYPE_PARTIAL_LINE
;
394 return TRACE_TYPE_HANDLED
;
398 static enum print_line_t
399 print_graph_lat_fmt(struct trace_seq
*s
, struct trace_entry
*entry
)
401 if (!trace_seq_putc(s
, ' '))
404 return trace_print_lat_fmt(s
, entry
);
407 /* If the pid changed since the last trace, output this event */
408 static enum print_line_t
409 verif_pid(struct trace_seq
*s
, pid_t pid
, int cpu
, struct fgraph_data
*data
)
416 return TRACE_TYPE_HANDLED
;
418 last_pid
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->last_pid
);
420 if (*last_pid
== pid
)
421 return TRACE_TYPE_HANDLED
;
423 prev_pid
= *last_pid
;
427 return TRACE_TYPE_HANDLED
;
429 * Context-switch trace line:
431 ------------------------------------------
432 | 1) migration/0--1 => sshd-1755
433 ------------------------------------------
436 ret
= trace_seq_printf(s
,
437 " ------------------------------------------\n");
439 return TRACE_TYPE_PARTIAL_LINE
;
441 ret
= print_graph_cpu(s
, cpu
);
442 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
443 return TRACE_TYPE_PARTIAL_LINE
;
445 ret
= print_graph_proc(s
, prev_pid
);
446 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
447 return TRACE_TYPE_PARTIAL_LINE
;
449 ret
= trace_seq_printf(s
, " => ");
451 return TRACE_TYPE_PARTIAL_LINE
;
453 ret
= print_graph_proc(s
, pid
);
454 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
455 return TRACE_TYPE_PARTIAL_LINE
;
457 ret
= trace_seq_printf(s
,
458 "\n ------------------------------------------\n\n");
460 return TRACE_TYPE_PARTIAL_LINE
;
462 return TRACE_TYPE_HANDLED
;
465 static struct ftrace_graph_ret_entry
*
466 get_return_for_leaf(struct trace_iterator
*iter
,
467 struct ftrace_graph_ent_entry
*curr
)
469 struct fgraph_data
*data
= iter
->private;
470 struct ring_buffer_iter
*ring_iter
= NULL
;
471 struct ring_buffer_event
*event
;
472 struct ftrace_graph_ret_entry
*next
;
475 * If the previous output failed to write to the seq buffer,
476 * then we just reuse the data from before.
478 if (data
&& data
->failed
) {
483 ring_iter
= iter
->buffer_iter
[iter
->cpu
];
485 /* First peek to compare current entry and the next one */
487 event
= ring_buffer_iter_peek(ring_iter
, NULL
);
490 * We need to consume the current entry to see
493 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
494 event
= ring_buffer_peek(iter
->tr
->buffer
, iter
->cpu
,
501 next
= ring_buffer_event_data(event
);
505 * Save current and next entries for later reference
506 * if the output fails.
510 * If the next event is not a return type, then
511 * we only care about what type it is. Otherwise we can
512 * safely copy the entire event.
514 if (next
->ent
.type
== TRACE_GRAPH_RET
)
517 data
->ret
.ent
.type
= next
->ent
.type
;
521 if (next
->ent
.type
!= TRACE_GRAPH_RET
)
524 if (curr
->ent
.pid
!= next
->ent
.pid
||
525 curr
->graph_ent
.func
!= next
->ret
.func
)
528 /* this is a leaf, now advance the iterator */
530 ring_buffer_read(ring_iter
, NULL
);
535 /* Signal a overhead of time execution to the output */
537 print_graph_overhead(unsigned long long duration
, struct trace_seq
*s
)
539 /* If duration disappear, we don't need anything */
540 if (!(tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
))
543 /* Non nested entry or return */
545 return trace_seq_printf(s
, " ");
547 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERHEAD
) {
548 /* Duration exceeded 100 msecs */
549 if (duration
> 100000ULL)
550 return trace_seq_printf(s
, "! ");
552 /* Duration exceeded 10 msecs */
553 if (duration
> 10000ULL)
554 return trace_seq_printf(s
, "+ ");
557 return trace_seq_printf(s
, " ");
560 static int print_graph_abs_time(u64 t
, struct trace_seq
*s
)
562 unsigned long usecs_rem
;
564 usecs_rem
= do_div(t
, NSEC_PER_SEC
);
567 return trace_seq_printf(s
, "%5lu.%06lu | ",
568 (unsigned long)t
, usecs_rem
);
571 static enum print_line_t
572 print_graph_irq(struct trace_iterator
*iter
, unsigned long addr
,
573 enum trace_type type
, int cpu
, pid_t pid
)
576 struct trace_seq
*s
= &iter
->seq
;
578 if (addr
< (unsigned long)__irqentry_text_start
||
579 addr
>= (unsigned long)__irqentry_text_end
)
580 return TRACE_TYPE_UNHANDLED
;
583 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
) {
584 ret
= print_graph_abs_time(iter
->ts
, s
);
586 return TRACE_TYPE_PARTIAL_LINE
;
590 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
591 ret
= print_graph_cpu(s
, cpu
);
592 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
593 return TRACE_TYPE_PARTIAL_LINE
;
597 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
598 ret
= print_graph_proc(s
, pid
);
599 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
600 return TRACE_TYPE_PARTIAL_LINE
;
601 ret
= trace_seq_printf(s
, " | ");
603 return TRACE_TYPE_PARTIAL_LINE
;
607 ret
= print_graph_overhead(-1, s
);
609 return TRACE_TYPE_PARTIAL_LINE
;
611 if (type
== TRACE_GRAPH_ENT
)
612 ret
= trace_seq_printf(s
, "==========>");
614 ret
= trace_seq_printf(s
, "<==========");
617 return TRACE_TYPE_PARTIAL_LINE
;
619 /* Don't close the duration column if haven't one */
620 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
621 trace_seq_printf(s
, " |");
622 ret
= trace_seq_printf(s
, "\n");
625 return TRACE_TYPE_PARTIAL_LINE
;
626 return TRACE_TYPE_HANDLED
;
630 trace_print_graph_duration(unsigned long long duration
, struct trace_seq
*s
)
632 unsigned long nsecs_rem
= do_div(duration
, 1000);
633 /* log10(ULONG_MAX) + '\0' */
639 sprintf(msecs_str
, "%lu", (unsigned long) duration
);
642 ret
= trace_seq_printf(s
, "%s", msecs_str
);
644 return TRACE_TYPE_PARTIAL_LINE
;
646 len
= strlen(msecs_str
);
648 /* Print nsecs (we don't want to exceed 7 numbers) */
650 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
651 ret
= trace_seq_printf(s
, ".%s", nsecs_str
);
653 return TRACE_TYPE_PARTIAL_LINE
;
654 len
+= strlen(nsecs_str
);
657 ret
= trace_seq_printf(s
, " us ");
659 return TRACE_TYPE_PARTIAL_LINE
;
661 /* Print remaining spaces to fit the row's width */
662 for (i
= len
; i
< 7; i
++) {
663 ret
= trace_seq_printf(s
, " ");
665 return TRACE_TYPE_PARTIAL_LINE
;
667 return TRACE_TYPE_HANDLED
;
670 static enum print_line_t
671 print_graph_duration(unsigned long long duration
, struct trace_seq
*s
)
675 ret
= trace_print_graph_duration(duration
, s
);
676 if (ret
!= TRACE_TYPE_HANDLED
)
679 ret
= trace_seq_printf(s
, "| ");
681 return TRACE_TYPE_PARTIAL_LINE
;
683 return TRACE_TYPE_HANDLED
;
686 /* Case of a leaf function on its call entry */
687 static enum print_line_t
688 print_graph_entry_leaf(struct trace_iterator
*iter
,
689 struct ftrace_graph_ent_entry
*entry
,
690 struct ftrace_graph_ret_entry
*ret_entry
, struct trace_seq
*s
)
692 struct fgraph_data
*data
= iter
->private;
693 struct ftrace_graph_ret
*graph_ret
;
694 struct ftrace_graph_ent
*call
;
695 unsigned long long duration
;
699 graph_ret
= &ret_entry
->ret
;
700 call
= &entry
->graph_ent
;
701 duration
= graph_ret
->rettime
- graph_ret
->calltime
;
704 struct fgraph_cpu_data
*cpu_data
;
707 cpu_data
= per_cpu_ptr(data
->cpu_data
, cpu
);
710 * Comments display at + 1 to depth. Since
711 * this is a leaf function, keep the comments
712 * equal to this depth.
714 cpu_data
->depth
= call
->depth
- 1;
716 /* No need to keep this function around for this depth */
717 if (call
->depth
< FTRACE_RETFUNC_DEPTH
)
718 cpu_data
->enter_funcs
[call
->depth
] = 0;
722 ret
= print_graph_overhead(duration
, s
);
724 return TRACE_TYPE_PARTIAL_LINE
;
727 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
728 ret
= print_graph_duration(duration
, s
);
729 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
730 return TRACE_TYPE_PARTIAL_LINE
;
734 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
735 ret
= trace_seq_printf(s
, " ");
737 return TRACE_TYPE_PARTIAL_LINE
;
740 ret
= trace_seq_printf(s
, "%ps();\n", (void *)call
->func
);
742 return TRACE_TYPE_PARTIAL_LINE
;
744 return TRACE_TYPE_HANDLED
;
747 static enum print_line_t
748 print_graph_entry_nested(struct trace_iterator
*iter
,
749 struct ftrace_graph_ent_entry
*entry
,
750 struct trace_seq
*s
, int cpu
)
752 struct ftrace_graph_ent
*call
= &entry
->graph_ent
;
753 struct fgraph_data
*data
= iter
->private;
758 struct fgraph_cpu_data
*cpu_data
;
761 cpu_data
= per_cpu_ptr(data
->cpu_data
, cpu
);
762 cpu_data
->depth
= call
->depth
;
764 /* Save this function pointer to see if the exit matches */
765 if (call
->depth
< FTRACE_RETFUNC_DEPTH
)
766 cpu_data
->enter_funcs
[call
->depth
] = call
->func
;
770 ret
= print_graph_overhead(-1, s
);
772 return TRACE_TYPE_PARTIAL_LINE
;
775 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
776 ret
= trace_seq_printf(s
, " | ");
778 return TRACE_TYPE_PARTIAL_LINE
;
782 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
783 ret
= trace_seq_printf(s
, " ");
785 return TRACE_TYPE_PARTIAL_LINE
;
788 ret
= trace_seq_printf(s
, "%ps() {\n", (void *)call
->func
);
790 return TRACE_TYPE_PARTIAL_LINE
;
793 * we already consumed the current entry to check the next one
794 * and see if this is a leaf.
796 return TRACE_TYPE_NO_CONSUME
;
799 static enum print_line_t
800 print_graph_prologue(struct trace_iterator
*iter
, struct trace_seq
*s
,
801 int type
, unsigned long addr
)
803 struct fgraph_data
*data
= iter
->private;
804 struct trace_entry
*ent
= iter
->ent
;
809 if (verif_pid(s
, ent
->pid
, cpu
, data
) == TRACE_TYPE_PARTIAL_LINE
)
810 return TRACE_TYPE_PARTIAL_LINE
;
814 ret
= print_graph_irq(iter
, addr
, type
, cpu
, ent
->pid
);
815 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
816 return TRACE_TYPE_PARTIAL_LINE
;
820 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
) {
821 ret
= print_graph_abs_time(iter
->ts
, s
);
823 return TRACE_TYPE_PARTIAL_LINE
;
827 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
828 ret
= print_graph_cpu(s
, cpu
);
829 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
830 return TRACE_TYPE_PARTIAL_LINE
;
834 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
835 ret
= print_graph_proc(s
, ent
->pid
);
836 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
837 return TRACE_TYPE_PARTIAL_LINE
;
839 ret
= trace_seq_printf(s
, " | ");
841 return TRACE_TYPE_PARTIAL_LINE
;
845 if (trace_flags
& TRACE_ITER_LATENCY_FMT
) {
846 ret
= print_graph_lat_fmt(s
, ent
);
847 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
848 return TRACE_TYPE_PARTIAL_LINE
;
854 static enum print_line_t
855 print_graph_entry(struct ftrace_graph_ent_entry
*field
, struct trace_seq
*s
,
856 struct trace_iterator
*iter
)
858 struct fgraph_data
*data
= iter
->private;
859 struct ftrace_graph_ent
*call
= &field
->graph_ent
;
860 struct ftrace_graph_ret_entry
*leaf_ret
;
861 static enum print_line_t ret
;
864 if (print_graph_prologue(iter
, s
, TRACE_GRAPH_ENT
, call
->func
))
865 return TRACE_TYPE_PARTIAL_LINE
;
867 leaf_ret
= get_return_for_leaf(iter
, field
);
869 ret
= print_graph_entry_leaf(iter
, field
, leaf_ret
, s
);
871 ret
= print_graph_entry_nested(iter
, field
, s
, cpu
);
875 * If we failed to write our output, then we need to make
876 * note of it. Because we already consumed our entry.
888 static enum print_line_t
889 print_graph_return(struct ftrace_graph_ret
*trace
, struct trace_seq
*s
,
890 struct trace_entry
*ent
, struct trace_iterator
*iter
)
892 unsigned long long duration
= trace
->rettime
- trace
->calltime
;
893 struct fgraph_data
*data
= iter
->private;
894 pid_t pid
= ent
->pid
;
901 struct fgraph_cpu_data
*cpu_data
;
904 cpu_data
= per_cpu_ptr(data
->cpu_data
, cpu
);
907 * Comments display at + 1 to depth. This is the
908 * return from a function, we now want the comments
909 * to display at the same level of the bracket.
911 cpu_data
->depth
= trace
->depth
- 1;
913 if (trace
->depth
< FTRACE_RETFUNC_DEPTH
) {
914 if (cpu_data
->enter_funcs
[trace
->depth
] != trace
->func
)
916 cpu_data
->enter_funcs
[trace
->depth
] = 0;
920 if (print_graph_prologue(iter
, s
, 0, 0))
921 return TRACE_TYPE_PARTIAL_LINE
;
924 ret
= print_graph_overhead(duration
, s
);
926 return TRACE_TYPE_PARTIAL_LINE
;
929 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
930 ret
= print_graph_duration(duration
, s
);
931 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
932 return TRACE_TYPE_PARTIAL_LINE
;
936 for (i
= 0; i
< trace
->depth
* TRACE_GRAPH_INDENT
; i
++) {
937 ret
= trace_seq_printf(s
, " ");
939 return TRACE_TYPE_PARTIAL_LINE
;
943 * If the return function does not have a matching entry,
944 * then the entry was lost. Instead of just printing
945 * the '}' and letting the user guess what function this
946 * belongs to, write out the function name.
949 ret
= trace_seq_printf(s
, "}\n");
951 return TRACE_TYPE_PARTIAL_LINE
;
953 ret
= trace_seq_printf(s
, "} /* %ps */\n", (void *)trace
->func
);
955 return TRACE_TYPE_PARTIAL_LINE
;
959 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERRUN
) {
960 ret
= trace_seq_printf(s
, " (Overruns: %lu)\n",
963 return TRACE_TYPE_PARTIAL_LINE
;
966 ret
= print_graph_irq(iter
, trace
->func
, TRACE_GRAPH_RET
, cpu
, pid
);
967 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
968 return TRACE_TYPE_PARTIAL_LINE
;
970 return TRACE_TYPE_HANDLED
;
973 static enum print_line_t
974 print_graph_comment(struct trace_seq
*s
, struct trace_entry
*ent
,
975 struct trace_iterator
*iter
)
977 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
978 struct fgraph_data
*data
= iter
->private;
979 struct trace_event
*event
;
985 depth
= per_cpu_ptr(data
->cpu_data
, iter
->cpu
)->depth
;
987 if (print_graph_prologue(iter
, s
, 0, 0))
988 return TRACE_TYPE_PARTIAL_LINE
;
991 ret
= print_graph_overhead(-1, s
);
993 return TRACE_TYPE_PARTIAL_LINE
;
996 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
997 ret
= trace_seq_printf(s
, " | ");
999 return TRACE_TYPE_PARTIAL_LINE
;
1004 for (i
= 0; i
< (depth
+ 1) * TRACE_GRAPH_INDENT
; i
++) {
1005 ret
= trace_seq_printf(s
, " ");
1007 return TRACE_TYPE_PARTIAL_LINE
;
1011 ret
= trace_seq_printf(s
, "/* ");
1013 return TRACE_TYPE_PARTIAL_LINE
;
1015 switch (iter
->ent
->type
) {
1017 ret
= trace_print_bprintk_msg_only(iter
);
1018 if (ret
!= TRACE_TYPE_HANDLED
)
1022 ret
= trace_print_printk_msg_only(iter
);
1023 if (ret
!= TRACE_TYPE_HANDLED
)
1027 event
= ftrace_find_event(ent
->type
);
1029 return TRACE_TYPE_UNHANDLED
;
1031 ret
= event
->trace(iter
, sym_flags
);
1032 if (ret
!= TRACE_TYPE_HANDLED
)
1036 /* Strip ending newline */
1037 if (s
->buffer
[s
->len
- 1] == '\n') {
1038 s
->buffer
[s
->len
- 1] = '\0';
1042 ret
= trace_seq_printf(s
, " */\n");
1044 return TRACE_TYPE_PARTIAL_LINE
;
1046 return TRACE_TYPE_HANDLED
;
1051 print_graph_function(struct trace_iterator
*iter
)
1053 struct ftrace_graph_ent_entry
*field
;
1054 struct fgraph_data
*data
= iter
->private;
1055 struct trace_entry
*entry
= iter
->ent
;
1056 struct trace_seq
*s
= &iter
->seq
;
1057 int cpu
= iter
->cpu
;
1060 if (data
&& per_cpu_ptr(data
->cpu_data
, cpu
)->ignore
) {
1061 per_cpu_ptr(data
->cpu_data
, cpu
)->ignore
= 0;
1062 return TRACE_TYPE_HANDLED
;
1066 * If the last output failed, there's a possibility we need
1067 * to print out the missing entry which would never go out.
1069 if (data
&& data
->failed
) {
1071 iter
->cpu
= data
->cpu
;
1072 ret
= print_graph_entry(field
, s
, iter
);
1073 if (ret
== TRACE_TYPE_HANDLED
&& iter
->cpu
!= cpu
) {
1074 per_cpu_ptr(data
->cpu_data
, iter
->cpu
)->ignore
= 1;
1075 ret
= TRACE_TYPE_NO_CONSUME
;
1081 switch (entry
->type
) {
1082 case TRACE_GRAPH_ENT
: {
1084 * print_graph_entry() may consume the current event,
1085 * thus @field may become invalid, so we need to save it.
1086 * sizeof(struct ftrace_graph_ent_entry) is very small,
1087 * it can be safely saved at the stack.
1089 struct ftrace_graph_ent_entry saved
;
1090 trace_assign_type(field
, entry
);
1092 return print_graph_entry(&saved
, s
, iter
);
1094 case TRACE_GRAPH_RET
: {
1095 struct ftrace_graph_ret_entry
*field
;
1096 trace_assign_type(field
, entry
);
1097 return print_graph_return(&field
->ret
, s
, entry
, iter
);
1100 return print_graph_comment(s
, entry
, iter
);
1103 return TRACE_TYPE_HANDLED
;
1106 static void print_lat_header(struct seq_file
*s
)
1108 static const char spaces
[] = " " /* 16 spaces */
1110 " "; /* 17 spaces */
1113 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
1115 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
1117 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
1120 seq_printf(s
, "#%.*s _-----=> irqs-off \n", size
, spaces
);
1121 seq_printf(s
, "#%.*s / _----=> need-resched \n", size
, spaces
);
1122 seq_printf(s
, "#%.*s| / _---=> hardirq/softirq \n", size
, spaces
);
1123 seq_printf(s
, "#%.*s|| / _--=> preempt-depth \n", size
, spaces
);
1124 seq_printf(s
, "#%.*s||| / _-=> lock-depth \n", size
, spaces
);
1125 seq_printf(s
, "#%.*s|||| / \n", size
, spaces
);
1128 static void print_graph_headers(struct seq_file
*s
)
1130 int lat
= trace_flags
& TRACE_ITER_LATENCY_FMT
;
1133 print_lat_header(s
);
1137 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
1138 seq_printf(s
, " TIME ");
1139 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
1140 seq_printf(s
, " CPU");
1141 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
1142 seq_printf(s
, " TASK/PID ");
1144 seq_printf(s
, "|||||");
1145 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
1146 seq_printf(s
, " DURATION ");
1147 seq_printf(s
, " FUNCTION CALLS\n");
1151 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
1152 seq_printf(s
, " | ");
1153 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
1154 seq_printf(s
, " | ");
1155 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
1156 seq_printf(s
, " | | ");
1158 seq_printf(s
, "|||||");
1159 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
1160 seq_printf(s
, " | | ");
1161 seq_printf(s
, " | | | |\n");
1164 static void graph_trace_open(struct trace_iterator
*iter
)
1166 /* pid and depth on the last trace processed */
1167 struct fgraph_data
*data
;
1170 iter
->private = NULL
;
1172 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1176 data
->cpu_data
= alloc_percpu(struct fgraph_cpu_data
);
1177 if (!data
->cpu_data
)
1180 for_each_possible_cpu(cpu
) {
1181 pid_t
*pid
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->last_pid
);
1182 int *depth
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->depth
);
1183 int *ignore
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->ignore
);
1189 iter
->private = data
;
1196 pr_warning("function graph tracer: not enough memory\n");
1199 static void graph_trace_close(struct trace_iterator
*iter
)
1201 struct fgraph_data
*data
= iter
->private;
1204 free_percpu(data
->cpu_data
);
1209 static struct tracer graph_trace __read_mostly
= {
1210 .name
= "function_graph",
1211 .open
= graph_trace_open
,
1212 .pipe_open
= graph_trace_open
,
1213 .close
= graph_trace_close
,
1214 .pipe_close
= graph_trace_close
,
1215 .wait_pipe
= poll_wait_pipe
,
1216 .init
= graph_trace_init
,
1217 .reset
= graph_trace_reset
,
1218 .print_line
= print_graph_function
,
1219 .print_header
= print_graph_headers
,
1220 .flags
= &tracer_flags
,
1221 #ifdef CONFIG_FTRACE_SELFTEST
1222 .selftest
= trace_selftest_startup_function_graph
,
1226 static __init
int init_graph_trace(void)
1228 max_bytes_for_cpu
= snprintf(NULL
, 0, "%d", nr_cpu_ids
- 1);
1230 return register_tracer(&graph_trace
);
1233 device_initcall(init_graph_trace
);