Linux驱动的调试-打印函数调用堆栈

(1)dump_stack()打印函数调用堆栈

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

static int hello_init()
{
    printk("hello_init\n");
    dump_stack();
    return 0;
}
static int hello_exit()
{
    printk("hello_exit\n");
    return 0;
}


module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

调用后现象:打印下面内容,内核可以正常使用

# insmod hello.ko 
hello_init
CPU: 0 PID: 509 Comm: insmod Tainted: G           O 3.10.107 #12
Backtrace: 
[<c00121b0>] (dump_backtrace+0x0/0x108) from [<c00123c0>] (show_stack+0x18/0x1c)
 r6:bf000084 r5:bf000000 r4:c0502f80 r3:00000000
[<c00123a8>] (show_stack+0x0/0x1c) from [<c0390e68>] (dump_stack+0x20/0x28)
[<c0390e48>] (dump_stack+0x0/0x28) from [<bf000018>] (hello_init+0x18/0x24 [hello])
[<bf000000>] (hello_init+0x0/0x24 [hello]) from [<c0008870>] (do_one_initcall+0xf4/0x170)
[<c000877c>] (do_one_initcall+0x0/0x170) from [<c005c5cc>] (load_module+0x1928/0x1e10)
[<c005aca4>] (load_module+0x0/0x1e10) from [<c005cc18>] (SyS_finit_module+0x6c/0x7c)
[<c005cbac>] (SyS_finit_module+0x0/0x7c) from [<c000ee80>] (ret_fast_syscall+0x0/0x34)
 r6:bec50de2 r5:00000006 r4:0089b008

(2)WARN_ON();有参数,根据真假来判断是否打印调用堆栈。

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

static int hello_init()
{
    printk("hello_init\n");
    WARN_ON(1);
    return 0;
}
static int hello_exit()
{
    printk("hello_exit\n");
    return 0;
}


module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

现象,还会告诉在源代码第几行调用的WARN_ON();系统正常运行。

# insmod hello.ko 
hello_init
------------[ cut here ]------------
WARNING: at /home/myhome/module/oop/hello.c:8 hello_init+0x20/0x30 [hello]()
Modules linked in: hello(O+) [last unloaded: hello]
CPU: 0 PID: 517 Comm: insmod Tainted: G           O 3.10.107 #12
Backtrace: 
[<c00121b0>] (dump_backtrace+0x0/0x108) from [<c00123c0>] (show_stack+0x18/0x1c)
 r6:bf004020 r5:00000009 r4:00000000 r3:00000000
[<c00123a8>] (show_stack+0x0/0x1c) from [<c0390e68>] (dump_stack+0x20/0x28)
[<c0390e48>] (dump_stack+0x0/0x28) from [<c001cf34>] (warn_slowpath_common+0x54/0x70)
[<c001cee0>] (warn_slowpath_common+0x0/0x70) from [<c001cff4>] (warn_slowpath_null+0x24/0x2c)
 r8:00000000 r7:00000001 r6:bf0040b0 r5:bf004000 r4:c0502f80
r3:00000009
[<c001cfd0>] (warn_slowpath_null+0x0/0x2c) from [<bf004020>] (hello_init+0x20/0x30 [hello])
[<bf004000>] (hello_init+0x0/0x30 [hello]) from [<c0008870>] (do_one_initcall+0xf4/0x170)
[<c000877c>] (do_one_initcall+0x0/0x170) from [<c005c5cc>] (load_module+0x1928/0x1e10)
[<c005aca4>] (load_module+0x0/0x1e10) from [<c005cc18>] (SyS_finit_module+0x6c/0x7c)
[<c005cbac>] (SyS_finit_module+0x0/0x7c) from [<c000ee80>] (ret_fast_syscall+0x0/0x34)
 r6:be922de2 r5:00000006 r4:01b73008
---[ end trace c8d0911921e7ffd0 ]---

(3)BUG():会引发oops

BUG_ON()有条件判断

在驱动错误的地方用,模块崩溃,系统还能正常

 

