Linux source code analysis tool chain

Source: Internet
Author: User

Linux source code analysis tool chain
Preface

Viewing source code is a must for programmers and a shortcut to improving their capabilities. I personally think: the only way to fully master a software is to read the source code.

I have the source code reading software sourceinsight in Windows (although I have never used it, it is a good evaluation on the internet). Because I am a Linuxer, I don't like to use Windows, therefore, it is natural to choose a tool to read the source code in Linux.

Next I will introduce the tool for reading the source code in Linux.

 

Vim + ctags + cs.pdf

Three muskeys for source code reading: vim works with ctags and csags, which is sufficient to fly freely in the source code and jump between functions and variables.

 

Install
  1. sudo apt-get install vim ctags cscope

 

Vim

The use of vim is skipped. There are a lot of online materials, not an article. In the future, I may write an article about vim. We recommend a concise Vim leveling strategy, which will be used after reading it.

 

Ctags

Ctags is easy to use. vim has built-in support for ctags.

First, run the command in the source code root directory.ctags -RRecursively create a tags for the source code. A Tags file is generated in the root directory to store tags of various functions and variables for easy jump:

  • Place the cursor on a function or variable,Ctrl + ]You can jump to its definition.
  • Ctrl + tYou can go back to your previous position

For simple code, ctags is enough, but for complicated code, ctags seems a little inadequate, so the next swordsman will be on the stage.

 

Cscope

Vim also has built-in support for cscope.

First, run the command in the source code root directory.cscope -RbqThe cssag. out file (index database) is generated)

  • -R: Search for the code in the subdirectory tree when generating the index file
  • -B: Only the index file is generated, and the cssag interface is not displayed.
  • -Q: generate the cssag. in. out and cssag. po. out files to speed up the cssag index.

Then execute:cs add cscope.outTo add a database.

You can use:cs find x var. (X indicates the query option, and var indicates the name of the function or variable to be searched)

Cssag supports eight query methods

  • S: Find the C language symbol, that is, find the place where the function name, Macro, enumerated value, and so on appear
  • G: Find the locations defined by functions, macros, and enumeration, similar to the functions provided by ctags.
  • D: Find the function called by this function.
  • C: Find the function that calls this function.
  • T: Find the specified string
  • E: Find the egrep mode, which is equivalent to the egrep function, but the search speed is much faster.
  • F: find and open the file, similar to the find function of vim.
  • I: Find the file that contains this file

For example, if we want to find the function that calls the do_cscope () function in the source code of vim 7.0, We can enter: ": cs find c do_cscope" and press enter to find that the matching function is not found, there may be no function to call do_cs1 (). Enter ": cs find s do_cscope" to find the location where the C symbol appears. Now vim lists all the locations where the symbol appears.

Input every timecs findIs it a bit difficult to find data? Is there any more convenient way. Of course, the magic of vim is its customization. Provide a cssag configuration and place it in. 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-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>
  16. nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>
  17. nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>
  18. nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>
  19. nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>
  20. nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
  21. nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  22. nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>

In the preceding configurationCtrl + @As:cs findTo find a function name, you only need to place the cursor on the function name and pressCtrl + @ + sThat's easy. In addition, you can modify the configuration and map it to the shortcut keys that you feel comfortable.

Based on the principle of not repeating the wheel building, I simply wrote the basic usage of cssag. In fact, it is basically enough. For advanced usage, refer to this article vi/vim for advanced usage: cscope, a tool for programmers.

 

Doxygen

We are talking about using vim to view the source code. But when dealing with hundreds of thousands of codes, it is not what vim can do to see the relationship between the structures. At this time, 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 flavors), Fortran, VHDL, Tcl, and to some extent D.

Doxygen is a tool for generating documents based on source code. Although it is mainly used for C ++, it also supports other languages.

Go to the official website for installation and follow the instructions.

The following describes how to use doxygen.

First, executedoxygen -gThen, a file named Doxyfile will pop up in the root directory. This file is the configuration file generated by doxygen.

The key point is how to configure this file. The default configuration is to generate the member data of each struct according to the code, and then generate two folders, html and latex, respectively, webpage and latex documents.

To be honest, the documents generated by default are useless, except for making it easier for you to see the members of each struct. What we care about is the relationship between various struct, which is for C. For C ++ and java, you are concerned about the relationship between classes. Therefore, the default configuration must be modified. For how to change it, read the official documentation.

Of course, if the only suggestion in this article is to read the document, what is the significance of this article. Everyone knows that reading documents can solve the problem, but the time is too high and it is in English. The purpose of writing this article is to share your learning experience and avoid detours. If you cannot solve your problem, you can only read the document.

Next, I will explain the configurations used in my own documents.

First, the configuration in Doxygen is smelly and long, and you will never have the desire to read it. So I will give you several key configuration items, and then you can search for them to modify them.

  • This is an option for optimizing output for various languages. The default value is "NO", because it does not know the language you are using. (Do you know the suffix ...)

    1. OPTIMIZE_OUTPUT_FOR_C
    2. OPTIMIZE_OUTPUT_JAVA
    3. OPTIMIZE_FOR_FORTRAN
    4. OPTIMIZE_OUTPUT_VHDL
  • This is an option for document generation. html and latex are generated by default. six types of documents are supported. There are many configurations for each type of documents, which can be configured as needed.

    1. GENERATE_HTML
    2. GENERATE_LATEX
    3. GENERATE_RTF
    4. GENERATE_XML
    5. GENERATE_DOCBOOK
    6. GENERATE_MAN
  • Options for generating images. Doxygen uses the dot tool for plotting, So executesudo apt-get install graphvizInstall dot. After setting this drawing option, doxygen will generate the relationship between the various structs, and function call relationships will be generated for the class (I have not tried it because I only tried C ).

    1. HAVE_DOT (Be sure to set it to YES, and the following options depend on this)
    2. DOT_NUM_THREADS)

A struct graph is generated by doxygen.

[Machine_class]

The last step is to run the command in the source code root directory.doxygenIt will automatically find the Doxygen configuration and generate a document based on the configuration.

 

Gdb

The last tool is the well-known gdb. The best way to analyze the source code execution process is to run it and then execute it step by step. The best tool for observing it is gdb (for C/C ++ ).

I am not planning to create a wheel for using gdb. I can directly refer to the GDB debugging program. This article is well written and easy to understand.

 

Postscript

The above is the Linux tool I used when reading the source code. The three muskeys vim + ctags + cs.pdf, and the two axes doxygen gdb are enough to ride the source code.

Vim and gdb are the most difficult tools to learn and the learning curve is steep. But after learning well, you will be able to keep the cloud open to the moon.

This article permanently updates the link address:

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.