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