ftrace: add basic support for gcc profiler instrumentation
Subsystems (5)
PATCH 10/19 ftrace: add basic support for gcc profiler instrumentation
From: Arnaldo Carvalho de Melo <[email protected]>
If CONFIG_FTRACE is selected and /proc/sys/kernel/ftrace_enabled is set to a non-zero value the ftrace routine will be called everytime we enter a kernel function that is not marked with the "notrace" attribute.
The ftrace routine will then call a registered function if a function happens to be registered.
[ This code has been highly hacked by Steven Rostedt and Ingo Molnar,
so don't blame Arnaldo for all of this ;-) ]
Update:
It is now possible to register more than one ftrace function. If only one ftrace function is registered, that will be the function that ftrace calls directly. If more than one function is registered, then ftrace will call a function that will loop through the functions to call.
--- Makefile | 3 arch/x86/Kconfig | 1 arch/x86/kernel/entry_32.S | 27 ++++++++ arch/x86/kernel/entry_64.S | 37 ++++++++++++ include/linux/ftrace.h | 38 ++++++++++++ kernel/Makefile | 1 kernel/trace/Kconfig | 5 + kernel/trace/Makefile | 3 kernel/trace/ftrace.c | 138 +++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 2 10 files changed, 255 insertions(+)
Makefile +3 -0
@@ -509,6 +509,9 @@ endif include $(srctree)/arch/$(SRCARCH)/Makefile +ifdef CONFIG_FTRACE +KBUILD_CFLAGS += -pg +endif ifdef CONFIG_FRAME_POINTER KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls else
arch/x86/Kconfig +1 -0
@@ -20,6 +20,7 @@ config X86 def_bool y select HAVE_OPROFILE select HAVE_KPROBES + select HAVE_FTRACE config GENERIC_LOCKBREAK def_bool n
arch/x86/kernel/entry_32.S +27 -0
@@ -75,6 +75,33 @@ DF_MASK = 0x00000400 NT_MASK = 0x00004000 VM_MASK = 0x00020000 +#ifdef CONFIG_FTRACE +ENTRY(mcount) + cmpl $ftrace_stub, ftrace_trace_function + jnz trace + +.globl ftrace_stub +ftrace_stub: + ret + + /* taken from glibc */ +trace: + pushl %eax + pushl %ecx + pushl %edx + movl 0xc(%esp), %eax + movl 0x4(%ebp), %edx + + call *ftrace_trace_function + + popl %edx + popl %ecx + popl %eax + + jmp ftrace_stub +END(mcount) +#endif + #ifdef CONFIG_PREEMPT #define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF #else
arch/x86/kernel/entry_64.S +37 -0
@@ -54,6 +54,43 @@ .code64 +#ifdef CONFIG_FTRACE +ENTRY(mcount) + cmpq $ftrace_stub, ftrace_trace_function + jnz trace +.globl ftrace_stub +ftrace_stub: + retq + +trace: + /* taken from glibc */ + subq $0x38, %rsp + movq %rax, (%rsp) + movq %rcx, 8(%rsp) + movq %rdx, 16(%rsp) + movq %rsi, 24(%rsp) + movq %rdi, 32(%rsp) + movq %r8, 40(%rsp) + movq %r9, 48(%rsp) + + movq 0x38(%rsp), %rdi + movq 8(%rbp), %rsi + + call *ftrace_trace_function + + movq 48(%rsp), %r9 + movq 40(%rsp), %r8 + movq 32(%rsp), %rdi + movq 24(%rsp), %rsi + movq 16(%rsp), %rdx + movq 8(%rsp), %rcx + movq (%rsp), %rax + addq $0x38, %rsp + + jmp ftrace_stub +END(mcount) +#endif + #ifndef CONFIG_PREEMPT #define retint_kernel retint_restore_args #endif
include/linux/ftrace.h new file +38 -0
@@ -0,0 +1,38 @@ +#ifndef _LINUX_FTRACE_H +#define _LINUX_FTRACE_H + +#ifdef CONFIG_FTRACE + +#include <linux/linkage.h> + +#define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0)) +#define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1)) +#define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2)) + +typedef void (*ftrace_func_t )(unsigned long ip,unsigned long parent_ip); + +struct ftrace_ops { +ftrace_func_t func; +struct ftrace_ops *next; +}; + +/* +* The ftrace_ops must be a static and should also +* be read_mostly. These functions do modify read_mostly variables +* so use them sparely. Never free an ftrace_op or modify the +* next pointer after it has been registered. Even after unregistering +* it, the next pointer may still be used internally. +*/ +int register_ftrace_function (struct ftrace_ops *ops); +int unregister_ftrace_function (struct ftrace_ops *ops); +void clear_ftrace_function (void ); + +extern void ftrace_stub (unsigned long a0,unsigned long a1); +extern void mcount (void ); + +#else /* !CONFIG_FTRACE */ +# define register_ftrace_function (ops) do { } while (0) +# define unregister_ftrace_function (ops) do { } while (0) +# define clear_ftrace_function (ops) do { } while (0) +#endif /* CONFIG_FTRACE */ +#endif /* _LINUX_FTRACE_H */
kernel/Makefile +1 -0
@@ -68,6 +68,7 @@ obj-$(CONFIG_TASK_DELAY_ACCT) += delayac obj-$(CONFIG_TASKSTATS) += taskstats.o tsacct.o obj-$(CONFIG_MARKERS) += marker.o obj-$(CONFIG_LATENCYTOP) += latencytop.o +obj-$(CONFIG_FTRACE) += trace/ ifneq ($(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER),y) # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer is
kernel/trace/Kconfig new file +5 -0
@@ -0,0 +1,5 @@ +# +# Architectures that offer an FTRACE implementation should select HAVE_FTRACE: +# +config HAVE_FTRACE + bool
kernel/trace/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_FTRACE) += libftrace.o + +libftrace-y := ftrace.o
kernel/trace/ftrace.c new file +138 -0
138 lines not on this page · raw message
lib/Kconfig.debug +2 -0
@@ -602,6 +602,8 @@ config LATENCYTOP Enable this option if you want to use the LatencyTOP tool to find out which userspace is blocking on what kernel operations. +source kernel/trace/Kconfig + config PROVIDE_OHCI1394_DMA_INIT bool "Provide code for enabling DMA over FireWire early on boot" depends on PCI && X86
Quoted 9 lines · Ingo Molnar
Makefile +0
-0@@ -509,6 +509,9 @@ endifinclude $(srctree)/arch/$(SRCARCH)/Makefile
Makefile +3
-0+ifdef CONFIG_FTRACE +KBUILD_CFLAGS += -pg +endif
Quoted 3 lines
ifdef
CONFIG_FRAME_POINTERKBUILD_CFLAGS+= -fno-omit-frame-pointer -fno-optimize-sibling-calls
else
You should use -fno-omit-frame-pointer when building with -pg; As far as I can remember from using gprof in the past (in userspace, I might add), the output does not make much sense without frame boundaries.
Steven Rostedt
maintainerQuoted 8 lines · Jan Engelhardt
Quoted 6 lines · Ingo Molnar
Makefile +0
-0@@ -509,6 +509,9 @@ endifinclude $(srctree)/arch/$(SRCARCH)/Makefile
Quoted 11 lines
Quoted 6 lines
Makefile +3
-0+ifdef CONFIG_FTRACE +KBUILD_CFLAGS += -pg +endififdef
CONFIG_FRAME_POINTERKBUILD_CFLAGS+= -fno-omit-frame-pointer -fno-optimize-sibling-calls
elseYou should use -fno-omit-frame-pointer when building with -pg; As far as I can remember from using gprof in the past (in userspace, I might add), the output does not make much sense without frame boundaries.
It is, but is hidden in the Kconfig. I moved it to Kconfig as it was recommended to me because it is cleaner to do it there.
Two patches later:
+config FTRACE
+ bool "Kernel Function Tracer" + depends on DEBUG_KERNEL && HAVE_FTRACE + select FRAME_POINTER
-- Steve