在 Linux 内核中使用断言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6359700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using assertion in the Linux kernel
提问by macindows
I have a question about assert()
in Linux: can I use it in the kernel?
我assert()
在 Linux 中有一个问题:我可以在内核中使用它吗?
If no, what techniques do you usually use if, for example I don't want to enter NULL pointer?
如果不是,您通常使用什么技术,例如我不想输入 NULL 指针?
采纳答案by Nemo
The corresponding kernel macros are BUG_ON
and WARN_ON
. The former is for when you want to make the kernel panic and bring the system down (i.e., unrecoverable error). The latter is for when you want to log something to the kernel log (viewable via dmesg
).
相应的内核宏是BUG_ON
和WARN_ON
。前者用于当您想让内核崩溃并使系统停机(即,不可恢复的错误)时。后者用于当您想将某些内容记录到内核日志时(可通过 查看dmesg
)。
As @Michael says, in the kernel, you need to validate anything that comes from userspace and just handle it, whatever it is. BUG_ON and WARN_ON are to catch bugs in your own code or problems with the hardware.
正如@Michael 所说,在内核中,您需要验证来自用户空间的任何内容并处理它,无论它是什么。BUG_ON 和 WARN_ON 用于捕获您自己代码中的错误或硬件问题。
回答by Michael Foukarakis
No. Unless you're working on the kernel core and rather on a module, you should do your best to never crash (technically, abort()
) the kernel. If you don't want to use a NULL pointer, just don't do it. Check it before using it, and produce an error log if it is.
不。除非您正在处理内核核心而不是模块,否则您应该尽力避免(从技术上讲abort()
)内核崩溃。如果您不想使用 NULL 指针,请不要这样做。使用前检查一下,如果是,则生成错误日志。
The closest thing you mightwant to do if you're actually handling a fatal case is the panic()
function or the BUG_ON
and WARN_ON
macros, which will abort execution and produce diagnostic messages, a stack trace and a list of modules.
如果您实际上正在处理致命情况,您可能最想做的事情是panic()
函数或BUG_ON
andWARN_ON
宏,它们将中止执行并生成诊断消息、堆栈跟踪和模块列表。
回答by sleske
One option would be to use the macro BUG_ON()
. It will printk
a message, and then panic()
(i.e. crash) the kernel.
一种选择是使用宏BUG_ON()
。它将printk
一条消息,然后panic()
(即崩溃)内核。
http://kernelnewbies.org/KernelHacking-HOWTO/Debugging_Kernel
http://kernelnewbies.org/KernelHacking-HOWTO/Debugging_Kernel
Of course, this should only be used as an error handling strategy of last resort (just like assert
)...
当然,这应该只用作最后的错误处理策略(就像assert
)...
回答by shodanex
Well, dereferencing null pointer will produce an oops, which you can use to find the offending code. Now, if you want to assert() a given condition, you can use
好吧,取消引用空指针会产生一个 oops,你可以用它来找到有问题的代码。现在,如果你想 assert() 一个给定的条件,你可以使用
BUG_ON(condition)
A less lethal mechanism is WARN_ON, which will produce a backtrace without crashing the kernel.
一种不太致命的机制是 WARN_ON,它会在不使内核崩溃的情况下生成回溯。
回答by krazycoder
BUG_ON()
is the appropriate approach to do it. It checks for the condition to be true and calls the macro BUG()
.
BUG_ON()
是适当的方法来做到这一点。它检查条件是否为真并调用宏BUG()
。
How BUG()
handles the rest is explained very well in the following article:
BUG()
以下文章很好地解释了如何处理其余部分:
回答by LIUB
I use this macro, it uses BUG() but adds some more info I normally use for debugging, and of course you can edit it to include more info if you wish:
我使用这个宏,它使用 BUG() 但添加了更多我通常用于调试的信息,当然,如果您愿意,您可以编辑它以包含更多信息:
#define ASSERT(x) \
do { if (x) break; \
printk(KERN_EMERG "### ASSERTION FAILED %s: %s: %d: %s\n", \
__FILE__, __func__, __LINE__, #x); dump_stack(); BUG(); \
} while (0)