使用vim查看linux kernel
阅读数:496 评论数:0
跳转到新版页面分类
C/C++
正文
一、linux kernel与常规C项目的区别
1、Linux内核是一个非常大的项目,所以需要我们有选择的进行代码索引。
2、Linux内核是架构相关,但是我们一般只关注于某一个选定架构,所以不需要索引其它的架构相关代码。
3、Linux内核有特定的编码风格。
4、Linux内核没有使用标准的C库,而是使用自己类似功能的函数,所以不需要索引libc库。
5、vim多窗口使用技巧
(1):tabc 关闭当前窗口
(2):tabo 关闭所有窗口
(3)ctrl+w 依次切换窗口
(4)打开文件到指定行 vim +linenumber filename,如vim +5 ~/.vimrc
(5):sp file-path 多窗口打开
(6)gf go to file
(7)bf back to file
二、配置vim环境
为了在内核代码中进行导航,可以安装cscope和ctags工具。
yum install cscope ctags
# 或者deebian下
apt-get install ctags cscope
(1)cscope
常用于函数之间的代码导航,它也可以跳转到符号定义。
(2)ctags
Tagbar插件和Omni completion插件都会依赖它,同时它也可以用于导航,但对于C语言,导航功能还是cscope更好一些,因为ctags不能跳转到符号的调用者,只能跳转到符号的定义处。
现在我们要对内核代码进行索引,有两个方法:
(1)手动创建索引
ctags -R
ctrl+] //跳转到光标所在变量的定义处
ctrl+t //返回查找
:tp // 上一个tag标记文件,
:tn // 下一个tag标记文件中
vim -t tag //跳转到tag所有文件的定义处
find . -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files
cscope -Rbkq -i cscope.files
R表示把所有子目录里的文件也索引
b表示scope不启动自带的用户界面,而仅仅建立符号数据库
q生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
k生成索引文件时,不搜索/usr/include目录
cscope命令:
add : Add a new database (Usage: add file|dir [pre-path] [flags])
find : Query for a pattern (Usage: find c|d|e|f|g|i|s|t name)
c: Find functions calling this function,调用本函数的地方
d: Find functions called by this function,本函数调用的函数
e: Find this egrep pattern
f: Find this file,查找并打开文件
g: Find this definition,类似ctags
i: Find files #including this file
s: Find this C symbol,查找C语言的符号
t: Find assignments to,查找指定的字符串
help : Show this message (Usage: help)
kill : Kill a connection (Usage: kill #)
reset: Reinit all connections (Usage: reset)
show : Show connections (Usage: show)
(2)使用内核代码中的脚本(即3中的方法)
如果你自己不能确定使用哪种方式,推荐使用内核中的脚本,当然你需要先在你的机器上配置和编译内核,因为生成的文件可用于完善后续的索引过程。
这是内核提供的一个创建内核索引数据库的脚本,我们需要使用make cscope和make tags规则来创建索引,而不是直接运行这个脚本 。
如:
cp -v /boot/config-$(uname -r) .config
make oldconfig
make prepare
make O=. ARCH=arm SUBARCH=omap2 COMPILED_SOURCE=1 cscope tagscp
如果报bison not found ,安装bison flex
然后在~/.vimrc中添加
if filereadable("cscope.out)
cs add cscope.out
endif
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>
注意:先按ctrl不放,按\键,同时放开,快速按g键!
(1)O=.
如果你想在内核目录外部加载创建的cscope/ctags索引文件,如开发out-of-tree 内核模块,使用绝对路径;如果你只是在内核目录内开发,使用相对路径时,可以忽略这个参数。
(2)ARCH=...
选择索引的CPU架构,可以参考 arch/目录。例如ARCH=arm,那么arch/arm/目录下的内容会被索引,其它arch/*目录会被忽略。
(3)SUBARCH=...
选择索引的子架构,如主版相关的文件 。例如SUBARCH=omap2,那么只有arch/arm/mach-omap2/和arch/arm/plat-omap/目录会被索引。
(4)COMPILED_SOURCE=1
表示只索引编译文件。如果你想索引未编译的文件,只要忽略这个参数。
(5)cscopte和tags
表示创建cscope和ctags索引的规则。
如果使用的是vim7可以使用pathogen来管理插件,如果是vim8可以使用自带的插件管理功能。下面以pathogen为例:
mkdir -p ~/.vim/autoload ~/.vim/bundle &&
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
#然后在~/.vimrc中添加
execute pathogen#infect()
syntax on
filetype plugin indent on
#安装插件时
cd ~/.vim/bundle &&
git clone https://github.com/tpope/vim-sensible.git
git clone https://github.com/joe-skb7/cscope-maps
git clone https://github.com/vivien/vim-linux-coding-style
默认情况下,插件对所有Linux project的文件都生效,可以配置为只对指定目录生效
#~/.vimrc
let g:linuxsty_patterns = ["/home/xxx/linux"]
git clone https://github.com/preservim/nerdtree.git ~/vim/bundle/nerdtree
#~/.vimrc
call pathogen#infect()
syntax on
filetype plugin indent on
通过:NERDTree可以打开树,也可以配置为vim打开时自动打开,参考官方文档。
git clone https://github.com/preservim/tagbar
#在~/.vimrc中
nmap <F8> :TagbarToggle<CR>
这样可以通过F8切换tagbar的关开。
会在vim的底部显示一行状态。
git clone http://github.com/vim-airline/vim-airline
更新使用技巧参考项目git
这是一个语法检查的插件,
git clone http://github.com/vim-syntastic/syntastic
#~/.vimrc
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastis_check_on_wq = 0
let g:ycm_show_diagnostics_ui = 0
如果出现[>4;2m这样的错误,通过在~/.vimrc中添加
let &t_TI=""
let &t_TE=""
解决问题。
这个插件安装比较麻烦,主要是因为国内访问外国的一些网址比较慢。
先更新vim为vim8
yum install centos-release-scl
yum install rh-python36.x86_64
scl enable rh-python36 bash
yum install -y ncurses-devel ruby ruby-devel lua lua-devel luajit luajit-devel ctags tcl-devel perl perl-devel perl-ExtUtils-ParseXS perl-ExtUtils-XSpp perl-ExtUtils-CBuilder perl-ExtUtils-Embed
git clone https://github.com/vim/vim.gt
cd vim
./configure --with-features= huge --enable-multibyte --enable-rubyinterp=yes --enable-python3interp=yes --with-python3-config-dir=$(python-config --configdir) --enable-perlinterp=yes --enable-luainterp=yes --enable-gui=yes --enable-gui=gtk2 --enable-scope --prefix=/usr/local
make VIMRUNYTIMEDIR=/usr/local/share/vim/vim82
make install
最后通过vim --version|grep python查看python3前是否有+号,没有+号后续无法成功。
make clean 清除上次make命令产生的object文件及可执行文件,make distclean类似make clean,但同时也将configure生成的文件全部删除掉,包括makefile。
安装ycm
git clone https://github.com.cnpmjs.org/ycm-core/YouCompleteMe
#下载成功后,修改.gitmodules,把github.com改为github.com.cnpmjs.org这样可以提高速度
git submodule update --init --recursive
#再进入third_part/ycmd/中修改.gitmodules,把github.com改为github.com.cnpmjs.org
git submodule update --init --recursive
python3 install.py --clangd-completer
相关配置参考ycm项目git
(1)80字符
内核代码一行最多80个字符
set colorcolumn=81
highlight ColorColumn ctermbg=Black ctermfg=DarkRed
(2)尾部不允许多余的空格
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()