Debugging Raspberry PI kernel module with VisualKernel

This tutorial shows how to debug a basic Linux kernel module on Raspberry PI over JTAG using Visual Studio. Before you begin, follow the Raspberry PI JTAG setup tutorial and the Raspberry PI kernel building tutorial. Also ensure that you have the latest VisualKernel.

  1. Start Visual Studio. Select File->New->Project->VisualKernel->Linux Kernel Module Wizard:
  2. On the first page select Create a new kernel module -> Hello, World :
  3. On the Target selection page specify the following settings:
    • Targeted Linux computer - your Raspberry PI
    • Additional build computer - the build machine from the kernel build tutorial
    • Custom GDB binary - GDB binary from the cross-toolchain from the kernel build tutorial
    • Disable the "Detect and install headers/symbols" option
  4. On the next page select "Install kernel symbols for another kernel":
  5. VisualKernel will ask for a directory on the build machine where the kernel symbols will be stored. Proceed with the default directory or select another one.
  6. In the Kernel Import Wizard select "Specify kernel symbols and sources manually":
  7. On the second page specify the paths to the kernel binary, sources and GDB:
    • Kernel file with symbols: <downloaded kernel source directory>/vmlinux
    • Source directory: <downloaded kernel source directory>
    • Compatible GDB binary: GDB binary from the cross-toolchain from the kernel build tutorial
  8. VisualKernel will now index the in-tree kernel modules and add the kernel to the known kernel list:
  9. Specify the linux source directory (containing vmlinux file) as the kernel header directory and ensure that the target architecture and cross-compiler prefix are set correctly. The cross-compiler prefix is the part of the GCC/GDB file name just before the gcc/gdb part (e.g. for /tmp/my-gdb the prefix will be /tmp/my-):
  10. VisualKernel will test the specified settings by trying to build a basic kernel module:
  11. On the Source Code Access page you can modify the directory where the sources of your module will be stored, or continue with the default one:
  12. On the Debug Settings page enter the following options:
    • Select "Specify GDBServer host/port manually"
    • Leave host name blank
    • Specify port 3333 (default OpenOCD port)
    • Select "Run a GDB stub" and enter OpenOCD location and arguments from the JTAG setup tutorial
    • Specify the working directory for OpenOCD
    • Select "Run a command before detaching" and enter command "mon resume"
  13. Press Finish to complete the wizard. VisualKernel will generate a Visual Studio project for you. Build it with Ctrl-Shift-B:
  14. Before you start debugging ensure that JTAG ports are enabled on your Raspberry PI:Don't forget to run and stop OpenOCD to work around the continue bug. See the JTAG setup tutorial for more details.
  15. Start debugging by pressing F5. The breakpoint will be hit:Note that OpenOCD output is shown in the OpenOCD pane in Visual Studio.
  16. Use the Call Stack to navigate to the relevant parts of the Linux kernel:
  17. Press F10 to step over printk(). Note how the message appears in the kernel output pane:
  18. Press F5 to continue execution. Use the GDB Session window to unload the module: