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 ftrace_graph_ent_entry *entry;
178 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
181 event = trace_buffer_lock_reserve(tr, TRACE_GRAPH_ENT,
182 sizeof(*entry), flags, pc);
185 entry = ring_buffer_event_data(event);
186 entry->graph_ent = *trace;
187 if (!filter_current_check_discard(call, entry, event))
188 ring_buffer_unlock_commit(tr->buffer, event);
193 int trace_graph_entry(struct ftrace_graph_ent *trace)
195 struct trace_array *tr = graph_array;
196 struct trace_array_cpu *data;
206 if (!ftrace_trace_task(current))
209 if (!ftrace_graph_addr(trace->func))
212 local_irq_save(flags);
213 cpu = raw_smp_processor_id();
214 data = tr->data[cpu];
215 disabled = atomic_inc_return(&data->disabled);
216 if (likely(disabled == 1)) {
217 pc = preempt_count();
218 ret = __trace_graph_entry(tr, trace, flags, pc);
222 /* Only do the atomic if it is not already set */
223 if (!test_tsk_trace_graph(current))
224 set_tsk_trace_graph(current);
226 atomic_dec(&data->disabled);
227 local_irq_restore(flags);
232 static void __trace_graph_return(struct trace_array *tr,
233 struct ftrace_graph_ret *trace,
237 struct ftrace_event_call *call = &event_funcgraph_exit;
238 struct ring_buffer_event *event;
239 struct ftrace_graph_ret_entry *entry;
241 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
244 event = trace_buffer_lock_reserve(tr, TRACE_GRAPH_RET,
245 sizeof(*entry), flags, pc);
248 entry = ring_buffer_event_data(event);
250 if (!filter_current_check_discard(call, entry, event))
251 ring_buffer_unlock_commit(tr->buffer, event);
254 void trace_graph_return(struct ftrace_graph_ret *trace)
256 struct trace_array *tr = graph_array;
257 struct trace_array_cpu *data;
263 local_irq_save(flags);
264 cpu = raw_smp_processor_id();
265 data = tr->data[cpu];
266 disabled = atomic_inc_return(&data->disabled);
267 if (likely(disabled == 1)) {
268 pc = preempt_count();
269 __trace_graph_return(tr, trace, flags, pc);
272 clear_tsk_trace_graph(current);
273 atomic_dec(&data->disabled);
274 local_irq_restore(flags);
277 static int graph_trace_init(struct trace_array *tr)
282 ret = register_ftrace_graph(&trace_graph_return,
286 tracing_start_cmdline_record();
291 void set_graph_array(struct trace_array *tr)
296 static void graph_trace_reset(struct trace_array *tr)
298 tracing_stop_cmdline_record();
299 unregister_ftrace_graph();
302 static int max_bytes_for_cpu;
304 static enum print_line_t
305 print_graph_cpu(struct trace_seq *s, int cpu)
310 * Start with a space character - to make it stand out
311 * to the right a bit when trace output is pasted into
314 ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
316 return TRACE_TYPE_PARTIAL_LINE;
318 return TRACE_TYPE_HANDLED;
321 #define TRACE_GRAPH_PROCINFO_LENGTH 14
323 static enum print_line_t
324 print_graph_proc(struct trace_seq *s, pid_t pid)
326 char comm[TASK_COMM_LEN];
327 /* sign + log10(MAX_INT) + '\0' */
334 trace_find_cmdline(pid, comm);
336 sprintf(pid_str, "%d", pid);
338 /* 1 stands for the "-" character */
339 len = strlen(comm) + strlen(pid_str) + 1;
341 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
342 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
344 /* First spaces to align center */
345 for (i = 0; i < spaces / 2; i++) {
346 ret = trace_seq_printf(s, " ");
348 return TRACE_TYPE_PARTIAL_LINE;
351 ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
353 return TRACE_TYPE_PARTIAL_LINE;
355 /* Last spaces to align center */
356 for (i = 0; i < spaces - (spaces / 2); i++) {
357 ret = trace_seq_printf(s, " ");
359 return TRACE_TYPE_PARTIAL_LINE;
361 return TRACE_TYPE_HANDLED;
365 /* If the pid changed since the last trace, output this event */
366 static enum print_line_t
367 verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
374 return TRACE_TYPE_HANDLED;
376 last_pid = &(per_cpu_ptr(data, cpu)->last_pid);
378 if (*last_pid == pid)
379 return TRACE_TYPE_HANDLED;
381 prev_pid = *last_pid;
385 return TRACE_TYPE_HANDLED;
387 * Context-switch trace line:
389 ------------------------------------------
390 | 1) migration/0--1 => sshd-1755
391 ------------------------------------------
394 ret = trace_seq_printf(s,
395 " ------------------------------------------\n");
397 return TRACE_TYPE_PARTIAL_LINE;
399 ret = print_graph_cpu(s, cpu);
400 if (ret == TRACE_TYPE_PARTIAL_LINE)
401 return TRACE_TYPE_PARTIAL_LINE;
403 ret = print_graph_proc(s, prev_pid);
404 if (ret == TRACE_TYPE_PARTIAL_LINE)
405 return TRACE_TYPE_PARTIAL_LINE;
407 ret = trace_seq_printf(s, " => ");
409 return TRACE_TYPE_PARTIAL_LINE;
411 ret = print_graph_proc(s, pid);
412 if (ret == TRACE_TYPE_PARTIAL_LINE)
413 return TRACE_TYPE_PARTIAL_LINE;
415 ret = trace_seq_printf(s,
416 "\n ------------------------------------------\n\n");
418 return TRACE_TYPE_PARTIAL_LINE;
420 return TRACE_TYPE_HANDLED;
423 static struct ftrace_graph_ret_entry *
424 get_return_for_leaf(struct trace_iterator *iter,
425 struct ftrace_graph_ent_entry *curr)
427 struct ring_buffer_iter *ring_iter;
428 struct ring_buffer_event *event;
429 struct ftrace_graph_ret_entry *next;
431 ring_iter = iter->buffer_iter[iter->cpu];
433 /* First peek to compare current entry and the next one */
435 event = ring_buffer_iter_peek(ring_iter, NULL);
437 /* We need to consume the current entry to see the next one */
438 ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
439 event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
446 next = ring_buffer_event_data(event);
448 if (next->ent.type != TRACE_GRAPH_RET)
451 if (curr->ent.pid != next->ent.pid ||
452 curr->graph_ent.func != next->ret.func)
455 /* this is a leaf, now advance the iterator */
457 ring_buffer_read(ring_iter, NULL);
462 /* Signal a overhead of time execution to the output */
464 print_graph_overhead(unsigned long long duration, struct trace_seq *s)
466 /* If duration disappear, we don't need anything */
467 if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
470 /* Non nested entry or return */
472 return trace_seq_printf(s, " ");
474 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
475 /* Duration exceeded 100 msecs */
476 if (duration > 100000ULL)
477 return trace_seq_printf(s, "! ");
479 /* Duration exceeded 10 msecs */
480 if (duration > 10000ULL)
481 return trace_seq_printf(s, "+ ");
484 return trace_seq_printf(s, " ");
487 static int print_graph_abs_time(u64 t, struct trace_seq *s)
489 unsigned long usecs_rem;
491 usecs_rem = do_div(t, NSEC_PER_SEC);
494 return trace_seq_printf(s, "%5lu.%06lu | ",
495 (unsigned long)t, usecs_rem);
498 static enum print_line_t
499 print_graph_irq(struct trace_iterator *iter, unsigned long addr,
500 enum trace_type type, int cpu, pid_t pid)
503 struct trace_seq *s = &iter->seq;
505 if (addr < (unsigned long)__irqentry_text_start ||
506 addr >= (unsigned long)__irqentry_text_end)
507 return TRACE_TYPE_UNHANDLED;
510 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
511 ret = print_graph_abs_time(iter->ts, s);
513 return TRACE_TYPE_PARTIAL_LINE;
517 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
518 ret = print_graph_cpu(s, cpu);
519 if (ret == TRACE_TYPE_PARTIAL_LINE)
520 return TRACE_TYPE_PARTIAL_LINE;
523 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
524 ret = print_graph_proc(s, pid);
525 if (ret == TRACE_TYPE_PARTIAL_LINE)
526 return TRACE_TYPE_PARTIAL_LINE;
527 ret = trace_seq_printf(s, " | ");
529 return TRACE_TYPE_PARTIAL_LINE;
533 ret = print_graph_overhead(-1, s);
535 return TRACE_TYPE_PARTIAL_LINE;
537 if (type == TRACE_GRAPH_ENT)
538 ret = trace_seq_printf(s, "==========>");
540 ret = trace_seq_printf(s, "<==========");
543 return TRACE_TYPE_PARTIAL_LINE;
545 /* Don't close the duration column if haven't one */
546 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
547 trace_seq_printf(s, " |");
548 ret = trace_seq_printf(s, "\n");
551 return TRACE_TYPE_PARTIAL_LINE;
552 return TRACE_TYPE_HANDLED;
556 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
558 unsigned long nsecs_rem = do_div(duration, 1000);
559 /* log10(ULONG_MAX) + '\0' */
565 sprintf(msecs_str, "%lu", (unsigned long) duration);
568 ret = trace_seq_printf(s, "%s", msecs_str);
570 return TRACE_TYPE_PARTIAL_LINE;
572 len = strlen(msecs_str);
574 /* Print nsecs (we don't want to exceed 7 numbers) */
576 snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
577 ret = trace_seq_printf(s, ".%s", nsecs_str);
579 return TRACE_TYPE_PARTIAL_LINE;
580 len += strlen(nsecs_str);
583 ret = trace_seq_printf(s, " us ");
585 return TRACE_TYPE_PARTIAL_LINE;
587 /* Print remaining spaces to fit the row's width */
588 for (i = len; i < 7; i++) {
589 ret = trace_seq_printf(s, " ");
591 return TRACE_TYPE_PARTIAL_LINE;
593 return TRACE_TYPE_HANDLED;
596 static enum print_line_t
597 print_graph_duration(unsigned long long duration, struct trace_seq *s)
601 ret = trace_print_graph_duration(duration, s);
602 if (ret != TRACE_TYPE_HANDLED)
605 ret = trace_seq_printf(s, "| ");
607 return TRACE_TYPE_PARTIAL_LINE;
609 return TRACE_TYPE_HANDLED;
612 /* Case of a leaf function on its call entry */
613 static enum print_line_t
614 print_graph_entry_leaf(struct trace_iterator *iter,
615 struct ftrace_graph_ent_entry *entry,
616 struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
618 struct fgraph_data *data = iter->private;
619 struct ftrace_graph_ret *graph_ret;
620 struct ftrace_graph_ent *call;
621 unsigned long long duration;
625 graph_ret = &ret_entry->ret;
626 call = &entry->graph_ent;
627 duration = graph_ret->rettime - graph_ret->calltime;
631 int *depth = &(per_cpu_ptr(data, cpu)->depth);
634 * Comments display at + 1 to depth. Since
635 * this is a leaf function, keep the comments
636 * equal to this depth.
638 *depth = call->depth - 1;
642 ret = print_graph_overhead(duration, s);
644 return TRACE_TYPE_PARTIAL_LINE;
647 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
648 ret = print_graph_duration(duration, s);
649 if (ret == TRACE_TYPE_PARTIAL_LINE)
650 return TRACE_TYPE_PARTIAL_LINE;
654 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
655 ret = trace_seq_printf(s, " ");
657 return TRACE_TYPE_PARTIAL_LINE;
660 ret = trace_seq_printf(s, "%pf();\n", (void *)call->func);
662 return TRACE_TYPE_PARTIAL_LINE;
664 return TRACE_TYPE_HANDLED;
667 static enum print_line_t
668 print_graph_entry_nested(struct trace_iterator *iter,
669 struct ftrace_graph_ent_entry *entry,
670 struct trace_seq *s, int cpu)
672 struct ftrace_graph_ent *call = &entry->graph_ent;
673 struct fgraph_data *data = iter->private;
679 int *depth = &(per_cpu_ptr(data, cpu)->depth);
681 *depth = call->depth;
685 ret = print_graph_overhead(-1, s);
687 return TRACE_TYPE_PARTIAL_LINE;
690 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
691 ret = trace_seq_printf(s, " | ");
693 return TRACE_TYPE_PARTIAL_LINE;
697 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
698 ret = trace_seq_printf(s, " ");
700 return TRACE_TYPE_PARTIAL_LINE;
703 ret = trace_seq_printf(s, "%pf() {\n", (void *)call->func);
705 return TRACE_TYPE_PARTIAL_LINE;
708 * we already consumed the current entry to check the next one
709 * and see if this is a leaf.
711 return TRACE_TYPE_NO_CONSUME;
714 static enum print_line_t
715 print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
716 int type, unsigned long addr)
718 struct fgraph_data *data = iter->private;
719 struct trace_entry *ent = iter->ent;
724 if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
725 return TRACE_TYPE_PARTIAL_LINE;
729 ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
730 if (ret == TRACE_TYPE_PARTIAL_LINE)
731 return TRACE_TYPE_PARTIAL_LINE;
735 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
736 ret = print_graph_abs_time(iter->ts, s);
738 return TRACE_TYPE_PARTIAL_LINE;
742 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
743 ret = print_graph_cpu(s, cpu);
744 if (ret == TRACE_TYPE_PARTIAL_LINE)
745 return TRACE_TYPE_PARTIAL_LINE;
749 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
750 ret = print_graph_proc(s, ent->pid);
751 if (ret == TRACE_TYPE_PARTIAL_LINE)
752 return TRACE_TYPE_PARTIAL_LINE;
754 ret = trace_seq_printf(s, " | ");
756 return TRACE_TYPE_PARTIAL_LINE;
762 static enum print_line_t
763 print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
764 struct trace_iterator *iter)
767 struct ftrace_graph_ent *call = &field->graph_ent;
768 struct ftrace_graph_ret_entry *leaf_ret;
770 if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
771 return TRACE_TYPE_PARTIAL_LINE;
773 leaf_ret = get_return_for_leaf(iter, field);
775 return print_graph_entry_leaf(iter, field, leaf_ret, s);
777 return print_graph_entry_nested(iter, field, s, cpu);
781 static enum print_line_t
782 print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
783 struct trace_entry *ent, struct trace_iterator *iter)
785 unsigned long long duration = trace->rettime - trace->calltime;
786 struct fgraph_data *data = iter->private;
787 pid_t pid = ent->pid;
794 int *depth = &(per_cpu_ptr(data, cpu)->depth);
797 * Comments display at + 1 to depth. This is the
798 * return from a function, we now want the comments
799 * to display at the same level of the bracket.
801 *depth = trace->depth - 1;
804 if (print_graph_prologue(iter, s, 0, 0))
805 return TRACE_TYPE_PARTIAL_LINE;
808 ret = print_graph_overhead(duration, s);
810 return TRACE_TYPE_PARTIAL_LINE;
813 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
814 ret = print_graph_duration(duration, s);
815 if (ret == TRACE_TYPE_PARTIAL_LINE)
816 return TRACE_TYPE_PARTIAL_LINE;
820 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
821 ret = trace_seq_printf(s, " ");
823 return TRACE_TYPE_PARTIAL_LINE;
826 ret = trace_seq_printf(s, "}\n");
828 return TRACE_TYPE_PARTIAL_LINE;
831 if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
832 ret = trace_seq_printf(s, " (Overruns: %lu)\n",
835 return TRACE_TYPE_PARTIAL_LINE;
838 ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
839 if (ret == TRACE_TYPE_PARTIAL_LINE)
840 return TRACE_TYPE_PARTIAL_LINE;
842 return TRACE_TYPE_HANDLED;
845 static enum print_line_t
846 print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
847 struct trace_iterator *iter)
849 unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
850 struct fgraph_data *data = iter->private;
851 struct trace_event *event;
857 depth = per_cpu_ptr(data, iter->cpu)->depth;
859 if (print_graph_prologue(iter, s, 0, 0))
860 return TRACE_TYPE_PARTIAL_LINE;
863 ret = print_graph_overhead(-1, s);
865 return TRACE_TYPE_PARTIAL_LINE;
868 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
869 ret = trace_seq_printf(s, " | ");
871 return TRACE_TYPE_PARTIAL_LINE;
876 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
877 ret = trace_seq_printf(s, " ");
879 return TRACE_TYPE_PARTIAL_LINE;
883 ret = trace_seq_printf(s, "/* ");
885 return TRACE_TYPE_PARTIAL_LINE;
887 switch (iter->ent->type) {
889 ret = trace_print_bprintk_msg_only(iter);
890 if (ret != TRACE_TYPE_HANDLED)
894 ret = trace_print_printk_msg_only(iter);
895 if (ret != TRACE_TYPE_HANDLED)
899 event = ftrace_find_event(ent->type);
901 return TRACE_TYPE_UNHANDLED;
903 ret = event->trace(iter, sym_flags);
904 if (ret != TRACE_TYPE_HANDLED)
908 /* Strip ending newline */
909 if (s->buffer[s->len - 1] == '\n') {
910 s->buffer[s->len - 1] = '\0';
914 ret = trace_seq_printf(s, " */\n");
916 return TRACE_TYPE_PARTIAL_LINE;
918 return TRACE_TYPE_HANDLED;
923 print_graph_function(struct trace_iterator *iter)
925 struct trace_entry *entry = iter->ent;
926 struct trace_seq *s = &iter->seq;
928 switch (entry->type) {
929 case TRACE_GRAPH_ENT: {
931 * print_graph_entry() may consume the current event,
932 * thus @field may become invalid, so we need to save it.
933 * sizeof(struct ftrace_graph_ent_entry) is very small,
934 * it can be safely saved at the stack.
936 struct ftrace_graph_ent_entry *field, saved;
937 trace_assign_type(field, entry);
939 return print_graph_entry(&saved, s, iter);
941 case TRACE_GRAPH_RET: {
942 struct ftrace_graph_ret_entry *field;
943 trace_assign_type(field, entry);
944 return print_graph_return(&field->ret, s, entry, iter);
947 return print_graph_comment(s, entry, iter);
950 return TRACE_TYPE_HANDLED;
953 static void print_graph_headers(struct seq_file *s)
957 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
958 seq_printf(s, " TIME ");
959 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
960 seq_printf(s, "CPU");
961 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
962 seq_printf(s, " TASK/PID ");
963 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
964 seq_printf(s, " DURATION ");
965 seq_printf(s, " FUNCTION CALLS\n");
969 if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
970 seq_printf(s, " | ");
971 if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
973 if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
974 seq_printf(s, " | | ");
975 if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
976 seq_printf(s, " | | ");
977 seq_printf(s, " | | | |\n");
980 static void graph_trace_open(struct trace_iterator *iter)
982 /* pid and depth on the last trace processed */
983 struct fgraph_data *data = alloc_percpu(struct fgraph_data);
987 pr_warning("function graph tracer: not enough memory\n");
989 for_each_possible_cpu(cpu) {
990 pid_t *pid = &(per_cpu_ptr(data, cpu)->last_pid);
991 int *depth = &(per_cpu_ptr(data, cpu)->depth);
996 iter->private = data;
999 static void graph_trace_close(struct trace_iterator *iter)
1001 free_percpu(iter->private);
1004 static struct tracer graph_trace __read_mostly = {
1005 .name = "function_graph",
1006 .open = graph_trace_open,
1007 .close = graph_trace_close,
1008 .wait_pipe = poll_wait_pipe,
1009 .init = graph_trace_init,
1010 .reset = graph_trace_reset,
1011 .print_line = print_graph_function,
1012 .print_header = print_graph_headers,
1013 .flags = &tracer_flags,
1014 #ifdef CONFIG_FTRACE_SELFTEST
1015 .selftest = trace_selftest_startup_function_graph,
1019 static __init int init_graph_trace(void)
1021 max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
1023 return register_tracer(&graph_trace);
1026 device_initcall(init_graph_trace);