KernelNewbies:

How to get around in the source code tree?

There exists a number of tools (some proprietoty, some FOSS, some with IDEs..etc. Which one is the best?...

You might be aware of a number of tools that gives you easy access to source trees. But the one I have found best is, without surprise vim/emacs and grep. Mix it with cscope and tags, you don't need anything else.

cscope

Set up cscope with the make target cscope

$ pwd
/home/om/src
$ make cscope
$ ls cscope*
308K cscope.files   24M cscope.in.out  134M cscope.out  190M cscope.po.out
$ cscope -d    # starts cscope

ctags

Set up ctags with the make target tags

$ pwd
/home/om/src
$ make tags
$ ls tags
61M tags

Once you launch [g]vim editor (emacs should work too, I don't know much about it), make sure tags are set right.

<ESC>:set tags?
  tags=./tags,./TAGS,tags,TAGS

GREP and Regular Expressions

REs pack a lot of power. The best reason for using them is, that it is not easy. Secondly, the more REs you use, the more familiar with them you grow. Moreover, you would grow more familiar with the source code too.

For example, I want to get to the definition of struct task_struct. I can use cscope, and in my setup, it returns around 30 entries. I can go through all and finally find out it is defined in sched.h. Similarly, using ctags, I get almost the same number of entries.

Now since I am trying to use, grep lets see how many entries can I see using grep.

$ type findc
findc is a function
findc () 
{ 
    find $1 -name '*.[ch]'
}
[om@testserv ~/src]
$ findc | xargs grep task_struct | wc -l
3738

3738 lines?... if you think searching among 30 lines is a lot better, I agree with you. But wait a bit, what I did was not really correct. I just searched for occurrences of task_struct instead of definition. If I can't reduce the #of lines I have to search for definition to around 2-3, there may not much point to using grep. How can I grep for definition of task_struct? Here is REs comes in handy.

BTW, findc is a function I use to avoid typing some number of characters.

Let's see, we can reasonably assume that the keyword struct would preceed the task_struct where it is defined. So, lets grep for that.

$ findc | xargs grep -EnH "struct task_struct" | wc -l
3456

That is not helping either. The reason is every function definition, extern declarations ..etc of task_struct would have the keyword struct preceeding it

Hmm... Okay. In definition of task_struct, { should succeed task_struct, i.e, the RE should be struct task_struct {. Is that correct? Let's see.

$ findc | xargs grep -EnH "struct task_struct {" 
./include/linux/sched.h:947:struct task_struct {

And you got it. Exactly where it is defined.

KernelNewbies: FAQ/CodeBrowsing (last edited 2008-09-07 06:50:56 by OmNarasimhan)