Index: linux-2.6.16.39/Makefile =================================================================== --- linux-2.6.16.39.orig/Makefile 2007-03-13 20:37:12.074863856 +0530 +++ linux-2.6.16.39/Makefile 2007-03-13 20:39:06.244507432 +0530 @@ -559,7 +559,7 @@ ifeq ($(KBUILD_EXTMOD),) -core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ +core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ kprobe-tests/ vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \ $(core-y) $(core-m) $(drivers-y) $(drivers-m) \ Index: linux-2.6.16.39/kprobe-tests/Kconfig =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/Kconfig 2007-03-13 20:39:06.245507280 +0530 @@ -0,0 +1,13 @@ +# +# Kprobes test modules configuration +# +config KPROBESTEST + tristate "Kprobes test modules" + depends on KPROBES + default m if KPROBES=y + help + This support if enabled, will be used to test the kprobes support. + + To compile this kprobe test support as a module, choose M here: the + module will be called k-00x(x will be any number in 1,2,....9). + If in doubt, say "N" Index: linux-2.6.16.39/kprobe-tests/Makefile =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/Makefile 2007-03-13 20:39:06.245507280 +0530 @@ -0,0 +1,5 @@ +# +# Makefile for the Kprobes test modules kernel. +# +obj-$(CONFIG_KPROBESTEST) += k-001.o k-002.o k-003.o k-004.o k-005.o k-006.o \ + k-007.o k-008.o k-009.o Index: linux-2.6.16.39/kprobe-tests/k-001.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-001.c 2007-03-13 20:39:06.246507128 +0530 @@ -0,0 +1,91 @@ +/* + * k-001.c - A Kprobe with probe point to the kernel function 'do_fork'. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include "kprobes-test.h" + +#if defined(CONFIG_PPC32) +extern void show_allregs(struct pt_regs *regs); +#endif +static struct kprobe k_001_kpr; + +static void __exit k_001_exit_probe(void) +{ + printk("\nModule exiting from do_fork \n"); + unregister_kprobe(&k_001_kpr); +} + +static int k_001_before_hook(struct kprobe *k_001_kpr, struct pt_regs *p) +{ + printk("\nBefore hook in do_fork"); + printk("\nThis is the Kprobe pre \n" + "handler for instruction at %p\n", k_001_kpr->addr); + printk("Stack Dump :\n"); + dump_stack(); + printk("The Registers are:\n"); +#if defined(CONFIG_PPC32) + show_allregs(p); +#endif + return 0; +} + +static int k_001_after_hook(struct kprobe *k_001_kpr, + struct pt_regs *p, unsigned long flags) +{ + printk("\nAfter hook in do_fork"); + printk("\nThis is the Kprobe post \n" + "handler for instruction at" " %p\n", k_001_kpr->addr); + printk("Stack Dump :\n"); + dump_stack(); + printk("The Registers are:\n"); +#if defined(CONFIG_PPC32) + show_allregs(p); +#endif + return 0; +} + +static int __init k_001_init_probe(void) +{ + printk("\nInserting the kprobe for do_fork\n"); + + /* Registering a kprobe */ + k_001_kpr.pre_handler = (kprobe_pre_handler_t) k_001_before_hook; + k_001_kpr.post_handler = (kprobe_post_handler_t) k_001_after_hook; + k_001_kpr.addr = (kprobe_opcode_t *) kallsyms_lookup_name("do_fork"); + if (k_001_kpr.addr == NULL) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the kprobe is \n" + "going to be inserted - %p\n", k_001_kpr.addr); + register_kprobe(&k_001_kpr); + return 0; +} + +module_init(k_001_init_probe); +module_exit(k_001_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-002.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-002.c 2007-03-13 20:39:06.247506976 +0530 @@ -0,0 +1,95 @@ +/* + * k-002.c - Multiple Kprobes with the same probe point for the kernel + * function 'do_fork'. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include + +static struct kprobe k_002_kp1; +static struct kprobe k_002_kp2; + +static void __exit k_002_exit_probe(void) +{ + printk("\nModule exiting"); + printk("2 kprobes from do-fork\n"); + unregister_kprobe(&k_002_kp2); + unregister_kprobe(&k_002_kp1); +} + +static int k_002_before_hook(struct kprobe *k_002_kp1, struct pt_regs *p) +{ + printk("\nBefore hook 1st do_fork\n"); + return 0; +} + +static int k_002_after_hook(struct kprobe *k_002_kp1, + struct pt_regs *p, unsigned long flags) +{ + printk("\nAfter hook 1st do_fork\n"); + return 0; +} + +static int k_002_pre_handler(struct kprobe *k_002_kp2, struct pt_regs *p) +{ + printk("\nBefore hook 2nd do_fork\n"); + return 0; +} + +static int k_002_post_handler(struct kprobe *k_002_kp2, + struct pt_regs *p, unsigned long flags) +{ + printk("\nAfter hook 2nd do_fork\n"); + return 0; +} + +static int __init k_002_init_probe(void) +{ + printk("\nInserting two kprobes at the same probe point\n"); + + /* Registering a kprobe */ + k_002_kp1.pre_handler = (kprobe_pre_handler_t) k_002_before_hook; + k_002_kp1.post_handler = (kprobe_post_handler_t) k_002_after_hook; + + k_002_kp2.pre_handler = (kprobe_pre_handler_t) k_002_pre_handler; + k_002_kp2.post_handler = (kprobe_post_handler_t) k_002_post_handler; + k_002_kp2.addr = k_002_kp1.addr = + (kprobe_opcode_t *) kallsyms_lookup_name("do_fork"); + if ((k_002_kp1.addr == NULL) || (k_002_kp2.addr == NULL)) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the two kprobes are \n" + "going to be inserted - %p and %p\n", k_002_kp1.addr, + k_002_kp2.addr); + register_kprobe(&k_002_kp1); + register_kprobe(&k_002_kp2); + + return 0; +} + +module_init(k_002_init_probe); +module_exit(k_002_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-003.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-003.c 2007-03-13 20:39:06.248506824 +0530 @@ -0,0 +1,97 @@ +/* + * k-003.c - Multiple kprobes with the different probe point for the kernel + * function 'do_fork' and 'sys_gettimeofday'. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include + +static struct kprobe k_003_kp1; +static struct kprobe k_003_kp2; + +static void __exit k_003_exit_probe(void) +{ + printk("\nModule exiting"); + printk("\n2 kprobes from do-fork and sys_gettimeofday\n"); + unregister_kprobe(&k_003_kp2); + unregister_kprobe(&k_003_kp1); +} + +static int k_003_before_hook(struct kprobe *k_003_kp1, struct pt_regs *p) +{ + printk("\nBefore hook in do_fork\n"); + return 0; +} + +static int k_003_after_hook(struct kprobe *k_003_kp1, + struct pt_regs *p, unsigned long flags) +{ + printk("\nAfter hook in do_fork\n"); + return 0; +} + +static int k_003_pre_handler(struct kprobe *k_003_kp2, struct pt_regs *p) +{ + printk("\nBefore hook in sys_gettimeofday\n"); + return 0; +} + +static int k_003_post_handler(struct kprobe *k_003_kp2, + struct pt_regs *p, unsigned long flags) +{ + printk("\nAfter hook in sys_gettimeofday\n"); + return 0; +} + +static int __init k_003_init_probe(void) +{ + printk("\nInserting two kprobes at differnt probe point\n"); + + /* Registering a kprobe */ + k_003_kp1.pre_handler = (kprobe_pre_handler_t) k_003_before_hook; + k_003_kp1.post_handler = (kprobe_post_handler_t) k_003_after_hook; + + k_003_kp2.pre_handler = (kprobe_pre_handler_t) k_003_pre_handler; + k_003_kp2.post_handler = (kprobe_post_handler_t) k_003_post_handler; + k_003_kp1.addr = (kprobe_opcode_t *) kallsyms_lookup_name("do_fork"); + k_003_kp2.addr = + (kprobe_opcode_t *) kallsyms_lookup_name("sys_gettimeofday"); + + if ((k_003_kp1.addr == NULL) | (k_003_kp2.addr == NULL)) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the kprobes are \n" + "going to be inserted - %p and %p\n", k_003_kp1.addr, + k_003_kp2.addr); + register_kprobe(&k_003_kp1); + register_kprobe(&k_003_kp2); + + return 0; +} + +module_init(k_003_init_probe); +module_exit(k_003_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-004.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-004.c 2007-03-13 20:39:06.249506672 +0530 @@ -0,0 +1,124 @@ +/* + * k-004.c - Kprobes with the fault handler for the kernel function + * 'sys_gettimeofday'. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include "kprobes-test.h" + +#if defined(CONFIG_PPC32) +extern void show_allregs(struct pt_regs *regs); +#endif +static struct kprobe k_004_kpr; + +void k_004_CPY_FROM_USER(struct file *file, char *buf, int len) +{ + + char x = 'a'; + + if (put_user(x, buf)) { + printk("put_user : -EFAULT\n"); + } + + printk("CPY_FROM_USER\n"); +} + +static void __exit k_004_exit_probe(void) +{ + printk("\nModule exiting from sys_gettimeofday \n"); + unregister_kprobe(&k_004_kpr); +} + +static int k_004_before_hook(struct kprobe *k_004_kpr, struct pt_regs *p) +{ + int len = 500; + struct file *file; + + printk("\nBefore hook in sys_gettimeofday"); + printk("\nThis is the Kprobe pre \n" + "handler for instruction at" "%p\n", k_004_kpr->addr); + printk("Stack Dump:\n"); + dump_stack(); + printk("The Registers are:\n"); +#if defined(CONFIG_PPC32) + show_allregs(p); +#endif + k_004_CPY_FROM_USER(file, NULL, len); + return 0; +} + +static int k_004_after_hook(struct kprobe *k_004_kpr, + struct pt_regs *p, unsigned long flags) +{ + printk("\nAfter hook in sys_gettimeofday"); + printk("\nThis is the Kprobe post \n" + "handler for instruction at" " %p\n", k_004_kpr->addr); + printk("Stack Dump:\n"); + dump_stack(); + printk("The Registers are:\n"); +#if defined(CONFIG_PPC32) + show_allregs(p); +#endif + return 0; +} + +int k_004_fault_probe(struct kprobe *p, struct pt_regs *regs, int trapnr) +{ + printk("\nThis is the Kprobe fault \n" + "handler for sys_gettimeodday\n"); + printk("fault_handler:p->addr=0x%p\n", p->addr); + printk("Stack Dump:\n"); + dump_stack(); + printk("The Registers are:\n"); +#if defined(CONFIG_PPC32) + show_allregs(regs); +#endif + return 0; +} + +static int __init k_004_init_probe(void) +{ + printk("\nInserting the kprobes for sys_gettimeofday\n"); + + /* Registering a kprobe */ + k_004_kpr.pre_handler = (kprobe_pre_handler_t) k_004_before_hook; + k_004_kpr.post_handler = (kprobe_post_handler_t) k_004_after_hook; + k_004_kpr.fault_handler = (kprobe_fault_handler_t) k_004_fault_probe; + k_004_kpr.addr = + (kprobe_opcode_t *) kallsyms_lookup_name("sys_gettimeofday"); + if (k_004_kpr.addr == NULL) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the kprobe is \n" + "going to be inserted - %p\n", k_004_kpr.addr); + register_kprobe(&k_004_kpr); + return 0; +} + +module_init(k_004_init_probe); +module_exit(k_004_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-005.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-005.c 2007-03-13 20:39:06.249506672 +0530 @@ -0,0 +1,72 @@ +/* + * k-005.c - A Kprobe with probe point to the kernel function sys_read. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include + +static struct kprobe k_005_kpr; +static int k_005_kr_kprobe_read_cnt; + +static void __exit k_005_exit_probe(void) +{ + printk("kernel kprobe_read_cnt is %d \n", k_005_kr_kprobe_read_cnt); + printk("\nModule exiting from sys_read \n"); + unregister_kprobe(&k_005_kpr); +} + +static int k_005_before_hook(struct kprobe *kpr, struct pt_regs *p) +{ + return 0; +} + +static int k_005_after_hook(struct kprobe *kpr, + struct pt_regs *p, unsigned long flags) +{ + k_005_kr_kprobe_read_cnt++; + return 0; +} + +static int __init k_005_init_probe(void) +{ + printk("\nInserting the kprobe for sys_read\n"); + + /* Registering a kprobe */ + k_005_kpr.pre_handler = (kprobe_pre_handler_t) k_005_before_hook; + k_005_kpr.post_handler = (kprobe_post_handler_t) k_005_after_hook; + k_005_kpr.addr = (kprobe_opcode_t *) kallsyms_lookup_name("sys_read"); + if (k_005_kpr.addr == NULL) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the kprobe is \n" + "going to be inserted - %p\n", k_005_kpr.addr); + register_kprobe(&k_005_kpr); + return 0; +} + +module_init(k_005_init_probe); +module_exit(k_005_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-006.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-006.c 2007-03-13 20:39:06.250506520 +0530 @@ -0,0 +1,72 @@ +/* + * k-006.c - A Kprobe with probe point to the kernel function sys_write. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include + +static struct kprobe k_006_kpr; +static int k_006_kr_kprobe_write_cnt; + +static void __exit k_006_exit_probe(void) +{ + printk("kernel kprobe_write_cnt is %d \n", k_006_kr_kprobe_write_cnt); + printk("\nModule exiting from sys_write \n"); + unregister_kprobe(&k_006_kpr); +} + +static int k_006_before_hook(struct kprobe *kpr, struct pt_regs *p) +{ + return 0; +} + +static int k_006_after_hook(struct kprobe *kpr, + struct pt_regs *p, unsigned long flags) +{ + k_006_kr_kprobe_write_cnt++; + return 0; +} + +static int __init k_006_init_probe(void) +{ + printk("\nInserting the kprobe for sys_write\n"); + + /* Registering a kprobe */ + k_006_kpr.pre_handler = (kprobe_pre_handler_t) k_006_before_hook; + k_006_kpr.post_handler = (kprobe_post_handler_t) k_006_after_hook; + k_006_kpr.addr = (kprobe_opcode_t *) kallsyms_lookup_name("sys_write"); + if (k_006_kpr.addr == NULL) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the kprobe is \n" + "going to be inserted - %p\n", k_006_kpr.addr); + register_kprobe(&k_006_kpr); + return 0; +} + +module_init(k_006_init_probe); +module_exit(k_006_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-007.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-007.c 2007-03-13 20:39:06.251506368 +0530 @@ -0,0 +1,80 @@ +/* + * k-007.c - Kprobes with probe points to the kernel functions __kmalloc and + * kfree. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include + +static struct kprobe k_007_kp, k_007_kp1; +int k_007_kmalloc_count = 0; +int k_007_kfree_count = 0; + +static int k_007_kmalloc_hndlr(struct kprobe *kpr, struct pt_regs *p) +{ + k_007_kmalloc_count++; + return 0; +} + +static int k_007_kfree_hndlr(struct kprobe *kpr, struct pt_regs *p) +{ + k_007_kfree_count++; + return 0; +} + +static int __init k_007_kmf_init(void) +{ + int ret; + k_007_kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("__kmalloc"); + k_007_kp.pre_handler = k_007_kmalloc_hndlr; + k_007_kp1.addr = (kprobe_opcode_t *) kallsyms_lookup_name("kfree"); + k_007_kp1.pre_handler = k_007_kfree_hndlr; + ret = register_kprobe(&k_007_kp); + if (ret != 0) { + printk(KERN_ERR "register_kprobe returns %d %d \n", ret, + __LINE__); + return -1; + } + + ret = register_kprobe(&k_007_kp1); + if (ret != 0) { + printk(KERN_ERR "register_kprobe returns %d %d \n", ret, + __LINE__); + return -1; + } + + return 0; +} + +static void __exit k_007_kmf_exit(void) +{ + printk("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n\n\n"); + printk("kmalloc count is %d \n", k_007_kmalloc_count); + printk("kfree count is %d \n", k_007_kfree_count); + printk("\n\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"); + unregister_kprobe(&k_007_kp); + unregister_kprobe(&k_007_kp1); + printk(KERN_INFO "ktime: exiting...\n"); +} + +module_init(k_007_kmf_init); +module_exit(k_007_kmf_exit); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-008.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-008.c 2007-03-13 20:39:06.252506216 +0530 @@ -0,0 +1,68 @@ +/* + * k-008.c - A Kprobe with a probe point to the kernel function + * do_gettimeofday, with an empty pre-handler which can be used + * to measure the Kprobes overhead. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include + +static struct kprobe k_008_kp1; + +static void __exit k_008_exit_probe(void) +{ + printk("\nModule exiting "); + printk("from gettimeofday\n"); + unregister_kprobe(&k_008_kp1); +} + +static int k_008_pre_handler(struct kprobe *k_008_kp1, struct pt_regs *p) +{ + return 0; +} + +static int __init k_008_init_probe(void) +{ + printk("\nInserting the kprobe at do_gettimeofday\n"); + + /* Registering a kprobe */ + k_008_kp1.pre_handler = (kprobe_pre_handler_t) k_008_pre_handler; + k_008_kp1.addr = + (kprobe_opcode_t *) kallsyms_lookup_name("do_gettimeofday"); + + if (k_008_kp1.addr == NULL) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the kprobes are \n" + "going to be inserted - %p \n", k_008_kp1.addr); + register_kprobe(&k_008_kp1); + + return 0; +} + +module_init(k_008_init_probe); +module_exit(k_008_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/kprobe-tests/k-009.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/k-009.c 2007-03-13 20:39:06.253506064 +0530 @@ -0,0 +1,74 @@ +/* + * k-009.c - A Kprobe with a probe point to the kernel function + * do_gettimeofday, with the empty pre-handler and post-handler + * which can be used to measure the Kprobes overhead. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include + +static struct kprobe k_009_kp1; + +static void __exit k_009_exit_probe(void) +{ + printk("\nModule exiting "); + printk("from gettimeofday\n"); + unregister_kprobe(&k_009_kp1); +} + +static int k_009_pre_handler(struct kprobe *k_009_kp1, struct pt_regs *p) +{ + return 0; +} + +static int k_009_post_handler(struct kprobe *k_009_kp1, + struct pt_regs *p, unsigned long flags) +{ + return 0; +} + +static int __init k_009_init_probe(void) +{ + printk("\nInserting the kprobe at do_gettimeofday\n"); + + /* Registering a kprobe */ + k_009_kp1.pre_handler = (kprobe_pre_handler_t) k_009_pre_handler; + k_009_kp1.post_handler = (kprobe_post_handler_t) k_009_post_handler; + k_009_kp1.addr = + (kprobe_opcode_t *) kallsyms_lookup_name("do_gettimeofday"); + + if (k_009_kp1.addr == NULL) { + printk("kallsyms_lookup_name could not find address " + "for the specified symbol name\n"); + return 1; + } + + printk("\nAddress where the kprobes are \n" + "going to be inserted - %p \n", k_009_kp1.addr); + register_kprobe(&k_009_kp1); + + return 0; +} + +module_init(k_009_init_probe); +module_exit(k_009_exit_probe); + +MODULE_DESCRIPTION("Kprobes test module"); +MODULE_LICENSE("GPL"); Index: linux-2.6.16.39/Documentation/kprobe-tests.txt =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/Documentation/kprobe-tests.txt 2007-03-13 20:39:06.254505912 +0530 @@ -0,0 +1,299 @@ +Title : Kernel Probes test modules + +Kprobes is a kernel debugging technique. This can be tested using kernel +modules. These kernel module will hook the user defined pre handler, post +handler and fault handler to the probed point. + +When configuring the kernel using make menuconfig/xconfig/oldconfig, +ensure that CONFIG_KPROBES is set to "y". Under "Instrumentation +Support", look for "Kprobes". + +And also ensure that CONFIG_KPROBESTEST is set to "m", only if +CONFIG_KPROBES is set to "y" + +To load and unload Kprobes-based instrumentation modules, make sure +"Loadable module support" (CONFIG_MODULES) and "Module unloading" +(CONFIG_MODULE_UNLOAD) are set to "y". + +You may also want to ensure that CONFIG_KALLSYMS and perhaps even +CONFIG_KALLSYMS_ALL are set to "y", since kallsyms_lookup_name() +is a handy, version-independent way to find a function's address. + +The Kprobes modules are: + +- kprobe-tests/k-00x.c ( x = 0,1,2....9 ) + + +1. k-001.c + +A Kprobe with probe point to the kernel function 'do_fork'. +This will test the working of KPROBES support. + +DESCRIPTION: +It has a kernel probe 'k_001_kpr' of the type `struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler : k_001_before_hook +2. Kprobe's post-handler : k_001_after_hook + +And the probed adddress is : kallsyms_lookup_name("do_fork"). + +When this module is inserted using insmod, it registers the k_001_kpr +using 'register_kprobe(&k_001_kpr)'. + +When the adress of a kernel routine 'do_fork' is hit, Kprobes calls +user defined pre handler 'k_001_before_hook()'. As part of the this +function call it dumps the stack using dump_stack() and also shows +the register content at that point using k_001_show_allregs(). + +After the probed instruction single-stepped, Kprobes calls the post +handler 'k_001_after_hook()'. This also does the same dump_stack and +shows the register content at that point using k_001_show_allregs(). + +While unloading the module using rmmod(), it deregisters the k_001_kpr +using 'unregister_kprobe()'. + + +2. k-002.c + +OBJECTIVE : +Multiple Kprobes with the same probe point for the kernel function +'do_fork'. + +DESCRIPTION : + It has two kernel probes 'k_002_kp1 and k_002_kp2' of the type +`struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler for k_002_kp1 : k_002_before_hook +2 Kprobe's post-handler for k_002_kp1 : k_002_after_hook +3. Kprobe's pre-handler for k_002_kp2 : k_002_pre_handler +4. Kprobe's post-handler for k_002_kp2 : k_002_post_handler + +And the probed adddress for k_002_kp1 is : kallsyms_lookup_name("do_fork"). +And the probed adddress for k_002_kp2 is : kallsyms_lookup_name("do_fork"). + +When this module is inserted using insmod, it registers the k_002_kp1 and +k_002_kp2 using 'register_kprobe()'. + +When the adress of a kernel routine 'do_fork' is hit, Kprobes calls +user defined pre handlers 'k_002_pre_handler' and 'k_002_before_hook'. + +After the probed instruction single-stepped, Kprobes calls the post +handlers 'k_002_post_handler' and 'k_002_after_hook()'. + +While unloading the module using rmmod(), it deregisters the k_002_kp1 +and k_002_kp2 using 'unregister_kprobe()'. + + +3. k-003.c + +OBJECTIVE : +Multiple kprobes with the different probe point for the kernel function +'do_fork' and 'sys_gettimeofday'. + +DESCRIPTION : +It has two kernel probes 'k_003_kp1 and k_003_kp2' of the type +`struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler for k_003_kp1 : k_003_before_hook +2 Kprobe's post-handler for k_003_kp1 : k_003_after_hook +3. Kprobe's pre-handler for k_003_kp2 : k_003_pre_handler +4. kprobe's post-handler for k_003_kp2 : k_003_post_handler + +And the probed adddress for k_003_kp1 is : kallsyms_lookup_name("do_fork"). +And for k_003_kp2 is : kallsyms_lookup_name("sys_gettimeofday"). + +When this module is inserted using insmod, it registers the k_003_kp1 and +k_003_kp2 using 'register_kprobe()'. + +When the adress of a kernel routine 'do_fork' is hit, Kprobes calls +user defined pre handlers 'k_003_before_hook'. + +When the adress of a kernel routine 'sys_gettimeofday' is hit, Kprobes +calls user defined pre handlers 'k_003_pre_handler'. + +After the probed instruction single-stepped, Kprobes calls the post +handlers 'k_003_post_handler' and 'k_003_after_hook()' for each probed +address. + +While unloading the module using rmmod(), it deregisters the k_003_kp1 +and k_003_kp2 using 'unregister_kprobe()'. + + +4. k-004.c + +Kprobes with the fault handler for the kernel function 'sys_gettimeofday'. + +DESCRIPTION: +It has a kernel probe 'k_004_kpr' of the type `struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler : k_004_before_hook +2. Kprobe's post-handler : k_004_after_hook +3. Kprobe's fault-handler : k_004_fault_probe + +And the probed adddress is : kallsyms_lookup_name("sys_gettimeofday"). + +When this module is inserted using insmod, it registers the k_004_kpr +using 'register_kprobe(&k_004_kpr)'. + +When the adress of a kernel routine 'sys_gettimeofday' is hit, Kprobes +calls user defined pre handler 'k_004_before_hook()'. As part of the this +function call it dumps the stack using dump_stack() and also shows the +register content at that point using k_004_show_allregs(). +And also pre handler k_004_before_hook() tries to genearate a fault by +doing put_user() for a null buffer. + +When this fault is hit, Kprobe calls kprobe's fault handler 'k_004_fault_probe'. +After the probed instruction single-stepped, Kprobes calls the post handler +'k_004_after_hook()'. This also does the same dump_stack and shows the +register content at that point using k_004_show_allregs(). + +While unloading the module using rmmod(), it deregisters the k_004_kpr using +'unregister_kprobe()'. + + +5. k-005.c + +A Kprobe with probe point to the kernel function sys_read. + +DESCRIPTION: +It has a kernel probe 'k_005_kpr' of the type `struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler : k_005_before_hook +2. Kprobe's post-handler : k_005_after_hook + +And the probed adddress is : kallsyms_lookup_name("sys_read"). + +When this module is inserted using insmod, it registers the k_005_kpr +using 'register_kprobe(&k_005_kpr)'. + +When the adress of a kernel routine 'sys_read' is hit, Kprobes +calls user defined pre handler 'k_005_before_hook()'. + +After the probed instruction single-stepped, Kprobes calls the post +handler 'k_005_after_hook()'. This will increases the count variable +'k_005_kr_kprobe_read_cnt' by one. + +While unloading the module using rmmod(), it deregisters the k_005_kpr +using 'unregister_kprobe()' and also prints the total number of read count. + + +6. k-006.c + +A Kprobe with probe point to the kernel function sys_write. + +DESCRIPTION: +It has a kernel probe 'k_006_kpr' of the type `struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler : k_006_before_hook +2. Kprobe's post-handler : k_006_after_hook + +And the probed adddress is : kallsyms_lookup_name("sys_write"). + +When this module is inserted using insmod, it registers the k_006_kpr +using 'register_kprobe(&k_006_kpr)'. + +When the adress of a kernel routine 'sys_read' is hit, Kprobes calls +user defined pre handler 'k_006_before_hook()'. + +After the probed instruction single-stepped, Kprobes calls the post +handler 'k_006_after_hook()'. This will increases the count variable +'k_006_kr_kprobe_write_cnt' by one. + +While unloading the module using rmmod(), it deregisters the k_006_kpr +using 'unregister_kprobe()' and also prints the total number of write +count. + + +7. k-007.c + +Kprobes with probe points to the kernel functions __kmalloc and kfree. + +DESCRIPTION : +It has two kernel probes 'k_007_kp and k_007_kp1' of the type +`struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler for k_007_kp : k_007_kmalloc_hndlr +2. Kprobe's pre-handler for k_007_kp1 : k_007_kfree_hndlr + +And the probed adddress for k_007_kp is : kallsyms_lookup_name("__kmalloc"). +And the probed adddress for k_007_kp1 is: kallsyms_lookup_name("kfree"). + +When this module is inserted using insmod, it registers the k_007_kp and +k_007_kp1 using 'register_kprobe()'. + +When the adress of a kernel routine '__kmalloc' is hit, Kprobes calls +user defined pre handlers 'k_007_kmalloc_hndlr'. This will increases +the count variable 'k_007_kmalloc_count' by one. + +When the adress of a kernel routine 'free' is hit, Kprobes calls user +defined pre handlers 'k_007_kfree_hndlr'. This will increases the +count variable 'k_007_kfree_count' by one. + +After the pre handler excution it does the single-stepping of the +probed instruction. + +While unloading the module using rmmod(), it deregisters the k_003_kp +and k_003_kp1 using 'unregister_kprobe()' and also prints the total +number of __kmalloc and kfree counts. + + +8. k-008.c + +A Kprobe with a probe point to the kernel function do_gettimeofday, +with an empty pre-handler which can be used to measure the Kprobes +overhead. + +DESCRIPTION: +It has a kernel probe 'k_008_kp1' of the type `struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler : k_008_pre_handler + +And the probed adddress is : kallsyms_lookup_name("do_gettimeofday"). + +When this module is inserted using insmod, it registers the k_008_kp1 +using 'register_kprobe(&k_008_kp1)'. + +When the adress of a kernel routine 'do_gettimeofday' is hit, Kprobes +calls user defined empty pre handler 'k_008_pre_handler()'. + +After the pre handler excution it does the single-stepping of the probed +instruction. + +While unloading the module using rmmod(), it deregisters the k_008_kp1 +using 'unregister_kprobe()'. + + +9. k-009.c + +A Kprobe with a probe point to the kernel function do_gettimeofday, +with the empty pre-handler and post-handler which can be used to +measure the Kprobes overhead. + +DESCRIPTION: +It has a kernel probe 'k_009_kp1' of the type `struct kprobe`. + +This module has user defined handlers as follows: +1. Kprobe's pre-handler : k_009_pre_handler +2. Kprobe's post-handler : k_009_post_handler + +And the probed adddress is : kallsyms_lookup_name("do_gettimeofday"). + +When this module is inserted using insmod, it registers the k_009_kp1 +using 'register_kprobe(&k_009_kp1)'. + +When the adress of a kernel routine 'do_gettimeofday' is hit, Kprobes +calls user defined empty pre handler 'k_009_pre_handler()'. + +After the probed instruction single-stepped, Kprobes calls the empty post +handler 'k_009_post_handler'. + +While unloading the module using rmmod(), it deregisters the k_009_kp1 +using 'unregister_kprobe()'. Index: linux-2.6.16.39/kprobe-tests/kprobes-test.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6.16.39/kprobe-tests/kprobes-test.h 2007-03-13 20:39:06.255505760 +0530 @@ -0,0 +1,101 @@ +#ifndef _KPROBES_TEST_H +#define _KPROBES_TEST_H +/* + * kprobes-test.h - Architecture specific kprobes test functions. + * + * Copyright 2007 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef CONFIG_PPC32 +#include +#include + +#define REG "%08lX" +#define REGS_PER_LINE 8 +#define LAST_VOLATILE 12 + +static struct regbit { + unsigned long bit; + const char *name; +} msr_bits[] = { + {MSR_WE, "WE"}, + {MSR_CE, "CE"}, + {MSR_EE, "EE"}, + {MSR_PR, "PR"}, + {MSR_ME, "ME"}, + {MSR_DWE, "DWE"}, + {MSR_DE, "DE"}, + {MSR_IS, "IS"}, + {MSR_DS, "DS"}, + {0, NULL} +}; + +static void printbits(unsigned long val, struct regbit *bits) +{ + const char *sep = ""; + + printk("<"); + for (; bits->bit; ++bits) + if (val & bits->bit) { + printk("%s%s", sep, bits->name); + sep = ","; + } + printk(">"); +} +void show_allregs(struct pt_regs * regs) +{ + int i, trap; + + printk("NIP: "REG" LR: "REG" CTR: "REG"\n", + regs->nip, regs->link, regs->ctr); + printk("REGS: %p TRAP: %04lx (%s)\n", + regs, regs->trap, system_utsname.release); + printk("MSR: "REG" ", regs->msr); + printbits(regs->msr, msr_bits); + printk(" CR: %08lX XER: %08lX\n", regs->ccr, regs->xer); + trap = TRAP(regs); + if (trap == 0x300 || trap == 0x600) + printk("DAR: "REG", DSISR: "REG"\n", regs->dar, regs->dsisr); + printk("TASK = %p[%d] '%s' THREAD: %p", + current, current->pid, current->comm, task_thread_info(current)); + +#ifdef CONFIG_SMP + printk(" CPU: %d", smp_processor_id()); +#endif /* CONFIG_SMP */ + + for (i = 0; i < 32; i++) { + if ((i % REGS_PER_LINE) == 0) + printk("\n" KERN_INFO "GPR%02d: ", i); + printk(REG " ", regs->gpr[i]); + if (i == LAST_VOLATILE && !FULL_REGS(regs)) + break; + } + printk("\n"); +#ifdef CONFIG_KALLSYMS + /* + * Lookup NIP late so we have the best change of getting the + * above info out without failing + */ + printk("NIP ["REG"] ", regs->nip); + print_symbol("%s\n", regs->nip); + printk("LR ["REG"] ", regs->link); + print_symbol("%s\n", regs->link); +#endif +} +#endif + + +#endif Index: linux-2.6.16.39/arch/ppc/Kconfig =================================================================== --- linux-2.6.16.39.orig/arch/ppc/Kconfig 2007-03-13 20:39:04.533767504 +0530 +++ linux-2.6.16.39/arch/ppc/Kconfig 2007-03-13 20:39:06.257505456 +0530 @@ -1410,4 +1410,5 @@ for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". +source "kprobe-tests/Kconfig" endmenu