关于BUG_ON()的一点笔记

时间:2021-10-04 12:59:56

关于BUG_ON()的一点笔记

最近在看isp1362的驱动经常看到这个BUG_ON()在网上找了些相关资料,现总结如下

先看代码吧:

对于blackfin来说有如下定义(现在没时间学习blackfin体系,还看不懂以后有时间在看了):

作用:一些内核调用可以用来方便标记bug,提供断言并输出信息。最常用的两个是BUG()和BUG_ON()。当被调用的时候,它们会引发oops,导致栈的回溯和错误信息的打印。为什么这些声明会导致 oops跟硬件的体系结构是相关的。大部分体系结构把BUG()和BUG_ON()定义成某种非法操作,这样自然会产生需要的oops。你可以把这些调用当作断言使用,想要断言某种情况不该发生
if (bad_thing)
BUG();
或者使用更好的形式:
BUG_ON(bad_thing);

可以用panic()引发更严重的错误。调用panic()不但会打印错误消息而且还会挂起整个系统。显然,你只应该在极端恶劣的情况下使用它:
if (terrible_thing)
       panic("foo is %ld/n", foo);

 有些时候,你只是需要在终端上打印一下栈的回溯信息来帮助你测试。此时可以使用dump_stack()。它只在终端上打印寄存器上下文和函数的跟踪线索:
if (!debug_check) {
       printk(KERN_DEBUG "provide some information.../n");
       dump_stack();
}
from:http://www.lupaworld.com/bbs/thread-36983-1-8.html


A lots of places in linux kernel we encounter the macro BUG_ON.
BUG_ON macro first checks for the condition and if condition is true then it calls BUG which do a printk of msg and call panic which halts the system.(如果BUG_ON中的条件为真就调用BUG,它输出一些信息,然后调用panic函数挂起系统。)

The unlikely in BUG_ON macro is compiler directive which ask compiler to generate assembly such that the condition mentioned in unlikely isn't going to be met most-of-the time, which reduces jumps and speeds-up things, although if condition mentioned in unlikely met then there will be a long jump and speed will be effected, so must be used carefully. Same is with likely as it is the opposite of unlikely.(据说likely一般是很少用的)

from: http://knol.google.com/k/vivek-bhadra/linux-debugging-bug-on/3c84lj4klzp0d/14#

 

这个unlikely()使我想到了之前看到过的一个宏__cold,原来这两个是同一个东西,__cold是4.3版本之后的gcc才支持的。

__cold的定义


 

 

hot
The hot attribute is used to inform the compiler that a function is a hot spot of the compiled program. The function is optimized more aggressively and on many target it is placed into special subsection of the text section so all hot functions appears close together improving locality.
When profile feedback is available, via -fprofile-use, hot functions are automatically detected and this attribute is ignored.
The hot attribute is not implemented in GCC versions earlier than 4.3.
cold
The cold attribute is used to inform the compiler that a function is unlikely executed. The function is optimized for size rather than speed and on many targets it is placed into special subsection of the text section so all cold functions appears close together improving code locality of non-cold parts of program. The paths leading to call of cold functions within code are marked as unlikely by the branch prediction mechanism. It is thus useful to mark functions used to handle unlikely conditions, such as perror, as cold to improve optimization of hot functions that do call marked functions in rare occasions.
When profile feedback is available, via -fprofile-use, hot functions are automatically detected and this attribute is ignored.
The cold attribute is not implemented in GCC versions earlier than 4.3.

from: http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Function-Attributes.html