BUG_ON && WARN_ON
Linux中BUG_ON,WARN_ON用于调试,比如
14 #define BUG_ON(condition) do { /
15 if (unlikely((condition)!=0)) /
16 BUG(); /
17 } while(0)
如果觉得该condition下是一个BUG,可以添加此调试信息,查看对应堆栈内容
具体的BUG_ON最终调用__bug
__bug
{
*(int*)0=0;
}
从而地址非法访问,
例如在你的驱动中调用BUG_ON(1),dmesg会出现下面类似信息
[ 19.360000] Unable to handle kernel NULL pointer dereference at virtual address 00000000
[ 19.360000] pgd = c0004000
[ 19.360000] [00000000] *pgd=00000000
[ 19.360000] Internal error: Oops: 817 [#1] PREEMPT
[ 19.360000] Modules linked in: 。。。。
[ 19.360000] CPU: 0 Tainted: P (2.6.31 #201)
[ 19.360000] pc : [
[ 19.360000] sp : c705ff50 ip : c705ff50 fp : c705ffc4
[ 19.360000] r10: 00000000 r9 : 00000000 r8 : 00000000
[ 19.360000] r7 : bf04c0b0 r6 : c705e000 r5 : c7908c80 r4 : 00000000
[ 19.360000] r3 : 00000000 r2 : c705ff40 r1 : c705ff84 r0 : 00000000
[ 19.360000] Flags: nZcv IRQs on FIQs on Mode SVC_32 ISA ARM Segment kernel
[ 19.360000] Control: 0005317f Table: 87150000 DAC: 00000017
[ 19.360000] Process ts_thread (pid: 230, stack limit = 0xc705e270)
[ 19.360000] Stack: (0xc705ff50 to 0xc7060000)
[ 19.360000] ff40: c0056590 c00560e8 00000000 c003466c
[ 19.360000] ff60: c781c01c c7908c80 c705ff8c c705ff78 c00565dc c005656c 00000000 c7030480
[ 19.360000] ff80: c705ffc4 00000000 c7908c80 c0051238 c705ff90 c705ff90 c705ffc4 c705ffcc
[ 19.360000] ffa0: c7053dc0 00000000 bf04c0b0 00000000 00000000 00000000 c705fff4 c705ffc8
[ 19.360000] ffc0: c0050fc4 bf04c0c0 00000000 00000000 c705ffd0 c705ffd0 00000000 00000000
[ 19.360000] ffe0: 00000000 00000000 00000000 c705fff8 c003e0d8 c0050f4c 80376031 80376431
[ 19.360000] Backtrace:
[ 19.360000] Function entered at [
[ 19.360000] Function entered at [
[ 19.360000] r7:00000000 r6:00000000 r5:00000000 r4:00000000
[ 19.360000] Code: 0a000001 eb4031aa eaffffc0 e3a03000 (e5833000)
[ 19.360000] ---[ end trace 3b848f3e016c12c9 ]---
函数的调用流程为,
代码流程,
fault.c
__do_kernel_fault--------[ 19.360000] Unable to handle kernel NULL pointer dereference at virtual address 00000000
traps.c
die--->__die--->__show_regs / dump_mem
die -----[ 19.360000] Internal error: Oops: 817 [#1] PREEMPT(支持抢占)
而WARN_ON则是调用dump_stack,打印堆栈信息,不会OOPS
23 #define WARN_ON(condition) do { /
24 if (unlikely((condition)!=0)) { /
25 printk("Badness in %s at %s:%d/n", __FUNCTION__, __FILE__, __LINE__); /
26 dump_stack(); /
27 } /
28 } while (0)
具体的调用情况与中断有关,可以通过linux中断机制来学习。