Linux kernel code with cscope and ctags

https://elinux.org/Editor_Tips
http://cscope.sourceforge.net/cscope_vim_tutorial.html

Using cscope and ctags with VIM

You can do symbol navigation with scope and ctags in VIM. This is handy to explore the kernel and to get information about function calls and structures when coding.

install pre-requisites

First, install cscope and exuberant-ctags onto your workstation.

sudo apt-get install cscope exuberant-ctags
These are the packages that handle building and indexing the cscope and ctags database, respectively.

building the cscope database

To build the cscope database for a particular architecture, do:

$ make ARCH=arm cscope
(of course, supply the appropriate architecture for your project on the kernel command line)

This creates the following files in the root of the kernel source tree:

cscope.files - this is the list of files that were indexed by cscope
cscope.out - this is the cscope database
cscope.out.in and cscope.out.po - these are a reverse index, to speed up lookups in the database

building the ctags database

To build the ctags database, do:

$ make ARCH=arm tags
This creates the file ‘tags’ in the root of the kernel source tree.

navigating symbols in vim

To use cscope symbols in vim, do the following:

To open the editor and position the cursor to where a particular tag is located, do:

$ vi -t tag
For example:

$ vi -t start_kernel
This will open init/main.c and position the cursor on the ‘start_kernel’ function.

From inside the editor, to navigate to the symbol that is under the cursor:

ctrl-]
then select one of the references. Use ctrl-T to return to where you jumped from.

other commands

You can use colon commands to perform specific operations:

:ta tag - to find a tag
:cs find g name - find the definition for ‘name’
:cs find d func - find functions called by ‘func’
:cs find c func - find functions that call ‘func’
:cs help - show cscope help in VIM
See http://cscope.sourceforge.net/cscope_vim_tutorial.html for more information.

The Vim/Cscope tutorial

  • Download the cscope_maps.vim file, and arrange for it to be read by Vim at startup time. If you are using Vim 6.x, stick the file in your $HOME/.vim/plugin directory (or in any other ‘plugin’ subdirectory in your ‘runtimepath’). If you are using Vim 5.x, you can either cut and paste the entire contents of the cscope_maps file into your $HOME/.vimrc file, or stick a “source cscope_maps.vim” line into your .vimrc file.

  • Start up Vim. If you want, you can start it with a C symbol (ex: ‘vim -t main’), and you should hop right to the definition of that symbol in your code.

  • Put the cursor over a C symbol that is used in several places in your program. Type “CTRL-\ s” (Control-backslash, then just ‘s’) in quick succession, and you should see a menu at the bottom of your Vim window showing you all the uses of the symbol in the program. Select one of them and hit enter, and you’ll jump to that use. As with ctags, you can hit “CTRL-t” to jump back to your original location before the search (and you can nest searches and CTRL-t will unwind them one at a time).
    Mnemonic: the ‘’ key is right next to the ‘]’ key, which is used for ctags searches.

  • Try the same search, but this time via “CTRL-spacebar s”. This time, your Vim window will split in two horizontally , and the Cscope search result will be put in the new window. [if you’ve never used multiple Vim windows before: move between windows via ‘CTRL-W w’ (or CTRL-W arrow key, or CTRL-W h/j/k/l for left/up/down/right), close a window via ‘CTRL-W c’ (or good old ‘:q’), make the current window the only one via ‘CTRL-W o’, split a window into two via ‘CTRL-W s’ (or ‘CTRL-W v’ for a vertical split), open a file in a new window via ‘:spl[it] filename’]
    Mnemonic: there’s now a big, spacebar-like bar across the middle of your screen separating your Vim windows.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值