linux/scripts
Wu Zhangjin 538f19526e MIPS: Tracing: Add dynamic function tracer support
With dynamic function tracer, by default, _mcount is defined as an
"empty" function, it returns directly without any more action . When
enabling it in user-space, it will jump to a real tracing
function(ftrace_caller), and do the real job for us.

Differ from the static function tracer, dynamic function tracer provides
two functions ftrace_make_call()/ftrace_make_nop() to enable/disable the
tracing of some indicated kernel functions(set_ftrace_filter).

In the -v4 version, the implementation of this support is basically the same as
X86 version does: _mcount is implemented as an empty function and ftrace_caller
is implemented as a real tracing function respectively.

But in this version, to support module tracing with the help of
-mlong-calls in arch/mips/Makefile:

MODFLAGS += -mlong-calls.

The stuff becomes a little more complex. We need to cope with two
different type of calling to _mcount.

For the kernel part, the calling to _mcount(result of "objdump -hdr
vmlinux"). is like this:

	108:   03e0082d        move    at,ra
	10c:   0c000000        jal     0 <fpcsr_pending>
                        10c: R_MIPS_26  _mcount
                        10c: R_MIPS_NONE        *ABS*
                        10c: R_MIPS_NONE        *ABS*
	110:   00020021        nop

For the module with -mlong-calls, it looks like this:

	c:	3c030000 	lui	v1,0x0
			c: R_MIPS_HI16	_mcount
			c: R_MIPS_NONE	*ABS*
			c: R_MIPS_NONE	*ABS*
	10:	64630000 	daddiu	v1,v1,0
			10: R_MIPS_LO16	_mcount
			10: R_MIPS_NONE	*ABS*
			10: R_MIPS_NONE	*ABS*
	14:	03e0082d 	move	at,ra
	18:	0060f809 	jalr	v1

In the kernel version, there is only one "_mcount" string for every
kernel function, so, we just need to match this one in mcount_regex of
scripts/recordmcount.pl, but in the module version, we need to choose
one of the two to match. Herein, I choose the first one with
"R_MIPS_HI16 _mcount".

and In the kernel verion, without module tracing support, we just need
to replace "jal _mcount" by "jal ftrace_caller" to do real tracing, and
filter the tracing of some kernel functions via replacing it by a nop
instruction.

but as we have described before, the instruction "jal ftrace_caller" only left
32bit length for the address of ftrace_caller, it will fail when calling from
the module space. so, herein, we must replace something else.

the basic idea is loading the address of ftrace_caller to v1 via changing these
two instructions:

	lui	v1,0x0
	addiu	v1,v1,0

If we want to enable the tracing, we need to replace the above instructions to:

	lui	v1, HI_16BIT_ftrace_caller
	addiu	v1, v1, LOW_16BIT_ftrace_caller

If we want to stop the tracing of the indicated kernel functions, we
just need to replace the "jalr v1" to a nop instruction. but we need to
replace two instructions and encode the above two instructions
oursevles.

Is there a simpler solution? Yes! Here it is, in this version, we put _mcount
and ftrace_caller together, which means the address of _mcount and
ftrace_caller is the same:

_mcount:
ftrace_caller:
	j	ftrace_stub
	 nop

	...(do real tracing here)...

ftrace_stub:
	jr	ra
	 move	ra, at

By default, the kernel functions call _mcount, and then jump to ftrace_stub and
return. and when we want to do real tracing, we just need to remove that "j
ftrace_stub", and it will run through the two "nop" instructions and then do
the real tracing job.

what about filtering job? we just need to do this:

	 lui v1, hi_16bit_of_mcount        <--> b 1f (0x10000004)
	 addiu v1, v1, low_16bit_of_mcount
	 move at, ra
	 jalr v1
	 nop
	 				     1f: (rec->ip + 12)

In linux-mips64, there will be some local symbols, whose name are
prefixed by $L, which need to be filtered. thanks goes to Steven for
writing the mips64-specific function_regex.

In a conclusion, with RISC, things becomes easier with such a "stupid"
trick, RISC is something like K.I.S.S, and also, there are lots of
"simple" tricks in the whole ftrace support, thanks goes to Steven and
the other folks for providing such a wonderful tracing framework!

Signed-off-by: Wu Zhangjin <wuzhangjin@gmail.com>
Cc: Nicholas Mc Guire <der.herr@hofr.at>
Cc: zhangfx@lemote.com
Cc: Wu Zhangjin <wuzhangjin@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-mips@linux-mips.org
Patchwork: http://patchwork.linux-mips.org/patch/675/
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
2009-12-17 01:57:23 +00:00
..
basic Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild-next 2009-09-23 15:37:02 -07:00
dtc dtc: Mark various internal functions static 2009-11-15 15:01:41 -08:00
genksyms genksyms: Mark is_reserved_word static 2009-11-15 15:01:22 -08:00
kconfig Merge branch 'for-next' into for-linus 2009-12-07 18:36:35 +01:00
ksymoops
mod modpost: fix segfault with short symbol names 2009-12-15 16:28:37 +10:30
package kbuild: fix the binrpm-pkg target to work with KBUILD_OUTPUT set 2009-10-11 23:21:24 +02:00
rt-tester
selinux SELinux: header generation may hit infinite loop 2009-11-23 09:00:44 +11:00
tracing tracing, perf: Convert the power tracer into an event tracer 2009-09-19 11:42:12 +02:00
.gitignore gitignore: ignore scripts/ihex2fw 2009-06-27 00:17:48 +02:00
bin2c.c
binoffset.c
bloat-o-meter
bootgraph.pl
checkincludes.pl checkincludes.pl: add option to remove duplicates in place 2009-09-20 12:27:43 +02:00
checkkconfigsymbols.sh kbuild,scripts: use non-builtin echo for '-e' 2009-10-11 23:21:25 +02:00
checkpatch.pl checkpatch: version 0.30 2009-10-29 07:39:31 -07:00
checkstack.pl
checksyscalls.sh
checkversion.pl
cleanfile
cleanpatch
config
conmakehash.c Fix all -Wmissing-prototypes warnings in x86 defconfig 2009-09-23 07:39:28 -07:00
decodecode
diffconfig
export_report.pl
extract-ikconfig kconfig: have extract-ikconfig read ELF files 2009-08-18 22:00:18 -04:00
gcc-version.sh
gcc-x86_32-has-stack-protector.sh
gcc-x86_64-has-stack-protector.sh
gen_initramfs_list.sh
get_maintainer.pl scripts/get_maintainer.pl: support multiple VCSs - add mercurial 2009-12-15 08:53:29 -08:00
gfp-translate
headerdep.pl
headers_check.pl
headers_install.pl warn about use of uninstalled kernel headers 2009-10-11 23:21:25 +02:00
headers.sh
kallsyms.c kallsyms: fix segfault in prefix_underscores_count() 2009-09-23 07:39:30 -07:00
Kbuild.include kbuild,scripts: use non-builtin echo for '-e' 2009-10-11 23:21:25 +02:00
kernel-doc tracing: Add DEFINE_EVENT(), DEFINE_SINGLE_EVENT() support to docbook 2009-12-02 09:57:37 +01:00
Lindent
Makefile sparc: Kill PROM console driver. 2009-09-15 17:04:38 -07:00
Makefile.build MIPS: Tracing: Add an endian argument to scripts/recordmcount.pl 2009-12-17 01:57:22 +00:00
Makefile.clean
Makefile.fwinst
Makefile.headersinst
Makefile.host
Makefile.lib module: make MODULE_SYMBOL_PREFIX into a CONFIG option 2009-12-15 16:28:26 +10:30
Makefile.modinst
Makefile.modpost tracing: Remove markers 2009-09-18 21:22:08 +02:00
makelst
markup_oops.pl markup_oops: use modinfo to avoid confusion with underscored module names 2009-09-20 12:27:43 +02:00
mkcompile_h kbuild: mkcompile_h: trivial cleanups 2009-10-11 23:21:25 +02:00
mkmakefile
mksysmap
mkuboot.sh
mkversion
module-common.lds linker script: throw away .discard section 2009-06-24 15:13:38 +09:00
namespace.pl
patch-kernel
pnmtologo.c fbdev: work around old compiler bug 2009-06-30 18:55:59 -07:00
profile2linkerlist.pl
recordmcount.pl MIPS: Tracing: Add dynamic function tracer support 2009-12-17 01:57:23 +00:00
setlocalversion
show_delta
tags.sh ctags: usability fix 2009-09-20 12:27:43 +02:00
unifdef.c
ver_linux