3 * Function graph tracer.
4 * Copyright (c) 2008 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>
16 #define TRACE_GRAPH_INDENT 2
18 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
19 static struct tracer_opt trace_opts
[] = {
20 /* Display overruns or not */
21 { TRACER_OPT(overrun
, TRACE_GRAPH_PRINT_OVERRUN
) },
25 static struct tracer_flags tracer_flags
= {
26 .val
= 0, /* Don't display overruns by default */
30 /* pid on the last trace processed */
31 static pid_t last_pid
= -1;
33 static int graph_trace_init(struct trace_array
*tr
)
37 for_each_online_cpu(cpu
)
38 tracing_reset(tr
, cpu
);
40 ret
= register_ftrace_graph(&trace_graph_return
,
44 tracing_start_cmdline_record();
49 static void graph_trace_reset(struct trace_array
*tr
)
51 tracing_stop_cmdline_record();
52 unregister_ftrace_graph();
55 /* If the pid changed since the last trace, output this event */
56 static int verif_pid(struct trace_seq
*s
, pid_t pid
)
60 if (last_pid
!= -1 && last_pid
== pid
)
64 comm
= trace_find_cmdline(pid
);
66 return trace_seq_printf(s
, "\n------------8<---------- thread %s-%d"
67 " ------------8<----------\n\n",
71 static enum print_line_t
72 print_graph_entry(struct ftrace_graph_ent
*call
, struct trace_seq
*s
,
73 struct trace_entry
*ent
)
78 if (!verif_pid(s
, ent
->pid
))
79 return TRACE_TYPE_PARTIAL_LINE
;
81 for (i
= 0; i
< call
->depth
* TRACE_GRAPH_INDENT
; i
++) {
82 ret
= trace_seq_printf(s
, " ");
84 return TRACE_TYPE_PARTIAL_LINE
;
87 ret
= seq_print_ip_sym(s
, call
->func
, 0);
89 return TRACE_TYPE_PARTIAL_LINE
;
91 ret
= trace_seq_printf(s
, "() {\n");
93 return TRACE_TYPE_PARTIAL_LINE
;
94 return TRACE_TYPE_HANDLED
;
97 static enum print_line_t
98 print_graph_return(struct ftrace_graph_ret
*trace
, struct trace_seq
*s
,
99 struct trace_entry
*ent
)
104 if (!verif_pid(s
, ent
->pid
))
105 return TRACE_TYPE_PARTIAL_LINE
;
107 for (i
= 0; i
< trace
->depth
* TRACE_GRAPH_INDENT
; i
++) {
108 ret
= trace_seq_printf(s
, " ");
110 return TRACE_TYPE_PARTIAL_LINE
;
113 ret
= trace_seq_printf(s
, "} ");
115 return TRACE_TYPE_PARTIAL_LINE
;
117 ret
= trace_seq_printf(s
, "%llu\n", trace
->rettime
- trace
->calltime
);
119 return TRACE_TYPE_PARTIAL_LINE
;
121 if (tracer_flags
.val
& TRACE_GRAPH_PRINT_OVERRUN
) {
122 ret
= trace_seq_printf(s
, " (Overruns: %lu)\n",
125 return TRACE_TYPE_PARTIAL_LINE
;
127 return TRACE_TYPE_HANDLED
;
131 print_graph_function(struct trace_iterator
*iter
)
133 struct trace_seq
*s
= &iter
->seq
;
134 struct trace_entry
*entry
= iter
->ent
;
136 switch (entry
->type
) {
137 case TRACE_GRAPH_ENT
: {
138 struct ftrace_graph_ent_entry
*field
;
139 trace_assign_type(field
, entry
);
140 return print_graph_entry(&field
->graph_ent
, s
, entry
);
142 case TRACE_GRAPH_RET
: {
143 struct ftrace_graph_ret_entry
*field
;
144 trace_assign_type(field
, entry
);
145 return print_graph_return(&field
->ret
, s
, entry
);
148 return TRACE_TYPE_UNHANDLED
;
152 static struct tracer graph_trace __read_mostly
= {
153 .name
= "function-graph",
154 .init
= graph_trace_init
,
155 .reset
= graph_trace_reset
,
156 .print_line
= print_graph_function
,
157 .flags
= &tracer_flags
,
160 static __init
int init_graph_trace(void)
162 return register_tracer(&graph_trace
);
165 device_initcall(init_graph_trace
);