Linux Fu: Easy Kernel Debugging

It used to be that building the Linux kernel was not easy. Testing and debugging were even worse. Nowadays, it is reasonably easy to build a custom kernel and test or debug it using virtualization. But if you still find it daunting, try [deepseagirl’s] script to download, configure, build, and debug the kernel.

The Python program takes command line arguments so you can select a kernel version and different operations. The script can download the source, patch the configuration, build the kernel, and then package it into a Debian package you can boot under qemu. From there, you can test and even debug with gdb. No risk of hosing your everyday system and no need to understand how to configure everything to run.

If you are impatient, the -a option will do everything. For example, to build the 6.2 kernel, you could execute:

python3 easylkb.py -k 6.2 -a

This will take a few minutes and tax your CPU as it builds lots of files using as many CPUs as it can find. Usually, though, you’ll want to change the configuration a bit. You can use the -d flag to simply download the kernel source.

You’ll find the source in the kernel subdirectory of the directory where easylkb lives. There will be a subdirectory for each kernel version you’ve downloaded. There’s also a create-image.sh file that needs the execute bit set on it. If you fail to do this, part of the packaging process will probably fail. If you get an error message, you’ll have to find the copy of the script and set the flag there (for example, linux-6.2/img/create-image.sh).

Source Control

Using xconfig is handy and explains options, too.

If you already have a kernel source tree, the -p option will let you point to that instead of specifying a kernel version with -k. If you specify a version, you can specify the -d option to download it.

Once downloaded, you might want to go to the directory and change the configuration using one of the config options in make. While make config is classic, you’ll probably prefer make menuconfig or make xconfig to make life easier. You can also run make help to see all the options available.

You can specify a configuration file to append to the kernel’s configuration using –kconfig. By default, the example.KConfig file sets some options useful for debugging under qemu.

Building and Running

The next step is to use the -m option to compile the kernel. That will take a bit of time and probably make your machine sluggish. Once it is done, you can use the -i option to build a bootable package for qemu.

The virtual system booting up

The only thing left to do is boot the system up with -r. You’ll see the familiar boot console running in your terminal. The project documentation has some tips on how to ssh into the virtual system and how to use gdb to debug the kernel.

If you just want to do everything, use -a. It will either download the kernel (if you use -k) or pick up the one you specify with -p. You can also do several steps in one run. For example, if you want to configure and make but nothing else, you could use something like:

python3 easylkb -p /tmp/ksource -cm

Wrap Up

Do you have to use this script? Of course not. People have been building kernels for a long time. But it does let you focus on the actual problem at hand and less on having to worry about how to package and boot the kernel for testing.

We aren’t shocked that the script won’t work for kernel 0.01. Need to know what that strange Linux kernel function does? Map it.

2 thoughts on “Linux Fu: Easy Kernel Debugging

  1. If using a custom kernel in a vm is too hard, download this python script to do all the hard work and if it fails, you have to go into the issues page and ask for help. I understand and made my own scripts to pull the latest source and compile a kernel, busybox and boot it in qemu, very handy. I definitely will look for nice tricks.
    Id advice anyone playing arround with the kernel and these type of scripts to also try doing it by hand, see what options they apply and why. Then debugging a script, preferably your own by now (with borrowed code ofcourse) you understand why it may have failed, how to add a feature or how to make it also compile the very early kernels or add another kernel like bsd.

    Too often ppl want a fancy auto builder for something custom, while a premade wouldnhave done it. Use scripts? Yes. Read them and adabt them and understand the majority of it (after a while), YES. Dont just complain if it doesnt work on your machine, check what should make it work first.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.