updated: 2023-06-21 Wed 00:00

debug tiny Linux kernel


Setup a tiny Linux kernel in Debian Linux.

install packages

# apt-get install libncurses5-dev gcc make git exuberant-ctags bc libssl-dev

download 6.x kernel from www.kernel.org


default tinyconfig

$ make mrproper
$ make tinyconfig

add tty, serial, and 64-bit support.

$ make menuconfig

General setup --> Configure standard kernel features (expert users) --> Enable support for printk
64-bit kernel
Device Drivers --> Character devices --> Enable TTY
Device Drivers --> Character devices --> Serial drivers --> 8250/16550 and compatible serial support
								 --> Console on 8250/16550 and compatible serial port
$ make -j$(nproc)


run

# qemu-system-x86_64 -kernel arch/x86/boot/bzImage -nographic -append "console=ttyS0,115200 nokaslr"

exit from qemu

ctrl+a x


debugging

enable kernel debug symbols using $ make menuconfig

You may also edit .config manually.

CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_KERNEL=y

build new kernel

$ make -j$(nproc)

It might show several options for DWARF config.


run in debug mode

add -s -S options to enable gdb debug mode.

-s  shorthand for -gdb tcp::1234
-S  freeze CPU at startup
# qemu-system-x86_64 -kernel arch/x86/boot/bzImage -nographic -append "console=ttyS0,115200 nokaslr" -s -S

start gdb in another window

$ gdb vmlinux
$ break start_kernel
$ target remote :1234

c to continue


resources:

https://kernelnewbies.org/KernelBuild

https://www.qemu.org/docs/master/system/gdb.html

https://www.kernel.org/doc/html/latest/dev-tools/gdb-kernel-debugging.html

https://weeraman.com/building-a-tiny-linux-kernel-8c07579ae79d