BUG_ON vs WARN_ON macros in Linux Kernel

The most frequently found macro in Linux device drivers is BUG_ON/BUG and WARN_ON

BUG_ON(condition)
or
if (condition)
    BUG()

A quick grep in the drivers folder for BUG_ON( gave 5000 plus count, and for WARN_ON gave more than 6000 count





What does BUG() macro do?

  • Prints the contents of the registers
  • Prints Stack Trace
  • Current Process dies
What does WARN() macro do?
  • Prints the contents of the registers
  • Prints Stack Trace.
Sample Code demonstrating BUG:


#include <linux/kernel.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");
static int test_bug_init(void)
{
    printk(KERN_INFO"%s: In init\n", __func__);
    BUG();
    return 0;
}

static void test_bug_exit(void)
{
    printk(KERN_INFO"%s: In exit\n", __func__);
}

module_init(test_bug_init);
module_exit(test_bug_exit);

Please note, after loading this module using insmod, you cannot unload this. If you try to call rmmod, you will get "Module in use" error




Comments

Post a Comment

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig