Linux Source code analysis tool chain

Source: Internet
Author: User
Tags doxygen egrep

Objective

Looking at the source code is a programmer must go through the things, but also can improve the ability of a shortcut. Personally think: to fully master a software method only read the source code.

Under Windows has sourceinsight this source reading software (although I did not use, but the online evaluation is good), because I am a linuxer, do not like to use Windows, so naturally choose to read the source under Linux tools.

Below I will introduce the tools to read the source code under Linux.

Vim+ctags+cscope

SOURCE Reading Three Musketeers: Vim with Ctags and cscope, enough to fly freely in the source code, between functions and variables freely jump.

Installation
    1. sudo apt-get install vim ctags cscope
Vim

The use of vim has been skipped, many online materials, not an article can be said, the future I may write a vim of the use of experience. Recommend a concise Vim training strategy, read it will almost use.

Ctags

The use of ctags is simple, and vim has built-in support for Ctags.

First in the source code root directory execution ctags -R , recursive for the code to build tags, in the root directory will generate a tags file, store various functions and tags tag, easy to jump:

    • Make the cursor on a function or variable to Ctrl + ] jump to its definition
    • Ctrl + tYou can go back to where you were before you jumped.

For the simple code, Ctags is enough, but for the more complex code, Ctags seems a little overwhelmed, so, the next swordsman on the debut.

Cscope

Vim also has a built-in support for Cscope.

The cscope -Rbq cscope.out file (index database) is generated by first executing in the source code root directory

    • -r: Searching for code in the subtree when building an index file
    • -B: Only the index file is generated and does not enter the Cscope interface
    • -Q: Generate Cscope.in.out and cscope.po.out files to speed up cscope indexing

Then execute in Vim :cs add cscope.out and add the database.

You can then use :cs find x var the lookup. (x is the query option, VAR indicates the function or variable name to find)

Cscope supports 8 ways of querying

    • S: Find the C language symbol, that is, find the function name, macro, enumeration value and other occurrences of the place
    • G: Find the location defined by functions, macros, enumerations, and similar functions provided by ctags
    • D: Find functions called by this function
    • C: Find functions that call this function
    • T: finds the specified string
    • E: Find Egrep mode, equivalent to Egrep function, but find much faster
    • F: Find and open files, like Vim's find function
    • I: Find files that contain this file

For example, we would like to find a function called Do_cscope () function in the source code of Vim 7.0, we can enter: ": CS Find C Do_cscope", after the return to find no matching function, there may be no function call Do_cscope (). We then enter ": CS find S Do_cscope" to find out where the C symbol appears, and now vim lists all the locations where the symbol appears.

Every time there is input cs find to find the data is not a bit troublesome, there is no more convenient way. Of course, the magic of Vim lies in its customizable nature. Provide a copy of the Cscope configuration and place it in the. vimrc.

  1. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  2. " cscope setting
  3. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  4. if has("cscope")
  5. set csprg=/usr/bin/cscope
  6. set csto=1
  7. set cst
  8. set nocsverb
  9. " add any database in current directory
  10. if filereadable("cscope.out")
  11. cs add cscope.out
  12. endif
  13. set csverb
  14. endif
  15. nmap <C[email protected]>s :cs find s <C-R>=expand("<cword>")<CR><CR>
  16. nmap <c[email  protected]>g :cs find G <c-r>=expand ( "<cword>" ) <CR ><cr>
  17. nmap <c[email  protected]>c :cs find C <c-r>=expand ( "<cword>" ) <CR ><cr>
  18. nmap <c[email  protected]>t :cs find T <c-r>=expand ( "<cword>" ) <CR ><cr>
  19. nmap <c[email  protected]>e :cs find e <c-r>=expand ( "<cword>" ) <CR ><cr>
  20. nmap <c[email  protected]>f :cs find F <c-r>=expand ( "<cfile>" ) <CR ><cr>
  21. nmap <c[email  protected]>i :cs find I ^<< Span class= "PLN" >c-r>=expand ( "<cfile>" ) <CR >$<cr>
  22. nmap <C[email protected]>d :cs find d <C-R>=expand("<cword>")<CR><CR>

