JoelFernandes.org

Age is of no importance unless you're a cheese -- Billie Burke

Hello! I’m Joel and this my personal website built with Jekyll! I currently work at Google. My interests are scheduler, RCU, tracing, synchronization, memory models and other kernel internals. I also love contributing to the upstream Linux kernel and other open source projects.

Connect with me on Twitter, and LinkedIn. Or, drop me an email at: joel at joelfernandes dot org

Look for my name in the kernel git log to find my upstream kernel patches. Check out my resume for full details of my work experience. I also actively present at conferences, see a list of my past talks, presentations and publications.

Full list of all blog posts on this site:
  • 25 Jun 2023   SVM and vectors for the curious
  • 10 Jun 2023   SELinux Debugging on ChromeOS
  • 28 Apr 2023   Understanding Hazard Pointers
  • 25 Apr 2023   PowerPC stack guard false positives in Linux kernel
  • 24 Feb 2023   Getting YouCompleteMe working for kernel development
  • 29 Jan 2023   Figuring out herd7 memory models
  • 13 Nov 2022   On workings of hrtimer's slack time functionality
  • 25 Oct 2020   C++ rvalue references
  • 06 Mar 2020   SRCU state double scan
  • 18 Oct 2019   Modeling (lack of) store ordering using PlusCal - and a wishlist
  • 02 Sep 2019   Making sense of scheduler deadlocks in RCU
  • 23 Dec 2018   Dumping User and Kernel stacks on Kernel events
  • 15 Jun 2018   RCU and dynticks-idle mode
  • 10 Jun 2018   Single-stepping the kernel's C code
  • 10 May 2018   RCU-preempt: What happens on a context switch
  • 11 Feb 2018   USDT for reliable Userspace event tracing
  • 08 Jan 2018   BPFd- Running BCC tools remotely across systems
  • 01 Jan 2017   ARMv8: flamegraph and NMI support
  • 19 Jun 2016   Ftrace events mechanism
  • 20 Mar 2016   TIF_NEED_RESCHED: why is it needed
  • 25 Dec 2015   Tying 2 voltage sources/signals together
  • 04 Jun 2014   MicroSD card remote switch
  • 08 May 2014   Linux Spinlock Internals
  • 25 Apr 2014   Studying cache-line sharing effects on SMP systems
  • 23 Apr 2014   Design of fork followed by exec in Linux
  • Most Recept Post:

    Getting YouCompleteMe working for kernel development

    | Comments

    YCM is a pretty neat tool for speedy kernel development in vim. Especially when you don’t want to write a whole bunch of code, and then deal with 100s of compiler errors. It is better to fix the errors on the spot, as you write the code. That’s exactly what YCM is best at since it constantly builds the code on the fly. The other great thing about it among other things being, it will show you function prototypes and so forth as you type, so you can call functions correctly with the right set of parameters and types. YCM uses clangd under the hood which understands code navigation, helps with code completion, and so forth. clangd is used by a whole plehtora of IDEs and tools. I remember using it when I tried to get vscode working with ChromeOS source navigation (which is actually a bigger project in terms of lines of code, than the Linux kernel!).

    Interestingly YCM does not use cscope even if it is available, and they do not want to support it. Further, they reluctantly support ctags. Since I don’t use ctags, I am not sure how having tags available changes YCM behavior and how it works with clangd. But that is something worth trying.

    Getting YCM working with vim is pretty easy, but requires a few steps.

    First you have to build a compile_commands.json file in the kernel root. To do so, run:

    bear -- make -j99 CC=clang
    

    Note the CC variable passed to Make. It has to be clang, otherwise YCM throws a tonne of errors when it is in use. I believe that is because clangd incorrectly passes gcc-specific options to clang, which are not all supported.

    Following this stackoverflow post shows how to avoid some issues. This post is how I learnt to use bear. Just use bear and ignore all other posts or articles that ask you to create a .ycm_extra_conf.py file. I did not need to do that at all.

    I faced the following issues:

    1. First, there might some warnings in the code that arise when clangd/clang try to build your code on the fly. In case these warnings are not legit, it is best to ignore them. For that the g:ycm_filter_diagnostics vim variable can be defined. The above post shows an example.

    2. Editing header files may not work well: This happens because header files cannot really be “built”. Further, sometimes header files require other header files to be included, which may be done in the actual C file that includes the header but not the header itself. However, vim/YCM does not know about that. This issue can simply be fixed by including other header dependencies into the header file being edited.

    3. Kernel config macro dependencies: If a build has not happened yet, or you are writing C code that depends on a new CONFIG option, a build may not have happened yet, so the autoconf headers may not yet be available. This causes YCM to not build those sections of code. A quick fix might be to add something like this in the sources:

      #ifndef CONFIG_FOO_BAR
      #define CONFIG_FOO_BAR
      #endif
      

      Similar tricks can be employed to satisfy macros such as IS_ENABLED(CONFIG_FOO_BAR).

    Hope this helps, do you have any other tips or ideas to use YCM better? If so, let me know in the comments!