[Date Prev][Date Next][Date Index]
[linux] Using Cscope and Ctag to search through kernel source
cscope
cscope is useful for doing many of the things you could use grep for, but is
more intelligent and provides a nicer interface to work with. You can search
for definitions, uses, strings etc.
Before you can use cscope you need to build an index file. This can be done
by issuing the command cscope -b -R -k in the top level source directory; -b
to build the index, -R to search recursively through the source tree and -k
to indicate kernel use; this ensures the appropriate include files are used
when generating the index. Note that the index should be built from a clean
source tree, so backup your .config file and make mrproper in the top level
source directory.
To start a cscope session, type cscope -d. You will then get something that
looks like this:
Cscope version 15.3 Press the ? key for
help
Find this C symbol:
Find this global definition:
Find functions called by this function:
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
The top half of the screen is used to display search results, while the
lower half is used to issue commands. Example; suppose you want to find the
definition of the file_system_type data structure. Use the arrow keys to
move the cursor to the Find this global definition: field and enter the name
of the data structure, followed by enter. If all is well, cscope will find
just one instance and will open your favourite editor (set by the EDITOR
environment variable) to display the appropriate section of the file.
Now suppose you want to find the definition of super_block. Follow the
procedure above, which should give you this output:
Global definition: super_block
File Line
0 vxfs_extern.h 44 struct super_block;
1 super.c 265 int (*test)(struct super_block *, struct buffer_head *);
2 udfdecl.h 52 struct super_block;
3 fs.h 688 struct super_block
4 fs.h 936 struct super_block *(*read_super) (struct super_block *,
void *, int );
5 udf_fs_sb.h 71 __u32 (*s_partition_func)(struct super_block *, __u32,
__u16, __u32);
Find this C symbol:
Find this global definition:
Find functions called by this function:
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
This time, cscope has found multiple possible definitions. You can view each
definition in its context by pressing the numbers given next to the list of
files, alternatively, you can use Tab to move to the top half of the screen
and the arrow keys to select a definition. Simply quit out of the editor to
return to cscope. Press Tab again to return to the command area. Note that
you can also move up and down using Ctrl-p and Ctrl-n, which saves moving
your hands away from a typing position. To exit cscope, press Ctrl-d.
cscope source code and documentation is available from the cscope home page,
I would advise using version 15.3 or later as this version includes support
for recursive indexing and using kernel headers.
ctags
ctags provides similar functionality to cscope, but integrates closely with
your editor, allowing look-ups with a few key strokes. Like cscope, ctags
builds an index file to speed up searches. The easiest way to generate the
index is to type make tags in the top level kernel source directory.
When using vim, moving the cursor to a use of a function/variable/data
structure/type and pressing Ctrl-] should take you to the definition. Ctrl-t
takes you back to where you were. Note that lookups can be nested, in which
case, Ctrl-t takes you up one level.
Details for using ctags with emacs are welcome!
ctags can be obtained from the ctags home page. Debian users will want to
install the exuberant-ctags package.