vim+cscope打造源码阅读环境

在linux虚拟机下做嵌入式开发时,可以使用vim+cscope打造一个类似source insight的源码阅读环境。本文使用ubuntu18.04,下面来看下如何操作。

一 安装和配置vim

打开终端,输入命令:sudo apt install vim
安装好后,配置.vimrc,打开.vimrc命令:vim ~/.vimrc,然后把以下内容输入进去,覆盖原有内容,

set wildmenu
set number
set nocompatible
set autoindent
set history=50
set ruler
set showcmd
set incsearch
set ai
set showmatch
set showmode
set hlsearch

set t_Co=8
set cino=:0
set cindent

colo ron
syntax on

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim           
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE: 
" -- vim 6:     Stick this file in your ~/.vim/plugin directory (or in a
"               'plugin' directory in some other directory that is in your
"               'runtimepath'.
"
" -- vim 5:     Stick this file somewhere and 'source cscope.vim' it from
"               your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE: 
" These key maps use multiple keystrokes (2 or 3 keys).  If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell       [email protected]     2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled.  If it wasn't, time to recompile vim... 

    """"""""""""" Standard cscope/vim boilerplate

    " use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
    " set cscopetag

    " check cscope for definition of a symbol before checking ctags: set to 1
    " if you want the reverse search order.
    set csto=0

    " add any cscope database in current directory
    if filereadable("cscope.out")
    	cs add cscope.out  
    "else add the database pointed to by environment variable 
    elseif $CSCOPE_DB != ""
       cs add $CSCOPE_DB
    endif

    " show msg when any other cscope db added
    set cscopeverbose  


    """"""""""""" My cscope/vim key mappings
    "
    " The following maps all invoke one of the following cscope search types:
    "
    "   '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
    "
    " Below are three sets of the maps: one set that just jumps to your
    " search result, one that splits the existing vim window horizontally and
    " diplays your search result in the new window, and one that does the same
    " thing, but does a vertical split instead (vim 6 only).
    "
    " I've used CTRL-\ and CTRL-@ as the starting keys for these maps, as it's
    " unlikely that you need their default mappings (CTRL-\'s default use is
    " as part of CTRL-\ CTRL-N typemap, which basically just does the same
    " thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
    " If you don't like using 'CTRL-@' or CTRL-\, , you can change some or all
    " of these maps to use other keys.  One likely candidate is 'CTRL-_'
    " (which also maps to CTRL-/, which is easier to type).  By default it is
    " used to switch between Hebrew and English keyboard mode.
    "
    " All of the maps involving the <cfile> macro use '^<cfile>$': this is so
    " that searches over '#include <time.h>" return only references to
    " 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
    " files that contain 'time.h' as part of their name).


    " To do the first type of search, hit 'CTRL-\', followed by one of the
    " cscope search types above (s,g,c,t,e,f,i,d).  The result of your cscope
    " search will be displayed in the current window.  You can use CTRL-T to
    " go back to where you were before the search.  
    "

    nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>	
    nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
    nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>	


    " Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
    " makes the vim window split horizontally, with search result displayed in
    " the new window.
    "
    " (Note: earlier versions of vim may not have the :scs command, but it
    " can be simulated roughly via:
    "    nmap <C-@>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>	

    nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR>	
    nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR>	
    nmap <C-@>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>	
    nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR>	


    " Hitting CTRL-space *twice* before the search type does a vertical 
    " split instead of a horizontal one (vim 6 and up only)
    "
    " (Note: you may wish to put a 'set splitright' in your .vimrc
    " if you prefer the new window on the right instead of the left

    nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>	
    nmap <C-@><C-@>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>	
    nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>

    " replace string
    nmap <C-\>r :%s/ostr/nstr/g

二 安装cscope

打开终端,输入命令:

  • sudo apt install cscope

三 使用cscope

这里使用linux内核源码举例,我们在kernel.org上下载好linux内核源码后,把源码包放到虚拟机里解压缩,这里下载的源码版本是4.9.75。

在终端下cd到内核源码路径下,然后输入以下命令,

  • cscope-indexer -r

命令执行需要等待一段时间,执行完后会在源码目录下生成一个cscope.out的文件,
在这里插入图片描述
这个文件大约有450M

然后在终端下输入vim回车,输入以下命令来查找start_kernel函数,

  • : cs f g start_kernel (ps:f是find的缩写,g是global的缩写,即查找全局定义情况)

在这里插入图片描述
可以看到这里会显示出所有定义start_kernel函数的地方,以及所在的文件。此时输入4就可以跳转到函数定义的位置。

在这里插入图片描述
打开源码后,假如想查看smp_setup_processor_id函数的定义,可以再按照刚才的方法输入: cs f g smp_setup_processor_id来查看。
这种方法比较麻烦,要输入很多字符,其实我们也可以通过如下快捷键来操作,

  • 把光标移动到函数名上
  • 按下CTRL + \,释放按键,再按下g (ps 快捷键是在.vimrc里配置的,也可以根据需要进行修改)

可以达到相同的效果。这里要注意一下以上2种方法的区别,第一种方法可以在没打开源码或者在当前源码下没看到目标函数或变量时就可以使用,当然看到目标函数或变量时也可以使用,而第二种方法只能在当前源码下看到目标函数或变量时才能使用,因为要移动光标定位。

选择好函数进行跳转后,我们一般还想返回,可以按CTRL + t

有时我们还想查看函数被调用的情况,可以使用以下2种方式,

  • 命令 : cs f c xxx (ps:f是find的缩写,c是call的缩写,即查找全局调用情况)
  • 光标移动到函数名上,按下CTRL + \,释放按键,再按下c

四 ctags辅助

ctags可以支持函数的跳转和返回,功能和cscope有一定的重合,不过可以配合cscope来使用。

安装ctags,

  • sudo apt install ctags

然后打开终端并cd到源码目录下,输入ctags -R来生成tags文件,然后在.vimrc文件最后一行添加set tags=/xx/yy/zz/tags,其中/xx/yy/zz是指源码路径,因为tags文件是生成在这个路径下。

vim打开源码后,跳转按CTRL+],返回按CTRL + t

这里举个例子看ctags如何配合cscope使用:

  • 使用: cs f g task_struct来查看task_struct的全局定义,结果发现有160多处,其实只有一处定义,其他地方是声明
  • 选择1进入其中一个声明的地方,然后按下ctrl+]就可以跳转到定义的地方

如果不使用ctags,可以每行看看也能找到,只是稍微麻烦。

四 总结

在linux环境下,使用vim+cscope基本可以满足阅读源码的需求(也可以加入ctags的辅助),不过和windows下的source insight相比,方便程度还是有点差距,只是source insight只能在windows下用而且要收费。vim+cscope使用习惯了,应该也差不多。

另外,本文只列出了常用的几个操作,其它操作可以查看.vimrc里的注释,

‘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

如果有写的不对的地方,希望能留言指正,谢谢阅读。

猜你喜欢

转载自blog.csdn.net/whahu1989/article/details/84891960