赞
踩
转发自:https://blog.csdn.net/phenix_lord/article/details/41415559
内核由于共享内存地址空间,如果没有合适的工具,很多踩内存的问题即使复现,也无法快速定位;
在新的内核版本中引入了一个新工具hardware breakpoint,其能够监视对指定的地址的特定类型(读/写)的数据访问,有利于该类问题的定位;
以下是一个使用该工具的例子(来自内核代码linux-3.10/samples/hw_breakpoint/data_breakpoint.c)
struct perf_event * __percpu *sample_hbp; static char ksym_name[KSYM_NAME_LEN] = "pid_max"; module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO); MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any" " write operations on the kernel symbol"); static void sample_hbp_handler(struct perf_event *bp, struct perf_sample_data *data, struct pt_regs *regs) { printk(KERN_INFO "%s value is changed\n", ksym_name); dump_stack(); printk(KERN_INFO "Dump stack from sample_hbp_handler\n"); } static int __init hw_break_module_init(void) { int ret; struct perf_event_attr attr; hw_breakpoint_init(&attr); attr.bp_addr = kallsyms_lookup_name(ksym_name); attr.bp_len = HW_BREAKPOINT_LEN_4; attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R; sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL); if (IS_ERR((void __force *)sample_hbp)) { ret = PTR_ERR((void __force *)sample_hbp); goto fail; } printk(KERN_INFO "HW Breakpoint for %s write installed\n", ksym_name); return 0; fail: printk(KERN_INFO "Breakpoint registration failed\n"); return ret; } static void __exit hw_break_module_exit(void) { unregister_wide_hw_breakpoint(sample_hbp); printk(KERN_INFO "HW Breakpoint for %s write uninstalled\n", ksym_name); } module_init(hw_break_module_init); module_exit(hw_break_module_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("K.Prasad"); MODULE_DESCRIPTION("ksym breakpoint");
1、各个不同的CPU有同时支持的hardwarebreakpoint数量限制,对X86,为4个.
所以对于随机的内存踩踏(频繁的申请、使用、释放)是很难处理的,比较适合固定的地址踩踏
2、指定的回调函数的调用时机:对X86,如果监视的是数据地址,则是在访问该数据的指令执行完成后,通过exception触发回调,如果监视的是指令地址,则是在该指令被执行前通过exception触发回调
3、该方式只能监视通过CPU访问地址的情况,对DMA就无能为力了
如何跟踪指令地址
addr = kallsyms_lookup_name("gcwq_nr_running");
addr = (unsigned long)per_cpu_ptr((atomic_t *)addr,cpu);
attr.bp_addr = addr
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。