內核學習第一步:查看源碼Vim+Cscope
內核學習第一步:查看源碼Vim+Cscope
本文參照Linux內核修鍊之道編寫,作者為任橋偉。
Vim就不用多說了,大家都在用。Cscope看軟體名字就知道C+scope,本身設計出來就是為了瀏覽c的代碼的。通過Cscope可以知道諸如某個函數的定義、那些函數調用了它、它調用了那些函數等等。是在控制台學習Linux源碼不可缺少的有力工具。你可以通過閱讀Vim中文幫助手冊來了解:help if_cscop.txt。
1. 安裝Cscope
從http://cscope.sourceforge.net下載然後編譯安裝
2. 生成Cscope的資料庫
進入你需要查看的源碼的根目錄,比如你要查看的是Linux內核源碼,位於/usr/src/linux下,那麼使用以下命令生成Cscope資料庫文件
view plaincopy to clipboardprint?
cd /usr/src/linux
cscope –Rbq
cd /usr/src/linux
cscope –Rbq 以上命令會生成三個文件:cscope.in.out cscope.out cscope.po.out
3. 將Cscope資料庫文件導入Vimview plaincopy to clipboardprint?
vim
:cs add /usr/src/linux/cscope.out /usr/src/linux
vim
:cs add /usr/src/linux/cscope.out /usr/src/linux 4. Cscope的使用方法
你可以通過閱讀Vim的在線文檔詳細了解 :help if_cscop.txt 。我簡單的解釋一下最常用的find命令:view plaincopy to clipboardprint?
:cs find c|d|e|f|g|i|s|t name
:cs find c|d|e|f|g|i|s|t name S: 查找本C代碼符號
g: 查找本定義
d: 查找本函數調用的函數
c: 查找調用本函數的函數
t: 查找本字元串
e: 查找本egrep模式
f: 查找本文件
i: 查找包含本文件的文件
例如查找main函數的定義view plaincopy to clipboardprint?
:cs find g main
:cs find g main 都知道vim支持縮寫命令格式,你可以使用:cs f g main來代替
5. 配置Vim,加速Cscope的使用
首先你要對Vim配置有個初步的了解,請參看此文打造你自己的vim--如何配置vim
你用vim打開你個人根目錄下的vim配置文件
view plaincopy to clipboardprint?
username@ubuntu:~$ vim /home/username/.vimrc
username@ubuntu:~$ vim /home/username/.vimrc 添加如下內容:view plaincopy to clipboardprint?
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set nu
syntax on
set tabstop=4
set autoindent
set matchtime=1
set hlsearch
set cindent
set shiftwidth=4
set ruler
set showmode
《解決方案》
謝謝分享....學習鳥
《解決方案》
學習了