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)
36 for_each_online_cpu(cpu)
37 tracing_reset(tr, cpu);
39 return register_ftrace_graph(&trace_graph_return,
43 static void graph_trace_reset(struct trace_array *tr)
45 unregister_ftrace_graph();
48 /* If the pid changed since the last trace, output this event */
49 static int verif_pid(struct trace_seq *s, pid_t pid)
51 if (last_pid != -1 && last_pid == pid)
55 return trace_seq_printf(s, "\n------------8<---------- thread %d"
56 " ------------8<----------\n\n",
60 static enum print_line_t
61 print_graph_entry(struct ftrace_graph_ent *call, struct trace_seq *s,
62 struct trace_entry *ent)
67 if (!verif_pid(s, ent->pid))
68 return TRACE_TYPE_PARTIAL_LINE;
70 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
71 ret = trace_seq_printf(s, " ");
73 return TRACE_TYPE_PARTIAL_LINE;
76 ret = seq_print_ip_sym(s, call->func, 0);
78 return TRACE_TYPE_PARTIAL_LINE;
80 ret = trace_seq_printf(s, "() {\n");
82 return TRACE_TYPE_PARTIAL_LINE;
83 return TRACE_TYPE_HANDLED;
86 static enum print_line_t
87 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
88 struct trace_entry *ent)
93 if (!verif_pid(s, ent->pid))
94 return TRACE_TYPE_PARTIAL_LINE;
96 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
97 ret = trace_seq_printf(s, " ");
99 return TRACE_TYPE_PARTIAL_LINE;
102 ret = trace_seq_printf(s, "} ");
104 return TRACE_TYPE_PARTIAL_LINE;
106 ret = trace_seq_printf(s, "%llu\n", trace->rettime - trace->calltime);
108 return TRACE_TYPE_PARTIAL_LINE;
110 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
111 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
114 return TRACE_TYPE_PARTIAL_LINE;
116 return TRACE_TYPE_HANDLED;
120 print_graph_function(struct trace_iterator *iter)
122 struct trace_seq *s = &iter->seq;
123 struct trace_entry *entry = iter->ent;
125 switch (entry->type) {
126 case TRACE_GRAPH_ENT: {
127 struct ftrace_graph_ent_entry *field;
128 trace_assign_type(field, entry);
129 return print_graph_entry(&field->graph_ent, s, entry);
131 case TRACE_GRAPH_RET: {
132 struct ftrace_graph_ret_entry *field;
133 trace_assign_type(field, entry);
134 return print_graph_return(&field->ret, s, entry);
137 return TRACE_TYPE_UNHANDLED;
141 static struct tracer graph_trace __read_mostly = {
142 .name = "function-graph",
143 .init = graph_trace_init,
144 .reset = graph_trace_reset,
145 .print_line = print_graph_function,
146 .flags = &tracer_flags,
149 static __init int init_graph_trace(void)
151 return register_tracer(&graph_trace);
154 device_initcall(init_graph_trace);