BUG_ON&&WARN_ON&BUILD_BUG_ON

转载:http://wenx05124561.blog.163.com/blog/static/124000805201223112811490/

 

一.BUG_ON

Linux中BUG_ON,WARN_ON用于调试,比如

#define BUG_ON(condition) do { /
         if (unlikely((condition)!=0)) /
                 BUG(); /
 } 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

 

函数的调用流程为

代码流程,

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

 

二.WARN_ON

而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)

三.BUILD_BUG_ON

#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))

 

BUILD_BUG_ON宏中的condition如果为真就会报错。假设条件为真,则BUILD_BUG_ON变化为:(void) sizeof (char[-1]),这种语法是在难理解,因为char[-1]本来就是非法的。结果导致编译错误。

 

例子:

#define condition 0

static int __init main_init(void)

{         

            printk("in %s function\n", __func__);

            BUILD_BUG_ON(condition);/*if the macro "confition" is not zero, the program can not be compiled to success. so the BUILD_BUG_ON used for the build of program*/

            WARN_ON(!condition);/*if the macro "confition" is not zero, there will dump the satck information for this program. so the WARN_ON used for debugof program*/

 

            BUG_ON(condition);  /*if the macro "confition" is not zero, The kernel will occur an Oops errro "Unable to handle kernel NULL pointer dereference at virtual address 00000000". so the BUG_ON used for ending the bug program*/

 

            return 0;

}

你可能感兴趣的:(Build)