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:
- Launch Visual Studio. Begin creating a new project with VisualKernel kernel module wizard:
- Enter the name and location for your project:
- On the first wizard page select “Create a new kernel module” and continue by clicking “Next”:
- 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:
- 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:
- 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:
- Press “Next”. VisualKernel will try building a basic kernel module on the target, and will install the missing dependencies if required:
- Proceed with the default settings on the next wizard page:
- On the last wizard page select “Debug a Virtual Machine”, then select “VirtualBox” and specify the path to your .vbox file:
- Press “Finish” to generate your project. Now you can build it using the “Build All” command in Visual Studio:
- 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%:
- 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.