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>
15 #include "trace_output.h"
17 struct fgraph_cpu_data
{
24 struct fgraph_cpu_data
*cpu_data
;
26 /* Place to preserve last processed entry. */
27 struct ftrace_graph_ent_entry ent
;
28 struct ftrace_graph_ret_entry ret
;
33 #define TRACE_GRAPH_INDENT 2
36 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
37 #define TRACE_GRAPH_PRINT_CPU 0x2
38 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
39 #define TRACE_GRAPH_PRINT_PROC 0x8
40 #define TRACE_GRAPH_PRINT_DURATION 0x10
41 #define TRACE_GRAPH_PRINT_ABS_TIME 0X20
43 static struct tracer_opt trace_opts
[] = {
44 /* Display overruns? (for self-debug purpose) */
45 { TRACER_OPT(funcgraph
-overrun
, TRACE_GRAPH_PRINT_OVERRUN
) },
47 { TRACER_OPT(funcgraph
-cpu
, TRACE_GRAPH_PRINT_CPU
) },
48 /* Display Overhead ? */
49 { TRACER_OPT(funcgraph
-overhead
, TRACE_GRAPH_PRINT_OVERHEAD
) },
50 /* Display proc name/pid */
51 { TRACER_OPT(funcgraph
-proc
, TRACE_GRAPH_PRINT_PROC
) },
52 /* Display duration of execution */
53 { TRACER_OPT(funcgraph
-duration
, TRACE_GRAPH_PRINT_DURATION
) },
54 /* Display absolute time of an entry */
55 { TRACER_OPT(funcgraph
-abstime
, TRACE_GRAPH_PRINT_ABS_TIME
) },
59 static struct tracer_flags tracer_flags
= {
60 /* Don't display overruns and proc by default */
61 .val
= TRACE_GRAPH_PRINT_CPU
| TRACE_GRAPH_PRINT_OVERHEAD
|
62 TRACE_GRAPH_PRINT_DURATION
,
66 static struct trace_array
*graph_array
;
69 /* Add a function return address to the trace stack on thread info.*/
71 ftrace_push_return_trace(unsigned long ret
, unsigned long func
, int *depth
,
72 unsigned long frame_pointer
)
74 unsigned long long calltime
;
77 if (!current
->ret_stack
)
81 * We must make sure the ret_stack is tested before we read
86 /* The return trace stack is full */
87 if (current
->curr_ret_stack
== FTRACE_RETFUNC_DEPTH
- 1) {
88 atomic_inc(¤t
->trace_overrun
);
92 calltime
= trace_clock_local();
94 index
= ++current
->curr_ret_stack
;
96 current
->ret_stack
[index
].ret
= ret
;
97 current
->ret_stack
[index
].func
= func
;
98 current
->ret_stack
[index
].calltime
= calltime
;
99 current
->ret_stack
[index
].subtime
= 0;
100 current
->ret_stack
[index
].fp
= frame_pointer
;
106 /* Retrieve a function return address to the trace stack on thread info.*/
108 ftrace_pop_return_trace(struct ftrace_graph_ret
*trace
, unsigned long *ret
,
109 unsigned long frame_pointer
)
113 index
= current
->curr_ret_stack
;
115 if (unlikely(index
< 0)) {
118 /* Might as well panic, otherwise we have no where to go */
119 *ret
= (unsigned long)panic
;
123 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
125 * The arch may choose to record the frame pointer used
126 * and check it here to make sure that it is what we expect it
127 * to be. If gcc does not set the place holder of the return
128 * address in the frame pointer, and does a copy instead, then
129 * the function graph trace will fail. This test detects this
132 * Currently, x86_32 with optimize for size (-Os) makes the latest
135 if (unlikely(current
->ret_stack
[index
].fp
!= frame_pointer
)) {
137 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
138 " from func %ps return to %lx\n",
139 current
->ret_stack
[index
].fp
,
141 (void *)current
->ret_stack
[index
].func
,
142 current
->ret_stack
[index
].ret
);
143 *ret
= (unsigned long)panic
;
148 *ret
= current
->ret_stack
[index
].ret
;
149 trace
->func
= current
->ret_stack
[index
].func
;
150 trace
->calltime
= current
->ret_stack
[index
].calltime
;
151 trace
->overrun
= atomic_read(¤t
->trace_overrun
);
152 trace
->depth
= index
;
156 * Send the trace to the ring-buffer.
157 * @return the original return address.
159 unsigned long ftrace_return_to_handler(unsigned long frame_pointer
)
161 struct ftrace_graph_ret trace
;
164 ftrace_pop_return_trace(&trace
, &ret
, frame_pointer
);
165 trace
.rettime
= trace_clock_local();
166 ftrace_graph_return(&trace
);
168 current
->curr_ret_stack
--;
170 if (unlikely(!ret
)) {
173 /* Might as well panic. What else to do? */
174 ret
= (unsigned long)panic
;
180 static int __trace_graph_entry(struct trace_array
*tr
,
181 struct ftrace_graph_ent
*trace
,
185 struct ftrace_event_call
*call
= &event_funcgraph_entry
;
186 struct ring_buffer_event
*event
;
187 struct ring_buffer
*buffer
= tr
->buffer
;
188 struct ftrace_graph_ent_entry
*entry
;
190 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled
))))
193 event
= trace_buffer_lock_reserve(buffer
, TRACE_GRAPH_ENT
,
194 sizeof(*entry
), flags
, pc
);
197 entry
= ring_buffer_event_data(event
);
198 entry
->graph_ent
= *trace
;
199 if (!filter_current_check_discard(buffer
, call
, entry
, event
))
200 ring_buffer_unlock_commit(buffer
, event
);
205 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
207 struct trace_array
*tr
= graph_array
;
208 struct trace_array_cpu
*data
;
218 if (!ftrace_trace_task(current
))
221 if (!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
);
234 /* Only do the atomic if it is not already set */
235 if (!test_tsk_trace_graph(current
))
236 set_tsk_trace_graph(current
);
238 atomic_dec(&data
->disabled
);
239 local_irq_restore(flags
);
244 static void __trace_graph_return(struct trace_array
*tr
,
245 struct ftrace_graph_ret
*trace
,
249 struct ftrace_event_call
*call
= &event_funcgraph_exit
;
250 struct ring_buffer_event
*event
;
251 struct ring_buffer
*buffer
= tr
->buffer
;
252 struct ftrace_graph_ret_entry
*entry
;
254 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled
))))
257 event
= trace_buffer_lock_reserve(buffer
, TRACE_GRAPH_RET
,
258 sizeof(*entry
), flags
, pc
);
261 entry
= ring_buffer_event_data(event
);
263 if (!filter_current_check_discard(buffer
, call
, entry
, event
))
264 ring_buffer_unlock_commit(buffer
, event
);
267 void trace_graph_return(struct ftrace_graph_ret
*trace
)
269 struct trace_array
*tr
= graph_array
;
270 struct trace_array_cpu
*data
;
276 local_irq_save(flags
);
277 cpu
= raw_smp_processor_id();
278 data
= tr
->data
[cpu
];
279 disabled
= atomic_inc_return(&data
->disabled
);
280 if (likely(disabled
== 1)) {
281 pc
= preempt_count();
282 __trace_graph_return(tr
, trace
, flags
, pc
);
285 clear_tsk_trace_graph(current
);
286 atomic_dec(&data
->disabled
);
287 local_irq_restore(flags
);
290 static int graph_trace_init(struct trace_array
*tr
)
295 ret
= register_ftrace_graph(&trace_graph_return
,
299 tracing_start_cmdline_record();
304 void set_graph_array(struct trace_array
*tr
)
309 static void graph_trace_reset(struct trace_array
*tr
)
311 tracing_stop_cmdline_record();
312 unregister_ftrace_graph();
315 static int max_bytes_for_cpu
;
317 static enum print_line_t
318 print_graph_cpu(struct trace_seq
*s
, int cpu
)
323 * Start with a space character - to make it stand out
324 * to the right a bit when trace output is pasted into
327 ret
= trace_seq_printf(s
, " %*d) ", max_bytes_for_cpu
, cpu
);
329 return TRACE_TYPE_PARTIAL_LINE
;
331 return TRACE_TYPE_HANDLED
;
334 #define TRACE_GRAPH_PROCINFO_LENGTH 14
336 static enum print_line_t
337 print_graph_proc(struct trace_seq
*s
, pid_t pid
)
339 char comm
[TASK_COMM_LEN
];
340 /* sign + log10(MAX_INT) + '\0' */
347 trace_find_cmdline(pid
, comm
);
349 sprintf(pid_str
, "%d", pid
);
351 /* 1 stands for the "-" character */
352 len
= strlen(comm
) + strlen(pid_str
) + 1;
354 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
355 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
357 /* First spaces to align center */
358 for (i
= 0; i
< spaces
/ 2; i
++) {
359 ret
= trace_seq_printf(s
, " ");
361 return TRACE_TYPE_PARTIAL_LINE
;
364 ret
= trace_seq_printf(s
, "%s-%s", comm
, pid_str
);
366 return TRACE_TYPE_PARTIAL_LINE
;
368 /* Last spaces to align center */
369 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++) {
370 ret
= trace_seq_printf(s
, " ");
372 return TRACE_TYPE_PARTIAL_LINE
;
374 return TRACE_TYPE_HANDLED
;
378 static enum print_line_t
379 print_graph_lat_fmt(struct trace_seq
*s
, struct trace_entry
*entry
)
381 if (!trace_seq_putc(s
, ' '))
384 return trace_print_lat_fmt(s
, entry
);
387 /* If the pid changed since the last trace, output this event */
388 static enum print_line_t
389 verif_pid(struct trace_seq
*s
, pid_t pid
, int cpu
, struct fgraph_data
*data
)
396 return TRACE_TYPE_HANDLED
;
398 last_pid
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->last_pid
);
400 if (*last_pid
== pid
)
401 return TRACE_TYPE_HANDLED
;
403 prev_pid
= *last_pid
;
407 return TRACE_TYPE_HANDLED
;
409 * Context-switch trace line:
411 ------------------------------------------
412 | 1) migration/0--1 => sshd-1755
413 ------------------------------------------
416 ret
= trace_seq_printf(s
,
417 " ------------------------------------------\n");
419 return TRACE_TYPE_PARTIAL_LINE
;
421 ret
= print_graph_cpu(s
, cpu
);
422 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
423 return TRACE_TYPE_PARTIAL_LINE
;
425 ret
= print_graph_proc(s
, prev_pid
);
426 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
427 return TRACE_TYPE_PARTIAL_LINE
;
429 ret
= trace_seq_printf(s
, " => ");
431 return TRACE_TYPE_PARTIAL_LINE
;
433 ret
= print_graph_proc(s
, pid
);
434 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
435 return TRACE_TYPE_PARTIAL_LINE
;
437 ret
= trace_seq_printf(s
,
438 "\n ------------------------------------------\n\n");
440 return TRACE_TYPE_PARTIAL_LINE
;
442 return TRACE_TYPE_HANDLED
;
445 static struct ftrace_graph_ret_entry
*
446 get_return_for_leaf(struct trace_iterator
*iter
,
447 struct ftrace_graph_ent_entry
*curr
)
449 struct fgraph_data
*data
= iter
->private;
450 struct ring_buffer_iter
*ring_iter
= NULL
;
451 struct ring_buffer_event
*event
;
452 struct ftrace_graph_ret_entry
*next
;
455 * If the previous output failed to write to the seq buffer,
456 * then we just reuse the data from before.
458 if (data
&& data
->failed
) {
463 ring_iter
= iter
->buffer_iter
[iter
->cpu
];
465 /* First peek to compare current entry and the next one */
467 event
= ring_buffer_iter_peek(ring_iter
, NULL
);
470 * We need to consume the current entry to see
473 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
474 event
= ring_buffer_peek(iter
->tr
->buffer
, iter
->cpu
,
481 next
= ring_buffer_event_data(event
);
485 * Save current and next entries for later reference
486 * if the output fails.
493 if (next
->ent
.type
!= TRACE_GRAPH_RET
)
496 if (curr
->ent
.pid
!= next
->ent
.pid
||
497 curr
->graph_ent
.func
!= next
->ret
.func
)
500 /* this is a leaf, now advance the iterator */
502 ring_buffer_read(ring_iter
, NULL
);
507 /* Signal a overhead of time execution to the output */
509 print_graph_overhead(unsigned long long duration
, struct trace_seq
*s
)
511 /* If duration disappear, we don't need anything */
512 if (!(tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
))
515 /* Non nested entry or return */
517 return trace_seq_printf(s
, " ");
519 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERHEAD
) {
520 /* Duration exceeded 100 msecs */
521 if (duration
> 100000ULL)
522 return trace_seq_printf(s
, "! ");
524 /* Duration exceeded 10 msecs */
525 if (duration
> 10000ULL)
526 return trace_seq_printf(s
, "+ ");
529 return trace_seq_printf(s
, " ");
532 static int print_graph_abs_time(u64 t
, struct trace_seq
*s
)
534 unsigned long usecs_rem
;
536 usecs_rem
= do_div(t
, NSEC_PER_SEC
);
539 return trace_seq_printf(s
, "%5lu.%06lu | ",
540 (unsigned long)t
, usecs_rem
);
543 static enum print_line_t
544 print_graph_irq(struct trace_iterator
*iter
, unsigned long addr
,
545 enum trace_type type
, int cpu
, pid_t pid
)
548 struct trace_seq
*s
= &iter
->seq
;
550 if (addr
< (unsigned long)__irqentry_text_start
||
551 addr
>= (unsigned long)__irqentry_text_end
)
552 return TRACE_TYPE_UNHANDLED
;
555 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
) {
556 ret
= print_graph_abs_time(iter
->ts
, s
);
558 return TRACE_TYPE_PARTIAL_LINE
;
562 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
563 ret
= print_graph_cpu(s
, cpu
);
564 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
565 return TRACE_TYPE_PARTIAL_LINE
;
569 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
570 ret
= print_graph_proc(s
, pid
);
571 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
572 return TRACE_TYPE_PARTIAL_LINE
;
573 ret
= trace_seq_printf(s
, " | ");
575 return TRACE_TYPE_PARTIAL_LINE
;
579 ret
= print_graph_overhead(-1, s
);
581 return TRACE_TYPE_PARTIAL_LINE
;
583 if (type
== TRACE_GRAPH_ENT
)
584 ret
= trace_seq_printf(s
, "==========>");
586 ret
= trace_seq_printf(s
, "<==========");
589 return TRACE_TYPE_PARTIAL_LINE
;
591 /* Don't close the duration column if haven't one */
592 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
593 trace_seq_printf(s
, " |");
594 ret
= trace_seq_printf(s
, "\n");
597 return TRACE_TYPE_PARTIAL_LINE
;
598 return TRACE_TYPE_HANDLED
;
602 trace_print_graph_duration(unsigned long long duration
, struct trace_seq
*s
)
604 unsigned long nsecs_rem
= do_div(duration
, 1000);
605 /* log10(ULONG_MAX) + '\0' */
611 sprintf(msecs_str
, "%lu", (unsigned long) duration
);
614 ret
= trace_seq_printf(s
, "%s", msecs_str
);
616 return TRACE_TYPE_PARTIAL_LINE
;
618 len
= strlen(msecs_str
);
620 /* Print nsecs (we don't want to exceed 7 numbers) */
622 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
623 ret
= trace_seq_printf(s
, ".%s", nsecs_str
);
625 return TRACE_TYPE_PARTIAL_LINE
;
626 len
+= strlen(nsecs_str
);
629 ret
= trace_seq_printf(s
, " us ");
631 return TRACE_TYPE_PARTIAL_LINE
;
633 /* Print remaining spaces to fit the row's width */
634 for (i
= len
; i
< 7; i
++) {
635 ret
= trace_seq_printf(s
, " ");
637 return TRACE_TYPE_PARTIAL_LINE
;
639 return TRACE_TYPE_HANDLED
;
642 static enum print_line_t
643 print_graph_duration(unsigned long long duration
, struct trace_seq
*s
)
647 ret
= trace_print_graph_duration(duration
, s
);
648 if (ret
!= TRACE_TYPE_HANDLED
)
651 ret
= trace_seq_printf(s
, "| ");
653 return TRACE_TYPE_PARTIAL_LINE
;
655 return TRACE_TYPE_HANDLED
;
658 /* Case of a leaf function on its call entry */
659 static enum print_line_t
660 print_graph_entry_leaf(struct trace_iterator
*iter
,
661 struct ftrace_graph_ent_entry
*entry
,
662 struct ftrace_graph_ret_entry
*ret_entry
, struct trace_seq
*s
)
664 struct fgraph_data
*data
= iter
->private;
665 struct ftrace_graph_ret
*graph_ret
;
666 struct ftrace_graph_ent
*call
;
667 unsigned long long duration
;
671 graph_ret
= &ret_entry
->ret
;
672 call
= &entry
->graph_ent
;
673 duration
= graph_ret
->rettime
- graph_ret
->calltime
;
677 int *depth
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->depth
);
680 * Comments display at + 1 to depth. Since
681 * this is a leaf function, keep the comments
682 * equal to this depth.
684 *depth
= call
->depth
- 1;
688 ret
= print_graph_overhead(duration
, s
);
690 return TRACE_TYPE_PARTIAL_LINE
;
693 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
694 ret
= print_graph_duration(duration
, s
);
695 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
696 return TRACE_TYPE_PARTIAL_LINE
;
700 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
701 ret
= trace_seq_printf(s
, " ");
703 return TRACE_TYPE_PARTIAL_LINE
;
706 ret
= trace_seq_printf(s
, "%ps();\n", (void *)call
->func
);
708 return TRACE_TYPE_PARTIAL_LINE
;
710 return TRACE_TYPE_HANDLED
;
713 static enum print_line_t
714 print_graph_entry_nested(struct trace_iterator
*iter
,
715 struct ftrace_graph_ent_entry
*entry
,
716 struct trace_seq
*s
, int cpu
)
718 struct ftrace_graph_ent
*call
= &entry
->graph_ent
;
719 struct fgraph_data
*data
= iter
->private;
725 int *depth
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->depth
);
727 *depth
= call
->depth
;
731 ret
= print_graph_overhead(-1, s
);
733 return TRACE_TYPE_PARTIAL_LINE
;
736 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
737 ret
= trace_seq_printf(s
, " | ");
739 return TRACE_TYPE_PARTIAL_LINE
;
743 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
744 ret
= trace_seq_printf(s
, " ");
746 return TRACE_TYPE_PARTIAL_LINE
;
749 ret
= trace_seq_printf(s
, "%ps() {\n", (void *)call
->func
);
751 return TRACE_TYPE_PARTIAL_LINE
;
754 * we already consumed the current entry to check the next one
755 * and see if this is a leaf.
757 return TRACE_TYPE_NO_CONSUME
;
760 static enum print_line_t
761 print_graph_prologue(struct trace_iterator
*iter
, struct trace_seq
*s
,
762 int type
, unsigned long addr
)
764 struct fgraph_data
*data
= iter
->private;
765 struct trace_entry
*ent
= iter
->ent
;
770 if (verif_pid(s
, ent
->pid
, cpu
, data
) == TRACE_TYPE_PARTIAL_LINE
)
771 return TRACE_TYPE_PARTIAL_LINE
;
775 ret
= print_graph_irq(iter
, addr
, type
, cpu
, ent
->pid
);
776 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
777 return TRACE_TYPE_PARTIAL_LINE
;
781 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
) {
782 ret
= print_graph_abs_time(iter
->ts
, s
);
784 return TRACE_TYPE_PARTIAL_LINE
;
788 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
789 ret
= print_graph_cpu(s
, cpu
);
790 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
791 return TRACE_TYPE_PARTIAL_LINE
;
795 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
796 ret
= print_graph_proc(s
, ent
->pid
);
797 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
798 return TRACE_TYPE_PARTIAL_LINE
;
800 ret
= trace_seq_printf(s
, " | ");
802 return TRACE_TYPE_PARTIAL_LINE
;
806 if (trace_flags
& TRACE_ITER_LATENCY_FMT
) {
807 ret
= print_graph_lat_fmt(s
, ent
);
808 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
809 return TRACE_TYPE_PARTIAL_LINE
;
815 static enum print_line_t
816 print_graph_entry(struct ftrace_graph_ent_entry
*field
, struct trace_seq
*s
,
817 struct trace_iterator
*iter
)
819 struct fgraph_data
*data
= iter
->private;
820 struct ftrace_graph_ent
*call
= &field
->graph_ent
;
821 struct ftrace_graph_ret_entry
*leaf_ret
;
822 static enum print_line_t ret
;
825 if (print_graph_prologue(iter
, s
, TRACE_GRAPH_ENT
, call
->func
))
826 return TRACE_TYPE_PARTIAL_LINE
;
828 leaf_ret
= get_return_for_leaf(iter
, field
);
830 ret
= print_graph_entry_leaf(iter
, field
, leaf_ret
, s
);
832 ret
= print_graph_entry_nested(iter
, field
, s
, cpu
);
836 * If we failed to write our output, then we need to make
837 * note of it. Because we already consumed our entry.
849 static enum print_line_t
850 print_graph_return(struct ftrace_graph_ret
*trace
, struct trace_seq
*s
,
851 struct trace_entry
*ent
, struct trace_iterator
*iter
)
853 unsigned long long duration
= trace
->rettime
- trace
->calltime
;
854 struct fgraph_data
*data
= iter
->private;
855 pid_t pid
= ent
->pid
;
862 int *depth
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->depth
);
865 * Comments display at + 1 to depth. This is the
866 * return from a function, we now want the comments
867 * to display at the same level of the bracket.
869 *depth
= trace
->depth
- 1;
872 if (print_graph_prologue(iter
, s
, 0, 0))
873 return TRACE_TYPE_PARTIAL_LINE
;
876 ret
= print_graph_overhead(duration
, s
);
878 return TRACE_TYPE_PARTIAL_LINE
;
881 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
882 ret
= print_graph_duration(duration
, s
);
883 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
884 return TRACE_TYPE_PARTIAL_LINE
;
888 for (i
= 0; i
< trace
->depth
* TRACE_GRAPH_INDENT
; i
++) {
889 ret
= trace_seq_printf(s
, " ");
891 return TRACE_TYPE_PARTIAL_LINE
;
894 ret
= trace_seq_printf(s
, "}\n");
896 return TRACE_TYPE_PARTIAL_LINE
;
899 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERRUN
) {
900 ret
= trace_seq_printf(s
, " (Overruns: %lu)\n",
903 return TRACE_TYPE_PARTIAL_LINE
;
906 ret
= print_graph_irq(iter
, trace
->func
, TRACE_GRAPH_RET
, cpu
, pid
);
907 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
908 return TRACE_TYPE_PARTIAL_LINE
;
910 return TRACE_TYPE_HANDLED
;
913 static enum print_line_t
914 print_graph_comment(struct trace_seq
*s
, struct trace_entry
*ent
,
915 struct trace_iterator
*iter
)
917 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
918 struct fgraph_data
*data
= iter
->private;
919 struct trace_event
*event
;
925 depth
= per_cpu_ptr(data
->cpu_data
, iter
->cpu
)->depth
;
927 if (print_graph_prologue(iter
, s
, 0, 0))
928 return TRACE_TYPE_PARTIAL_LINE
;
931 ret
= print_graph_overhead(-1, s
);
933 return TRACE_TYPE_PARTIAL_LINE
;
936 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
937 ret
= trace_seq_printf(s
, " | ");
939 return TRACE_TYPE_PARTIAL_LINE
;
944 for (i
= 0; i
< (depth
+ 1) * TRACE_GRAPH_INDENT
; i
++) {
945 ret
= trace_seq_printf(s
, " ");
947 return TRACE_TYPE_PARTIAL_LINE
;
951 ret
= trace_seq_printf(s
, "/* ");
953 return TRACE_TYPE_PARTIAL_LINE
;
955 switch (iter
->ent
->type
) {
957 ret
= trace_print_bprintk_msg_only(iter
);
958 if (ret
!= TRACE_TYPE_HANDLED
)
962 ret
= trace_print_printk_msg_only(iter
);
963 if (ret
!= TRACE_TYPE_HANDLED
)
967 event
= ftrace_find_event(ent
->type
);
969 return TRACE_TYPE_UNHANDLED
;
971 ret
= event
->trace(iter
, sym_flags
);
972 if (ret
!= TRACE_TYPE_HANDLED
)
976 /* Strip ending newline */
977 if (s
->buffer
[s
->len
- 1] == '\n') {
978 s
->buffer
[s
->len
- 1] = '\0';
982 ret
= trace_seq_printf(s
, " */\n");
984 return TRACE_TYPE_PARTIAL_LINE
;
986 return TRACE_TYPE_HANDLED
;
991 print_graph_function(struct trace_iterator
*iter
)
993 struct ftrace_graph_ent_entry
*field
;
994 struct fgraph_data
*data
= iter
->private;
995 struct trace_entry
*entry
= iter
->ent
;
996 struct trace_seq
*s
= &iter
->seq
;
1000 if (data
&& per_cpu_ptr(data
->cpu_data
, cpu
)->ignore
) {
1001 per_cpu_ptr(data
->cpu_data
, cpu
)->ignore
= 0;
1002 return TRACE_TYPE_HANDLED
;
1006 * If the last output failed, there's a possibility we need
1007 * to print out the missing entry which would never go out.
1009 if (data
&& data
->failed
) {
1011 iter
->cpu
= data
->cpu
;
1012 ret
= print_graph_entry(field
, s
, iter
);
1013 if (ret
== TRACE_TYPE_HANDLED
&& iter
->cpu
!= cpu
) {
1014 per_cpu_ptr(data
->cpu_data
, iter
->cpu
)->ignore
= 1;
1015 ret
= TRACE_TYPE_NO_CONSUME
;
1021 switch (entry
->type
) {
1022 case TRACE_GRAPH_ENT
: {
1024 * print_graph_entry() may consume the current event,
1025 * thus @field may become invalid, so we need to save it.
1026 * sizeof(struct ftrace_graph_ent_entry) is very small,
1027 * it can be safely saved at the stack.
1029 struct ftrace_graph_ent_entry saved
;
1030 trace_assign_type(field
, entry
);
1032 return print_graph_entry(&saved
, s
, iter
);
1034 case TRACE_GRAPH_RET
: {
1035 struct ftrace_graph_ret_entry
*field
;
1036 trace_assign_type(field
, entry
);
1037 return print_graph_return(&field
->ret
, s
, entry
, iter
);
1040 return print_graph_comment(s
, entry
, iter
);
1043 return TRACE_TYPE_HANDLED
;
1046 static void print_lat_header(struct seq_file
*s
)
1048 static const char spaces
[] = " " /* 16 spaces */
1050 " "; /* 17 spaces */
1053 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
1055 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
1057 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
1060 seq_printf(s
, "#%.*s _-----=> irqs-off \n", size
, spaces
);
1061 seq_printf(s
, "#%.*s / _----=> need-resched \n", size
, spaces
);
1062 seq_printf(s
, "#%.*s| / _---=> hardirq/softirq \n", size
, spaces
);
1063 seq_printf(s
, "#%.*s|| / _--=> preempt-depth \n", size
, spaces
);
1064 seq_printf(s
, "#%.*s||| / _-=> lock-depth \n", size
, spaces
);
1065 seq_printf(s
, "#%.*s|||| / \n", size
, spaces
);
1068 static void print_graph_headers(struct seq_file
*s
)
1070 int lat
= trace_flags
& TRACE_ITER_LATENCY_FMT
;
1073 print_lat_header(s
);
1077 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
1078 seq_printf(s
, " TIME ");
1079 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
1080 seq_printf(s
, " CPU");
1081 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
1082 seq_printf(s
, " TASK/PID ");
1084 seq_printf(s
, "|||||");
1085 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
1086 seq_printf(s
, " DURATION ");
1087 seq_printf(s
, " FUNCTION CALLS\n");
1091 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
1092 seq_printf(s
, " | ");
1093 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
1094 seq_printf(s
, " | ");
1095 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
1096 seq_printf(s
, " | | ");
1098 seq_printf(s
, "|||||");
1099 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
1100 seq_printf(s
, " | | ");
1101 seq_printf(s
, " | | | |\n");
1104 static void graph_trace_open(struct trace_iterator
*iter
)
1106 /* pid and depth on the last trace processed */
1107 struct fgraph_data
*data
;
1110 iter
->private = NULL
;
1112 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1116 data
->cpu_data
= alloc_percpu(struct fgraph_cpu_data
);
1117 if (!data
->cpu_data
)
1120 for_each_possible_cpu(cpu
) {
1121 pid_t
*pid
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->last_pid
);
1122 int *depth
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->depth
);
1123 int *ignore
= &(per_cpu_ptr(data
->cpu_data
, cpu
)->ignore
);
1129 iter
->private = data
;
1136 pr_warning("function graph tracer: not enough memory\n");
1139 static void graph_trace_close(struct trace_iterator
*iter
)
1141 struct fgraph_data
*data
= iter
->private;
1144 free_percpu(data
->cpu_data
);
1149 static struct tracer graph_trace __read_mostly
= {
1150 .name
= "function_graph",
1151 .open
= graph_trace_open
,
1152 .pipe_open
= graph_trace_open
,
1153 .close
= graph_trace_close
,
1154 .pipe_close
= graph_trace_close
,
1155 .wait_pipe
= poll_wait_pipe
,
1156 .init
= graph_trace_init
,
1157 .reset
= graph_trace_reset
,
1158 .print_line
= print_graph_function
,
1159 .print_header
= print_graph_headers
,
1160 .flags
= &tracer_flags
,
1161 #ifdef CONFIG_FTRACE_SELFTEST
1162 .selftest
= trace_selftest_startup_function_graph
,
1166 static __init
int init_graph_trace(void)
1168 max_bytes_for_cpu
= snprintf(NULL
, 0, "%d", nr_cpu_ids
- 1);
1170 return register_tracer(&graph_trace
);
1173 device_initcall(init_graph_trace
);