1 // SPDX-License-Identifier: GPL-2.0
4 * Function graph tracer.
6 * Mostly borrowed from function tracer which
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
17 #include "trace_output.h"
19 /* When set, irq functions will be ignored */
20 static int ftrace_graph_skip_irqs;
22 struct fgraph_cpu_data {
27 unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH];
31 struct fgraph_cpu_data __percpu *cpu_data;
33 /* Place to preserve last processed entry. */
34 struct ftrace_graph_ent_entry ent;
35 struct ftrace_graph_ret_entry ret;
40 #define TRACE_GRAPH_INDENT 2
42 unsigned int fgraph_max_depth;
44 static struct tracer_opt trace_opts[] = {
45 /* Display overruns? (for self-debug purpose) */
46 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
48 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
49 /* Display Overhead ? */
50 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
51 /* Display proc name/pid */
52 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
53 /* Display duration of execution */
54 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
55 /* Display absolute time of an entry */
56 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
57 /* Display interrupts */
58 { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
59 /* Display function name after trailing } */
60 { TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },
61 /* Include sleep time (scheduled out) between entry and return */
62 { TRACER_OPT(sleep-time, TRACE_GRAPH_SLEEP_TIME) },
64 #ifdef CONFIG_FUNCTION_PROFILER
65 /* Include time within nested functions */
66 { TRACER_OPT(graph-time, TRACE_GRAPH_GRAPH_TIME) },
72 static struct tracer_flags tracer_flags = {
73 /* Don't display overruns, proc, or tail by default */
74 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
75 TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS |
76 TRACE_GRAPH_SLEEP_TIME | TRACE_GRAPH_GRAPH_TIME,
80 static struct trace_array *graph_array;
83 * DURATION column is being also used to display IRQ signs,
84 * following values are used by print_graph_irq and others
85 * to fill in space into DURATION column.
88 FLAGS_FILL_FULL = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,
89 FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,
90 FLAGS_FILL_END = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,
94 print_graph_duration(struct trace_array *tr, unsigned long long duration,
95 struct trace_seq *s, u32 flags);
97 int __trace_graph_entry(struct trace_array *tr,
98 struct ftrace_graph_ent *trace,
102 struct trace_event_call *call = &event_funcgraph_entry;
103 struct ring_buffer_event *event;
104 struct ring_buffer *buffer = tr->trace_buffer.buffer;
105 struct ftrace_graph_ent_entry *entry;
107 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
108 sizeof(*entry), flags, pc);
111 entry = ring_buffer_event_data(event);
112 entry->graph_ent = *trace;
113 if (!call_filter_check_discard(call, entry, buffer, event))
114 trace_buffer_unlock_commit_nostack(buffer, event);
119 static inline int ftrace_graph_ignore_irqs(void)
121 if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
127 int trace_graph_entry(struct ftrace_graph_ent *trace)
129 struct trace_array *tr = graph_array;
130 struct trace_array_cpu *data;
137 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT))
140 if (ftrace_graph_notrace_addr(trace->func)) {
141 trace_recursion_set(TRACE_GRAPH_NOTRACE_BIT);
143 * Need to return 1 to have the return called
144 * that will clear the NOTRACE bit.
149 if (!ftrace_trace_task(tr))
152 if (ftrace_graph_ignore_func(trace))
155 if (ftrace_graph_ignore_irqs())
159 * Do not trace a function if it's filtered by set_graph_notrace.
160 * Make the index of ret stack negative to indicate that it should
161 * ignore further functions. But it needs its own ret stack entry
162 * to recover the original index in order to continue tracing after
163 * returning from the function.
165 if (ftrace_graph_notrace_addr(trace->func))
169 * Stop here if tracing_threshold is set. We only write function return
170 * events to the ring buffer.
175 local_irq_save(flags);
176 cpu = raw_smp_processor_id();
177 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
178 disabled = atomic_inc_return(&data->disabled);
179 if (likely(disabled == 1)) {
180 pc = preempt_count();
181 ret = __trace_graph_entry(tr, trace, flags, pc);
186 atomic_dec(&data->disabled);
187 local_irq_restore(flags);
193 __trace_graph_function(struct trace_array *tr,
194 unsigned long ip, unsigned long flags, int pc)
196 u64 time = trace_clock_local();
197 struct ftrace_graph_ent ent = {
201 struct ftrace_graph_ret ret = {
208 __trace_graph_entry(tr, &ent, flags, pc);
209 __trace_graph_return(tr, &ret, flags, pc);
213 trace_graph_function(struct trace_array *tr,
214 unsigned long ip, unsigned long parent_ip,
215 unsigned long flags, int pc)
217 __trace_graph_function(tr, ip, flags, pc);
220 void __trace_graph_return(struct trace_array *tr,
221 struct ftrace_graph_ret *trace,
225 struct trace_event_call *call = &event_funcgraph_exit;
226 struct ring_buffer_event *event;
227 struct ring_buffer *buffer = tr->trace_buffer.buffer;
228 struct ftrace_graph_ret_entry *entry;
230 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
231 sizeof(*entry), flags, pc);
234 entry = ring_buffer_event_data(event);
236 if (!call_filter_check_discard(call, entry, buffer, event))
237 trace_buffer_unlock_commit_nostack(buffer, event);
240 void trace_graph_return(struct ftrace_graph_ret *trace)
242 struct trace_array *tr = graph_array;
243 struct trace_array_cpu *data;
249 ftrace_graph_addr_finish(trace);
251 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
252 trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
256 local_irq_save(flags);
257 cpu = raw_smp_processor_id();
258 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
259 disabled = atomic_inc_return(&data->disabled);
260 if (likely(disabled == 1)) {
261 pc = preempt_count();
262 __trace_graph_return(tr, trace, flags, pc);
264 atomic_dec(&data->disabled);
265 local_irq_restore(flags);
268 void set_graph_array(struct trace_array *tr)
272 /* Make graph_array visible before we start tracing */
277 static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
279 ftrace_graph_addr_finish(trace);
281 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
282 trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
286 if (tracing_thresh &&
287 (trace->rettime - trace->calltime < tracing_thresh))
290 trace_graph_return(trace);
293 static struct fgraph_ops funcgraph_thresh_ops = {
294 .entryfunc = &trace_graph_entry,
295 .retfunc = &trace_graph_thresh_return,
298 static struct fgraph_ops funcgraph_ops = {
299 .entryfunc = &trace_graph_entry,
300 .retfunc = &trace_graph_return,
303 static int graph_trace_init(struct trace_array *tr)
309 ret = register_ftrace_graph(&funcgraph_thresh_ops);
311 ret = register_ftrace_graph(&funcgraph_ops);
314 tracing_start_cmdline_record();
319 static void graph_trace_reset(struct trace_array *tr)
321 tracing_stop_cmdline_record();
323 unregister_ftrace_graph(&funcgraph_thresh_ops);
325 unregister_ftrace_graph(&funcgraph_ops);
328 static int graph_trace_update_thresh(struct trace_array *tr)
330 graph_trace_reset(tr);
331 return graph_trace_init(tr);
334 static int max_bytes_for_cpu;
336 static void print_graph_cpu(struct trace_seq *s, int cpu)
339 * Start with a space character - to make it stand out
340 * to the right a bit when trace output is pasted into
343 trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
346 #define TRACE_GRAPH_PROCINFO_LENGTH 14
348 static void print_graph_proc(struct trace_seq *s, pid_t pid)
350 char comm[TASK_COMM_LEN];
351 /* sign + log10(MAX_INT) + '\0' */
357 trace_find_cmdline(pid, comm);
359 sprintf(pid_str, "%d", pid);
361 /* 1 stands for the "-" character */
362 len = strlen(comm) + strlen(pid_str) + 1;
364 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
365 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
367 /* First spaces to align center */
368 for (i = 0; i < spaces / 2; i++)
369 trace_seq_putc(s, ' ');
371 trace_seq_printf(s, "%s-%s", comm, pid_str);
373 /* Last spaces to align center */
374 for (i = 0; i < spaces - (spaces / 2); i++)
375 trace_seq_putc(s, ' ');
379 static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
381 trace_seq_putc(s, ' ');
382 trace_print_lat_fmt(s, entry);
383 trace_seq_puts(s, " | ");
386 /* If the pid changed since the last trace, output this event */
388 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
396 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
398 if (*last_pid == pid)
401 prev_pid = *last_pid;
407 * Context-switch trace line:
409 ------------------------------------------
410 | 1) migration/0--1 => sshd-1755
411 ------------------------------------------
414 trace_seq_puts(s, " ------------------------------------------\n");
415 print_graph_cpu(s, cpu);
416 print_graph_proc(s, prev_pid);
417 trace_seq_puts(s, " => ");
418 print_graph_proc(s, pid);
419 trace_seq_puts(s, "\n ------------------------------------------\n\n");
422 static struct ftrace_graph_ret_entry *
423 get_return_for_leaf(struct trace_iterator *iter,
424 struct ftrace_graph_ent_entry *curr)
426 struct fgraph_data *data = iter->private;
427 struct ring_buffer_iter *ring_iter = NULL;
428 struct ring_buffer_event *event;
429 struct ftrace_graph_ret_entry *next;
432 * If the previous output failed to write to the seq buffer,
433 * then we just reuse the data from before.
435 if (data && data->failed) {
440 ring_iter = trace_buffer_iter(iter, iter->cpu);
442 /* First peek to compare current entry and the next one */
444 event = ring_buffer_iter_peek(ring_iter, NULL);
447 * We need to consume the current entry to see
450 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
452 event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
459 next = ring_buffer_event_data(event);
463 * Save current and next entries for later reference
464 * if the output fails.
468 * If the next event is not a return type, then
469 * we only care about what type it is. Otherwise we can
470 * safely copy the entire event.
472 if (next->ent.type == TRACE_GRAPH_RET)
475 data->ret.ent.type = next->ent.type;
479 if (next->ent.type != TRACE_GRAPH_RET)
482 if (curr->ent.pid != next->ent.pid ||
483 curr->graph_ent.func != next->ret.func)
486 /* this is a leaf, now advance the iterator */
488 ring_buffer_read(ring_iter, NULL);
493 static void print_graph_abs_time(u64 t, struct trace_seq *s)
495 unsigned long usecs_rem;
497 usecs_rem = do_div(t, NSEC_PER_SEC);
500 trace_seq_printf(s, "%5lu.%06lu | ",
501 (unsigned long)t, usecs_rem);
505 print_graph_rel_time(struct trace_iterator *iter, struct trace_seq *s)
507 unsigned long long usecs;
509 usecs = iter->ts - iter->trace_buffer->time_start;
510 do_div(usecs, NSEC_PER_USEC);
512 trace_seq_printf(s, "%9llu us | ", usecs);
516 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
517 enum trace_type type, int cpu, pid_t pid, u32 flags)
519 struct trace_array *tr = iter->tr;
520 struct trace_seq *s = &iter->seq;
521 struct trace_entry *ent = iter->ent;
523 if (addr < (unsigned long)__irqentry_text_start ||
524 addr >= (unsigned long)__irqentry_text_end)
527 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
529 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
530 print_graph_abs_time(iter->ts, s);
533 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
534 print_graph_rel_time(iter, s);
537 if (flags & TRACE_GRAPH_PRINT_CPU)
538 print_graph_cpu(s, cpu);
541 if (flags & TRACE_GRAPH_PRINT_PROC) {
542 print_graph_proc(s, pid);
543 trace_seq_puts(s, " | ");
547 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
548 print_graph_lat_fmt(s, ent);
552 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START);
554 if (type == TRACE_GRAPH_ENT)
555 trace_seq_puts(s, "==========>");
557 trace_seq_puts(s, "<==========");
559 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END);
560 trace_seq_putc(s, '\n');
564 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
566 unsigned long nsecs_rem = do_div(duration, 1000);
567 /* log10(ULONG_MAX) + '\0' */
573 sprintf(usecs_str, "%lu", (unsigned long) duration);
576 trace_seq_printf(s, "%s", usecs_str);
578 len = strlen(usecs_str);
580 /* Print nsecs (we don't want to exceed 7 numbers) */
582 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
584 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
585 trace_seq_printf(s, ".%s", nsecs_str);
586 len += strlen(nsecs_str) + 1;
589 trace_seq_puts(s, " us ");
591 /* Print remaining spaces to fit the row's width */
592 for (i = len; i < 8; i++)
593 trace_seq_putc(s, ' ');
597 print_graph_duration(struct trace_array *tr, unsigned long long duration,
598 struct trace_seq *s, u32 flags)
600 if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
601 !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
604 /* No real adata, just filling the column with spaces */
605 switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
606 case FLAGS_FILL_FULL:
607 trace_seq_puts(s, " | ");
609 case FLAGS_FILL_START:
610 trace_seq_puts(s, " ");
613 trace_seq_puts(s, " |");
617 /* Signal a overhead of time execution to the output */
618 if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
619 trace_seq_printf(s, "%c ", trace_find_mark(duration));
621 trace_seq_puts(s, " ");
623 trace_print_graph_duration(duration, s);
624 trace_seq_puts(s, "| ");
627 /* Case of a leaf function on its call entry */
628 static enum print_line_t
629 print_graph_entry_leaf(struct trace_iterator *iter,
630 struct ftrace_graph_ent_entry *entry,
631 struct ftrace_graph_ret_entry *ret_entry,
632 struct trace_seq *s, u32 flags)
634 struct fgraph_data *data = iter->private;
635 struct trace_array *tr = iter->tr;
636 struct ftrace_graph_ret *graph_ret;
637 struct ftrace_graph_ent *call;
638 unsigned long long duration;
642 graph_ret = &ret_entry->ret;
643 call = &entry->graph_ent;
644 duration = graph_ret->rettime - graph_ret->calltime;
647 struct fgraph_cpu_data *cpu_data;
649 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
652 * Comments display at + 1 to depth. Since
653 * this is a leaf function, keep the comments
654 * equal to this depth.
656 cpu_data->depth = call->depth - 1;
658 /* No need to keep this function around for this depth */
659 if (call->depth < FTRACE_RETFUNC_DEPTH &&
660 !WARN_ON_ONCE(call->depth < 0))
661 cpu_data->enter_funcs[call->depth] = 0;
664 /* Overhead and duration */
665 print_graph_duration(tr, duration, s, flags);
668 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
669 trace_seq_putc(s, ' ');
671 trace_seq_printf(s, "%ps();\n", (void *)call->func);
673 print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET,
674 cpu, iter->ent->pid, flags);
676 return trace_handle_return(s);
679 static enum print_line_t
680 print_graph_entry_nested(struct trace_iterator *iter,
681 struct ftrace_graph_ent_entry *entry,
682 struct trace_seq *s, int cpu, u32 flags)
684 struct ftrace_graph_ent *call = &entry->graph_ent;
685 struct fgraph_data *data = iter->private;
686 struct trace_array *tr = iter->tr;
690 struct fgraph_cpu_data *cpu_data;
693 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
694 cpu_data->depth = call->depth;
696 /* Save this function pointer to see if the exit matches */
697 if (call->depth < FTRACE_RETFUNC_DEPTH &&
698 !WARN_ON_ONCE(call->depth < 0))
699 cpu_data->enter_funcs[call->depth] = call->func;
703 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
706 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
707 trace_seq_putc(s, ' ');
709 trace_seq_printf(s, "%ps() {\n", (void *)call->func);
711 if (trace_seq_has_overflowed(s))
712 return TRACE_TYPE_PARTIAL_LINE;
715 * we already consumed the current entry to check the next one
716 * and see if this is a leaf.
718 return TRACE_TYPE_NO_CONSUME;
722 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
723 int type, unsigned long addr, u32 flags)
725 struct fgraph_data *data = iter->private;
726 struct trace_entry *ent = iter->ent;
727 struct trace_array *tr = iter->tr;
731 verif_pid(s, ent->pid, cpu, data);
735 print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
737 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
741 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
742 print_graph_abs_time(iter->ts, s);
745 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
746 print_graph_rel_time(iter, s);
749 if (flags & TRACE_GRAPH_PRINT_CPU)
750 print_graph_cpu(s, cpu);
753 if (flags & TRACE_GRAPH_PRINT_PROC) {
754 print_graph_proc(s, ent->pid);
755 trace_seq_puts(s, " | ");
759 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
760 print_graph_lat_fmt(s, ent);
766 * Entry check for irq code
769 * - we are inside irq code
770 * - we just entered irq code
773 * - funcgraph-interrupts option is set
774 * - we are not inside irq code
777 check_irq_entry(struct trace_iterator *iter, u32 flags,
778 unsigned long addr, int depth)
782 struct fgraph_data *data = iter->private;
785 * If we are either displaying irqs, or we got called as
786 * a graph event and private data does not exist,
787 * then we bypass the irq check.
789 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
793 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
796 * We are inside the irq code
801 if ((addr < (unsigned long)__irqentry_text_start) ||
802 (addr >= (unsigned long)__irqentry_text_end))
806 * We are entering irq code.
813 * Return check for irq code
816 * - we are inside irq code
817 * - we just left irq code
820 * - funcgraph-interrupts option is set
821 * - we are not inside irq code
824 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
828 struct fgraph_data *data = iter->private;
831 * If we are either displaying irqs, or we got called as
832 * a graph event and private data does not exist,
833 * then we bypass the irq check.
835 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
839 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
842 * We are not inside the irq code.
844 if (*depth_irq == -1)
848 * We are inside the irq code, and this is returning entry.
849 * Let's not trace it and clear the entry depth, since
850 * we are out of irq code.
852 * This condition ensures that we 'leave the irq code' once
853 * we are out of the entry depth. Thus protecting us from
854 * the RETURN entry loss.
856 if (*depth_irq >= depth) {
862 * We are inside the irq code, and this is not the entry.
867 static enum print_line_t
868 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
869 struct trace_iterator *iter, u32 flags)
871 struct fgraph_data *data = iter->private;
872 struct ftrace_graph_ent *call = &field->graph_ent;
873 struct ftrace_graph_ret_entry *leaf_ret;
874 static enum print_line_t ret;
877 if (check_irq_entry(iter, flags, call->func, call->depth))
878 return TRACE_TYPE_HANDLED;
880 print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
882 leaf_ret = get_return_for_leaf(iter, field);
884 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
886 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
890 * If we failed to write our output, then we need to make
891 * note of it. Because we already consumed our entry.
903 static enum print_line_t
904 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
905 struct trace_entry *ent, struct trace_iterator *iter,
908 unsigned long long duration = trace->rettime - trace->calltime;
909 struct fgraph_data *data = iter->private;
910 struct trace_array *tr = iter->tr;
911 pid_t pid = ent->pid;
916 if (check_irq_return(iter, flags, trace->depth))
917 return TRACE_TYPE_HANDLED;
920 struct fgraph_cpu_data *cpu_data;
923 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
926 * Comments display at + 1 to depth. This is the
927 * return from a function, we now want the comments
928 * to display at the same level of the bracket.
930 cpu_data->depth = trace->depth - 1;
932 if (trace->depth < FTRACE_RETFUNC_DEPTH &&
933 !WARN_ON_ONCE(trace->depth < 0)) {
934 if (cpu_data->enter_funcs[trace->depth] != trace->func)
936 cpu_data->enter_funcs[trace->depth] = 0;
940 print_graph_prologue(iter, s, 0, 0, flags);
942 /* Overhead and duration */
943 print_graph_duration(tr, duration, s, flags);
946 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
947 trace_seq_putc(s, ' ');
950 * If the return function does not have a matching entry,
951 * then the entry was lost. Instead of just printing
952 * the '}' and letting the user guess what function this
953 * belongs to, write out the function name. Always do
954 * that if the funcgraph-tail option is enabled.
956 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
957 trace_seq_puts(s, "}\n");
959 trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
962 if (flags & TRACE_GRAPH_PRINT_OVERRUN)
963 trace_seq_printf(s, " (Overruns: %lu)\n",
966 print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
969 return trace_handle_return(s);
972 static enum print_line_t
973 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
974 struct trace_iterator *iter, u32 flags)
976 struct trace_array *tr = iter->tr;
977 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
978 struct fgraph_data *data = iter->private;
979 struct trace_event *event;
985 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
987 print_graph_prologue(iter, s, 0, 0, flags);
990 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
994 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
995 trace_seq_putc(s, ' ');
998 trace_seq_puts(s, "/* ");
1000 switch (iter->ent->type) {
1002 ret = trace_print_bputs_msg_only(iter);
1003 if (ret != TRACE_TYPE_HANDLED)
1007 ret = trace_print_bprintk_msg_only(iter);
1008 if (ret != TRACE_TYPE_HANDLED)
1012 ret = trace_print_printk_msg_only(iter);
1013 if (ret != TRACE_TYPE_HANDLED)
1017 event = ftrace_find_event(ent->type);
1019 return TRACE_TYPE_UNHANDLED;
1021 ret = event->funcs->trace(iter, sym_flags, event);
1022 if (ret != TRACE_TYPE_HANDLED)
1026 if (trace_seq_has_overflowed(s))
1029 /* Strip ending newline */
1030 if (s->buffer[s->seq.len - 1] == '\n') {
1031 s->buffer[s->seq.len - 1] = '\0';
1035 trace_seq_puts(s, " */\n");
1037 return trace_handle_return(s);
1042 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1044 struct ftrace_graph_ent_entry *field;
1045 struct fgraph_data *data = iter->private;
1046 struct trace_entry *entry = iter->ent;
1047 struct trace_seq *s = &iter->seq;
1048 int cpu = iter->cpu;
1051 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1052 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1053 return TRACE_TYPE_HANDLED;
1057 * If the last output failed, there's a possibility we need
1058 * to print out the missing entry which would never go out.
1060 if (data && data->failed) {
1062 iter->cpu = data->cpu;
1063 ret = print_graph_entry(field, s, iter, flags);
1064 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1065 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1066 ret = TRACE_TYPE_NO_CONSUME;
1072 switch (entry->type) {
1073 case TRACE_GRAPH_ENT: {
1075 * print_graph_entry() may consume the current event,
1076 * thus @field may become invalid, so we need to save it.
1077 * sizeof(struct ftrace_graph_ent_entry) is very small,
1078 * it can be safely saved at the stack.
1080 struct ftrace_graph_ent_entry saved;
1081 trace_assign_type(field, entry);
1083 return print_graph_entry(&saved, s, iter, flags);
1085 case TRACE_GRAPH_RET: {
1086 struct ftrace_graph_ret_entry *field;
1087 trace_assign_type(field, entry);
1088 return print_graph_return(&field->ret, s, entry, iter, flags);
1092 /* dont trace stack and functions as comments */
1093 return TRACE_TYPE_UNHANDLED;
1096 return print_graph_comment(s, entry, iter, flags);
1099 return TRACE_TYPE_HANDLED;
1102 static enum print_line_t
1103 print_graph_function(struct trace_iterator *iter)
1105 return print_graph_function_flags(iter, tracer_flags.val);
1108 static enum print_line_t
1109 print_graph_function_event(struct trace_iterator *iter, int flags,
1110 struct trace_event *event)
1112 return print_graph_function(iter);
1115 static void print_lat_header(struct seq_file *s, u32 flags)
1117 static const char spaces[] = " " /* 16 spaces */
1119 " "; /* 17 spaces */
1122 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1124 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1126 if (flags & TRACE_GRAPH_PRINT_CPU)
1128 if (flags & TRACE_GRAPH_PRINT_PROC)
1131 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1132 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1133 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1134 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
1135 seq_printf(s, "#%.*s||| / \n", size, spaces);
1138 static void __print_graph_headers_flags(struct trace_array *tr,
1139 struct seq_file *s, u32 flags)
1141 int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
1144 print_lat_header(s, flags);
1148 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1149 seq_puts(s, " TIME ");
1150 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1151 seq_puts(s, " REL TIME ");
1152 if (flags & TRACE_GRAPH_PRINT_CPU)
1153 seq_puts(s, " CPU");
1154 if (flags & TRACE_GRAPH_PRINT_PROC)
1155 seq_puts(s, " TASK/PID ");
1157 seq_puts(s, "|||| ");
1158 if (flags & TRACE_GRAPH_PRINT_DURATION)
1159 seq_puts(s, " DURATION ");
1160 seq_puts(s, " FUNCTION CALLS\n");
1164 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1166 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1168 if (flags & TRACE_GRAPH_PRINT_CPU)
1170 if (flags & TRACE_GRAPH_PRINT_PROC)
1171 seq_puts(s, " | | ");
1173 seq_puts(s, "|||| ");
1174 if (flags & TRACE_GRAPH_PRINT_DURATION)
1175 seq_puts(s, " | | ");
1176 seq_puts(s, " | | | |\n");
1179 static void print_graph_headers(struct seq_file *s)
1181 print_graph_headers_flags(s, tracer_flags.val);
1184 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1186 struct trace_iterator *iter = s->private;
1187 struct trace_array *tr = iter->tr;
1189 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
1192 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
1193 /* print nothing if the buffers are empty */
1194 if (trace_empty(iter))
1197 print_trace_header(s, iter);
1200 __print_graph_headers_flags(tr, s, flags);
1203 void graph_trace_open(struct trace_iterator *iter)
1205 /* pid and depth on the last trace processed */
1206 struct fgraph_data *data;
1210 iter->private = NULL;
1212 /* We can be called in atomic context via ftrace_dump() */
1213 gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
1215 data = kzalloc(sizeof(*data), gfpflags);
1219 data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
1220 if (!data->cpu_data)
1223 for_each_possible_cpu(cpu) {
1224 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1225 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1226 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1227 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1235 iter->private = data;
1242 pr_warn("function graph tracer: not enough memory\n");
1245 void graph_trace_close(struct trace_iterator *iter)
1247 struct fgraph_data *data = iter->private;
1250 free_percpu(data->cpu_data);
1256 func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1258 if (bit == TRACE_GRAPH_PRINT_IRQS)
1259 ftrace_graph_skip_irqs = !set;
1261 if (bit == TRACE_GRAPH_SLEEP_TIME)
1262 ftrace_graph_sleep_time_control(set);
1264 if (bit == TRACE_GRAPH_GRAPH_TIME)
1265 ftrace_graph_graph_time_control(set);
1270 static struct trace_event_functions graph_functions = {
1271 .trace = print_graph_function_event,
1274 static struct trace_event graph_trace_entry_event = {
1275 .type = TRACE_GRAPH_ENT,
1276 .funcs = &graph_functions,
1279 static struct trace_event graph_trace_ret_event = {
1280 .type = TRACE_GRAPH_RET,
1281 .funcs = &graph_functions
1284 static struct tracer graph_trace __tracer_data = {
1285 .name = "function_graph",
1286 .update_thresh = graph_trace_update_thresh,
1287 .open = graph_trace_open,
1288 .pipe_open = graph_trace_open,
1289 .close = graph_trace_close,
1290 .pipe_close = graph_trace_close,
1291 .init = graph_trace_init,
1292 .reset = graph_trace_reset,
1293 .print_line = print_graph_function,
1294 .print_header = print_graph_headers,
1295 .flags = &tracer_flags,
1296 .set_flag = func_graph_set_flag,
1297 #ifdef CONFIG_FTRACE_SELFTEST
1298 .selftest = trace_selftest_startup_function_graph,
1304 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1310 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1314 fgraph_max_depth = val;
1322 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1325 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1328 n = sprintf(buf, "%d\n", fgraph_max_depth);
1330 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1333 static const struct file_operations graph_depth_fops = {
1334 .open = tracing_open_generic,
1335 .write = graph_depth_write,
1336 .read = graph_depth_read,
1337 .llseek = generic_file_llseek,
1340 static __init int init_graph_tracefs(void)
1342 struct dentry *d_tracer;
1344 d_tracer = tracing_init_dentry();
1345 if (IS_ERR(d_tracer))
1348 trace_create_file("max_graph_depth", 0644, d_tracer,
1349 NULL, &graph_depth_fops);
1353 fs_initcall(init_graph_tracefs);
1355 static __init int init_graph_trace(void)
1357 max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1);
1359 if (!register_trace_event(&graph_trace_entry_event)) {
1360 pr_warn("Warning: could not register graph trace events\n");
1364 if (!register_trace_event(&graph_trace_ret_event)) {
1365 pr_warn("Warning: could not register graph trace events\n");
1369 return register_tracer(&graph_trace);
1372 core_initcall(init_graph_trace);