dump_stack的简单使用

刚刚接触内核,在调试过程中用printk打印信息当然是直接有效的办法,但当我们不知到一个函数或者一个模块到底在哪里出了问题时我们可以利用dump_stack有效的找到问题的根源,下面只是简单的给出了使用方法。
  我在自己的主机上试了一下dump_stack()

Makefile文件

点击(此处)折叠或打开

  1. obj-m := hello.o
  2. KERNELBUILD :=/lib/modules/$(shell uname -r)/build
  3. default:
  4.         make -C $(KERNELBUILD) M=$(shell pwd) modules
  5. clean:
  6.         rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions

hello.c文件

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/kprobes.h>
  4. #include <asm/traps.h>
  5.  
  6. MODULE_LICENSE("Dual BSD/GPL");
  7.   
  8. static int __init hello_init(void)
  9. {
  10.      printk(KERN_ALERT "dump_stack start\n");
  11.      dump_stack();
  12.      printk(KERN_ALERT "dump_stack over\n");
  13.      return 0;
  14.  }
  15.  static void __exit hello_exit(void)
  16.  {
  17.       printk(KERN_ALERT "test module\n");
  18.  }
  19.  
  20. module_init(hello_init);
  21. module_exit(hello_exit);
注意使用dump_stack()要加上这两个头文件

点击(此处)折叠或打开

  1. #include <linux/kprobes.h>
  2. #include <asm/traps.h>
然后make得到hello.ko
在运行insmod hello.ko把模块插入内核
运行dmesg
[ 3719.352022] usb 1-8: new high speed USB device number 11 using ehci_hcd
[ 4266.252826] usb 1-8: USB disconnect, device number 11
[ 5246.942980] dump_stack start
[ 5246.942985] Pid: 3438, comm: insmod Not tainted 3.0.0-21-generic #35-Ubuntu
[ 5246.942987] Call Trace:
[ 5246.942993]  [<ffffffffa0072017>] hello_init+0x17/0x1000 [hello]
[ 5246.942999]  [<ffffffff81002042>] do_one_initcall+0x42/0x180
[ 5246.943003]  [<ffffffff810a011e>] sys_init_module+0xbe/0x230
[ 5246.943006]  [<ffffffff815fd202>] system_call_fastpath+0x16/0x1b
[ 5246.943008] dump_stack over


打出运行这个模块时调用的函数
删除模rmmod hello


补充:

Android.mk文件

点击(此处)折叠或打开

  1. obj-m := hello.o
  2. #hello-objs := hello-world.o

  3. KVERSION := $(ANDROID_PRODUCT_OUT)/obj/KERNEL_OBJ

  4. all:
  5.     make ARCH=arm CROSS_COMPILE=arm-eabi- -C $(KVERSION) M=$(PWD) modules
  6. clean:
  7.     make -C $(KVERSION) M=$(PWD) clean
在android编译环境下编译,编译出来的.ko文件可以在手机中insmod
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值