The above configuration is Ctrl + @ used as :cs find the shortcut key, that is, to find a function name, only need to put the cursor on the function name, press Ctrl + @ + s can, simply not more convenient. And you can modify the configuration yourself and map to the shortcut keys that you feel comfortable with.

In line with the principle of not repeating the wheel, I simply wrote the basic usage of cscope, in fact, basically enough, advanced usage reference this article Vi/vim use Advanced: Programmer's Tool –cscope.

Doxygen

Above we are talking about using Vim to view the source code, but in the face of hundreds of thousands of code, want to see clearly the relationship between the structure is not what vim can do. That's when we need Doxygen to help.

Doxygen is the de facto standard tool for generating documentation from annotated C + + sources, but it also supports other Popular programming languages such as C, Objective-c, C #, PHP, Java, Python, IDL (Corba, Microsoft, and Uno/openoffice FLA VORs), Fortran, VHDL, TCL, and to some extent D.

Doxygen is a tool for generating documents based on source code, which is primarily for C + +, but it also supports other languages.

Install directly to the official website, follow its instructions to install.

Here's how to use Doxygen.

First, at the root of the source code execution doxygen -g , and then in the root will suddenly pop up a file named doxyfile , this file is the Doxygen generated the document configuration file.

So, the point is, how to configure this file, the default configuration is to generate the member data of each structure according to the code, and then generate the HTML and latex two folders, respectively, Web pages and latex documents.

To tell the truth, the default generated documents are not egg-free, except to make it easier for you to see the constituent members of each structure. We are concerned with the relationship between the various structures, which is for C. For the purposes of C + + and Java, the relationship between classes is of concern. Therefore, the default configuration is definitely to be modified. As for how to change, look at the official documents.

Of course, if the only advice given in this article is to read the document, what is the point of this article? We all know that reading documents can solve the problem, but the time cost is too high, and it is in English. The purpose of writing this article is to share their learning experience, so that people less detours, if not solve your problem, you can only read the document.

Next, I'll explain to you the configuration I used to read the document.

First of all, the doxygen inside of the configuration is smelly and long, you will never have to read its desire. So I give a few key configuration items, then search it to modify it

  • This is the option to optimize the output for a variety of languages, no by default, because it is unclear what language you are using (say the suffix don't know ...)

    1. OPTIMIZE_OUTPUT_FOR_C
    2. OPTIMIZE_OUTPUT_JAVA
    3. OPTIMIZE_FOR_FORTRAN
    4. OPTIMIZE_OUTPUT_VHDL
  • This is the option to generate the type of document, the default generation of HTML and latex, a total of six types of documents, each type of build configuration is also many, depending on the need to configure the

    1. generate_html
    2. generate_latex
    3. generate_rtf
    4. generate_xml
    5. generate_docbook
    6. generate_man
  • About options for generating images. Doxygen uses dot as the tool to draw, so you need to perform the sudo apt-get install graphviz installation dot first. After setting this drawing option, Doxygen generates the relationships for each struct, and for the class it generates a function call relationship (I haven't tried it because I've tried C only).

    1. HAVE_DOT (一定要置为YES,后面的选项都依赖这个)
    2. DOT_NUM_THREADS (使用dot绘图的线程数量,越多越快,我一般是设置跟cpu的核数一样)

To a struct-body diagram, which is doxygen generated.

[Machine_class]

The final step, executed at the source root doxygen , will automatically find the Doxygen configuration and generate the document based on the configuration.

Gdb

The last tool, the famous gdb. The best way to analyze the source code execution process is to run it and then step through it. The best tool for observing it is, of course, GDB (for C/s + +).

GDB use I also do not intend to build wheels, direct reference with the GDB debugging program, this write is very good, easy to understand.

Postscript

The above is the time I read the source of the use of Linux tools, Three Musketeers Vim+ctags+cscope, two axe Doxygen gdb, enough to gallop the source code of the lake.

The above tools, vim and GDB are the hardest to learn and the learning curve is steep. But after learning, you can keep the cloud open to see the moon Ming.

Reference article, thanks for help: Vi/vim Use advanced: Programmer's weapon –cscope

Linux Source code analysis tool chain

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.