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"
17 struct fgraph_cpu_data {
24 struct fgraph_cpu_data *cpu_data;
26 /* Place to preserve last processed entry. */
27 struct ftrace_graph_ent_entry ent;
28 struct ftrace_graph_ret_entry ret;
33 #define TRACE_GRAPH_INDENT 2
36 #define TRACE_GRAPH_PRINT_OVERRUN 0x1
37 #define TRACE_GRAPH_PRINT_CPU 0x2
38 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
39 #define TRACE_GRAPH_PRINT_PROC 0x8
40 #define TRACE_GRAPH_PRINT_DURATION 0x10
41 #define TRACE_GRAPH_PRINT_ABS_TIME 0X20
43 static struct tracer_opt trace_opts[] = {
44 /* Display overruns? (for self-debug purpose) */
45 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
47 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
48 /* Display Overhead ? */
49 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
50 /* Display proc name/pid */
51 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
52 /* Display duration of execution */
53 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
54 /* Display absolute time of an entry */
55 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
59 static struct tracer_flags tracer_flags = {
60 /* Don't display overruns and proc by default */
61 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
62 TRACE_GRAPH_PRINT_DURATION,
66 static struct trace_array *graph_array;
69 /* Add a function return address to the trace stack on thread info.*/
71 ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
72 unsigned long frame_pointer)
74 unsigned long long calltime;
77 if (!current->ret_stack)
81 * We must make sure the ret_stack is tested before we read
86 /* The return trace stack is full */
87 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
88 atomic_inc(¤t->trace_overrun);
92 calltime = trace_clock_local();
94 index = ++current->curr_ret_stack;
96 current->ret_stack[index].ret = ret;
97 current->ret_stack[index].func = func;
98 current->ret_stack[index].calltime = calltime;
99 current->ret_stack[index].subtime = 0;
100 current->ret_stack[index].fp = frame_pointer;
106 /* Retrieve a function return address to the trace stack on thread info.*/
108 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
109 unsigned long frame_pointer)
113 index = current->curr_ret_stack;
115 if (unlikely(index < 0)) {
118 /* Might as well panic, otherwise we have no where to go */
119 *ret = (unsigned long)panic;
123 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
125 * The arch may choose to record the frame pointer used
126 * and check it here to make sure that it is what we expect it
127 * to be. If gcc does not set the place holder of the return
128 * address in the frame pointer, and does a copy instead, then
129 * the function graph trace will fail. This test detects this
132 * Currently, x86_32 with optimize for size (-Os) makes the latest
135 if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
137 WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
138 " from func %ps return to %lx\n",
139 current->ret_stack[index].fp,
141 (void *)current->ret_stack[index].func,
142 current->ret_stack[index].ret);
143 *ret = (unsigned long)panic;
148 *ret = current->ret_stack[index].ret;
149 trace->func = current->ret_stack[index].func;
150 trace->calltime = current->ret_stack[index].calltime;
151 trace->overrun = atomic_read(¤t->trace_overrun);
152 trace->depth = index;
156 * Send the trace to the ring-buffer.
157 * @return the original return address.
159 unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
161 struct ftrace_graph_ret trace;
164 ftrace_pop_return_trace(&trace, &ret, frame_pointer);
165 trace.rettime = trace_clock_local();
166 ftrace_graph_return(&trace);
168 current->curr_ret_stack--;
170 if (unlikely(!ret)) {
173 /* Might as well panic. What else to do? */
174 ret = (unsigned long)panic;
180 static int __trace_graph_entry(struct trace_array *tr,
181 struct ftrace_graph_ent *trace,
185 struct ftrace_event_call *call = &event_funcgraph_entry;
186 struct ring_buffer_event *event;
187 struct ring_buffer *buffer = tr->buffer;
188 struct ftrace_graph_ent_entry *entry;
190 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
193 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
194 sizeof(*entry), flags, pc);
197 entry = ring_buffer_event_data(event);
198 entry->graph_ent = *trace;
199 if (!filter_current_check_discard(buffer, call, entry, event))
200 ring_buffer_unlock_commit(buffer, event);
205 int trace_graph_entry(struct ftrace_graph_ent *trace)
207 struct trace_array *tr = graph_array;
208 struct trace_array_cpu *data;
215 if (!ftrace_trace_task(current))
218 if (!ftrace_graph_addr(trace->func))
221 local_irq_save(flags);
222 cpu = raw_smp_processor_id();
223 data = tr->data[cpu];
224 disabled = atomic_inc_return(&data->disabled);
225 if (likely(disabled == 1)) {
226 pc = preempt_count();
227 ret = __trace_graph_entry(tr, trace, flags, pc);
231 /* Only do the atomic if it is not already set */
232 if (!test_tsk_trace_graph(current))
233 set_tsk_trace_graph(current);
235 atomic_dec(&data->disabled);
236 local_irq_restore(flags);
241 static void __trace_graph_return(struct trace_array *tr,
242 struct ftrace_graph_ret *trace,
246 struct ftrace_event_call *call = &event_funcgraph_exit;
247 struct ring_buffer_event *event;
248 struct ring_buffer *buffer = tr->buffer;
249 struct ftrace_graph_ret_entry *entry;
251 if (unlikely(__this_cpu_read(per_cpu_var(ftrace_cpu_disabled))))
254 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
255 sizeof(*entry), flags, pc);
258 entry = ring_buffer_event_data(event);
260 if (!filter_current_check_discard(buffer, call, entry, event))
261 ring_buffer_unlock_commit(buffer, event);
264 void trace_graph_return(struct ftrace_graph_ret *trace)
266 struct trace_array *tr = graph_array;
267 struct trace_array_cpu *data;
273 local_irq_save(flags);
274 cpu = raw_smp_processor_id();
275 data = tr->data[cpu];
276 disabled = atomic_inc_return(&data->disabled);
277 if (likely(disabled == 1)) {
278 pc = preempt_count();
279 __trace_graph_return(tr, trace, flags, pc);
282 clear_tsk_trace_graph(current);
283 atomic_dec(&data->disabled);
284 local_irq_restore(flags);
287 void set_graph_array(struct trace_array *tr)
291 /* Make graph_array visible before we start tracing */
296 static int graph_trace_init(struct trace_array *tr)
301 ret = register_ftrace_graph(&trace_graph_return,
305 tracing_start_cmdline_record();
310 static void graph_trace_reset(struct trace_array *tr)
312 tracing_stop_cmdline_record();
313 unregister_ftrace_graph();
316 static int max_bytes_for_cpu;
318 static enum print_line_t
319 print_graph_cpu(struct trace_seq *s, int cpu)
324 * Start with a space character - to make it stand out
325 * to the right a bit when trace output is pasted into
328 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
330 return TRACE_TYPE_PARTIAL_LINE;
332 return TRACE_TYPE_HANDLED;
335 #define TRACE_GRAPH_PROCINFO_LENGTH 14
337 static enum print_line_t
338 print_graph_proc(struct trace_seq *s, pid_t pid)
340 char comm[TASK_COMM_LEN];
341 /* sign + log10(MAX_INT) + '\0' */
348 trace_find_cmdline(pid, comm);
350 sprintf(pid_str, "%d", pid);
352 /* 1 stands for the "-" character */
353 len = strlen(comm) + strlen(pid_str) + 1;
355 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
356 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
358 /* First spaces to align center */
359 for (i = 0; i < spaces / 2; i++) {
360 ret = trace_seq_printf(s, " ");
362 return TRACE_TYPE_PARTIAL_LINE;
365 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
367 return TRACE_TYPE_PARTIAL_LINE;
369 /* Last spaces to align center */
370 for (i = 0; i < spaces - (spaces / 2); i++) {
371 ret = trace_seq_printf(s, " ");
373 return TRACE_TYPE_PARTIAL_LINE;
375 return TRACE_TYPE_HANDLED;
379 static enum print_line_t
380 print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
382 if (!trace_seq_putc(s, ' '))
385 return trace_print_lat_fmt(s, entry);
388 /* If the pid changed since the last trace, output this event */
389 static enum print_line_t
390 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
397 return TRACE_TYPE_HANDLED;
399 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
401 if (*last_pid == pid)
402 return TRACE_TYPE_HANDLED;
404 prev_pid = *last_pid;
408 return TRACE_TYPE_HANDLED;
410 * Context-switch trace line:
412 ------------------------------------------
413 | 1) migration/0--1 => sshd-1755
414 ------------------------------------------
417 ret = trace_seq_printf(s,
418 " ------------------------------------------\n");
420 return TRACE_TYPE_PARTIAL_LINE;
422 ret = print_graph_cpu(s, cpu);
423 if (ret == TRACE_TYPE_PARTIAL_LINE)
424 return TRACE_TYPE_PARTIAL_LINE;
426 ret = print_graph_proc(s, prev_pid);
427 if (ret == TRACE_TYPE_PARTIAL_LINE)
428 return TRACE_TYPE_PARTIAL_LINE;
430 ret = trace_seq_printf(s, " => ");
432 return TRACE_TYPE_PARTIAL_LINE;
434 ret = print_graph_proc(s, pid);
435 if (ret == TRACE_TYPE_PARTIAL_LINE)
436 return TRACE_TYPE_PARTIAL_LINE;
438 ret = trace_seq_printf(s,
439 "\n ------------------------------------------\n\n");
441 return TRACE_TYPE_PARTIAL_LINE;
443 return TRACE_TYPE_HANDLED;
446 static struct ftrace_graph_ret_entry *
447 get_return_for_leaf(struct trace_iterator *iter,
448 struct ftrace_graph_ent_entry *curr)
450 struct fgraph_data *data = iter->private;
451 struct ring_buffer_iter *ring_iter = NULL;
452 struct ring_buffer_event *event;
453 struct ftrace_graph_ret_entry *next;
456 * If the previous output failed to write to the seq buffer,
457 * then we just reuse the data from before.
459 if (data && data->failed) {
464 ring_iter = iter->buffer_iter[iter->cpu];
466 /* First peek to compare current entry and the next one */
468 event = ring_buffer_iter_peek(ring_iter, NULL);
471 * We need to consume the current entry to see
474 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
475 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
482 next = ring_buffer_event_data(event);
486 * Save current and next entries for later reference
487 * if the output fails.
494 if (next->ent.type != TRACE_GRAPH_RET)
497 if (curr->ent.pid != next->ent.pid ||
498 curr->graph_ent.func != next->ret.func)
501 /* this is a leaf, now advance the iterator */
503 ring_buffer_read(ring_iter, NULL);
508 /* Signal a overhead of time execution to the output */
510 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
512 /* If duration disappear, we don't need anything */
513 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
516 /* Non nested entry or return */
518 return trace_seq_printf(s, " ");
520 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
521 /* Duration exceeded 100 msecs */
522 if (duration > 100000ULL)
523 return trace_seq_printf(s, "! ");
525 /* Duration exceeded 10 msecs */
526 if (duration > 10000ULL)
527 return trace_seq_printf(s, "+ ");
530 return trace_seq_printf(s, " ");
533 static int print_graph_abs_time(u64 t, struct trace_seq *s)
535 unsigned long usecs_rem;
537 usecs_rem = do_div(t, NSEC_PER_SEC);
540 return trace_seq_printf(s, "%5lu.%06lu | ",
541 (unsigned long)t, usecs_rem);
544 static enum print_line_t
545 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
546 enum trace_type type, int cpu, pid_t pid)
549 struct trace_seq *s = &iter->seq;
551 if (addr < (unsigned long)__irqentry_text_start ||
552 addr >= (unsigned long)__irqentry_text_end)
553 return TRACE_TYPE_UNHANDLED;
556 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
557 ret = print_graph_abs_time(iter->ts, s);
559 return TRACE_TYPE_PARTIAL_LINE;
563 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
564 ret = print_graph_cpu(s, cpu);
565 if (ret == TRACE_TYPE_PARTIAL_LINE)
566 return TRACE_TYPE_PARTIAL_LINE;
570 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
571 ret = print_graph_proc(s, pid);
572 if (ret == TRACE_TYPE_PARTIAL_LINE)
573 return TRACE_TYPE_PARTIAL_LINE;
574 ret = trace_seq_printf(s, " | ");
576 return TRACE_TYPE_PARTIAL_LINE;
580 ret = print_graph_overhead(-1, s);
582 return TRACE_TYPE_PARTIAL_LINE;
584 if (type == TRACE_GRAPH_ENT)
585 ret = trace_seq_printf(s, "==========>");
587 ret = trace_seq_printf(s, "<==========");
590 return TRACE_TYPE_PARTIAL_LINE;
592 /* Don't close the duration column if haven't one */
593 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
594 trace_seq_printf(s, " |");
595 ret = trace_seq_printf(s, "\n");
598 return TRACE_TYPE_PARTIAL_LINE;
599 return TRACE_TYPE_HANDLED;
603 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
605 unsigned long nsecs_rem = do_div(duration, 1000);
606 /* log10(ULONG_MAX) + '\0' */
612 sprintf(msecs_str, "%lu", (unsigned long) duration);
615 ret = trace_seq_printf(s, "%s", msecs_str);
617 return TRACE_TYPE_PARTIAL_LINE;
619 len = strlen(msecs_str);
621 /* Print nsecs (we don't want to exceed 7 numbers) */
623 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
624 ret = trace_seq_printf(s, ".%s", nsecs_str);
626 return TRACE_TYPE_PARTIAL_LINE;
627 len += strlen(nsecs_str);
630 ret = trace_seq_printf(s, " us ");
632 return TRACE_TYPE_PARTIAL_LINE;
634 /* Print remaining spaces to fit the row's width */
635 for (i = len; i < 7; i++) {
636 ret = trace_seq_printf(s, " ");
638 return TRACE_TYPE_PARTIAL_LINE;
640 return TRACE_TYPE_HANDLED;
643 static enum print_line_t
644 print_graph_duration(unsigned long long duration, struct trace_seq *s)
648 ret = trace_print_graph_duration(duration, s);
649 if (ret != TRACE_TYPE_HANDLED)
652 ret = trace_seq_printf(s, "| ");
654 return TRACE_TYPE_PARTIAL_LINE;
656 return TRACE_TYPE_HANDLED;
659 /* Case of a leaf function on its call entry */
660 static enum print_line_t
661 print_graph_entry_leaf(struct trace_iterator *iter,
662 struct ftrace_graph_ent_entry *entry,
663 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
665 struct fgraph_data *data = iter->private;
666 struct ftrace_graph_ret *graph_ret;
667 struct ftrace_graph_ent *call;
668 unsigned long long duration;
672 graph_ret = &ret_entry->ret;
673 call = &entry->graph_ent;
674 duration = graph_ret->rettime - graph_ret->calltime;
678 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
681 * Comments display at + 1 to depth. Since
682 * this is a leaf function, keep the comments
683 * equal to this depth.
685 *depth = call->depth - 1;
689 ret = print_graph_overhead(duration, s);
691 return TRACE_TYPE_PARTIAL_LINE;
694 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
695 ret = print_graph_duration(duration, s);
696 if (ret == TRACE_TYPE_PARTIAL_LINE)
697 return TRACE_TYPE_PARTIAL_LINE;
701 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
702 ret = trace_seq_printf(s, " ");
704 return TRACE_TYPE_PARTIAL_LINE;
707 ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
709 return TRACE_TYPE_PARTIAL_LINE;
711 return TRACE_TYPE_HANDLED;
714 static enum print_line_t
715 print_graph_entry_nested(struct trace_iterator *iter,
716 struct ftrace_graph_ent_entry *entry,
717 struct trace_seq *s, int cpu)
719 struct ftrace_graph_ent *call = &entry->graph_ent;
720 struct fgraph_data *data = iter->private;
726 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
728 *depth = call->depth;
732 ret = print_graph_overhead(-1, s);
734 return TRACE_TYPE_PARTIAL_LINE;
737 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
738 ret = trace_seq_printf(s, " | ");
740 return TRACE_TYPE_PARTIAL_LINE;
744 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
745 ret = trace_seq_printf(s, " ");
747 return TRACE_TYPE_PARTIAL_LINE;
750 ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
752 return TRACE_TYPE_PARTIAL_LINE;
755 * we already consumed the current entry to check the next one
756 * and see if this is a leaf.
758 return TRACE_TYPE_NO_CONSUME;
761 static enum print_line_t
762 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
763 int type, unsigned long addr)
765 struct fgraph_data *data = iter->private;
766 struct trace_entry *ent = iter->ent;
771 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
772 return TRACE_TYPE_PARTIAL_LINE;
776 ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
777 if (ret == TRACE_TYPE_PARTIAL_LINE)
778 return TRACE_TYPE_PARTIAL_LINE;
782 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
783 ret = print_graph_abs_time(iter->ts, s);
785 return TRACE_TYPE_PARTIAL_LINE;
789 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
790 ret = print_graph_cpu(s, cpu);
791 if (ret == TRACE_TYPE_PARTIAL_LINE)
792 return TRACE_TYPE_PARTIAL_LINE;
796 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
797 ret = print_graph_proc(s, ent->pid);
798 if (ret == TRACE_TYPE_PARTIAL_LINE)
799 return TRACE_TYPE_PARTIAL_LINE;
801 ret = trace_seq_printf(s, " | ");
803 return TRACE_TYPE_PARTIAL_LINE;
807 if (trace_flags & TRACE_ITER_LATENCY_FMT) {
808 ret = print_graph_lat_fmt(s, ent);
809 if (ret == TRACE_TYPE_PARTIAL_LINE)
810 return TRACE_TYPE_PARTIAL_LINE;
816 static enum print_line_t
817 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
818 struct trace_iterator *iter)
820 struct fgraph_data *data = iter->private;
821 struct ftrace_graph_ent *call = &field->graph_ent;
822 struct ftrace_graph_ret_entry *leaf_ret;
823 static enum print_line_t ret;
826 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
827 return TRACE_TYPE_PARTIAL_LINE;
829 leaf_ret = get_return_for_leaf(iter, field);
831 ret = print_graph_entry_leaf(iter, field, leaf_ret, s);
833 ret = print_graph_entry_nested(iter, field, s, cpu);
837 * If we failed to write our output, then we need to make
838 * note of it. Because we already consumed our entry.
850 static enum print_line_t
851 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
852 struct trace_entry *ent, struct trace_iterator *iter)
854 unsigned long long duration = trace->rettime - trace->calltime;
855 struct fgraph_data *data = iter->private;
856 pid_t pid = ent->pid;
863 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
866 * Comments display at + 1 to depth. This is the
867 * return from a function, we now want the comments
868 * to display at the same level of the bracket.
870 *depth = trace->depth - 1;
873 if (print_graph_prologue(iter, s, 0, 0))
874 return TRACE_TYPE_PARTIAL_LINE;
877 ret = print_graph_overhead(duration, s);
879 return TRACE_TYPE_PARTIAL_LINE;
882 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
883 ret = print_graph_duration(duration, s);
884 if (ret == TRACE_TYPE_PARTIAL_LINE)
885 return TRACE_TYPE_PARTIAL_LINE;
889 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
890 ret = trace_seq_printf(s, " ");
892 return TRACE_TYPE_PARTIAL_LINE;
895 ret = trace_seq_printf(s, "}\n");
897 return TRACE_TYPE_PARTIAL_LINE;
900 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
901 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
904 return TRACE_TYPE_PARTIAL_LINE;
907 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
908 if (ret == TRACE_TYPE_PARTIAL_LINE)
909 return TRACE_TYPE_PARTIAL_LINE;
911 return TRACE_TYPE_HANDLED;
914 static enum print_line_t
915 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
916 struct trace_iterator *iter)
918 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
919 struct fgraph_data *data = iter->private;
920 struct trace_event *event;
926 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
928 if (print_graph_prologue(iter, s, 0, 0))
929 return TRACE_TYPE_PARTIAL_LINE;
932 ret = print_graph_overhead(-1, s);
934 return TRACE_TYPE_PARTIAL_LINE;
937 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
938 ret = trace_seq_printf(s, " | ");
940 return TRACE_TYPE_PARTIAL_LINE;
945 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
946 ret = trace_seq_printf(s, " ");
948 return TRACE_TYPE_PARTIAL_LINE;
952 ret = trace_seq_printf(s, "/* ");
954 return TRACE_TYPE_PARTIAL_LINE;
956 switch (iter->ent->type) {
958 ret = trace_print_bprintk_msg_only(iter);
959 if (ret != TRACE_TYPE_HANDLED)
963 ret = trace_print_printk_msg_only(iter);
964 if (ret != TRACE_TYPE_HANDLED)
968 event = ftrace_find_event(ent->type);
970 return TRACE_TYPE_UNHANDLED;
972 ret = event->trace(iter, sym_flags);
973 if (ret != TRACE_TYPE_HANDLED)
977 /* Strip ending newline */
978 if (s->buffer[s->len - 1] == '\n') {
979 s->buffer[s->len - 1] = '\0';
983 ret = trace_seq_printf(s, " */\n");
985 return TRACE_TYPE_PARTIAL_LINE;
987 return TRACE_TYPE_HANDLED;
992 print_graph_function(struct trace_iterator *iter)
994 struct ftrace_graph_ent_entry *field;
995 struct fgraph_data *data = iter->private;
996 struct trace_entry *entry = iter->ent;
997 struct trace_seq *s = &iter->seq;
1001 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1002 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1003 return TRACE_TYPE_HANDLED;
1007 * If the last output failed, there's a possibility we need
1008 * to print out the missing entry which would never go out.
1010 if (data && data->failed) {
1012 iter->cpu = data->cpu;
1013 ret = print_graph_entry(field, s, iter);
1014 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1015 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1016 ret = TRACE_TYPE_NO_CONSUME;
1022 switch (entry->type) {
1023 case TRACE_GRAPH_ENT: {
1025 * print_graph_entry() may consume the current event,
1026 * thus @field may become invalid, so we need to save it.
1027 * sizeof(struct ftrace_graph_ent_entry) is very small,
1028 * it can be safely saved at the stack.
1030 struct ftrace_graph_ent_entry saved;
1031 trace_assign_type(field, entry);
1033 return print_graph_entry(&saved, s, iter);
1035 case TRACE_GRAPH_RET: {
1036 struct ftrace_graph_ret_entry *field;
1037 trace_assign_type(field, entry);
1038 return print_graph_return(&field->ret, s, entry, iter);
1041 return print_graph_comment(s, entry, iter);
1044 return TRACE_TYPE_HANDLED;
1047 static void print_lat_header(struct seq_file *s)
1049 static const char spaces[] = " " /* 16 spaces */
1051 " "; /* 17 spaces */
1054 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1056 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1058 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1061 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1062 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1063 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1064 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
1065 seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces);
1066 seq_printf(s, "#%.*s|||| / \n", size, spaces);
1069 static void print_graph_headers(struct seq_file *s)
1071 int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
1074 print_lat_header(s);
1078 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1079 seq_printf(s, " TIME ");
1080 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1081 seq_printf(s, " CPU");
1082 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1083 seq_printf(s, " TASK/PID ");
1085 seq_printf(s, "|||||");
1086 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1087 seq_printf(s, " DURATION ");
1088 seq_printf(s, " FUNCTION CALLS\n");
1092 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
1093 seq_printf(s, " | ");
1094 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
1095 seq_printf(s, " | ");
1096 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
1097 seq_printf(s, " | | ");
1099 seq_printf(s, "|||||");
1100 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
1101 seq_printf(s, " | | ");
1102 seq_printf(s, " | | | |\n");
1105 static void graph_trace_open(struct trace_iterator *iter)
1107 /* pid and depth on the last trace processed */
1108 struct fgraph_data *data;
1111 iter->private = NULL;
1113 data = kzalloc(sizeof(*data), GFP_KERNEL);
1117 data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
1118 if (!data->cpu_data)
1121 for_each_possible_cpu(cpu) {
1122 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1123 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1124 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
1130 iter->private = data;
1137 pr_warning("function graph tracer: not enough memory\n");
1140 static void graph_trace_close(struct trace_iterator *iter)
1142 struct fgraph_data *data = iter->private;
1145 free_percpu(data->cpu_data);
1150 static struct tracer graph_trace __read_mostly = {
1151 .name = "function_graph",
1152 .open = graph_trace_open,
1153 .pipe_open = graph_trace_open,
1154 .close = graph_trace_close,
1155 .pipe_close = graph_trace_close,
1156 .wait_pipe = poll_wait_pipe,
1157 .init = graph_trace_init,
1158 .reset = graph_trace_reset,
1159 .print_line = print_graph_function,
1160 .print_header = print_graph_headers,
1161 .flags = &tracer_flags,
1162 #ifdef CONFIG_FTRACE_SELFTEST
1163 .selftest = trace_selftest_startup_function_graph,
1167 static __init int init_graph_trace(void)
1169 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1171 return register_tracer(&graph_trace);
1174 device_initcall(init_graph_trace);