Kprobe在Linux kernel debug中的应用
来源:互联网 发布:室内装饰设计软件 编辑:程序博客网 时间:2024/05/17 07:03
一直在做kernel开发方面的工作,也一直苦于kernel debug的困惑,到底如何进行kernel开发的debug的工作?今天经美国同事的推荐,我认为kprobe是一个非常好的debug工具。其本质原理就是在你需要probe的地方放入断点指令,然后在断点处调用你的调试/测试程序,从而可以实现对kernel程序的调试/测试。
Kprobe只是提供了一种机制,使得用户可以在系统运行时调试/测试内核程序。使用Kprobe需要做如下几件事情:
1,需要找到测试点所对应的内存地址。这件工作可能是最麻烦的,如果测试点是函数,那么可以通过/proc/kallsyms接口得到需要测试函数的内存地址,当然也可以通过kallsyms_lookup_name函数找到函数的内存地址。如果测试点是函数中间的某个位置,那么需要通过通过反汇编找到这个内存地址,这一步可以通过objdump来完成。目前,我不知道是不是有现成的程序可以完成测试点内存地址的查找工作,我觉得完全可以开发一个perl脚本对ko文件解析,从而获取测试点的内存地址。
2,写一个kernel module,完成信息收集,测试等工作,所有的测试代码需要在这个kernel module中完成。
下面是我今天的一个测试程序,用Kprobe测试一个内核函数所用的jiffies时间,基本上是一个Kprobe kernel module的基本框架,仅供参考:
点击(此处)折叠或打开
- /*
- * kprobe_jiffies.c
- */
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/string.h>
- #include <linux/init.h>
- #include <linux/kprobes.h>
- #include <linux/kallsyms.h>
- /* global probe object */
- struct kprobe probe;
- /* jiffies record */
- unsigned long jiffies_enter = 0;
- unsigned long jiffies_exit = 0;
- /*
- * enter the probe pointer
- */
- static int pre_probe(struct kprobe *probe, struct pt_regs *regs)
- {
- jiffiesjiffies_enter = jiffies;
- return 0;
- }
- /*
- * exit the probe pointer
- */
- static void post_probe(struct kprobe *probe, struct pt_regs *regs, unsigned long flags)
- {
- unsigned long diff;
- jiffiesjiffies_exit = jiffies;
- diff = jiffies_exit - jiffies_enter;
- printk("spending time: %lu, enter: %lu, exit: %lu\n",
- diff, jiffies_enter, jiffies_exit);
- }
- static int __init kprobe_jiffies_init(void)
- {
- probe.pre_handler = pre_probe;
- probe.post_handler = post_probe;
- probe.addr = (kprobe_opcode_t *) kallsyms_lookup_name("probe_function");
- if (probe.addr == NULL) {
- printk("Cannot find out 'dd_xor_encode_sse' in system\n");
- return 1;
- }
- register_kprobe(&probe);
- printk("register probe jffies driver.\n");
- return 0;
- }
- static void __exit kprobe_jiffies_exit(void)
- {
- unregister_kprobe(&probe);
- printk("unregister probe jffies driver.\n");
- return;
- }
- module_init(kprobe_jiffies_init);
- module_exit(kprobe_jiffies_exit);
- MODULE_AUTHOR("xxx");
- MODULE_DESCRIPTION("kernel probe driver");
- MODULE_LICENSE("GPL");
我想Kprobe是个非常不错的东西,如果在Kprobe的基础上包装一下,使得用户更加容易的使用,那么对内核程序的调试将是会发生革命性的变化。我想有时间我应该在Kprobe的基础上做一个内核调试工具了。
上一篇:Kprobes—insight into the Linux kernel—replace kernel function with module
下一篇:Linux内核kprobe机制实现浅析
- linux 常见服务端口
- xmanager 2.0 for linux配置
- 【ROOTFS搭建】busybox的httpd...
- openwrt中luci学习笔记
- 什么是shell
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
- Kprobe在Linux kernel debug中的应用
- Kprobe在Linux kernel debug中的应用
- debug在批处理文件中的应用
- Linux Kernel -- Debug VirtualBox
- linux kernel dynamic debug
- linux kernel panic debug
- Linux Kernel Crash Debug
- linux kernel debug
- 经典数据结构与算法在经典软件(linux kernel)中的应用
- 在KERNEL中加入DEBUG
- 在KERNEL中加入DEBUG .
- Linux kernel debug技巧----开启DEBUG选项
- Linux kernel debug技巧----开启DEBUG选项
- Linux kernel Low Level debug
- Debug Linux Kernel with Qemu
- Linux kernel Low Level debug
- linux kernel / android debug notes
- Linux kernel Low Level debug
- netmap分析(2)-原理分析之内存管理
- InfluxDB 开源分布式时序、事件和指标数据库
- Kprobes—insight into the Linux kernel—replace kernel function with module
- 大数据时代的精准推送限制了我们的视野
- 重新学习《C++Primer5》第16章-模板与泛型编程
- Kprobe在Linux kernel debug中的应用
- LIBSVM与LIBLINEAR(三)
- Linux内核kprobe机制实现浅析
- 服务器可能遭受攻击汇总
- OLSR—Optimized Link State Routing——MANET—Mobile Ad hoc network
- 全球机器人技术专利十强企业中日本独占八家
- oracle表分区详解
- 为Android安装BusyBox —— 完整的bash shell
- 贪心算法—Problem P