构建ctags+cscope数据库以及vim+plugin实现一个像Source Insight工具分析linux源码

本文详细介绍如何下载并安装Linux内核2.6.30.4版本,配置ctags和cscope数据库,以及安装和设置Vim插件,如SourceExplorer、NERDTree和TagList,用于高效地分析Linux内核源代码。

下载并安装Linux源内核

  1. 下载源内核
   首先下载要分析的对象------Linux内核。进入www.kernel.org下载代码。在网页www.kernel.org中点击 	https://www.kernel.org/pub/链接进入页面,将目录项按照linux-->kernel-->2.6移动,点击代码文件linux2.6.30.4.tar.gz链接进行下载。
  1. 安装源内核

创建目录src,将下载的Linux内核移动到此进行分析。

vmuser@Linux-host:~$mkdir src
vmuser@Linux-host:~$cp  linux2.6.30.4.tar.gz  ./src
vmuser@Linux-host:~$cd src/
vmuser@Linux-host:src$ls
linux-2.6.30.4.tar.gz
vmuser@Linux-host:src$

然后用tar命令解压。

vmuser@Linux-host:src$tar zxvf  linux-2.6.30.4.tar.gz

安装ctags+cscope

  1. 用ctags制作源代码标签

下载ctags

vmuser@Linux-host:linux-2.6.30.4$sudo apt-get install ctags

查看tags.sh文件是否存在

vmuser@Linux-host:linux-2.6.30.4$ ls -la ./scripts/tags.sh 
-rwxrwxr-x 1 vmuser vmuser 4119  7月 31  2009 ./scripts/tags.sh
vmuser@Linux-host:linux-2.6.30.4$ 

使用make生成ARM标签

vmuser@Linux-host:linux-2.6.30.4$ make tags ARCH=arm
vmuser@Linux-host:linux-2.6.30.4$ ls tags -l
-rw-rw-r-- 1 vmuser vmuser 92799435 11月 14 10:25 tags
vmuser@Linux-host:linux-2.6.30.4$
  1. 制作cscope标签数据库

下载cscope

vmuser@Linux-host:linux-2.6.30.4$sudo apt-get install cscope

使用make生成ARM的cscope数据库

vmuser@Linux-host:linux-2.6.30.4$ make cscope ARCH=arm

vim插件下载及环境设置

  1. 下载vim插件

需要Source Explorer、NERD Tree、Tag List插件。vim插件可进入www.vim.org下载。

在这里插入图片描述

在左侧菜单中点击Scripts,显示如下菜单列表,点击其中的Browse all。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. vim+plugin的环境结构

安装已经下载的插件并设置环境。

vmuser@Linux-host:~$ mkdir .vim
vmuser@Linux-host:~$ cp SrcExpl-6.0.zip .vim/
vmuser@Linux-host:~$ cp NERD_tree.zip .vim/
vmuser@Linux-host:~$ cp taglist_46.zip .vim/
vmuser@Linux-host:~$ cd .vim/
vmuser@Linux-host:.vim$ ls
SrcExpl-6.0.zip  NERD_tree.zip  taglist_46.zip
vmuser@Linux-host:.vim$ unzip SrcExpl-6.0.zip 
vmuser@Linux-host:.vim$ unzip NERD_tree.zip 
vmuser@Linux-host:.vim$ unzip taglist_46.zip 
vmuser@Linux-host:.vim$ ls plugin/
NERD_tree.vim  srcexpl.vim  taglist.vim
vmuser@Linux-host:.vim$
  1. vim环境设置

修改vim的环境文件.vimrc,使已安装的插件和ctags、cscope数据库能够联动。
所需信息有ctags和cscope数据库文件的位置、ctags和cscope执行文件的位置。

vmuser@Linux-host:~$ ls /home/vmuser/src/linux-2.6.30.4/tags 
/home/vmuser/src/linux-2.6.30.4/tags
vmuser@Linux-host:~$ ls /home/vmuser/src/linux-2.6.30.4/cscope.out
/home/vmuser/src/linux-2.6.30.4/cscope.out
vmuser@Linux-host:~$ 
vmuser@Linux-host:~$ whereis ctags
ctags: /usr/bin/ctags /usr/bin/X11/ctags /usr/share/man/man1/ctags.1.gz
vmuser@Linux-host:~$ whereis cscope
cscope: /usr/bin/cscope /usr/bin/X11/cscope /usr/share/man/man1/cscope.1.gz
vmuser@Linux-host:~$ vim ~/.vimrc

