Call Graph)

Source: Internet
Author: User
Tags perl script

Codeviz is understanding the Linux Virtual Memory Manager (at Amazon, at the end of the page) author Mel Gorman wrote an open source tool to analyze the function call relationship in C/C ++ source code (similar open source software includes Egypt and NCC ). The basic principle is to patch GCC so that it can dump the call graph of the function during each source file during compilation, and then collect and sort out the call relationship using the Perl script, transfer the image to graphviz to draw the image.

Codeviz was originally a small tool used by the author to analyze the source code of Linux virtual memory. Now it basically supports C ++, the latest version 1.0.9 can be compiled and used smoothly in Windows + cygwin :). Note: 1) download the source code gcc-3.4.1.tar.gz for GCC 3.4.1 to the codeviz-1.0.9/compilers, 2) install the patch program (which belongs to the utils class), 3) download from the http://www.graphviz.org and install graphviz 2.6.

I used codeviz to analyze the first sample program in embedded real-time operating system uC/OS-II (second edition), as follows:

1. Find a way for GCC to compile the source code of uC/OS 2.52 and the sample program. Each C source file generates the. C. cdepn file. Compile (Parameters -c.

2. Call genfull to generate full. graph. This file records the location of all functions in the source code and the call relationship between them.

3. Use gengraph to generate the call relationships of functions I care about.

First, analyze main ():

1.gengraph --output-type gif -f main
Analyze the Call Graph of main (), and the figure is as follows. The essentials are not displayed:

2.gengraph --output-type gif -f main -s OSInit
Do not care about the internal implementation details (parameters) of osinit () -s) To display it as a node. The figure below is a bit messy, but it is much better:

3.gengraph --output-type gif -f main -s OSInit -i "OSCPUSaveSR;OSCPURestoreSR"
Basically, each function has code to enter/exit the critical section. -i). The figure is as follows, which is clear:

4.gengraph --output-type gif -f main -s "OSInit;OSSemCreate" -i "OSCPUSaveSR;OSCPURestoreSR" -k
The internal details of ossemcreate () do not seem to concern, but keep the intermediate file sub. Graph (Parameter -k), As shown in the following figure,

5.dot -Tgif -o main.gif sub.graph
Modify sub. Graph to make the graph conform to the function call sequence. The final figure is shown below. You don't need to read the code when you do this :)

Next, analyze the call relationship of ostimedly:

Gengraph -- output-type GIF-r-F ostimedly

Check which functions call ostimedly (), parameter-R, task (), and taskstart () are user-written functions:

Finally, let's see which functions are directly called by task:

Gengraph -- output-type GIF-D 1-F task

Only the first-layer call starting from the task (parameter-D 1 ):

When analyzing the source code, print these images at hand and take notes on them, which is very convenient.

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.