blob: 66fc7b806a38ea7d1b73c446012338d95e7148b2 [file] [log] [blame]
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001/*
2 *
3 * Function graph tracer.
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08004 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005 * 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"
Steven Rostedtf0868d12008-12-23 23:24:12 -050015#include "trace_output.h"
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010016
Frederic Weisbecker287b6e62008-11-26 00:57:25 +010017#define TRACE_GRAPH_INDENT 2
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010018
Frederic Weisbecker1a056152008-11-28 00:42:46 +010019/* Flag options */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010020#define TRACE_GRAPH_PRINT_OVERRUN 0x1
Frederic Weisbecker1a056152008-11-28 00:42:46 +010021#define TRACE_GRAPH_PRINT_CPU 0x2
22#define TRACE_GRAPH_PRINT_OVERHEAD 0x4
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +010023#define TRACE_GRAPH_PRINT_PROC 0x8
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080024#define TRACE_GRAPH_PRINT_DURATION 0x10
25#define TRACE_GRAPH_PRINT_ABS_TIME 0X20
Frederic Weisbecker1a056152008-11-28 00:42:46 +010026
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010027static struct tracer_opt trace_opts[] = {
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080028 /* Display overruns? (for self-debug purpose) */
Frederic Weisbecker1a056152008-11-28 00:42:46 +010029 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
30 /* Display CPU ? */
31 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
32 /* Display Overhead ? */
33 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +010034 /* Display proc name/pid */
35 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080036 /* Display duration of execution */
37 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
38 /* Display absolute time of an entry */
39 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010040 { } /* Empty entry */
41};
42
43static struct tracer_flags tracer_flags = {
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +010044 /* Don't display overruns and proc by default */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080045 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
46 TRACE_GRAPH_PRINT_DURATION,
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010047 .opts = trace_opts
48};
49
Frederic Weisbecker287b6e62008-11-26 00:57:25 +010050/* pid on the last trace processed */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080051
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010052
53static int graph_trace_init(struct trace_array *tr)
54{
Steven Rostedt660c7f92008-11-26 00:16:26 -050055 int cpu, ret;
56
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010057 for_each_online_cpu(cpu)
58 tracing_reset(tr, cpu);
59
Steven Rostedt660c7f92008-11-26 00:16:26 -050060 ret = register_ftrace_graph(&trace_graph_return,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +010061 &trace_graph_entry);
Steven Rostedt660c7f92008-11-26 00:16:26 -050062 if (ret)
63 return ret;
64 tracing_start_cmdline_record();
65
66 return 0;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010067}
68
69static void graph_trace_reset(struct trace_array *tr)
70{
Steven Rostedt660c7f92008-11-26 00:16:26 -050071 tracing_stop_cmdline_record();
72 unregister_ftrace_graph();
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010073}
74
Frederic Weisbecker1a056152008-11-28 00:42:46 +010075static inline int log10_cpu(int nb)
76{
77 if (nb / 100)
78 return 3;
79 if (nb / 10)
80 return 2;
81 return 1;
82}
83
84static enum print_line_t
85print_graph_cpu(struct trace_seq *s, int cpu)
86{
87 int i;
88 int ret;
89 int log10_this = log10_cpu(cpu);
Rusty Russell44623442009-01-01 10:12:23 +103090 int log10_all = log10_cpu(cpumask_weight(cpu_online_mask));
Frederic Weisbecker1a056152008-11-28 00:42:46 +010091
92
Ingo Molnard51090b2008-11-28 09:55:16 +010093 /*
94 * Start with a space character - to make it stand out
95 * to the right a bit when trace output is pasted into
96 * email:
97 */
98 ret = trace_seq_printf(s, " ");
99
100 /*
101 * Tricky - we space the CPU field according to the max
102 * number of online CPUs. On a 2-cpu system it would take
103 * a maximum of 1 digit - on a 128 cpu system it would
104 * take up to 3 digits:
105 */
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100106 for (i = 0; i < log10_all - log10_this; i++) {
107 ret = trace_seq_printf(s, " ");
108 if (!ret)
109 return TRACE_TYPE_PARTIAL_LINE;
110 }
111 ret = trace_seq_printf(s, "%d) ", cpu);
112 if (!ret)
Ingo Molnard51090b2008-11-28 09:55:16 +0100113 return TRACE_TYPE_PARTIAL_LINE;
114
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100115 return TRACE_TYPE_HANDLED;
116}
117
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100118#define TRACE_GRAPH_PROCINFO_LENGTH 14
119
120static enum print_line_t
121print_graph_proc(struct trace_seq *s, pid_t pid)
122{
123 int i;
124 int ret;
125 int len;
126 char comm[8];
127 int spaces = 0;
128 /* sign + log10(MAX_INT) + '\0' */
129 char pid_str[11];
130
131 strncpy(comm, trace_find_cmdline(pid), 7);
132 comm[7] = '\0';
133 sprintf(pid_str, "%d", pid);
134
135 /* 1 stands for the "-" character */
136 len = strlen(comm) + strlen(pid_str) + 1;
137
138 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
139 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
140
141 /* First spaces to align center */
142 for (i = 0; i < spaces / 2; i++) {
143 ret = trace_seq_printf(s, " ");
144 if (!ret)
145 return TRACE_TYPE_PARTIAL_LINE;
146 }
147
148 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
149 if (!ret)
150 return TRACE_TYPE_PARTIAL_LINE;
151
152 /* Last spaces to align center */
153 for (i = 0; i < spaces - (spaces / 2); i++) {
154 ret = trace_seq_printf(s, " ");
155 if (!ret)
156 return TRACE_TYPE_PARTIAL_LINE;
157 }
158 return TRACE_TYPE_HANDLED;
159}
160
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100161
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100162/* If the pid changed since the last trace, output this event */
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100163static enum print_line_t
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800164verif_pid(struct trace_seq *s, pid_t pid, int cpu, pid_t *last_pids_cpu)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100165{
Ingo Molnard51090b2008-11-28 09:55:16 +0100166 pid_t prev_pid;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800167 pid_t *last_pid;
Ingo Molnard51090b2008-11-28 09:55:16 +0100168 int ret;
Steven Rostedt660c7f92008-11-26 00:16:26 -0500169
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800170 if (!last_pids_cpu)
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100171 return TRACE_TYPE_HANDLED;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100172
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800173 last_pid = per_cpu_ptr(last_pids_cpu, cpu);
Steven Rostedt660c7f92008-11-26 00:16:26 -0500174
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800175 if (*last_pid == pid)
176 return TRACE_TYPE_HANDLED;
177
178 prev_pid = *last_pid;
179 *last_pid = pid;
180
181 if (prev_pid == -1)
182 return TRACE_TYPE_HANDLED;
Ingo Molnard51090b2008-11-28 09:55:16 +0100183/*
184 * Context-switch trace line:
185
186 ------------------------------------------
187 | 1) migration/0--1 => sshd-1755
188 ------------------------------------------
189
190 */
191 ret = trace_seq_printf(s,
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100192 " ------------------------------------------\n");
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100193 if (!ret)
194 TRACE_TYPE_PARTIAL_LINE;
195
196 ret = print_graph_cpu(s, cpu);
197 if (ret == TRACE_TYPE_PARTIAL_LINE)
198 TRACE_TYPE_PARTIAL_LINE;
199
200 ret = print_graph_proc(s, prev_pid);
201 if (ret == TRACE_TYPE_PARTIAL_LINE)
202 TRACE_TYPE_PARTIAL_LINE;
203
204 ret = trace_seq_printf(s, " => ");
205 if (!ret)
206 TRACE_TYPE_PARTIAL_LINE;
207
208 ret = print_graph_proc(s, pid);
209 if (ret == TRACE_TYPE_PARTIAL_LINE)
210 TRACE_TYPE_PARTIAL_LINE;
211
212 ret = trace_seq_printf(s,
213 "\n ------------------------------------------\n\n");
214 if (!ret)
215 TRACE_TYPE_PARTIAL_LINE;
216
Ingo Molnard51090b2008-11-28 09:55:16 +0100217 return ret;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100218}
219
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100220static bool
221trace_branch_is_leaf(struct trace_iterator *iter,
222 struct ftrace_graph_ent_entry *curr)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100223{
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100224 struct ring_buffer_iter *ring_iter;
225 struct ring_buffer_event *event;
226 struct ftrace_graph_ret_entry *next;
227
228 ring_iter = iter->buffer_iter[iter->cpu];
229
230 if (!ring_iter)
231 return false;
232
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100233 event = ring_buffer_iter_peek(ring_iter, NULL);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100234
235 if (!event)
236 return false;
237
238 next = ring_buffer_event_data(event);
239
240 if (next->ent.type != TRACE_GRAPH_RET)
241 return false;
242
243 if (curr->ent.pid != next->ent.pid ||
244 curr->graph_ent.func != next->ret.func)
245 return false;
246
247 return true;
248}
249
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800250/* Signal a overhead of time execution to the output */
251static int
252print_graph_overhead(unsigned long long duration, struct trace_seq *s)
253{
254 /* If duration disappear, we don't need anything */
255 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
256 return 1;
257
258 /* Non nested entry or return */
259 if (duration == -1)
260 return trace_seq_printf(s, " ");
261
262 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
263 /* Duration exceeded 100 msecs */
264 if (duration > 100000ULL)
265 return trace_seq_printf(s, "! ");
266
267 /* Duration exceeded 10 msecs */
268 if (duration > 10000ULL)
269 return trace_seq_printf(s, "+ ");
270 }
271
272 return trace_seq_printf(s, " ");
273}
274
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100275static enum print_line_t
276print_graph_irq(struct trace_seq *s, unsigned long addr,
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800277 enum trace_type type, int cpu, pid_t pid)
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100278{
279 int ret;
280
281 if (addr < (unsigned long)__irqentry_text_start ||
282 addr >= (unsigned long)__irqentry_text_end)
283 return TRACE_TYPE_UNHANDLED;
284
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800285 /* Cpu */
286 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
287 ret = print_graph_cpu(s, cpu);
288 if (ret == TRACE_TYPE_PARTIAL_LINE)
289 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100290 }
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800291 /* Proc */
292 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
293 ret = print_graph_proc(s, pid);
294 if (ret == TRACE_TYPE_PARTIAL_LINE)
295 return TRACE_TYPE_PARTIAL_LINE;
296 ret = trace_seq_printf(s, " | ");
297 if (!ret)
298 return TRACE_TYPE_PARTIAL_LINE;
299 }
300
301 /* No overhead */
302 ret = print_graph_overhead(-1, s);
303 if (!ret)
304 return TRACE_TYPE_PARTIAL_LINE;
305
306 if (type == TRACE_GRAPH_ENT)
307 ret = trace_seq_printf(s, "==========>");
308 else
309 ret = trace_seq_printf(s, "<==========");
310
311 if (!ret)
312 return TRACE_TYPE_PARTIAL_LINE;
313
314 /* Don't close the duration column if haven't one */
315 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
316 trace_seq_printf(s, " |");
317 ret = trace_seq_printf(s, "\n");
318
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100319 if (!ret)
320 return TRACE_TYPE_PARTIAL_LINE;
321 return TRACE_TYPE_HANDLED;
322}
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100323
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100324static enum print_line_t
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100325print_graph_duration(unsigned long long duration, struct trace_seq *s)
326{
327 unsigned long nsecs_rem = do_div(duration, 1000);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100328 /* log10(ULONG_MAX) + '\0' */
329 char msecs_str[21];
330 char nsecs_str[5];
331 int ret, len;
332 int i;
333
334 sprintf(msecs_str, "%lu", (unsigned long) duration);
335
336 /* Print msecs */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800337 ret = trace_seq_printf(s, "%s", msecs_str);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100338 if (!ret)
339 return TRACE_TYPE_PARTIAL_LINE;
340
341 len = strlen(msecs_str);
342
343 /* Print nsecs (we don't want to exceed 7 numbers) */
344 if (len < 7) {
345 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
346 ret = trace_seq_printf(s, ".%s", nsecs_str);
347 if (!ret)
348 return TRACE_TYPE_PARTIAL_LINE;
349 len += strlen(nsecs_str);
350 }
351
352 ret = trace_seq_printf(s, " us ");
353 if (!ret)
354 return TRACE_TYPE_PARTIAL_LINE;
355
356 /* Print remaining spaces to fit the row's width */
357 for (i = len; i < 7; i++) {
358 ret = trace_seq_printf(s, " ");
359 if (!ret)
360 return TRACE_TYPE_PARTIAL_LINE;
361 }
362
363 ret = trace_seq_printf(s, "| ");
364 if (!ret)
365 return TRACE_TYPE_PARTIAL_LINE;
366 return TRACE_TYPE_HANDLED;
367
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100368}
369
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800370static int print_graph_abs_time(u64 t, struct trace_seq *s)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100371{
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800372 unsigned long usecs_rem;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100373
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800374 usecs_rem = do_div(t, 1000000000);
375 usecs_rem /= 1000;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100376
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800377 return trace_seq_printf(s, "%5lu.%06lu | ",
378 (unsigned long)t, usecs_rem);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100379}
380
381/* Case of a leaf function on its call entry */
382static enum print_line_t
383print_graph_entry_leaf(struct trace_iterator *iter,
384 struct ftrace_graph_ent_entry *entry, struct trace_seq *s)
385{
386 struct ftrace_graph_ret_entry *ret_entry;
387 struct ftrace_graph_ret *graph_ret;
388 struct ring_buffer_event *event;
389 struct ftrace_graph_ent *call;
390 unsigned long long duration;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100391 int ret;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100392 int i;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100393
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100394 event = ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
395 ret_entry = ring_buffer_event_data(event);
396 graph_ret = &ret_entry->ret;
397 call = &entry->graph_ent;
398 duration = graph_ret->rettime - graph_ret->calltime;
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500399
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100400 /* Overhead */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800401 ret = print_graph_overhead(duration, s);
402 if (!ret)
403 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100404
405 /* Duration */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800406 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
407 ret = print_graph_duration(duration, s);
408 if (ret == TRACE_TYPE_PARTIAL_LINE)
409 return TRACE_TYPE_PARTIAL_LINE;
410 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100411
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100412 /* Function */
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100413 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
414 ret = trace_seq_printf(s, " ");
415 if (!ret)
416 return TRACE_TYPE_PARTIAL_LINE;
417 }
418
419 ret = seq_print_ip_sym(s, call->func, 0);
420 if (!ret)
421 return TRACE_TYPE_PARTIAL_LINE;
422
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100423 ret = trace_seq_printf(s, "();\n");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100424 if (!ret)
425 return TRACE_TYPE_PARTIAL_LINE;
426
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100427 return TRACE_TYPE_HANDLED;
428}
429
430static enum print_line_t
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100431print_graph_entry_nested(struct ftrace_graph_ent_entry *entry,
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100432 struct trace_seq *s, pid_t pid, int cpu)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100433{
434 int i;
435 int ret;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100436 struct ftrace_graph_ent *call = &entry->graph_ent;
437
438 /* No overhead */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800439 ret = print_graph_overhead(-1, s);
440 if (!ret)
441 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100442
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800443 /* No time */
444 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100445 ret = trace_seq_printf(s, " | ");
446 if (!ret)
447 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100448 }
449
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100450 /* Function */
451 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
452 ret = trace_seq_printf(s, " ");
453 if (!ret)
454 return TRACE_TYPE_PARTIAL_LINE;
455 }
456
457 ret = seq_print_ip_sym(s, call->func, 0);
458 if (!ret)
459 return TRACE_TYPE_PARTIAL_LINE;
460
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100461 ret = trace_seq_printf(s, "() {\n");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100462 if (!ret)
463 return TRACE_TYPE_PARTIAL_LINE;
464
465 return TRACE_TYPE_HANDLED;
466}
467
468static enum print_line_t
469print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800470 struct trace_iterator *iter)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100471{
472 int ret;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800473 int cpu = iter->cpu;
474 pid_t *last_entry = iter->private;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100475 struct trace_entry *ent = iter->ent;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800476 struct ftrace_graph_ent *call = &field->graph_ent;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100477
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100478 /* Pid */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800479 if (verif_pid(s, ent->pid, cpu, last_entry) == TRACE_TYPE_PARTIAL_LINE)
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500480 return TRACE_TYPE_PARTIAL_LINE;
481
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800482 /* Interrupt */
483 ret = print_graph_irq(s, call->func, TRACE_GRAPH_ENT, cpu, ent->pid);
484 if (ret == TRACE_TYPE_PARTIAL_LINE)
485 return TRACE_TYPE_PARTIAL_LINE;
486
487 /* Absolute time */
488 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
489 ret = print_graph_abs_time(iter->ts, s);
490 if (!ret)
491 return TRACE_TYPE_PARTIAL_LINE;
492 }
493
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100494 /* Cpu */
495 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
496 ret = print_graph_cpu(s, cpu);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100497 if (ret == TRACE_TYPE_PARTIAL_LINE)
498 return TRACE_TYPE_PARTIAL_LINE;
499 }
500
501 /* Proc */
502 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
503 ret = print_graph_proc(s, ent->pid);
504 if (ret == TRACE_TYPE_PARTIAL_LINE)
505 return TRACE_TYPE_PARTIAL_LINE;
506
507 ret = trace_seq_printf(s, " | ");
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100508 if (!ret)
509 return TRACE_TYPE_PARTIAL_LINE;
510 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100511
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100512 if (trace_branch_is_leaf(iter, field))
513 return print_graph_entry_leaf(iter, field, s);
514 else
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100515 return print_graph_entry_nested(field, s, iter->ent->pid, cpu);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100516
517}
518
519static enum print_line_t
520print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800521 struct trace_entry *ent, struct trace_iterator *iter)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100522{
523 int i;
524 int ret;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800525 int cpu = iter->cpu;
526 pid_t *last_pid = iter->private;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100527 unsigned long long duration = trace->rettime - trace->calltime;
528
529 /* Pid */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800530 if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100531 return TRACE_TYPE_PARTIAL_LINE;
532
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800533 /* Absolute time */
534 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
535 ret = print_graph_abs_time(iter->ts, s);
536 if (!ret)
537 return TRACE_TYPE_PARTIAL_LINE;
538 }
539
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100540 /* Cpu */
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100541 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
542 ret = print_graph_cpu(s, cpu);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100543 if (ret == TRACE_TYPE_PARTIAL_LINE)
544 return TRACE_TYPE_PARTIAL_LINE;
545 }
546
547 /* Proc */
548 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
549 ret = print_graph_proc(s, ent->pid);
550 if (ret == TRACE_TYPE_PARTIAL_LINE)
551 return TRACE_TYPE_PARTIAL_LINE;
552
553 ret = trace_seq_printf(s, " | ");
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100554 if (!ret)
555 return TRACE_TYPE_PARTIAL_LINE;
556 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100557
558 /* Overhead */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800559 ret = print_graph_overhead(duration, s);
560 if (!ret)
561 return TRACE_TYPE_PARTIAL_LINE;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100562
563 /* Duration */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800564 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
565 ret = print_graph_duration(duration, s);
566 if (ret == TRACE_TYPE_PARTIAL_LINE)
567 return TRACE_TYPE_PARTIAL_LINE;
568 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100569
570 /* Closing brace */
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100571 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
572 ret = trace_seq_printf(s, " ");
573 if (!ret)
574 return TRACE_TYPE_PARTIAL_LINE;
575 }
576
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100577 ret = trace_seq_printf(s, "}\n");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100578 if (!ret)
579 return TRACE_TYPE_PARTIAL_LINE;
580
581 /* Overrun */
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100582 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
583 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
584 trace->overrun);
585 if (!ret)
586 return TRACE_TYPE_PARTIAL_LINE;
587 }
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100588
589 ret = print_graph_irq(s, trace->func, TRACE_GRAPH_RET, cpu, ent->pid);
590 if (ret == TRACE_TYPE_PARTIAL_LINE)
591 return TRACE_TYPE_PARTIAL_LINE;
592
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100593 return TRACE_TYPE_HANDLED;
594}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100595
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100596static enum print_line_t
597print_graph_comment(struct print_entry *trace, struct trace_seq *s,
598 struct trace_entry *ent, struct trace_iterator *iter)
599{
600 int i;
601 int ret;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800602 int cpu = iter->cpu;
603 pid_t *last_pid = iter->private;
604
605 /* Absolute time */
606 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
607 ret = print_graph_abs_time(iter->ts, s);
608 if (!ret)
609 return TRACE_TYPE_PARTIAL_LINE;
610 }
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100611
612 /* Pid */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800613 if (verif_pid(s, ent->pid, cpu, last_pid) == TRACE_TYPE_PARTIAL_LINE)
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100614 return TRACE_TYPE_PARTIAL_LINE;
615
616 /* Cpu */
617 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800618 ret = print_graph_cpu(s, cpu);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100619 if (ret == TRACE_TYPE_PARTIAL_LINE)
620 return TRACE_TYPE_PARTIAL_LINE;
621 }
622
623 /* Proc */
624 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
625 ret = print_graph_proc(s, ent->pid);
626 if (ret == TRACE_TYPE_PARTIAL_LINE)
627 return TRACE_TYPE_PARTIAL_LINE;
628
629 ret = trace_seq_printf(s, " | ");
630 if (!ret)
631 return TRACE_TYPE_PARTIAL_LINE;
632 }
633
634 /* No overhead */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800635 ret = print_graph_overhead(-1, s);
636 if (!ret)
637 return TRACE_TYPE_PARTIAL_LINE;
638
639 /* No time */
640 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
641 ret = trace_seq_printf(s, " | ");
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100642 if (!ret)
643 return TRACE_TYPE_PARTIAL_LINE;
644 }
645
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100646 /* Indentation */
647 if (trace->depth > 0)
648 for (i = 0; i < (trace->depth + 1) * TRACE_GRAPH_INDENT; i++) {
649 ret = trace_seq_printf(s, " ");
650 if (!ret)
651 return TRACE_TYPE_PARTIAL_LINE;
652 }
653
654 /* The comment */
655 ret = trace_seq_printf(s, "/* %s", trace->buf);
656 if (!ret)
657 return TRACE_TYPE_PARTIAL_LINE;
658
Frederic Weisbecker412d0bb2008-12-24 01:43:25 +0100659 /* Strip ending newline */
660 if (s->buffer[s->len - 1] == '\n') {
661 s->buffer[s->len - 1] = '\0';
662 s->len--;
663 }
664
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100665 ret = trace_seq_printf(s, " */\n");
666 if (!ret)
667 return TRACE_TYPE_PARTIAL_LINE;
668
669 return TRACE_TYPE_HANDLED;
670}
671
672
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100673enum print_line_t
674print_graph_function(struct trace_iterator *iter)
675{
676 struct trace_seq *s = &iter->seq;
677 struct trace_entry *entry = iter->ent;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100678
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100679 switch (entry->type) {
680 case TRACE_GRAPH_ENT: {
681 struct ftrace_graph_ent_entry *field;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100682 trace_assign_type(field, entry);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800683 return print_graph_entry(field, s, iter);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100684 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100685 case TRACE_GRAPH_RET: {
686 struct ftrace_graph_ret_entry *field;
687 trace_assign_type(field, entry);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800688 return print_graph_return(&field->ret, s, entry, iter);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100689 }
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100690 case TRACE_PRINT: {
691 struct print_entry *field;
692 trace_assign_type(field, entry);
693 return print_graph_comment(field, s, entry, iter);
694 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100695 default:
696 return TRACE_TYPE_UNHANDLED;
697 }
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100698}
699
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100700static void print_graph_headers(struct seq_file *s)
701{
702 /* 1st line */
703 seq_printf(s, "# ");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800704 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
705 seq_printf(s, " TIME ");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100706 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800707 seq_printf(s, "CPU");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100708 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800709 seq_printf(s, " TASK/PID ");
710 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
711 seq_printf(s, " DURATION ");
712 seq_printf(s, " FUNCTION CALLS\n");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100713
714 /* 2nd line */
715 seq_printf(s, "# ");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800716 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
717 seq_printf(s, " | ");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100718 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800719 seq_printf(s, "| ");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100720 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800721 seq_printf(s, " | | ");
722 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
723 seq_printf(s, " | | ");
724 seq_printf(s, " | | | |\n");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100725}
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800726
727static void graph_trace_open(struct trace_iterator *iter)
728{
729 /* pid on the last trace processed */
730 pid_t *last_pid = alloc_percpu(pid_t);
731 int cpu;
732
733 if (!last_pid)
734 pr_warning("function graph tracer: not enough memory\n");
735 else
736 for_each_possible_cpu(cpu) {
737 pid_t *pid = per_cpu_ptr(last_pid, cpu);
738 *pid = -1;
739 }
740
741 iter->private = last_pid;
742}
743
744static void graph_trace_close(struct trace_iterator *iter)
745{
746 percpu_free(iter->private);
747}
748
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100749static struct tracer graph_trace __read_mostly = {
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100750 .name = "function_graph",
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800751 .open = graph_trace_open,
752 .close = graph_trace_close,
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100753 .init = graph_trace_init,
754 .reset = graph_trace_reset,
755 .print_line = print_graph_function,
756 .print_header = print_graph_headers,
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100757 .flags = &tracer_flags,
758};
759
760static __init int init_graph_trace(void)
761{
762 return register_tracer(&graph_trace);
763}
764
765device_initcall(init_graph_trace);