vscode for linux kernel

VSCode VS VIM + Ctags + Cscope!

Background

I have used VIM + Ctags + CScope to navigate linux kernel source code for quite a long time, I can only say it’s usable but not perfect, the disadvantages are as follows:

  1. After updating kernel code, you should regenerate tags and cscope.out files
  2. The function go to definition not always work as you expect(whether “ctrl + ]” in VIM or “cs f g xxx” in Cscope)

Example1:

if you are currently working on file drivers/net/ethernet/intel/e1000/e1000_main.c, and you type “ctrl + ]” on function e1000_probe to find definition, you may get a list like this:

_config.yml

because both driver e1000 and e1000e have a function named e1000_probe, and this is quite common in linux kernel, many user defined functions have the same name in different drivers.

Example2:

you want to find definition of mutex structure, if you type “ctrl + ]” you will get a list with more than 400 options:

_config.yml

It’s a pain to find mutex definition from these 455 options, of course you can type cs f g struct mutex { to find the correct one, but it’s not that convenient to type so many words and it’s a bit slow.

Now vscode provides a better experience compared to VIM + Ctags + Cscope

vscode for linux kernel

  1. install vscode on your linux machine
  2. install MS C/C++ IntelliSense plugin
  3. config MS C/C++ IntelliSense plugin

because linux kernel has many different configs written in Kconfig files which affect function definitions, you should tell plugin the includePath and defines, luckly this can be done through compile_commands.json file:

_config.yml

  • get compile_commands.json

    config kernel based on your flavor

      make defconfig
    

    build kernel

      make -j8
    

    there is already a script in linux kernel to generate compile_commands.json from *.o.cmd generated by step above

      ./scripts/clang-tools/gen_compile_commands.py
    

    clean generated *.o.cmd so that they will not show in vscode explorer

      make clean
    
  • tell MS C/C++ IntelliSense plugin the location of compile_commands.json file

    edit your .vscode/c_cpp_properties.json

    _config.yml

References

https://code.visualstudio.com/docs/cpp/config-linux

https://github.com/torvalds/linux/commit/b30204640192

Written on June 7, 2020