vimrc配置文件(参考~/.vim/doc/下的NERD_tree.txt 、srcexpl.txt、 taglist.txt)

 set nu      "line number
 set ai      "auto indent
 set ts=4    "tab size 
 set bg=dark "backgroud color
 
 set tags=/home/vmuser/src/linux-2.6.30.4/tags   "ctags 
 
 set csprg=/usr/bin/cscope   "cscope
 set csto=0          "cscope DB search first
 set cst             "cscope DB tag DB search
 set nocsverb        "verbose off
 
 cs add /home/vmuser/src/linux-2.6.30.4/cscope.out   /home/vmuser/src/linux-2.6.30.4
 set csverb          "vetbose off
 
 filetype on                 "vim filetype on
 nmap <F7> :TlistToggle<CR>          "F7 Key = Tag List Toggling
 let Tlist_Ctags_Cmd = "/usr/bin/ctags"      "ctags
 let Tlist_Inc_Winwidth = 0          "window width change off
 let Tlist_Exit_OnlyWindow = 0           "tag/file完成选择时taglist
                         "window close = off
 let Tlist_Auto_Open = 0             "vim开始时window open off
 let Tlist_Use_Right_Window = 1          "vim开始时window open off
 
 nmap <F8> :SrcExplToggle<CR>
 nmap <C-H> <C-W>h
 nmap <C-J> <C-W>j
 nmap <C-K> <C-W>k
 nmap <C-L> <C-W>l
 
 let g:SrcExpl_winHeight = 8
 let g:SrcExpl_refreshTIme = 100
 let g:SrcExpl_jumpKey = "<ENTER>"
 let g:SrcExpl_gobackKey = "<SPACE>"
 let g:SrcExpl_isUpdateTags = 0
 
 let NERDTreeWinPos = "left"
 nmap <F9> :NERDTreeToggle<CR>

查看源码分析环境工具

完成所有操作后,在内核源代码所在目录中执行vim

vmuser@Linux-host:~$ cd src/linux-2.6.30.4/
vmuser@Linux-host:linux-2.6.30.4$ vim

运行vin编辑器后,依次输入F7、F8、F9。显示 Tag List、Source Explorer、NERD Tree分割窗口。

在这里插入图片描述

左侧是NERD Tree Window,右侧是Tag List Window,上方是Source Window,下方是Source Explorer Window。
利用Ctrl+h(移动到左侧窗口)、利用Ctrl+j(移动到下侧窗口)、利用Ctrl+k(移动到上侧窗口)、利用Ctrl+l(移动到右侧窗口)。
进入vim的ex(按Esc键)后,在最下端窗口输入:搜索目标后按Enter,Tag List窗口的光标会移动到"搜索目标"符号,若按Enter键,则代码文件窗口显示定义"搜索目标"的位置。
在代码中移动光标并放在函数或变量上时,下方的Source Explorer Window窗口显示定义相应符号的位置。
移动到下方窗口(Ctrl+j)后,选择列表中的1项(Enter键),在代码窗口显示该文件,按空格键(space bar)即可回到之前的状态。
以上就是使用(与ctags数据库联动的)vim插件构建分析环境的方法。但有时会发现还有仅用ctags无法完成的部分,那就要使用cscope数据库了。
有时Source Explorer Window窗口显示Definition Not Found信息。即无法查到符号。如果分析代码时发生这种情况,可通过cscope指令得到与符号相关的文件列表。在vim的ex模式中输入: cs find s 符号。
从cscope数据库可以找到符号被定义的位置,因此可用cscope弥补ctags中
未能搜索到的部分。

我们通过示例了解的ctags和cscope使用方法只利用了较少命令。其实ctags和cscope提供的搜索命令很多,常用ctags命令如下:

Ctrl+]							移动到定义函数的位置
Ctrl+t							移动到移动前的阶段
:Tselect <function-name>			function-name目录列表
:tnext							出现多个搜索结果时移动到下个结果
:tprevious						移动到之前的结果
:tfrist							移动到第一个目录
:tlast							移动到最后的目录

用cscope搜索时,在vim的ex模式下使用如下格式提问。

:cs find <querytype> <name>

搜索我们曾用作示例的符号时,在vim的ex模式下向querytype输入s、向name中输入要搜素的符号,然后搜索。

