web123456

Reading/Modifying/Commenting Linux Kernel Source Code with Clion

preamble

Actually.bootlinis a good listening to read the source code of the tool, can be very convenient to help us check the function, macro definitions, references and so on. And is based on the browser, the configuration of our local machine does not have any high requirements.

But if you want to do some comments, changes, then we have to download the source code to the local, this time we may need other tools.

ClionThe C/C++ IDE I'm most familiar with, I really like the Jetbrains family of IDEs, so using Clion to read/comment/modify the source code became my first choice, and sadly, theLinux The kernel is passed through theMakefileThen Clion is using CMake, so if we want to read the source code with the help of Clion, we need to generate the Cmake file for the Linux kernel!

So far, it is possible to successfully implement the use of Clion to read/comment/modify the source code, but it is not possible to successfully compile it, so the compilation still has to be done with make, but this is harmless!

Configuration Requirements

Clion requires a lot of cached kernel files, so we must ensure that the machine has enough RAM and crank up Clion's heap memory configuration, with at least 8G of RAM on the machine and Clion heap memory adjusted to at least 4G. Configuration method:

Click "Help->Change Memory Settings" on the status bar of CLion, and then modify the "MAX Heap Size", as shown in the figure below, I adjusted it to 8G, and my machine has 24G. then reboot the configuration to take effect!

Source code preparation

Installation of tools necessary for compilation

sudo apt update && sudo apt upgrade
sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
  • 1
  • 2

To download the source code, it is best to go toThe Linux Kernel ArchivesDownload the zip of the source code and unzip it!

tar -zxf linux-5.14.
cd linux-5.14.2
  • 1
  • 2

Configure the kernel using the local configuration file

cp /boot/config-$(uname -r) .config
make menuconfig
  • 1
  • 2

The above process is actually the steps to compile the kernel.

Attention:
Using ubuntu's .config directly may encounter the following error when compiling:

make[1]: *** No rule to make target 'debian/certs/benh@', needed by 'certs/x509_certificate_list'
  • 1

At this point you need to edit the .config a bit and set CONFIG_SYSTEM_TRUSTED_KEYS to null:

CONFIG_SYSTEM_TRUSTED_KEYS = ""
  • 1

generating

Install the bear tool, bear can generate the compile_commands.json file according to the process of tracking make compilation, and then we are through other tools through the compile_commands.json to finally generate the

sudo apt-get install bear
  • 1

Use bear, make to compile the kernel. The latest bear should require a "-" between bear and make. make's "-j12" enables 12 threads to be compiled at once, depending on the number of cores on your machine, which can take a long time!

bear make  -j12
  • 1

2020 Updated September 21
Before that I had been usingmake -j12to compile the kernel, which also takes up to 30 minutes on my machine with 12 cores, and it wasn't until the other day that I realized that this is actually pretty stupid, because themake -j12In fact, the default compilation of all the content, but if we just compile the kernel, only need to compile vmlinux can be, later in order to use qemu to debug, in addition to bzImage on the line, that is, themake -j12 vmlinux bzImageIn this way, the compilation takes less than 5 minutes and the final Cmakea file is only about 5MB, so Clion can eliminate the need for such a high memory allocation!

clone (loanword)kernel-grok project

cd ~
git clone /habemus-papadum/kernel-grok
  • 1
  • 2

Go back to the kernel directory and generate

cd linux-5.14.2
~/kernel-grok/generate_cmake  ## creates 
  • 1
  • 2

modifications

This time we need to manually add some parameter configurations

Add the following to the very beginning of the file:

cmake_minimum_required(VERSION 2.8.8)
project(kernel)

set(SYSROOT sysroot)
SET(CMAKE_C_COMPILER "gcc")
set(CMAKE_C_STANDARD 90)
set(CMAKE_C_FLAGS  ${CMAKE_C_FLAGS} " --sysroot=${SYSROOT}" )

include_directories("include")
include_directories("include/uapi")
include_directories("arch/x86/include")
include_directories("arch/x86/include/uapi")
include_directories("arch/x86/include/generated")
include_directories("arch/x86/include/generated/uapi")

add_definitions(-D__KERNEL__)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

The additions mainly include:

  1. To modify sysroot, you need to first create an empty directory named sysroot in the same directory as the kernel, and then set the compilation parameter "-sysroot=${SYSROOT}", this is because sysroot is actually the location of the C standard header files, which the kernel obviously does not need, and their existence will interfere with the clion's search for the correct header file, creating an unnecessary conflict.
  2. Add the path to the header file, if you do not do this step, then clion will be a large number of errors, suggesting that the header file can not be found or variables, constants, functions and so on. I have repeatedly tested the contents of the specific additions, add these contents to basically ensure that the header file to find the correct, of which the last four are related to the architecture, x84_64 can be used without fear!
  3. Using C90's C standard, at first I used Clion's default C standard, and there has been no problem, but yesterday all of a sudden there were a large number of errors, not to exclude that because I upgraded the latest Clion 2021.2.2 , and finally added theset(CMAKE_C_STANDARD 90)It's back to normal.

Launch CLion

Click on the status bar "File->Open", then select the one we created, then select "Open as Project".

Clion will then automatically load theCmake, currently it will eventually fail to load because this method is not used to compile the kernel!

At this time to view the kernel source code, normal is not any red error message, can be normal for jumping andauto-complete

concern

In addition to not being able to compile, there is also a problem with the macro definitions for the compilation options, which do not display properly:

Above is a piece of code in task_struct, clion's code display indicates that CONFIG_MEMCG could not be found, and therefore memcg_data is not defined, but holding down Ctrl it is possible to jump to the definition of CONFIG_MEMCG, which is in include/generated/, which is very strange!