ext3: Flush disk caches on fsync when needed
[deliverable/linux.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"
127 " from func %pF return to %lx\n",
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
287b6e68 367/* If the pid changed since the last trace, output this event */
11e84acc 368static enum print_line_t
2fbcdb35 369verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
287b6e68 370{
d51090b3 371 pid_t prev_pid;
9005f3eb 372 pid_t *last_pid;
d51090b3 373 int ret;
660c7f9b 374
2fbcdb35 375 if (!data)
9005f3eb
FW
376 return TRACE_TYPE_HANDLED;
377
2fbcdb35 378 last_pid = &(per_cpu_ptr(data, cpu)->last_pid);
9005f3eb
FW
379
380 if (*last_pid == pid)
11e84acc 381 return TRACE_TYPE_HANDLED;
fb52607a 382
9005f3eb
FW
383 prev_pid = *last_pid;
384 *last_pid = pid;
d51090b3 385
9005f3eb
FW
386 if (prev_pid == -1)
387 return TRACE_TYPE_HANDLED;
d51090b3
IM
388/*
389 * Context-switch trace line:
390
391 ------------------------------------------
392 | 1) migration/0--1 => sshd-1755
393 ------------------------------------------
394
395 */
396 ret = trace_seq_printf(s,
1fd8f2a3 397 " ------------------------------------------\n");
11e84acc 398 if (!ret)
810dc732 399 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
400
401 ret = print_graph_cpu(s, cpu);
402 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 403 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
404
405 ret = print_graph_proc(s, prev_pid);
406 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 407 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
408
409 ret = trace_seq_printf(s, " => ");
410 if (!ret)
810dc732 411 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
412
413 ret = print_graph_proc(s, pid);
414 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 415 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
416
417 ret = trace_seq_printf(s,
418 "\n ------------------------------------------\n\n");
419 if (!ret)
810dc732 420 return TRACE_TYPE_PARTIAL_LINE;
11e84acc 421
810dc732 422 return TRACE_TYPE_HANDLED;
287b6e68
FW
423}
424
b91facc3
FW
425static struct ftrace_graph_ret_entry *
426get_return_for_leaf(struct trace_iterator *iter,
83a8df61
FW
427 struct ftrace_graph_ent_entry *curr)
428{
429 struct ring_buffer_iter *ring_iter;
430 struct ring_buffer_event *event;
431 struct ftrace_graph_ret_entry *next;
432
433 ring_iter = iter->buffer_iter[iter->cpu];
434
b91facc3
FW
435 /* First peek to compare current entry and the next one */
436 if (ring_iter)
437 event = ring_buffer_iter_peek(ring_iter, NULL);
438 else {
439 /* We need to consume the current entry to see the next one */
440 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
441 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
442 NULL);
443 }
83a8df61
FW
444
445 if (!event)
b91facc3 446 return NULL;
83a8df61
FW
447
448 next = ring_buffer_event_data(event);
449
450 if (next->ent.type != TRACE_GRAPH_RET)
b91facc3 451 return NULL;
83a8df61
FW
452
453 if (curr->ent.pid != next->ent.pid ||
454 curr->graph_ent.func != next->ret.func)
b91facc3 455 return NULL;
83a8df61 456
b91facc3
FW
457 /* this is a leaf, now advance the iterator */
458 if (ring_iter)
459 ring_buffer_read(ring_iter, NULL);
460
461 return next;
83a8df61
FW
462}
463
9005f3eb
FW
464/* Signal a overhead of time execution to the output */
465static int
466print_graph_overhead(unsigned long long duration, struct trace_seq *s)
467{
468 /* If duration disappear, we don't need anything */
469 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
470 return 1;
471
472 /* Non nested entry or return */
473 if (duration == -1)
474 return trace_seq_printf(s, " ");
475
476 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
477 /* Duration exceeded 100 msecs */
478 if (duration > 100000ULL)
479 return trace_seq_printf(s, "! ");
480
481 /* Duration exceeded 10 msecs */
482 if (duration > 10000ULL)
483 return trace_seq_printf(s, "+ ");
484 }
485
486 return trace_seq_printf(s, " ");
487}
488
d1f9cbd7
FW
489static int print_graph_abs_time(u64 t, struct trace_seq *s)
490{
491 unsigned long usecs_rem;
492
493 usecs_rem = do_div(t, NSEC_PER_SEC);
494 usecs_rem /= 1000;
495
496 return trace_seq_printf(s, "%5lu.%06lu | ",
497 (unsigned long)t, usecs_rem);
498}
499
f8b755ac 500static enum print_line_t
d1f9cbd7 501print_graph_irq(struct trace_iterator *iter, unsigned long addr,
9005f3eb 502 enum trace_type type, int cpu, pid_t pid)
f8b755ac
FW
503{
504 int ret;
d1f9cbd7 505 struct trace_seq *s = &iter->seq;
f8b755ac
FW
506
507 if (addr < (unsigned long)__irqentry_text_start ||
508 addr >= (unsigned long)__irqentry_text_end)
509 return TRACE_TYPE_UNHANDLED;
510
d1f9cbd7
FW
511 /* Absolute time */
512 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
513 ret = print_graph_abs_time(iter->ts, s);
514 if (!ret)
515 return TRACE_TYPE_PARTIAL_LINE;
516 }
517
9005f3eb
FW
518 /* Cpu */
519 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
520 ret = print_graph_cpu(s, cpu);
521 if (ret == TRACE_TYPE_PARTIAL_LINE)
522 return TRACE_TYPE_PARTIAL_LINE;
523 }
524 /* Proc */
525 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
526 ret = print_graph_proc(s, pid);
527 if (ret == TRACE_TYPE_PARTIAL_LINE)
528 return TRACE_TYPE_PARTIAL_LINE;
529 ret = trace_seq_printf(s, " | ");
530 if (!ret)
531 return TRACE_TYPE_PARTIAL_LINE;
532 }
f8b755ac 533
9005f3eb
FW
534 /* No overhead */
535 ret = print_graph_overhead(-1, s);
536 if (!ret)
537 return TRACE_TYPE_PARTIAL_LINE;
f8b755ac 538
9005f3eb
FW
539 if (type == TRACE_GRAPH_ENT)
540 ret = trace_seq_printf(s, "==========>");
541 else
542 ret = trace_seq_printf(s, "<==========");
543
544 if (!ret)
545 return TRACE_TYPE_PARTIAL_LINE;
546
547 /* Don't close the duration column if haven't one */
548 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
549 trace_seq_printf(s, " |");
550 ret = trace_seq_printf(s, "\n");
f8b755ac 551
f8b755ac
FW
552 if (!ret)
553 return TRACE_TYPE_PARTIAL_LINE;
554 return TRACE_TYPE_HANDLED;
555}
83a8df61 556
0706f1c4
SR
557enum print_line_t
558trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
83a8df61
FW
559{
560 unsigned long nsecs_rem = do_div(duration, 1000);
166d3c79
FW
561 /* log10(ULONG_MAX) + '\0' */
562 char msecs_str[21];
563 char nsecs_str[5];
564 int ret, len;
565 int i;
566
567 sprintf(msecs_str, "%lu", (unsigned long) duration);
568
569 /* Print msecs */
9005f3eb 570 ret = trace_seq_printf(s, "%s", msecs_str);
166d3c79
FW
571 if (!ret)
572 return TRACE_TYPE_PARTIAL_LINE;
573
574 len = strlen(msecs_str);
575
576 /* Print nsecs (we don't want to exceed 7 numbers) */
577 if (len < 7) {
578 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
579 ret = trace_seq_printf(s, ".%s", nsecs_str);
580 if (!ret)
581 return TRACE_TYPE_PARTIAL_LINE;
582 len += strlen(nsecs_str);
583 }
584
585 ret = trace_seq_printf(s, " us ");
586 if (!ret)
587 return TRACE_TYPE_PARTIAL_LINE;
588
589 /* Print remaining spaces to fit the row's width */
590 for (i = len; i < 7; i++) {
591 ret = trace_seq_printf(s, " ");
592 if (!ret)
593 return TRACE_TYPE_PARTIAL_LINE;
594 }
0706f1c4
SR
595 return TRACE_TYPE_HANDLED;
596}
597
598static enum print_line_t
599print_graph_duration(unsigned long long duration, struct trace_seq *s)
600{
601 int ret;
602
603 ret = trace_print_graph_duration(duration, s);
604 if (ret != TRACE_TYPE_HANDLED)
605 return ret;
166d3c79
FW
606
607 ret = trace_seq_printf(s, "| ");
608 if (!ret)
609 return TRACE_TYPE_PARTIAL_LINE;
166d3c79 610
0706f1c4 611 return TRACE_TYPE_HANDLED;
83a8df61
FW
612}
613
83a8df61 614/* Case of a leaf function on its call entry */
287b6e68 615static enum print_line_t
83a8df61 616print_graph_entry_leaf(struct trace_iterator *iter,
b91facc3
FW
617 struct ftrace_graph_ent_entry *entry,
618 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
fb52607a 619{
2fbcdb35 620 struct fgraph_data *data = iter->private;
83a8df61 621 struct ftrace_graph_ret *graph_ret;
83a8df61
FW
622 struct ftrace_graph_ent *call;
623 unsigned long long duration;
fb52607a 624 int ret;
1a056155 625 int i;
fb52607a 626
83a8df61
FW
627 graph_ret = &ret_entry->ret;
628 call = &entry->graph_ent;
629 duration = graph_ret->rettime - graph_ret->calltime;
630
2fbcdb35
SR
631 if (data) {
632 int cpu = iter->cpu;
633 int *depth = &(per_cpu_ptr(data, cpu)->depth);
634
635 /*
636 * Comments display at + 1 to depth. Since
637 * this is a leaf function, keep the comments
638 * equal to this depth.
639 */
640 *depth = call->depth - 1;
641 }
642
83a8df61 643 /* Overhead */
9005f3eb
FW
644 ret = print_graph_overhead(duration, s);
645 if (!ret)
646 return TRACE_TYPE_PARTIAL_LINE;
1a056155
FW
647
648 /* Duration */
9005f3eb
FW
649 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
650 ret = print_graph_duration(duration, s);
651 if (ret == TRACE_TYPE_PARTIAL_LINE)
652 return TRACE_TYPE_PARTIAL_LINE;
653 }
437f24fb 654
83a8df61
FW
655 /* Function */
656 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
657 ret = trace_seq_printf(s, " ");
658 if (!ret)
659 return TRACE_TYPE_PARTIAL_LINE;
660 }
661
68baafcf 662 ret = trace_seq_printf(s, "%pf();\n", (void *)call->func);
83a8df61
FW
663 if (!ret)
664 return TRACE_TYPE_PARTIAL_LINE;
665
666 return TRACE_TYPE_HANDLED;
667}
668
669static enum print_line_t
2fbcdb35
SR
670print_graph_entry_nested(struct trace_iterator *iter,
671 struct ftrace_graph_ent_entry *entry,
672 struct trace_seq *s, int cpu)
83a8df61 673{
83a8df61 674 struct ftrace_graph_ent *call = &entry->graph_ent;
2fbcdb35
SR
675 struct fgraph_data *data = iter->private;
676 int ret;
677 int i;
678
679 if (data) {
680 int cpu = iter->cpu;
681 int *depth = &(per_cpu_ptr(data, cpu)->depth);
682
683 *depth = call->depth;
684 }
83a8df61
FW
685
686 /* No overhead */
9005f3eb
FW
687 ret = print_graph_overhead(-1, s);
688 if (!ret)
689 return TRACE_TYPE_PARTIAL_LINE;
1a056155 690
9005f3eb
FW
691 /* No time */
692 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
f8b755ac
FW
693 ret = trace_seq_printf(s, " | ");
694 if (!ret)
695 return TRACE_TYPE_PARTIAL_LINE;
f8b755ac
FW
696 }
697
83a8df61 698 /* Function */
287b6e68
FW
699 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
700 ret = trace_seq_printf(s, " ");
fb52607a
FW
701 if (!ret)
702 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
703 }
704
68baafcf 705 ret = trace_seq_printf(s, "%pf() {\n", (void *)call->func);
83a8df61
FW
706 if (!ret)
707 return TRACE_TYPE_PARTIAL_LINE;
708
b91facc3
FW
709 /*
710 * we already consumed the current entry to check the next one
711 * and see if this is a leaf.
712 */
713 return TRACE_TYPE_NO_CONSUME;
287b6e68
FW
714}
715
83a8df61 716static enum print_line_t
ac5f6c96
SR
717print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
718 int type, unsigned long addr)
83a8df61 719{
2fbcdb35 720 struct fgraph_data *data = iter->private;
83a8df61 721 struct trace_entry *ent = iter->ent;
ac5f6c96
SR
722 int cpu = iter->cpu;
723 int ret;
83a8df61 724
1a056155 725 /* Pid */
2fbcdb35 726 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
9005f3eb
FW
727 return TRACE_TYPE_PARTIAL_LINE;
728
ac5f6c96
SR
729 if (type) {
730 /* Interrupt */
731 ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
732 if (ret == TRACE_TYPE_PARTIAL_LINE)
733 return TRACE_TYPE_PARTIAL_LINE;
734 }
83a8df61 735
9005f3eb
FW
736 /* Absolute time */
737 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
738 ret = print_graph_abs_time(iter->ts, s);
739 if (!ret)
740 return TRACE_TYPE_PARTIAL_LINE;
741 }
742
1a056155
FW
743 /* Cpu */
744 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
745 ret = print_graph_cpu(s, cpu);
11e84acc
FW
746 if (ret == TRACE_TYPE_PARTIAL_LINE)
747 return TRACE_TYPE_PARTIAL_LINE;
748 }
749
750 /* Proc */
751 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
00a8bf85 752 ret = print_graph_proc(s, ent->pid);
11e84acc
FW
753 if (ret == TRACE_TYPE_PARTIAL_LINE)
754 return TRACE_TYPE_PARTIAL_LINE;
755
756 ret = trace_seq_printf(s, " | ");
1a056155
FW
757 if (!ret)
758 return TRACE_TYPE_PARTIAL_LINE;
759 }
83a8df61 760
ac5f6c96
SR
761 return 0;
762}
763
764static enum print_line_t
765print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
766 struct trace_iterator *iter)
767{
768 int cpu = iter->cpu;
769 struct ftrace_graph_ent *call = &field->graph_ent;
770 struct ftrace_graph_ret_entry *leaf_ret;
771
772 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
773 return TRACE_TYPE_PARTIAL_LINE;
774
b91facc3
FW
775 leaf_ret = get_return_for_leaf(iter, field);
776 if (leaf_ret)
777 return print_graph_entry_leaf(iter, field, leaf_ret, s);
83a8df61 778 else
2fbcdb35 779 return print_graph_entry_nested(iter, field, s, cpu);
83a8df61
FW
780
781}
782
287b6e68
FW
783static enum print_line_t
784print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
9005f3eb 785 struct trace_entry *ent, struct trace_iterator *iter)
287b6e68 786{
83a8df61 787 unsigned long long duration = trace->rettime - trace->calltime;
2fbcdb35
SR
788 struct fgraph_data *data = iter->private;
789 pid_t pid = ent->pid;
790 int cpu = iter->cpu;
791 int ret;
792 int i;
793
794 if (data) {
795 int cpu = iter->cpu;
796 int *depth = &(per_cpu_ptr(data, cpu)->depth);
797
798 /*
799 * Comments display at + 1 to depth. This is the
800 * return from a function, we now want the comments
801 * to display at the same level of the bracket.
802 */
803 *depth = trace->depth - 1;
804 }
287b6e68 805
ac5f6c96 806 if (print_graph_prologue(iter, s, 0, 0))
437f24fb
SR
807 return TRACE_TYPE_PARTIAL_LINE;
808
83a8df61 809 /* Overhead */
9005f3eb
FW
810 ret = print_graph_overhead(duration, s);
811 if (!ret)
812 return TRACE_TYPE_PARTIAL_LINE;
1a056155
FW
813
814 /* Duration */
9005f3eb
FW
815 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
816 ret = print_graph_duration(duration, s);
817 if (ret == TRACE_TYPE_PARTIAL_LINE)
818 return TRACE_TYPE_PARTIAL_LINE;
819 }
83a8df61
FW
820
821 /* Closing brace */
287b6e68
FW
822 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
823 ret = trace_seq_printf(s, " ");
fb52607a
FW
824 if (!ret)
825 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
826 }
827
1a056155 828 ret = trace_seq_printf(s, "}\n");
287b6e68
FW
829 if (!ret)
830 return TRACE_TYPE_PARTIAL_LINE;
fb52607a 831
83a8df61 832 /* Overrun */
287b6e68
FW
833 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
834 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
835 trace->overrun);
fb52607a
FW
836 if (!ret)
837 return TRACE_TYPE_PARTIAL_LINE;
287b6e68 838 }
f8b755ac 839
d1f9cbd7 840 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
f8b755ac
FW
841 if (ret == TRACE_TYPE_PARTIAL_LINE)
842 return TRACE_TYPE_PARTIAL_LINE;
843
287b6e68
FW
844 return TRACE_TYPE_HANDLED;
845}
846
1fd8f2a3 847static enum print_line_t
5087f8d2
SR
848print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
849 struct trace_iterator *iter)
1fd8f2a3 850{
5087f8d2 851 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2fbcdb35 852 struct fgraph_data *data = iter->private;
5087f8d2 853 struct trace_event *event;
2fbcdb35 854 int depth = 0;
1fd8f2a3 855 int ret;
2fbcdb35
SR
856 int i;
857
858 if (data)
859 depth = per_cpu_ptr(data, iter->cpu)->depth;
9005f3eb 860
ac5f6c96 861 if (print_graph_prologue(iter, s, 0, 0))
d1f9cbd7
FW
862 return TRACE_TYPE_PARTIAL_LINE;
863
1fd8f2a3 864 /* No overhead */
9005f3eb
FW
865 ret = print_graph_overhead(-1, s);
866 if (!ret)
867 return TRACE_TYPE_PARTIAL_LINE;
868
869 /* No time */
870 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
871 ret = trace_seq_printf(s, " | ");
1fd8f2a3
FW
872 if (!ret)
873 return TRACE_TYPE_PARTIAL_LINE;
874 }
875
1fd8f2a3 876 /* Indentation */
2fbcdb35
SR
877 if (depth > 0)
878 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1fd8f2a3
FW
879 ret = trace_seq_printf(s, " ");
880 if (!ret)
881 return TRACE_TYPE_PARTIAL_LINE;
882 }
883
884 /* The comment */
769b0441
FW
885 ret = trace_seq_printf(s, "/* ");
886 if (!ret)
887 return TRACE_TYPE_PARTIAL_LINE;
888
5087f8d2
SR
889 switch (iter->ent->type) {
890 case TRACE_BPRINT:
891 ret = trace_print_bprintk_msg_only(iter);
892 if (ret != TRACE_TYPE_HANDLED)
893 return ret;
894 break;
895 case TRACE_PRINT:
896 ret = trace_print_printk_msg_only(iter);
897 if (ret != TRACE_TYPE_HANDLED)
898 return ret;
899 break;
900 default:
901 event = ftrace_find_event(ent->type);
902 if (!event)
903 return TRACE_TYPE_UNHANDLED;
904
905 ret = event->trace(iter, sym_flags);
906 if (ret != TRACE_TYPE_HANDLED)
907 return ret;
908 }
1fd8f2a3 909
412d0bb5
FW
910 /* Strip ending newline */
911 if (s->buffer[s->len - 1] == '\n') {
912 s->buffer[s->len - 1] = '\0';
913 s->len--;
914 }
915
1fd8f2a3
FW
916 ret = trace_seq_printf(s, " */\n");
917 if (!ret)
918 return TRACE_TYPE_PARTIAL_LINE;
919
920 return TRACE_TYPE_HANDLED;
921}
922
923
287b6e68
FW
924enum print_line_t
925print_graph_function(struct trace_iterator *iter)
926{
287b6e68 927 struct trace_entry *entry = iter->ent;
5087f8d2 928 struct trace_seq *s = &iter->seq;
fb52607a 929
287b6e68
FW
930 switch (entry->type) {
931 case TRACE_GRAPH_ENT: {
38ceb592
LJ
932 /*
933 * print_graph_entry() may consume the current event,
934 * thus @field may become invalid, so we need to save it.
935 * sizeof(struct ftrace_graph_ent_entry) is very small,
936 * it can be safely saved at the stack.
937 */
938 struct ftrace_graph_ent_entry *field, saved;
287b6e68 939 trace_assign_type(field, entry);
38ceb592
LJ
940 saved = *field;
941 return print_graph_entry(&saved, s, iter);
287b6e68
FW
942 }
943 case TRACE_GRAPH_RET: {
944 struct ftrace_graph_ret_entry *field;
945 trace_assign_type(field, entry);
9005f3eb 946 return print_graph_return(&field->ret, s, entry, iter);
287b6e68
FW
947 }
948 default:
5087f8d2 949 return print_graph_comment(s, entry, iter);
fb52607a 950 }
5087f8d2
SR
951
952 return TRACE_TYPE_HANDLED;
fb52607a
FW
953}
954
decbec38
FW
955static void print_graph_headers(struct seq_file *s)
956{
957 /* 1st line */
958 seq_printf(s, "# ");
9005f3eb
FW
959 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
960 seq_printf(s, " TIME ");
decbec38 961 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
9005f3eb 962 seq_printf(s, "CPU");
decbec38 963 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
9005f3eb
FW
964 seq_printf(s, " TASK/PID ");
965 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
966 seq_printf(s, " DURATION ");
967 seq_printf(s, " FUNCTION CALLS\n");
decbec38
FW
968
969 /* 2nd line */
970 seq_printf(s, "# ");
9005f3eb
FW
971 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
972 seq_printf(s, " | ");
decbec38 973 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
9005f3eb 974 seq_printf(s, "| ");
decbec38 975 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
9005f3eb
FW
976 seq_printf(s, " | | ");
977 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
978 seq_printf(s, " | | ");
979 seq_printf(s, " | | | |\n");
decbec38 980}
9005f3eb
FW
981
982static void graph_trace_open(struct trace_iterator *iter)
983{
2fbcdb35
SR
984 /* pid and depth on the last trace processed */
985 struct fgraph_data *data = alloc_percpu(struct fgraph_data);
9005f3eb
FW
986 int cpu;
987
2fbcdb35 988 if (!data)
9005f3eb
FW
989 pr_warning("function graph tracer: not enough memory\n");
990 else
991 for_each_possible_cpu(cpu) {
2fbcdb35
SR
992 pid_t *pid = &(per_cpu_ptr(data, cpu)->last_pid);
993 int *depth = &(per_cpu_ptr(data, cpu)->depth);
9005f3eb 994 *pid = -1;
2fbcdb35 995 *depth = 0;
9005f3eb
FW
996 }
997
2fbcdb35 998 iter->private = data;
9005f3eb
FW
999}
1000
1001static void graph_trace_close(struct trace_iterator *iter)
1002{
8293dd6f 1003 free_percpu(iter->private);
9005f3eb
FW
1004}
1005
fb52607a 1006static struct tracer graph_trace __read_mostly = {
ef18012b 1007 .name = "function_graph",
9005f3eb
FW
1008 .open = graph_trace_open,
1009 .close = graph_trace_close,
6eaaa5d5 1010 .wait_pipe = poll_wait_pipe,
ef18012b
SR
1011 .init = graph_trace_init,
1012 .reset = graph_trace_reset,
decbec38
FW
1013 .print_line = print_graph_function,
1014 .print_header = print_graph_headers,
fb52607a 1015 .flags = &tracer_flags,
7447dce9
FW
1016#ifdef CONFIG_FTRACE_SELFTEST
1017 .selftest = trace_selftest_startup_function_graph,
1018#endif
fb52607a
FW
1019};
1020
1021static __init int init_graph_trace(void)
1022{
0c9e6f63
LJ
1023 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1024
fb52607a
FW
1025 return register_tracer(&graph_trace);
1026}
1027
1028device_initcall(init_graph_trace);
This page took 0.101588 seconds and 5 git commands to generate.