:cs find s 符号

cscope提供的多种querytype如下:

querytype								说明
0 or s						查找C符号
1 or g						查找定义(definition)
2 or d						查找被该函数调用的(called)函数
3 or c						查找调用该函数的(calling)函数
4 or t						查找文本字符串(text string)
6 or e						查找egrep范式
7 or f						查找文件
8 or i						查找用#include包含该文件的文件

如想获取关于cscope的帮助,可在vim中转换为ex模式,然后通过:help cscope查看更详细的指令用法。

vim数据库连接插件,便于数据库SQL调试,支持几乎所有的常见数据库, For instructions on installing this file, type :help add-local-help |add-local-help| inside Vim. Homepage: http://vim.sourceforge.net/script.php?script_id=356 SourceForge: $Revision: 1.23 $ *dbext* *dbext.vim* *db_ext* *db_ext.vim* *database-extension* *pgsql* *mysql* *asa* *ase* *ingres* *interbase* *sqlite* *sqlsrv* *ora* *db2* 1. Overview |dbext-overview| 2. Installation |dbext-install| 3. Configuration |dbext-configure| 3.1 Displaying Results |dbext-configure-results| 3.2 Script Variables |dbext-configure-variables| 3.3 Database Specific Options |dbext-configure-options| 3.4 DB2 Modes |dbext-configure-db2| 4. Mappings and commands |dbext-mappings| 5. Adding new database types |dbext-newdb| 6. Prompting for input parameters |dbext-prompting| 7. Setting up connection information |dbext-connect| 7.1 Connection Parameters |dbext-connect-parameters| 7.2 Prompting for Parameters |dbext-connect-prompting| 7.3 Connection profiles |dbext-connect-profiles| 7.4 Connection information in modelines |dbext-connect-modelines| 7.5 Asking for connection parameters |dbext-connect-ask| 8. Creating mappings using dbext commands |dbext-in-mappings| 9. Object Completion |dbext-completion| 10. Listing Objects in the Database |dbext-list-objects| 11. Plugin integration |dbext-integration| 11.1 OMNI completion integration |dbext-omni-completion| 11.2 Intellisense integration |dbext-intellisense| 12. Filetype support |dbext-filetypes| 12.1 Using filetype support |dbext-filetypes-using| 12.2 Adding new filetypes |dbext-filetypes-adding| 13. Using SQL History |dbext-history| 14. Open Source |dbext-sourceforge| 15. Tutorial |dbext-tutorial| {Vi does not have any of this} ------------------------------------------------------------------------------ What's New *dbext-new* Version 4.20 New Features ------------ - Improved support for Cygwin. If you are using a Cygwin compiled Vim (on Windows) and are accessing Windows compiled binaries (i.e. sqlplus.exe) the binary will complain since it does not understand Unix path names. Added the option g:dbext_default_use_win32_filenames which allows you to indicate the binaries must use translated Windows paths instead. (Richard) - DBGetOption displays more information. Bug Fixes ------------ - SQL Server support had issues with the queries when running DBCompleteTable, DBCompleteProcedure, DBCompleteView which also affected the sqlcomplete.vim plugin included with Vim7 (Albie Janse van Rensburg). Version 4.10 New Features ------------ - Updated DBGetOption to additionally display a list of all database profiles and their types. All dbext options that have been overriden via the vimrc are also displayed. Bug Fixes ------------ - db2 support had issues with the queries when running DBCompleteTable, DBCompleteProcedure, DBCompleteView which also affected the sqlcomplete.vim plugin included with Vim7 (Peter Princz). - The documentation was still indicating there was a plugin dependency which has been removed with Vim7. Version 4.00 New Features ------------ - dbext.vim now requires Vim7. - dbext.vim required 2 additional plugins multvals and genutil to operate. These dependencies have been removed by taking advantage of the new Vim7 features (Lists and Dictionaries). - When using the DBCompleteTable, DBCompleteProcedure, DBCompleteView commands errors are displayed instead of silently ignored. This makes them more useful with the sqlComplete plugin (see |sql.txt|). - Added new option, dbext_default_MYSQL_version, for MySQL to indicate the version you using. - You can optionally define a function, DBextPostResult, in your .vimrc, this function will be called each time the result window is updated. This function can be used to do anything, for example, syntax highlighting the result set in the result window.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值