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