Path: blob/master/samples/hw_breakpoint/data_breakpoint.c
10818 views
/*1* data_breakpoint.c - Sample HW Breakpoint file to watch kernel data address2*3* This program is free software; you can redistribute it and/or modify4* it under the terms of the GNU General Public License as published by5* the Free Software Foundation; either version 2 of the License, or6* (at your option) any later version.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program; if not, write to the Free Software15* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.16*17* usage: insmod data_breakpoint.ko ksym=<ksym_name>18*19* This file is a kernel module that places a breakpoint over ksym_name kernel20* variable using Hardware Breakpoint register. The corresponding handler which21* prints a backtrace is invoked every time a write operation is performed on22* that variable.23*24* Copyright (C) IBM Corporation, 200925*26* Author: K.Prasad <[email protected]>27*/28#include <linux/module.h> /* Needed by all modules */29#include <linux/kernel.h> /* Needed for KERN_INFO */30#include <linux/init.h> /* Needed for the macros */31#include <linux/kallsyms.h>3233#include <linux/perf_event.h>34#include <linux/hw_breakpoint.h>3536struct perf_event * __percpu *sample_hbp;3738static char ksym_name[KSYM_NAME_LEN] = "pid_max";39module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);40MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"41" write operations on the kernel symbol");4243static void sample_hbp_handler(struct perf_event *bp, int nmi,44struct perf_sample_data *data,45struct pt_regs *regs)46{47printk(KERN_INFO "%s value is changed\n", ksym_name);48dump_stack();49printk(KERN_INFO "Dump stack from sample_hbp_handler\n");50}5152static int __init hw_break_module_init(void)53{54int ret;55struct perf_event_attr attr;5657hw_breakpoint_init(&attr);58attr.bp_addr = kallsyms_lookup_name(ksym_name);59attr.bp_len = HW_BREAKPOINT_LEN_4;60attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;6162sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler);63if (IS_ERR((void __force *)sample_hbp)) {64ret = PTR_ERR((void __force *)sample_hbp);65goto fail;66}6768printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name);6970return 0;7172fail:73printk(KERN_INFO "Breakpoint registration failed\n");7475return ret;76}7778static void __exit hw_break_module_exit(void)79{80unregister_wide_hw_breakpoint(sample_hbp);81printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);82}8384module_init(hw_break_module_init);85module_exit(hw_break_module_exit);8687MODULE_LICENSE("GPL");88MODULE_AUTHOR("K.Prasad");89MODULE_DESCRIPTION("ksym breakpoint");909192