blob: bc7d90850be5c2977bfe4f81803d5d3f7c7bedec [file] [log] [blame]
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001/*
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
Frederic Weisbecker287b6e62008-11-26 00:57:25 +010016#define TRACE_GRAPH_INDENT 2
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010017
Frederic Weisbecker1a056152008-11-28 00:42:46 +010018/* Flag options */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010019#define TRACE_GRAPH_PRINT_OVERRUN 0x1
Frederic Weisbecker1a056152008-11-28 00:42:46 +010020#define TRACE_GRAPH_PRINT_CPU 0x2
21#define TRACE_GRAPH_PRINT_OVERHEAD 0x4
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +010022#define TRACE_GRAPH_PRINT_PROC 0x8
Frederic Weisbecker1a056152008-11-28 00:42:46 +010023
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010024static struct tracer_opt trace_opts[] = {
Frederic Weisbecker1a056152008-11-28 00:42:46 +010025 /* Display overruns ? */
26 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
27 /* Display CPU ? */
28 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
29 /* Display Overhead ? */
30 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +010031 /* Display proc name/pid */
32 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010033 { } /* Empty entry */
34};
35
36static struct tracer_flags tracer_flags = {
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +010037 /* Don't display overruns and proc by default */
Frederic Weisbecker1a056152008-11-28 00:42:46 +010038 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD,
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010039 .opts = trace_opts
40};
41
Frederic Weisbecker287b6e62008-11-26 00:57:25 +010042/* pid on the last trace processed */
Steven Rostedt437f24fb2008-11-26 00:16:27 -050043static pid_t last_pid[NR_CPUS] = { [0 ... NR_CPUS-1] = -1 };
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010044
45static int graph_trace_init(struct trace_array *tr)
46{
Steven Rostedt660c7f92008-11-26 00:16:26 -050047 int cpu, ret;
48
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010049 for_each_online_cpu(cpu)
50 tracing_reset(tr, cpu);
51
Steven Rostedt660c7f92008-11-26 00:16:26 -050052 ret = register_ftrace_graph(&trace_graph_return,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +010053 &trace_graph_entry);
Steven Rostedt660c7f92008-11-26 00:16:26 -050054 if (ret)
55 return ret;
56 tracing_start_cmdline_record();
57
58 return 0;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010059}
60
61static void graph_trace_reset(struct trace_array *tr)
62{
Steven Rostedt660c7f92008-11-26 00:16:26 -050063 tracing_stop_cmdline_record();
64 unregister_ftrace_graph();
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010065}
66
Frederic Weisbecker1a056152008-11-28 00:42:46 +010067static inline int log10_cpu(int nb)
68{
69 if (nb / 100)
70 return 3;
71 if (nb / 10)
72 return 2;
73 return 1;
74}
75
76static enum print_line_t
77print_graph_cpu(struct trace_seq *s, int cpu)
78{
79 int i;
80 int ret;
81 int log10_this = log10_cpu(cpu);
82 int log10_all = log10_cpu(cpus_weight_nr(cpu_online_map));
83
84
Ingo Molnard51090b2008-11-28 09:55:16 +010085 /*
86 * Start with a space character - to make it stand out
87 * to the right a bit when trace output is pasted into
88 * email:
89 */
90 ret = trace_seq_printf(s, " ");
91
92 /*
93 * Tricky - we space the CPU field according to the max
94 * number of online CPUs. On a 2-cpu system it would take
95 * a maximum of 1 digit - on a 128 cpu system it would
96 * take up to 3 digits:
97 */
Frederic Weisbecker1a056152008-11-28 00:42:46 +010098 for (i = 0; i < log10_all - log10_this; i++) {
99 ret = trace_seq_printf(s, " ");
100 if (!ret)
101 return TRACE_TYPE_PARTIAL_LINE;
102 }
103 ret = trace_seq_printf(s, "%d) ", cpu);
104 if (!ret)
Ingo Molnard51090b2008-11-28 09:55:16 +0100105 return TRACE_TYPE_PARTIAL_LINE;
106
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100107 return TRACE_TYPE_HANDLED;
108}
109
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100110#define TRACE_GRAPH_PROCINFO_LENGTH 14
111
112static enum print_line_t
113print_graph_proc(struct trace_seq *s, pid_t pid)
114{
115 int i;
116 int ret;
117 int len;
118 char comm[8];
119 int spaces = 0;
120 /* sign + log10(MAX_INT) + '\0' */
121 char pid_str[11];
122
123 strncpy(comm, trace_find_cmdline(pid), 7);
124 comm[7] = '\0';
125 sprintf(pid_str, "%d", pid);
126
127 /* 1 stands for the "-" character */
128 len = strlen(comm) + strlen(pid_str) + 1;
129
130 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
131 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
132
133 /* First spaces to align center */
134 for (i = 0; i < spaces / 2; i++) {
135 ret = trace_seq_printf(s, " ");
136 if (!ret)
137 return TRACE_TYPE_PARTIAL_LINE;
138 }
139
140 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
141 if (!ret)
142 return TRACE_TYPE_PARTIAL_LINE;
143
144 /* Last spaces to align center */
145 for (i = 0; i < spaces - (spaces / 2); i++) {
146 ret = trace_seq_printf(s, " ");
147 if (!ret)
148 return TRACE_TYPE_PARTIAL_LINE;
149 }
150 return TRACE_TYPE_HANDLED;
151}
152
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100153
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100154/* If the pid changed since the last trace, output this event */
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100155static enum print_line_t
156verif_pid(struct trace_seq *s, pid_t pid, int cpu)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100157{
Ingo Molnard51090b2008-11-28 09:55:16 +0100158 pid_t prev_pid;
159 int ret;
Steven Rostedt660c7f92008-11-26 00:16:26 -0500160
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500161 if (last_pid[cpu] != -1 && last_pid[cpu] == pid)
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100162 return TRACE_TYPE_HANDLED;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100163
Ingo Molnard51090b2008-11-28 09:55:16 +0100164 prev_pid = last_pid[cpu];
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500165 last_pid[cpu] = pid;
Steven Rostedt660c7f92008-11-26 00:16:26 -0500166
Ingo Molnard51090b2008-11-28 09:55:16 +0100167/*
168 * Context-switch trace line:
169
170 ------------------------------------------
171 | 1) migration/0--1 => sshd-1755
172 ------------------------------------------
173
174 */
175 ret = trace_seq_printf(s,
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100176 " ------------------------------------------\n");
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100177 if (!ret)
178 TRACE_TYPE_PARTIAL_LINE;
179
180 ret = print_graph_cpu(s, cpu);
181 if (ret == TRACE_TYPE_PARTIAL_LINE)
182 TRACE_TYPE_PARTIAL_LINE;
183
184 ret = print_graph_proc(s, prev_pid);
185 if (ret == TRACE_TYPE_PARTIAL_LINE)
186 TRACE_TYPE_PARTIAL_LINE;
187
188 ret = trace_seq_printf(s, " => ");
189 if (!ret)
190 TRACE_TYPE_PARTIAL_LINE;
191
192 ret = print_graph_proc(s, pid);
193 if (ret == TRACE_TYPE_PARTIAL_LINE)
194 TRACE_TYPE_PARTIAL_LINE;
195
196 ret = trace_seq_printf(s,
197 "\n ------------------------------------------\n\n");
198 if (!ret)
199 TRACE_TYPE_PARTIAL_LINE;
200
Ingo Molnard51090b2008-11-28 09:55:16 +0100201 return ret;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100202}
203
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100204static bool
205trace_branch_is_leaf(struct trace_iterator *iter,
206 struct ftrace_graph_ent_entry *curr)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100207{
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100208 struct ring_buffer_iter *ring_iter;
209 struct ring_buffer_event *event;
210 struct ftrace_graph_ret_entry *next;
211
212 ring_iter = iter->buffer_iter[iter->cpu];
213
214 if (!ring_iter)
215 return false;
216
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100217 event = ring_buffer_iter_peek(ring_iter, NULL);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100218
219 if (!event)
220 return false;
221
222 next = ring_buffer_event_data(event);
223
224 if (next->ent.type != TRACE_GRAPH_RET)
225 return false;
226
227 if (curr->ent.pid != next->ent.pid ||
228 curr->graph_ent.func != next->ret.func)
229 return false;
230
231 return true;
232}
233
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100234static enum print_line_t
235print_graph_irq(struct trace_seq *s, unsigned long addr,
236 enum trace_type type, int cpu, pid_t pid)
237{
238 int ret;
239
240 if (addr < (unsigned long)__irqentry_text_start ||
241 addr >= (unsigned long)__irqentry_text_end)
242 return TRACE_TYPE_UNHANDLED;
243
244 if (type == TRACE_GRAPH_ENT) {
245 ret = trace_seq_printf(s, "==========> | ");
246 } else {
247 /* Cpu */
248 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
249 ret = print_graph_cpu(s, cpu);
250 if (ret == TRACE_TYPE_PARTIAL_LINE)
251 return TRACE_TYPE_PARTIAL_LINE;
252 }
253 /* Proc */
254 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
255 ret = print_graph_proc(s, pid);
256 if (ret == TRACE_TYPE_PARTIAL_LINE)
257 return TRACE_TYPE_PARTIAL_LINE;
258
259 ret = trace_seq_printf(s, " | ");
260 if (!ret)
261 return TRACE_TYPE_PARTIAL_LINE;
262 }
263
264 /* No overhead */
265 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
266 ret = trace_seq_printf(s, " ");
267 if (!ret)
268 return TRACE_TYPE_PARTIAL_LINE;
269 }
270
271 ret = trace_seq_printf(s, "<========== |\n");
272 }
273 if (!ret)
274 return TRACE_TYPE_PARTIAL_LINE;
275 return TRACE_TYPE_HANDLED;
276}
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100277
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100278static enum print_line_t
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100279print_graph_duration(unsigned long long duration, struct trace_seq *s)
280{
281 unsigned long nsecs_rem = do_div(duration, 1000);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100282 /* log10(ULONG_MAX) + '\0' */
283 char msecs_str[21];
284 char nsecs_str[5];
285 int ret, len;
286 int i;
287
288 sprintf(msecs_str, "%lu", (unsigned long) duration);
289
290 /* Print msecs */
291 ret = trace_seq_printf(s, msecs_str);
292 if (!ret)
293 return TRACE_TYPE_PARTIAL_LINE;
294
295 len = strlen(msecs_str);
296
297 /* Print nsecs (we don't want to exceed 7 numbers) */
298 if (len < 7) {
299 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
300 ret = trace_seq_printf(s, ".%s", nsecs_str);
301 if (!ret)
302 return TRACE_TYPE_PARTIAL_LINE;
303 len += strlen(nsecs_str);
304 }
305
306 ret = trace_seq_printf(s, " us ");
307 if (!ret)
308 return TRACE_TYPE_PARTIAL_LINE;
309
310 /* Print remaining spaces to fit the row's width */
311 for (i = len; i < 7; i++) {
312 ret = trace_seq_printf(s, " ");
313 if (!ret)
314 return TRACE_TYPE_PARTIAL_LINE;
315 }
316
317 ret = trace_seq_printf(s, "| ");
318 if (!ret)
319 return TRACE_TYPE_PARTIAL_LINE;
320 return TRACE_TYPE_HANDLED;
321
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100322}
323
324/* Signal a overhead of time execution to the output */
325static int
326print_graph_overhead(unsigned long long duration, struct trace_seq *s)
327{
328 /* Duration exceeded 100 msecs */
329 if (duration > 100000ULL)
330 return trace_seq_printf(s, "! ");
331
332 /* Duration exceeded 10 msecs */
333 if (duration > 10000ULL)
334 return trace_seq_printf(s, "+ ");
335
336 return trace_seq_printf(s, " ");
337}
338
339/* Case of a leaf function on its call entry */
340static enum print_line_t
341print_graph_entry_leaf(struct trace_iterator *iter,
342 struct ftrace_graph_ent_entry *entry, struct trace_seq *s)
343{
344 struct ftrace_graph_ret_entry *ret_entry;
345 struct ftrace_graph_ret *graph_ret;
346 struct ring_buffer_event *event;
347 struct ftrace_graph_ent *call;
348 unsigned long long duration;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100349 int ret;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100350 int i;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100351
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100352 event = ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
353 ret_entry = ring_buffer_event_data(event);
354 graph_ret = &ret_entry->ret;
355 call = &entry->graph_ent;
356 duration = graph_ret->rettime - graph_ret->calltime;
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500357
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100358 /* Overhead */
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100359 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
360 ret = print_graph_overhead(duration, s);
361 if (!ret)
362 return TRACE_TYPE_PARTIAL_LINE;
363 }
364
365 /* Duration */
366 ret = print_graph_duration(duration, s);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100367 if (ret == TRACE_TYPE_PARTIAL_LINE)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100368 return TRACE_TYPE_PARTIAL_LINE;
369
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100370 /* Function */
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100371 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
372 ret = trace_seq_printf(s, " ");
373 if (!ret)
374 return TRACE_TYPE_PARTIAL_LINE;
375 }
376
377 ret = seq_print_ip_sym(s, call->func, 0);
378 if (!ret)
379 return TRACE_TYPE_PARTIAL_LINE;
380
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100381 ret = trace_seq_printf(s, "();\n");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100382 if (!ret)
383 return TRACE_TYPE_PARTIAL_LINE;
384
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100385 return TRACE_TYPE_HANDLED;
386}
387
388static enum print_line_t
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100389print_graph_entry_nested(struct ftrace_graph_ent_entry *entry,
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100390 struct trace_seq *s, pid_t pid, int cpu)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100391{
392 int i;
393 int ret;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100394 struct ftrace_graph_ent *call = &entry->graph_ent;
395
396 /* No overhead */
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100397 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
398 ret = trace_seq_printf(s, " ");
399 if (!ret)
400 return TRACE_TYPE_PARTIAL_LINE;
401 }
402
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100403 /* Interrupt */
404 ret = print_graph_irq(s, call->func, TRACE_GRAPH_ENT, cpu, pid);
405 if (ret == TRACE_TYPE_UNHANDLED) {
406 /* No time */
407 ret = trace_seq_printf(s, " | ");
408 if (!ret)
409 return TRACE_TYPE_PARTIAL_LINE;
410 } else {
411 if (ret == TRACE_TYPE_PARTIAL_LINE)
412 return TRACE_TYPE_PARTIAL_LINE;
413 }
414
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100415
416 /* Function */
417 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
418 ret = trace_seq_printf(s, " ");
419 if (!ret)
420 return TRACE_TYPE_PARTIAL_LINE;
421 }
422
423 ret = seq_print_ip_sym(s, call->func, 0);
424 if (!ret)
425 return TRACE_TYPE_PARTIAL_LINE;
426
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100427 ret = trace_seq_printf(s, "() {\n");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100428 if (!ret)
429 return TRACE_TYPE_PARTIAL_LINE;
430
431 return TRACE_TYPE_HANDLED;
432}
433
434static enum print_line_t
435print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
436 struct trace_iterator *iter, int cpu)
437{
438 int ret;
439 struct trace_entry *ent = iter->ent;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100440
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100441 /* Pid */
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100442 if (verif_pid(s, ent->pid, cpu) == TRACE_TYPE_PARTIAL_LINE)
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500443 return TRACE_TYPE_PARTIAL_LINE;
444
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100445 /* Cpu */
446 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
447 ret = print_graph_cpu(s, cpu);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100448 if (ret == TRACE_TYPE_PARTIAL_LINE)
449 return TRACE_TYPE_PARTIAL_LINE;
450 }
451
452 /* Proc */
453 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
454 ret = print_graph_proc(s, ent->pid);
455 if (ret == TRACE_TYPE_PARTIAL_LINE)
456 return TRACE_TYPE_PARTIAL_LINE;
457
458 ret = trace_seq_printf(s, " | ");
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100459 if (!ret)
460 return TRACE_TYPE_PARTIAL_LINE;
461 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100462
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100463 if (trace_branch_is_leaf(iter, field))
464 return print_graph_entry_leaf(iter, field, s);
465 else
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100466 return print_graph_entry_nested(field, s, iter->ent->pid, cpu);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100467
468}
469
470static enum print_line_t
471print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
472 struct trace_entry *ent, int cpu)
473{
474 int i;
475 int ret;
476 unsigned long long duration = trace->rettime - trace->calltime;
477
478 /* Pid */
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100479 if (verif_pid(s, ent->pid, cpu) == TRACE_TYPE_PARTIAL_LINE)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100480 return TRACE_TYPE_PARTIAL_LINE;
481
482 /* Cpu */
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100483 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
484 ret = print_graph_cpu(s, cpu);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100485 if (ret == TRACE_TYPE_PARTIAL_LINE)
486 return TRACE_TYPE_PARTIAL_LINE;
487 }
488
489 /* Proc */
490 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
491 ret = print_graph_proc(s, ent->pid);
492 if (ret == TRACE_TYPE_PARTIAL_LINE)
493 return TRACE_TYPE_PARTIAL_LINE;
494
495 ret = trace_seq_printf(s, " | ");
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100496 if (!ret)
497 return TRACE_TYPE_PARTIAL_LINE;
498 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100499
500 /* Overhead */
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100501 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
502 ret = print_graph_overhead(duration, s);
503 if (!ret)
504 return TRACE_TYPE_PARTIAL_LINE;
505 }
506
507 /* Duration */
508 ret = print_graph_duration(duration, s);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100509 if (ret == TRACE_TYPE_PARTIAL_LINE)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100510 return TRACE_TYPE_PARTIAL_LINE;
511
512 /* Closing brace */
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100513 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
514 ret = trace_seq_printf(s, " ");
515 if (!ret)
516 return TRACE_TYPE_PARTIAL_LINE;
517 }
518
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100519 ret = trace_seq_printf(s, "}\n");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100520 if (!ret)
521 return TRACE_TYPE_PARTIAL_LINE;
522
523 /* Overrun */
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100524 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
525 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
526 trace->overrun);
527 if (!ret)
528 return TRACE_TYPE_PARTIAL_LINE;
529 }
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100530
531 ret = print_graph_irq(s, trace->func, TRACE_GRAPH_RET, cpu, ent->pid);
532 if (ret == TRACE_TYPE_PARTIAL_LINE)
533 return TRACE_TYPE_PARTIAL_LINE;
534
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100535 return TRACE_TYPE_HANDLED;
536}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100537
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100538static enum print_line_t
539print_graph_comment(struct print_entry *trace, struct trace_seq *s,
540 struct trace_entry *ent, struct trace_iterator *iter)
541{
542 int i;
543 int ret;
544
545 /* Pid */
546 if (verif_pid(s, ent->pid, iter->cpu) == TRACE_TYPE_PARTIAL_LINE)
547 return TRACE_TYPE_PARTIAL_LINE;
548
549 /* Cpu */
550 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
551 ret = print_graph_cpu(s, iter->cpu);
552 if (ret == TRACE_TYPE_PARTIAL_LINE)
553 return TRACE_TYPE_PARTIAL_LINE;
554 }
555
556 /* Proc */
557 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
558 ret = print_graph_proc(s, ent->pid);
559 if (ret == TRACE_TYPE_PARTIAL_LINE)
560 return TRACE_TYPE_PARTIAL_LINE;
561
562 ret = trace_seq_printf(s, " | ");
563 if (!ret)
564 return TRACE_TYPE_PARTIAL_LINE;
565 }
566
567 /* No overhead */
568 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
569 ret = trace_seq_printf(s, " ");
570 if (!ret)
571 return TRACE_TYPE_PARTIAL_LINE;
572 }
573
574 /* No time */
575 ret = trace_seq_printf(s, " | ");
576 if (!ret)
577 return TRACE_TYPE_PARTIAL_LINE;
578
579 /* Indentation */
580 if (trace->depth > 0)
581 for (i = 0; i < (trace->depth + 1) * TRACE_GRAPH_INDENT; i++) {
582 ret = trace_seq_printf(s, " ");
583 if (!ret)
584 return TRACE_TYPE_PARTIAL_LINE;
585 }
586
587 /* The comment */
588 ret = trace_seq_printf(s, "/* %s", trace->buf);
589 if (!ret)
590 return TRACE_TYPE_PARTIAL_LINE;
591
592 if (ent->flags & TRACE_FLAG_CONT)
593 trace_seq_print_cont(s, iter);
594
Frederic Weisbecker412d0bb2008-12-24 01:43:25 +0100595 /* Strip ending newline */
596 if (s->buffer[s->len - 1] == '\n') {
597 s->buffer[s->len - 1] = '\0';
598 s->len--;
599 }
600
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100601 ret = trace_seq_printf(s, " */\n");
602 if (!ret)
603 return TRACE_TYPE_PARTIAL_LINE;
604
605 return TRACE_TYPE_HANDLED;
606}
607
608
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100609enum print_line_t
610print_graph_function(struct trace_iterator *iter)
611{
612 struct trace_seq *s = &iter->seq;
613 struct trace_entry *entry = iter->ent;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100614
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100615 switch (entry->type) {
616 case TRACE_GRAPH_ENT: {
617 struct ftrace_graph_ent_entry *field;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100618 trace_assign_type(field, entry);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100619 return print_graph_entry(field, s, iter,
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500620 iter->cpu);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100621 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100622 case TRACE_GRAPH_RET: {
623 struct ftrace_graph_ret_entry *field;
624 trace_assign_type(field, entry);
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500625 return print_graph_return(&field->ret, s, entry, iter->cpu);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100626 }
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100627 case TRACE_PRINT: {
628 struct print_entry *field;
629 trace_assign_type(field, entry);
630 return print_graph_comment(field, s, entry, iter);
631 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100632 default:
633 return TRACE_TYPE_UNHANDLED;
634 }
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100635}
636
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100637static void print_graph_headers(struct seq_file *s)
638{
639 /* 1st line */
640 seq_printf(s, "# ");
641 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
642 seq_printf(s, "CPU ");
643 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
644 seq_printf(s, "TASK/PID ");
645 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD)
646 seq_printf(s, "OVERHEAD/");
647 seq_printf(s, "DURATION FUNCTION CALLS\n");
648
649 /* 2nd line */
650 seq_printf(s, "# ");
651 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
652 seq_printf(s, "| ");
653 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
654 seq_printf(s, "| | ");
655 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
656 seq_printf(s, "| ");
657 seq_printf(s, "| | | | |\n");
658 } else
659 seq_printf(s, " | | | | |\n");
660}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100661static struct tracer graph_trace __read_mostly = {
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +0100662 .name = "function_graph",
663 .init = graph_trace_init,
664 .reset = graph_trace_reset,
665 .print_line = print_graph_function,
666 .print_header = print_graph_headers,
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100667 .flags = &tracer_flags,
668};
669
670static __init int init_graph_trace(void)
671{
672 return register_tracer(&graph_trace);
673}
674
675device_initcall(init_graph_trace);