July 30, 2020

Studying the Linux source code.

Let's be honest. This is always painful and difficult.

The linux sources seem to be organized in a deliberately confusing manner and you need to jump between what seens to be an endless list of directories to gather all the information related to a single topic (such as a driver). The configuration process causes yet more trouble, and along with symbolic links and a wild arrangement of include files, things can get extremely confusing.

My usual method is to use grep and perhaps grep -r to make my way around other peoples source code. For any number of reasons this fails with the linux sources. At the least, it is slow and awkward.

But there are better ways. I have some notes here from years gone by:

Ctags

My old notes discuss a number of other tools, but here I just want to discuss using ctags along with the vim editor.

The first step is to go to the base directory of whatever linux source snapshot you intend to study and type this command:

ctags -R .
This will take a while (given that linux is a bit project) and will produce a file named "tags" in the current directory. There may already be a tags file, but don't let that stop you.

Using tags from Vim

There are a variety of Vim plugins related to ctags. Dealing with these will drag\ you into Vim plugin hell, which makes a nice companion to Unix source code hell. Don't expect any quick short cuts on this road. The following have nothing to do with these plugins as far as I know.

Type Control-] with the cursor on some name, and you will go to where that name is defined.

Type Control-t to return (go back) to whereever you came from.

Just these two commands are enough to change your life!

The above may make a decision for you on which of several definition it goes to. To see a menu of all the possibilities, place the cursor over the thing of interest and type "g]". You then have to type a number and hit return.

This has nothing to do with ctags, it is plain old Vim, but very handy. Put the cursor on a name of interest and hit "*" and you go to the next occurence of that name in the same file.

Cscope

This is a standalone program. It lets you do the reverse of what is described above, namely start with a function and then find all the places it is referenced.


Have any comments? Questions? Drop me a line!

Kyu / [email protected]