Linux Kernel Programming

VS Code for kernel development

Now that we know that the kernel compiles and runs, we can start making some changes to it or write some modules.

To make this experience more bearable enjoyable, we’ll use VS Code with some extensions. Most of them are automatically recommended to install, but because the kernel isn’t your usual everyday codebase, we have to configure them for optimal performance.

As before I’ll assume your directory looks something like this:

.
├── linux # The kernel source code
└── my-kernel-module

Then the interesting files for us will be:

.
├── linux # The kernel source code
│   ├── .clang-format
│   ├── .vscode
│   │   ├── c_cpp_properties.json
│   │   ├── settings.json
│   ├── scripts
│   │   └── checkpatch.pl
└── my-kernel-module
│   ├── .vscode
│   │   ├── c_cpp_properties.json
│   │   ├── settings.json

I’ll mention all of them in the context of the extension that uses it.

Extensions

C/C++

C/C++ IntelliSense, debugging, and code browsing

It does exactly what it says on the tin and if you’ve ever opened a C file in VS Code, you probably already have it.

As you might have guessed, it’s configured in .vscode/​c_cpp_properties.json. There you can have multiple configurations, I’ve named mine Linux Modules.

If you’re not using the same directory structure as me or aren’t using arm64 mac, you might need to change:

  • include paths - so that the extension can find the kernel headers (also applies to .config) (otherwise you won’t get any type information or code completion)

  • compiler path/mode - so that the extension can compile your code using the same compiler as you (if not you might get warnings during build that aren’t present in the editor)

Clang-Format

Clang-Format

When writing kernel code, you should follow the kernel’s coding style. You could run clang-format manually from the terminal, but it’s much nicer if it happens automatically (on save).

To configure the formatting, you need .clang-format in the root of your project. Just symlink the one from the kernel source code:

ln -s ../linux/.clang-format .clang-format

Makefile Tools

Makefile Tools

Another one that VS Code will recommend.

checkpatch

checkpatch

This one is a bit more niche. Another part of the kernel’s coding style is to run checkpatch.pl on your code. It’s a script that checks your code for common mistakes and style issues, and sometimes even suggests fixes.

Normally you would have to run it in the terminal, but this extension runs it automatically on save and shows the results in the editor.

To get all automatic fixes, you can run a script like this:

CHECKPATCH_PATH="../linux/scripts/checkpatch.pl"

find . -type f -name "*.c" -o -name "*.h" | xargs perl $CHECKPATCH_PATH -f --no-tree --fix-inplace

GitHub Copilot

GitHub Copilot

This one might be controversial, but asking the editor about what some function is, was often quicker than finding it in the documentation.

Also, if you, like me, are coming from a more modern language, the Copilot is great for writing the boilerplate code.

Settings

There are also some things in VS Code itself, that will make your life easier.

{
    {
    "files.exclude": {
        "**/.*.*.cmd": true,
        "**/.*.d": true,
        "**/.*.S": true,
        "**/.*.tmp": true,
        "**/*.a": true,
        "**/*.dtb": true,
        "**/*.ko": true,
        "**/*.mod": true,
        "**/*.mod.c": true,
        "**/*.o": true,
        "**/modules.order": true,
        "**/*.symvers": true
    },
    "[c]": {
        "editor.detectIndentation": false,
        "editor.tabSize": 8,
        "editor.insertSpaces": false,
        "editor.rulers": [
            80,
            100
        ]
    },
    "files.associations": {
        "*.h": "c"
    },
    "C_Cpp.errorSquiggles": "enabled",
    "C_Cpp_Runner.msvcBatchPath": "",
}
}
  • files.exclude - building a kernel module involves a lot of auxilary files, which will clutter your file explorer, so let’s hide them

  • [c] - some settings for C files

    • mostly just conform to kernel coding style
    • also rulers to show you how much space you have left in each line
  • files.associations - so that .h files are treated as C files (helps with IntelliSense and highlighting)


Honourable (not kernel-specific) mentions

If you’re using VS Code for other stuff, you might also want to check out:

  • GitLens for the contextual git information and better diffs

  • cSpell for spell-checking in code!

  • LTeX for spell-checking in comments and strings

  • Tailscale for connecting with other devices


  • vs code
  • kernel
  • developer experience
Content width
© 2023 - 2024 mastermakrela