Date created: 09/08/19 19:46:53. Last modified: 09/08/19 19:54:39

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 <[email protected]>");
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:

[email protected]:~/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'

[email protected]:~/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

[email protected]:~/C/kdebug$ sudo insmod kdebug.ko

[email protected]:~/C/kdebug$ echo $?
0

[email protected]:~/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

[email protected]:~/C/kdebug$ sudo rmmod kdebug

[email protected]:~/C/kdebug$ echo $?
0

[email protected]:~/C/kdebug$ dmesg | tail -n 1
[ 4160.378806] KDebug module removed