Re: [PATCH 2/2] Kprobes: Move kprobes examples to samples/ - updated
On Tue, Feb 05, 2008 at 11:06:22AM +0530, Abhishek Sagar wrote: > On 2/5/08, Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> wrote: > > > + * Build and insert the kernel module as done in the kprobe example. > > + * You will see the trace data in /var/log/messages and on the console > > + * whenever sys_open() returns a negative value. > > A passing observation"sys_open" should be replaced with "do_fork", > whose return value is not checked at all. Oops yes! Thanks Abhishek. I just wanted the program to print execution time irrespective of error. Updated patch below... --- From: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> Move kprobes examples from Documentation/kprobes.txt to under samples/. Patch originally by Randy Dunlap. o Updated the patch to apply on 2.6.24-mm1 o Modified examples code to build on multiple architectures. Currently, the examples code works for x86 and powerpc o Cleaned up unneeded #includes o Cleaned up Kconfig per Sam Ravnborg's suggestions to fix build break on archs that don't have kretprobes o Implemented suggestions by Mathieu Desnoyers on CONFIG_KRETPROBES o Included Andrew Morton's cleanup based on x86-git Signed-off-by: Randy Dunlap <[EMAIL PROTECTED]> Signed-off-by: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> Signed-off-by: Andrew Morton <[EMAIL PROTECTED]> Acked-by: Mathieu Desnoyers <[EMAIL PROTECTED]> --- Documentation/kprobes.txt | 235 samples/Kconfig | 11 + samples/Makefile|2 samples/kprobes/Makefile|5 samples/kprobes/jprobe_example.c| 68 ++ samples/kprobes/kprobe_example.c| 91 + samples/kprobes/kretprobe_example.c | 94 ++ 7 files changed, 275 insertions(+), 231 deletions(-) Index: linux-2.6.24/Documentation/kprobes.txt === --- linux-2.6.24.orig/Documentation/kprobes.txt +++ linux-2.6.24/Documentation/kprobes.txt @@ -193,7 +193,8 @@ code mapping. The Kprobes API includes a "register" function and an "unregister" function for each type of probe. Here are terse, mini-man-page specifications for these functions and the associated probe handlers -that you'll write. See the latter half of this document for examples. +that you'll write. See the files in the samples/kprobes/ sub-directory +for examples. 4.1 register_kprobe @@ -421,249 +422,15 @@ e. Watchpoint probes (which fire on data 8. Kprobes Example -Here's a sample kernel module showing the use of kprobes to dump a -stack trace and selected i386 registers when do_fork() is called. -- cut here - -/*kprobe_example.c*/ -#include -#include -#include -#include - -/*For each probe you need to allocate a kprobe structure*/ -static struct kprobe kp; - -/*kprobe pre_handler: called just before the probed instruction is executed*/ -int handler_pre(struct kprobe *p, struct pt_regs *regs) -{ - printk("pre_handler: p->addr=0x%p, eip=%lx, eflags=0x%lx\n", - p->addr, regs->eip, regs->eflags); - dump_stack(); - return 0; -} - -/*kprobe post_handler: called after the probed instruction is executed*/ -void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) -{ - printk("post_handler: p->addr=0x%p, eflags=0x%lx\n", - p->addr, regs->eflags); -} - -/* fault_handler: this is called if an exception is generated for any - * instruction within the pre- or post-handler, or when Kprobes - * single-steps the probed instruction. - */ -int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) -{ - printk("fault_handler: p->addr=0x%p, trap #%dn", - p->addr, trapnr); - /* Return 0 because we don't handle the fault. */ - return 0; -} - -static int __init kprobe_init(void) -{ - int ret; - kp.pre_handler = handler_pre; - kp.post_handler = handler_post; - kp.fault_handler = handler_fault; - kp.symbol_name = "do_fork"; - - ret = register_kprobe(&kp); - if (ret < 0) { - printk("register_kprobe failed, returned %d\n", ret); - return ret; - } - printk("kprobe registered\n"); - return 0; -} - -static void __exit kprobe_exit(void) -{ - unregister_kprobe(&kp); - printk("kprobe unregistered\n"); -} - -module_init(kprobe_init) -module_exit(kprobe_exit) -MODULE_LICENSE("GPL"); -- cut here - - -You can build the kernel module, kprobe-example.ko, using the following -Makefile: -- cut here - -obj-m := kprobe-example.o -KDIR := /lib/modules/$(shell uname -r)/build -PWD := $(shell pwd) -default: - $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules -clean: - rm -f *.mod.c *.ko *.o -- cut here - - -$ make -$ su - -... -# insmod kprobe-example.ko - -You will see the trace data in /var/log/messages and on the console -whenever do_fork() is invoked to create a n
Re: [PATCH 2/2] Kprobes: Move kprobes examples to samples/
On 2/5/08, Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> wrote: > + * Build and insert the kernel module as done in the kprobe example. > + * You will see the trace data in /var/log/messages and on the console > + * whenever sys_open() returns a negative value. A passing observation"sys_open" should be replaced with "do_fork", whose return value is not checked at all. -- Regards, Abhishek -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[PATCH 2/2] Kprobes: Move kprobes examples to samples/
From: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> Move kprobes examples from Documentation/kprobes.txt to under samples/. Patch originally by Randy Dunlap. o Updated the patch to apply on 2.6.24-mm1 o Modified examples code to build on multiple architectures. Currently, the examples code works for x86 and powerpc o Cleaned up unneeded #includes o Cleaned up Kconfig per Sam Ravnborg's suggestions to fix build break on archs that don't have kretprobes o Implemented suggestions by Mathieu Desnoyers on CONFIG_KRETPROBES o Included Andrew Morton's cleanup based on x86-git Signed-off-by: Randy Dunlap <[EMAIL PROTECTED]> Signed-off-by: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> Signed-off-by: Andrew Morton <[EMAIL PROTECTED]> Acked-by: Mathieu Desnoyers <[EMAIL PROTECTED]> --- Documentation/kprobes.txt | 235 samples/Kconfig | 11 + samples/Makefile|2 samples/kprobes/Makefile|5 samples/kprobes/jprobe_example.c| 68 ++ samples/kprobes/kprobe_example.c| 91 + samples/kprobes/kretprobe_example.c | 95 ++ 7 files changed, 276 insertions(+), 231 deletions(-) Index: linux-2.6.24/Documentation/kprobes.txt === --- linux-2.6.24.orig/Documentation/kprobes.txt +++ linux-2.6.24/Documentation/kprobes.txt @@ -193,7 +193,8 @@ code mapping. The Kprobes API includes a "register" function and an "unregister" function for each type of probe. Here are terse, mini-man-page specifications for these functions and the associated probe handlers -that you'll write. See the latter half of this document for examples. +that you'll write. See the files in the samples/kprobes/ sub-directory +for examples. 4.1 register_kprobe @@ -421,249 +422,15 @@ e. Watchpoint probes (which fire on data 8. Kprobes Example -Here's a sample kernel module showing the use of kprobes to dump a -stack trace and selected i386 registers when do_fork() is called. -- cut here - -/*kprobe_example.c*/ -#include -#include -#include -#include - -/*For each probe you need to allocate a kprobe structure*/ -static struct kprobe kp; - -/*kprobe pre_handler: called just before the probed instruction is executed*/ -int handler_pre(struct kprobe *p, struct pt_regs *regs) -{ - printk("pre_handler: p->addr=0x%p, eip=%lx, eflags=0x%lx\n", - p->addr, regs->eip, regs->eflags); - dump_stack(); - return 0; -} - -/*kprobe post_handler: called after the probed instruction is executed*/ -void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) -{ - printk("post_handler: p->addr=0x%p, eflags=0x%lx\n", - p->addr, regs->eflags); -} - -/* fault_handler: this is called if an exception is generated for any - * instruction within the pre- or post-handler, or when Kprobes - * single-steps the probed instruction. - */ -int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) -{ - printk("fault_handler: p->addr=0x%p, trap #%dn", - p->addr, trapnr); - /* Return 0 because we don't handle the fault. */ - return 0; -} - -static int __init kprobe_init(void) -{ - int ret; - kp.pre_handler = handler_pre; - kp.post_handler = handler_post; - kp.fault_handler = handler_fault; - kp.symbol_name = "do_fork"; - - ret = register_kprobe(&kp); - if (ret < 0) { - printk("register_kprobe failed, returned %d\n", ret); - return ret; - } - printk("kprobe registered\n"); - return 0; -} - -static void __exit kprobe_exit(void) -{ - unregister_kprobe(&kp); - printk("kprobe unregistered\n"); -} - -module_init(kprobe_init) -module_exit(kprobe_exit) -MODULE_LICENSE("GPL"); -- cut here - - -You can build the kernel module, kprobe-example.ko, using the following -Makefile: -- cut here - -obj-m := kprobe-example.o -KDIR := /lib/modules/$(shell uname -r)/build -PWD := $(shell pwd) -default: - $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules -clean: - rm -f *.mod.c *.ko *.o -- cut here - - -$ make -$ su - -... -# insmod kprobe-example.ko - -You will see the trace data in /var/log/messages and on the console -whenever do_fork() is invoked to create a new process. +See samples/kprobes/kprobe_example.c 9. Jprobes Example -Here's a sample kernel module showing the use of jprobes to dump -the arguments of do_fork(). -- cut here - -/*jprobe-example.c */ -#include -#include -#include -#include -#include - -/* - * Jumper probe for do_fork. - * Mirror principle enables access to arguments of the probed routine - * from the probe handler. - */ - -/* Proxy routine having the same arguments as actual do_fork() routine */ -long jdo_fork(unsigned long clone_flags, unsigned long stack_start, - struct pt_regs *re
Re: [PATCH 2/2] Kprobes: Move kprobes examples to samples/
On Thu, Jan 03, 2008 at 10:18:06AM -0500, Mathieu Desnoyers wrote: > * Ingo Molnar ([EMAIL PROTECTED]) wrote: > > > > * Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> wrote: > > > > > > feature request: please make this work in the !modular case as well > > > > - if built-in then it should just run sometime during bootup and run > > > > the tests and report success/failure. This way automated testing can > > > > pick up any regressions much easier. > > > > > > Will try cook up something along those lines. It'll be easy to verify > > > if the probes inserted and removed properly, but verifying handlers > > > run correctly will need some work. > > > > > > We have a sort of regression test bucket that uses expect to parse the > > > dmesg to verify handlers did run correctly; that isn't a totally > > > in-kernel solution anyway. I have a couple of ideas in mind to make it > > > easier. > > > > Great. Would be really nice to have something along the lines of > > CONFIG_DEBUG_LOCKING_API_SELFTESTS. Those unit tests took time to > > develop, but they caught more than 90% (!) of the internal lockdep > > engine bugs before they ever hit mainline. > > > > I would just like to point out that the samples/ directory should keep > files as easy to read and understand for newcomers (it is meant to be > compiled Documentation examples). I see the interest in turning it into > a regression test too, but I would recommend leaving the "test" code out > of the sample module itself to improve readability. Agreed. I am working on a test bucket that doesn't touch the samples. It'll live on its own, helping with boot time smoke tests. Ananth -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 2/2] Kprobes: Move kprobes examples to samples/
* Ingo Molnar ([EMAIL PROTECTED]) wrote: > > * Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> wrote: > > > > feature request: please make this work in the !modular case as well > > > - if built-in then it should just run sometime during bootup and run > > > the tests and report success/failure. This way automated testing can > > > pick up any regressions much easier. > > > > Will try cook up something along those lines. It'll be easy to verify > > if the probes inserted and removed properly, but verifying handlers > > run correctly will need some work. > > > > We have a sort of regression test bucket that uses expect to parse the > > dmesg to verify handlers did run correctly; that isn't a totally > > in-kernel solution anyway. I have a couple of ideas in mind to make it > > easier. > > Great. Would be really nice to have something along the lines of > CONFIG_DEBUG_LOCKING_API_SELFTESTS. Those unit tests took time to > develop, but they caught more than 90% (!) of the internal lockdep > engine bugs before they ever hit mainline. > I would just like to point out that the samples/ directory should keep files as easy to read and understand for newcomers (it is meant to be compiled Documentation examples). I see the interest in turning it into a regression test too, but I would recommend leaving the "test" code out of the sample module itself to improve readability. Mathieu > Ingo -- Mathieu Desnoyers Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 2/2] Kprobes: Move kprobes examples to samples/
* Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> wrote: > > feature request: please make this work in the !modular case as well > > - if built-in then it should just run sometime during bootup and run > > the tests and report success/failure. This way automated testing can > > pick up any regressions much easier. > > Will try cook up something along those lines. It'll be easy to verify > if the probes inserted and removed properly, but verifying handlers > run correctly will need some work. > > We have a sort of regression test bucket that uses expect to parse the > dmesg to verify handlers did run correctly; that isn't a totally > in-kernel solution anyway. I have a couple of ideas in mind to make it > easier. Great. Would be really nice to have something along the lines of CONFIG_DEBUG_LOCKING_API_SELFTESTS. Those unit tests took time to develop, but they caught more than 90% (!) of the internal lockdep engine bugs before they ever hit mainline. Ingo -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 2/2] Kprobes: Move kprobes examples to samples/
On Thu, Jan 03, 2008 at 10:33:03AM +0100, Ingo Molnar wrote: > > * Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> wrote: > > > From: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> > > > > Move kprobes examples from Documentation/kprobes.txt to under > > samples/. Patch originally by Randy Dunlap. > > nice! > > > +config SAMPLE_KPROBES > > + tristate "Build kprobes examples -- loadable modules only" > > + depends on KPROBES && m > > + help > > + This build several kprobes example modules. > > feature request: please make this work in the !modular case as well - if > built-in then it should just run sometime during bootup and run the > tests and report success/failure. This way automated testing can pick up > any regressions much easier. Will try cook up something along those lines. It'll be easy to verify if the probes inserted and removed properly, but verifying handlers run correctly will need some work. We have a sort of regression test bucket that uses expect to parse the dmesg to verify handlers did run correctly; that isn't a totally in-kernel solution anyway. I have a couple of ideas in mind to make it easier. Ananth -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH 2/2] Kprobes: Move kprobes examples to samples/
* Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> wrote: > From: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> > > Move kprobes examples from Documentation/kprobes.txt to under > samples/. Patch originally by Randy Dunlap. nice! > +config SAMPLE_KPROBES > + tristate "Build kprobes examples -- loadable modules only" > + depends on KPROBES && m > + help > + This build several kprobes example modules. feature request: please make this work in the !modular case as well - if built-in then it should just run sometime during bootup and run the tests and report success/failure. This way automated testing can pick up any regressions much easier. Ingo -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[PATCH 2/2] Kprobes: Move kprobes examples to samples/
From: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> Move kprobes examples from Documentation/kprobes.txt to under samples/. Patch originally by Randy Dunlap. o Updated the patch to apply on 2.6.24-rc6-mm1 o Modified examples code to build on multiple architectures. Currently, the examples code works for x86 and powerpc o Cleaned up unneeded #includes o Cleaned up Kconfig per Sam Ravnborg's suggestions to fix build break on archs that don't have kretprobes o Implemented suggestions by Mathieu Desnoyers on CONFIG_KRETPROBES o Included Andrew Morton's cleanup based on x86-git Signed-off-by: Randy Dunlap <[EMAIL PROTECTED]> Signed-off-by: Ananth N Mavinakayanahalli <[EMAIL PROTECTED]> Signed-off-by: Andrew Morton <[EMAIL PROTECTED]> Acked-by: Mathieu Desnoyers <[EMAIL PROTECTED]> --- Documentation/kprobes.txt | 206 samples/Kconfig | 11 + samples/Makefile|2 samples/kprobes/Makefile|5 samples/kprobes/jprobe_example.c| 65 +++ samples/kprobes/kprobe_example.c| 88 +++ samples/kprobes/kretprobe_example.c | 61 ++ 7 files changed, 236 insertions(+), 202 deletions(-) Index: linux-2.6.24-rc6/Documentation/kprobes.txt === --- linux-2.6.24-rc6.orig/Documentation/kprobes.txt +++ linux-2.6.24-rc6/Documentation/kprobes.txt @@ -166,7 +166,8 @@ code mapping. The Kprobes API includes a "register" function and an "unregister" function for each type of probe. Here are terse, mini-man-page specifications for these functions and the associated probe handlers -that you'll write. See the latter half of this document for examples. +that you'll write. See the files in the samples/kprobes/ sub-directory +for examples. 4.1 register_kprobe @@ -392,220 +393,15 @@ e. Watchpoint probes (which fire on data 8. Kprobes Example -Here's a sample kernel module showing the use of kprobes to dump a -stack trace and selected i386 registers when do_fork() is called. -- cut here - -/*kprobe_example.c*/ -#include -#include -#include -#include - -/*For each probe you need to allocate a kprobe structure*/ -static struct kprobe kp; - -/*kprobe pre_handler: called just before the probed instruction is executed*/ -int handler_pre(struct kprobe *p, struct pt_regs *regs) -{ - printk("pre_handler: p->addr=0x%p, eip=%lx, eflags=0x%lx\n", - p->addr, regs->eip, regs->eflags); - dump_stack(); - return 0; -} - -/*kprobe post_handler: called after the probed instruction is executed*/ -void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) -{ - printk("post_handler: p->addr=0x%p, eflags=0x%lx\n", - p->addr, regs->eflags); -} - -/* fault_handler: this is called if an exception is generated for any - * instruction within the pre- or post-handler, or when Kprobes - * single-steps the probed instruction. - */ -int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) -{ - printk("fault_handler: p->addr=0x%p, trap #%dn", - p->addr, trapnr); - /* Return 0 because we don't handle the fault. */ - return 0; -} - -static int __init kprobe_init(void) -{ - int ret; - kp.pre_handler = handler_pre; - kp.post_handler = handler_post; - kp.fault_handler = handler_fault; - kp.symbol_name = "do_fork"; - - ret = register_kprobe(&kp); - if (ret < 0) { - printk("register_kprobe failed, returned %d\n", ret); - return ret; - } - printk("kprobe registered\n"); - return 0; -} - -static void __exit kprobe_exit(void) -{ - unregister_kprobe(&kp); - printk("kprobe unregistered\n"); -} - -module_init(kprobe_init) -module_exit(kprobe_exit) -MODULE_LICENSE("GPL"); -- cut here - - -You can build the kernel module, kprobe-example.ko, using the following -Makefile: -- cut here - -obj-m := kprobe-example.o -KDIR := /lib/modules/$(shell uname -r)/build -PWD := $(shell pwd) -default: - $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules -clean: - rm -f *.mod.c *.ko *.o -- cut here - - -$ make -$ su - -... -# insmod kprobe-example.ko - -You will see the trace data in /var/log/messages and on the console -whenever do_fork() is invoked to create a new process. +See samples/kprobes/kprobe_example.c. 9. Jprobes Example -Here's a sample kernel module showing the use of jprobes to dump -the arguments of do_fork(). -- cut here - -/*jprobe-example.c */ -#include -#include -#include -#include -#include - -/* - * Jumper probe for do_fork. - * Mirror principle enables access to arguments of the probed routine - * from the probe handler. - */ - -/* Proxy routine having the same arguments as actual do_fork() routine */ -long jdo_fork(unsigned long clone_flags, unsigned long stack_start, - st