以Ubuntu Linux安装为例,并以源码linux-3.13.0为分析目标
-
安装ctags
(1) 安装:~> sudo apt-get install exuberant-ctags
(2) 确认tags.sh存在.
~/linux3.13.0> ls –al ./scripts/tags.sh
(3) 生成x86_64/ARM标签, 且tag 文件大小为89M.
~/linux3.13.0> make tags ARCH=arm(x86_64) -
安装cscope
(1) 安装:~> sudo apt-get install cscope
(2) 生成cscope数据库
~/linux3.13.0> make cscope ARCH=arm(x86_64)
(3) 查看是否生成4个文件scope.files, scope.out, scope.out.in, scope.out.po:
~/linux3.13.0> ls –alh scope.* -
下载插件Source Explorer、 NERD Tree、 Tag List. 插件官网www.vim.org
插件位置在http://www.vim.org/scripts/script_search_results.php
并使用关键字(KeyWords)搜索
(1) 下载Source Explorer
网址:https://github.com/wesleyche/SrcExpl
(2) 下载NERD Tree(NERD_tree.zip最新版)
网址:http://www.vim.org/scripts/script.php?script_id=1658
(3) 下载Tag List(taglist_46.zip)
网址:http://www.vim.org/scripts/script.php?script_id=273 -
安装插件
(1)检查~/目录下是否存在.vim子目录及.vim目录中是否存在plugin子目录,若不存在,则需要mkdir创建,目录结构如下图所示。
(2)将下载的插件解压缩后,放在~/.vim/plugin目录中,两种方法:(1)unzip xxx.zip -d ~/.vim/ ;(2)unzip xxx ,然后进入目录中的plugin子目录中cp *.vim ~/.vim/plugin/ 。如下图所示的NERD_tree.vim、srcexpl.vim、taglist.vim。
(3) 检查是否安装成功
~/.vim> ls ./plugin
NERD_Tree.vim srcexpl.vim taglist.vim
5.vim环境配置
修改.vimrc文件,使得ctgs、cscope数据库与已安装的插件能够联动。
(1) 查看ctags和scope数据库文件的存放路径,ctags和scope可执行文件的安装路径:
~> whereis ctags
~> whereis cscope
得到ctags和cscope的数据库文件和可执行文件的路径。
(2) vim环境配置文件.vimrc如下
"*******VIM CONFIGURATION*******"
"--------------------------------------------------------------"
" VIM environment configuration
"--------------------------------------------------------------"
set nu "line number
set ai "auto indent
set ts=4 "tab size = 4
set bg=dark "background color
"-------------------------------------------------------------"
"ctags database path configuration
"-------------------------------------------------------------"
"ctgs DB's place <home/code> and change it your own place
" xxx/kernel-code/linux-3.13.0/ is the absolute path
set tags=xxx/kernel-code/linux-3.13.0/tags
"-------------------------------------------------------------"
"cscope database path configuration
"-------------------------------------------------------------"
set csprg=/usr/bin/cscope "cscope's path
set csto=0 "cscope DB search first
set cst "cscope DB tag DB search
set nocsverb "verbose off
"cscope's DB path configuration, change the path <home/code> to absolute path"
" xxx/kernel-code/linux-3.13.0/ is the absolute path
cs add xxx/kernel-code/linux-3.13.0/cscope.out xxx/kernel_v2.6/linux-3.13.0/
set csverb "verbose off
"-------------------------------------------------------------"
"Tag List environment configuration
"-------------------------------------------------------------"
filetype on "vim filetype on
nmap <F7> :TlistToggle<CR> "F7 key = tag list toggling
let Tlist_CTags_Cmd="/usr/bin/ctags" "ctags binary program's path
let Tlist_Inc_Winwidth = 0 "window width change off
let Tlist_Exit_OnlyWindow = 0 "window close = off when tag/file completed
let Tlist_Auto_Open = 0 "window open = off when vim starts
let Tlist_Use_Right_Window = 1 "window open = off when vim starts
"-------------------------------------------------------------"
"source explorer environment configuration
"-------------------------------------------------------------"
nmap <F8> :SrcExplToggle<CR> "F8 key = secExpl Toggling
nmap <C-H> <C-W>h "move window to the left side
nmap <C-J> <C-W>j "move window to the down side(preview)
nmap <C-K> <C-W>k "move window to the upper side
nmap <C-L> <C-W>l "move window to the right side
let g:SrcExpl_winHeight = 8 "set SrcExpl Windows's height
let g:SrcExpl_refreshTime = 100 "refreshing time = 100ms
let g:SrcExpl_jumpKey = "<ENTER>" "jump to the right definition
let g:SrcExpl_gobackKey = "<SPACE>" "back
let g:SrcExpl_isUpdateTags = 0 "tag file update = off
"-------------------------------------------------------------"
"NERD Tree environment configuration
"-------------------------------------------------------------"
let NERDTreeWinPos = "left" "NERD Tree places at the left side
nmap <F9> :NERDTreeToggle<CR> "F9 key = NERD Tree Toggling
6.使用方法
(1) 打开vim后依次敲F7、F8、F9,将会出现Tag List、Source Explorer、NERD Tree三个窗口(从右至左)。VIM示意图如下
(2) 利用四个组合键在不同的窗口进行切换
(3) Cscope常用命令
Cscope 用法:用cscope搜索时,在vim的ex模式下
命令格式,:cs find [queryType] [name]
比如我们要查询kvm_vcpu_init函数,在vim的ex模式下,
queryType = s[0],name = kvm_vcpu_init,然后搜索。
完整命令:cs find s kvm_vcpu_init
7.Cscope与ctags
Ctags能构建源码符号索引,通过索引查找符号所在文件,但Ctags 也有缺点:
(1) 只能进行单向搜索,无法查找调用函数
(2) 无法输出调用该函数的函数;
(3) 无法输出该函数调用的函数;
但cscope可以很好弥补ctags的不足,其将分析文件的目录保存在cscope.files文件中,用命令可生成数据库。
8.源码分析
博主曾用过的内核源码分析工具主要包括source Insight、Understand、SublimeText。source Insight貌似在windows上使用的比较多,无奈博主的操作系统为Linux。Understand也用过,这货太沉重了,跑起来有时候能卡爆机器,再说主要用来找函数间的调用关系,用了一段时间发现源码分析还是得回归到源码本身,关系图也就那么点作用。而且现在使用的就只有SublimeText,感觉是轻便,插件安装想要啥样的都OK,所以说sublime才是博主的最爱。但进入公司上述分析工具就不能堂而皇之的使用了吧!(hehehe ,你懂的!)
开源才是个好东西!