Merge branch 'tracing/core' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.git] / kernel / trace / trace_functions_graph.c
CommitLineData
fb52607a
FW
1/*
2 *
3 * Function graph tracer.
9005f3eb 4 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
fb52607a
FW
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"
f0868d1e 15#include "trace_output.h"
fb52607a 16
be1eca39 17struct fgraph_cpu_data {
2fbcdb35
SR
18 pid_t last_pid;
19 int depth;
be1eca39
JO
20 int ignore;
21};
22
23struct fgraph_data {
24 struct fgraph_cpu_data *cpu_data;
25
26 /* Place to preserve last processed entry. */
27 struct ftrace_graph_ent_entry ent;
28 struct ftrace_graph_ret_entry ret;
29 int failed;
30 int cpu;
2fbcdb35
SR
31};
32
287b6e68 33#define TRACE_GRAPH_INDENT 2
fb52607a 34
1a056155 35/* Flag options */
fb52607a 36#define TRACE_GRAPH_PRINT_OVERRUN 0x1
1a056155
FW
37#define TRACE_GRAPH_PRINT_CPU 0x2
38#define TRACE_GRAPH_PRINT_OVERHEAD 0x4
11e84acc 39#define TRACE_GRAPH_PRINT_PROC 0x8
9005f3eb
FW
40#define TRACE_GRAPH_PRINT_DURATION 0x10
41#define TRACE_GRAPH_PRINT_ABS_TIME 0X20
1a056155 42
fb52607a 43static struct tracer_opt trace_opts[] = {
9005f3eb 44 /* Display overruns? (for self-debug purpose) */
1a056155
FW
45 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
46 /* Display CPU ? */
47 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
48 /* Display Overhead ? */
49 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
11e84acc
FW
50 /* Display proc name/pid */
51 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
9005f3eb
FW
52 /* Display duration of execution */
53 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
54 /* Display absolute time of an entry */
55 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
fb52607a
FW
56 { } /* Empty entry */
57};
58
59static struct tracer_flags tracer_flags = {
11e84acc 60 /* Don't display overruns and proc by default */
9005f3eb
FW
61 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
62 TRACE_GRAPH_PRINT_DURATION,
fb52607a
FW
63 .opts = trace_opts
64};
65
1a0799a8 66static struct trace_array *graph_array;
9005f3eb 67
fb52607a 68
712406a6
SR
69/* Add a function return address to the trace stack on thread info.*/
70int
71e308a2
SR
71ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
72 unsigned long frame_pointer)
712406a6 73{
5d1a03dc 74 unsigned long long calltime;
712406a6
SR
75 int index;
76
77 if (!current->ret_stack)
78 return -EBUSY;
79
82310a32
SR
80 /*
81 * We must make sure the ret_stack is tested before we read
82 * anything else.
83 */
84 smp_rmb();
85
712406a6
SR
86 /* The return trace stack is full */
87 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
88 atomic_inc(&current->trace_overrun);
89 return -EBUSY;
90 }
91
5d1a03dc
SR
92 calltime = trace_clock_local();
93
712406a6
SR
94 index = ++current->curr_ret_stack;
95 barrier();
96 current->ret_stack[index].ret = ret;
97 current->ret_stack[index].func = func;
5d1a03dc 98 current->ret_stack[index].calltime = calltime;
a2a16d6a 99 current->ret_stack[index].subtime = 0;
71e308a2 100 current->ret_stack[index].fp = frame_pointer;
712406a6
SR
101 *depth = index;
102
103 return 0;
104}
105
106/* Retrieve a function return address to the trace stack on thread info.*/
a2a16d6a 107static void
71e308a2
SR
108ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
109 unsigned long frame_pointer)
712406a6
SR
110{
111 int index;
112
113 index = current->curr_ret_stack;
114
115 if (unlikely(index < 0)) {
116 ftrace_graph_stop();
117 WARN_ON(1);
118 /* Might as well panic, otherwise we have no where to go */
119 *ret = (unsigned long)panic;
120 return;
121 }
122
71e308a2
SR
123#ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
124 /*
125 * The arch may choose to record the frame pointer used
126 * and check it here to make sure that it is what we expect it
127 * to be. If gcc does not set the place holder of the return
128 * address in the frame pointer, and does a copy instead, then
129 * the function graph trace will fail. This test detects this
130 * case.
131 *
132 * Currently, x86_32 with optimize for size (-Os) makes the latest
133 * gcc do the above.
134 */
135 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
136 ftrace_graph_stop();
137 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
b375a11a 138 " from func %ps return to %lx\n",
71e308a2
SR
139 current->ret_stack[index].fp,
140 frame_pointer,
141 (void *)current->ret_stack[index].func,
142 current->ret_stack[index].ret);
143 *ret = (unsigned long)panic;
144 return;
145 }
146#endif
147
712406a6
SR
148 *ret = current->ret_stack[index].ret;
149 trace->func = current->ret_stack[index].func;
150 trace->calltime = current->ret_stack[index].calltime;
151 trace->overrun = atomic_read(&current->trace_overrun);
152 trace->depth = index;
712406a6
SR
153}
154
155/*
156 * Send the trace to the ring-buffer.
157 * @return the original return address.
158 */
71e308a2 159unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
712406a6
SR
160{
161 struct ftrace_graph_ret trace;
162 unsigned long ret;
163
71e308a2 164 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
0012693a 165 trace.rettime = trace_clock_local();
712406a6 166 ftrace_graph_return(&trace);
a2a16d6a
SR
167 barrier();
168 current->curr_ret_stack--;
712406a6
SR
169
170 if (unlikely(!ret)) {
171 ftrace_graph_stop();
172 WARN_ON(1);
173 /* Might as well panic. What else to do? */
174 ret = (unsigned long)panic;
175 }
176
177 return ret;
178}
179
1a0799a8
FW
180static int __trace_graph_entry(struct trace_array *tr,
181 struct ftrace_graph_ent *trace,
182 unsigned long flags,
183 int pc)
184{
185 struct ftrace_event_call *call = &event_funcgraph_entry;
186 struct ring_buffer_event *event;
e77405ad 187 struct ring_buffer *buffer = tr->buffer;
1a0799a8
FW
188 struct ftrace_graph_ent_entry *entry;
189
dec54bf5 190 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
1a0799a8
FW
191 return 0;
192
e77405ad 193 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
1a0799a8
FW
194 sizeof(*entry), flags, pc);
195 if (!event)
196 return 0;
197 entry = ring_buffer_event_data(event);
198 entry->graph_ent = *trace;
e77405ad
SR
199 if (!filter_current_check_discard(buffer, call, entry, event))
200 ring_buffer_unlock_commit(buffer, event);
1a0799a8
FW
201
202 return 1;
203}
204
205int trace_graph_entry(struct ftrace_graph_ent *trace)
206{
207 struct trace_array *tr = graph_array;
208 struct trace_array_cpu *data;
209 unsigned long flags;
210 long disabled;
211 int ret;
212 int cpu;
213 int pc;
214
1a0799a8
FW
215 if (!ftrace_trace_task(current))
216 return 0;
217
ea2c68a0
LJ
218 /* trace it when it is-nested-in or is a function enabled. */
219 if (!(trace->depth || ftrace_graph_addr(trace->func)))
1a0799a8
FW
220 return 0;
221
222 local_irq_save(flags);
223 cpu = raw_smp_processor_id();
224 data = tr->data[cpu];
225 disabled = atomic_inc_return(&data->disabled);
226 if (likely(disabled == 1)) {
227 pc = preempt_count();
228 ret = __trace_graph_entry(tr, trace, flags, pc);
229 } else {
230 ret = 0;
231 }
1a0799a8
FW
232
233 atomic_dec(&data->disabled);
234 local_irq_restore(flags);
235
236 return ret;
237}
238
239static void __trace_graph_return(struct trace_array *tr,
240 struct ftrace_graph_ret *trace,
241 unsigned long flags,
242 int pc)
243{
244 struct ftrace_event_call *call = &event_funcgraph_exit;
245 struct ring_buffer_event *event;
e77405ad 246 struct ring_buffer *buffer = tr->buffer;
1a0799a8
FW
247 struct ftrace_graph_ret_entry *entry;
248
dec54bf5 249 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
1a0799a8
FW
250 return;
251
e77405ad 252 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
1a0799a8
FW
253 sizeof(*entry), flags, pc);
254 if (!event)
255 return;
256 entry = ring_buffer_event_data(event);
257 entry->ret = *trace;
e77405ad
SR
258 if (!filter_current_check_discard(buffer, call, entry, event))
259 ring_buffer_unlock_commit(buffer, event);
1a0799a8
FW
260}
261
262void trace_graph_return(struct ftrace_graph_ret *trace)
263{
264 struct trace_array *tr = graph_array;
265 struct trace_array_cpu *data;
266 unsigned long flags;
267 long disabled;
268 int cpu;
269 int pc;
270
271 local_irq_save(flags);
272 cpu = raw_smp_processor_id();
273 data = tr->data[cpu];
274 disabled = atomic_inc_return(&data->disabled);
275 if (likely(disabled == 1)) {
276 pc = preempt_count();
277 __trace_graph_return(tr, trace, flags, pc);
278 }
1a0799a8
FW
279 atomic_dec(&data->disabled);
280 local_irq_restore(flags);
281}
282
24a53652
FW
283void set_graph_array(struct trace_array *tr)
284{
285 graph_array = tr;
286
287 /* Make graph_array visible before we start tracing */
288
289 smp_mb();
290}
291
fb52607a
FW
292static int graph_trace_init(struct trace_array *tr)
293{
1a0799a8
FW
294 int ret;
295
24a53652 296 set_graph_array(tr);
1a0799a8
FW
297 ret = register_ftrace_graph(&trace_graph_return,
298 &trace_graph_entry);
660c7f9b
SR
299 if (ret)
300 return ret;
301 tracing_start_cmdline_record();
302
303 return 0;
fb52607a
FW
304}
305
306static void graph_trace_reset(struct trace_array *tr)
307{
660c7f9b
SR
308 tracing_stop_cmdline_record();
309 unregister_ftrace_graph();
fb52607a
FW
310}
311
0c9e6f63 312static int max_bytes_for_cpu;
1a056155
FW
313
314static enum print_line_t
315print_graph_cpu(struct trace_seq *s, int cpu)
316{
1a056155 317 int ret;
1a056155 318
d51090b3
IM
319 /*
320 * Start with a space character - to make it stand out
321 * to the right a bit when trace output is pasted into
322 * email:
323 */
0c9e6f63 324 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
1a056155 325 if (!ret)
d51090b3
IM
326 return TRACE_TYPE_PARTIAL_LINE;
327
1a056155
FW
328 return TRACE_TYPE_HANDLED;
329}
330
11e84acc
FW
331#define TRACE_GRAPH_PROCINFO_LENGTH 14
332
333static enum print_line_t
334print_graph_proc(struct trace_seq *s, pid_t pid)
335{
4ca53085 336 char comm[TASK_COMM_LEN];
11e84acc
FW
337 /* sign + log10(MAX_INT) + '\0' */
338 char pid_str[11];
4ca53085
SR
339 int spaces = 0;
340 int ret;
341 int len;
342 int i;
11e84acc 343
4ca53085 344 trace_find_cmdline(pid, comm);
11e84acc
FW
345 comm[7] = '\0';
346 sprintf(pid_str, "%d", pid);
347
348 /* 1 stands for the "-" character */
349 len = strlen(comm) + strlen(pid_str) + 1;
350
351 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
352 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
353
354 /* First spaces to align center */
355 for (i = 0; i < spaces / 2; i++) {
356 ret = trace_seq_printf(s, " ");
357 if (!ret)
358 return TRACE_TYPE_PARTIAL_LINE;
359 }
360
361 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
362 if (!ret)
363 return TRACE_TYPE_PARTIAL_LINE;
364
365 /* Last spaces to align center */
366 for (i = 0; i < spaces - (spaces / 2); i++) {
367 ret = trace_seq_printf(s, " ");
368 if (!ret)
369 return TRACE_TYPE_PARTIAL_LINE;
370 }
371 return TRACE_TYPE_HANDLED;
372}
373
1a056155 374
49ff5903
SR
375static enum print_line_t
376print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
377{
f81c972d 378 if (!trace_seq_putc(s, ' '))
637e7e86
SR
379 return 0;
380
f81c972d 381 return trace_print_lat_fmt(s, entry);
49ff5903
SR
382}
383
287b6e68 384/* If the pid changed since the last trace, output this event */
11e84acc 385static enum print_line_t
2fbcdb35 386verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
287b6e68 387{
d51090b3 388 pid_t prev_pid;
9005f3eb 389 pid_t *last_pid;
d51090b3 390 int ret;
660c7f9b 391
2fbcdb35 392 if (!data)
9005f3eb
FW
393 return TRACE_TYPE_HANDLED;
394
be1eca39 395 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
9005f3eb
FW
396
397 if (*last_pid == pid)
11e84acc 398 return TRACE_TYPE_HANDLED;
fb52607a 399
9005f3eb
FW
400 prev_pid = *last_pid;
401 *last_pid = pid;
d51090b3 402
9005f3eb
FW
403 if (prev_pid == -1)
404 return TRACE_TYPE_HANDLED;
d51090b3
IM
405/*
406 * Context-switch trace line:
407
408 ------------------------------------------
409 | 1) migration/0--1 => sshd-1755
410 ------------------------------------------
411
412 */
413 ret = trace_seq_printf(s,
1fd8f2a3 414 " ------------------------------------------\n");
11e84acc 415 if (!ret)
810dc732 416 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
417
418 ret = print_graph_cpu(s, cpu);
419 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 420 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
421
422 ret = print_graph_proc(s, prev_pid);
423 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 424 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
425
426 ret = trace_seq_printf(s, " => ");
427 if (!ret)
810dc732 428 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
429
430 ret = print_graph_proc(s, pid);
431 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 432 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
433
434 ret = trace_seq_printf(s,
435 "\n ------------------------------------------\n\n");
436 if (!ret)
810dc732 437 return TRACE_TYPE_PARTIAL_LINE;
11e84acc 438
810dc732 439 return TRACE_TYPE_HANDLED;
287b6e68
FW
440}
441
b91facc3
FW
442static struct ftrace_graph_ret_entry *
443get_return_for_leaf(struct trace_iterator *iter,
83a8df61
FW
444 struct ftrace_graph_ent_entry *curr)
445{
be1eca39
JO
446 struct fgraph_data *data = iter->private;
447 struct ring_buffer_iter *ring_iter = NULL;
83a8df61
FW
448 struct ring_buffer_event *event;
449 struct ftrace_graph_ret_entry *next;
450
be1eca39
JO
451 /*
452 * If the previous output failed to write to the seq buffer,
453 * then we just reuse the data from before.
454 */
455 if (data && data->failed) {
456 curr = &data->ent;
457 next = &data->ret;
458 } else {
83a8df61 459
be1eca39
JO
460 ring_iter = iter->buffer_iter[iter->cpu];
461
462 /* First peek to compare current entry and the next one */
463 if (ring_iter)
464 event = ring_buffer_iter_peek(ring_iter, NULL);
465 else {
466 /*
467 * We need to consume the current entry to see
468 * the next one.
469 */
470 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
471 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
472 NULL);
473 }
83a8df61 474
be1eca39
JO
475 if (!event)
476 return NULL;
477
478 next = ring_buffer_event_data(event);
83a8df61 479
be1eca39
JO
480 if (data) {
481 /*
482 * Save current and next entries for later reference
483 * if the output fails.
484 */
485 data->ent = *curr;
486 data->ret = *next;
487 }
488 }
83a8df61
FW
489
490 if (next->ent.type != TRACE_GRAPH_RET)
b91facc3 491 return NULL;
83a8df61
FW
492
493 if (curr->ent.pid != next->ent.pid ||
494 curr->graph_ent.func != next->ret.func)
b91facc3 495 return NULL;
83a8df61 496
b91facc3
FW
497 /* this is a leaf, now advance the iterator */
498 if (ring_iter)
499 ring_buffer_read(ring_iter, NULL);
500
501 return next;
83a8df61
FW
502}
503
9005f3eb
FW
504/* Signal a overhead of time execution to the output */
505static int
506print_graph_overhead(unsigned long long duration, struct trace_seq *s)
507{
508 /* If duration disappear, we don't need anything */
509 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
510 return 1;
511
512 /* Non nested entry or return */
513 if (duration == -1)
514 return trace_seq_printf(s, " ");
515
516 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
517 /* Duration exceeded 100 msecs */
518 if (duration > 100000ULL)
519 return trace_seq_printf(s, "! ");
520
521 /* Duration exceeded 10 msecs */
522 if (duration > 10000ULL)
523 return trace_seq_printf(s, "+ ");
524 }
525
526 return trace_seq_printf(s, " ");
527}
528
d1f9cbd7
FW
529static int print_graph_abs_time(u64 t, struct trace_seq *s)
530{
531 unsigned long usecs_rem;
532
533 usecs_rem = do_div(t, NSEC_PER_SEC);
534 usecs_rem /= 1000;
535
536 return trace_seq_printf(s, "%5lu.%06lu | ",
537 (unsigned long)t, usecs_rem);
538}
539
f8b755ac 540static enum print_line_t
d1f9cbd7 541print_graph_irq(struct trace_iterator *iter, unsigned long addr,
9005f3eb 542 enum trace_type type, int cpu, pid_t pid)
f8b755ac
FW
543{
544 int ret;
d1f9cbd7 545 struct trace_seq *s = &iter->seq;
f8b755ac
FW
546
547 if (addr < (unsigned long)__irqentry_text_start ||
548 addr >= (unsigned long)__irqentry_text_end)
549 return TRACE_TYPE_UNHANDLED;
550
d1f9cbd7
FW
551 /* Absolute time */
552 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
553 ret = print_graph_abs_time(iter->ts, s);
554 if (!ret)
555 return TRACE_TYPE_PARTIAL_LINE;
556 }
557
9005f3eb
FW
558 /* Cpu */
559 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
560 ret = print_graph_cpu(s, cpu);
561 if (ret == TRACE_TYPE_PARTIAL_LINE)
562 return TRACE_TYPE_PARTIAL_LINE;
563 }
49ff5903 564
9005f3eb
FW
565 /* Proc */
566 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
567 ret = print_graph_proc(s, pid);
568 if (ret == TRACE_TYPE_PARTIAL_LINE)
569 return TRACE_TYPE_PARTIAL_LINE;
570 ret = trace_seq_printf(s, " | ");
571 if (!ret)
572 return TRACE_TYPE_PARTIAL_LINE;
573 }
f8b755ac 574
9005f3eb
FW
575 /* No overhead */
576 ret = print_graph_overhead(-1, s);
577 if (!ret)
578 return TRACE_TYPE_PARTIAL_LINE;
f8b755ac 579
9005f3eb
FW
580 if (type == TRACE_GRAPH_ENT)
581 ret = trace_seq_printf(s, "==========>");
582 else
583 ret = trace_seq_printf(s, "<==========");
584
585 if (!ret)
586 return TRACE_TYPE_PARTIAL_LINE;
587
588 /* Don't close the duration column if haven't one */
589 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
590 trace_seq_printf(s, " |");
591 ret = trace_seq_printf(s, "\n");
f8b755ac 592
f8b755ac
FW
593 if (!ret)
594 return TRACE_TYPE_PARTIAL_LINE;
595 return TRACE_TYPE_HANDLED;
596}
83a8df61 597
0706f1c4
SR
598enum print_line_t
599trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
83a8df61
FW
600{
601 unsigned long nsecs_rem = do_div(duration, 1000);
166d3c79
FW
602 /* log10(ULONG_MAX) + '\0' */
603 char msecs_str[21];
604 char nsecs_str[5];
605 int ret, len;
606 int i;
607
608 sprintf(msecs_str, "%lu", (unsigned long) duration);
609
610 /* Print msecs */
9005f3eb 611 ret = trace_seq_printf(s, "%s", msecs_str);
166d3c79
FW
612 if (!ret)
613 return TRACE_TYPE_PARTIAL_LINE;
614
615 len = strlen(msecs_str);
616
617 /* Print nsecs (we don't want to exceed 7 numbers) */
618 if (len < 7) {
619 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
620 ret = trace_seq_printf(s, ".%s", nsecs_str);
621 if (!ret)
622 return TRACE_TYPE_PARTIAL_LINE;
623 len += strlen(nsecs_str);
624 }
625
626 ret = trace_seq_printf(s, " us ");
627 if (!ret)
628 return TRACE_TYPE_PARTIAL_LINE;
629
630 /* Print remaining spaces to fit the row's width */
631 for (i = len; i < 7; i++) {
632 ret = trace_seq_printf(s, " ");
633 if (!ret)
634 return TRACE_TYPE_PARTIAL_LINE;
635 }
0706f1c4
SR
636 return TRACE_TYPE_HANDLED;
637}
638
639static enum print_line_t
640print_graph_duration(unsigned long long duration, struct trace_seq *s)
641{
642 int ret;
643
644 ret = trace_print_graph_duration(duration, s);
645 if (ret != TRACE_TYPE_HANDLED)
646 return ret;
166d3c79
FW
647
648 ret = trace_seq_printf(s, "| ");
649 if (!ret)
650 return TRACE_TYPE_PARTIAL_LINE;
166d3c79 651
0706f1c4 652 return TRACE_TYPE_HANDLED;
83a8df61
FW
653}
654
83a8df61 655/* Case of a leaf function on its call entry */
287b6e68 656static enum print_line_t
83a8df61 657print_graph_entry_leaf(struct trace_iterator *iter,
b91facc3
FW
658 struct ftrace_graph_ent_entry *entry,
659 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
fb52607a 660{
2fbcdb35 661 struct fgraph_data *data = iter->private;
83a8df61 662 struct ftrace_graph_ret *graph_ret;
83a8df61
FW
663 struct ftrace_graph_ent *call;
664 unsigned long long duration;
fb52607a 665 int ret;
1a056155 666 int i;
fb52607a 667
83a8df61
FW
668 graph_ret = &ret_entry->ret;
669 call = &entry->graph_ent;
670 duration = graph_ret->rettime - graph_ret->calltime;
671
2fbcdb35
SR
672 if (data) {
673 int cpu = iter->cpu;
be1eca39 674 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
2fbcdb35
SR
675
676 /*
677 * Comments display at + 1 to depth. Since
678 * this is a leaf function, keep the comments
679 * equal to this depth.
680 */
681 *depth = call->depth - 1;
682 }
683
83a8df61 684 /* Overhead */
9005f3eb
FW
685 ret = print_graph_overhead(duration, s);
686 if (!ret)
687 return TRACE_TYPE_PARTIAL_LINE;
1a056155
FW
688
689 /* Duration */
9005f3eb
FW
690 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
691 ret = print_graph_duration(duration, s);
692 if (ret == TRACE_TYPE_PARTIAL_LINE)
693 return TRACE_TYPE_PARTIAL_LINE;
694 }
437f24fb 695
83a8df61
FW
696 /* Function */
697 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
698 ret = trace_seq_printf(s, " ");
699 if (!ret)
700 return TRACE_TYPE_PARTIAL_LINE;
701 }
702
b375a11a 703 ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
83a8df61
FW
704 if (!ret)
705 return TRACE_TYPE_PARTIAL_LINE;
706
707 return TRACE_TYPE_HANDLED;
708}
709
710static enum print_line_t
2fbcdb35
SR
711print_graph_entry_nested(struct trace_iterator *iter,
712 struct ftrace_graph_ent_entry *entry,
713 struct trace_seq *s, int cpu)
83a8df61 714{
83a8df61 715 struct ftrace_graph_ent *call = &entry->graph_ent;
2fbcdb35
SR
716 struct fgraph_data *data = iter->private;
717 int ret;
718 int i;
719
720 if (data) {
721 int cpu = iter->cpu;
be1eca39 722 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
2fbcdb35
SR
723
724 *depth = call->depth;
725 }
83a8df61
FW
726
727 /* No overhead */
9005f3eb
FW
728 ret = print_graph_overhead(-1, s);
729 if (!ret)
730 return TRACE_TYPE_PARTIAL_LINE;
1a056155 731
9005f3eb
FW
732 /* No time */
733 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
f8b755ac
FW
734 ret = trace_seq_printf(s, " | ");
735 if (!ret)
736 return TRACE_TYPE_PARTIAL_LINE;
f8b755ac
FW
737 }
738
83a8df61 739 /* Function */
287b6e68
FW
740 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
741 ret = trace_seq_printf(s, " ");
fb52607a
FW
742 if (!ret)
743 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
744 }
745
b375a11a 746 ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
83a8df61
FW
747 if (!ret)
748 return TRACE_TYPE_PARTIAL_LINE;
749
b91facc3
FW
750 /*
751 * we already consumed the current entry to check the next one
752 * and see if this is a leaf.
753 */
754 return TRACE_TYPE_NO_CONSUME;
287b6e68
FW
755}
756
83a8df61 757static enum print_line_t
ac5f6c96
SR
758print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
759 int type, unsigned long addr)
83a8df61 760{
2fbcdb35 761 struct fgraph_data *data = iter->private;
83a8df61 762 struct trace_entry *ent = iter->ent;
ac5f6c96
SR
763 int cpu = iter->cpu;
764 int ret;
83a8df61 765
1a056155 766 /* Pid */
2fbcdb35 767 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
9005f3eb
FW
768 return TRACE_TYPE_PARTIAL_LINE;
769
ac5f6c96
SR
770 if (type) {
771 /* Interrupt */
772 ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
773 if (ret == TRACE_TYPE_PARTIAL_LINE)
774 return TRACE_TYPE_PARTIAL_LINE;
775 }
83a8df61 776
9005f3eb
FW
777 /* Absolute time */
778 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
779 ret = print_graph_abs_time(iter->ts, s);
780 if (!ret)
781 return TRACE_TYPE_PARTIAL_LINE;
782 }
783
1a056155
FW
784 /* Cpu */
785 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
786 ret = print_graph_cpu(s, cpu);
11e84acc
FW
787 if (ret == TRACE_TYPE_PARTIAL_LINE)
788 return TRACE_TYPE_PARTIAL_LINE;
789 }
790
791 /* Proc */
792 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
00a8bf85 793 ret = print_graph_proc(s, ent->pid);
11e84acc
FW
794 if (ret == TRACE_TYPE_PARTIAL_LINE)
795 return TRACE_TYPE_PARTIAL_LINE;
796
797 ret = trace_seq_printf(s, " | ");
1a056155
FW
798 if (!ret)
799 return TRACE_TYPE_PARTIAL_LINE;
800 }
83a8df61 801
49ff5903
SR
802 /* Latency format */
803 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
804 ret = print_graph_lat_fmt(s, ent);
805 if (ret == TRACE_TYPE_PARTIAL_LINE)
806 return TRACE_TYPE_PARTIAL_LINE;
807 }
808
ac5f6c96
SR
809 return 0;
810}
811
812static enum print_line_t
813print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
814 struct trace_iterator *iter)
815{
be1eca39 816 struct fgraph_data *data = iter->private;
ac5f6c96
SR
817 struct ftrace_graph_ent *call = &field->graph_ent;
818 struct ftrace_graph_ret_entry *leaf_ret;
be1eca39
JO
819 static enum print_line_t ret;
820 int cpu = iter->cpu;
ac5f6c96
SR
821
822 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
823 return TRACE_TYPE_PARTIAL_LINE;
824
b91facc3
FW
825 leaf_ret = get_return_for_leaf(iter, field);
826 if (leaf_ret)
be1eca39 827 ret = print_graph_entry_leaf(iter, field, leaf_ret, s);
83a8df61 828 else
be1eca39 829 ret = print_graph_entry_nested(iter, field, s, cpu);
83a8df61 830
be1eca39
JO
831 if (data) {
832 /*
833 * If we failed to write our output, then we need to make
834 * note of it. Because we already consumed our entry.
835 */
836 if (s->full) {
837 data->failed = 1;
838 data->cpu = cpu;
839 } else
840 data->failed = 0;
841 }
842
843 return ret;
83a8df61
FW
844}
845
287b6e68
FW
846static enum print_line_t
847print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
9005f3eb 848 struct trace_entry *ent, struct trace_iterator *iter)
287b6e68 849{
83a8df61 850 unsigned long long duration = trace->rettime - trace->calltime;
2fbcdb35
SR
851 struct fgraph_data *data = iter->private;
852 pid_t pid = ent->pid;
853 int cpu = iter->cpu;
854 int ret;
855 int i;
856
857 if (data) {
be1eca39 858 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
2fbcdb35
SR
859
860 /*
861 * Comments display at + 1 to depth. This is the
862 * return from a function, we now want the comments
863 * to display at the same level of the bracket.
864 */
865 *depth = trace->depth - 1;
866 }
287b6e68 867
ac5f6c96 868 if (print_graph_prologue(iter, s, 0, 0))
437f24fb
SR
869 return TRACE_TYPE_PARTIAL_LINE;
870
83a8df61 871 /* Overhead */
9005f3eb
FW
872 ret = print_graph_overhead(duration, s);
873 if (!ret)
874 return TRACE_TYPE_PARTIAL_LINE;
1a056155
FW
875
876 /* Duration */
9005f3eb
FW
877 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
878 ret = print_graph_duration(duration, s);
879 if (ret == TRACE_TYPE_PARTIAL_LINE)
880 return TRACE_TYPE_PARTIAL_LINE;
881 }
83a8df61
FW
882
883 /* Closing brace */
287b6e68
FW
884 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
885 ret = trace_seq_printf(s, " ");
fb52607a
FW
886 if (!ret)
887 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
888 }
889
1a056155 890 ret = trace_seq_printf(s, "}\n");
287b6e68
FW
891 if (!ret)
892 return TRACE_TYPE_PARTIAL_LINE;
fb52607a 893
83a8df61 894 /* Overrun */
287b6e68
FW
895 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
896 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
897 trace->overrun);
fb52607a
FW
898 if (!ret)
899 return TRACE_TYPE_PARTIAL_LINE;
287b6e68 900 }
f8b755ac 901
d1f9cbd7 902 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
f8b755ac
FW
903 if (ret == TRACE_TYPE_PARTIAL_LINE)
904 return TRACE_TYPE_PARTIAL_LINE;
905
287b6e68
FW
906 return TRACE_TYPE_HANDLED;
907}
908
1fd8f2a3 909static enum print_line_t
5087f8d2
SR
910print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
911 struct trace_iterator *iter)
1fd8f2a3 912{
5087f8d2 913 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2fbcdb35 914 struct fgraph_data *data = iter->private;
5087f8d2 915 struct trace_event *event;
2fbcdb35 916 int depth = 0;
1fd8f2a3 917 int ret;
2fbcdb35
SR
918 int i;
919
920 if (data)
be1eca39 921 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
9005f3eb 922
ac5f6c96 923 if (print_graph_prologue(iter, s, 0, 0))
d1f9cbd7
FW
924 return TRACE_TYPE_PARTIAL_LINE;
925
1fd8f2a3 926 /* No overhead */
9005f3eb
FW
927 ret = print_graph_overhead(-1, s);
928 if (!ret)
929 return TRACE_TYPE_PARTIAL_LINE;
930
931 /* No time */
932 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
933 ret = trace_seq_printf(s, " | ");
1fd8f2a3
FW
934 if (!ret)
935 return TRACE_TYPE_PARTIAL_LINE;
936 }
937
1fd8f2a3 938 /* Indentation */
2fbcdb35
SR
939 if (depth > 0)
940 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1fd8f2a3
FW
941 ret = trace_seq_printf(s, " ");
942 if (!ret)
943 return TRACE_TYPE_PARTIAL_LINE;
944 }
945
946 /* The comment */
769b0441
FW
947 ret = trace_seq_printf(s, "/* ");
948 if (!ret)
949 return TRACE_TYPE_PARTIAL_LINE;
950
5087f8d2
SR
951 switch (iter->ent->type) {
952 case TRACE_BPRINT:
953 ret = trace_print_bprintk_msg_only(iter);
954 if (ret != TRACE_TYPE_HANDLED)
955 return ret;
956 break;
957 case TRACE_PRINT:
958 ret = trace_print_printk_msg_only(iter);
959 if (ret != TRACE_TYPE_HANDLED)
960 return ret;
961 break;
962 default:
963 event = ftrace_find_event(ent->type);
964 if (!event)
965 return TRACE_TYPE_UNHANDLED;
966
967 ret = event->trace(iter, sym_flags);
968 if (ret != TRACE_TYPE_HANDLED)
969 return ret;
970 }
1fd8f2a3 971
412d0bb5
FW
972 /* Strip ending newline */
973 if (s->buffer[s->len - 1] == '\n') {
974 s->buffer[s->len - 1] = '\0';
975 s->len--;
976 }
977
1fd8f2a3
FW
978 ret = trace_seq_printf(s, " */\n");
979 if (!ret)
980 return TRACE_TYPE_PARTIAL_LINE;
981
982 return TRACE_TYPE_HANDLED;
983}
984
985
287b6e68
FW
986enum print_line_t
987print_graph_function(struct trace_iterator *iter)
988{
be1eca39
JO
989 struct ftrace_graph_ent_entry *field;
990 struct fgraph_data *data = iter->private;
287b6e68 991 struct trace_entry *entry = iter->ent;
5087f8d2 992 struct trace_seq *s = &iter->seq;
be1eca39
JO
993 int cpu = iter->cpu;
994 int ret;
995
996 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
997 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
998 return TRACE_TYPE_HANDLED;
999 }
1000
1001 /*
1002 * If the last output failed, there's a possibility we need
1003 * to print out the missing entry which would never go out.
1004 */
1005 if (data && data->failed) {
1006 field = &data->ent;
1007 iter->cpu = data->cpu;
1008 ret = print_graph_entry(field, s, iter);
1009 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1010 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1011 ret = TRACE_TYPE_NO_CONSUME;
1012 }
1013 iter->cpu = cpu;
1014 return ret;
1015 }
fb52607a 1016
287b6e68
FW
1017 switch (entry->type) {
1018 case TRACE_GRAPH_ENT: {
38ceb592
LJ
1019 /*
1020 * print_graph_entry() may consume the current event,
1021 * thus @field may become invalid, so we need to save it.
1022 * sizeof(struct ftrace_graph_ent_entry) is very small,
1023 * it can be safely saved at the stack.
1024 */
be1eca39 1025 struct ftrace_graph_ent_entry saved;
287b6e68 1026 trace_assign_type(field, entry);
38ceb592
LJ
1027 saved = *field;
1028 return print_graph_entry(&saved, s, iter);
287b6e68
FW
1029 }
1030 case TRACE_GRAPH_RET: {
1031 struct ftrace_graph_ret_entry *field;
1032 trace_assign_type(field, entry);
9005f3eb 1033 return print_graph_return(&field->ret, s, entry, iter);
287b6e68
FW
1034 }
1035 default:
5087f8d2 1036 return print_graph_comment(s, entry, iter);
fb52607a 1037 }
5087f8d2
SR
1038
1039 return TRACE_TYPE_HANDLED;
fb52607a
FW
1040}
1041
49ff5903
SR
1042static void print_lat_header(struct seq_file *s)
1043{
1044 static const char spaces[] = " " /* 16 spaces */
1045 " " /* 4 spaces */
1046 " "; /* 17 spaces */
1047 int size = 0;
1048
1049 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1050 size += 16;
1051 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1052 size += 4;
1053 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1054 size += 17;
1055
1056 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1057 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1058 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1059 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
637e7e86
SR
1060 seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces);
1061 seq_printf(s, "#%.*s|||| / \n", size, spaces);
49ff5903
SR
1062}
1063
decbec38
FW
1064static void print_graph_headers(struct seq_file *s)
1065{
49ff5903
SR
1066 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1067
1068 if (lat)
1069 print_lat_header(s);
1070
decbec38 1071 /* 1st line */
49ff5903 1072 seq_printf(s, "#");
9005f3eb
FW
1073 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1074 seq_printf(s, " TIME ");
decbec38 1075 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
49ff5903 1076 seq_printf(s, " CPU");
decbec38 1077 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1078 seq_printf(s, " TASK/PID ");
1079 if (lat)
637e7e86 1080 seq_printf(s, "|||||");
9005f3eb
FW
1081 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1082 seq_printf(s, " DURATION ");
1083 seq_printf(s, " FUNCTION CALLS\n");
decbec38
FW
1084
1085 /* 2nd line */
49ff5903 1086 seq_printf(s, "#");
9005f3eb
FW
1087 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1088 seq_printf(s, " | ");
decbec38 1089 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
49ff5903 1090 seq_printf(s, " | ");
decbec38 1091 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1092 seq_printf(s, " | | ");
1093 if (lat)
637e7e86 1094 seq_printf(s, "|||||");
9005f3eb
FW
1095 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1096 seq_printf(s, " | | ");
1097 seq_printf(s, " | | | |\n");
decbec38 1098}
9005f3eb
FW
1099
1100static void graph_trace_open(struct trace_iterator *iter)
1101{
2fbcdb35 1102 /* pid and depth on the last trace processed */
be1eca39 1103 struct fgraph_data *data;
9005f3eb
FW
1104 int cpu;
1105
be1eca39
JO
1106 iter->private = NULL;
1107
1108 data = kzalloc(sizeof(*data), GFP_KERNEL);
2fbcdb35 1109 if (!data)
be1eca39
JO
1110 goto out_err;
1111
1112 data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1113 if (!data->cpu_data)
1114 goto out_err_free;
1115
1116 for_each_possible_cpu(cpu) {
1117 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1118 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1119 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1120 *pid = -1;
1121 *depth = 0;
1122 *ignore = 0;
1123 }
9005f3eb 1124
2fbcdb35 1125 iter->private = data;
be1eca39
JO
1126
1127 return;
1128
1129 out_err_free:
1130 kfree(data);
1131 out_err:
1132 pr_warning("function graph tracer: not enough memory\n");
9005f3eb
FW
1133}
1134
1135static void graph_trace_close(struct trace_iterator *iter)
1136{
be1eca39
JO
1137 struct fgraph_data *data = iter->private;
1138
1139 if (data) {
1140 free_percpu(data->cpu_data);
1141 kfree(data);
1142 }
9005f3eb
FW
1143}
1144
fb52607a 1145static struct tracer graph_trace __read_mostly = {
ef18012b 1146 .name = "function_graph",
9005f3eb 1147 .open = graph_trace_open,
be1eca39 1148 .pipe_open = graph_trace_open,
9005f3eb 1149 .close = graph_trace_close,
be1eca39 1150 .pipe_close = graph_trace_close,
6eaaa5d5 1151 .wait_pipe = poll_wait_pipe,
ef18012b
SR
1152 .init = graph_trace_init,
1153 .reset = graph_trace_reset,
decbec38
FW
1154 .print_line = print_graph_function,
1155 .print_header = print_graph_headers,
fb52607a 1156 .flags = &tracer_flags,
7447dce9
FW
1157#ifdef CONFIG_FTRACE_SELFTEST
1158 .selftest = trace_selftest_startup_function_graph,
1159#endif
fb52607a
FW
1160};
1161
1162static __init int init_graph_trace(void)
1163{
0c9e6f63
LJ
1164 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1165
fb52607a
FW
1166 return register_tracer(&graph_trace);
1167}
1168
1169device_initcall(init_graph_trace);