1. In your home directory, create a new folder:
mkdir -p ~/.vim/plugin
2. Move to that directory and download the cscope mappings file which will be used by Vim:
cd ~/.vim/plugin
wget http://cscope.sourceforge.net/cscope_maps.vim
We assume that you have already created the cscope database in the previous step, which is needed to browse the files.
3. Learn how to use the mappings.
You can check the cscope_maps.vim for details about the mappings that can be used. There are three useful mappings which you might find helpful:
1. CTRL + ']'
If you move the cursor to any function call (or struct, ...) and issue this mapping, it will take you to the definition of the function call. For example:
a) Open kern/main/main.c with Vim:
vim kern/main/main.c
b) Go the kmain() function and move the cursor to any place on the boot() function call. Issue the mapping by pressing CTRL and ] at the same time. You will see that now you're at the definition of the boot() function call. There might be multiple places where a function is defined. In that case Vim will give you a list and you can choose the one that you think is relevant to what you're searching for.
2. CTRL + 't'
This will take you back up in the stack to the point before you issued CTRL + ]. You can issue this mapping from anywhere ( the cursor doesn't have to be on anything). If you issue this mapping after you jumped to the boot() definition, you will be taken back to the kmain() function.
3- CTRL + '\' and then 's'
You perform this mapping in a similar way to the CTRL+ ], however this mapping will give you a list of all the places where the given function ( or struct or anything else) is used throughout the source code.
4. Support cscope while running Vim from any directory (Thanks to Augustine)
For now, you can only use cscope with Vim if you run Vim from the same directory where the cscope tags are generated. To use cscope regardless from where you run Vim, you can add the following line to the ~/.vimrc file (assuming that you have generated the cscope files in ~/os161/src/):
cs add $HOME/os161/src/cscope.out $HOME/os161/src/
Make sure you generate the tags at the highest convenient level.
5. Advanced cscope options: use cs help command inside Vim to explore more advanced options:
:cs help
cscope commands:
add : Add a new database (Usage: add file|dir [pre-path] [flags])
find : Query for a pattern (Usage: find c|d|e|f|g|i|s|t name)
c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find this text string
help : Show this message (Usage: help)
kill : Kill a connection (Usage: kill #)
reset: Reinit all connections (Usage: reset)
show : Show connections (Usage: show)
For example, to lookup where is strcmp method is defined, try:
:cs find g strcmp
You should see something similar to this:
Cscope tag: strcmp
# line filename / context / line
1 48 src/common/libc/string/strcmp.c <<strcmp>>
strcmp(const char *a, const char *b)
2 1 src/kern/compile/DUMBVM/.depend.strcmp.c <<strcmp>>
strcmp.o: ../../../common/libc/string/strcmp.c ../../include/types.h \
3 1 src/kern/compile/SYNCHPROBS/.depend.strcmp.c <<strcmp>>
strcmp.o: ../../../common/libc/string/strcmp.c ../../include/types.h \
4 144 /usr/include/string.h <<strcmp>>
extern int strcmp (const char *__s1, const char *__s2)
Type number and <Enter> (empty cancels):
where you can enter 1 to jump to the method implementation.