Use VIM + ctags + csags to read source code

Source: Internet
Author: User
Tags perl script

-- Kong Jianjun (kongove. cn)
2008.11.15

Code Reading tools

For those who are learning the Linux kernel, reading the source code is particularly important.

Because all design ideas and internal mechanisms are
It is implemented in the form of code, and all the materials are also to better interpret the code. So a good reading tool can
It can improve the reading efficiency and effect. Common Code Reading tools include source navigator and source insight.
, Lxr, csscope, kscope, etc.

• "Source Navigator" is a red hat company released with gnu gpl and can be downloaded and used from the official website [1.
• "Source insight" is currently only available on Windows platforms. its official website is [2]. You must register to use the service, or
Download the registration machine from the Internet to generate a registration code. In Linux, wine simulation can be used conveniently, but after all
It is a Windows platform and cannot be used freely.
• Lxr (Linux cross reference) [3] builds an index database for the program source code and uses the Perl script CGI to dynamically
Generate a Web page containing the source code, which can be viewed in any browser. The disadvantage is that it requires server support and
Speed.
• "Cscope" [4] is a terminal-based code reading tool that consumes less resources and operates on lines loyal to command lines.
It is more convenient and flexible to use. This is also an important reason for the recommendation in this article. Some
Cssag and emasc are bound to read source code. Of course, the selection of tools also depends on personal habits.

VIM + ctags + cs.pdf

Csflood requires the cooperation of vim and ctags, which are based on command lines. In ubuntu, you only need
Run "sudo apt-Get install cs1_cs1_- indexer ctags vim-full" to install the software.
The following describes the common methods of the three tools. For more functions, refer to the man manual or official documents.

Vim

Vim is regarded as a text editor specially designed for programmers. Its rich editing commands are common simple characters.
Users are easy to use. Vim can highlight the syntax of more than 180 languages and automatically indent the C language. If it is true, it matches the expression characters.
Search is powerful and supports multiple operating system platforms. We will not explain how to use Vim here. Vim Chinese Text
File [5].
Vim installed by default in Ubuntu does not have the syntax brightening function. Therefore, install vim-full and configure it in vim.
File ~ /. Add a line "syntax on" to vimrc so that the source code opened in VIM will be highlighted by syntax.
Vim comes with many color themes. You can directly select the following line and add it to the vim configuration file.
Valid.

Colorscheme elflord "I use this
Colorscheme darkblue
Colorscheme evening
Colorscheme Murphy
Colorscheme torte
Colorscheme desert

Ctags

Run the ctags-R command in the source code root directory to generate a Tag file for the program source code. The-r option indicates
Operation. A label file is also generated for the subdirectory. Vim can search and
Switch back and forth between C language elements of different files.

Simple use of ctags in VIM
1) Jump to the specified function and enter Vim. Use ": Tag func_name" to jump to the function func_name. Use tag
Command, you can use the tab key for matching and continue to press the tab key to switch down.
When a function has multiple definitions

: Tag
Jump to the first definition and jump to the current file first.
: Tnext
Jump to the first
: Tfirst
Jump to the previous count
: [Count] tprevious
Jump to the next count
: [Count] tnext
Jump to the last
: Tlast
You can also choose from all tagnames:
: Tselect tagname

If you want to jump to the identifier containing the block: "tag/block", use the tab key to select. Here '/' is to tell Vim
'Block' is a statement block tag.
2) use the "Ctrl +]" shortcut to jump to the definition of the Function Identifier where the cursor is located.
3) Use "Ctrl + T" to return to the upper layer. If you want to select the identifier starting with write _, tselect/^
Write _ here, '^' indicates the beginning. Similarly, '$' indicates the end. Multiple identifiers with the same name

Cscope

Run the cssag command. Two panels are displayed. The top is a display panel of the search result, and the bottom is a search condition.
Specify the panel. Use the tab key to switch between the two panels. You can also use the top, bottom, left, and top pages keys in the same panel.
Change the position. Select an item in the display panel and press enter to enter the file. This is to call Vim to open the file.
It can be used in combination with ctags. Of course, you can also directly use Vim to open the file and read the source code in combination with ctags.
Before use, you must use the "cssag-indexer-R" command to recursively generate the index information file. In special cases
You must use the find command to generate an index information file and specify it to the cssag tool. Cssag provides the following
Nine query methods:

Find this C symbol:
# Search for the specified C symbol
Find this global definition:
# Search for the specified global definition
Find functions called by this function:
# Find the function called by the specified function
Find functions calling this function:
# Find the function that calls the specified function
Find this text string:
# Searching strings
Change this text string:
# Modifying a specified string
Find this egrep pattern:
# Search for matching characters
Find this file:
# Searching for a specified file
Find Files # Including this file:
# Specify the reference header file for search

Enter the Search Condition in the corresponding item, press enter to perform the query, and the result is displayed on the display panel.

Application Instance

The following uses cssag to read the kernel source code as an example:

$ Wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.27.6.tar.bz2
#
Download the kernel source code from the Linux kernel Official Website
$ Tar xvfj linux-2.6.27.6.tar.bz2
# Decompress the file
$ Linux-2.6.27.6 CD
# Enter the source code root directory
$ Ctags-R
# Recursively generate tag files
$ Cscope-indexer-R
# Recursively generate index information files
$ Cscope
# Use cscope to read the source code

The tag file and index information file only need to be generated for the first time or when the code changes, and will be run directly later.
Cscope.

[1] http://sourcenav.sourceforge.net/

[2] http://www.sourceinsight.com/

[3] http://lxr.linux.no/

[4] http://cscope.sourceforge.net/

[5] http://vcd.gro.clinux.org/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.