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.
- 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:
- 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:
- 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:
- VisualKernel will start downloading the kernel package and preparing it to build. This can take 5-10 minutes depending on your Internet connection speed:
- 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:
- Once you click "Next" VisualKernel will start downloading the source code to the Windows machine:
- On the last wizard page select the method you want to use to connect to the kernel (e.g. KGDB):
- 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.
- Once you press "OK", VisualKernel will create your project:
- Build it by pressing Ctrl-Shift-B. Note that the kernel is huge so it may take up to 2-3 hours to build:
- 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:
- Set a breakpoint inside the load_module()
function. Then go to the SSH Console and type the following
commands:sudo rmmod floppyAs you can see, the load_module() function allocates the mod object that stores various information about the module:
sudo modprobe floppy
- 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":
- Add the following field to the end of the struct
- Add the following line to load_module()
after mod is allocated and checked:mod->module_load_time = get_seconds();
- Now we will find the code that manages the /proc/modules file. Search for "modules" (with quotes):
- 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);
- 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:
- Restart your Linux machine so that the new kernel loads.
Start debugging and run the following commands in the SSH
Session window:cd /proc
sudo rmmod floppy
sudo modprobe floppy
cat modules | grep floppy
- 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.