Building a Linux Kernel module using a second machine

This tutorial demonstrates how to build and debug a Linux kernel module using a second Linux machine. Unlike the normal setup when the module is built on the same machine where it will be loaded, this setup involves 2 Linux machines:

  • The build machine that will be used to build the module and run gdb when debugging it
  • The target machine that will be used to load the kernel module

The two-machine setup is advantageous when the target machine is slow and building the kernel module on it would take too much time. However, this method puts several additional restrictions compared to the one-machine method:

  • In order for the automatic header setup to work, both machines should be running the same Linux distro
  • The build machine should have enough free space (1-5 GB) to store a copy of Linux sources and symbols
  • The connection between the two machines should be fast enough to transfer symbols between them

Before you begin with this tutorial, follow one of our basic tutorials to ensure that the simpler one-machine setup works. Then proceed with the steps below:

  1. Start the VisualKernel kernel module wizard:
  2. Continue with default settings on the first page:
  3. On the target page select “use an additional Linux computer to build the module”. Then select the target machine (that will run the module) and the build machine (that will run gcc/gdb):
  4. When you press “Next” VisualKernel will ask for a directory on the build machine where the Linux kernel symbols and sources from the target machine will be cached:
  5. Press OK. VisualKernel will download the kernel sources and symbols from the target machine to the build machine:
  6. On the next page you can change the location of the source files on the build machine or continue with the default settings:
  7. On the last page select the debugging method to use for debugging the kernel:
  8. Press “Finish” to create the project. Build it by selecting Build->Build Solution:
  9. Set a breakpoint in the init function and hit F5 to begin debugging:From this point the debugging experience will be the same as for the one-machine setup. Press Shift-F5 when you are ready to stop debugging.
  10. If you decide to remove the cached symbol/source files from the build machine, select Tools->Manage Linux Kernel Symbols:
  11. Select the build machine in the Linux Kernel Symbol Manager window. You can use the buttons to the right to delete the symbol cache or get files for another kernel version: