Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1203947
  • 博文数量: 122
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4002
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-20 08:27
文章分类
文章存档

2016年(1)

2015年(21)

2014年(100)

分类: LINUX

2014-12-29 10:35:57

1、问题
   SystemTap工具能调试新增的内核模块吗?比如自己编写的helloworld模块。

    答案:是可以的。

2、关键点:
 1)使用SystemTap调试内核模块,探测点的编写格式示例为:
      module("ext3").function("ext3_*")
 2)需要将新增的helloworld模块cp到/lib/modules/`uname -r`/extra目录中,否则找不到符号。

3、实例:
  1)helloworld模块
   a)模块代码(hello.c):

点击(此处)折叠或打开

  1. #include<linux/module.h>
  2. #include<linux/init.h>

  3. int test()
  4. {
  5.     printk("Testing....\n");
  6. }

  7. int __init hello_init (void)

  8. {
  9.     printk("HelloWorld\n");
  10.     test();
  11.     return 0;
  12. }


  13. void __exit hello_exit(void)
  14. {
  15.     test();
  16.     printk("GoodBye\n");
  17. }

  18. EXPORT_SYMBOL_GPL(test);
  19. MODULE_AUTHOR("jb ");
  20. MODULE_DESCRIPTION("hello");
  21. MODULE_LICENSE("GPL");

  22. module_init(hello_init);
  23. module_exit(hello_exit);


    b)编译方法:新建一个Makefile文件。文件的内容如下:
      obj-m   :=hello.o
      使用下面的命令进行编译:
      # make -C /usr/src/kernels/`uname -r`/ modules M=$PWD

 2)编译后的模块名为hello.ko,将其cp到/lib/modules/`uname -r`/extra目录中,并安装模块:
     cp ./hello.ko /lib/modules/`uname -r`/extra
    insmod /lib/modules/`uname -r`/extra/hello.ko
 3) 编写systemtap脚本,示例如下:

点击(此处)折叠或打开

  1. probe module("hello").function("test")
  2. {
  3.         print("Hello Systemtap!\n")
  4. }

4)执行systemtap脚本:
# stap hello.stp > hello_output.txt &
# rmmod hello
阅读(7943) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~