Path: blob/master/samples/hw_breakpoint/data_breakpoint.c
26278 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* data_breakpoint.c - Sample HW Breakpoint file to watch kernel data address3*4* usage: insmod data_breakpoint.ko ksym=<ksym_name>5*6* This file is a kernel module that places a breakpoint over ksym_name kernel7* variable using Hardware Breakpoint register. The corresponding handler which8* prints a backtrace is invoked every time a write operation is performed on9* that variable.10*11* Copyright (C) IBM Corporation, 200912*13* Author: K.Prasad <[email protected]>14*/15#include <linux/module.h> /* Needed by all modules */16#include <linux/kernel.h> /* Needed for KERN_INFO */17#include <linux/init.h> /* Needed for the macros */18#include <linux/kallsyms.h>1920#include <linux/perf_event.h>21#include <linux/hw_breakpoint.h>2223static struct perf_event * __percpu *sample_hbp;2425static char ksym_name[KSYM_NAME_LEN] = "jiffies";26module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);27MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"28" write operations on the kernel symbol");2930static void sample_hbp_handler(struct perf_event *bp,31struct perf_sample_data *data,32struct pt_regs *regs)33{34printk(KERN_INFO "%s value is changed\n", ksym_name);35dump_stack();36printk(KERN_INFO "Dump stack from sample_hbp_handler\n");37}3839static int __init hw_break_module_init(void)40{41int ret;42struct perf_event_attr attr;43void *addr = __symbol_get(ksym_name);4445if (!addr)46return -ENXIO;4748hw_breakpoint_init(&attr);49attr.bp_addr = (unsigned long)addr;50attr.bp_len = HW_BREAKPOINT_LEN_4;51attr.bp_type = HW_BREAKPOINT_W;5253sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);54if (IS_ERR_PCPU(sample_hbp)) {55ret = PTR_ERR_PCPU(sample_hbp);56goto fail;57}5859printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name);6061return 0;6263fail:64printk(KERN_INFO "Breakpoint registration failed\n");6566return ret;67}6869static void __exit hw_break_module_exit(void)70{71unregister_wide_hw_breakpoint(sample_hbp);72#ifdef CONFIG_MODULE_UNLOAD73__symbol_put(ksym_name);74#endif75printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name);76}7778module_init(hw_break_module_init);79module_exit(hw_break_module_exit);8081MODULE_LICENSE("GPL");82MODULE_AUTHOR("K.Prasad");83MODULE_DESCRIPTION("ksym breakpoint");848586