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);
385 /* If the pid changed since the last trace, output this event */
387 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
395 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
397 if (*last_pid == pid)
400 prev_pid = *last_pid;
406 * Context-switch trace line:
408 ------------------------------------------
409 | 1) migration/0--1 => sshd-1755
410 ------------------------------------------
413 trace_seq_puts(s, " ------------------------------------------\n");
414 print_graph_cpu(s, cpu);
415 print_graph_proc(s, prev_pid);
416 trace_seq_puts(s, " => ");
417 print_graph_proc(s, pid);
418 trace_seq_puts(s, "\n ------------------------------------------\n\n");
421 static struct ftrace_graph_ret_entry *
422 get_return_for_leaf(struct trace_iterator *iter,
423 struct ftrace_graph_ent_entry *curr)
425 struct fgraph_data *data = iter->private;
426 struct ring_buffer_iter *ring_iter = NULL;
427 struct ring_buffer_event *event;
428 struct ftrace_graph_ret_entry *next;
431 * If the previous output failed to write to the seq buffer,
432 * then we just reuse the data from before.
434 if (data && data->failed) {
439 ring_iter = trace_buffer_iter(iter, iter->cpu);
441 /* First peek to compare current entry and the next one */
443 event = ring_buffer_iter_peek(ring_iter, NULL);
446 * We need to consume the current entry to see
449 ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
451 event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
458 next = ring_buffer_event_data(event);
462 * Save current and next entries for later reference
463 * if the output fails.
467 * If the next event is not a return type, then
468 * we only care about what type it is. Otherwise we can
469 * safely copy the entire event.
471 if (next->ent.type == TRACE_GRAPH_RET)
474 data->ret.ent.type = next->ent.type;
478 if (next->ent.type != TRACE_GRAPH_RET)
481 if (curr->ent.pid != next->ent.pid ||
482 curr->graph_ent.func != next->ret.func)
485 /* this is a leaf, now advance the iterator */
487 ring_buffer_read(ring_iter, NULL);
492 static void print_graph_abs_time(u64 t, struct trace_seq *s)
494 unsigned long usecs_rem;
496 usecs_rem = do_div(t, NSEC_PER_SEC);
499 trace_seq_printf(s, "%5lu.%06lu | ",
500 (unsigned long)t, usecs_rem);
504 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
505 enum trace_type type, int cpu, pid_t pid, u32 flags)
507 struct trace_array *tr = iter->tr;
508 struct trace_seq *s = &iter->seq;
509 struct trace_entry *ent = iter->ent;
511 if (addr < (unsigned long)__irqentry_text_start ||
512 addr >= (unsigned long)__irqentry_text_end)
515 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
517 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
518 print_graph_abs_time(iter->ts, s);
521 if (flags & TRACE_GRAPH_PRINT_CPU)
522 print_graph_cpu(s, cpu);
525 if (flags & TRACE_GRAPH_PRINT_PROC) {
526 print_graph_proc(s, pid);
527 trace_seq_puts(s, " | ");
531 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
532 print_graph_lat_fmt(s, ent);
536 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START);
538 if (type == TRACE_GRAPH_ENT)
539 trace_seq_puts(s, "==========>");
541 trace_seq_puts(s, "<==========");
543 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END);
544 trace_seq_putc(s, '\n');
548 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
550 unsigned long nsecs_rem = do_div(duration, 1000);
551 /* log10(ULONG_MAX) + '\0' */
557 sprintf(usecs_str, "%lu", (unsigned long) duration);
560 trace_seq_printf(s, "%s", usecs_str);
562 len = strlen(usecs_str);
564 /* Print nsecs (we don't want to exceed 7 numbers) */
566 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
568 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
569 trace_seq_printf(s, ".%s", nsecs_str);
570 len += strlen(nsecs_str) + 1;
573 trace_seq_puts(s, " us ");
575 /* Print remaining spaces to fit the row's width */
576 for (i = len; i < 8; i++)
577 trace_seq_putc(s, ' ');
581 print_graph_duration(struct trace_array *tr, unsigned long long duration,
582 struct trace_seq *s, u32 flags)
584 if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
585 !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
588 /* No real adata, just filling the column with spaces */
589 switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
590 case FLAGS_FILL_FULL:
591 trace_seq_puts(s, " | ");
593 case FLAGS_FILL_START:
594 trace_seq_puts(s, " ");
597 trace_seq_puts(s, " |");
601 /* Signal a overhead of time execution to the output */
602 if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
603 trace_seq_printf(s, "%c ", trace_find_mark(duration));
605 trace_seq_puts(s, " ");
607 trace_print_graph_duration(duration, s);
608 trace_seq_puts(s, "| ");
611 /* Case of a leaf function on its call entry */
612 static enum print_line_t
613 print_graph_entry_leaf(struct trace_iterator *iter,
614 struct ftrace_graph_ent_entry *entry,
615 struct ftrace_graph_ret_entry *ret_entry,
616 struct trace_seq *s, u32 flags)
618 struct fgraph_data *data = iter->private;
619 struct trace_array *tr = iter->tr;
620 struct ftrace_graph_ret *graph_ret;
621 struct ftrace_graph_ent *call;
622 unsigned long long duration;
626 graph_ret = &ret_entry->ret;
627 call = &entry->graph_ent;
628 duration = graph_ret->rettime - graph_ret->calltime;
631 struct fgraph_cpu_data *cpu_data;
633 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
636 * Comments display at + 1 to depth. Since
637 * this is a leaf function, keep the comments
638 * equal to this depth.
640 cpu_data->depth = call->depth - 1;
642 /* No need to keep this function around for this depth */
643 if (call->depth < FTRACE_RETFUNC_DEPTH &&
644 !WARN_ON_ONCE(call->depth < 0))
645 cpu_data->enter_funcs[call->depth] = 0;
648 /* Overhead and duration */
649 print_graph_duration(tr, duration, s, flags);
652 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
653 trace_seq_putc(s, ' ');
655 trace_seq_printf(s, "%ps();\n", (void *)call->func);
657 print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET,
658 cpu, iter->ent->pid, flags);
660 return trace_handle_return(s);
663 static enum print_line_t
664 print_graph_entry_nested(struct trace_iterator *iter,
665 struct ftrace_graph_ent_entry *entry,
666 struct trace_seq *s, int cpu, u32 flags)
668 struct ftrace_graph_ent *call = &entry->graph_ent;
669 struct fgraph_data *data = iter->private;
670 struct trace_array *tr = iter->tr;
674 struct fgraph_cpu_data *cpu_data;
677 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
678 cpu_data->depth = call->depth;
680 /* Save this function pointer to see if the exit matches */
681 if (call->depth < FTRACE_RETFUNC_DEPTH &&
682 !WARN_ON_ONCE(call->depth < 0))
683 cpu_data->enter_funcs[call->depth] = call->func;
687 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
690 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
691 trace_seq_putc(s, ' ');
693 trace_seq_printf(s, "%ps() {\n", (void *)call->func);
695 if (trace_seq_has_overflowed(s))
696 return TRACE_TYPE_PARTIAL_LINE;
699 * we already consumed the current entry to check the next one
700 * and see if this is a leaf.
702 return TRACE_TYPE_NO_CONSUME;
706 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
707 int type, unsigned long addr, u32 flags)
709 struct fgraph_data *data = iter->private;
710 struct trace_entry *ent = iter->ent;
711 struct trace_array *tr = iter->tr;
715 verif_pid(s, ent->pid, cpu, data);
719 print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
721 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
725 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
726 print_graph_abs_time(iter->ts, s);
729 if (flags & TRACE_GRAPH_PRINT_CPU)
730 print_graph_cpu(s, cpu);
733 if (flags & TRACE_GRAPH_PRINT_PROC) {
734 print_graph_proc(s, ent->pid);
735 trace_seq_puts(s, " | ");
739 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
740 print_graph_lat_fmt(s, ent);
746 * Entry check for irq code
749 * - we are inside irq code
750 * - we just entered irq code
753 * - funcgraph-interrupts option is set
754 * - we are not inside irq code
757 check_irq_entry(struct trace_iterator *iter, u32 flags,
758 unsigned long addr, int depth)
762 struct fgraph_data *data = iter->private;
765 * If we are either displaying irqs, or we got called as
766 * a graph event and private data does not exist,
767 * then we bypass the irq check.
769 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
773 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
776 * We are inside the irq code
781 if ((addr < (unsigned long)__irqentry_text_start) ||
782 (addr >= (unsigned long)__irqentry_text_end))
786 * We are entering irq code.
793 * Return check for irq code
796 * - we are inside irq code
797 * - we just left irq code
800 * - funcgraph-interrupts option is set
801 * - we are not inside irq code
804 check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
808 struct fgraph_data *data = iter->private;
811 * If we are either displaying irqs, or we got called as
812 * a graph event and private data does not exist,
813 * then we bypass the irq check.
815 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
819 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
822 * We are not inside the irq code.
824 if (*depth_irq == -1)
828 * We are inside the irq code, and this is returning entry.
829 * Let's not trace it and clear the entry depth, since
830 * we are out of irq code.
832 * This condition ensures that we 'leave the irq code' once
833 * we are out of the entry depth. Thus protecting us from
834 * the RETURN entry loss.
836 if (*depth_irq >= depth) {
842 * We are inside the irq code, and this is not the entry.
847 static enum print_line_t
848 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
849 struct trace_iterator *iter, u32 flags)
851 struct fgraph_data *data = iter->private;
852 struct ftrace_graph_ent *call = &field->graph_ent;
853 struct ftrace_graph_ret_entry *leaf_ret;
854 static enum print_line_t ret;
857 if (check_irq_entry(iter, flags, call->func, call->depth))
858 return TRACE_TYPE_HANDLED;
860 print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
862 leaf_ret = get_return_for_leaf(iter, field);
864 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
866 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
870 * If we failed to write our output, then we need to make
871 * note of it. Because we already consumed our entry.
883 static enum print_line_t
884 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
885 struct trace_entry *ent, struct trace_iterator *iter,
888 unsigned long long duration = trace->rettime - trace->calltime;
889 struct fgraph_data *data = iter->private;
890 struct trace_array *tr = iter->tr;
891 pid_t pid = ent->pid;
896 if (check_irq_return(iter, flags, trace->depth))
897 return TRACE_TYPE_HANDLED;
900 struct fgraph_cpu_data *cpu_data;
903 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
906 * Comments display at + 1 to depth. This is the
907 * return from a function, we now want the comments
908 * to display at the same level of the bracket.
910 cpu_data->depth = trace->depth - 1;
912 if (trace->depth < FTRACE_RETFUNC_DEPTH &&
913 !WARN_ON_ONCE(trace->depth < 0)) {
914 if (cpu_data->enter_funcs[trace->depth] != trace->func)
916 cpu_data->enter_funcs[trace->depth] = 0;
920 print_graph_prologue(iter, s, 0, 0, flags);
922 /* Overhead and duration */
923 print_graph_duration(tr, duration, s, flags);
926 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
927 trace_seq_putc(s, ' ');
930 * If the return function does not have a matching entry,
931 * then the entry was lost. Instead of just printing
932 * the '}' and letting the user guess what function this
933 * belongs to, write out the function name. Always do
934 * that if the funcgraph-tail option is enabled.
936 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
937 trace_seq_puts(s, "}\n");
939 trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
942 if (flags & TRACE_GRAPH_PRINT_OVERRUN)
943 trace_seq_printf(s, " (Overruns: %lu)\n",
946 print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
949 return trace_handle_return(s);
952 static enum print_line_t
953 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
954 struct trace_iterator *iter, u32 flags)
956 struct trace_array *tr = iter->tr;
957 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
958 struct fgraph_data *data = iter->private;
959 struct trace_event *event;
965 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
967 print_graph_prologue(iter, s, 0, 0, flags);
970 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
974 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
975 trace_seq_putc(s, ' ');
978 trace_seq_puts(s, "/* ");
980 switch (iter->ent->type) {
982 ret = trace_print_bputs_msg_only(iter);
983 if (ret != TRACE_TYPE_HANDLED)
987 ret = trace_print_bprintk_msg_only(iter);
988 if (ret != TRACE_TYPE_HANDLED)
992 ret = trace_print_printk_msg_only(iter);
993 if (ret != TRACE_TYPE_HANDLED)
997 event = ftrace_find_event(ent->type);
999 return TRACE_TYPE_UNHANDLED;
1001 ret = event->funcs->trace(iter, sym_flags, event);
1002 if (ret != TRACE_TYPE_HANDLED)
1006 if (trace_seq_has_overflowed(s))
1009 /* Strip ending newline */
1010 if (s->buffer[s->seq.len - 1] == '\n') {
1011 s->buffer[s->seq.len - 1] = '\0';
1015 trace_seq_puts(s, " */\n");
1017 return trace_handle_return(s);
1022 print_graph_function_flags(struct trace_iterator *iter, u32 flags)
1024 struct ftrace_graph_ent_entry *field;
1025 struct fgraph_data *data = iter->private;
1026 struct trace_entry *entry = iter->ent;
1027 struct trace_seq *s = &iter->seq;
1028 int cpu = iter->cpu;
1031 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1032 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1033 return TRACE_TYPE_HANDLED;
1037 * If the last output failed, there's a possibility we need
1038 * to print out the missing entry which would never go out.
1040 if (data && data->failed) {
1042 iter->cpu = data->cpu;
1043 ret = print_graph_entry(field, s, iter, flags);
1044 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1045 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1046 ret = TRACE_TYPE_NO_CONSUME;
1052 switch (entry->type) {
1053 case TRACE_GRAPH_ENT: {
1055 * print_graph_entry() may consume the current event,
1056 * thus @field may become invalid, so we need to save it.
1057 * sizeof(struct ftrace_graph_ent_entry) is very small,
1058 * it can be safely saved at the stack.
1060 struct ftrace_graph_ent_entry saved;
1061 trace_assign_type(field, entry);
1063 return print_graph_entry(&saved, s, iter, flags);
1065 case TRACE_GRAPH_RET: {
1066 struct ftrace_graph_ret_entry *field;
1067 trace_assign_type(field, entry);
1068 return print_graph_return(&field->ret, s, entry, iter, flags);
1072 /* dont trace stack and functions as comments */
1073 return TRACE_TYPE_UNHANDLED;
1076 return print_graph_comment(s, entry, iter, flags);
1079 return TRACE_TYPE_HANDLED;
1082 static enum print_line_t
1083 print_graph_function(struct trace_iterator *iter)
1085 return print_graph_function_flags(iter, tracer_flags.val);
1088 static enum print_line_t
1089 print_graph_function_event(struct trace_iterator *iter, int flags,
1090 struct trace_event *event)
1092 return print_graph_function(iter);
1095 static void print_lat_header(struct seq_file *s, u32 flags)
1097 static const char spaces[] = " " /* 16 spaces */
1099 " "; /* 17 spaces */
1102 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1104 if (flags & TRACE_GRAPH_PRINT_CPU)
1106 if (flags & TRACE_GRAPH_PRINT_PROC)
1109 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1110 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1111 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1112 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
1113 seq_printf(s, "#%.*s||| / \n", size, spaces);
1116 static void __print_graph_headers_flags(struct trace_array *tr,
1117 struct seq_file *s, u32 flags)
1119 int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
1122 print_lat_header(s, flags);
1126 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1127 seq_puts(s, " TIME ");
1128 if (flags & TRACE_GRAPH_PRINT_CPU)
1129 seq_puts(s, " CPU");
1130 if (flags & TRACE_GRAPH_PRINT_PROC)
1131 seq_puts(s, " TASK/PID ");
1133 seq_puts(s, "||||");
1134 if (flags & TRACE_GRAPH_PRINT_DURATION)
1135 seq_puts(s, " DURATION ");
1136 seq_puts(s, " FUNCTION CALLS\n");
1140 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
1142 if (flags & TRACE_GRAPH_PRINT_CPU)
1144 if (flags & TRACE_GRAPH_PRINT_PROC)
1145 seq_puts(s, " | | ");
1147 seq_puts(s, "||||");
1148 if (flags & TRACE_GRAPH_PRINT_DURATION)
1149 seq_puts(s, " | | ");
1150 seq_puts(s, " | | | |\n");
1153 static void print_graph_headers(struct seq_file *s)
1155 print_graph_headers_flags(s, tracer_flags.val);
1158 void print_graph_headers_flags(struct seq_file *s, u32 flags)
1160 struct trace_iterator *iter = s->private;
1161 struct trace_array *tr = iter->tr;
1163 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
1166 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
1167 /* print nothing if the buffers are empty */
1168 if (trace_empty(iter))
1171 print_trace_header(s, iter);
1174 __print_graph_headers_flags(tr, s, flags);
1177 void graph_trace_open(struct trace_iterator *iter)
1179 /* pid and depth on the last trace processed */
1180 struct fgraph_data *data;
1184 iter->private = NULL;
1186 /* We can be called in atomic context via ftrace_dump() */
1187 gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
1189 data = kzalloc(sizeof(*data), gfpflags);
1193 data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
1194 if (!data->cpu_data)
1197 for_each_possible_cpu(cpu) {
1198 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1199 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1200 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1201 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1209 iter->private = data;
1216 pr_warn("function graph tracer: not enough memory\n");
1219 void graph_trace_close(struct trace_iterator *iter)
1221 struct fgraph_data *data = iter->private;
1224 free_percpu(data->cpu_data);
1230 func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1232 if (bit == TRACE_GRAPH_PRINT_IRQS)
1233 ftrace_graph_skip_irqs = !set;
1235 if (bit == TRACE_GRAPH_SLEEP_TIME)
1236 ftrace_graph_sleep_time_control(set);
1238 if (bit == TRACE_GRAPH_GRAPH_TIME)
1239 ftrace_graph_graph_time_control(set);
1244 static struct trace_event_functions graph_functions = {
1245 .trace = print_graph_function_event,
1248 static struct trace_event graph_trace_entry_event = {
1249 .type = TRACE_GRAPH_ENT,
1250 .funcs = &graph_functions,
1253 static struct trace_event graph_trace_ret_event = {
1254 .type = TRACE_GRAPH_RET,
1255 .funcs = &graph_functions
1258 static struct tracer graph_trace __tracer_data = {
1259 .name = "function_graph",
1260 .update_thresh = graph_trace_update_thresh,
1261 .open = graph_trace_open,
1262 .pipe_open = graph_trace_open,
1263 .close = graph_trace_close,
1264 .pipe_close = graph_trace_close,
1265 .init = graph_trace_init,
1266 .reset = graph_trace_reset,
1267 .print_line = print_graph_function,
1268 .print_header = print_graph_headers,
1269 .flags = &tracer_flags,
1270 .set_flag = func_graph_set_flag,
1271 #ifdef CONFIG_FTRACE_SELFTEST
1272 .selftest = trace_selftest_startup_function_graph,
1278 graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1284 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1288 fgraph_max_depth = val;
1296 graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1299 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1302 n = sprintf(buf, "%d\n", fgraph_max_depth);
1304 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1307 static const struct file_operations graph_depth_fops = {
1308 .open = tracing_open_generic,
1309 .write = graph_depth_write,
1310 .read = graph_depth_read,
1311 .llseek = generic_file_llseek,
1314 static __init int init_graph_tracefs(void)
1316 struct dentry *d_tracer;
1318 d_tracer = tracing_init_dentry();
1319 if (IS_ERR(d_tracer))
1322 trace_create_file("max_graph_depth", 0644, d_tracer,
1323 NULL, &graph_depth_fops);
1327 fs_initcall(init_graph_tracefs);
1329 static __init int init_graph_trace(void)
1331 max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1);
1333 if (!register_trace_event(&graph_trace_entry_event)) {
1334 pr_warn("Warning: could not register graph trace events\n");
1338 if (!register_trace_event(&graph_trace_ret_event)) {
1339 pr_warn("Warning: could not register graph trace events\n");
1343 return register_tracer(&graph_trace);
1346 core_initcall(init_graph_trace);