GTAGS

使用Gtags辅助查看kernel源码

使用make gtags在kernel根目录下生成索引文件。

1
gtags       - Generate GNU GLOBAL index

kernel已对该命令进行封装,不必手动生成索引文件。在生成索引文件时还可以指定架构,这可以有效减少干扰项。

1
2
make gtags ARCH=arm
make gtags ARCH=riscv

kernel根目录下会生成三个新文件

1
2
$ ls G*
GPATH GRTAGS GTAGS

gtags.vim

General form of Gtags command is as follows:

1
:Gtags [option] pattern

To go to ‘func’, you can say

1
:Gtags func

Input completion is available. If you forgot the name of a function but recall only some characters of the head, please input them and press key.

1
2
:Gtags fu<TAB>
:Gtags func <- Vim will append 'nc'.

To go to the referenced point of ‘func’, add -r option.

1
:Gtags -r func

To go to any symbols which are not defined in GTAGS, try this.

1
:Gtags -s func

To go to any string other than symbol, try this.

1
:Gtags -g ^[sg]et_

This command accomplishes the same function as grep(1) but is more convenient because it retrieves an entire directory structure.

To get list of objects in a file ‘main.c’, use -f command.

1
:Gtags -f main.c

If you are editing main.c itself, you can use ‘%’ instead.

1
:Gtags -f %

You can get a list of files whose path include specified pattern.
For example:

1
2
3
   :Gtags -P /vm/			<- all files under 'vm' directory.
:Gtags -P \.h$ <- all include files.
:Gtags -P init <- all paths includes 'init'

If you omitted an argument and input only key to the prompt, vim shows list of all files in the project.

Since all short options are sent to global(1) as is, you can use the -i, -o, -O, and so on.

For example, if you want to ignore case distinctions in pattern.

1
:Gtags -gi paTtern

It will match to both of ‘PATTERN’ and ‘pattern’.

If you want to search a pattern which starts with a hyphen like ‘-C’
then you can use the -e option like grep(1).

1
:Gtags -ge -C

By default, Gtags command search only in source files. If you want to
search in both source files and text files, or only in text files then

1
2
:Gtags -go pattern		# both source and text
:Gtags -gO pattern # only text file

gtags-cscope

1
2
3
4
5
6
7
8
9
Find symbol		:cs find 0 or s
Find definition :cs find 1 or g
Find functions called by this function :cs find 2 or d
Find reference :cs find 3 or c
Find text string :cs find 4 or t
Find egrep pattern :cs find 6 or e
Find path :cs find 7 or f
Find include file :cs find 8 or i
Find assignments :cs find 9 or a

总结

一般情况下,Gtags已能满足需求。