Merge tag 'trace-3.17' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux...
[deliverable/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
110static enum print_line_t
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
FW
485
486static enum print_line_t
487print_graph_cpu(struct trace_seq *s, int cpu)
488{
1a056155 489 int ret;
1a056155 490
d51090b3
IM
491 /*
492 * Start with a space character - to make it stand out
493 * to the right a bit when trace output is pasted into
494 * email:
495 */
0c9e6f63 496 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
1a056155 497 if (!ret)
d51090b3
IM
498 return TRACE_TYPE_PARTIAL_LINE;
499
1a056155
FW
500 return TRACE_TYPE_HANDLED;
501}
502
11e84acc
FW
503#define TRACE_GRAPH_PROCINFO_LENGTH 14
504
505static enum print_line_t
506print_graph_proc(struct trace_seq *s, pid_t pid)
507{
4ca53085 508 char comm[TASK_COMM_LEN];
11e84acc
FW
509 /* sign + log10(MAX_INT) + '\0' */
510 char pid_str[11];
4ca53085
SR
511 int spaces = 0;
512 int ret;
513 int len;
514 int i;
11e84acc 515
4ca53085 516 trace_find_cmdline(pid, comm);
11e84acc
FW
517 comm[7] = '\0';
518 sprintf(pid_str, "%d", pid);
519
520 /* 1 stands for the "-" character */
521 len = strlen(comm) + strlen(pid_str) + 1;
522
523 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
524 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
525
526 /* First spaces to align center */
527 for (i = 0; i < spaces / 2; i++) {
146c3442 528 ret = trace_seq_putc(s, ' ');
11e84acc
FW
529 if (!ret)
530 return TRACE_TYPE_PARTIAL_LINE;
531 }
532
533 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
534 if (!ret)
535 return TRACE_TYPE_PARTIAL_LINE;
536
537 /* Last spaces to align center */
538 for (i = 0; i < spaces - (spaces / 2); i++) {
146c3442 539 ret = trace_seq_putc(s, ' ');
11e84acc
FW
540 if (!ret)
541 return TRACE_TYPE_PARTIAL_LINE;
542 }
543 return TRACE_TYPE_HANDLED;
544}
545
1a056155 546
49ff5903
SR
547static enum print_line_t
548print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
549{
f81c972d 550 if (!trace_seq_putc(s, ' '))
637e7e86
SR
551 return 0;
552
f81c972d 553 return trace_print_lat_fmt(s, entry);
49ff5903
SR
554}
555
287b6e68 556/* If the pid changed since the last trace, output this event */
11e84acc 557static enum print_line_t
2fbcdb35 558verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
287b6e68 559{
d51090b3 560 pid_t prev_pid;
9005f3eb 561 pid_t *last_pid;
d51090b3 562 int ret;
660c7f9b 563
2fbcdb35 564 if (!data)
9005f3eb
FW
565 return TRACE_TYPE_HANDLED;
566
be1eca39 567 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
9005f3eb
FW
568
569 if (*last_pid == pid)
11e84acc 570 return TRACE_TYPE_HANDLED;
fb52607a 571
9005f3eb
FW
572 prev_pid = *last_pid;
573 *last_pid = pid;
d51090b3 574
9005f3eb
FW
575 if (prev_pid == -1)
576 return TRACE_TYPE_HANDLED;
d51090b3
IM
577/*
578 * Context-switch trace line:
579
580 ------------------------------------------
581 | 1) migration/0--1 => sshd-1755
582 ------------------------------------------
583
584 */
146c3442 585 ret = trace_seq_puts(s,
1fd8f2a3 586 " ------------------------------------------\n");
11e84acc 587 if (!ret)
810dc732 588 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
589
590 ret = print_graph_cpu(s, cpu);
591 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 592 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
593
594 ret = print_graph_proc(s, prev_pid);
595 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 596 return TRACE_TYPE_PARTIAL_LINE;
11e84acc 597
146c3442 598 ret = trace_seq_puts(s, " => ");
11e84acc 599 if (!ret)
810dc732 600 return TRACE_TYPE_PARTIAL_LINE;
11e84acc
FW
601
602 ret = print_graph_proc(s, pid);
603 if (ret == TRACE_TYPE_PARTIAL_LINE)
810dc732 604 return TRACE_TYPE_PARTIAL_LINE;
11e84acc 605
146c3442 606 ret = trace_seq_puts(s,
11e84acc
FW
607 "\n ------------------------------------------\n\n");
608 if (!ret)
810dc732 609 return TRACE_TYPE_PARTIAL_LINE;
11e84acc 610
810dc732 611 return TRACE_TYPE_HANDLED;
287b6e68
FW
612}
613
b91facc3
FW
614static struct ftrace_graph_ret_entry *
615get_return_for_leaf(struct trace_iterator *iter,
83a8df61
FW
616 struct ftrace_graph_ent_entry *curr)
617{
be1eca39
JO
618 struct fgraph_data *data = iter->private;
619 struct ring_buffer_iter *ring_iter = NULL;
83a8df61
FW
620 struct ring_buffer_event *event;
621 struct ftrace_graph_ret_entry *next;
622
be1eca39
JO
623 /*
624 * If the previous output failed to write to the seq buffer,
625 * then we just reuse the data from before.
626 */
627 if (data && data->failed) {
628 curr = &data->ent;
629 next = &data->ret;
630 } else {
83a8df61 631
6d158a81 632 ring_iter = trace_buffer_iter(iter, iter->cpu);
be1eca39
JO
633
634 /* First peek to compare current entry and the next one */
635 if (ring_iter)
636 event = ring_buffer_iter_peek(ring_iter, NULL);
637 else {
638 /*
639 * We need to consume the current entry to see
640 * the next one.
641 */
12883efb 642 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
66a8cb95 643 NULL, NULL);
12883efb 644 event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
66a8cb95 645 NULL, NULL);
be1eca39 646 }
83a8df61 647
be1eca39
JO
648 if (!event)
649 return NULL;
650
651 next = ring_buffer_event_data(event);
83a8df61 652
be1eca39
JO
653 if (data) {
654 /*
655 * Save current and next entries for later reference
656 * if the output fails.
657 */
658 data->ent = *curr;
575570f0
SL
659 /*
660 * If the next event is not a return type, then
661 * we only care about what type it is. Otherwise we can
662 * safely copy the entire event.
663 */
664 if (next->ent.type == TRACE_GRAPH_RET)
665 data->ret = *next;
666 else
667 data->ret.ent.type = next->ent.type;
be1eca39
JO
668 }
669 }
83a8df61
FW
670
671 if (next->ent.type != TRACE_GRAPH_RET)
b91facc3 672 return NULL;
83a8df61
FW
673
674 if (curr->ent.pid != next->ent.pid ||
675 curr->graph_ent.func != next->ret.func)
b91facc3 676 return NULL;
83a8df61 677
b91facc3
FW
678 /* this is a leaf, now advance the iterator */
679 if (ring_iter)
680 ring_buffer_read(ring_iter, NULL);
681
682 return next;
83a8df61
FW
683}
684
d1f9cbd7
FW
685static int print_graph_abs_time(u64 t, struct trace_seq *s)
686{
687 unsigned long usecs_rem;
688
689 usecs_rem = do_div(t, NSEC_PER_SEC);
690 usecs_rem /= 1000;
691
692 return trace_seq_printf(s, "%5lu.%06lu | ",
693 (unsigned long)t, usecs_rem);
694}
695
f8b755ac 696static enum print_line_t
d1f9cbd7 697print_graph_irq(struct trace_iterator *iter, unsigned long addr,
d7a8d9e9 698 enum trace_type type, int cpu, pid_t pid, u32 flags)
f8b755ac
FW
699{
700 int ret;
d1f9cbd7 701 struct trace_seq *s = &iter->seq;
f8b755ac
FW
702
703 if (addr < (unsigned long)__irqentry_text_start ||
704 addr >= (unsigned long)__irqentry_text_end)
705 return TRACE_TYPE_UNHANDLED;
706
749230b0
JO
707 if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
708 /* Absolute time */
709 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
710 ret = print_graph_abs_time(iter->ts, s);
711 if (!ret)
712 return TRACE_TYPE_PARTIAL_LINE;
713 }
d1f9cbd7 714
749230b0
JO
715 /* Cpu */
716 if (flags & TRACE_GRAPH_PRINT_CPU) {
717 ret = print_graph_cpu(s, cpu);
718 if (ret == TRACE_TYPE_PARTIAL_LINE)
719 return TRACE_TYPE_PARTIAL_LINE;
720 }
49ff5903 721
749230b0
JO
722 /* Proc */
723 if (flags & TRACE_GRAPH_PRINT_PROC) {
724 ret = print_graph_proc(s, pid);
725 if (ret == TRACE_TYPE_PARTIAL_LINE)
726 return TRACE_TYPE_PARTIAL_LINE;
146c3442 727 ret = trace_seq_puts(s, " | ");
749230b0
JO
728 if (!ret)
729 return TRACE_TYPE_PARTIAL_LINE;
730 }
9005f3eb 731 }
f8b755ac 732
9005f3eb 733 /* No overhead */
6fc84ea7 734 ret = print_graph_duration(0, s, flags | FLAGS_FILL_START);
ffeb80fc
JO
735 if (ret != TRACE_TYPE_HANDLED)
736 return ret;
f8b755ac 737
9005f3eb 738 if (type == TRACE_GRAPH_ENT)
146c3442 739 ret = trace_seq_puts(s, "==========>");
9005f3eb 740 else
146c3442 741 ret = trace_seq_puts(s, "<==========");
9005f3eb
FW
742
743 if (!ret)
744 return TRACE_TYPE_PARTIAL_LINE;
745
6fc84ea7 746 ret = print_graph_duration(0, s, flags | FLAGS_FILL_END);
ffeb80fc
JO
747 if (ret != TRACE_TYPE_HANDLED)
748 return ret;
749
146c3442 750 ret = trace_seq_putc(s, '\n');
f8b755ac 751
f8b755ac
FW
752 if (!ret)
753 return TRACE_TYPE_PARTIAL_LINE;
754 return TRACE_TYPE_HANDLED;
755}
83a8df61 756
0706f1c4
SR
757enum print_line_t
758trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
83a8df61
FW
759{
760 unsigned long nsecs_rem = do_div(duration, 1000);
166d3c79
FW
761 /* log10(ULONG_MAX) + '\0' */
762 char msecs_str[21];
763 char nsecs_str[5];
764 int ret, len;
765 int i;
766
767 sprintf(msecs_str, "%lu", (unsigned long) duration);
768
769 /* Print msecs */
9005f3eb 770 ret = trace_seq_printf(s, "%s", msecs_str);
166d3c79
FW
771 if (!ret)
772 return TRACE_TYPE_PARTIAL_LINE;
773
774 len = strlen(msecs_str);
775
776 /* Print nsecs (we don't want to exceed 7 numbers) */
777 if (len < 7) {
14cae9bd
BP
778 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
779
780 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
166d3c79
FW
781 ret = trace_seq_printf(s, ".%s", nsecs_str);
782 if (!ret)
783 return TRACE_TYPE_PARTIAL_LINE;
784 len += strlen(nsecs_str);
785 }
786
146c3442 787 ret = trace_seq_puts(s, " us ");
166d3c79
FW
788 if (!ret)
789 return TRACE_TYPE_PARTIAL_LINE;
790
791 /* Print remaining spaces to fit the row's width */
792 for (i = len; i < 7; i++) {
146c3442 793 ret = trace_seq_putc(s, ' ');
166d3c79
FW
794 if (!ret)
795 return TRACE_TYPE_PARTIAL_LINE;
796 }
0706f1c4
SR
797 return TRACE_TYPE_HANDLED;
798}
799
800static enum print_line_t
ffeb80fc
JO
801print_graph_duration(unsigned long long duration, struct trace_seq *s,
802 u32 flags)
0706f1c4 803{
ffeb80fc
JO
804 int ret = -1;
805
749230b0
JO
806 if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
807 !(trace_flags & TRACE_ITER_CONTEXT_INFO))
808 return TRACE_TYPE_HANDLED;
ffeb80fc
JO
809
810 /* No real adata, just filling the column with spaces */
6fc84ea7
SRRH
811 switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
812 case FLAGS_FILL_FULL:
146c3442 813 ret = trace_seq_puts(s, " | ");
ffeb80fc 814 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
6fc84ea7 815 case FLAGS_FILL_START:
146c3442 816 ret = trace_seq_puts(s, " ");
ffeb80fc 817 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
6fc84ea7 818 case FLAGS_FILL_END:
146c3442 819 ret = trace_seq_puts(s, " |");
ffeb80fc
JO
820 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
821 }
822
823 /* Signal a overhead of time execution to the output */
824 if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
825 /* Duration exceeded 100 msecs */
826 if (duration > 100000ULL)
146c3442 827 ret = trace_seq_puts(s, "! ");
ffeb80fc
JO
828 /* Duration exceeded 10 msecs */
829 else if (duration > 10000ULL)
146c3442 830 ret = trace_seq_puts(s, "+ ");
ffeb80fc
JO
831 }
832
833 /*
834 * The -1 means we either did not exceed the duration tresholds
835 * or we dont want to print out the overhead. Either way we need
836 * to fill out the space.
837 */
838 if (ret == -1)
146c3442 839 ret = trace_seq_puts(s, " ");
ffeb80fc
JO
840
841 /* Catching here any failure happenned above */
842 if (!ret)
843 return TRACE_TYPE_PARTIAL_LINE;
0706f1c4
SR
844
845 ret = trace_print_graph_duration(duration, s);
846 if (ret != TRACE_TYPE_HANDLED)
847 return ret;
166d3c79 848
146c3442 849 ret = trace_seq_puts(s, "| ");
166d3c79
FW
850 if (!ret)
851 return TRACE_TYPE_PARTIAL_LINE;
166d3c79 852
0706f1c4 853 return TRACE_TYPE_HANDLED;
83a8df61
FW
854}
855
83a8df61 856/* Case of a leaf function on its call entry */
287b6e68 857static enum print_line_t
83a8df61 858print_graph_entry_leaf(struct trace_iterator *iter,
b91facc3 859 struct ftrace_graph_ent_entry *entry,
d7a8d9e9
JO
860 struct ftrace_graph_ret_entry *ret_entry,
861 struct trace_seq *s, u32 flags)
fb52607a 862{
2fbcdb35 863 struct fgraph_data *data = iter->private;
83a8df61 864 struct ftrace_graph_ret *graph_ret;
83a8df61
FW
865 struct ftrace_graph_ent *call;
866 unsigned long long duration;
fb52607a 867 int ret;
1a056155 868 int i;
fb52607a 869
83a8df61
FW
870 graph_ret = &ret_entry->ret;
871 call = &entry->graph_ent;
872 duration = graph_ret->rettime - graph_ret->calltime;
873
2fbcdb35 874 if (data) {
f1c7f517 875 struct fgraph_cpu_data *cpu_data;
2fbcdb35 876 int cpu = iter->cpu;
f1c7f517
SR
877
878 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
2fbcdb35
SR
879
880 /*
881 * Comments display at + 1 to depth. Since
882 * this is a leaf function, keep the comments
883 * equal to this depth.
884 */
f1c7f517
SR
885 cpu_data->depth = call->depth - 1;
886
887 /* No need to keep this function around for this depth */
888 if (call->depth < FTRACE_RETFUNC_DEPTH)
889 cpu_data->enter_funcs[call->depth] = 0;
2fbcdb35
SR
890 }
891
ffeb80fc
JO
892 /* Overhead and duration */
893 ret = print_graph_duration(duration, s, flags);
894 if (ret == TRACE_TYPE_PARTIAL_LINE)
9005f3eb 895 return TRACE_TYPE_PARTIAL_LINE;
1a056155 896
83a8df61
FW
897 /* Function */
898 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
146c3442 899 ret = trace_seq_putc(s, ' ');
83a8df61
FW
900 if (!ret)
901 return TRACE_TYPE_PARTIAL_LINE;
902 }
903
b375a11a 904 ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
83a8df61
FW
905 if (!ret)
906 return TRACE_TYPE_PARTIAL_LINE;
907
908 return TRACE_TYPE_HANDLED;
909}
910
911static enum print_line_t
2fbcdb35
SR
912print_graph_entry_nested(struct trace_iterator *iter,
913 struct ftrace_graph_ent_entry *entry,
d7a8d9e9 914 struct trace_seq *s, int cpu, u32 flags)
83a8df61 915{
83a8df61 916 struct ftrace_graph_ent *call = &entry->graph_ent;
2fbcdb35
SR
917 struct fgraph_data *data = iter->private;
918 int ret;
919 int i;
920
921 if (data) {
f1c7f517 922 struct fgraph_cpu_data *cpu_data;
2fbcdb35 923 int cpu = iter->cpu;
2fbcdb35 924
f1c7f517
SR
925 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
926 cpu_data->depth = call->depth;
927
928 /* Save this function pointer to see if the exit matches */
929 if (call->depth < FTRACE_RETFUNC_DEPTH)
930 cpu_data->enter_funcs[call->depth] = call->func;
2fbcdb35 931 }
83a8df61 932
9005f3eb 933 /* No time */
6fc84ea7 934 ret = print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
ffeb80fc
JO
935 if (ret != TRACE_TYPE_HANDLED)
936 return ret;
f8b755ac 937
83a8df61 938 /* Function */
287b6e68 939 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
146c3442 940 ret = trace_seq_putc(s, ' ');
fb52607a
FW
941 if (!ret)
942 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
943 }
944
b375a11a 945 ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
83a8df61
FW
946 if (!ret)
947 return TRACE_TYPE_PARTIAL_LINE;
948
b91facc3
FW
949 /*
950 * we already consumed the current entry to check the next one
951 * and see if this is a leaf.
952 */
953 return TRACE_TYPE_NO_CONSUME;
287b6e68
FW
954}
955
83a8df61 956static enum print_line_t
ac5f6c96 957print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
d7a8d9e9 958 int type, unsigned long addr, u32 flags)
83a8df61 959{
2fbcdb35 960 struct fgraph_data *data = iter->private;
83a8df61 961 struct trace_entry *ent = iter->ent;
ac5f6c96
SR
962 int cpu = iter->cpu;
963 int ret;
83a8df61 964
1a056155 965 /* Pid */
2fbcdb35 966 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
9005f3eb
FW
967 return TRACE_TYPE_PARTIAL_LINE;
968
ac5f6c96
SR
969 if (type) {
970 /* Interrupt */
d7a8d9e9 971 ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
ac5f6c96
SR
972 if (ret == TRACE_TYPE_PARTIAL_LINE)
973 return TRACE_TYPE_PARTIAL_LINE;
974 }
83a8df61 975
749230b0
JO
976 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
977 return 0;
978
9005f3eb 979 /* Absolute time */
d7a8d9e9 980 if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
9005f3eb
FW
981 ret = print_graph_abs_time(iter->ts, s);
982 if (!ret)
983 return TRACE_TYPE_PARTIAL_LINE;
984 }
985
1a056155 986 /* Cpu */
d7a8d9e9 987 if (flags & TRACE_GRAPH_PRINT_CPU) {
1a056155 988 ret = print_graph_cpu(s, cpu);
11e84acc
FW
989 if (ret == TRACE_TYPE_PARTIAL_LINE)
990 return TRACE_TYPE_PARTIAL_LINE;
991 }
992
993 /* Proc */
d7a8d9e9 994 if (flags & TRACE_GRAPH_PRINT_PROC) {
00a8bf85 995 ret = print_graph_proc(s, ent->pid);
11e84acc
FW
996 if (ret == TRACE_TYPE_PARTIAL_LINE)
997 return TRACE_TYPE_PARTIAL_LINE;
998
146c3442 999 ret = trace_seq_puts(s, " | ");
1a056155
FW
1000 if (!ret)
1001 return TRACE_TYPE_PARTIAL_LINE;
1002 }
83a8df61 1003
49ff5903
SR
1004 /* Latency format */
1005 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
1006 ret = print_graph_lat_fmt(s, ent);
1007 if (ret == TRACE_TYPE_PARTIAL_LINE)
1008 return TRACE_TYPE_PARTIAL_LINE;
1009 }
1010
ac5f6c96
SR
1011 return 0;
1012}
1013
2bd16212
JO
1014/*
1015 * Entry check for irq code
1016 *
1017 * returns 1 if
1018 * - we are inside irq code
25985edc 1019 * - we just entered irq code
2bd16212
JO
1020 *
1021 * retunns 0 if
1022 * - funcgraph-interrupts option is set
1023 * - we are not inside irq code
1024 */
1025static int
1026check_irq_entry(struct trace_iterator *iter, u32 flags,
1027 unsigned long addr, int depth)
1028{
1029 int cpu = iter->cpu;
a9d61173 1030 int *depth_irq;
2bd16212 1031 struct fgraph_data *data = iter->private;
2bd16212 1032
a9d61173
JO
1033 /*
1034 * If we are either displaying irqs, or we got called as
1035 * a graph event and private data does not exist,
1036 * then we bypass the irq check.
1037 */
1038 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
1039 (!data))
2bd16212
JO
1040 return 0;
1041
a9d61173
JO
1042 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1043
2bd16212
JO
1044 /*
1045 * We are inside the irq code
1046 */
1047 if (*depth_irq >= 0)
1048 return 1;
1049
1050 if ((addr < (unsigned long)__irqentry_text_start) ||
1051 (addr >= (unsigned long)__irqentry_text_end))
1052 return 0;
1053
1054 /*
1055 * We are entering irq code.
1056 */
1057 *depth_irq = depth;
1058 return 1;
1059}
1060
1061/*
1062 * Return check for irq code
1063 *
1064 * returns 1 if
1065 * - we are inside irq code
1066 * - we just left irq code
1067 *
1068 * returns 0 if
1069 * - funcgraph-interrupts option is set
1070 * - we are not inside irq code
1071 */
1072static int
1073check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
1074{
1075 int cpu = iter->cpu;
a9d61173 1076 int *depth_irq;
2bd16212 1077 struct fgraph_data *data = iter->private;
2bd16212 1078
a9d61173
JO
1079 /*
1080 * If we are either displaying irqs, or we got called as
1081 * a graph event and private data does not exist,
1082 * then we bypass the irq check.
1083 */
1084 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
1085 (!data))
2bd16212
JO
1086 return 0;
1087
a9d61173
JO
1088 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1089
2bd16212
JO
1090 /*
1091 * We are not inside the irq code.
1092 */
1093 if (*depth_irq == -1)
1094 return 0;
1095
1096 /*
1097 * We are inside the irq code, and this is returning entry.
1098 * Let's not trace it and clear the entry depth, since
1099 * we are out of irq code.
1100 *
1101 * This condition ensures that we 'leave the irq code' once
1102 * we are out of the entry depth. Thus protecting us from
1103 * the RETURN entry loss.
1104 */
1105 if (*depth_irq >= depth) {
1106 *depth_irq = -1;
1107 return 1;
1108 }
1109
1110 /*
1111 * We are inside the irq code, and this is not the entry.
1112 */
1113 return 1;
1114}
1115
ac5f6c96
SR
1116static enum print_line_t
1117print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
d7a8d9e9 1118 struct trace_iterator *iter, u32 flags)
ac5f6c96 1119{
be1eca39 1120 struct fgraph_data *data = iter->private;
ac5f6c96
SR
1121 struct ftrace_graph_ent *call = &field->graph_ent;
1122 struct ftrace_graph_ret_entry *leaf_ret;
be1eca39
JO
1123 static enum print_line_t ret;
1124 int cpu = iter->cpu;
ac5f6c96 1125
2bd16212
JO
1126 if (check_irq_entry(iter, flags, call->func, call->depth))
1127 return TRACE_TYPE_HANDLED;
1128
d7a8d9e9 1129 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
ac5f6c96
SR
1130 return TRACE_TYPE_PARTIAL_LINE;
1131
b91facc3
FW
1132 leaf_ret = get_return_for_leaf(iter, field);
1133 if (leaf_ret)
d7a8d9e9 1134 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
83a8df61 1135 else
d7a8d9e9 1136 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
83a8df61 1137
be1eca39
JO
1138 if (data) {
1139 /*
1140 * If we failed to write our output, then we need to make
1141 * note of it. Because we already consumed our entry.
1142 */
1143 if (s->full) {
1144 data->failed = 1;
1145 data->cpu = cpu;
1146 } else
1147 data->failed = 0;
1148 }
1149
1150 return ret;
83a8df61
FW
1151}
1152
287b6e68
FW
1153static enum print_line_t
1154print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
d7a8d9e9
JO
1155 struct trace_entry *ent, struct trace_iterator *iter,
1156 u32 flags)
287b6e68 1157{
83a8df61 1158 unsigned long long duration = trace->rettime - trace->calltime;
2fbcdb35
SR
1159 struct fgraph_data *data = iter->private;
1160 pid_t pid = ent->pid;
1161 int cpu = iter->cpu;
f1c7f517 1162 int func_match = 1;
2fbcdb35
SR
1163 int ret;
1164 int i;
1165
2bd16212
JO
1166 if (check_irq_return(iter, flags, trace->depth))
1167 return TRACE_TYPE_HANDLED;
1168
2fbcdb35 1169 if (data) {
f1c7f517
SR
1170 struct fgraph_cpu_data *cpu_data;
1171 int cpu = iter->cpu;
1172
1173 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
2fbcdb35
SR
1174
1175 /*
1176 * Comments display at + 1 to depth. This is the
1177 * return from a function, we now want the comments
1178 * to display at the same level of the bracket.
1179 */
f1c7f517
SR
1180 cpu_data->depth = trace->depth - 1;
1181
1182 if (trace->depth < FTRACE_RETFUNC_DEPTH) {
1183 if (cpu_data->enter_funcs[trace->depth] != trace->func)
1184 func_match = 0;
1185 cpu_data->enter_funcs[trace->depth] = 0;
1186 }
2fbcdb35 1187 }
287b6e68 1188
d7a8d9e9 1189 if (print_graph_prologue(iter, s, 0, 0, flags))
437f24fb
SR
1190 return TRACE_TYPE_PARTIAL_LINE;
1191
ffeb80fc
JO
1192 /* Overhead and duration */
1193 ret = print_graph_duration(duration, s, flags);
1194 if (ret == TRACE_TYPE_PARTIAL_LINE)
9005f3eb 1195 return TRACE_TYPE_PARTIAL_LINE;
1a056155 1196
83a8df61 1197 /* Closing brace */
287b6e68 1198 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
146c3442 1199 ret = trace_seq_putc(s, ' ');
fb52607a
FW
1200 if (!ret)
1201 return TRACE_TYPE_PARTIAL_LINE;
287b6e68
FW
1202 }
1203
f1c7f517
SR
1204 /*
1205 * If the return function does not have a matching entry,
1206 * then the entry was lost. Instead of just printing
1207 * the '}' and letting the user guess what function this
607e3a29
RE
1208 * belongs to, write out the function name. Always do
1209 * that if the funcgraph-tail option is enabled.
f1c7f517 1210 */
607e3a29 1211 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL)) {
146c3442 1212 ret = trace_seq_puts(s, "}\n");
f1c7f517
SR
1213 if (!ret)
1214 return TRACE_TYPE_PARTIAL_LINE;
1215 } else {
a094fe04 1216 ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
f1c7f517
SR
1217 if (!ret)
1218 return TRACE_TYPE_PARTIAL_LINE;
1219 }
fb52607a 1220
83a8df61 1221 /* Overrun */
d7a8d9e9 1222 if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
287b6e68
FW
1223 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
1224 trace->overrun);
fb52607a
FW
1225 if (!ret)
1226 return TRACE_TYPE_PARTIAL_LINE;
287b6e68 1227 }
f8b755ac 1228
d7a8d9e9
JO
1229 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
1230 cpu, pid, flags);
f8b755ac
FW
1231 if (ret == TRACE_TYPE_PARTIAL_LINE)
1232 return TRACE_TYPE_PARTIAL_LINE;
1233
287b6e68
FW
1234 return TRACE_TYPE_HANDLED;
1235}
1236
1fd8f2a3 1237static enum print_line_t
d7a8d9e9
JO
1238print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
1239 struct trace_iterator *iter, u32 flags)
1fd8f2a3 1240{
5087f8d2 1241 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
2fbcdb35 1242 struct fgraph_data *data = iter->private;
5087f8d2 1243 struct trace_event *event;
2fbcdb35 1244 int depth = 0;
1fd8f2a3 1245 int ret;
2fbcdb35
SR
1246 int i;
1247
1248 if (data)
be1eca39 1249 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
9005f3eb 1250
d7a8d9e9 1251 if (print_graph_prologue(iter, s, 0, 0, flags))
d1f9cbd7
FW
1252 return TRACE_TYPE_PARTIAL_LINE;
1253
9005f3eb 1254 /* No time */
6fc84ea7 1255 ret = print_graph_duration(0, s, flags | FLAGS_FILL_FULL);
ffeb80fc
JO
1256 if (ret != TRACE_TYPE_HANDLED)
1257 return ret;
1fd8f2a3 1258
1fd8f2a3 1259 /* Indentation */
2fbcdb35
SR
1260 if (depth > 0)
1261 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
146c3442 1262 ret = trace_seq_putc(s, ' ');
1fd8f2a3
FW
1263 if (!ret)
1264 return TRACE_TYPE_PARTIAL_LINE;
1265 }
1266
1267 /* The comment */
146c3442 1268 ret = trace_seq_puts(s, "/* ");
769b0441
FW
1269 if (!ret)
1270 return TRACE_TYPE_PARTIAL_LINE;
1271
5087f8d2
SR
1272 switch (iter->ent->type) {
1273 case TRACE_BPRINT:
1274 ret = trace_print_bprintk_msg_only(iter);
1275 if (ret != TRACE_TYPE_HANDLED)
1276 return ret;
1277 break;
1278 case TRACE_PRINT:
1279 ret = trace_print_printk_msg_only(iter);
1280 if (ret != TRACE_TYPE_HANDLED)
1281 return ret;
1282 break;
1283 default:
1284 event = ftrace_find_event(ent->type);
1285 if (!event)
1286 return TRACE_TYPE_UNHANDLED;
1287
a9a57763 1288 ret = event->funcs->trace(iter, sym_flags, event);
5087f8d2
SR
1289 if (ret != TRACE_TYPE_HANDLED)
1290 return ret;
1291 }
1fd8f2a3 1292
412d0bb5
FW
1293 /* Strip ending newline */
1294 if (s->buffer[s->len - 1] == '\n') {
1295 s->buffer[s->len - 1] = '\0';
1296 s->len--;
1297 }
1298
146c3442 1299 ret = trace_seq_puts(s, " */\n");
1fd8f2a3
FW
1300 if (!ret)
1301 return TRACE_TYPE_PARTIAL_LINE;
1302
1303 return TRACE_TYPE_HANDLED;
1304}
1305
1306
287b6e68 1307enum print_line_t
321e68b0 1308print_graph_function_flags(struct trace_iterator *iter, u32 flags)
287b6e68 1309{
be1eca39
JO
1310 struct ftrace_graph_ent_entry *field;
1311 struct fgraph_data *data = iter->private;
287b6e68 1312 struct trace_entry *entry = iter->ent;
5087f8d2 1313 struct trace_seq *s = &iter->seq;
be1eca39
JO
1314 int cpu = iter->cpu;
1315 int ret;
1316
1317 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1318 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1319 return TRACE_TYPE_HANDLED;
1320 }
1321
1322 /*
1323 * If the last output failed, there's a possibility we need
1324 * to print out the missing entry which would never go out.
1325 */
1326 if (data && data->failed) {
1327 field = &data->ent;
1328 iter->cpu = data->cpu;
d7a8d9e9 1329 ret = print_graph_entry(field, s, iter, flags);
be1eca39
JO
1330 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1331 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1332 ret = TRACE_TYPE_NO_CONSUME;
1333 }
1334 iter->cpu = cpu;
1335 return ret;
1336 }
fb52607a 1337
287b6e68
FW
1338 switch (entry->type) {
1339 case TRACE_GRAPH_ENT: {
38ceb592
LJ
1340 /*
1341 * print_graph_entry() may consume the current event,
1342 * thus @field may become invalid, so we need to save it.
1343 * sizeof(struct ftrace_graph_ent_entry) is very small,
1344 * it can be safely saved at the stack.
1345 */
be1eca39 1346 struct ftrace_graph_ent_entry saved;
287b6e68 1347 trace_assign_type(field, entry);
38ceb592 1348 saved = *field;
d7a8d9e9 1349 return print_graph_entry(&saved, s, iter, flags);
287b6e68
FW
1350 }
1351 case TRACE_GRAPH_RET: {
1352 struct ftrace_graph_ret_entry *field;
1353 trace_assign_type(field, entry);
d7a8d9e9 1354 return print_graph_return(&field->ret, s, entry, iter, flags);
287b6e68 1355 }
62b915f1
JO
1356 case TRACE_STACK:
1357 case TRACE_FN:
1358 /* dont trace stack and functions as comments */
1359 return TRACE_TYPE_UNHANDLED;
1360
287b6e68 1361 default:
d7a8d9e9 1362 return print_graph_comment(s, entry, iter, flags);
fb52607a 1363 }
5087f8d2
SR
1364
1365 return TRACE_TYPE_HANDLED;
fb52607a
FW
1366}
1367
d7a8d9e9
JO
1368static enum print_line_t
1369print_graph_function(struct trace_iterator *iter)
1370{
321e68b0 1371 return print_graph_function_flags(iter, tracer_flags.val);
d7a8d9e9
JO
1372}
1373
9106b693 1374static enum print_line_t
a9a57763
SR
1375print_graph_function_event(struct trace_iterator *iter, int flags,
1376 struct trace_event *event)
9106b693
JO
1377{
1378 return print_graph_function(iter);
1379}
1380
d7a8d9e9 1381static void print_lat_header(struct seq_file *s, u32 flags)
49ff5903
SR
1382{
1383 static const char spaces[] = " " /* 16 spaces */
1384 " " /* 4 spaces */
1385 " "; /* 17 spaces */
1386 int size = 0;
1387
d7a8d9e9 1388 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
49ff5903 1389 size += 16;
d7a8d9e9 1390 if (flags & TRACE_GRAPH_PRINT_CPU)
49ff5903 1391 size += 4;
d7a8d9e9 1392 if (flags & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1393 size += 17;
1394
1395 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1396 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1397 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1398 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
199abfab 1399 seq_printf(s, "#%.*s||| / \n", size, spaces);
49ff5903
SR
1400}
1401
0a772620 1402static void __print_graph_headers_flags(struct seq_file *s, u32 flags)
decbec38 1403{
49ff5903
SR
1404 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1405
1406 if (lat)
d7a8d9e9 1407 print_lat_header(s, flags);
49ff5903 1408
decbec38 1409 /* 1st line */
49ff5903 1410 seq_printf(s, "#");
d7a8d9e9 1411 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
9005f3eb 1412 seq_printf(s, " TIME ");
d7a8d9e9 1413 if (flags & TRACE_GRAPH_PRINT_CPU)
49ff5903 1414 seq_printf(s, " CPU");
d7a8d9e9 1415 if (flags & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1416 seq_printf(s, " TASK/PID ");
1417 if (lat)
199abfab 1418 seq_printf(s, "||||");
d7a8d9e9 1419 if (flags & TRACE_GRAPH_PRINT_DURATION)
9005f3eb
FW
1420 seq_printf(s, " DURATION ");
1421 seq_printf(s, " FUNCTION CALLS\n");
decbec38
FW
1422
1423 /* 2nd line */
49ff5903 1424 seq_printf(s, "#");
d7a8d9e9 1425 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
9005f3eb 1426 seq_printf(s, " | ");
d7a8d9e9 1427 if (flags & TRACE_GRAPH_PRINT_CPU)
49ff5903 1428 seq_printf(s, " | ");
d7a8d9e9 1429 if (flags & TRACE_GRAPH_PRINT_PROC)
49ff5903
SR
1430 seq_printf(s, " | | ");
1431 if (lat)
199abfab 1432 seq_printf(s, "||||");
d7a8d9e9 1433 if (flags & TRACE_GRAPH_PRINT_DURATION)
9005f3eb
FW
1434 seq_printf(s, " | | ");
1435 seq_printf(s, " | | | |\n");
decbec38 1436}
9005f3eb 1437
ba1afef6 1438static void print_graph_headers(struct seq_file *s)
d7a8d9e9
JO
1439{
1440 print_graph_headers_flags(s, tracer_flags.val);
1441}
1442
0a772620
JO
1443void print_graph_headers_flags(struct seq_file *s, u32 flags)
1444{
1445 struct trace_iterator *iter = s->private;
1446
749230b0
JO
1447 if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
1448 return;
1449
0a772620
JO
1450 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
1451 /* print nothing if the buffers are empty */
1452 if (trace_empty(iter))
1453 return;
1454
1455 print_trace_header(s, iter);
321e68b0 1456 }
0a772620
JO
1457
1458 __print_graph_headers_flags(s, flags);
1459}
1460
62b915f1 1461void graph_trace_open(struct trace_iterator *iter)
9005f3eb 1462{
2fbcdb35 1463 /* pid and depth on the last trace processed */
be1eca39 1464 struct fgraph_data *data;
9005f3eb
FW
1465 int cpu;
1466
be1eca39
JO
1467 iter->private = NULL;
1468
1469 data = kzalloc(sizeof(*data), GFP_KERNEL);
2fbcdb35 1470 if (!data)
be1eca39
JO
1471 goto out_err;
1472
1473 data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1474 if (!data->cpu_data)
1475 goto out_err_free;
1476
1477 for_each_possible_cpu(cpu) {
1478 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1479 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1480 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
2bd16212
JO
1481 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1482
be1eca39
JO
1483 *pid = -1;
1484 *depth = 0;
1485 *ignore = 0;
2bd16212 1486 *depth_irq = -1;
be1eca39 1487 }
9005f3eb 1488
2fbcdb35 1489 iter->private = data;
be1eca39
JO
1490
1491 return;
1492
1493 out_err_free:
1494 kfree(data);
1495 out_err:
1496 pr_warning("function graph tracer: not enough memory\n");
9005f3eb
FW
1497}
1498
62b915f1 1499void graph_trace_close(struct trace_iterator *iter)
9005f3eb 1500{
be1eca39
JO
1501 struct fgraph_data *data = iter->private;
1502
1503 if (data) {
1504 free_percpu(data->cpu_data);
1505 kfree(data);
1506 }
9005f3eb
FW
1507}
1508
8c1a49ae
SRRH
1509static int
1510func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
b304d044
SR
1511{
1512 if (bit == TRACE_GRAPH_PRINT_IRQS)
1513 ftrace_graph_skip_irqs = !set;
1514
1515 return 0;
1516}
1517
a9a57763
SR
1518static struct trace_event_functions graph_functions = {
1519 .trace = print_graph_function_event,
1520};
1521
9106b693
JO
1522static struct trace_event graph_trace_entry_event = {
1523 .type = TRACE_GRAPH_ENT,
a9a57763 1524 .funcs = &graph_functions,
9106b693
JO
1525};
1526
1527static struct trace_event graph_trace_ret_event = {
1528 .type = TRACE_GRAPH_RET,
a9a57763 1529 .funcs = &graph_functions
9106b693
JO
1530};
1531
8f768993 1532static struct tracer graph_trace __tracer_data = {
ef18012b 1533 .name = "function_graph",
6508fa76 1534 .update_thresh = graph_trace_update_thresh,
9005f3eb 1535 .open = graph_trace_open,
be1eca39 1536 .pipe_open = graph_trace_open,
9005f3eb 1537 .close = graph_trace_close,
be1eca39 1538 .pipe_close = graph_trace_close,
ef18012b
SR
1539 .init = graph_trace_init,
1540 .reset = graph_trace_reset,
decbec38
FW
1541 .print_line = print_graph_function,
1542 .print_header = print_graph_headers,
fb52607a 1543 .flags = &tracer_flags,
b304d044 1544 .set_flag = func_graph_set_flag,
7447dce9
FW
1545#ifdef CONFIG_FTRACE_SELFTEST
1546 .selftest = trace_selftest_startup_function_graph,
1547#endif
fb52607a
FW
1548};
1549
8741db53
SR
1550
1551static ssize_t
1552graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1553 loff_t *ppos)
1554{
1555 unsigned long val;
1556 int ret;
1557
1558 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1559 if (ret)
1560 return ret;
1561
1562 max_depth = val;
1563
1564 *ppos += cnt;
1565
1566 return cnt;
1567}
1568
1569static ssize_t
1570graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1571 loff_t *ppos)
1572{
1573 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1574 int n;
1575
1576 n = sprintf(buf, "%d\n", max_depth);
1577
1578 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1579}
1580
1581static const struct file_operations graph_depth_fops = {
1582 .open = tracing_open_generic,
1583 .write = graph_depth_write,
1584 .read = graph_depth_read,
1585 .llseek = generic_file_llseek,
1586};
1587
1588static __init int init_graph_debugfs(void)
1589{
1590 struct dentry *d_tracer;
1591
1592 d_tracer = tracing_init_dentry();
1593 if (!d_tracer)
1594 return 0;
1595
1596 trace_create_file("max_graph_depth", 0644, d_tracer,
1597 NULL, &graph_depth_fops);
1598
1599 return 0;
1600}
1601fs_initcall(init_graph_debugfs);
1602
fb52607a
FW
1603static __init int init_graph_trace(void)
1604{
0c9e6f63
LJ
1605 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1606
9106b693
JO
1607 if (!register_ftrace_event(&graph_trace_entry_event)) {
1608 pr_warning("Warning: could not register graph trace events\n");
1609 return 1;
1610 }
1611
1612 if (!register_ftrace_event(&graph_trace_ret_event)) {
1613 pr_warning("Warning: could not register graph trace events\n");
1614 return 1;
1615 }
1616
fb52607a
FW
1617 return register_tracer(&graph_trace);
1618}
1619
6f415672 1620core_initcall(init_graph_trace);
This page took 0.314678 seconds and 5 git commands to generate.