linux-kernel-test/arch/x86_64/kernel
Rusty Lynch 73649dab0f [PATCH] x86_64 specific function return probes
The following patch adds the x86_64 architecture specific implementation
for function return probes.

Function return probes is a mechanism built on top of kprobes that allows
a caller to register a handler to be called when a given function exits.
For example, to instrument the return path of sys_mkdir:

static int sys_mkdir_exit(struct kretprobe_instance *i, struct pt_regs *regs)
{
	printk("sys_mkdir exited\n");
	return 0;
}
static struct kretprobe return_probe = {
	.handler = sys_mkdir_exit,
};

<inside setup function>

return_probe.kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("sys_mkdir");
if (register_kretprobe(&return_probe)) {
	printk(KERN_DEBUG "Unable to register return probe!\n");
	/* do error path */
}

<inside cleanup function>
unregister_kretprobe(&return_probe);

The way this works is that:

* At system initialization time, kernel/kprobes.c installs a kprobe
  on a function called kretprobe_trampoline() that is implemented in
  the arch/x86_64/kernel/kprobes.c  (More on this later)

* When a return probe is registered using register_kretprobe(),
  kernel/kprobes.c will install a kprobe on the first instruction of the
  targeted function with the pre handler set to arch_prepare_kretprobe()
  which is implemented in arch/x86_64/kernel/kprobes.c.

* arch_prepare_kretprobe() will prepare a kretprobe instance that stores:
  - nodes for hanging this instance in an empty or free list
  - a pointer to the return probe
  - the original return address
  - a pointer to the stack address

  With all this stowed away, arch_prepare_kretprobe() then sets the return
  address for the targeted function to a special trampoline function called
  kretprobe_trampoline() implemented in arch/x86_64/kernel/kprobes.c

* The kprobe completes as normal, with control passing back to the target
  function that executes as normal, and eventually returns to our trampoline
  function.

* Since a kprobe was installed on kretprobe_trampoline() during system
  initialization, control passes back to kprobes via the architecture
  specific function trampoline_probe_handler() which will lookup the
  instance in an hlist maintained by kernel/kprobes.c, and then call
  the handler function.

* When trampoline_probe_handler() is done, the kprobes infrastructure
  single steps the original instruction (in this case just a top), and
  then calls trampoline_post_handler().  trampoline_post_handler() then
  looks up the instance again, puts the instance back on the free list,
  and then makes a long jump back to the original return instruction.

So to recap, to instrument the exit path of a function this implementation
will cause four interruptions:

  - A breakpoint at the very beginning of the function allowing us to
    switch out the return address
  - A single step interruption to execute the original instruction that
    we replaced with the break instruction (normal kprobe flow)
  - A breakpoint in the trampoline function where our instrumented function
    returned to
  - A single step interruption to execute the original instruction that
    we replaced with the break instruction (normal kprobe flow)

Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-06-23 09:45:21 -07:00
..
acpi Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
cpufreq Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
aperture.c [PATCH] remove direct ref to contig_page_data for x86-64 2005-06-23 09:45:06 -07:00
apic.c [PATCH] x86_64: Collected NMI watchdog fixes. 2005-05-17 07:59:16 -07:00
asm-offsets.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
e820.c [PATCH] x86-64: Handle empty E820 regions correctly 2005-05-01 08:58:52 -07:00
early_printk.c [PATCH] allow early printk to use more than 25 lines 2005-06-23 09:45:10 -07:00
entry.S [PATCH] x86_64: Readd missing tests in entry.S 2005-05-17 07:59:13 -07:00
genapic_cluster.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
genapic_flat.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
genapic.c [PATCH] x86_64 genapic update 2005-04-16 15:24:53 -07:00
head64.c [PATCH] reorganize x86-64 NUMA and DISCONTIGMEM config options 2005-06-23 09:45:06 -07:00
head.S [PATCH] x86_64: Dump stack and prevent recursion on early fault 2005-04-16 15:25:00 -07:00
i387.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
i8259.c [PATCH] x86_64: i8259.c iso99 structure initialization 2005-06-23 09:45:12 -07:00
init_task.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
io_apic.c [PATCH] x86_64: More fixes for compilation without CONFIG_ACPI 2005-05-31 14:54:17 -07:00
ioport.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
irq.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
kprobes.c [PATCH] x86_64 specific function return probes 2005-06-23 09:45:21 -07:00
ldt.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
Makefile [PATCH] x86_64: Add pmtimer support 2005-05-17 07:59:15 -07:00
mce_intel.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
mce.c [PATCH] x86_64: Remove excessive stack allocation in MCE code with large NR_CPUS 2005-04-16 15:25:10 -07:00
module.c [PATCH] uml: kludgy compilation fixes for x86-64 subarch modules support 2005-05-05 16:36:33 -07:00
mpparse.c [PATCH] x86_64: avoid wasting IRQs 2005-06-23 09:45:13 -07:00
msr.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
nmi.c [PATCH] x86_64: Collected NMI watchdog fixes. 2005-05-17 07:59:16 -07:00
pci-dma.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
pci-gart.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
pci-nommu.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
pmtimer.c [PATCH] x86_64: Add pmtimer support 2005-05-17 07:59:15 -07:00
process.c [PATCH] x86_64 specific function return probes 2005-06-23 09:45:21 -07:00
ptrace.c [PATCH] x86_64: TASK_SIZE fixes for compatibility mode processes 2005-06-21 18:46:12 -07:00
reboot.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
semaphore.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
setup64.c [PATCH] Increase number of e820 entries hard limit from 32 to 128 2005-05-01 08:58:51 -07:00
setup.c [PATCH] add x86-64 specific support for sparsemem 2005-06-23 09:45:07 -07:00
signal.c [PATCH] xen: x86_64: use more usermode macro 2005-06-23 09:45:14 -07:00
smp.c [PATCH] x86_64: Switch SMP bootup over to new CPU hotplug state machine 2005-04-16 15:25:19 -07:00
smpboot.c [PATCH] x86_64: i386/x86-64: Export cpu_core_map 2005-05-20 15:48:21 -07:00
suspend_asm.S Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
suspend.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
sys_x86_64.c [PATCH] Avoiding mmap fragmentation 2005-06-21 18:46:16 -07:00
syscall.c Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
time.c [PATCH] x86_64: fix hpet for systems that don't support legacy replacement 2005-06-23 09:45:12 -07:00
trampoline.S Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
traps.c [PATCH] xen: x86_64: use more usermode macro 2005-06-23 09:45:14 -07:00
vmlinux.lds.S Linux-2.6.12-rc2 2005-04-16 15:20:36 -07:00
vsyscall.c [PATCH] x86_64: Make vsyscall.c compile without CONFIG_SYSCTL 2005-05-17 07:59:16 -07:00
x8664_ksyms.c [PATCH] x86_64: CONFIG_BUG=n fixes 2005-05-25 15:31:28 -07:00