Debugging Linux Kernel with Visual Studio + VirtualBox

This tutorial shows how to debug a Linux kernel module using Visual Studio and the internal VirtualBox debugger. This method of debugging is faster than using KGDB with a virtual COM port, however it requires installing a development build of VirtualBox.

Before you begin, please install Visual Studio, development build of VirtualBox and the latest VisualKernel.

Then follow the steps below:

  1. Launch Visual Studio. Begin creating a new project with VisualKernel kernel module wizard:
  2. Enter the name and location for your project:
  3. On the first wizard page select “Create a new kernel module” and continue by clicking “Next”:
  4. On the next page select “Setup a new target for running Linux Kernels”. If you already have a VM you would like to debug, select it from the list. You can also select “Download a ready-to-debug image” to automatically install a VM image along with the matching kernel symbols:
  5. If you are using an existing VM, VisualKernel will ask for the SSH settings to connect to the VM. Normally, just using the host name or IP address should work:
  6. If you cannot connect to the VM, double-check that it is using bridged networking linked with your primary network adapter, and that the “cable connected” checkbox is checked:
  7. Press “Next”. VisualKernel will try building a basic kernel module on the target, and will install the missing dependencies if required:
  8. Proceed with the default settings on the next wizard page:
  9. On the last wizard page select “Debug a Virtual Machine”, then select “VirtualBox” and specify the path to your .vbox file:
  10. Press “Finish” to generate your project. Now you can build it using the “Build All” command in Visual Studio:
  11. Due to the way VirtualBox handles memory access, attaching the kernel debugger to a machine that is actively running user-space code will prevent it from setting kernel breakpoints. VisualKernel will attempt to work around it by resuming the VM and requesting another break. You can minimize the need for the workarounds by stopping any user-mode processes with heavy CPU usage until the user-mode CPU utilization drops below 10%:
  12. Set a breakpoint in your module’s init() function and press F5 to begin debugging. VisualKernel will automatically start debugging, load the kernel module and the breakpoint will trigger:You can now use the normal debugging techniques to debug your kernel module.

You can also use VisualKernel to debug VMWare and QEMU virtual machines. See this page for more details.