]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/trace/trace_functions_graph.c
tracing/trivial: Remove cast from void*
[mirror_ubuntu-bionic-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) {
14cae9bd
BP
668 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
669
670 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
166d3c79
FW
671 ret = trace_seq_printf(s, ".%s", nsecs_str);
672 if (!ret)
673 return TRACE_TYPE_PARTIAL_LINE;
674 len += strlen(nsecs_str);
675 }
676
677 ret = trace_seq_printf(s, " us ");
678 if (!ret)
679 return TRACE_TYPE_PARTIAL_LINE;
680
681 /* Print remaining spaces to fit the row's width */
682 for (i = len; i < 7; i++) {
683 ret = trace_seq_printf(s, " ");
684 if (!ret)
685 return TRACE_TYPE_PARTIAL_LINE;
686 }
0706f1c4
SR
687 return TRACE_TYPE_HANDLED;
688}
689
690static enum print_line_t
691print_graph_duration(unsigned long long duration, struct trace_seq *s)
692{
693 int ret;
694
695 ret = trace_print_graph_duration(duration, s);
696 if (ret != TRACE_TYPE_HANDLED)
697 return ret;
166d3c79
FW
698
699 ret = trace_seq_printf(s, "| ");
700 if (!ret)
701 return TRACE_TYPE_PARTIAL_LINE;
166d3c79 702
0706f1c4 703 return TRACE_TYPE_HANDLED;
83a8df61
FW
704}
705
83a8df61 706/* Case of a leaf function on its call entry */
287b6e68 707static enum print_line_t
83a8df61 708print_graph_entry_leaf(struct trace_iterator *iter,
b91facc3 709 struct ftrace_graph_ent_entry *entry,
d7a8d9e9
JO
710 struct ftrace_graph_ret_entry *ret_entry,
711 struct trace_seq *s, u32 flags)
fb52607a 712{
2fbcdb35 713 struct fgraph_data *data = iter->private;
83a8df61 714 struct ftrace_graph_ret *graph_ret;
83a8df61
FW
715 struct ftrace_graph_ent *call;
716 unsigned long long duration;
fb52607a 717 int ret;
1a056155 718 int i;
fb52607a 719
83a8df61
FW
720 graph_ret = &ret_entry->ret;
721 call = &entry->graph_ent;
722 duration = graph_ret->rettime - graph_ret->calltime;
723
2fbcdb35 724 if (data) {
f1c7f517 725 struct fgraph_cpu_data *cpu_data;
2fbcdb35 726 int cpu = iter->cpu;
f1c7f517
SR
727
728 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
2fbcdb35
SR
729
730 /*
731 * Comments display at + 1 to depth. Since
732 * this is a leaf function, keep the comments
733 * equal to this depth.
734 */
f1c7f517
SR
735 cpu_data->depth = call->depth - 1;
736
737 /* No need to keep this function around for this depth */
738 if (call->depth < FTRACE_RETFUNC_DEPTH)
739 cpu_data->enter_funcs[call->depth] = 0;
2fbcdb35
SR
740 }
741
83a8df61 742 /* Overhead */
d7a8d9e9 743 ret = print_graph_overhead(duration, s, flags);
9005f3eb
FW
744 if (!ret)
745 return TRACE_TYPE_PARTIAL_LINE;
1a056155
FW
746
747 /* Duration */
d7a8d9e9 748 if (flags & TRACE_GRAPH_PRINT_DURATION) {
9005f3eb
FW
749 ret = print_graph_duration(duration, s);
750 if (ret == TRACE_TYPE_PARTIAL_LINE)
751 return TRACE_TYPE_PARTIAL_LINE;
752 }
437f24fb 753
83a8df61
FW
754 /* Function */
755 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
756 ret = trace_seq_printf(s, " ");
757 if (!ret)
758 return TRACE_TYPE_PARTIAL_LINE;
759 }
760
b375a11a 761 ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
83a8df61
FW
762 if (!ret)
763 return TRACE_TYPE_PARTIAL_LINE;
764
765 return TRACE_TYPE_HANDLED;
766}
767
768static enum print_line_t
2fbcdb35
SR
769print_graph_entry_nested(struct trace_iterator *iter,
770 struct ftrace_graph_ent_entry *entry,
d7a8d9e9 771 struct trace_seq *s, int cpu, u32 flags)
83a8df61 772{
83a8df61 773 struct ftrace_graph_ent *call = &entry->graph_ent;
2fbcdb35
SR
774 struct fgraph_data *data = iter->private;
775 int ret;
776 int i;
777
778 if (data) {
f1c7f517 779 struct fgraph_cpu_data *cpu_data;
2fbcdb35 780 int cpu = iter->cpu;
2fbcdb35 781
f1c7f517
SR
782 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
783 cpu_data->depth = call->depth;
784
785 /* Save this function pointer to see if the exit matches */
786 if (call->depth < FTRACE_RETFUNC_DEPTH)
787 cpu_data->enter_funcs[call->depth] = call->func;
2fbcdb35 788 }
83a8df61
FW
789
790 /* No overhead */
d7a8d9e9 791 ret = print_graph_overhead(-1, s, flags);
9005f3eb
FW
792 if (!ret)
793 return TRACE_TYPE_PARTIAL_LINE;
1a056155 794
9005f3eb 795 /* No time */
d7a8d9e9 796 if (flags & TRACE_GRAPH_PRINT_DURATION) {
f8b755ac
FW
797 ret = trace_seq_printf(s, " | ");
798 if (!ret)
799 return TRACE_TYPE_PARTIAL_LINE;
f8b755ac
FW
800 }
801
83a8df61 802 /* Function */
287b6e68
FW
803 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
804 ret = trace_seq_printf(s, " ");
fb52607a
FW
805 if (!ret)
806 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
807 }
808
b375a11a 809 ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
83a8df61
FW
810 if (!ret)
811 return TRACE_TYPE_PARTIAL_LINE;
812
b91facc3
FW
813 /*
814 * we already consumed the current entry to check the next one
815 * and see if this is a leaf.
816 */
817 return TRACE_TYPE_NO_CONSUME;
287b6e68
FW
818}
819
83a8df61 820static enum print_line_t
ac5f6c96 821print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
d7a8d9e9 822 int type, unsigned long addr, u32 flags)
83a8df61 823{
2fbcdb35 824 struct fgraph_data *data = iter->private;
83a8df61 825 struct trace_entry *ent = iter->ent;
ac5f6c96
SR
826 int cpu = iter->cpu;
827 int ret;
83a8df61 828
1a056155 829 /* Pid */
2fbcdb35 830 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
9005f3eb
FW
831 return TRACE_TYPE_PARTIAL_LINE;
832
ac5f6c96
SR
833 if (type) {
834 /* Interrupt */
d7a8d9e9 835 ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
ac5f6c96
SR
836 if (ret == TRACE_TYPE_PARTIAL_LINE)
837 return TRACE_TYPE_PARTIAL_LINE;
838 }
83a8df61 839
9005f3eb 840 /* Absolute time */
d7a8d9e9 841 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
9005f3eb
FW
842 ret = print_graph_abs_time(iter->ts, s);
843 if (!ret)
844 return TRACE_TYPE_PARTIAL_LINE;
845 }
846
1a056155 847 /* Cpu */
d7a8d9e9 848 if (flags & TRACE_GRAPH_PRINT_CPU) {
1a056155 849 ret = print_graph_cpu(s, cpu);
11e84acc
FW
850 if (ret == TRACE_TYPE_PARTIAL_LINE)
851 return TRACE_TYPE_PARTIAL_LINE;
852 }
853
854 /* Proc */
d7a8d9e9 855 if (flags & TRACE_GRAPH_PRINT_PROC) {
00a8bf85 856 ret = print_graph_proc(s, ent->pid);
11e84acc
FW
857 if (ret == TRACE_TYPE_PARTIAL_LINE)
858 return TRACE_TYPE_PARTIAL_LINE;
859
860 ret = trace_seq_printf(s, " | ");
1a056155
FW
861 if (!ret)
862 return TRACE_TYPE_PARTIAL_LINE;
863 }
83a8df61 864
49ff5903
SR
865 /* Latency format */
866 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
867 ret = print_graph_lat_fmt(s, ent);
868 if (ret == TRACE_TYPE_PARTIAL_LINE)
869 return TRACE_TYPE_PARTIAL_LINE;
870 }
871
ac5f6c96
SR
872 return 0;
873}
874
2bd16212
JO
875/*
876 * Entry check for irq code
877 *
878 * returns 1 if
879 * - we are inside irq code
880 * - we just extered irq code
881 *
882 * retunns 0 if
883 * - funcgraph-interrupts option is set
884 * - we are not inside irq code
885 */
886static int
887check_irq_entry(struct trace_iterator *iter, u32 flags,
888 unsigned long addr, int depth)
889{
890 int cpu = iter->cpu;
891 struct fgraph_data *data = iter->private;
892 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
893
894 if (flags & TRACE_GRAPH_PRINT_IRQS)
895 return 0;
896
897 /*
898 * We are inside the irq code
899 */
900 if (*depth_irq >= 0)
901 return 1;
902
903 if ((addr < (unsigned long)__irqentry_text_start) ||
904 (addr >= (unsigned long)__irqentry_text_end))
905 return 0;
906
907 /*
908 * We are entering irq code.
909 */
910 *depth_irq = depth;
911 return 1;
912}
913
914/*
915 * Return check for irq code
916 *
917 * returns 1 if
918 * - we are inside irq code
919 * - we just left irq code
920 *
921 * returns 0 if
922 * - funcgraph-interrupts option is set
923 * - we are not inside irq code
924 */
925static int
926check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
927{
928 int cpu = iter->cpu;
929 struct fgraph_data *data = iter->private;
930 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
931
932 if (flags & TRACE_GRAPH_PRINT_IRQS)
933 return 0;
934
935 /*
936 * We are not inside the irq code.
937 */
938 if (*depth_irq == -1)
939 return 0;
940
941 /*
942 * We are inside the irq code, and this is returning entry.
943 * Let's not trace it and clear the entry depth, since
944 * we are out of irq code.
945 *
946 * This condition ensures that we 'leave the irq code' once
947 * we are out of the entry depth. Thus protecting us from
948 * the RETURN entry loss.
949 */
950 if (*depth_irq >= depth) {
951 *depth_irq = -1;
952 return 1;
953 }
954
955 /*
956 * We are inside the irq code, and this is not the entry.
957 */
958 return 1;
959}
960
ac5f6c96
SR
961static enum print_line_t
962print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
d7a8d9e9 963 struct trace_iterator *iter, u32 flags)
ac5f6c96 964{
be1eca39 965 struct fgraph_data *data = iter->private;
ac5f6c96
SR
966 struct ftrace_graph_ent *call = &field->graph_ent;
967 struct ftrace_graph_ret_entry *leaf_ret;
be1eca39
JO
968 static enum print_line_t ret;
969 int cpu = iter->cpu;
ac5f6c96 970
2bd16212
JO
971 if (check_irq_entry(iter, flags, call->func, call->depth))
972 return TRACE_TYPE_HANDLED;
973
d7a8d9e9 974 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
ac5f6c96
SR
975 return TRACE_TYPE_PARTIAL_LINE;
976
b91facc3
FW
977 leaf_ret = get_return_for_leaf(iter, field);
978 if (leaf_ret)
d7a8d9e9 979 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
83a8df61 980 else
d7a8d9e9 981 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
83a8df61 982
be1eca39
JO
983 if (data) {
984 /*
985 * If we failed to write our output, then we need to make
986 * note of it. Because we already consumed our entry.
987 */
988 if (s->full) {
989 data->failed = 1;
990 data->cpu = cpu;
991 } else
992 data->failed = 0;
993 }
994
995 return ret;
83a8df61
FW
996}
997
287b6e68
FW
998static enum print_line_t
999print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
d7a8d9e9
JO
1000 struct trace_entry *ent, struct trace_iterator *iter,
1001 u32 flags)
287b6e68 1002{
83a8df61 1003 unsigned long long duration = trace->rettime - trace->calltime;
2fbcdb35
SR
1004 struct fgraph_data *data = iter->private;
1005 pid_t pid = ent->pid;
1006 int cpu = iter->cpu;
f1c7f517 1007 int func_match = 1;
2fbcdb35
SR
1008 int ret;
1009 int i;
1010
2bd16212
JO
1011 if (check_irq_return(iter, flags, trace->depth))
1012 return TRACE_TYPE_HANDLED;
1013
2fbcdb35 1014 if (data) {
f1c7f517
SR
1015 struct fgraph_cpu_data *cpu_data;
1016 int cpu = iter->cpu;
1017
1018 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
2fbcdb35
SR
1019
1020 /*
1021 * Comments display at + 1 to depth. This is the
1022 * return from a function, we now want the comments
1023 * to display at the same level of the bracket.
1024 */
f1c7f517
SR
1025 cpu_data->depth = trace->depth - 1;
1026
1027 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1028 if (cpu_data->enter_funcs[trace->depth] != trace->func)
1029 func_match = 0;
1030 cpu_data->enter_funcs[trace->depth] = 0;
1031 }
2fbcdb35 1032 }
287b6e68 1033
d7a8d9e9 1034 if (print_graph_prologue(iter, s, 0, 0, flags))
437f24fb
SR
1035 return TRACE_TYPE_PARTIAL_LINE;
1036
83a8df61 1037 /* Overhead */
d7a8d9e9 1038 ret = print_graph_overhead(duration, s, flags);
9005f3eb
FW
1039 if (!ret)
1040 return TRACE_TYPE_PARTIAL_LINE;
1a056155
FW
1041
1042 /* Duration */
d7a8d9e9 1043 if (flags & TRACE_GRAPH_PRINT_DURATION) {
9005f3eb
FW
1044 ret = print_graph_duration(duration, s);
1045 if (ret == TRACE_TYPE_PARTIAL_LINE)
1046 return TRACE_TYPE_PARTIAL_LINE;
1047 }
83a8df61
FW
1048
1049 /* Closing brace */
287b6e68
FW
1050 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
1051 ret = trace_seq_printf(s, " ");
fb52607a
FW
1052 if (!ret)
1053 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
1054 }
1055
f1c7f517
SR
1056 /*
1057 * If the return function does not have a matching entry,
1058 * then the entry was lost. Instead of just printing
1059 * the '}' and letting the user guess what function this
1060 * belongs to, write out the function name.
1061 */
1062 if (func_match) {
1063 ret = trace_seq_printf(s, "}\n");
1064 if (!ret)
1065 return TRACE_TYPE_PARTIAL_LINE;
1066 } else {
a094fe04 1067 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
f1c7f517
SR
1068 if (!ret)
1069 return TRACE_TYPE_PARTIAL_LINE;
1070 }
fb52607a 1071
83a8df61 1072 /* Overrun */
d7a8d9e9 1073 if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
287b6e68
FW
1074 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
1075 trace->overrun);
fb52607a
FW
1076 if (!ret)
1077 return TRACE_TYPE_PARTIAL_LINE;
287b6e68 1078 }
f8b755ac 1079
d7a8d9e9
JO
1080 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1081 cpu, pid, flags);
f8b755ac
FW
1082 if (ret == TRACE_TYPE_PARTIAL_LINE)
1083 return TRACE_TYPE_PARTIAL_LINE;
1084
287b6e68
FW
1085 return TRACE_TYPE_HANDLED;
1086}
1087
1fd8f2a3 1088static enum print_line_t
d7a8d9e9
JO
1089print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1090 struct trace_iterator *iter, u32 flags)
1fd8f2a3 1091{
5087f8d2 1092 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2fbcdb35 1093 struct fgraph_data *data = iter->private;
5087f8d2 1094 struct trace_event *event;
2fbcdb35 1095 int depth = 0;
1fd8f2a3 1096 int ret;
2fbcdb35
SR
1097 int i;
1098
1099 if (data)
be1eca39 1100 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
9005f3eb 1101
d7a8d9e9 1102 if (print_graph_prologue(iter, s, 0, 0, flags))
d1f9cbd7
FW
1103 return TRACE_TYPE_PARTIAL_LINE;
1104
1fd8f2a3 1105 /* No overhead */
d7a8d9e9 1106 ret = print_graph_overhead(-1, s, flags);
9005f3eb
FW
1107 if (!ret)
1108 return TRACE_TYPE_PARTIAL_LINE;
1109
1110 /* No time */
d7a8d9e9 1111 if (flags & TRACE_GRAPH_PRINT_DURATION) {
9005f3eb 1112 ret = trace_seq_printf(s, " | ");
1fd8f2a3
FW
1113 if (!ret)
1114 return TRACE_TYPE_PARTIAL_LINE;
1115 }
1116
1fd8f2a3 1117 /* Indentation */
2fbcdb35
SR
1118 if (depth > 0)
1119 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
1fd8f2a3
FW
1120 ret = trace_seq_printf(s, " ");
1121 if (!ret)
1122 return TRACE_TYPE_PARTIAL_LINE;
1123 }
1124
1125 /* The comment */
769b0441
FW
1126 ret = trace_seq_printf(s, "/* ");
1127 if (!ret)
1128 return TRACE_TYPE_PARTIAL_LINE;
1129
5087f8d2
SR
1130 switch (iter->ent->type) {
1131 case TRACE_BPRINT:
1132 ret = trace_print_bprintk_msg_only(iter);
1133 if (ret != TRACE_TYPE_HANDLED)
1134 return ret;
1135 break;
1136 case TRACE_PRINT:
1137 ret = trace_print_printk_msg_only(iter);
1138 if (ret != TRACE_TYPE_HANDLED)
1139 return ret;
1140 break;
1141 default:
1142 event = ftrace_find_event(ent->type);
1143 if (!event)
1144 return TRACE_TYPE_UNHANDLED;
1145
a9a57763 1146 ret = event->funcs->trace(iter, sym_flags, event);
5087f8d2
SR
1147 if (ret != TRACE_TYPE_HANDLED)
1148 return ret;
1149 }
1fd8f2a3 1150
412d0bb5
FW
1151 /* Strip ending newline */
1152 if (s->buffer[s->len - 1] == '\n') {
1153 s->buffer[s->len - 1] = '\0';
1154 s->len--;
1155 }
1156
1fd8f2a3
FW
1157 ret = trace_seq_printf(s, " */\n");
1158 if (!ret)
1159 return TRACE_TYPE_PARTIAL_LINE;
1160
1161 return TRACE_TYPE_HANDLED;
1162}
1163
1164
287b6e68 1165enum print_line_t
d7a8d9e9 1166print_graph_function_flags(struct trace_iterator *iter, u32 flags)
287b6e68 1167{
be1eca39
JO
1168 struct ftrace_graph_ent_entry *field;
1169 struct fgraph_data *data = iter->private;
287b6e68 1170 struct trace_entry *entry = iter->ent;
5087f8d2 1171 struct trace_seq *s = &iter->seq;
be1eca39
JO
1172 int cpu = iter->cpu;
1173 int ret;
1174
1175 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1176 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1177 return TRACE_TYPE_HANDLED;
1178 }
1179
1180 /*
1181 * If the last output failed, there's a possibility we need
1182 * to print out the missing entry which would never go out.
1183 */
1184 if (data && data->failed) {
1185 field = &data->ent;
1186 iter->cpu = data->cpu;
d7a8d9e9 1187 ret = print_graph_entry(field, s, iter, flags);
be1eca39
JO
1188 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1189 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1190 ret = TRACE_TYPE_NO_CONSUME;
1191 }
1192 iter->cpu = cpu;
1193 return ret;
1194 }
fb52607a 1195
287b6e68
FW
1196 switch (entry->type) {
1197 case TRACE_GRAPH_ENT: {
38ceb592
LJ
1198 /*
1199 * print_graph_entry() may consume the current event,
1200 * thus @field may become invalid, so we need to save it.
1201 * sizeof(struct ftrace_graph_ent_entry) is very small,
1202 * it can be safely saved at the stack.
1203 */
be1eca39 1204 struct ftrace_graph_ent_entry saved;
287b6e68 1205 trace_assign_type(field, entry);
38ceb592 1206 saved = *field;
d7a8d9e9 1207 return print_graph_entry(&saved, s, iter, flags);
287b6e68
FW
1208 }
1209 case TRACE_GRAPH_RET: {
1210 struct ftrace_graph_ret_entry *field;
1211 trace_assign_type(field, entry);
d7a8d9e9 1212 return print_graph_return(&field->ret, s, entry, iter, flags);
287b6e68 1213 }
62b915f1
JO
1214 case TRACE_STACK:
1215 case TRACE_FN:
1216 /* dont trace stack and functions as comments */
1217 return TRACE_TYPE_UNHANDLED;
1218
287b6e68 1219 default:
d7a8d9e9 1220 return print_graph_comment(s, entry, iter, flags);
fb52607a 1221 }
5087f8d2
SR
1222
1223 return TRACE_TYPE_HANDLED;
fb52607a
FW
1224}
1225
d7a8d9e9
JO
1226static enum print_line_t
1227print_graph_function(struct trace_iterator *iter)
1228{
1229 return print_graph_function_flags(iter, tracer_flags.val);
1230}
1231
9106b693 1232static enum print_line_t
a9a57763
SR
1233print_graph_function_event(struct trace_iterator *iter, int flags,
1234 struct trace_event *event)
9106b693
JO
1235{
1236 return print_graph_function(iter);
1237}
1238
d7a8d9e9 1239static void print_lat_header(struct seq_file *s, u32 flags)
49ff5903
SR
1240{
1241 static const char spaces[] = " " /* 16 spaces */
1242 " " /* 4 spaces */
1243 " "; /* 17 spaces */
1244 int size = 0;
1245
d7a8d9e9 1246 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
49ff5903 1247 size += 16;
d7a8d9e9 1248 if (flags & TRACE_GRAPH_PRINT_CPU)
49ff5903 1249 size += 4;
d7a8d9e9 1250 if (flags & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1251 size += 17;
1252
1253 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1254 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1255 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1256 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
637e7e86
SR
1257 seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces);
1258 seq_printf(s, "#%.*s|||| / \n", size, spaces);
49ff5903
SR
1259}
1260
d7a8d9e9 1261void print_graph_headers_flags(struct seq_file *s, u32 flags)
decbec38 1262{
49ff5903
SR
1263 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1264
1265 if (lat)
d7a8d9e9 1266 print_lat_header(s, flags);
49ff5903 1267
decbec38 1268 /* 1st line */
49ff5903 1269 seq_printf(s, "#");
d7a8d9e9 1270 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
9005f3eb 1271 seq_printf(s, " TIME ");
d7a8d9e9 1272 if (flags & TRACE_GRAPH_PRINT_CPU)
49ff5903 1273 seq_printf(s, " CPU");
d7a8d9e9 1274 if (flags & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1275 seq_printf(s, " TASK/PID ");
1276 if (lat)
637e7e86 1277 seq_printf(s, "|||||");
d7a8d9e9 1278 if (flags & TRACE_GRAPH_PRINT_DURATION)
9005f3eb
FW
1279 seq_printf(s, " DURATION ");
1280 seq_printf(s, " FUNCTION CALLS\n");
decbec38
FW
1281
1282 /* 2nd line */
49ff5903 1283 seq_printf(s, "#");
d7a8d9e9 1284 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
9005f3eb 1285 seq_printf(s, " | ");
d7a8d9e9 1286 if (flags & TRACE_GRAPH_PRINT_CPU)
49ff5903 1287 seq_printf(s, " | ");
d7a8d9e9 1288 if (flags & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1289 seq_printf(s, " | | ");
1290 if (lat)
637e7e86 1291 seq_printf(s, "|||||");
d7a8d9e9 1292 if (flags & TRACE_GRAPH_PRINT_DURATION)
9005f3eb
FW
1293 seq_printf(s, " | | ");
1294 seq_printf(s, " | | | |\n");
decbec38 1295}
9005f3eb 1296
62b915f1 1297void print_graph_headers(struct seq_file *s)
d7a8d9e9
JO
1298{
1299 print_graph_headers_flags(s, tracer_flags.val);
1300}
1301
62b915f1 1302void graph_trace_open(struct trace_iterator *iter)
9005f3eb 1303{
2fbcdb35 1304 /* pid and depth on the last trace processed */
be1eca39 1305 struct fgraph_data *data;
9005f3eb
FW
1306 int cpu;
1307
be1eca39
JO
1308 iter->private = NULL;
1309
1310 data = kzalloc(sizeof(*data), GFP_KERNEL);
2fbcdb35 1311 if (!data)
be1eca39
JO
1312 goto out_err;
1313
1314 data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1315 if (!data->cpu_data)
1316 goto out_err_free;
1317
1318 for_each_possible_cpu(cpu) {
1319 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1320 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1321 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
2bd16212
JO
1322 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1323
be1eca39
JO
1324 *pid = -1;
1325 *depth = 0;
1326 *ignore = 0;
2bd16212 1327 *depth_irq = -1;
be1eca39 1328 }
9005f3eb 1329
2fbcdb35 1330 iter->private = data;
be1eca39
JO
1331
1332 return;
1333
1334 out_err_free:
1335 kfree(data);
1336 out_err:
1337 pr_warning("function graph tracer: not enough memory\n");
9005f3eb
FW
1338}
1339
62b915f1 1340void graph_trace_close(struct trace_iterator *iter)
9005f3eb 1341{
be1eca39
JO
1342 struct fgraph_data *data = iter->private;
1343
1344 if (data) {
1345 free_percpu(data->cpu_data);
1346 kfree(data);
1347 }
9005f3eb
FW
1348}
1349
b304d044
SR
1350static int func_graph_set_flag(u32 old_flags, u32 bit, int set)
1351{
1352 if (bit == TRACE_GRAPH_PRINT_IRQS)
1353 ftrace_graph_skip_irqs = !set;
1354
1355 return 0;
1356}
1357
a9a57763
SR
1358static struct trace_event_functions graph_functions = {
1359 .trace = print_graph_function_event,
1360};
1361
9106b693
JO
1362static struct trace_event graph_trace_entry_event = {
1363 .type = TRACE_GRAPH_ENT,
a9a57763 1364 .funcs = &graph_functions,
9106b693
JO
1365};
1366
1367static struct trace_event graph_trace_ret_event = {
1368 .type = TRACE_GRAPH_RET,
a9a57763 1369 .funcs = &graph_functions
9106b693
JO
1370};
1371
fb52607a 1372static struct tracer graph_trace __read_mostly = {
ef18012b 1373 .name = "function_graph",
9005f3eb 1374 .open = graph_trace_open,
be1eca39 1375 .pipe_open = graph_trace_open,
9005f3eb 1376 .close = graph_trace_close,
be1eca39 1377 .pipe_close = graph_trace_close,
6eaaa5d5 1378 .wait_pipe = poll_wait_pipe,
ef18012b
SR
1379 .init = graph_trace_init,
1380 .reset = graph_trace_reset,
decbec38
FW
1381 .print_line = print_graph_function,
1382 .print_header = print_graph_headers,
fb52607a 1383 .flags = &tracer_flags,
b304d044 1384 .set_flag = func_graph_set_flag,
7447dce9
FW
1385#ifdef CONFIG_FTRACE_SELFTEST
1386 .selftest = trace_selftest_startup_function_graph,
1387#endif
fb52607a
FW
1388};
1389
1390static __init int init_graph_trace(void)
1391{
0c9e6f63
LJ
1392 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1393
9106b693
JO
1394 if (!register_ftrace_event(&graph_trace_entry_event)) {
1395 pr_warning("Warning: could not register graph trace events\n");
1396 return 1;
1397 }
1398
1399 if (!register_ftrace_event(&graph_trace_ret_event)) {
1400 pr_warning("Warning: could not register graph trace events\n");
1401 return 1;
1402 }
1403
fb52607a
FW
1404 return register_tracer(&graph_trace);
1405}
1406
1407device_initcall(init_graph_trace);