# insmod hello.ko 
hello_init
------------[ cut here ]------------
kernel BUG at /home/myhome/module/oop/hello.c:8!
Internal error: Oops - BUG: 0 [#1] PREEMPT ARM
Modules linked in: hello(O+)
CPU: 0 PID: 505 Comm: insmod Tainted: G           O 3.10.107 #12
task: c38be000 ti: c39dc000 task.ti: c39dc000
PC is at hello_init+0x14/0x1c [hello]
LR is at 0xc04edc4c
pc : [<bf000014>]    lr : [<c04edc4c>]    psr: 60000013
sp : c39dde40  ip : c04edc4c  fp : c39dde4c
r10: 3cffb81c  r9 : c39dc010  r8 : 00000000
r7 : 00000001  r6 : bf0000a8  r5 : bf000000  r4 : c0502f80
r3 : 00000001  r2 : 00000000  r1 : 60000093  r0 : 0000000a
Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment user
Control: 0005317f  Table: 03014000  DAC: 00000015
Process insmod (pid: 505, stack limit = 0xc39dc1b8)
Stack: (0xc39dde40 to 0xc39de000)
de40: c39dde8c c39dde50 c0008870 bf000010 c39dde7c c39dde60 c39ddf44 bf0000b4
de60: bf0000a8 c39ddf44 bf0000b4 bf0000a8 00000001 c3004800 c3004808 3cffb81c
de80: c39ddf3c c39dde90 c005c5cc c000878c bf0000b4 00007fff c0059194 c00bc844
dea0: bf0001c0 bf0000b4 bf0001c0 00000000 00000001 bf0000f0 c39dc010 00000000
dec0: c4a07000 00000988 00ca3c45 00000000 0000000b 00000000 00000000 00000000
dee0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
df00: 00000000 00000000 00000000 00000000 00000008 00000000 00000003 01ce4008
df20: 0000017b c000f028 c39dc000 00000000 c39ddfa4 c39ddf40 c005cc18 c005acb4
df40: c00c7284 c4a07000 00000988 c4a07690 c4a075d0 c4a074f0 000001d4 00000274
df60: 00000000 00000000 00000000 000002d0 00000010 00000011 0000000a 00000000
df80: 00000007 00000000 c39ddfa4 01ce4008 00000006 be89ade2 00000000 c39ddfa8
dfa0: c000ee80 c005cbbc 01ce4008 00000006 00000003 01ce4008 00000000 be89ade2
dfc0: 01ce4008 00000006 be89ade2 0000017b 00000000 00000000 b6f59f74 00000000
dfe0: be89ab28 be89ab18 00025af0 b6ea3d60 a0000010 00000003 00000000 00000000
Backtrace: 
[<bf000000>] (hello_init+0x0/0x1c [hello]) from [<c0008870>] (do_one_initcall+0xf4/0x170)
[<c000877c>] (do_one_initcall+0x0/0x170) from [<c005c5cc>] (load_module+0x1928/0x1e10)
[<c005aca4>] (load_module+0x0/0x1e10) from [<c005cc18>] (SyS_finit_module+0x6c/0x7c)
[<c005cbac>] (SyS_finit_module+0x0/0x7c) from [<c000ee80>] (ret_fast_syscall+0x0/0x34)
 r6:be89ade2 r5:00000006 r4:01ce4008
Code: e92dd800 e24cb004 e59f0004 eb4e3eeb (e7f001f2) 
---[ end trace ae02eee25f704b15 ]---
Segmentation fault

 

打印oops之后,不能卸载模块,显示:

# rmmod hello
rmmod: can't unload module 'hello': Device or resource busy
# lsmod 
Module                  Size  Used by    Tainted: G  
hello                   1425  1 

(4)panic("hello");参数是字符串,会被打印出来

打印调用栈

控制台彻底没反应。

 

# insmod hello.ko 
hello_init
Kernel panic - not syncing: hello
CPU: 0 PID: 502 Comm: insmod Tainted: G           O 3.10.107 #12
Backtrace: 
[<c00121b0>] (dump_backtrace+0x0/0x108) from [<c00123c0>] (show_stack+0x18/0x1c)
 r6:bf00008c r5:bf000000 r4:c0502f80 r3:00000000
[<c00123a8>] (show_stack+0x0/0x1c) from [<c0390e68>] (dump_stack+0x20/0x28)
[<c0390e48>] (dump_stack+0x0/0x28) from [<c038fa40>] (panic+0xa0/0x1f4)
[<c038f9a0>] (panic+0x0/0x1f4) from [<bf00001c>] (hello_init+0x1c/0x24 [hello])
 r3:00000001 r2:00000000 r1:60000093 r0:bf000074
 r7:00000001
[<bf000000>] (hello_init+0x0/0x24 [hello]) from [<c0008870>] (do_one_initcall+0xf4/0x170)
[<c000877c>] (do_one_initcall+0x0/0x170) from [<c005c5cc>] (load_module+0x1928/0x1e10)
[<c005aca4>] (load_module+0x0/0x1e10) from [<c005cc18>] (SyS_finit_module+0x6c/0x7c)
[<c005cbac>] (SyS_finit_module+0x0/0x7c) from [<c000ee80>] (ret_fast_syscall+0x0/0x34)
 r6:bee62de2 r5:00000006 r4:003fc008

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值