How to find calling and called functions in C code in vi / vim?

I want to know how I can easily click (or maybe use some simple shortcuts) on a function name, and find all its called messages or open where they were defined. Most web guides on the Internet are really hard to track or can't find a job. Say I want to click allocuvm and see where it was defined?

 uint newstk=allocuvm(pgdir, USERTOP-PGSIZE, USERTOP); 
+7
c linux vim vi
source share
3 answers

To do this, Vim integrates with the cscope tool; see :help cscope for more information.

+10
source share

minimal cscope example

Ingo mentioned this, here is an example.

Go to the base directory of your project and run:

 cscope -Rb 

This generates a cscope.out file containing the parsed information. Generation is fast enough, even for large projects such as the Linux kernel.

Open vim and run:

 :cs add cscope.out :cs find c my_func 

c is mnemonic for callers . Other possible cscope queries are also possible, the mnemonics are listed below:

 help cscope 

This adds the caller list to the quickfix list, which you can open with:

 :copen 

Scroll to the line you are interested in and press enter to go there.

To find the subscribers of the function name under the cursor, add to your .vimrc :

 function! Csc() cscope find c <cword> copen endfunction command! Csc call Csc() 

and enter :Csc<enter> when the cursor is over the function.

TODO:

  • do this for the current function under the cursor with a single command. Related: Vim: show function name in status bar
  • automatically adds the nearest database (parent directories) when entering a file: how to automatically load cscope.out in vim
  • interactively open a call graph, such as Eclipse. Related: Generate call tree from cscope database

A word of advice: I love vim, but for me it is too difficult to configure this. If the project is enough for you, try getting the project to work on some kind of "IDE". This may be due to some overhead if the project does not track IDE configuration files (which automatically change drops that pollute the repo ...), but it's worth it. For C / C ++, my favorite so far has been KDevelop 4.

+11
source share

vi /. --- / - search function in vi and. will repeat the same command.

you can also use sed (stream editor) if this big sed file grep can get line numbers

read the manual page

0
source share

All Articles