Use cscope to read the Linux source code

Source: Internet
Author: User

Source: Liu Jianwen | academic half-it Song-Wen (http://arttech.us)

Csflood is a magic horse

What is cssag? First look at the official definition:

"Cscope is an interactive, screen-oriented tool that allows the user to browse through C source files for specified elements of Code ."

Simplified translation and added: "cssag is a screen-oriented (opposite to line-oriented) Interactive CSource codeBrowseProgram. It canCode(For example, various labels: variables, macros, and function calls). It provides a simple character query interface, you can query and view its definition and reference based on [element name."

The preceding is a strict and complete definition of cssag. This definition indicates that cssag has two main functions: [Symbol Index] for the C source code file, and a simple query interface. If you only use advanced tools such as Vim to edit and read the source code, you can fully understand cssag as a tool for generating the source code [Symbol reference database.

Source files)

The source file is the c source file of cssag. There are several source files,

    • 1. Default: Current Directory (including subdirectories, using the-R parameter);
    • second, enter the command line.
    • 3. Source Code File List (as defined below);
    • fourth, contains the directory.

A simple understanding of these four file sources is that a directory is a set of files; a command line is one or several files; a directory contains files in a file; the file list is a list of files for cspers. It is a simple and custom file that cspers can find on their own.

Since there are multiple file sources to be searched, the order of source code to be searched is also the above Order when multiple sources are provided for cssag.

Include dir)

If any C source code contains a file, if it is not an absolute path (starting with a backslash) and is not in [command line Input] or [source code file list, cssag searches for the [cssag Directory], which is specified by the command line parameter-I or the environment variable $ incdir.

Source code file list (list file) cscope. Files

The source code file list function improves the flexibility of csflood. It allows csflood to search for similar files without limitation, and the source code is C source code. The default source code file list is the csfiles. files of the current directory, which can also be customized using the-I parameter.

Symbol reference database cssag. Out

The index output of CSLE is a symble cross-reference file called CSLE. Out. The name has a profound connotation, but it is somewhat academic and hard to remember. This document is simplified to [Symbol reference database]. The format of the database, the query principle, and the database name can be customized using the parameter-f.

Use cssag to read the core source code of Linux

1. Determine the project source code location, such as/home/Kemin/linux-2.6.31;

2. select a location for saving the cscope data (including the [source code list file] and [Symbol reference database]), such as/home/Kemin/cscope/linux_no_driver. For relatively fixed large projects, it is necessary to do so.

3. If necessary, it can be converted into a [source code list file] indexed by csplus, triggering csplus to index these files, rather than fully indexing the current directory tree. There are two situations where it is necessary to do this:

First, you can selectively scan the project source code file to filter out code that you are not interested in. For example, you can filter out documents (DOC) and scripts in the Linux source code directory) and driver:

Lnx =/home/Kemin/linux-2.6.31
CD/
Find $ lnx \
-Path "$ lnx/ARCH /*"! -Path "$ lnx/ARCH/i386 *"-prune-O \
-Path "$ lnx/include/ASM -*"! -Path "$ lnx/include/asm-i386 *"-prune-O \
-Path "$ lnx/tmp *"-prune-O \
-Path "$ lnx/Documentation *"-prune-O \
-Path "$ lnx/scripts *"-prune-O \
-Path "$ lnx/drivers *"-prune-O \
-Name "*. [chxss]"-print>/home/jru/cscope. Files

Second, the project is not a C Language Project, and the file type is. C,. H,. Y, or. L. For example, creating an index for a Java project:

$ CD/; find/My/project/Dir-name '*. Java'>/My/cs1_/ DIR/cs1_. files

Note:: The above two scripts are first forwarded to the root directory (CD/) for operation. In this way, the generated [Symbol reference database] has no dependency on the generated directory, the editing program can run in any directory. You only need to set the environment variable $ cscope_db to [Symbol reference database:

Cscope_db =/Foo/cs1_. out; export cscope_db

In particular, if the conditions are complex and find is difficult to write, you can directly edit the cs.pdf. Files file.

4. Generate [symbolic reference database] cscope. Out

If it is a simple test project, you can directly execute the following command in the source code Directory: cssag or cs1_- R and then call the editing program for browsing. Otherwise, as in this experiment, go to the specified directory for operations:

$ CD/home/Kemin/cs.pdf/linux_no_driver # The Directory with 'cs;. files'
$ Cscope-B-Q-K

Semantics of the three parameters:

  • The-B flag tells csust to just build the database, and not launch the cscope GUI.
  • The-Q causes an additional, 'inverted Index' file to be created, which makes searches run much faster for large databases.
  • Finally, -K sets cssets's 'kernel 'mode -- it will not look in/usr/include for any header files that are # supported ded in your source files (this is mainly useful when you are using cscope with operating system and/or C library source code, as we are here ).

Finally, three files are generated: results in 3 files (cs;. out, cs;. in. Out, And cs;. Po. out)

5. Use of csflood

Cssag comes with a character-based query interface (cssag GUI). When you directly execute cssag in the source code directory (without any parameters), the default behavior of cssag is to index the current directory or cssag. files specifies the source code file to generate a [Symbol reference database] cscope. out (if the database already exists, update it) and call out the cssag gui. If you select the query result, the specified editing program (specified by $ editor or the default VI) will be called out for viewing. -Parameter B only generates the database, and parameter-d only calls out the cssag GUI without updating the database.

The use of cssag GUI is very primitive. It is best to use it with some advanced editing programs. Let's talk about Vim below.

Use with vim

1. Check the vim version, which is later than 6.x, and support for cscope is enabled during compilation. The compile switch is -- enable-cscope.

2. download and install the script file cscope_maps.vim (http://cscope.sourceforge.net/cscope_maps.vim), usually just copy this to the sub-plugin directory under the vim installation directory (you can use the vim command: scriptename to view ).

3. cursor search

put the cursor over a C symbol,

  • current window search

    • type" Ctrl-\ s "(Control-backslash, then just's '), the '\ 'key is right next to the'] 'key, which is used for ctags searches
    • hit" Ctrl-T "to jump back
  • Split Window search
    • Horizontal segmentation: "Ctrl-spacebar S"
    • Vertical Split: Ctrl-spacebar s

4. Traditional command line search

    • ": Cs0000find symbol foo" (or, more tersely, ": cs f s foo ").
    • To do the Horizontal Split Version, use ": scscope" (or just ": SCS ")

5. Four common search targets

    • 's ', for 'Find all uses of symbol x '.
    • 'G' finds the global definition (s) of a symbol,
    • 'C' finds all callto a function,
    • 'F' opens the filename under the cursor

6.6 use the vim Multi-Window Function

  • Switching between windows: 'ctrl-W W' (or CTRL-W arrow key, or CTRL-W H/J/k/L for left/up/down/right ),
  • Close the current window: 'ctrl-W C' (or good old ': Q '),
  • Close other windows: 'ctrl-W o ',
  • Split the current window: 'ctrl-W s' (or 'ctrl-W V' for a vertical split ),
  • Create a file in the new window: ': SPL [it] filename']

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.