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