Wednesday, April 16, 2014

kdb: Debugging Virtualbox VM's linux kernel with built-in debugger interface


In my earlier post - Linux kernel debuggers: Overview - I covered the basic features of kdb. In this post, I will provide steps to use kdb. 


kdb usage - HowTo


Here are the steps that worked for me to debug a VirtualBox Ubuntu VM.



  • Recompile the kernel in the VM with kgdb & kdb options enabled. We can find out whether they are already compiled in by grep-ing for KDB & KGDB compile macros: 

root@babu-VirtualBox:~# egrep "(KDB|KGDB)" /boot/config-3.13.5
CONFIG_SERIAL_KGDB_NMI=y
CONFIG_HAVE_ARCH_KGDB=y
CONFIG_KGDB=y
CONFIG_KGDB_SERIAL_CONSOLE=y
# CONFIG_KGDB_TESTS is not set
CONFIG_KGDB_LOW_LEVEL_TRAP=y
CONFIG_KGDB_KDB=y
CONFIG_KDB_KEYBOARD=y
CONFIG_KDB_CONTINUE_CATASTROPHIC=0
root@babu-VirtualBox:~# 


  • Boot the VM with keyboard based kdb front end enabled in boot parameters. To do so, add "kgdboc=kbd" to the boot options.

                     
    • kdb front end can also be used over a serial port. To do so, add "kgdboc=ttyS0,115200" to the boot options.
    • kdb frontend can be enabled on keyboard as well as serial port. To do so, add "kgdboc=kbd,ttyS0,115200" to the boot options.
  • Then, allow the booting process to continue & let the VM completely boot.
  • Get into text mode from GUI mode: Ctrl+Alt+F1 (fn+Ctrl+Option+F1 in Mac)
  • Even if kdb is not enabled in boot parameters, kdb can be enabled at run time in sysfs interface.

  • To enable kdb at run-time:
    root@babu-VirtualBox:~# cat /sys/module/kgdboc/parameters/kgdboc
    ttyS0,115200
    root@babu-VirtualBox:~# 

    To disable kdb at run-time:
    root@babu-VirtualBox:~# echo "" > /sys/module/kgdboc/parameters/kgdboc
    root@babu-VirtualBox:~# 

    To check whether kdb is enabled/disabled:
    root@babu-VirtualBox:~# cat /sys/module/kgdboc/parameters/kgdboc
    ttyS0,115200
    root@babu-VirtualBox:~# 

  • Now, to bring the control to kdb, type "echo g > /proc/sysrq-trigger" 


                     
  • You can now place breakpoints, single stepping, read/write memory, print backtrace, etc.
  • For testing purposes, I then crash the system. In case of crash, the debugger automatically gets the control for further inspection.

                                    


Hope this helps.

No comments:

UA-48797665-1