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 [email protected] 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': [email protected] doesn't seem to have any default use).
" If you don't like using ' [email protected]' 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 [email protected] 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 <[email protected]>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]>g :scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]>c :scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]>t :scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]>e :scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <[email protected]>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <[email protected]>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 <[email protected]><[email protected]>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]><[email protected]>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]><[email protected]>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]><[email protected]>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]><[email protected]>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]><[email protected]>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <[email protected]><[email protected]>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <[email protected]><[email protected]>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
如果有寫的不對的地方,希望能留言指正,謝謝閱讀。