Skip to contentSkip to conversation

Add function graph tracer support for ARM (resend)

PATCH In review Tim Bird 2 participants 2 messages Apr 23, 2009

Subsystems (2)
lore
Share & tools
lore

Tim Bird

author
#0 lore raw

PATCH 1/1 Add function graph tracer support for ARM (resend)

My first post of this wasn't wide enough. Now adding the linux-arm-kernel list and a few more CC:s. Sorry if you get this twice.

Add ftrace function-graph tracer support for ARM.

This includes adding code in mcount to check for (and call) a registered function graph trace entry routine, and adding infrastructure to the threadinfo structure to support a return trampoline.

I wrote a separate program to scan the kernel assembly output and validate that callframes for mcount are generated properly. (It's available upon request for anyone interested.)

Much of the code in arch/arm/kernel/ftrace.c was cloned from arch/x86/kernel/ftrace.c

IRQENTRY_TEXT was added to vmlinux.lds.S (to eliminate a compiler error on kernel/trace/trace_functions_graph.c), although no routines were marked as __irq_entry.

FIXTHIS indicates questions or areas of uncertainty.

This works for me with a gcc 4.1.1 compiler (EABI with frame pointers turned on) on an OMAP OSK board, with kernel 2.6.29.

The use of "already_here" works around trace entry nesting (I believe due to instrumentation of either cpu_clock or raw_smp_processor_id). Advice on the best way to deal with this would be appreciated. (that is, whether to use the guard variable, or just un-instrument the offending routines).

Thanks for any feedback.

Signed-off-by <[email protected]>
---
 arch/arm/Kconfig               |    1
 arch/arm/include/asm/ftrace.h  |   28 +++++++
 arch/arm/kernel/Makefile       |    3
 arch/arm/kernel/entry-common.S |   29 +++++++-
 arch/arm/kernel/ftrace.c       |  146 +++++++++++++++++++++++++++++++++++++++++
 arch/arm/kernel/vmlinux.lds.S  |    1
 6 files changed, 204 insertions(+), 4 deletions(-)
arch/arm/Kconfig +1 -0
@@ -17,6 +17,7 @@ config ARM
 	select HAVE_KPROBES if (!XIP_KERNEL)
 	select HAVE_KRETPROBES if (HAVE_KPROBES)
 	select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
+	select HAVE_FUNCTION_GRAPH_TRACER if (!XIP_KERNEL)
 	select HAVE_GENERIC_DMA_COHERENT
 	help
 	  The ARM series is a line of low-power-consumption RISC chip designs
arch/arm/include/asm/ftrace.h +26 -2
@@ -7,8 +7,32 @@
 
 #ifndef __ASSEMBLY__
 extern void mcount(void);
-#endif
+#endif /* __ASSEMBLY__ */
 
-#endif
+#endif /* CONFIG_FUNCTION_TRACER */
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+
+#ifndef __ASSEMBLY__
+
+/*
+ * Stack of return addresses for functions of a thread.
+ * Used in struct thread_info
+ */
+struct ftrace_ret_stack {
+	unsigned long ret;
+	unsigned long func;
+	unsigned long long calltime;
+};
+
+/*
+ * Primary handler of a function return.
+ * It relays on ftrace_return_to_handler.
+ * Defined in entry-common.S
+ */
+extern void return_to_handler(void);
+
+#endif /* __ASSEMBLY__ */
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
 
 #endif /* _ASM_ARM_FTRACE */
arch/arm/kernel/Makefile +2 -1
@@ -4,7 +4,7 @@
 
 AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
 
-ifdef CONFIG_DYNAMIC_FTRACE
+ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_ftrace.o = -pg
 endif
 
@@ -23,6 +23,7 @@ obj-$(CONFIG_ISA_DMA)		+= dma-isa.o
 obj-$(CONFIG_PCI)		+= bios32.o isa.o
 obj-$(CONFIG_SMP)		+= smp.o
 obj-$(CONFIG_DYNAMIC_FTRACE)	+= ftrace.o
+obj-$(CONFIG_FUNCTION_GRAPH_TRACER)	+= ftrace.o
 obj-$(CONFIG_KEXEC)		+= machine_kexec.o relocate_kernel.o
 obj-$(CONFIG_KPROBES)		+= kprobes.o kprobes-decode.o
 obj-$(CONFIG_ATAGS_PROC)	+= atags.o
arch/arm/kernel/entry-common.S +28 -1
@@ -135,8 +135,16 @@ ENTRY(mcount)
 	adr r0, ftrace_stub
 	cmp r0, r2
 	bne trace
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+	ldr r1, =ftrace_graph_return
+	ldr r2, [r1]
+	cmp r0, r2		@ if *ftrace_graph_return != ftrace_stub
+	bne ftrace_graph_caller
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+
 	ldr lr, [fp, #-4]			@ restore lr
-	ldmia sp!, {r0-r3, pc}
+	ldmia sp!, {r0-r3, pc}			@ return doing nothing
 
 trace:
 	ldr r1, [fp, #-4]			@ lr of instrumented routine
@@ -147,6 +155,25 @@ trace:
 	mov lr, r1				@ restore lr
 	ldmia sp!, {r0-r3, pc}
 
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+ENTRY(ftrace_graph_caller)
+	sub r0, fp, #4			@ &lr of instrumented routine (&parent)
+	mov r1, lr			@ instrumented routine (func)
+	sub r1, r1, #MCOUNT_INSN_SIZE
+	bl prepare_ftrace_return
+	ldr lr, [fp, #-4]		@ restore lr
+	ldmia sp!, {r0-r3, pc}
+
+	.globl return_to_handler
+return_to_handler:
+	stmdb sp!, {r0-r3}
+	bl ftrace_return_to_handler
+	mov lr, r0			@ r0 has real ret addr
+	ldmia sp!, {r0-r3}
+	mov pc, lr
+
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+
 #endif /* CONFIG_DYNAMIC_FTRACE */
 
 	.globl ftrace_stub
arch/arm/kernel/ftrace.c +146 -0

155 lines not on this page · raw message

arch/arm/kernel/vmlinux.lds.S +1 -0
@@ -95,6 +95,7 @@ SECTIONS
 			SCHED_TEXT
 			LOCK_TEXT
 			KPROBES_TEXT
+			IRQENTRY_TEXT
 #ifdef CONFIG_MMU
 			*(.fixup)
 #endif
#1 lore raw
Quoted 6 lines · Tim Bird

The use of "already_here" works around trace entry nesting (I believe due to instrumentation of either cpu_clock or raw_smp_processor_id). Advice on the best way to deal with this would be appreciated. (that is, whether to use the guard variable, or just un-instrument the offending routines).

I think it's better to keep functions instrumented where possible, if checking for context isn't too expensive. Sometimes it's interesting to see how many times they are called.

But it's not much use instrumenting those functions if activity on a different CPU suppresses it. Is there no way to make "already_here" task/CPU specific?

What do other architectures do? Consistency is good.

-- Jamie