kprobe module code

#include <linux/kprobes.h> 
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/notifier.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
 /* pre_handler: this is called just before the probed instruction is
  *	executed.
  */
static int handler_pre(struct kprobe *p, struct pt_regs *regs) {
	printk("pre_handler: p->addr=0x%p, ARM_cpsr=0x%lx\n",p->addr,
		regs->ARM_cpsr);
	return 0;
}
 /* post_handler: this is called after the probed instruction is executed
  * 	(provided no exception is generated).
  */
static void handler_post(struct kprobe *p, struct pt_regs *regs, unsigned long flags) {
	printk("post_handler: p->addr=0x%p, ARM_cpsr=0x%lx\n", p->addr,
		regs->ARM_cpsr);
}
 /* fault_handler: this is called if an exception is generated for any
  *	instruction within the fault-handler, or when Kprobes
  *	single-steps the probed instruction.
  */
static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr) {
	printk("fault_handler:p->addr=0x%p, ARM_cpsr=0x%lx\n", p->addr,
		regs->ARM_cpsr);
	return 0;
}
 
static struct kprobe kp;

static int __init my_kprobe_init(void)
{
/* specify pre_handler address
  */
	kp.pre_handler=handler_pre;
 /* specify post_handler address
  */
	kp.post_handler=handler_post;
 /* specify fault_handler address
  */
	kp.fault_handler=handler_fault;
 /* specify the address/offset where you want to insert probe.
  * You can get the address using one of the methods described above.
  */
	kp.addr = (kprobe_opcode_t *) kallsyms_lookup_name("ft5x0x_ts_pen_irq_work");
 /* check if the kallsyms_lookup_name() returned the correct value.
  */
	if (kp.addr == NULL) {
		printk("kallsyms_lookup_name could not find address for the specified symbol name\n");
		return 1;
	}
 /*	or specify address directly.
  * $grep "do_fork" /usr/src/linux/System.map
  * or
  * $cat /proc/kallsyms |grep do_fork
  * or
  * $nm vmlinuz |grep do_fork
  */
//	kp.addr = (kprobe_opcode_t *) 0xc01441d0;
 /* All set to register with Kprobes
  */
        register_kprobe(&kp);
		return 0;
}
static void __exit my_kprobe_exit(void)
{	
	unregister_kprobe(&kp); 
}

module_init(my_kprobe_init);
module_exit(my_kprobe_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kprobe test driver");

你可能感兴趣的:(内核)