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"
22 #define TRACE_GRAPH_INDENT 2
25 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
26 #define TRACE_GRAPH_PRINT_CPU 0x2
27 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
28 #define TRACE_GRAPH_PRINT_PROC 0x8
29 #define TRACE_GRAPH_PRINT_DURATION 0x10
30 #define TRACE_GRAPH_PRINT_ABS_TIME 0X20
32 static struct tracer_opt trace_opts
[] = {
33 /* Display overruns? (for self-debug purpose) */
34 { TRACER_OPT(funcgraph
-overrun
, TRACE_GRAPH_PRINT_OVERRUN
) },
36 { TRACER_OPT(funcgraph
-cpu
, TRACE_GRAPH_PRINT_CPU
) },
37 /* Display Overhead ? */
38 { TRACER_OPT(funcgraph
-overhead
, TRACE_GRAPH_PRINT_OVERHEAD
) },
39 /* Display proc name/pid */
40 { TRACER_OPT(funcgraph
-proc
, TRACE_GRAPH_PRINT_PROC
) },
41 /* Display duration of execution */
42 { TRACER_OPT(funcgraph
-duration
, TRACE_GRAPH_PRINT_DURATION
) },
43 /* Display absolute time of an entry */
44 { TRACER_OPT(funcgraph
-abstime
, TRACE_GRAPH_PRINT_ABS_TIME
) },
48 static struct tracer_flags tracer_flags
= {
49 /* Don't display overruns and proc by default */
50 .val
= TRACE_GRAPH_PRINT_CPU
| TRACE_GRAPH_PRINT_OVERHEAD
|
51 TRACE_GRAPH_PRINT_DURATION
,
55 static struct trace_array
*graph_array
;
58 /* Add a function return address to the trace stack on thread info.*/
60 ftrace_push_return_trace(unsigned long ret
, unsigned long func
, int *depth
,
61 unsigned long frame_pointer
)
63 unsigned long long calltime
;
66 if (!current
->ret_stack
)
70 * We must make sure the ret_stack is tested before we read
75 /* The return trace stack is full */
76 if (current
->curr_ret_stack
== FTRACE_RETFUNC_DEPTH
- 1) {
77 atomic_inc(¤t
->trace_overrun
);
81 calltime
= trace_clock_local();
83 index
= ++current
->curr_ret_stack
;
85 current
->ret_stack
[index
].ret
= ret
;
86 current
->ret_stack
[index
].func
= func
;
87 current
->ret_stack
[index
].calltime
= calltime
;
88 current
->ret_stack
[index
].subtime
= 0;
89 current
->ret_stack
[index
].fp
= frame_pointer
;
95 /* Retrieve a function return address to the trace stack on thread info.*/
97 ftrace_pop_return_trace(struct ftrace_graph_ret
*trace
, unsigned long *ret
,
98 unsigned long frame_pointer
)
102 index
= current
->curr_ret_stack
;
104 if (unlikely(index
< 0)) {
107 /* Might as well panic, otherwise we have no where to go */
108 *ret
= (unsigned long)panic
;
112 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
114 * The arch may choose to record the frame pointer used
115 * and check it here to make sure that it is what we expect it
116 * to be. If gcc does not set the place holder of the return
117 * address in the frame pointer, and does a copy instead, then
118 * the function graph trace will fail. This test detects this
121 * Currently, x86_32 with optimize for size (-Os) makes the latest
124 if (unlikely(current
->ret_stack
[index
].fp
!= frame_pointer
)) {
126 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
127 " from func %pF return to %lx\n",
128 current
->ret_stack
[index
].fp
,
130 (void *)current
->ret_stack
[index
].func
,
131 current
->ret_stack
[index
].ret
);
132 *ret
= (unsigned long)panic
;
137 *ret
= current
->ret_stack
[index
].ret
;
138 trace
->func
= current
->ret_stack
[index
].func
;
139 trace
->calltime
= current
->ret_stack
[index
].calltime
;
140 trace
->overrun
= atomic_read(¤t
->trace_overrun
);
141 trace
->depth
= index
;
145 * Send the trace to the ring-buffer.
146 * @return the original return address.
148 unsigned long ftrace_return_to_handler(unsigned long frame_pointer
)
150 struct ftrace_graph_ret trace
;
153 ftrace_pop_return_trace(&trace
, &ret
, frame_pointer
);
154 trace
.rettime
= trace_clock_local();
155 ftrace_graph_return(&trace
);
157 current
->curr_ret_stack
--;
159 if (unlikely(!ret
)) {
162 /* Might as well panic. What else to do? */
163 ret
= (unsigned long)panic
;
169 static int __trace_graph_entry(struct trace_array
*tr
,
170 struct ftrace_graph_ent
*trace
,
174 struct ftrace_event_call
*call
= &event_funcgraph_entry
;
175 struct ring_buffer_event
*event
;
176 struct ring_buffer
*buffer
= tr
->buffer
;
177 struct ftrace_graph_ent_entry
*entry
;
179 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
182 event
= trace_buffer_lock_reserve(buffer
, TRACE_GRAPH_ENT
,
183 sizeof(*entry
), flags
, pc
);
186 entry
= ring_buffer_event_data(event
);
187 entry
->graph_ent
= *trace
;
188 if (!filter_current_check_discard(buffer
, call
, entry
, event
))
189 ring_buffer_unlock_commit(buffer
, event
);
194 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
196 struct trace_array
*tr
= graph_array
;
197 struct trace_array_cpu
*data
;
207 if (!ftrace_trace_task(current
))
210 if (!ftrace_graph_addr(trace
->func
))
213 local_irq_save(flags
);
214 cpu
= raw_smp_processor_id();
215 data
= tr
->data
[cpu
];
216 disabled
= atomic_inc_return(&data
->disabled
);
217 if (likely(disabled
== 1)) {
218 pc
= preempt_count();
219 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
223 /* Only do the atomic if it is not already set */
224 if (!test_tsk_trace_graph(current
))
225 set_tsk_trace_graph(current
);
227 atomic_dec(&data
->disabled
);
228 local_irq_restore(flags
);
233 static void __trace_graph_return(struct trace_array
*tr
,
234 struct ftrace_graph_ret
*trace
,
238 struct ftrace_event_call
*call
= &event_funcgraph_exit
;
239 struct ring_buffer_event
*event
;
240 struct ring_buffer
*buffer
= tr
->buffer
;
241 struct ftrace_graph_ret_entry
*entry
;
243 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
246 event
= trace_buffer_lock_reserve(buffer
, TRACE_GRAPH_RET
,
247 sizeof(*entry
), flags
, pc
);
250 entry
= ring_buffer_event_data(event
);
252 if (!filter_current_check_discard(buffer
, call
, entry
, event
))
253 ring_buffer_unlock_commit(buffer
, event
);
256 void trace_graph_return(struct ftrace_graph_ret
*trace
)
258 struct trace_array
*tr
= graph_array
;
259 struct trace_array_cpu
*data
;
265 local_irq_save(flags
);
266 cpu
= raw_smp_processor_id();
267 data
= tr
->data
[cpu
];
268 disabled
= atomic_inc_return(&data
->disabled
);
269 if (likely(disabled
== 1)) {
270 pc
= preempt_count();
271 __trace_graph_return(tr
, trace
, flags
, pc
);
274 clear_tsk_trace_graph(current
);
275 atomic_dec(&data
->disabled
);
276 local_irq_restore(flags
);
279 static int graph_trace_init(struct trace_array
*tr
)
284 ret
= register_ftrace_graph(&trace_graph_return
,
288 tracing_start_cmdline_record();
293 void set_graph_array(struct trace_array
*tr
)
298 static void graph_trace_reset(struct trace_array
*tr
)
300 tracing_stop_cmdline_record();
301 unregister_ftrace_graph();
304 static int max_bytes_for_cpu
;
306 static enum print_line_t
307 print_graph_cpu(struct trace_seq
*s
, int cpu
)
312 * Start with a space character - to make it stand out
313 * to the right a bit when trace output is pasted into
316 ret
= trace_seq_printf(s
, " %*d) ", max_bytes_for_cpu
, cpu
);
318 return TRACE_TYPE_PARTIAL_LINE
;
320 return TRACE_TYPE_HANDLED
;
323 #define TRACE_GRAPH_PROCINFO_LENGTH 14
325 static enum print_line_t
326 print_graph_proc(struct trace_seq
*s
, pid_t pid
)
328 char comm
[TASK_COMM_LEN
];
329 /* sign + log10(MAX_INT) + '\0' */
336 trace_find_cmdline(pid
, comm
);
338 sprintf(pid_str
, "%d", pid
);
340 /* 1 stands for the "-" character */
341 len
= strlen(comm
) + strlen(pid_str
) + 1;
343 if (len
< TRACE_GRAPH_PROCINFO_LENGTH
)
344 spaces
= TRACE_GRAPH_PROCINFO_LENGTH
- len
;
346 /* First spaces to align center */
347 for (i
= 0; i
< spaces
/ 2; i
++) {
348 ret
= trace_seq_printf(s
, " ");
350 return TRACE_TYPE_PARTIAL_LINE
;
353 ret
= trace_seq_printf(s
, "%s-%s", comm
, pid_str
);
355 return TRACE_TYPE_PARTIAL_LINE
;
357 /* Last spaces to align center */
358 for (i
= 0; i
< spaces
- (spaces
/ 2); i
++) {
359 ret
= trace_seq_printf(s
, " ");
361 return TRACE_TYPE_PARTIAL_LINE
;
363 return TRACE_TYPE_HANDLED
;
367 /* If the pid changed since the last trace, output this event */
368 static enum print_line_t
369 verif_pid(struct trace_seq
*s
, pid_t pid
, int cpu
, struct fgraph_data
*data
)
376 return TRACE_TYPE_HANDLED
;
378 last_pid
= &(per_cpu_ptr(data
, cpu
)->last_pid
);
380 if (*last_pid
== pid
)
381 return TRACE_TYPE_HANDLED
;
383 prev_pid
= *last_pid
;
387 return TRACE_TYPE_HANDLED
;
389 * Context-switch trace line:
391 ------------------------------------------
392 | 1) migration/0--1 => sshd-1755
393 ------------------------------------------
396 ret
= trace_seq_printf(s
,
397 " ------------------------------------------\n");
399 return TRACE_TYPE_PARTIAL_LINE
;
401 ret
= print_graph_cpu(s
, cpu
);
402 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
403 return TRACE_TYPE_PARTIAL_LINE
;
405 ret
= print_graph_proc(s
, prev_pid
);
406 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
407 return TRACE_TYPE_PARTIAL_LINE
;
409 ret
= trace_seq_printf(s
, " => ");
411 return TRACE_TYPE_PARTIAL_LINE
;
413 ret
= print_graph_proc(s
, pid
);
414 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
415 return TRACE_TYPE_PARTIAL_LINE
;
417 ret
= trace_seq_printf(s
,
418 "\n ------------------------------------------\n\n");
420 return TRACE_TYPE_PARTIAL_LINE
;
422 return TRACE_TYPE_HANDLED
;
425 static struct ftrace_graph_ret_entry
*
426 get_return_for_leaf(struct trace_iterator
*iter
,
427 struct ftrace_graph_ent_entry
*curr
)
429 struct ring_buffer_iter
*ring_iter
;
430 struct ring_buffer_event
*event
;
431 struct ftrace_graph_ret_entry
*next
;
433 ring_iter
= iter
->buffer_iter
[iter
->cpu
];
435 /* First peek to compare current entry and the next one */
437 event
= ring_buffer_iter_peek(ring_iter
, NULL
);
439 /* We need to consume the current entry to see the next one */
440 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
441 event
= ring_buffer_peek(iter
->tr
->buffer
, iter
->cpu
,
448 next
= ring_buffer_event_data(event
);
450 if (next
->ent
.type
!= TRACE_GRAPH_RET
)
453 if (curr
->ent
.pid
!= next
->ent
.pid
||
454 curr
->graph_ent
.func
!= next
->ret
.func
)
457 /* this is a leaf, now advance the iterator */
459 ring_buffer_read(ring_iter
, NULL
);
464 /* Signal a overhead of time execution to the output */
466 print_graph_overhead(unsigned long long duration
, struct trace_seq
*s
)
468 /* If duration disappear, we don't need anything */
469 if (!(tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
))
472 /* Non nested entry or return */
474 return trace_seq_printf(s
, " ");
476 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERHEAD
) {
477 /* Duration exceeded 100 msecs */
478 if (duration
> 100000ULL)
479 return trace_seq_printf(s
, "! ");
481 /* Duration exceeded 10 msecs */
482 if (duration
> 10000ULL)
483 return trace_seq_printf(s
, "+ ");
486 return trace_seq_printf(s
, " ");
489 static int print_graph_abs_time(u64 t
, struct trace_seq
*s
)
491 unsigned long usecs_rem
;
493 usecs_rem
= do_div(t
, NSEC_PER_SEC
);
496 return trace_seq_printf(s
, "%5lu.%06lu | ",
497 (unsigned long)t
, usecs_rem
);
500 static enum print_line_t
501 print_graph_irq(struct trace_iterator
*iter
, unsigned long addr
,
502 enum trace_type type
, int cpu
, pid_t pid
)
505 struct trace_seq
*s
= &iter
->seq
;
507 if (addr
< (unsigned long)__irqentry_text_start
||
508 addr
>= (unsigned long)__irqentry_text_end
)
509 return TRACE_TYPE_UNHANDLED
;
512 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
) {
513 ret
= print_graph_abs_time(iter
->ts
, s
);
515 return TRACE_TYPE_PARTIAL_LINE
;
519 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
520 ret
= print_graph_cpu(s
, cpu
);
521 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
522 return TRACE_TYPE_PARTIAL_LINE
;
525 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
526 ret
= print_graph_proc(s
, pid
);
527 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
528 return TRACE_TYPE_PARTIAL_LINE
;
529 ret
= trace_seq_printf(s
, " | ");
531 return TRACE_TYPE_PARTIAL_LINE
;
535 ret
= print_graph_overhead(-1, s
);
537 return TRACE_TYPE_PARTIAL_LINE
;
539 if (type
== TRACE_GRAPH_ENT
)
540 ret
= trace_seq_printf(s
, "==========>");
542 ret
= trace_seq_printf(s
, "<==========");
545 return TRACE_TYPE_PARTIAL_LINE
;
547 /* Don't close the duration column if haven't one */
548 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
549 trace_seq_printf(s
, " |");
550 ret
= trace_seq_printf(s
, "\n");
553 return TRACE_TYPE_PARTIAL_LINE
;
554 return TRACE_TYPE_HANDLED
;
558 trace_print_graph_duration(unsigned long long duration
, struct trace_seq
*s
)
560 unsigned long nsecs_rem
= do_div(duration
, 1000);
561 /* log10(ULONG_MAX) + '\0' */
567 sprintf(msecs_str
, "%lu", (unsigned long) duration
);
570 ret
= trace_seq_printf(s
, "%s", msecs_str
);
572 return TRACE_TYPE_PARTIAL_LINE
;
574 len
= strlen(msecs_str
);
576 /* Print nsecs (we don't want to exceed 7 numbers) */
578 snprintf(nsecs_str
, 8 - len
, "%03lu", nsecs_rem
);
579 ret
= trace_seq_printf(s
, ".%s", nsecs_str
);
581 return TRACE_TYPE_PARTIAL_LINE
;
582 len
+= strlen(nsecs_str
);
585 ret
= trace_seq_printf(s
, " us ");
587 return TRACE_TYPE_PARTIAL_LINE
;
589 /* Print remaining spaces to fit the row's width */
590 for (i
= len
; i
< 7; i
++) {
591 ret
= trace_seq_printf(s
, " ");
593 return TRACE_TYPE_PARTIAL_LINE
;
595 return TRACE_TYPE_HANDLED
;
598 static enum print_line_t
599 print_graph_duration(unsigned long long duration
, struct trace_seq
*s
)
603 ret
= trace_print_graph_duration(duration
, s
);
604 if (ret
!= TRACE_TYPE_HANDLED
)
607 ret
= trace_seq_printf(s
, "| ");
609 return TRACE_TYPE_PARTIAL_LINE
;
611 return TRACE_TYPE_HANDLED
;
614 /* Case of a leaf function on its call entry */
615 static enum print_line_t
616 print_graph_entry_leaf(struct trace_iterator
*iter
,
617 struct ftrace_graph_ent_entry
*entry
,
618 struct ftrace_graph_ret_entry
*ret_entry
, struct trace_seq
*s
)
620 struct fgraph_data
*data
= iter
->private;
621 struct ftrace_graph_ret
*graph_ret
;
622 struct ftrace_graph_ent
*call
;
623 unsigned long long duration
;
627 graph_ret
= &ret_entry
->ret
;
628 call
= &entry
->graph_ent
;
629 duration
= graph_ret
->rettime
- graph_ret
->calltime
;
633 int *depth
= &(per_cpu_ptr(data
, cpu
)->depth
);
636 * Comments display at + 1 to depth. Since
637 * this is a leaf function, keep the comments
638 * equal to this depth.
640 *depth
= call
->depth
- 1;
644 ret
= print_graph_overhead(duration
, s
);
646 return TRACE_TYPE_PARTIAL_LINE
;
649 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
650 ret
= print_graph_duration(duration
, s
);
651 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
652 return TRACE_TYPE_PARTIAL_LINE
;
656 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
657 ret
= trace_seq_printf(s
, " ");
659 return TRACE_TYPE_PARTIAL_LINE
;
662 ret
= trace_seq_printf(s
, "%pf();\n", (void *)call
->func
);
664 return TRACE_TYPE_PARTIAL_LINE
;
666 return TRACE_TYPE_HANDLED
;
669 static enum print_line_t
670 print_graph_entry_nested(struct trace_iterator
*iter
,
671 struct ftrace_graph_ent_entry
*entry
,
672 struct trace_seq
*s
, int cpu
)
674 struct ftrace_graph_ent
*call
= &entry
->graph_ent
;
675 struct fgraph_data
*data
= iter
->private;
681 int *depth
= &(per_cpu_ptr(data
, cpu
)->depth
);
683 *depth
= call
->depth
;
687 ret
= print_graph_overhead(-1, s
);
689 return TRACE_TYPE_PARTIAL_LINE
;
692 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
693 ret
= trace_seq_printf(s
, " | ");
695 return TRACE_TYPE_PARTIAL_LINE
;
699 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
700 ret
= trace_seq_printf(s
, " ");
702 return TRACE_TYPE_PARTIAL_LINE
;
705 ret
= trace_seq_printf(s
, "%pf() {\n", (void *)call
->func
);
707 return TRACE_TYPE_PARTIAL_LINE
;
710 * we already consumed the current entry to check the next one
711 * and see if this is a leaf.
713 return TRACE_TYPE_NO_CONSUME
;
716 static enum print_line_t
717 print_graph_prologue(struct trace_iterator
*iter
, struct trace_seq
*s
,
718 int type
, unsigned long addr
)
720 struct fgraph_data
*data
= iter
->private;
721 struct trace_entry
*ent
= iter
->ent
;
726 if (verif_pid(s
, ent
->pid
, cpu
, data
) == TRACE_TYPE_PARTIAL_LINE
)
727 return TRACE_TYPE_PARTIAL_LINE
;
731 ret
= print_graph_irq(iter
, addr
, type
, cpu
, ent
->pid
);
732 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
733 return TRACE_TYPE_PARTIAL_LINE
;
737 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
) {
738 ret
= print_graph_abs_time(iter
->ts
, s
);
740 return TRACE_TYPE_PARTIAL_LINE
;
744 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
) {
745 ret
= print_graph_cpu(s
, cpu
);
746 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
747 return TRACE_TYPE_PARTIAL_LINE
;
751 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
) {
752 ret
= print_graph_proc(s
, ent
->pid
);
753 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
754 return TRACE_TYPE_PARTIAL_LINE
;
756 ret
= trace_seq_printf(s
, " | ");
758 return TRACE_TYPE_PARTIAL_LINE
;
764 static enum print_line_t
765 print_graph_entry(struct ftrace_graph_ent_entry
*field
, struct trace_seq
*s
,
766 struct trace_iterator
*iter
)
769 struct ftrace_graph_ent
*call
= &field
->graph_ent
;
770 struct ftrace_graph_ret_entry
*leaf_ret
;
772 if (print_graph_prologue(iter
, s
, TRACE_GRAPH_ENT
, call
->func
))
773 return TRACE_TYPE_PARTIAL_LINE
;
775 leaf_ret
= get_return_for_leaf(iter
, field
);
777 return print_graph_entry_leaf(iter
, field
, leaf_ret
, s
);
779 return print_graph_entry_nested(iter
, field
, s
, cpu
);
783 static enum print_line_t
784 print_graph_return(struct ftrace_graph_ret
*trace
, struct trace_seq
*s
,
785 struct trace_entry
*ent
, struct trace_iterator
*iter
)
787 unsigned long long duration
= trace
->rettime
- trace
->calltime
;
788 struct fgraph_data
*data
= iter
->private;
789 pid_t pid
= ent
->pid
;
796 int *depth
= &(per_cpu_ptr(data
, cpu
)->depth
);
799 * Comments display at + 1 to depth. This is the
800 * return from a function, we now want the comments
801 * to display at the same level of the bracket.
803 *depth
= trace
->depth
- 1;
806 if (print_graph_prologue(iter
, s
, 0, 0))
807 return TRACE_TYPE_PARTIAL_LINE
;
810 ret
= print_graph_overhead(duration
, s
);
812 return TRACE_TYPE_PARTIAL_LINE
;
815 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
816 ret
= print_graph_duration(duration
, s
);
817 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
818 return TRACE_TYPE_PARTIAL_LINE
;
822 for (i
= 0; i
< trace
->depth
* TRACE_GRAPH_INDENT
; i
++) {
823 ret
= trace_seq_printf(s
, " ");
825 return TRACE_TYPE_PARTIAL_LINE
;
828 ret
= trace_seq_printf(s
, "}\n");
830 return TRACE_TYPE_PARTIAL_LINE
;
833 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERRUN
) {
834 ret
= trace_seq_printf(s
, " (Overruns: %lu)\n",
837 return TRACE_TYPE_PARTIAL_LINE
;
840 ret
= print_graph_irq(iter
, trace
->func
, TRACE_GRAPH_RET
, cpu
, pid
);
841 if (ret
== TRACE_TYPE_PARTIAL_LINE
)
842 return TRACE_TYPE_PARTIAL_LINE
;
844 return TRACE_TYPE_HANDLED
;
847 static enum print_line_t
848 print_graph_comment(struct trace_seq
*s
, struct trace_entry
*ent
,
849 struct trace_iterator
*iter
)
851 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
852 struct fgraph_data
*data
= iter
->private;
853 struct trace_event
*event
;
859 depth
= per_cpu_ptr(data
, iter
->cpu
)->depth
;
861 if (print_graph_prologue(iter
, s
, 0, 0))
862 return TRACE_TYPE_PARTIAL_LINE
;
865 ret
= print_graph_overhead(-1, s
);
867 return TRACE_TYPE_PARTIAL_LINE
;
870 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
) {
871 ret
= trace_seq_printf(s
, " | ");
873 return TRACE_TYPE_PARTIAL_LINE
;
878 for (i
= 0; i
< (depth
+ 1) * TRACE_GRAPH_INDENT
; i
++) {
879 ret
= trace_seq_printf(s
, " ");
881 return TRACE_TYPE_PARTIAL_LINE
;
885 ret
= trace_seq_printf(s
, "/* ");
887 return TRACE_TYPE_PARTIAL_LINE
;
889 switch (iter
->ent
->type
) {
891 ret
= trace_print_bprintk_msg_only(iter
);
892 if (ret
!= TRACE_TYPE_HANDLED
)
896 ret
= trace_print_printk_msg_only(iter
);
897 if (ret
!= TRACE_TYPE_HANDLED
)
901 event
= ftrace_find_event(ent
->type
);
903 return TRACE_TYPE_UNHANDLED
;
905 ret
= event
->trace(iter
, sym_flags
);
906 if (ret
!= TRACE_TYPE_HANDLED
)
910 /* Strip ending newline */
911 if (s
->buffer
[s
->len
- 1] == '\n') {
912 s
->buffer
[s
->len
- 1] = '\0';
916 ret
= trace_seq_printf(s
, " */\n");
918 return TRACE_TYPE_PARTIAL_LINE
;
920 return TRACE_TYPE_HANDLED
;
925 print_graph_function(struct trace_iterator
*iter
)
927 struct trace_entry
*entry
= iter
->ent
;
928 struct trace_seq
*s
= &iter
->seq
;
930 switch (entry
->type
) {
931 case TRACE_GRAPH_ENT
: {
933 * print_graph_entry() may consume the current event,
934 * thus @field may become invalid, so we need to save it.
935 * sizeof(struct ftrace_graph_ent_entry) is very small,
936 * it can be safely saved at the stack.
938 struct ftrace_graph_ent_entry
*field
, saved
;
939 trace_assign_type(field
, entry
);
941 return print_graph_entry(&saved
, s
, iter
);
943 case TRACE_GRAPH_RET
: {
944 struct ftrace_graph_ret_entry
*field
;
945 trace_assign_type(field
, entry
);
946 return print_graph_return(&field
->ret
, s
, entry
, iter
);
949 return print_graph_comment(s
, entry
, iter
);
952 return TRACE_TYPE_HANDLED
;
955 static void print_graph_headers(struct seq_file
*s
)
959 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
960 seq_printf(s
, " TIME ");
961 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
962 seq_printf(s
, "CPU");
963 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
964 seq_printf(s
, " TASK/PID ");
965 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
966 seq_printf(s
, " DURATION ");
967 seq_printf(s
, " FUNCTION CALLS\n");
971 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_ABS_TIME
)
972 seq_printf(s
, " | ");
973 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_CPU
)
975 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_PROC
)
976 seq_printf(s
, " | | ");
977 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_DURATION
)
978 seq_printf(s
, " | | ");
979 seq_printf(s
, " | | | |\n");
982 static void graph_trace_open(struct trace_iterator
*iter
)
984 /* pid and depth on the last trace processed */
985 struct fgraph_data
*data
= alloc_percpu(struct fgraph_data
);
989 pr_warning("function graph tracer: not enough memory\n");
991 for_each_possible_cpu(cpu
) {
992 pid_t
*pid
= &(per_cpu_ptr(data
, cpu
)->last_pid
);
993 int *depth
= &(per_cpu_ptr(data
, cpu
)->depth
);
998 iter
->private = data
;
1001 static void graph_trace_close(struct trace_iterator
*iter
)
1003 free_percpu(iter
->private);
1006 static struct tracer graph_trace __read_mostly
= {
1007 .name
= "function_graph",
1008 .open
= graph_trace_open
,
1009 .close
= graph_trace_close
,
1010 .wait_pipe
= poll_wait_pipe
,
1011 .init
= graph_trace_init
,
1012 .reset
= graph_trace_reset
,
1013 .print_line
= print_graph_function
,
1014 .print_header
= print_graph_headers
,
1015 .flags
= &tracer_flags
,
1016 #ifdef CONFIG_FTRACE_SELFTEST
1017 .selftest
= trace_selftest_startup_function_graph
,
1021 static __init
int init_graph_trace(void)
1023 max_bytes_for_cpu
= snprintf(NULL
, 0, "%d", nr_cpu_ids
- 1);
1025 return register_tracer(&graph_trace
);
1028 device_initcall(init_graph_trace
);