总结一下内核DEBUG中的dump_stack, BUG, BUG_ON以及panic

有点空闲时间,让我们来总结一下内核DEBUG中的各个语句吧。随便找个内核驱动,在init函数里面加入如下代码测试:

u8 a = 1, b = 0;

printk("----------dump stack\n");
dump_stack();
printk("----------BUG_ON\n");
BUG_ON(a<b);
BUG_ON(a>b);
printk("----------BUG\n");
if(a<b)
    BUG();
if(a>b)
    BUG();
printk("----------panic\n");
panic("really terrible\n");

烧进开发版后,开机,在串口中看到:
[    1.180936] eeprom is already initialized
[    1.184259] ----------dump stack
[    1.184313] [<c043b504>] (unwind_backtrace+0x0/0xf8) from [<c041ef28>] (versionhw_init+0xa8/0xd8)
[    1.184380] [<c041ef28>] (versionhw_init+0xa8/0xd8) from [<c04306e8>] (do_one_initcall+0xfc/0x164)
[    1.184443] [<c04306e8>] (do_one_initcall+0xfc/0x164) from [<c040893c>] (kernel_init+0x98/0x13c)
[    1.184502] [<c040893c>] (kernel_init+0x98/0x13c) from [<c04369c8>] (kernel_thread_exit+0x0/0x8)
[    1.184558] ----------BUG_ON
[    1.184580] kernel BUG at drivers/staging/rk29/eeprom/versionhw.c:106!
[    1.184625] Unable to handle kernel NULL pointer dereference at virtual address 00000000
[    1.399532] pgd = c0404000
[    1.402257] [00000000] *pgd=00000000
[    1.405846] Internal error: Oops: 805 [#1] PREEMPT SMP
[    1.410989] CPU: 0    Not tainted  (3.0.36+ #211)
[    1.415699] PC is at __bug+0x1c/0x28
[    1.419275] LR is at __bug+0x18/0x28
[    1.422853] pc : [<c04391a0>]    lr : [<c043919c>]    psr: 60000013
[    1.422861] sp : edc43f70  ip : 0000000d  fp : 00000000
[    1.434347] r10: 00000000  r9 : 00000000  r8 : 00000000
[    1.439573] r7 : c041ee80  r6 : c04369c8  r5 : c0ac5000  r4 : edc42000
[    1.446104] r3 : 00000000  r2 : c0a985f4  r1 : 60000093  r0 : 00000050
[    1.452637] Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment kernel
[    1.459952] Control: 10c5387d  Table: 6040404a  DAC: 00000015
[    1.465699]
[    1.465701] PC: 0xc0439120:
[    1.469978] 9120  e1a04000 e24dd010 e590003c eb018a16 e3500000 0594003c 0a00000d e2841034
[    1.478250] 9140  e594002c e891000e e88d000f ea000003 e59d000c eb018a0c e3500000 0a000006
[    1.486520] 9160  e1a0000d eb0007da e3500000 aafffff7 e3a00000 e28dd010 e8bd8010 e59d000c
[    1.494791] 9180  eafffffb e92d4008 e1a03000 e1a02001 e59f0010 e1a01003 eb127025 e3a03000
[    1.503061] 91a0  e5833000 eafffffe c09afff4 e3000010 e30012e1 e92d4008 e34c009b ebfffff0
[    1.511332] 91c0  e1a01000 e3000028 e92d4008 e34c009b eb127017 e3000010 e30012c5 e34c009b
[    1.519602] 91e0  ebffffe7 e92d4008 e1a02000 e1a03001 e3000048 e1a0100e e34c009b eb12700c
[    1.527874] 9200  e3000010 e3001299 e34c009b ebffffdc e92d4ff0 e24dd06c e1a06002 e1a08000

可见,在BUG触发后,没有再继续往下执行了,把测试语句改成:

printk("----------dump stack\n");
dump_stack();
printk("----------BUG\n");
if(a<b)
    BUG();
if(a>b)
    BUG();
printk("----------panic\n");
panic("really terrible\n");

调试日志如下,看来BUG和BUG_ON效果是一样的,具体用哪个,取决于你的喜好了,BUG_ON在编译优化的时候好处理一点,不过估计大部分人在生产版本中都还照样保留这些调试信息,呵呵

[    1.184387] ----------dump stack
[    1.184439] [<c043b504>] (unwind_backtrace+0x0/0xf8) from [<c041ef28>] (versionhw_init+0xa8/0xd8)
[    1.184507] [<c041ef28>] (versionhw_init+0xa8/0xd8) from [<c04306e8>] (do_one_initcall+0xfc/0x164)
[    1.184569] [<c04306e8>] (do_one_initcall+0xfc/0x164) from [<c040893c>] (kernel_init+0x98/0x13c)
[    1.184629] [<c040893c>] (kernel_init+0x98/0x13c) from [<c04369c8>] (kernel_thread_exit+0x0/0x8)
[    1.184685] ----------BUG
[    1.184705] kernel BUG at drivers/staging/rk29/eeprom/versionhw.c:113!
[    1.184750] Unable to handle kernel NULL pointer dereference at virtual address 00000000
[    1.399369] pgd = c0404000
[    1.402093] [00000000] *pgd=00000000
[    1.405682] Internal error: Oops: 805 [#1] PREEMPT SMP
[    1.410826] CPU: 0    Not tainted  (3.0.36+ #212)
[    1.415536] PC is at __bug+0x1c/0x28
[    1.419112] LR is at __bug+0x18/0x28
[    1.422690] pc : [<c04391a0>]    lr : [<c043919c>]    psr: 60000013
[    1.422699] sp : edc43f70  ip : 0000000d  fp : 00000000
[    1.434182] r10: 00000000  r9 : 00000000  r8 : 00000000
[    1.439409] r7 : c041ee80  r6 : c04369c8  r5 : c0ac5000  r4 : edc42000
[    1.445938] r3 : 00000000  r2 : c0a985f4  r1 : 60000093  r0 : 00000050
[    1.452470] Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment kernel
[    1.459784] Control: 10c5387d  Table: 6040404a  DAC: 00000015
[    1.465531]
[    1.465534] PC: 0xc0439120:
[    1.469811] 9120  e1a04000 e24dd010 e590003c eb018a16 e3500000 0594003c 0a00000d e2841034
[    1.478082] 9140  e594002c e891000e e88d000f ea000003 e59d000c eb018a0c e3500000 0a000006
[    1.486353] 9160  e1a0000d eb0007da e3500000 aafffff7 e3a00000 e28dd010 e8bd8010 e59d000c
[    1.494626] 9180  eafffffb e92d4008 e1a03000 e1a02001 e59f0010 e1a01003 eb127025 e3a03000
[    1.502895] 91a0  e5833000 eafffffe c09afff4 e3000010 e30012e1 e92d4008 e34c009b ebfffff0
[    1.511166] 91c0  e1a01000 e3000028 e92d4008 e34c009b eb127017 e3000010 e30012c5 e34c009b
[    1.519438] 91e0  ebffffe7 e92d4008 e1a02000 e1a03001 e3000048 e1a0100e e34c009b eb12700c
[    1.527708] 9200  e3000010 e3001299 e34c009b ebffffdc e92d4ff0 e24dd06c e1a06002 e1a08000
[    1.535978]
[    1.535981] LR: 0xc043911c:
[    1.540258] 911c  e92d4010 e1a04000 e24dd010 e590003c eb018a16 e3500000 0594003c 0a00000d
[    1.548529] 913c  e2841034 e594002c e891000e e88d000f ea000003 e59d000c eb018a0c e3500000
[    1.556800] 915c  0a000006 e1a0000d eb0007da e3500000 aafffff7 e3a00000 e28dd010 e8bd8010
[    1.565071] 917c  e59d000c eafffffb e92d4008 e1a03000 e1a02001 e59f0010 e1a01003 eb127025
[    1.573340] 919c  e3a03000 e5833000 eafffffe c09afff4 e3000010 e30012e1 e92d4008 e34c009b
[    1.581610] 91bc  ebfffff0 e1a01000 e3000028 e92d4008 e34c009b eb127017 e3000010 e30012c5
[    1.589880] 91dc  e34c009b ebffffe7 e92d4008 e1a02000 e1a03001 e3000048 e1a0100e e34c009b
[    1.598152] 91fc  eb12700c e3000010 e3001299 e34c009b ebffffdc e92d4ff0 e24dd06c e1a06002
[    1.606424]
[    1.606427] SP: 0xedc43ef0:
[    1.610704] 3ef0  00000002 205b0000 31202020 3438312e 5d353037 34380020 5d353836 00000020
[    1.618974] 3f10  00000000 ffffffff edc43f5c c04369c8 c041ee80 c0435a2c 00000050 60000093
[    1.627243] 3f30  c0a985f4 00000000 edc42000 c0ac5000 c04369c8 c041ee80 00000000 00000000
[    1.635514] 3f50  00000000 00000000 0000000d edc43f70 c043919c c04391a0 60000013 ffffffff
[    1.643786] 3f70  00000000 c041ef3c 302e3376 00000000 00000000 00000000 00000000 00000000
[    1.652055] 3f90  00000000 00000000 edc42000 c04306e8 0000016f c04af108 0000016f 00000000
[    1.660325] 3fb0  00000000 36330038 00000037 00000000 c0a9bb30 c0429168 c0429400 c04369c8
[    1.668597] 3fd0  00000013 00000000 00000000 00000000 00000000 c040893c 00000000 00000000
[    1.676866]
[    1.676869] R2: 0xc0a98574:
[    1.681146] 8574  00000019 0000001a 0000001b 0000001c 0000001d 0000001e 0000001f 00000001
[    1.689416] 8594  f7004000 00080000 00000001 c0a985a0 c0a985a0 00000000 00000000 000001f4
[    1.697687] 85b4  0000000a 00000000 00000000 00000000 0000000f 00000004 00000001 00000007
[    1.705958] 85d4  00000000 00000000 00000001 c0a985e0 c0a985e0 00000000 00000000 00000000
[    1.714227] 85f4  c0a985f4 c0a985f4 00000004 ffffffff 00000000 00000001 ffffffff 00000000
[    1.722497] 8614  c046c62c c0abee3c 00000000 00000000 00000001 00000000 00000000 c0a98630
[    1.730767] 8634  c0a98630 00000000 00000000 00000001 00000000 00000000 c0a9864c c0a9864c
[    1.739036] 8654  00000000 00000001 c09b802c c09b8030 c09b8038 c09b8040 c09b8048 c09b8050
[    1.747308]
[    1.747311] R4: 0xedc41f80:
[    1.751590] 1f80  f000019f 00000011 edc41fe0 00018180 00000000 00000000 00000000 00000000
[    1.759861] 1fa0  c08e8640 c08e0748 edc41f00 edc41e00 00000000 0000005f 00000000 00000000
[    1.768130] 1fc0  00000001 00000000 00000000 00000000 00000000 edc41fd4 edc41fd4 00000000
[    1.776400] 1fe0  5f706d73 69666661 7974696e 73696c5f dfff0074 ffb3b0ef ffefffff a9ffd7ff
[    1.784669] 2000  00000000 00000002 00000000 edc44000 c0a984e4 00000000 00000015 edc44000
[    1.792938] 2020  c1d744a0 edc42000 00000000 eddb6100 c0a9da88 0000001a edc43d94 edc43d30
[    1.801209] 2040  c08d9ed8 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.809478] 2060  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.817750]
[    1.817753] R5: 0xc0ac4f80:
[    1.822030] 4f80  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.830299] 4fa0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.838567] 4fc0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.846836] 4fe0  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.855106] 5000  00000000 c1d702b1 00000000 00000000 00000000 c1d700c0 c1d70280 00000000
[    1.863375] 5020  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.871644] 5040  00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
[    1.879915] 5060  00000000 00000000 00000000 00000000 00000000 00217adc 007c789d 00000001
[    1.888186]
[    1.888188] R6: 0xc0436948:
[    1.892465] 6948  e594300c e30fe288 e34ce08d e3a02001 e5933028 e200101f e1a0c2a0 e2811001
[    1.900736] 6968  e1a00004 e08e1101 e041110c e12fff33 e1a00006 eb129644 e59f0028 eb00d051
[    1.909005] 6988  e3500000 0affffc7 e1a01005 e1a02008 e59f0014 eb127a24 eaffffc2 c0a9bb30
[    1.917274] 69a8  c08df314 c0a723ac c08dd750 c09af76c e121f007 e1a00004 e1a0e006 e1a0f005
[    1.925543] 69c8  eb00dcfc e320f000 e30530a4 e34c30ac e5932000 e2822001 e5832000 e12fff1e
[    1.933814] 69e8  e30530a4 e34c30ac e5932000 e2422001 e5832000 e12fff1e e12fff1e e92d4008
[    1.942086] 6a08  e1a0200d e3c23d7f e3c3303f e5933000 e3130002 1a000000 eb0022d7 f1080080
[    1.950357] 6a28  e8bd8008 e92d4070 e24dd050 e1a04000 e1a05001 e28d0008 e3a01048 e1a06002
[    1.958627]
[    1.958630] R7: 0xc041ee00:
[    1.962908] ee00  1a00000e e5dd3002 e35300ff 1a00000b e5dd3003 e35300ff 1a000008 e59f204c
[    1.971178] ee20  e1a0300d e8920007 e8a30003 e3a00010 e5c32000 e1a0100d e3a02008 eb0cd507
[    1.979448] ee40  e30011b6 e3a02000 e59f0024 eb04848c e3500000 159f301c 15803038 159f3018
[    1.987718] ee60  1580303c e3a00000 e28dd024 e8bd8000 c0a023c4 c0a023d0 c0754574 c0754458
[    1.995987] ee80  e52de004 e24dd024 e3a01000 e3a02020 e1a0000d eb080819 e3a00018 e1a0100d
[    2.004257] eea0  e3a02008 eb0cd4d5 e5dd3000 e35300ff 1a000010 e5dd3001 e35300ff 1a00000d
[    2.012528] eec0  e5dd3002 e35300ff 1a00000a e5dd3003 e35300ff 1a000007 e59f305c e3a02008
[    2.020798] eee0  e8930003 e58d0000 e3a00018 e5cd1004 e1a0100d eb0cd4d9 e30011b6 e3a02000
[    2.029069] Process swapper (pid: 1, stack limit = 0xedc422f0)
[    2.034905] Stack: (0xedc43f70 to 0xedc44000)
[    2.039265] 3f60:                                     00000000 c041ef3c 302e3376 00000000
[    2.047447] 3f80: 00000000 00000000 00000000 00000000 00000000 00000000 edc42000 c04306e8
[    2.055630] 3fa0: 0000016f c04af108 0000016f 00000000 00000000 36330038 00000037 00000000
[    2.063813] 3fc0: c0a9bb30 c0429168 c0429400 c04369c8 00000013 00000000 00000000 00000000
[    2.071997] 3fe0: 00000000 c040893c 00000000 00000000 c04088a4 c04369c8 e4f2fffd d47bdfff
[    2.080187] [<c04391a0>] (__bug+0x1c/0x28) from [<c041ef3c>] (versionhw_init+0xbc/0xd8)
[    2.088203] [<c041ef3c>] (versionhw_init+0xbc/0xd8) from [<c04306e8>] (do_one_initcall+0xfc/0x164)
[    2.097170] [<c04306e8>] (do_one_initcall+0xfc/0x164) from [<c040893c>] (kernel_init+0x98/0x13c)
[    2.105963] [<c040893c>] (kernel_init+0x98/0x13c) from [<c04369c8>] (kernel_thread_exit+0x0/0x8)
[    2.114754] Code: e59f0010 e1a01003 eb127025 e3a03000 (e5833000)
[    2.124051] ---[ end trace f7c32295dc731873 ]---
[    2.128711] Kernel panic - not syncing: Attempted to kill init!
[    2.134668] [<c043b504>] (unwind_backtrace+0x0/0xf8) from [<c08d5118>] (panic+0x78/0x194)
[    2.142889] [<c08d5118>] (panic+0x78/0x194) from [<c046e4c4>] (do_exit+0x704/0x72c)
[    2.150559] [<c046e4c4>] (do_exit+0x704/0x72c) from [<c0439618>] (die+0x228/0x284)
[    2.158151] [<c0439618>] (die+0x228/0x284) from [<c08d4f0c>] (__do_kernel_fault.part.3+0x54/0x74)
[    2.167049] [<c08d4f0c>] (__do_kernel_fault.part.3+0x54/0x74) from [<c043cb54>] (do_page_fault+0x1f0/0x1f8)
[    2.176820] [<c043cb54>] (do_page_fault+0x1f0/0x1f8) from [<c04304b8>] (do_DataAbort+0x34/0x9c)
[    2.185536] [<c04304b8>] (do_DataAbort+0x34/0x9c) from [<c0435a2c>] (__dabt_svc+0x4c/0x60)
[    2.193827] Exception stack(0xedc43f28 to 0xedc43f70)
[    2.198883] 3f20:                   00000050 60000093 c0a985f4 00000000 edc42000 c0ac5000
[    2.207074] 3f40: c04369c8 c041ee80 00000000 00000000 00000000 00000000 0000000d edc43f70
[    2.215276] 3f60: c043919c c04391a0 60000013 ffffffff
[    2.220339] [<c0435a2c>] (__dabt_svc+0x4c/0x60) from [<c04391a0>] (__bug+0x1c/0x28)
[    2.228019] [<c04391a0>] (__bug+0x1c/0x28) from [<c041ef3c>] (versionhw_init+0xbc/0xd8)
[    2.236046] [<c041ef3c>] (versionhw_init+0xbc/0xd8) from [<c04306e8>] (do_one_initcall+0xfc/0x164)
[    2.245027] [<c04306e8>] (do_one_initcall+0xfc/0x164) from [<c040893c>] (kernel_init+0x98/0x13c)
[    2.253833] [<c040893c>] (kernel_init+0x98/0x13c) from [<c04369c8>] (kernel_thread_exit+0x0/0x8)
[    2.262639] CPU1: stopping
[    2.265358] [<c043b504>] (unwind_backtrace+0x0/0xf8) from [<c0430450>] (do_IPI+0x1d4/0x208)
[    2.273718] [<c0430450>] (do_IPI+0x1d4/0x208) from [<c0435a88>] (__irq_svc+0x48/0xe0)
[    2.281551] Exception stack(0xedc5ff68 to 0xedc5ffb0)
[    2.286606] ff60:                   00000000 a0000093 00000001 00000001 00000000 00000000
[    2.294789] ff80: 874d7380 00000000 c1d7b7e8 413fc090 c0bcbdf8 00000000 c043aeb4 edc5ffb0
[    2.302970] ffa0: c048bcdc c04970c8 20000013 ffffffff
[    2.308032] [<c0435a88>] (__irq_svc+0x48/0xe0) from [<c04970c8>] (tick_nohz_restart_sched_tick+0x1c8/0x1e8)
[    2.317783] [<c04970c8>] (tick_nohz_restart_sched_tick+0x1c8/0x1e8) from [<c0436d08>] (cpu_idle+0x8c/0xfc)
[    2.327447] [<c0436d08>] (cpu_idle+0x8c/0xfc) from [<608d1d14>] (0x608d1d14)
[    2.334502] Rebooting in 1 seconds..

