Various debugging technique are available for Linux Kernel Debugging. Most common of them is adding log and printing them using printk. This post list various technique which are useful for debugging a issue.
Adding Additional Log
printk is similar with printf on C standard library. It can be called from anywhere in the kernel at any time, from interrupt or process context. It has below log levels
- KERN_EMERG (0) : Emergency condition such as system is hang.
- KERN_ALERT (1) : Problem that requires immediate attention
- KERN_CRIT (2) : Critical condition
- KERN_ERR (3) : Error
- KERN_WARNING (4) : Warning
- KERN_NOTICE (5) : Normal
- KERN_INFO (6) : Informational message
- KERN_DEBUG (7) : Debugging message
- KERN_DEFAULT (d) : Default kernel loglevel
- KERN_CONT : Continued line of printout
Based on the loglevel, the kernel may print the message. To determine your current console_loglevel
cat /proc/sys/kernel/printk 4 4 1 7 current default minimum boot-time-default
To change console_loglevel, execute use below command
echo 8 > /proc/sys/kernel/printk
If is set to 8, all messages, including debugging ones, are displayed. Another way to change the console log level is to use dmesg with the -n parameter
dmesg -n 7
Below code adds additional log
printk(KERN_ERR "Error, return code: %d\n",ret);<br>
Add Debugging Configuration
Linux supports various configuration options for debugging Linux Kernel. Some of these options are not supported on all the platforms. To makes other debugging options available, enable below configuration
addr2line translates addresses and returns human friendly details such as file name and line number. Given an address, it pass executable. It uses the debugging information present in the executable to decode the address.
addr2line -f -e vmlinux 0xffffffff85077934
gdb can also be used for decoding address.
gdb -q vmlinux list *(0xffffffff85077934)
Debugging with gdb
GDB can be used to debug the kernel. In the below example, we are running gdb on uncompressed kernel image vmlinux.
gdb -q vmlinux /proc/kcore
KGDB patch enables gdb to debug kernel remotely with serial cable. To enable KGDB on your kernel, enable below configuration
CONFIG_FRAME_POINTER=y CONFIG_KGDB=y CONFIG_KGDB_SERIAL_CONSOLE=y CONFIG_KGDB_KDB=y CONFIG_KDB_KEYBOARD=y