ILD

vim day 6: cscope
作者:HerbertYuan 邮箱:yuanjp89@163.com
发布时间:2017-9-15 站点:Inside Linux Development

cscope用来查找函数的定义,被谁引用等。官方网站:http://cscope.sourceforge.net/


1 创建cscope数据库

cscope和创建索引相关的选项。

-b 只创建数据库,创建完后不显示GUI界面。

-f 指定输出文件名,默认为cscope.out。inverted索引,在文件名后加上.in和.po后缀。

-i namefile 使用namefile文件指定源码文件,而不是默认的cscope.files。

-k 内核模式,不使用/usr/include目录查找#include文件。

-q 创建inverted索引,这会多生成两个文件,但是可以加快符号索引,这个选项通常要设置。

-R recurse目录来搜索源码。

源码文件的指定有3种方式。

1 命令行指定,如:cscope -b -k -q ../fs.c 

2 当前目录,默认会检索当前目录下的文件,使用-R选项会检索所有的子目录文件。

3 cscope.files,如果存在cscope.files,也检索该文件列出的所有文件。使用-i选项可手动指定列表文件路径。使用-i选项会忽略所有的命令行文件。

如果没有找到源码文件,则报错 cscope: no source files found。

cscope存储到索引文件的路径,会忠实的使用3种方式指定的路径。如上,命令行选项指定的文件../fs.c,在索引文件如实的存储为该路径,那么在搜索时需要保证相对路径正确,否则找不到fs.c。

使用绝对路径还是相对路径看需求。绝对路径保证在任何地方使用cscope都能找到源码文件,但是你不能改变源码文件路径结构。使用相对路径可能找不到源码文件,但是允许源码路径结构改变,例如你可以同时移动源码和索引文件。

2 检索符号

使用-d选项,进入搜索模式。有两种搜索模式 :基于行输出的单次搜索模式,和界面搜索模式。

-L选项和-num pattern结合,为单次搜索模式,num为数字,指定搜索类型。

1
2
3
4
5
6
7
8
9
 0 Find this C symbol:
 1 Find this function definition:
 2 Find functions called by this function:
 3 Find functions calling this function:
 4 Find this text string:
 5 Change this text string:
 6 Find this egrep pattern:
 7 Find this file:
 8 Find files #including this file:

如要查找函数定义:cscope -d -L -1 dump_stack,也可以使用-f指定索引文件。

1
2
3
4
5
6
7
8
herbert@herbert-pc:/work/cscope$ cscope  -d -L -1 dump_stack -f linux-4.4.87/cscope.out 
arch/blackfin/kernel/dumpstack.c dump_stack 163 void dump_stack(void )
include/linux/printk.h dump_stack 233 extern asmlinkage void dump_stack(void ) __cold;
lib/dump_stack.c dump_stack 26 asmlinkage __visible void dump_stack(void )
lib/dump_stack.c dump_stack 59 asmlinkage __visible void dump_stack(void )
tools/lib/lockdep/uinclude/linux/stacktrace.h dump_stack 21 static inline int dump_stack(void )
tools/perf/util/util.c dump_stack 332 void dump_stack(void )
tools/perf/util/util.c dump_stack 347 void dump_stack(void ) {}

直接使用cscope -d进入curse-based GUI界面。

3 VIM中使用cscope

首先下载cscope_maps.vim到~/.vim/plugin目录。这个插件添加当前目录的索引文件,并设置了一些快捷键。

1
2
3
4
5
6
7
8
    "   's'   symbol: find all references to the token under cursor
    "   'g'   global: find global definition(s) of the token under cursor
    "   'c'   calls:  find all calls to the function name under cursor
    "   't'   text:   find all instances of the text under cursor
    "   'e'   egrep:  egrep search for the word under cursor
    "   'f'   file:   open the filename under cursor
    "   'i'   includes: find files that include the filename under cursor
    "   'd'   called: find functions that function under cursor calls

      先按CTRL+/或CTRL+space,在按上述字母进行对应的搜索。前者在当前窗口打开,后者分割一个水平窗口打开。

使用CTRL+t返回之前的源码处。


默认情况下,vim打开文件会相对于当前目录,而不是相对于索引文件的路径。如果是相对路径,使用下述方法来使vim正确的打开文件。

1 添加索引时,指定路径

:cscope add /path/to/cscope.out /path/to/src/code

2 设置'cscoperelative选项,只在没有开启-P选项时有效。使用索引文件的basename作为路径前缀。

set csre

set nocsre


参考

http://cscope.sourceforge.net/cscope_man_page.html

http://cscope.sourceforge.net/cscope_vim_tutorial.html


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.