xref: /linux/samples/hw_breakpoint/data_breakpoint.c (revision 4dc0da86967d5463708631d02a70cfed5b104884)
143203993SK.Prasad /*
243203993SK.Prasad  * data_breakpoint.c - Sample HW Breakpoint file to watch kernel data address
343203993SK.Prasad  *
443203993SK.Prasad  * This program is free software; you can redistribute it and/or modify
543203993SK.Prasad  * it under the terms of the GNU General Public License as published by
643203993SK.Prasad  * the Free Software Foundation; either version 2 of the License, or
743203993SK.Prasad  * (at your option) any later version.
843203993SK.Prasad  *
943203993SK.Prasad  * This program is distributed in the hope that it will be useful,
1043203993SK.Prasad  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1143203993SK.Prasad  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1243203993SK.Prasad  * GNU General Public License for more details.
1343203993SK.Prasad  *
1443203993SK.Prasad  * You should have received a copy of the GNU General Public License
1543203993SK.Prasad  * along with this program; if not, write to the Free Software
1643203993SK.Prasad  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1743203993SK.Prasad  *
1843203993SK.Prasad  * usage: insmod data_breakpoint.ko ksym=<ksym_name>
1943203993SK.Prasad  *
2043203993SK.Prasad  * This file is a kernel module that places a breakpoint over ksym_name kernel
2143203993SK.Prasad  * variable using Hardware Breakpoint register. The corresponding handler which
2243203993SK.Prasad  * prints a backtrace is invoked every time a write operation is performed on
2343203993SK.Prasad  * that variable.
2443203993SK.Prasad  *
2543203993SK.Prasad  * Copyright (C) IBM Corporation, 2009
26ba6909b7SK.Prasad  *
27ba6909b7SK.Prasad  * Author: K.Prasad <prasad@linux.vnet.ibm.com>
2843203993SK.Prasad  */
2943203993SK.Prasad #include <linux/module.h>	/* Needed by all modules */
3043203993SK.Prasad #include <linux/kernel.h>	/* Needed for KERN_INFO */
3143203993SK.Prasad #include <linux/init.h>		/* Needed for the macros */
32f60d24d2SFrederic Weisbecker #include <linux/kallsyms.h>
3343203993SK.Prasad 
34f60d24d2SFrederic Weisbecker #include <linux/perf_event.h>
35f60d24d2SFrederic Weisbecker #include <linux/hw_breakpoint.h>
3643203993SK.Prasad 
3744ee6358STejun Heo struct perf_event * __percpu *sample_hbp;
3843203993SK.Prasad 
3943203993SK.Prasad static char ksym_name[KSYM_NAME_LEN] = "pid_max";
4043203993SK.Prasad module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);
4143203993SK.Prasad MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"
4243203993SK.Prasad 			" write operations on the kernel symbol");
4343203993SK.Prasad 
44a8b0ca17SPeter Zijlstra static void sample_hbp_handler(struct perf_event *bp,
45b326e956SFrederic Weisbecker 			       struct perf_sample_data *data,
46b326e956SFrederic Weisbecker 			       struct pt_regs *regs)
4743203993SK.Prasad {
4843203993SK.Prasad 	printk(KERN_INFO "%s value is changed\n", ksym_name);
4943203993SK.Prasad 	dump_stack();
5043203993SK.Prasad 	printk(KERN_INFO "Dump stack from sample_hbp_handler\n");
5143203993SK.Prasad }
5243203993SK.Prasad 
5343203993SK.Prasad static int __init hw_break_module_init(void)
5443203993SK.Prasad {
5543203993SK.Prasad 	int ret;
56b326e956SFrederic Weisbecker 	struct perf_event_attr attr;
5743203993SK.Prasad 
58b326e956SFrederic Weisbecker 	hw_breakpoint_init(&attr);
59dd1853c3SFrederic Weisbecker 	attr.bp_addr = kallsyms_lookup_name(ksym_name);
60dd1853c3SFrederic Weisbecker 	attr.bp_len = HW_BREAKPOINT_LEN_4;
61dd1853c3SFrederic Weisbecker 	attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
6243203993SK.Prasad 
63*4dc0da86SAvi Kivity 	sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);
6444ee6358STejun Heo 	if (IS_ERR((void __force *)sample_hbp)) {
6544ee6358STejun Heo 		ret = PTR_ERR((void __force *)sample_hbp);
66f60d24d2SFrederic Weisbecker 		goto fail;
67f60d24d2SFrederic Weisbecker 	}
6843203993SK.Prasad 
69f60d24d2SFrederic Weisbecker 	printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name);
7043203993SK.Prasad 
7143203993SK.Prasad 	return 0;
72f60d24d2SFrederic Weisbecker 
73f60d24d2SFrederic Weisbecker fail:
74f60d24d2SFrederic Weisbecker 	printk(KERN_INFO "Breakpoint registration failed\n");
75f60d24d2SFrederic Weisbecker 
76f60d24d2SFrederic Weisbecker 	return ret;
7743203993SK.Prasad }
7843203993SK.Prasad 
7943203993SK.Prasad static void __exit hw_break_module_exit(void)
8043203993SK.Prasad {
81f60d24d2SFrederic Weisbecker 	unregister_wide_hw_breakpoint(sample_hbp);
8243203993SK.Prasad 	printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);
8343203993SK.Prasad }
8443203993SK.Prasad 
8543203993SK.Prasad module_init(hw_break_module_init);
8643203993SK.Prasad module_exit(hw_break_module_exit);
8743203993SK.Prasad 
8843203993SK.Prasad MODULE_LICENSE("GPL");
8943203993SK.Prasad MODULE_AUTHOR("K.Prasad");
9043203993SK.Prasad MODULE_DESCRIPTION("ksym breakpoint");
91