Debugging Raspberry PI Kernel with VisualKernel

This tutorial shows how to debug a the Linux Kernel on Raspberry PI over JTAG using Visual Studio. Before you begin, follow the basic Raspberry PI Kernel Module tutorial to ensure that your debugging setup works.

  1. Start Visual Studio. Select File->New->Project->VisualKernel->Custom Linux Kernel Wizard:
  2. Specify the same target settings you used in the Kernel Module tutorial:
  3. Select “Automatically download sources” and click “Next”:
  4. VisualKernel will automatically download the Raspberry Pi cross-toolchain and the kernel sources to the machine selected for building:
  5. On the next page of the wizard select the method to access the source code on the build machine. For most scenarios we recommend using direct SSH access. If you want to keep a local copy of the sources, select “Copy sources locally and upload modified files” instead:
  6. Enter the same debug settings that you used when testing the kernel module. Use the “test” button to validate them:
  7. Press Finish to generate the project. VisualKernel will create an advanced kernel project and will begin querying its structure from KBuild:
  8. Once the structure is queried, press Ctrl-Shift-B to build the kernel. The build may stop on an internal compiler error in dwarf2out_frame_debug_adjust_cfa():
  9. To work around this, open both failing files and insert the following code at the beginning of them:
    #pragma GCC optimize("-g0")

  10. Then build the project again. The build will now succeed and VisualKernel will remind you to install the kernel on Raspberry Pi:
  11. Open VisualKernel Project Properties on the Kernel build settings page and click “Install embedded kernel”:
  12. VisualKernel will automatically export the built kernel, transfer it to Raspberry Pi and install it there, including the related modules:
  13. Once the installation is complete, restart Raspberry Pi and ensure the new kernel starts:
  14. Press F5 to start debugging the kernel. VisualKernel will begin analyzing the kernel image and once it is done, the debugging session will automatically start:
  15. Set a breakpoint in the sys_open() function via the Breakpoints window:
  16. The breakpoint will soon hit:If it doesn’t get triggered, try reading any file on your Raspberry Pi using the ‘cat’ command.
  17. Press F11 until you step into do_sys_open(). Hover the mouse around various variables to see their values:
  18. If stepping through the kernel is too slow due to multiple printk() messages, you can disable printk() hooking via VisualKernel Project Properties:
  19. You can edit the kernel configuration by right-clicking on the kernel project and selecting “Configure Kernel Project”:
  20. This will launch the xconfig tool, letting you configure various kernel parameters directly from your Windows machine: