Skip to content

Optional: Get Around in the Source Code Tree

Warning

This task is for your self-exploration only. You are not required to submit any answers for this task. It is not graded and does not contribute to your final mark.

In this task, we introduce how to navigate the Linux kernel source code using clangd and VSCode. With these tools, you can easily browse the kernel source code and focus on active parts of the code. That is, code disabled by the kernel configuration will appear greyed out. This is different from the Elixir Cross Referencer, which shows all the code without considering the specific kernel configuration.

Several other tools are available to help navigate the kernel source code. You may check this page to see if they better suit your needs.

clangd is a language server that indexes the source code and provides features like code completion, navigation, and refactoring. You may check this page to see what you can do with it on VSCode.

If you prefer using other editors or IDEs, you can refer to this page for a list of other editors/IDEs compatible with clangd.

Below is a brief guide to setting up clangd and VSCode. For more detailed instructions, you can search the web or ask ChatGPT for assistance:

  1. Build the kernel on your Linux machine as usual.
  2. Run ./scripts/clang-tools/gen_compile_commands.py in the kernel source tree to generate the compile_commands.json file.
  3. Install VSCode on your local machine.
  4. Install the "remote-ssh" and "clangd" extensions from the Extension Marketplace in the VM.
  5. Connect to the VM using the "remote-ssh" extension in VSCode.
  6. Use "open folder" to open the kernel source tree in VSCode.
  7. Open a C file within the kernel source tree. If clangd is not installed, you will see a prompt at the bottom-right corner of the window asking you to install clangd. Click "Install" to proceed.

Note: If you have the official "C/C++" extension installed, you may need to disable it to prevent conflicts with clangd.

To try this out, open the file kernel/sched/core.c at line 6636, where the __schedule function is defined. This function serves as the main scheduler function, driving the scheduling process in the Linux kernel. You may find a call to the context_switch function at line 6756. To navigate to its definition, right-click on the function name and select "Go to Definition."