tags:

views:

112

answers:

6

I am wondering how I can effectively find the usages of a function/structure in the files using find & grep combination.

For example, I have source code for git on my machine. If you look at the commit.h, you can see commit structure is defined like,

struct commit {
    struct object object;
    void *util;
    unsigned int indegree;
    unsigned long date;
    struct commit_list *parents;
    struct tree *tree;
    char *buffer;
};

I am interested to find out from where this structure is initialized and how are they initializing buffer. First I tried,

grep -rn "(struct commit)" .

This gave me a file which has this structure initialization. Now I need to find out where the buffer variable which is a member of this structure is initialized.

grep -rn "buffer" .

returnes a lot of results and tough to find out where it is used.

So I am wondering, how do you find out the usages of a symbol effectively? I am not talking about what an IDE provides but with using standard linux tools like grep and find. How do you manage to hack into a big codebase and understand how it works?

A: 

You could always build git with the GCC debug flag enabled (-g) and use GDB. You could set a break point in the initialization function and backtrace from there. That should give you a better idea about what is happening in the callstack without having to follow a bunch of function declarations etc.

GWW
+10  A: 

Have you evaluated cscope or ctags for the purpose ?

Both of them work well with both vim and emacs. The main ability that they provide you is to lookup the definitions of a symbol in your source code that may be used and defined at different files.

I personally use cscope http://cscope.sourceforge.net/cscope_vim_tutorial.html and it works beautifully for me to dig into the code.

Kisalay
+1 because I love cscope and VIM are awesome
GWW
Thanks. It sounds promising. I will take a look.
Appu
A: 

Apart from cscope and ctags as mentioned by Kisalay.

You can use try id-utils. There are may VIM plugins to use idutils from VIM. It crates a ID file from you source code and very fast in searching.

Hemant
+2  A: 

Finding references to a member of a struct is easier if the member has a less generic name than "buffer". But you knew that.

Using just a modern version of grep, you can find references to all members named "buffer" accessed through a pointer as:

grep --recursive --include=\*.c "->buffer"

or accessed as a member of a local instance as:

grep --recursive --include=\*.c "\.buffer"

The sensible inclusion of --recursive in GNU grep makes learning to correctly use find over a source tree much less important. (Also, I think I've got the quoting right... but beware of shell quoting in an example typed off the cuff.)

I would concur with the recommendation to learn about tools like ctags, and how they integrate with your editor of choice. Both emacs and vim are a lot more powerful than they look at first glance.

RBerteig
A: 

I think what you need is GNU GLOBAL which will show all the uses of classes and functions in a cross-referenced manner, eg look at the analysis of the linux kernel

the_mandrill
+1  A: 

Kscope is cscope + ctags with a graphical user interface. Very convenient if you are not a big fan of vi or emacs. Since it was originally targeted at kernel source navigation, it has no problem handling large projects.

shodanex