开发者

Using grep/find combination to find the usages of a function or a struct

开发者 https://www.devze.com 2023-01-10 16:57 出处:网络
I am wondering how I can effectively find the usages of a function/structure in the files using find & grep combination.

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

For example, I hav开发者_如何学Goe 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?


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.


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.


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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消