notes on linux kernel debugging

personal notes on setting up kgdb with virtual box

Instructions for compiling linux-4.4.0 on a Ubuntu 16.04 virtualbox image. Disk space usage is somewhat large, recommend 30GB+ if using fixed size hd.

Required packages, and compiling the kernel. Accept default configuration (hold down enter key on make oldconfig if you are lazy) which should include ‘Compile kernel with debug info’ under the menu config.

sudo apt-get install linux-source libncurses5-dev libssl-dev
mkdir kernel
cd kernel
tar xjf /usr/source/linux-source-4.4.0/linux-source-4.4.0.tar.bz2
make oldconfig
make menuconfig

Manually apply this patch. The following steps will take an 2+ hours so go read a blog on local privilege escalation.

make modules
sudo make modules install
sudo make install

Now update grub to boot with the new kernel. The newly installed kernel lives in a separate directory from your old kernel, and grub won’t automatically start using your new build until you properly configure it.

grep -A300 submenu /boot/grub/grub.cfg | grep menuentry

Locate the newer kernel and concatenate with ‘>’ the uuid of the advanced options ‘gnulinux-advanced-…’ with the uuid of the kernel version you want to boot. It should look something like:


Now edit /etc/default/grub and replace the GRUB_DEFAULT with your concatenated string including the double-quotes. Also comment out (‘#’) the GRUB_HIDDEN_DEFAULT line so that the bootloader will show when you restart.

sudo vim /etc/default/grub #make necessary edits
sudo update-grub

After reboot ensure that you can boot into the new kernel (uname -a to check). Bask in the light of your monitor after successfully building your first kernel :-) Move onto the next step.

Set up a shared permanent folder on your virtualbox guest that contains the linux sources, gdb will use this to find the kernel’s symbols. Set up a serial port on virtualbox with settings:

	- Port Number: COM1
	- Port Mode: Host Pipe
	- Connect to existing pipe SHOULD NOT be selected
	- Path/Address: /home/youruser/serial

Reboot your guest and edit (‘e’) the GRUB entry pointing at the new kernel. Add the following at the end of the linux line:

kgdboc=ttyS0,115200 kgdbwait

After making the edit, boot the entry with the changes (‘Ctrl-x’) you’ll be greeted with the following prompt:

kgdb: Waiting for connection from remote gdb

On your host machine run the following

socat -d -d /home/youruser/serial PTY:

Leave the command running and note the /dev/pts/ file the pseudoterminal is connected to. Run the following to attach GDB from your host:

cd /your_shared_folder_containing_linux_source/
gdb ./vmlinux
target remote /dev/pts/<number>

You are now debugging the virtual kernel and can continue normal execution of the guest with ‘c’.