Kernel debugging using KGDB
Draft by yanghui
KGDB is used as a source level debugger for Linux kernel. It is used along with GDB to debug a Linux kernel. The expectation is that GDB can be used to break into the kernel to inspect memory , variables and look through call stack information similar to the way an application developer would use GDB to debug an application. It is possible to place breakpoints in kernel code and perform some limited execution stepping
Tow machines are required for using KGDB. One of these machine is a development machine and the other is the target machine. The kernel to be debugged runs on the target machine. The development runs a instance of GDB against the vmlinux file which contains the symbols. In GDB the developer specifies the connection parameters to KGDB.
A serial port is dedicated for the connection between KGDB and GDB running on thedevelopment host system, and an optional second serial can be used as a console. Systems that have only one serial port make GDB somehow more cumbersome to use. This document describes the method using one serial port only and takes the TVOS system as an example.
1)A development machine with Ubuntu system installed
2)The development machine has at least a serial port or can support usb-to-serial converter
3)A target machine with TVOS system installed
4)The target machines has at least a serial port
5)A RS-232 cable and a USB to Serial port converter if needed.
3.Compiling a Kernel
Check the .config file in the Linux kernel source code directory. For TVOS, it’s /usr/src/linux-184.108.40.206.
$ cd /usr/src/linux-220.127.116.11
Make sure the these symbols are set as following .
;#CONFIG_DEBUG_RODATA is not set
If these symbols are set as expected, run make bzImage to generate the vmlinux file . This file contains the kernel image symbols which will be used by GDB to locate the source code.
$ make bzImage
When this command finishes, a vmlinux file could be found in the kernel source code directory.
If these symbols aren’t set as expected , the kernel image and the kernel modules should be updated. Run the following commands
$ make $rm –rf /lib/firware/*
$rm –rf /lib/modules/18.104.22.168
4.Compiling a kernel module
The linux kernel is compiled with the optimization option –O2 by default. This option is
a must and you can’t change that. This makes some difficulties for GDB to do debugging. But
Some kernel modules can be compiled with –O0 option.
To compile a kernel module with a –O0 option, run the following commands. Let’s take
compiling the via.ko kernel module as an example
1.Change the –O2 option to –O0
Open the Makefile in this directory, and change all the –O2 options to –O0
2.build the via module
$cd /sw/s3gdrv/VX900/kernel/drm/via/ $ make
3.replace the via.ko in system with the newly built module
$ cp ./via.ko /lib/modules/22.214.171.124/kernel/drivers/gpu/drm/via/
5.Setup the KGDB debug environment
1.place the source code and object file at the right location
Suppose that you doing the GDB debugging in a directory named Debug on the
Copy the Linux kernel source code to the Debug directory
Copy the kernel modules source code that to be debugged to the Debug directory
Copy the vmlinux Image generated just now to the Debug directory
Copy the kernel module , for example, via.ko, to the Debug directory
2.Connect the target machine with the develop machine using a serial cable
3.Setup the serial port parameters on development machine
On development machine, Minicom is used as the serial port communication program.
If this program can’t be found on the machine, run $sudo apt-get install minicom to
install this software.
$ sudo minicom -s
Select Serial port setup
Set the serial port parameters according to the value in the below picture. The serial Device option should be set to the device name that is used in your system.
Save the configuration by selecting the Save setup as dfl option.Exit the configuring
Start the Minicom program to communicate with the target machine $ sudo minicom
Then the following message will be displayed.
4.Set the grub configuration for the target machine Add the following options to the grub option of the target machine
****** vmalloc=512M console=tty0 console=ttyS0, 115200 KGDBoc=ttyS0,115200
Then restart the target machine
During the target machine’s booting, booting messages could be seen on the serial console, If no
messages there, check the serial port can work normally or not.6.KGDB Debugging
1.Install the kernel modules
When the target system finishes booting, install the kernel modules
$ modprobe viafb
$ modprobe via
2.Get the .text and .bss and .data section address of the kernel module on which is debugged.
Let’s Take via.ko as an example.
3.Enter the kdb debug mode.
There are two ways you can enter the kernel debugger manually;When logged in as root or with a super user session you can run:
echo g > /proc/sysrq-trigger
Press and hold down: Alt
Press and release the key with the label: SysRq
Press and release: g
On the development machine’s serial console ,you can see the output like this
4.make the target machine leave kdb debug mode to KGDB mode
On the console , input KGDB , to make the target machine leave kdb debug mode to KGDB mode
At this time, the kernel debugger on the target machine is waiting for the GDB to connect.5.Quit the minicom.
Because a serial port can only be used by one program at a time, this serial port will be used by GDB later. In the serial console , press CTRL-A X , then select leave the Minicom6.start GDB program
On the development machine. Enter the Debug directory and start the GDB program
$ cd /your/debug/directory/Debug
$ GDB ./vmlinux
On the GDB shell, set the following GDB configuration
(GDB) set remotebaud 115200
(GDB) set remoteflow 0
# set the source code searching directory
(GDB) directory /your/debug/directory/Debug/linux-126.96.36.199
(GDB) directory /your/debug/directory/Debug/via
#add the kernel module symbol , use the address gotten early
(GDB)add-symbol-file /your/debug/directory/Debug/via.ko 0xf0d12000 –s .data 0xf0d1cee0 –s .data 0xf0d36db4
#connect to the debugger of the target machine
$target remote /dev/ttyUSB0
The following messages are displayed on the GDB shell
Now , you can do the kernel mode debug just as what you do with a user mode application.