ftrace: add thread comm to function graph tracer
[pandora-kernel.git] / kernel / trace / trace_functions_graph.c
1 /*
2  *
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>
7  *
8  */
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <linux/fs.h>
13
14 #include "trace.h"
15
16 #define TRACE_GRAPH_INDENT      2
17
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) },
22         { } /* Empty entry */
23 };
24
25 static struct tracer_flags tracer_flags = {
26         .val = 0, /* Don't display overruns by default */
27         .opts = trace_opts
28 };
29
30 /* pid on the last trace processed */
31 static pid_t last_pid = -1;
32
33 static int graph_trace_init(struct trace_array *tr)
34 {
35         int cpu, ret;
36
37         for_each_online_cpu(cpu)
38                 tracing_reset(tr, cpu);
39
40         ret = register_ftrace_graph(&trace_graph_return,
41                                         &trace_graph_entry);
42         if (ret)
43                 return ret;
44         tracing_start_cmdline_record();
45
46         return 0;
47 }
48
49 static void graph_trace_reset(struct trace_array *tr)
50 {
51         tracing_stop_cmdline_record();
52         unregister_ftrace_graph();
53 }
54
55 /* If the pid changed since the last trace, output this event */
56 static int verif_pid(struct trace_seq *s, pid_t pid)
57 {
58         char *comm;
59
60         if (last_pid != -1 && last_pid == pid)
61                 return 1;
62
63         last_pid = pid;
64         comm = trace_find_cmdline(pid);
65
66         return trace_seq_printf(s, "\n------------8<---------- thread %s-%d"
67                                     " ------------8<----------\n\n",
68                                     comm, pid);
69 }
70
71 static enum print_line_t
72 print_graph_entry(struct ftrace_graph_ent *call, struct trace_seq *s,
73                 struct trace_entry *ent)
74 {
75         int i;
76         int ret;
77
78         if (!verif_pid(s, ent->pid))
79                 return TRACE_TYPE_PARTIAL_LINE;
80
81         for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
82                 ret = trace_seq_printf(s, " ");
83                 if (!ret)
84                         return TRACE_TYPE_PARTIAL_LINE;
85         }
86
87         ret = seq_print_ip_sym(s, call->func, 0);
88         if (!ret)
89                 return TRACE_TYPE_PARTIAL_LINE;
90
91         ret = trace_seq_printf(s, "() {\n");
92         if (!ret)
93                 return TRACE_TYPE_PARTIAL_LINE;
94         return TRACE_TYPE_HANDLED;
95 }
96
97 static enum print_line_t
98 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
99                    struct trace_entry *ent)
100 {
101         int i;
102         int ret;
103
104         if (!verif_pid(s, ent->pid))
105                 return TRACE_TYPE_PARTIAL_LINE;
106
107         for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
108                 ret = trace_seq_printf(s, " ");
109                 if (!ret)
110                         return TRACE_TYPE_PARTIAL_LINE;
111         }
112
113         ret = trace_seq_printf(s, "} ");
114         if (!ret)
115                 return TRACE_TYPE_PARTIAL_LINE;
116
117         ret = trace_seq_printf(s, "%llu\n", trace->rettime - trace->calltime);
118         if (!ret)
119                 return TRACE_TYPE_PARTIAL_LINE;
120
121         if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
122                 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
123                                         trace->overrun);
124                 if (!ret)
125                         return TRACE_TYPE_PARTIAL_LINE;
126         }
127         return TRACE_TYPE_HANDLED;
128 }
129
130 enum print_line_t
131 print_graph_function(struct trace_iterator *iter)
132 {
133         struct trace_seq *s = &iter->seq;
134         struct trace_entry *entry = iter->ent;
135
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);
141         }
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);
146         }
147         default:
148                 return TRACE_TYPE_UNHANDLED;
149         }
150 }
151
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,
158 };
159
160 static __init int init_graph_trace(void)
161 {
162         return register_tracer(&graph_trace);
163 }
164
165 device_initcall(init_graph_trace);