继续修改测试代码:

    printk("----------dump stack\n");
    dump_stack();
    printk("----------panic\n");
    panic("really terrible\n");
    
这次的调试日志简单了很多:

[    1.184385] ----------dump stack
[    1.184437] [<c043b504>] (unwind_backtrace+0x0/0xf8) from [<c041ef28>] (versionhw_init+0xa8/0xd4)
[    1.184504] [<c041ef28>] (versionhw_init+0xa8/0xd4) from [<c04306e8>] (do_one_initcall+0xfc/0x164)
[    1.184566] [<c04306e8>] (do_one_initcall+0xfc/0x164) from [<c040893c>] (kernel_init+0x98/0x13c)
[    1.184626] [<c040893c>] (kernel_init+0x98/0x13c) from [<c04369c8>] (kernel_thread_exit+0x0/0x8)
[    1.184680] ----------panic
[    1.184701] Kernel panic - not syncing: really terrible
[    1.184709]
[    1.391598] [<c043b504>] (unwind_backtrace+0x0/0xf8) from [<c08d5118>] (panic+0x78/0x194)
[    1.399802] [<c08d5118>] (panic+0x78/0x194) from [<c041ef38>] (versionhw_init+0xb8/0xd4)
[    1.407929] [<c041ef38>] (versionhw_init+0xb8/0xd4) from [<c04306e8>] (do_one_initcall+0xfc/0x164)
[    1.416922] [<c04306e8>] (do_one_initcall+0xfc/0x164) from [<c040893c>] (kernel_init+0x98/0x13c)
[    1.425735] [<c040893c>] (kernel_init+0x98/0x13c) from [<c04369c8>] (kernel_thread_exit+0x0/0x8)
[    1.434544] CPU1: stopping
[    1.437266] [<c043b504>] (unwind_backtrace+0x0/0xf8) from [<c0430450>] (do_IPI+0x1d4/0x208)
[    1.445628] [<c0430450>] (do_IPI+0x1d4/0x208) from [<c0435a88>] (__irq_svc+0x48/0xe0)
[    1.453465] Exception stack(0xedc5ff90 to 0xedc5ffd8)
[    1.458521] ff80:                                     00000020 c0a72368 edc5ffd8 00000000
[    1.466705] ffa0: edc5e000 c0ac50a4 c08df314 c0a7ad2c 6040406a 413fc090 00000000 00000000
[    1.474889] ffc0: c043aeb4 edc5ffd8 c0436a24 c0436a28 60000013 ffffffff
[    1.481512] [<c0435a88>] (__irq_svc+0x48/0xe0) from [<c0436a28>] (default_idle+0x24/0x28)
[    1.489699] [<c0436a28>] (default_idle+0x24/0x28) from [<c0436d38>] (cpu_idle+0xbc/0xfc)
[    1.497799] [<c0436d38>] (cpu_idle+0xbc/0xfc) from [<608d1d14>] (0x608d1d14)
[    1.504858] Rebooting in 1 seconds..DDR Version 1.03 20130204

你可能感兴趣的:(debug)