/* 2008-10-21: File added by Sony Corporation */
/*
 *  kretp-kp-test-003.c - A kretprobe and kprobe with probe point to the 
 *  kernel function sys_read.
 *
 *  Copyright 2006,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 <linux/module.h>
#include <linux/init.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>

static const char *probed_func = "sys_read";
static struct kprobe k_003_kpr;
static int k_003_kr_kprobe_read_cnt;
static int k_003_kr_kretprobe_read_cnt;

/* Return-probe handler: Log the return value from the probed function. */
static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{
	k_003_kr_kretprobe_read_cnt++;
	return 0;
}

static struct kretprobe my_kretprobe = {
	.handler = ret_handler,
	/* Probe up to 20 instances concurrently. */
	.maxactive = 20
};

static void __exit k_003_exit_probe(void)
{
	printk("%s %d, kernel kprobe_read_cnt is %d \n", __FILE__, __LINE__, k_003_kr_kprobe_read_cnt);
	printk("%s %d, kernel kretprobe_read_cnt is %d \n", __FILE__, __LINE__, k_003_kr_kretprobe_read_cnt);
	printk("%s %d, Module exiting from sys_read \n", __FILE__, __LINE__);
	unregister_kprobe(&k_003_kpr);
	printk("%s %d,kprobe unregistered\n", __FILE__, __LINE__);
	unregister_kretprobe(&my_kretprobe);
	printk("%s %d,kretprobe unregistered\n", __FILE__, __LINE__);
}

static int k_003_before_hook(struct kprobe *kpr, struct pt_regs *p)
{
	return 0;
}

static int k_003_after_hook(struct kprobe *kpr,
		        struct pt_regs *p, unsigned long flags)
{
	k_003_kr_kprobe_read_cnt++;
	return 0;
}

static int __init k_003_init_probe(void)
{
	int ret;
	printk("%s %d, Inserting the kprobe for sys_read\n", __FILE__, __LINE__);

	/* Registering a kprobe */
	k_003_kpr.pre_handler = (kprobe_pre_handler_t) k_003_before_hook;
	k_003_kpr.post_handler = (kprobe_post_handler_t) k_003_after_hook;

	k_003_kpr.symbol_name = "sys_read";

	if (register_kprobe(&k_003_kpr) < 0) {
		printk("%s %d, register_kprobe is failed\n", __FILE__, __LINE__);
		return -1;
	}

	my_kretprobe.kp.symbol_name = (char *)probed_func;

	if ((ret = register_kretprobe(&my_kretprobe)) < 0) {
		printk("%s %d,register_kretprobe failed, returned %d\n", __FILE__, __LINE__, ret);
	return -1;
	}
	printk("%s %d,Planted return probe at %p\n", __FILE__, __LINE__, my_kretprobe.kp.addr);

	return 0;
}

module_init(k_003_init_probe);
module_exit(k_003_exit_probe);

MODULE_DESCRIPTION("Kprobes test module");
MODULE_LICENSE("GPL");
