3 * Function graph tracer.
4 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
5 * Mostly borrowed from function tracer which
6 * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
15 #include "trace_output.h"
22 #define TRACE_GRAPH_INDENT 2
25 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
26 #define TRACE_GRAPH_PRINT_CPU 0x2
27 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
28 #define TRACE_GRAPH_PRINT_PROC 0x8
29 #define TRACE_GRAPH_PRINT_DURATION 0x10
30 #define TRACE_GRAPH_PRINT_ABS_TIME 0X20
32 static struct tracer_opt trace_opts[] = {
33 /* Display overruns? (for self-debug purpose) */
34 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
36 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
37 /* Display Overhead ? */
38 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
39 /* Display proc name/pid */
40 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
41 /* Display duration of execution */
42 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
43 /* Display absolute time of an entry */
44 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
48 static struct tracer_flags tracer_flags = {
49 /* Don't display overruns and proc by default */
50 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
51 TRACE_GRAPH_PRINT_DURATION,
55 static struct trace_array *graph_array;
58 /* Add a function return address to the trace stack on thread info.*/
60 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
61 unsigned long frame_pointer)
63 unsigned long long calltime;
66 if (!current->ret_stack)
70 * We must make sure the ret_stack is tested before we read
75 /* The return trace stack is full */
76 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
77 atomic_inc(¤t->trace_overrun);
81 calltime = trace_clock_local();
83 index = ++current->curr_ret_stack;
85 current->ret_stack[index].ret = ret;
86 current->ret_stack[index].func = func;
87 current->ret_stack[index].calltime = calltime;
88 current->ret_stack[index].subtime = 0;
89 current->ret_stack[index].fp = frame_pointer;
95 /* Retrieve a function return address to the trace stack on thread info.*/
97 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
98 unsigned long frame_pointer)
102 index = current->curr_ret_stack;
104 if (unlikely(index < 0)) {
107 /* Might as well panic, otherwise we have no where to go */
108 *ret = (unsigned long)panic;
112 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
114 * The arch may choose to record the frame pointer used
115 * and check it here to make sure that it is what we expect it
116 * to be. If gcc does not set the place holder of the return
117 * address in the frame pointer, and does a copy instead, then
118 * the function graph trace will fail. This test detects this
121 * Currently, x86_32 with optimize for size (-Os) makes the latest
124 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
126 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
127 " from func %pF return to %lx\n",
128 current->ret_stack[index].fp,
130 (void *)current->ret_stack[index].func,
131 current->ret_stack[index].ret);
132 *ret = (unsigned long)panic;
137 *ret = current->ret_stack[index].ret;
138 trace->func = current->ret_stack[index].func;
139 trace->calltime = current->ret_stack[index].calltime;
140 trace->overrun = atomic_read(¤t->trace_overrun);
141 trace->depth = index;
145 * Send the trace to the ring-buffer.
146 * @return the original return address.
148 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
150 struct ftrace_graph_ret trace;
153 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
154 trace.rettime = trace_clock_local();
155 ftrace_graph_return(&trace);
157 current->curr_ret_stack--;
159 if (unlikely(!ret)) {
162 /* Might as well panic. What else to do? */
163 ret = (unsigned long)panic;
169 static int __trace_graph_entry(struct trace_array *tr,
170 struct ftrace_graph_ent *trace,
174 struct ftrace_event_call *call = &event_funcgraph_entry;
175 struct ring_buffer_event *event;
176 struct ring_buffer *buffer = tr->buffer;
177 struct ftrace_graph_ent_entry *entry;
179 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
182 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
183 sizeof(*entry), flags, pc);
186 entry = ring_buffer_event_data(event);
187 entry->graph_ent = *trace;
188 if (!filter_current_check_discard(buffer, call, entry, event))
189 ring_buffer_unlock_commit(buffer, event);
194 int trace_graph_entry(struct ftrace_graph_ent *trace)
196 struct trace_array *tr = graph_array;
197 struct trace_array_cpu *data;
207 if (!ftrace_trace_task(current))
210 if (!ftrace_graph_addr(trace->func))
213 local_irq_save(flags);
214 cpu = raw_smp_processor_id();
215 data = tr->data[cpu];
216 disabled = atomic_inc_return(&data->disabled);
217 if (likely(disabled == 1)) {
218 pc = preempt_count();
219 ret = __trace_graph_entry(tr, trace, flags, pc);
223 /* Only do the atomic if it is not already set */
224 if (!test_tsk_trace_graph(current))
225 set_tsk_trace_graph(current);
227 atomic_dec(&data->disabled);
228 local_irq_restore(flags);
233 static void __trace_graph_return(struct trace_array *tr,
234 struct ftrace_graph_ret *trace,
238 struct ftrace_event_call *call = &event_funcgraph_exit;
239 struct ring_buffer_event *event;
240 struct ring_buffer *buffer = tr->buffer;
241 struct ftrace_graph_ret_entry *entry;
243 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
246 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
247 sizeof(*entry), flags, pc);
250 entry = ring_buffer_event_data(event);
252 if (!filter_current_check_discard(buffer, call, entry, event))
253 ring_buffer_unlock_commit(buffer, event);
256 void trace_graph_return(struct ftrace_graph_ret *trace)
258 struct trace_array *tr = graph_array;
259 struct trace_array_cpu *data;
265 local_irq_save(flags);
266 cpu = raw_smp_processor_id();
267 data = tr->data[cpu];
268 disabled = atomic_inc_return(&data->disabled);
269 if (likely(disabled == 1)) {
270 pc = preempt_count();
271 __trace_graph_return(tr, trace, flags, pc);
274 clear_tsk_trace_graph(current);
275 atomic_dec(&data->disabled);
276 local_irq_restore(flags);
279 static int graph_trace_init(struct trace_array *tr)
284 ret = register_ftrace_graph(&trace_graph_return,
288 tracing_start_cmdline_record();
293 void set_graph_array(struct trace_array *tr)
298 static void graph_trace_reset(struct trace_array *tr)
300 tracing_stop_cmdline_record();
301 unregister_ftrace_graph();
304 static int max_bytes_for_cpu;
306 static enum print_line_t
307 print_graph_cpu(struct trace_seq *s, int cpu)
312 * Start with a space character - to make it stand out
313 * to the right a bit when trace output is pasted into
316 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
318 return TRACE_TYPE_PARTIAL_LINE;
320 return TRACE_TYPE_HANDLED;
323 #define TRACE_GRAPH_PROCINFO_LENGTH 14
325 static enum print_line_t
326 print_graph_proc(struct trace_seq *s, pid_t pid)
328 char comm[TASK_COMM_LEN];
329 /* sign + log10(MAX_INT) + '\0' */
336 trace_find_cmdline(pid, comm);
338 sprintf(pid_str, "%d", pid);
340 /* 1 stands for the "-" character */
341 len = strlen(comm) + strlen(pid_str) + 1;
343 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
344 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
346 /* First spaces to align center */
347 for (i = 0; i < spaces / 2; i++) {
348 ret = trace_seq_printf(s, " ");
350 return TRACE_TYPE_PARTIAL_LINE;
353 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
355 return TRACE_TYPE_PARTIAL_LINE;
357 /* Last spaces to align center */
358 for (i = 0; i < spaces - (spaces / 2); i++) {
359 ret = trace_seq_printf(s, " ");
361 return TRACE_TYPE_PARTIAL_LINE;
363 return TRACE_TYPE_HANDLED;
367 static enum print_line_t
368 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
370 int hardirq, softirq;
372 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
373 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
375 if (!trace_seq_printf(s, " %c%c%c",
376 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
377 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
379 (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
381 (hardirq && softirq) ? 'H' :
382 hardirq ? 'h' : softirq ? 's' : '.'))
385 if (entry->preempt_count)
386 return trace_seq_printf(s, "%x", entry->preempt_count);
387 return trace_seq_puts(s, ".");
390 /* If the pid changed since the last trace, output this event */
391 static enum print_line_t
392 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
399 return TRACE_TYPE_HANDLED;
401 last_pid = &(per_cpu_ptr(data, cpu)->last_pid);
403 if (*last_pid == pid)
404 return TRACE_TYPE_HANDLED;
406 prev_pid = *last_pid;
410 return TRACE_TYPE_HANDLED;
412 * Context-switch trace line:
414 ------------------------------------------
415 | 1) migration/0--1 => sshd-1755
416 ------------------------------------------
419 ret = trace_seq_printf(s,
420 " ------------------------------------------\n");
422 return TRACE_TYPE_PARTIAL_LINE;
424 ret = print_graph_cpu(s, cpu);
425 if (ret == TRACE_TYPE_PARTIAL_LINE)
426 return TRACE_TYPE_PARTIAL_LINE;
428 ret = print_graph_proc(s, prev_pid);
429 if (ret == TRACE_TYPE_PARTIAL_LINE)
430 return TRACE_TYPE_PARTIAL_LINE;
432 ret = trace_seq_printf(s, " => ");
434 return TRACE_TYPE_PARTIAL_LINE;
436 ret = print_graph_proc(s, pid);
437 if (ret == TRACE_TYPE_PARTIAL_LINE)
438 return TRACE_TYPE_PARTIAL_LINE;
440 ret = trace_seq_printf(s,
441 "\n ------------------------------------------\n\n");
443 return TRACE_TYPE_PARTIAL_LINE;
445 return TRACE_TYPE_HANDLED;
448 static struct ftrace_graph_ret_entry *
449 get_return_for_leaf(struct trace_iterator *iter,
450 struct ftrace_graph_ent_entry *curr)
452 struct ring_buffer_iter *ring_iter;
453 struct ring_buffer_event *event;
454 struct ftrace_graph_ret_entry *next;
456 ring_iter = iter->buffer_iter[iter->cpu];
458 /* First peek to compare current entry and the next one */
460 event = ring_buffer_iter_peek(ring_iter, NULL);
462 /* We need to consume the current entry to see the next one */
463 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
464 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
471 next = ring_buffer_event_data(event);
473 if (next->ent.type != TRACE_GRAPH_RET)
476 if (curr->ent.pid != next->ent.pid ||
477 curr->graph_ent.func != next->ret.func)
480 /* this is a leaf, now advance the iterator */
482 ring_buffer_read(ring_iter, NULL);
487 /* Signal a overhead of time execution to the output */
489 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
491 /* If duration disappear, we don't need anything */
492 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
495 /* Non nested entry or return */
497 return trace_seq_printf(s, " ");
499 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
500 /* Duration exceeded 100 msecs */
501 if (duration > 100000ULL)
502 return trace_seq_printf(s, "! ");
504 /* Duration exceeded 10 msecs */
505 if (duration > 10000ULL)
506 return trace_seq_printf(s, "+ ");
509 return trace_seq_printf(s, " ");
512 static int print_graph_abs_time(u64 t, struct trace_seq *s)
514 unsigned long usecs_rem;
516 usecs_rem = do_div(t, NSEC_PER_SEC);
519 return trace_seq_printf(s, "%5lu.%06lu | ",
520 (unsigned long)t, usecs_rem);
523 static enum print_line_t
524 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
525 enum trace_type type, int cpu, pid_t pid)
528 struct trace_seq *s = &iter->seq;
530 if (addr < (unsigned long)__irqentry_text_start ||
531 addr >= (unsigned long)__irqentry_text_end)
532 return TRACE_TYPE_UNHANDLED;
535 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
536 ret = print_graph_abs_time(iter->ts, s);
538 return TRACE_TYPE_PARTIAL_LINE;
542 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
543 ret = print_graph_cpu(s, cpu);
544 if (ret == TRACE_TYPE_PARTIAL_LINE)
545 return TRACE_TYPE_PARTIAL_LINE;
549 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
550 ret = print_graph_proc(s, pid);
551 if (ret == TRACE_TYPE_PARTIAL_LINE)
552 return TRACE_TYPE_PARTIAL_LINE;
553 ret = trace_seq_printf(s, " | ");
555 return TRACE_TYPE_PARTIAL_LINE;
559 ret = print_graph_overhead(-1, s);
561 return TRACE_TYPE_PARTIAL_LINE;
563 if (type == TRACE_GRAPH_ENT)
564 ret = trace_seq_printf(s, "==========>");
566 ret = trace_seq_printf(s, "<==========");
569 return TRACE_TYPE_PARTIAL_LINE;
571 /* Don't close the duration column if haven't one */
572 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
573 trace_seq_printf(s, " |");
574 ret = trace_seq_printf(s, "\n");
577 return TRACE_TYPE_PARTIAL_LINE;
578 return TRACE_TYPE_HANDLED;
582 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
584 unsigned long nsecs_rem = do_div(duration, 1000);
585 /* log10(ULONG_MAX) + '\0' */
591 sprintf(msecs_str, "%lu", (unsigned long) duration);
594 ret = trace_seq_printf(s, "%s", msecs_str);
596 return TRACE_TYPE_PARTIAL_LINE;
598 len = strlen(msecs_str);
600 /* Print nsecs (we don't want to exceed 7 numbers) */
602 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
603 ret = trace_seq_printf(s, ".%s", nsecs_str);
605 return TRACE_TYPE_PARTIAL_LINE;
606 len += strlen(nsecs_str);
609 ret = trace_seq_printf(s, " us ");
611 return TRACE_TYPE_PARTIAL_LINE;
613 /* Print remaining spaces to fit the row's width */
614 for (i = len; i < 7; i++) {
615 ret = trace_seq_printf(s, " ");
617 return TRACE_TYPE_PARTIAL_LINE;
619 return TRACE_TYPE_HANDLED;
622 static enum print_line_t
623 print_graph_duration(unsigned long long duration, struct trace_seq *s)
627 ret = trace_print_graph_duration(duration, s);
628 if (ret != TRACE_TYPE_HANDLED)
631 ret = trace_seq_printf(s, "| ");
633 return TRACE_TYPE_PARTIAL_LINE;
635 return TRACE_TYPE_HANDLED;
638 /* Case of a leaf function on its call entry */
639 static enum print_line_t
640 print_graph_entry_leaf(struct trace_iterator *iter,
641 struct ftrace_graph_ent_entry *entry,
642 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
644 struct fgraph_data *data = iter->private;
645 struct ftrace_graph_ret *graph_ret;
646 struct ftrace_graph_ent *call;
647 unsigned long long duration;
651 graph_ret = &ret_entry->ret;
652 call = &entry->graph_ent;
653 duration = graph_ret->rettime - graph_ret->calltime;
657 int *depth = &(per_cpu_ptr(data, cpu)->depth);
660 * Comments display at + 1 to depth. Since
661 * this is a leaf function, keep the comments
662 * equal to this depth.
664 *depth = call->depth - 1;
668 ret = print_graph_overhead(duration, s);
670 return TRACE_TYPE_PARTIAL_LINE;
673 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
674 ret = print_graph_duration(duration, s);
675 if (ret == TRACE_TYPE_PARTIAL_LINE)
676 return TRACE_TYPE_PARTIAL_LINE;
680 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
681 ret = trace_seq_printf(s, " ");
683 return TRACE_TYPE_PARTIAL_LINE;
686 ret = trace_seq_printf(s, "%pf();\n", (void *)call->func);
688 return TRACE_TYPE_PARTIAL_LINE;
690 return TRACE_TYPE_HANDLED;
693 static enum print_line_t
694 print_graph_entry_nested(struct trace_iterator *iter,
695 struct ftrace_graph_ent_entry *entry,
696 struct trace_seq *s, int cpu)
698 struct ftrace_graph_ent *call = &entry->graph_ent;
699 struct fgraph_data *data = iter->private;
705 int *depth = &(per_cpu_ptr(data, cpu)->depth);
707 *depth = call->depth;
711 ret = print_graph_overhead(-1, s);
713 return TRACE_TYPE_PARTIAL_LINE;
716 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
717 ret = trace_seq_printf(s, " | ");
719 return TRACE_TYPE_PARTIAL_LINE;
723 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
724 ret = trace_seq_printf(s, " ");
726 return TRACE_TYPE_PARTIAL_LINE;
729 ret = trace_seq_printf(s, "%pf() {\n", (void *)call->func);
731 return TRACE_TYPE_PARTIAL_LINE;
734 * we already consumed the current entry to check the next one
735 * and see if this is a leaf.
737 return TRACE_TYPE_NO_CONSUME;
740 static enum print_line_t
741 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
742 int type, unsigned long addr)
744 struct fgraph_data *data = iter->private;
745 struct trace_entry *ent = iter->ent;
750 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
751 return TRACE_TYPE_PARTIAL_LINE;
755 ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
756 if (ret == TRACE_TYPE_PARTIAL_LINE)
757 return TRACE_TYPE_PARTIAL_LINE;
761 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
762 ret = print_graph_abs_time(iter->ts, s);
764 return TRACE_TYPE_PARTIAL_LINE;
768 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
769 ret = print_graph_cpu(s, cpu);
770 if (ret == TRACE_TYPE_PARTIAL_LINE)
771 return TRACE_TYPE_PARTIAL_LINE;
775 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
776 ret = print_graph_proc(s, ent->pid);
777 if (ret == TRACE_TYPE_PARTIAL_LINE)
778 return TRACE_TYPE_PARTIAL_LINE;
780 ret = trace_seq_printf(s, " | ");
782 return TRACE_TYPE_PARTIAL_LINE;
786 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
787 ret = print_graph_lat_fmt(s, ent);
788 if (ret == TRACE_TYPE_PARTIAL_LINE)
789 return TRACE_TYPE_PARTIAL_LINE;
795 static enum print_line_t
796 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
797 struct trace_iterator *iter)
800 struct ftrace_graph_ent *call = &field->graph_ent;
801 struct ftrace_graph_ret_entry *leaf_ret;
803 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
804 return TRACE_TYPE_PARTIAL_LINE;
806 leaf_ret = get_return_for_leaf(iter, field);
808 return print_graph_entry_leaf(iter, field, leaf_ret, s);
810 return print_graph_entry_nested(iter, field, s, cpu);
814 static enum print_line_t
815 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
816 struct trace_entry *ent, struct trace_iterator *iter)
818 unsigned long long duration = trace->rettime - trace->calltime;
819 struct fgraph_data *data = iter->private;
820 pid_t pid = ent->pid;
827 int *depth = &(per_cpu_ptr(data, cpu)->depth);
830 * Comments display at + 1 to depth. This is the
831 * return from a function, we now want the comments
832 * to display at the same level of the bracket.
834 *depth = trace->depth - 1;
837 if (print_graph_prologue(iter, s, 0, 0))
838 return TRACE_TYPE_PARTIAL_LINE;
841 ret = print_graph_overhead(duration, s);
843 return TRACE_TYPE_PARTIAL_LINE;
846 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
847 ret = print_graph_duration(duration, s);
848 if (ret == TRACE_TYPE_PARTIAL_LINE)
849 return TRACE_TYPE_PARTIAL_LINE;
853 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
854 ret = trace_seq_printf(s, " ");
856 return TRACE_TYPE_PARTIAL_LINE;
859 ret = trace_seq_printf(s, "}\n");
861 return TRACE_TYPE_PARTIAL_LINE;
864 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
865 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
868 return TRACE_TYPE_PARTIAL_LINE;
871 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
872 if (ret == TRACE_TYPE_PARTIAL_LINE)
873 return TRACE_TYPE_PARTIAL_LINE;
875 return TRACE_TYPE_HANDLED;
878 static enum print_line_t
879 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
880 struct trace_iterator *iter)
882 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
883 struct fgraph_data *data = iter->private;
884 struct trace_event *event;
890 depth = per_cpu_ptr(data, iter->cpu)->depth;
892 if (print_graph_prologue(iter, s, 0, 0))
893 return TRACE_TYPE_PARTIAL_LINE;
896 ret = print_graph_overhead(-1, s);
898 return TRACE_TYPE_PARTIAL_LINE;
901 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
902 ret = trace_seq_printf(s, " | ");
904 return TRACE_TYPE_PARTIAL_LINE;
909 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
910 ret = trace_seq_printf(s, " ");
912 return TRACE_TYPE_PARTIAL_LINE;
916 ret = trace_seq_printf(s, "/* ");
918 return TRACE_TYPE_PARTIAL_LINE;
920 switch (iter->ent->type) {
922 ret = trace_print_bprintk_msg_only(iter);
923 if (ret != TRACE_TYPE_HANDLED)
927 ret = trace_print_printk_msg_only(iter);
928 if (ret != TRACE_TYPE_HANDLED)
932 event = ftrace_find_event(ent->type);
934 return TRACE_TYPE_UNHANDLED;
936 ret = event->trace(iter, sym_flags);
937 if (ret != TRACE_TYPE_HANDLED)
941 /* Strip ending newline */
942 if (s->buffer[s->len - 1] == '\n') {
943 s->buffer[s->len - 1] = '\0';
947 ret = trace_seq_printf(s, " */\n");
949 return TRACE_TYPE_PARTIAL_LINE;
951 return TRACE_TYPE_HANDLED;
956 print_graph_function(struct trace_iterator *iter)
958 struct trace_entry *entry = iter->ent;
959 struct trace_seq *s = &iter->seq;
961 switch (entry->type) {
962 case TRACE_GRAPH_ENT: {
964 * print_graph_entry() may consume the current event,
965 * thus @field may become invalid, so we need to save it.
966 * sizeof(struct ftrace_graph_ent_entry) is very small,
967 * it can be safely saved at the stack.
969 struct ftrace_graph_ent_entry *field, saved;
970 trace_assign_type(field, entry);
972 return print_graph_entry(&saved, s, iter);
974 case TRACE_GRAPH_RET: {
975 struct ftrace_graph_ret_entry *field;
976 trace_assign_type(field, entry);
977 return print_graph_return(&field->ret, s, entry, iter);
980 return print_graph_comment(s, entry, iter);
983 return TRACE_TYPE_HANDLED;
986 static void print_lat_header(struct seq_file *s)
988 static const char spaces[] = " " /* 16 spaces */
993 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
995 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
997 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1000 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1001 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1002 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1003 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
1004 seq_printf(s, "#%.*s||| / \n", size, spaces);
1005 seq_printf(s, "#%.*s|||| \n", size, spaces);
1008 static void print_graph_headers(struct seq_file *s)
1010 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1013 print_lat_header(s);
1017 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1018 seq_printf(s, " TIME ");
1019 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1020 seq_printf(s, " CPU");
1021 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1022 seq_printf(s, " TASK/PID ");
1024 seq_printf(s, "||||");
1025 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1026 seq_printf(s, " DURATION ");
1027 seq_printf(s, " FUNCTION CALLS\n");
1031 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1032 seq_printf(s, " | ");
1033 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1034 seq_printf(s, " | ");
1035 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1036 seq_printf(s, " | | ");
1038 seq_printf(s, "||||");
1039 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1040 seq_printf(s, " | | ");
1041 seq_printf(s, " | | | |\n");
1044 static void graph_trace_open(struct trace_iterator *iter)
1046 /* pid and depth on the last trace processed */
1047 struct fgraph_data *data = alloc_percpu(struct fgraph_data);
1051 pr_warning("function graph tracer: not enough memory\n");
1053 for_each_possible_cpu(cpu) {
1054 pid_t *pid = &(per_cpu_ptr(data, cpu)->last_pid);
1055 int *depth = &(per_cpu_ptr(data, cpu)->depth);
1060 iter->private = data;
1063 static void graph_trace_close(struct trace_iterator *iter)
1065 free_percpu(iter->private);
1068 static struct tracer graph_trace __read_mostly = {
1069 .name = "function_graph",
1070 .open = graph_trace_open,
1071 .close = graph_trace_close,
1072 .wait_pipe = poll_wait_pipe,
1073 .init = graph_trace_init,
1074 .reset = graph_trace_reset,
1075 .print_line = print_graph_function,
1076 .print_header = print_graph_headers,
1077 .flags = &tracer_flags,
1078 #ifdef CONFIG_FTRACE_SELFTEST
1079 .selftest = trace_selftest_startup_function_graph,
1083 static __init int init_graph_trace(void)
1085 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1087 return register_tracer(&graph_trace);
1090 device_initcall(init_graph_trace);