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