[Go] using Codeviz to generate a C + + function call graph

Source: Internet
Author: User
Tags lua install perl perl script ocaml

Operating environment: Ubuntu 11.04 under virtual machine

Combined with the Graphviz tool, you can use Codeviz to generate an intuitive and beautiful call graph between C + + program functions.

1, installation Graphviz

Before you install Codeviz, you must install the tool dot on which it depends, or you will not be able to complete the./configure operation and prompt for the following error message:

Checking for dot...not foundfatal:the program dot is not in your path. This was probably available for your distribution and the       Graphviz package. Install This before running configure again

You can either download the latest version of the Graphviz installer from http://www.graphviz.org/Download_linux_ubuntu.php manually or install it automatically using the following command:

$ sudo apt-get install Graphviz graphviz-dev graphviz-doc

Brief description of the program:

Graphviz-rich set of graph drawing toolsgraphviz-dev-transitional package for Graphviz-dev Renamegraphviz-doc-additi Onal documentation for Graphviz

To install the relevant library:

$ sudo apt-get install libgv-*

A brief description of the relevant library:

Libgv-guile-guile bindings for Graphvizlibgv-lua-lua bindings for Graphvizlibgv-ocaml-ocaml bindings for Graphvizlib Gv-perl-perl bindings for GRAPHVIZLIBGV-PHP5-PHP5 bindings for Graphvizlibgv-python-python bindings for GRAPHVIZLIBG V-ruby-ruby bindings for GRAPHVIZLIBGV-TCL-TCL bindings for Graphviz

2, Installation Codeviz

Download Codeviz installation package codeviz-1.0.10.tar.gz from http://www.skynet.ie/~mel/projects/codeviz/and from FTP://FTP.GNU.ORG/PUB/GNU/GCC /gcc-3.4.6 Download the GCC source package gcc-3.4.6.tar.gz and copy them to the same directory under Ubuntu.

Then unzip the Codeviz installation package and copy the gcc-3.4.6.tar.gz to the Codeviz-1.0.10/compiler directory:

$ tar zvxf codeviz-1.0.10.tar.gz$ CD codeviz-1.0.10/$ CP. /gcc-3.4.6.tar.gz compilers/  //You can also skip this step and let NcFTP (Ubuntu is not installed by default) download gcc-3.4.6.tar.gz$ automatically during make execution./configure

Patched GCC is installed by default in the/usr/local/gccgraph directory.

When installing GCC, you need to install the following libraries

Apt-get Install Libgmp10-dev Libmpfr-dev Libmpc-dev

Before you execute make, create a linked file with the following command:

$ sudo ln-sf/usr/include/asm-generic//usr/include/asm

Otherwise, the error message for the Asm/errno.h file is not found in the/usr/include/linux/errno.h file during make execution and the compilation is aborted.

Follow the make and make install command to create a patched gcc compiler in the/usr/local/gccgraph directory and create two Perl script Genfull and Gengraph in the/usr/local/bin directory.

$ make$ sudo make install

3, the use of Codeviz examples

The example source code is as follows:

/* test.c */#include <stdio.h> #include <stdlib.h> #include <string.h>void func (char *p) {    p = (char *) malloc (20);} int main (void) {    char *str = NULL;    Func (str);    strcpy (str, "Hello World");    printf ("String is%s\n", str);    return 0;}

Compiling the test.c with the compiler you just generated will generate a TEST.C.CDEPN file accordingly.

$/USR/LOCAL/GCCGRAPH/BIN/GCC test.c

TEST.C.CDEPN File Information:

F {func} {test.c:6}c {func} {Test.c:7} {malloc}f {main} {test.c:11}c {main} {test.c:18} {printf}c {main} {test.c:16} {STRC Py}c {main} {test.c:14} {func}

Then execute the Genfull script to generate the full.graph.

$ genfull

Full.graph File Information:

digraph fullgraph {node [Fontname=helvetica, fontsize=12]; Func "[label=" Func\ntest.c:5: "];" Main "[Label=" Main\ntest.c:10: "];" Func "--" malloc "[label=" Test.c:7 "];" Main "and" Func "[label=" test.c:14 "];" Main "--" strcpy "[label=" test.c:16 "];" Main "and" printf "[label=" test.c:18 "];}

Finally executes the gengraph generated function relationship call graph.

$ gengraph-f Main--output-type "PNG"


-f Specifies the topmost function,--output-type the output format of the specified picture.

Refer to the man manual for detailed instructions on Genfull and gengraph, and use the following commands to view:

$ sudo apt-get install Perl-doc//must be first installed perl-doc$ genfull--man$ gengraph--man

Also reference:
http://blog.csdn.net/solstice/article/details/486788

Codeviz is the author of Understanding the Linux Virtual Memory Manager (at Amazon, at the end of the page), and Mel Gorman writes an open that analyzes the function call relationships in C + + source code The source tool (similar to the open source software has Egypt, NCC). The basic principle is to give GCC a patch so that it dumps the call graph of the function at compile time for each source file, and then uses the Perl script to collect and organize the invocation relationship and transfer it to the Graphviz drawing.

Codeviz was originally used to analyze the Linux virtual memory source of a small tool to write, and now basically support the C + + language, the latest 1.0.9 version can be successfully compiled under Windows + Cygwin:). Note that: 1) Download GCC 3.4.1 Source gcc-3.4.1.tar.gz put to codeviz-1.0.9/compilers,2) Install patch program (belongs to Utils Class), 3) from HTTP://WWW.GRAPHVIZ.O RG Download and install Graphviz 2.6.

I used Codeviz to analyze the first example program in embedded RTOs uc/os-ii (second edition), with the following steps:

1. Find a way to enable GCC to compile Uc/os 2.52 and sample program source code, each C source file generated for the. c.cdepn file. As long as the compilation (Parameters -c ) on the line, no connection.

2. Call Genfull to generate Full.graph, which records the location of all functions in the source code and the invocation relationship between them.

3. Use Gengraph to generate call relationships for the functions I care about.

First analyze Main ():

1.gengraph --output-type gif -f main
To analyze the call graph of main (), we get the following figure, which shows the essentials:

2.gengraph --output-type gif -f main -s OSInit
Temporarily does not care about the internal implementation details (parameters) of Osinit () -s , so that it appears as a node. Get the figure below, a bit messy, but much better:

3.gengraph --output-type gif -f main -s OSInit -i "OSCPUSaveSR;OSCPURestoreSR"
Basically, each function will have code to enter/exit the critical section, ignoring it (parameters -i ). The figure below is basically 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 the intermediate file sub.graph (parameters) are retained -k , and the resulting figure is as follows,

5.dot -Tgif -o main.gif sub.graph
Modify the Sub.graph, so that the graph conforms to the function call order, the final figure is as follows, with this does not have to look at the code:)

Then analyze the called Relationship of Ostimedly ():

Gengraph--output-type gif-r-F ostimedly

See which functions call ostimedly (), parameter-R, Task (), and Taskstart () are user-written functions:

Finally, look at what functions the task () calls directly:

Gengraph--output-type gif-d 1-f Task

See only the first layer call from the task (parameter-D 1):

In the analysis of the source code, these graphics are printed at hand, in the above notes, it is very convenient.

[Go] using Codeviz to generate a C + + function call graph

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.