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