Date created: Sunday, September 8, 2019 7:46:53 PM. Last modified: Friday, January 3, 2020 3:35:02 PM

Linux Debug Levels

Kernel Debug Levels:

#define KERN_EMERG "<0>" /* system is unusable*/
#define KERN_ALERT "<1>" /* action must be taken immediately*/
#define KERN_CRIT "<2>" /* critical conditions*/
#define KERN_ERR "<3>" /* error conditions*/
#define KERN_WARNING "<4>" /* warning conditions*/
#define KERN_NOTICE "<5>" /* normal but significant condition*/
#define KERN_INFO "<6>" /* informational*/
#define KERN_DEBUG "<7>" /* debug-level messages*/

 

printk() Example

kdebug.c:

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

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("James Bensley <jwbensley@gmail.com>");
MODULE_DESCRIPTION("Kernel debug and print examples module");
MODULE_VERSION("1:0.0");


static int __init kdebug_init(void) {
printk(KERN_INFO "KDebug module loaded\n");

printk(KERN_WARNING "Testing KERN_WARNING\n");
printk(KERN_NOTICE "Testing KERN_NOTICE\n");
printk(KERN_INFO "Testing KERN_INFO\n");
printk(KERN_DEBUG "Testing KERN_DEBUG\n");

return 0;

}


void __exit kdebug_unload(void) {

printk(KERN_INFO "KDebug module removed\n");

}


module_init(kdebug_init);
module_exit(kdebug_unload);

 

Makefile:

# Build with: sudo make

obj-m += kdebug.o
KDIR = /lib/modules/$(shell uname -r)/build

#CCFLAGS="-pedantic"
#CXXFLAGS=$CCFLAGS

# The indentation below must be a \t tab character!
all:
make -C $(KDIR) M=$(shell pwd) modules

# The indentation below must be a \t tab character!
clean:
make -C $(KDIR) M=$(shell pwd) clean

 

Output:

bensley@ubuntu-laptop:~/C/kdebug$ sudo make
make -C /lib/modules/4.11.0-041100-generic/build M=/home/bensley/C/kdebug modules
make[1]: Entering directory '/usr/src/linux-headers-4.11.0-041100-generic'
CC [M] /home/bensley/C/kdebug/kdebug.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/bensley/C/kdebug/kdebug.mod.o
LD [M] /home/bensley/C/kdebug/kdebug.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.11.0-041100-generic'

bensley@ubuntu-laptop:~/C/kdebug$ ls -l
total 28
-rw-rw-r-- 1 bensley bensley 687 Sep 8 19:44 kdebug.c
-rw-r--r-- 1 root root 4088 Sep 8 19:49 kdebug.ko
-rw-r--r-- 1 root root 507 Sep 8 19:49 kdebug.mod.c
-rw-r--r-- 1 root root 2488 Sep 8 19:49 kdebug.mod.o
-rw-r--r-- 1 root root 2480 Sep 8 19:49 kdebug.o
-rw-rw-r-- 1 bensley bensley 326 Sep 8 19:41 Makefile
-rw-r--r-- 1 root root 40 Sep 8 19:49 modules.order
-rw-r--r-- 1 root root 0 Sep 8 19:49 Module.symvers

bensley@ubuntu-laptop:~/C/kdebug$ sudo insmod kdebug.ko

bensley@ubuntu-laptop:~/C/kdebug$ echo $?
0

bensley@ubuntu-laptop:~/C/kdebug$ dmesg | tail -n 5
[ 4014.604576] KDebug module loaded
[ 4014.604579] Testing KERN_WARNING
[ 4014.604580] Testing KERN_NOTICE
[ 4014.604581] Testing KERN_INFO
[ 4014.604582] Testing KERN_DEBUG

bensley@ubuntu-laptop:~/C/kdebug$ sudo rmmod kdebug

bensley@ubuntu-laptop:~/C/kdebug$ echo $?
0

bensley@ubuntu-laptop:~/C/kdebug$ dmesg | tail -n 1
[ 4160.378806] KDebug module removed