Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1269056
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: LINUX

2018-03-30 10:11:40

调试代码大部分都是真的代码行进行运行时调式,可是有时候我们想知道某个变量的变化情况,对于庞大的代码,对每一个读写该变量的位置进行log不太现实,鉴于该问题x86体系就提出了hwbp机制,linux也整合了该机制。

代码说明一切:

点击(此处)折叠或打开

  1. #include <linux/module.h> /* Needed by all modules */^M
  2. #include <linux/kernel.h> /* Needed for KERN_INFO */^M
  3. #include <linux/init.h> /* Needed for the macros */^M
  4. #include <linux/kallsyms.h>^M
  5. ^M
  6. #include <linux/perf_event.h>^M
  7. #include <linux/hw_breakpoint.h>^M
  8. #include <asm/rwsem.h>^M
  9. struct perf_event * __percpu *sample_hbp;^M
  10. ^M
  11. /*static char ksym_name[KSYM_NAME_LEN] = "pid_max";^M
  12. module_param_string(ksym, ksym_name, KSYM_NAME_LEN, S_IRUGO);^M
  13. MODULE_PARM_DESC(ksym, "Kernel symbol to monitor; this module will report any"^M
  14.                         " write operations on the kernel symbol");^M
  15. */^M
  16. static int pid=0;^M
  17. static struct task_struct *gtsk=NULL;^M
  18. module_param(pid,int,S_IRUGO);^M
  19. MODULE_PARM_DESC(pid,"pid of process to monitor mm rwsem");^M
  20. ^M
  21. enum rwsem_waiter_type {^M
  22.         RWSEM_WAITING_FOR_WRITE,^M
  23.         RWSEM_WAITING_FOR_READ^M
  24. };^M
  25. ^M
  26. struct rwsem_waiter {^M
  27.         struct list_head list;^M
  28.         struct task_struct *task;^M
  29.         enum rwsem_waiter_type type;^M
  30. };^M
  31. ^M
  32. static void sample_hbp_handler(struct perf_event *bp,^M
  33.                                struct perf_sample_data *data,^M
  34.                                struct pt_regs *regs)^M
  35. {^M
  36.         printk(KERN_INFO " value is changed\n");^M
  37.         dump_stack();^M
  38.         printk(KERN_INFO "Dump stack from sample_hbp_handler\n");^M
  39.         struct rwsem_waiter *waiter;^M
  40.         if (list_empty(&(gtsk->mm->mmap_sem.wait_list)))^M
  41.                 printk(KERN_INFO "sem list empty");^M
  42.         list_for_each_entry(waiter,&(gtsk->mm->mmap_sem.wait_list),list)^M
  43.                 printk(KERN_INFO "sem list pid %d",waiter->task->pid);^M
  44. }^M
  45. ^M
  46. static int __init hw_break_module_init(void)^M
  47. {^M
  48.         int ret = 0;^M
  49.         struct perf_event_attr attr;^M
  50. ^M
  51.         struct task_struct *tsk = NULL;^M
  52.         for_each_process(tsk)^M
  53.         {^M
  54.                 if (tsk->pid == pid)^M
  55.                 {^M
  56.                         gtsk = tsk;^M
  57.                         break;^M
  58.                 }^M
  59.         }^M
  60.         if (tsk == &init_task)^M
  61.                 goto fail;^M
  62.         ^M
  63.         hw_breakpoint_init(&attr);^M
  64.         /*attr.bp_addr = kallsyms_lookup_name(ksym_name);*/
  65.         attr.bp_addr = (__u64)&(tsk->mm->mmap_sem.count);
  66.         printk("addr of data is %p",&(tsk->mm->mmap_sem.count));
  67.         attr.bp_len = HW_BREAKPOINT_LEN_4;^M
  68.         attr.bp_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
  69. ^M
  70.         sample_hbp = register_wide_hw_breakpoint(&attr, sample_hbp_handler, NULL);
  71.         if (IS_ERR((void __force *)sample_hbp)) {
  72.                 ret = PTR_ERR((void __force *)sample_hbp);
  73.                 goto fail;^M
  74.         }^M
  75. ^M
  76.         printk(KERN_INFO "HW Breakpoint for rwsem write installed\n");^M
  77. ^M
  78.         return 0;^M
  79. ^M
  80. fail:^M
  81.         printk(KERN_INFO "Breakpoint registration failed\n");^M
  82. ^M
  83.         return ret;^M
  84. }^M
  85. ^M
  86. static void __exit hw_break_module_exit(void)^M
  87. {^M
  88.         unregister_wide_hw_breakpoint(sample_hbp);^M
  89.         printk(KERN_INFO "HW Breakpoint for rwsem %d write uninstalled\n", pid);^M
  90. }^M
  91. ^M
  92. module_init(hw_break_module_init);^M
  93. module_exit(hw_break_module_exit);^M
  94. ^M
  95. MODULE_LICENSE("GPL");^M
  96. MODULE_AUTHOR("K.Prasad");^M
  97. MODULE_DESCRIPTION("ksym breakpoint");

阅读(6529) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~