Building and modifying Linux Kernel with Visual Studio

This tutorial shows how to build the Linux kernel, add a small modification to it and debug the new kernel using Visual Studio with VisualKernel.

We will use a Kubuntu machine to build the kernel source package provided by the distribution and then modify the kernel so that it shows how much time has elapsed since loading of each kernel module.

  1. Before we begin modifying the kernel we should create a Visual Studio project for it. Open Visual Studio and start the Custom Linux Kernel Wizard in the VisualKernel folder:
  2. On the first wizard page select the remote machine you want to target. Note that it's recommended to use the root account to avoid sudo prompts each time you install the kernel:
  3. On the next page select "automatically download sources for the current kernel". This will work for the most popular Linux distributions. If you are not running one of them, you can also specify a pre-configured kernel source directory:
  4. VisualKernel will start downloading the kernel package and preparing it to build. This can take 5-10 minutes depending on your Internet connection speed:
  5. Once the kernel source package is setup, the wizard will ask you for a directory on the Windows machine where the sources will be copied. Note that Linux Kernel source is huge so ensure you have enough free space on your disk:
  6. Once you click "Next" VisualKernel will start downloading the source code to the Windows machine:
  7. On the last wizard page select the method you want to use to connect to the kernel (e.g. KGDB):
  8. Press "Finish" to complete the wizard. VisualKernel will ask you which directories you want to add to the Solution Explorer. Note that adding the entire kernel will result in a huge project that will take up to 15 minutes to open. So it is recommended to only select the subdirectories you are intending to modify. In this example we only select the 'kernel' subdirectory:Note that files from other files can be added to the project later via the "Add File" command. You can also modify some files inside the kernel tree that are not added to the project - VisualKernel will pick them automatically.
  9. Once you press "OK", VisualKernel will create your project:
  10. Build it by pressing Ctrl-Shift-B. Note that the kernel is huge so it may take up to 2-3 hours to build:
  11. Now we will test the debugging. Reboot your Linux machine so that the new kernel is loaded. Then press F5 to start debugging. If you are debugging this kernel for the first time VisualKernel will take some time to index the in-tree kernel modules:
  12. Set a breakpoint inside the load_module() function. Then go to the SSH Console and type the following commands:
    sudo rmmod floppy
    sudo modprobe floppy
    As you can see, the load_module() function allocates the mod object that stores various information about the module:
  13. Now we will modify the kernel to store the load time of each module. Right-click on the module word and select "Go to Definition":
  14. Add the following field to the end of the struct module:
    time_t module_load_time;
  15. Add the following line to load_module() after mod is allocated and checked:
    mod->module_load_time = get_seconds();
  16. Now we will find the code that manages the /proc/modules file. Search for "modules" (with quotes):
  17. Go to definition of modules_open() and then to definition of modules_op and m_show(). Add the following line before the last call to seq_printf():
    seq_printf(m, " [loaded %d seconds ago]",
               get_seconds() - mod->module_load_time);
  18. Press Ctrl-Shift-B to build the modified kernel. Note that VisualKernel will only re-upload the 2 source files you modified. However as the modified module.h is used by many other files, rebuilding the kernel will take a long time:
  19. Restart your Linux machine so that the new kernel loads. Start debugging and run the following commands in the SSH Session window:
    cd /proc
    cat modules
    sudo rmmod floppy
    sudo modprobe floppy
    cat modules | grep floppy
  20. You will see that each line now contains the information about the load time. Set a breakpoint on the added seq_printf() call and re-run the last command again:You will be able to step through your new code and examine the variable values just like when debugging normal programs.

You can now follow this tutorial to create and debug a kernel module for your custom kernel.